diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2015-01-18 07:49:58 -0800 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2015-01-18 07:49:58 -0800 |
commit | 4397537d1d48bef8d79eec48424f321fb926e9fe (patch) | |
tree | 9b50ff296a3813040699a11dc9f1d4e2ce721bc2 | |
parent | Merge pull request #283 from mbertschler/master (diff) | |
parent | Add truncate() and sync() on resize. (diff) | |
download | dedo-4397537d1d48bef8d79eec48424f321fb926e9fe.tar.gz dedo-4397537d1d48bef8d79eec48424f321fb926e9fe.tar.xz |
Merge pull request #286 from benbjohnson/trunc-n-sync
Add truncate() and sync() on resize.
-rw-r--r-- | bolt_unix.go | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/bolt_unix.go b/bolt_unix.go index 95647a7..e222cfd 100644 --- a/bolt_unix.go +++ b/bolt_unix.go @@ -3,6 +3,7 @@ package bolt import ( + "fmt" "os" "syscall" "time" @@ -41,6 +42,16 @@ func funlock(f *os.File) error { // mmap memory maps a DB's data file. func mmap(db *DB, sz int) error { + // Truncate and fsync to ensure file size metadata is flushed. + // https://github.com/boltdb/bolt/issues/284 + if err := db.file.Truncate(int64(sz)); err != nil { + return fmt.Errorf("file resize error: %s", err) + } + if err := db.file.Sync(); err != nil { + return fmt.Errorf("file sync error: %s", err) + } + + // Map the data file to memory. b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED) if err != nil { return err |