Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions cache/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
43 changes: 43 additions & 0 deletions cache/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading