diff options
| author | mhsanaei <ho3ein.sanaei@gmail.com> | 2025-10-14 23:03:17 +0300 |
|---|---|---|
| committer | mhsanaei <ho3ein.sanaei@gmail.com> | 2025-10-14 23:03:17 +0300 |
| commit | d8523bbdac4a65e3e33bca6d1a50a58f33804f52 (patch) | |
| tree | aed89949c074e3fa25e82c2881c967b620a560ef /web | |
| parent | 8afa39144ec68714aa584ad34925dde4bbdb14ca (diff) | |
fix(import): prevent sqlite disk I/O error by validating temp DB then swapping
Diffstat (limited to 'web')
| -rw-r--r-- | web/service/server.go | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/web/service/server.go b/web/service/server.go index eb261c88..b7cfc3a7 100644 --- a/web/service/server.go +++ b/web/service/server.go @@ -942,13 +942,26 @@ func (s *ServerService) ImportDB(file multipart.File) error { return common.NewErrorf("Error saving db: %v", err) } - // Check if we can init the db or not - if err = database.InitDB(tempPath); err != nil { - return common.NewErrorf("Error checking db: %v", err) + // Close temp file before opening via sqlite + if err = tempFile.Close(); err != nil { + return common.NewErrorf("Error closing temporary db file: %v", err) } + tempFile = nil - // Stop Xray - s.StopXrayService() + // Validate integrity (no migrations / side effects) + if err = database.ValidateSQLiteDB(tempPath); err != nil { + return common.NewErrorf("Invalid or corrupt db file: %v", err) + } + + // Stop Xray (ignore error but log) + if errStop := s.StopXrayService(); errStop != nil { + logger.Warningf("Failed to stop Xray before DB import: %v", errStop) + } + + // Close existing DB to release file locks (especially on Windows) + if errClose := database.CloseDB(); errClose != nil { + logger.Warningf("Failed to close existing DB before replacement: %v", errClose) + } // Backup the current database for fallback fallbackPath := fmt.Sprintf("%s.backup", config.GetDBPath()) @@ -983,7 +996,7 @@ func (s *ServerService) ImportDB(file multipart.File) error { return common.NewErrorf("Error moving db file: %v", err) } - // Migrate DB + // Open & migrate new DB if err = database.InitDB(config.GetDBPath()); err != nil { if errRename := os.Rename(fallbackPath, config.GetDBPath()); errRename != nil { return common.NewErrorf("Error migrating db and restoring fallback: %v", errRename) |
