Skip to content

Commit

Permalink
Merge pull request #120 from idosavion/ido/fix-crash-on-windows
Browse files Browse the repository at this point in the history
ido/fix crash on windows
  • Loading branch information
tidwall authored Sep 10, 2024
2 parents e0e630f + 9fc48c3 commit 3daff4e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion buntdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"os"
"runtime"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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)
Expand All @@ -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().
Expand Down
2 changes: 1 addition & 1 deletion buntdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down

0 comments on commit 3daff4e

Please sign in to comment.