From aa13f7f94f882fa0964b801c062217c0b9a45436 Mon Sep 17 00:00:00 2001 From: sasha-s Date: Mon, 18 May 2015 12:00:40 -0700 Subject: [PATCH] make ignoring Truncate() explicit https://github.com/boltdb/bolt/pull/371#issuecomment-103176330 --- bolt_unix.go | 4 ++-- bolt_windows.go | 8 +++++--- db.go | 7 ------- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/bolt_unix.go b/bolt_unix.go index 8107f4a..266222a 100644 --- a/bolt_unix.go +++ b/bolt_unix.go @@ -48,8 +48,8 @@ func funlock(f *os.File) error { 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 !db.NoGrowSync { - if err := db.ops.Truncate(int64(sz)); err != nil { + if !db.NoGrowSync && !db.readOnly { + if err := db.file.Truncate(int64(sz)); err != nil { return fmt.Errorf("file resize error: %s", err) } if err := db.file.Sync(); err != nil { diff --git a/bolt_windows.go b/bolt_windows.go index 783b633..8b782be 100644 --- a/bolt_windows.go +++ b/bolt_windows.go @@ -28,9 +28,11 @@ func funlock(f *os.File) error { // mmap memory maps a DB's data file. // Based on: https://github.com/edsrzf/mmap-go func mmap(db *DB, sz int) error { - // Truncate the database to the size of the mmap. - if err := db.ops.Truncate(int64(sz)); err != nil { - return fmt.Errorf("truncate: %s", err) + if !db.readOnly { + // Truncate the database to the size of the mmap. + if err := db.file.Truncate(int64(sz)); err != nil { + return fmt.Errorf("truncate: %s", err) + } } // Open a file mapping handle. diff --git a/db.go b/db.go index 5ae35cc..d1b722a 100644 --- a/db.go +++ b/db.go @@ -103,7 +103,6 @@ type DB struct { ops struct { writeAt func(b []byte, off int64) (n int, err error) - Truncate func(size int64) error } readOnly bool // Read only mode. Update()/Begin(true) would return ErrDatabaseReadOnly immediately. @@ -144,8 +143,6 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) { if options.ReadOnly { flag = os.O_RDONLY db.readOnly = true - // Ignore truncations. - db.ops.Truncate = func(int64) error { return nil } } // Open data file and separate sync handler for metadata writes. @@ -156,10 +153,6 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) { return nil, err } - if !db.readOnly { - db.ops.Truncate = db.file.Truncate - } - // Lock file so that other processes using Bolt in read-write mode cannot // use the database at the same time. This would cause corruption since // the two processes would write meta pages and free pages separately.