Merge pull request from gyuho/c1

db.go: clean up file descriptor on init/read failures
pull/114/head
Gyuho Lee 2018-03-17 17:15:26 -07:00 committed by GitHub
commit af9db2027c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 0 deletions

4
db.go
View File

@ -213,10 +213,13 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
// Initialize the database if it doesn't exist.
if info, err := db.file.Stat(); err != nil {
_ = db.close()
return nil, err
} else if info.Size() == 0 {
// Initialize new files with meta pages.
if err := db.init(); err != nil {
// clean up file descriptor on initialization fail
_ = db.close()
return nil, err
}
} else {
@ -236,6 +239,7 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
db.pageSize = int(m.pageSize)
}
} else {
_ = db.close()
return nil, ErrInvalid
}
}