Skip to content

Commit 3daff4e

Browse files
authored
Merge pull request #120 from idosavion/ido/fix-crash-on-windows
ido/fix crash on windows
2 parents e0e630f + 9fc48c3 commit 3daff4e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

buntdb.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"os"
13+
"runtime"
1314
"sort"
1415
"strconv"
1516
"strings"
@@ -753,7 +754,7 @@ func (db *DB) Shrink() error {
753754
return err
754755
}
755756
// Any failures below here are really bad. So just panic.
756-
if err := os.Rename(tmpname, fname); err != nil {
757+
if err := renameFile(tmpname, fname); err != nil {
757758
panicErr(err)
758759
}
759760
db.file, err = os.OpenFile(fname, os.O_CREATE|os.O_RDWR, 0666)
@@ -773,6 +774,18 @@ func panicErr(err error) error {
773774
panic(fmt.Errorf("buntdb: %w", err))
774775
}
775776

777+
func renameFile(src, dest string) error {
778+
var err error
779+
if err = os.Rename(src, dest); err != nil {
780+
if runtime.GOOS == "windows" {
781+
if err = os.Remove(dest); err == nil {
782+
err = os.Rename(src, dest)
783+
}
784+
}
785+
}
786+
return err
787+
}
788+
776789
// readLoad reads from the reader and loads commands into the database.
777790
// modTime is the modified time of the reader, should be no greater than
778791
// the current time.Now().

buntdb_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func testClose(db *DB) {
4646
_ = os.RemoveAll("data.db")
4747
}
4848

49-
func TestBackgroudOperations(t *testing.T) {
49+
func TestBackgroundOperations(t *testing.T) {
5050
db := testOpen(t)
5151
defer testClose(db)
5252
for i := 0; i < 1000; i++ {

0 commit comments

Comments
 (0)