mirror of https://github.com/etcd-io/bbolt.git
open read-only databases in read-only mode
parent
2c04100eb9
commit
019bf5b010
|
@ -45,7 +45,7 @@ func mmap(db *DB, sz int) error {
|
||||||
// Truncate and fsync to ensure file size metadata is flushed.
|
// Truncate and fsync to ensure file size metadata is flushed.
|
||||||
// https://github.com/boltdb/bolt/issues/284
|
// https://github.com/boltdb/bolt/issues/284
|
||||||
if !db.NoGrowSync {
|
if !db.NoGrowSync {
|
||||||
if err := db.file.Truncate(int64(sz)); err != nil {
|
if err := db.ops.Truncate(int64(sz)); err != nil {
|
||||||
return fmt.Errorf("file resize error: %s", err)
|
return fmt.Errorf("file resize error: %s", err)
|
||||||
}
|
}
|
||||||
if err := db.file.Sync(); err != nil {
|
if err := db.file.Sync(); err != nil {
|
||||||
|
|
|
@ -29,7 +29,7 @@ func funlock(f *os.File) error {
|
||||||
// Based on: https://github.com/edsrzf/mmap-go
|
// Based on: https://github.com/edsrzf/mmap-go
|
||||||
func mmap(db *DB, sz int) error {
|
func mmap(db *DB, sz int) error {
|
||||||
// Truncate the database to the size of the mmap.
|
// Truncate the database to the size of the mmap.
|
||||||
if err := db.file.Truncate(int64(sz)); err != nil {
|
if err := db.ops.Truncate(int64(sz)); err != nil {
|
||||||
return fmt.Errorf("truncate: %s", err)
|
return fmt.Errorf("truncate: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
53
db.go
53
db.go
|
@ -102,8 +102,11 @@ type DB struct {
|
||||||
statlock sync.RWMutex // Protects stats access.
|
statlock sync.RWMutex // Protects stats access.
|
||||||
|
|
||||||
ops struct {
|
ops struct {
|
||||||
writeAt func(b []byte, off int64) (n int, err error)
|
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.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Path returns the path to currently open database file.
|
// Path returns the path to currently open database file.
|
||||||
|
@ -137,21 +140,37 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
|
||||||
db.MaxBatchSize = DefaultMaxBatchSize
|
db.MaxBatchSize = DefaultMaxBatchSize
|
||||||
db.MaxBatchDelay = DefaultMaxBatchDelay
|
db.MaxBatchDelay = DefaultMaxBatchDelay
|
||||||
|
|
||||||
|
// Get file stats.
|
||||||
|
s, err := os.Stat(path)
|
||||||
|
flag := os.O_RDWR
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
return nil, err
|
||||||
|
} else if err == nil && (s.Mode().Perm()&0222) == 0 {
|
||||||
|
// remove www from mode as well.
|
||||||
|
mode ^= (mode & 0222)
|
||||||
|
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.
|
// Open data file and separate sync handler for metadata writes.
|
||||||
db.path = path
|
db.path = path
|
||||||
|
if db.file, err = os.OpenFile(db.path, flag|os.O_CREATE, mode); err != nil {
|
||||||
var err error
|
|
||||||
if db.file, err = os.OpenFile(db.path, os.O_RDWR|os.O_CREATE, mode); err != nil {
|
|
||||||
_ = db.close()
|
_ = db.close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lock file so that other processes using Bolt cannot use the database
|
// No need to lock read-only file.
|
||||||
// at the same time. This would cause corruption since the two processes
|
if !db.readOnly {
|
||||||
// would write meta pages and free pages separately.
|
db.ops.Truncate = db.file.Truncate
|
||||||
if err := flock(db.file, options.Timeout); err != nil {
|
// Lock file so that other processes using Bolt cannot use the database
|
||||||
_ = db.close()
|
// at the same time. This would cause corruption since the two processes
|
||||||
return nil, err
|
// would write meta pages and free pages separately.
|
||||||
|
if err := flock(db.file, options.Timeout); err != nil {
|
||||||
|
_ = db.close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default values for test hooks
|
// Default values for test hooks
|
||||||
|
@ -359,8 +378,11 @@ func (db *DB) close() error {
|
||||||
|
|
||||||
// Close file handles.
|
// Close file handles.
|
||||||
if db.file != nil {
|
if db.file != nil {
|
||||||
// Unlock the file.
|
// No need to unlock read-only file.
|
||||||
_ = funlock(db.file)
|
if !db.readOnly {
|
||||||
|
// Unlock the file.
|
||||||
|
_ = funlock(db.file)
|
||||||
|
}
|
||||||
|
|
||||||
// Close the file descriptor.
|
// Close the file descriptor.
|
||||||
if err := db.file.Close(); err != nil {
|
if err := db.file.Close(); err != nil {
|
||||||
|
@ -426,6 +448,9 @@ func (db *DB) beginTx() (*Tx, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) beginRWTx() (*Tx, error) {
|
func (db *DB) beginRWTx() (*Tx, error) {
|
||||||
|
if db.readOnly {
|
||||||
|
return nil, ErrDatabaseReadOnly
|
||||||
|
}
|
||||||
// Obtain writer lock. This is released by the transaction when it closes.
|
// Obtain writer lock. This is released by the transaction when it closes.
|
||||||
// This enforces only one writer transaction at a time.
|
// This enforces only one writer transaction at a time.
|
||||||
db.rwlock.Lock()
|
db.rwlock.Lock()
|
||||||
|
@ -622,6 +647,10 @@ func (db *DB) allocate(count int) (*page, error) {
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) IsReadOnly() bool {
|
||||||
|
return db.readOnly
|
||||||
|
}
|
||||||
|
|
||||||
// Options represents the options that can be set when opening a database.
|
// Options represents the options that can be set when opening a database.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
// Timeout is the amount of time to wait to obtain a file lock.
|
// Timeout is the amount of time to wait to obtain a file lock.
|
||||||
|
|
55
db_test.go
55
db_test.go
|
@ -224,6 +224,61 @@ func TestDB_Open_FileTooSmall(t *testing.T) {
|
||||||
equals(t, errors.New("file size too small"), err)
|
equals(t, errors.New("file size too small"), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure that a database can be opened in read-only mode.
|
||||||
|
func TestOpen_ReadOnly(t *testing.T) {
|
||||||
|
var bucket = []byte(`bucket`)
|
||||||
|
var key = []byte(`key`)
|
||||||
|
var value = []byte(`value`)
|
||||||
|
path := tempfile()
|
||||||
|
defer os.Remove(path)
|
||||||
|
db, err := bolt.Open(path, 0666, nil)
|
||||||
|
ok(t, db.Update(func(tx *bolt.Tx) error {
|
||||||
|
b, err := tx.CreateBucket(bucket)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return b.Put(key, value)
|
||||||
|
}))
|
||||||
|
assert(t, db != nil, "")
|
||||||
|
assert(t, !db.IsReadOnly(), "")
|
||||||
|
ok(t, err)
|
||||||
|
ok(t, db.Close())
|
||||||
|
// Make it read-only.
|
||||||
|
ok(t, os.Chmod(path, 0444))
|
||||||
|
// Open again.
|
||||||
|
db0, err := bolt.Open(path, 0666, nil)
|
||||||
|
ok(t, err)
|
||||||
|
defer db0.Close()
|
||||||
|
// And again.
|
||||||
|
db1, err := bolt.Open(path, 0666, nil)
|
||||||
|
ok(t, err)
|
||||||
|
defer db1.Close()
|
||||||
|
for _, db := range []*bolt.DB{db0, db1} {
|
||||||
|
// Verify is is in read only mode indeed.
|
||||||
|
assert(t, db.IsReadOnly(), "")
|
||||||
|
assert(t,
|
||||||
|
bolt.ErrDatabaseReadOnly == db.Update(func(*bolt.Tx) error {
|
||||||
|
panic(`should never get here`)
|
||||||
|
}),
|
||||||
|
"")
|
||||||
|
_, err = db.Begin(true)
|
||||||
|
assert(t, bolt.ErrDatabaseReadOnly == err, "")
|
||||||
|
// Verify the data.
|
||||||
|
ok(t, db.View(func(tx *bolt.Tx) error {
|
||||||
|
b := tx.Bucket(bucket)
|
||||||
|
if b == nil {
|
||||||
|
return fmt.Errorf("expected bucket `%s`", string(bucket))
|
||||||
|
}
|
||||||
|
got := string(b.Get(key))
|
||||||
|
expected := string(value)
|
||||||
|
if got != expected {
|
||||||
|
return fmt.Errorf("expected `%s`, got `%s`", expected, got)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO(benbjohnson): Test corruption at every byte of the first two pages.
|
// TODO(benbjohnson): Test corruption at every byte of the first two pages.
|
||||||
|
|
||||||
// Ensure that a database cannot open a transaction when it's not open.
|
// Ensure that a database cannot open a transaction when it's not open.
|
||||||
|
|
|
@ -36,6 +36,10 @@ var (
|
||||||
// ErrTxClosed is returned when committing or rolling back a transaction
|
// ErrTxClosed is returned when committing or rolling back a transaction
|
||||||
// that has already been committed or rolled back.
|
// that has already been committed or rolled back.
|
||||||
ErrTxClosed = errors.New("tx closed")
|
ErrTxClosed = errors.New("tx closed")
|
||||||
|
|
||||||
|
// ErrDatabaseReadOnly is returned when a mutating transaction is started on a
|
||||||
|
// read-only database.
|
||||||
|
ErrDatabaseReadOnly = errors.New("database is in read-only mode")
|
||||||
)
|
)
|
||||||
|
|
||||||
// These errors can occur when putting or deleting a value or a bucket.
|
// These errors can occur when putting or deleting a value or a bucket.
|
||||||
|
|
Loading…
Reference in New Issue