Do not create db file if opened read-only

Signed-off-by: Cenk Alti <cenkalti@gmail.com>
pull/501/head
Cenk Alti 2023-05-17 15:17:51 -04:00
parent 160cd29ec3
commit 05286ad2af
No known key found for this signature in database
GPG Key ID: 2DB2EA6FD1BF1761
2 changed files with 8 additions and 1 deletions

3
db.go
View File

@ -196,6 +196,7 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
} else {
// always load free pages in write mode
db.PreLoadFreelist = true
flag |= os.O_CREATE
}
db.openFile = options.OpenFile
@ -205,7 +206,7 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
// Open data file and separate sync handler for metadata writes.
var err error
if db.file, err = db.openFile(path, flag|os.O_CREATE, mode); err != nil {
if db.file, err = db.openFile(path, flag, mode); err != nil {
_ = db.close()
return nil, err
}

View File

@ -562,6 +562,12 @@ func TestDB_Open_ReadOnly(t *testing.T) {
}
}
func TestDB_Open_ReadOnly_NoCreate(t *testing.T) {
f := filepath.Join(t.TempDir(), "db")
_, err := bolt.Open(f, 0666, &bolt.Options{ReadOnly: true})
require.ErrorIs(t, err, os.ErrNotExist)
}
// TestOpen_BigPage checks the database uses bigger pages when
// changing PageSize.
func TestOpen_BigPage(t *testing.T) {