From 56cbf58e956e4d82d3ac1275a690b4ae2516e686 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 20 Nov 2026 12:06:11 +0800 Subject: [PATCH] fix: preserve selected SQLite snapshot names --- CHANGELOG.md | 2 ++ cache/sqlite.go | 7 +++++-- cache/sqlite_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e80514..13e0771 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Preserve literal SQLite snapshot names so surrounding whitespace does not redirect replacement to a neighboring database bundle; reject ambiguous destination names on Windows. + - Preserve whitespace in FTS table names during optimization so a similarly named table is not modified instead. - Keep SQLite sidecar cleanup in the selected mirror root when its path contains surrounding whitespace, rather than deleting sidecars from a neighboring directory. diff --git a/cache/sqlite.go b/cache/sqlite.go index 3e1d8d0..41b0e04 100644 --- a/cache/sqlite.go +++ b/cache/sqlite.go @@ -24,7 +24,7 @@ type SQLiteSnapshot struct { } // SnapshotSQLite copies a SQLite database and its optional WAL/SHM sidecars. -// SourcePath and DestinationDir retain literal whitespace. Ambiguous trailing +// SourcePath, DestinationDir and Name retain literal whitespace. Ambiguous trailing // spaces or dots in Windows path components are rejected. // The caller owns DestinationDir and decides whether snapshots are temporary // or retained. The caller must exclude concurrent destination readers/writers @@ -50,13 +50,16 @@ func SnapshotSQLite(opts SQLiteSnapshotOptions) (SQLiteSnapshot, error) { if !info.Mode().IsRegular() { return SQLiteSnapshot{}, fmt.Errorf("sqlite source is not a regular file: %s", source) } - name := strings.TrimSpace(opts.Name) + name := opts.Name if name == "" { name = filepath.Base(source) } if filepath.Base(name) != name || name == "." || name == ".." { return SQLiteSnapshot{}, fmt.Errorf("sqlite snapshot name must be a file name: %q", name) } + if err := validateSQLitePath(filepath.Join(destination, name)); err != nil { + return SQLiteSnapshot{}, err + } if err := os.MkdirAll(destination, 0o700); err != nil { return SQLiteSnapshot{}, fmt.Errorf("create sqlite snapshot dir: %w", err) } diff --git a/cache/sqlite_test.go b/cache/sqlite_test.go index 789babf..3be319b 100644 --- a/cache/sqlite_test.go +++ b/cache/sqlite_test.go @@ -14,6 +14,49 @@ import ( "github.com/openclaw/crawlkit/store" ) +func TestSnapshotSQLitePreservesLiteralNames(t *testing.T) { + for _, name := range []string{" archive.db", "archive.db ", " "} { + t.Run(name, func(t *testing.T) { + source := filepath.Join(t.TempDir(), "source.db") + destination := t.TempDir() + for _, suffix := range []string{"", "-wal", "-shm"} { + if err := os.WriteFile(source+suffix, []byte("captured"+suffix), 0o600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(destination, "archive.db")+suffix, []byte("retained"+suffix), 0o600); err != nil { + t.Fatal(err) + } + } + snap, err := SnapshotSQLite(SQLiteSnapshotOptions{SourcePath: source, DestinationDir: destination, Name: name}) + ambiguous := runtime.GOOS == "windows" && strings.HasSuffix(name, " ") + if ambiguous { + if err == nil || !strings.Contains(err.Error(), "ambiguous") { + t.Errorf("expected ambiguous Windows name rejection, got %v", err) + } + } else { + if err != nil { + t.Fatal(err) + } + if want := filepath.Join(destination, name); snap.Path != want { + t.Errorf("snapshot path = %q, want %q", snap.Path, want) + } + for _, suffix := range []string{"", "-wal", "-shm"} { + data, err := os.ReadFile(filepath.Join(destination, name) + suffix) + if err != nil || string(data) != "captured"+suffix { + t.Errorf("literal snapshot %q = %q, %v", suffix, data, err) + } + } + } + for _, suffix := range []string{"", "-wal", "-shm"} { + data, err := os.ReadFile(filepath.Join(destination, "archive.db") + suffix) + if err != nil || string(data) != "retained"+suffix { + t.Errorf("neighbor %q changed: %q, %v", suffix, data, err) + } + } + }) + } +} + func TestSnapshotSQLiteReusedDestinationDropsStaleWAL(t *testing.T) { ctx := context.Background() source := filepath.Join(t.TempDir(), "source.db")