From edbeb9e03ccf8be46cb8dbcd317aeb241215571b Mon Sep 17 00:00:00 2001 From: Ido Savion Date: Mon, 26 Aug 2024 17:24:57 +0300 Subject: [PATCH 1/2] Shrink: fix renaming on Windows Changing the behavior to support buntdb across different operating systems as the following crash encountered on windows while renaming in shrink: ``` panic: buntdb: rename mydb.tmp mydb: Access is denied ``` --- buntdb.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/buntdb.go b/buntdb.go index 4f7a6d9..0d61f7a 100644 --- a/buntdb.go +++ b/buntdb.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "os" + "runtime" "sort" "strconv" "strings" @@ -753,7 +754,7 @@ func (db *DB) Shrink() error { return err } // Any failures below here are really bad. So just panic. - if err := os.Rename(tmpname, fname); err != nil { + if err := renameFile(tmpname, fname); err != nil { panicErr(err) } db.file, err = os.OpenFile(fname, os.O_CREATE|os.O_RDWR, 0666) @@ -773,6 +774,18 @@ func panicErr(err error) error { panic(fmt.Errorf("buntdb: %w", err)) } +func renameFile(src, dest string) error { + var err error + if err = os.Rename(src, dest); err != nil { + if runtime.GOOS == "windows" { + if err = os.Remove(dest); err == nil { + err = os.Rename(src, dest) + } + } + } + return err +} + // readLoad reads from the reader and loads commands into the database. // modTime is the modified time of the reader, should be no greater than // the current time.Now(). From 9fc48c30530a32256c3bf261dcb525d65aca682f Mon Sep 17 00:00:00 2001 From: Ido Savion Date: Mon, 26 Aug 2024 17:27:27 +0300 Subject: [PATCH 2/2] Test: fix small typo --- buntdb_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buntdb_test.go b/buntdb_test.go index d32584d..a05d370 100644 --- a/buntdb_test.go +++ b/buntdb_test.go @@ -46,7 +46,7 @@ func testClose(db *DB) { _ = os.RemoveAll("data.db") } -func TestBackgroudOperations(t *testing.T) { +func TestBackgroundOperations(t *testing.T) { db := testOpen(t) defer testClose(db) for i := 0; i < 1000; i++ {