Call fdatasync/fsync after writing out non-meta pages

This avoids a case where writes can be reordered so meta page is
written before a page it refers to, potentially causing a corrupt
database after a power loss or kernel crash.
master
Tommi Virtanen 2014-03-22 20:45:53 -07:00
parent 6d6303a0a2
commit 5ce378b046
3 changed files with 23 additions and 0 deletions

10
sync_linux.go Normal file
View File

@ -0,0 +1,10 @@
package bolt
import (
"os"
"syscall"
)
func fdatasync(f *os.File) error {
return syscall.Fdatasync(int(f.Fd()))
}

10
sync_std.go Normal file
View File

@ -0,0 +1,10 @@
// +build !linux
package bolt
import "os"
// Fall back to syncing metadata too.
func fdatasync(f *os.File) error {
return f.Sync()
}

3
tx.go
View File

@ -329,6 +329,9 @@ func (t *Tx) write() error {
return err
}
}
if err := fdatasync(t.db.file); err != nil {
return err
}
// Clear out page cache.
t.pages = make(map[pgid]*page)