mirror of https://github.com/tidwall/buntdb.git
Merge pull request #120 from idosavion/ido/fix-crash-on-windows
ido/fix crash on windows
This commit is contained in:
commit
3daff4e123
15
buntdb.go
15
buntdb.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -753,7 +754,7 @@ func (db *DB) Shrink() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Any failures below here are really bad. So just panic.
|
// 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)
|
panicErr(err)
|
||||||
}
|
}
|
||||||
db.file, err = os.OpenFile(fname, os.O_CREATE|os.O_RDWR, 0666)
|
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))
|
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.
|
// readLoad reads from the reader and loads commands into the database.
|
||||||
// modTime is the modified time of the reader, should be no greater than
|
// modTime is the modified time of the reader, should be no greater than
|
||||||
// the current time.Now().
|
// the current time.Now().
|
||||||
|
|
|
@ -46,7 +46,7 @@ func testClose(db *DB) {
|
||||||
_ = os.RemoveAll("data.db")
|
_ = os.RemoveAll("data.db")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBackgroudOperations(t *testing.T) {
|
func TestBackgroundOperations(t *testing.T) {
|
||||||
db := testOpen(t)
|
db := testOpen(t)
|
||||||
defer testClose(db)
|
defer testClose(db)
|
||||||
for i := 0; i < 1000; i++ {
|
for i := 0; i < 1000; i++ {
|
||||||
|
|
Loading…
Reference in New Issue