Skip to content

Commit

Permalink
Use O_CREATE for bolt.Open(..) only when file doesn't exist
Browse files Browse the repository at this point in the history
+ By default, etcd-io/bbolt appends an O_CREATE flag to the flags passed in
  no matter which mode the user attempts to open the db ..
    https://github.com/etcd-io/bbolt/blob/v1.3.5/db.go#L197...L213
+ This appears to be bolt's design decision where they let you create a
  file that is only writeable by the process if it doesn't exist - even
  when it is OPEN-ed in READ-ONLY mode.
+ However, using an O_CREATE on a file that's owned by a different user
  but READ-able by all users, seems to be causing a permissions error.
    - While I cannot seem to reproduce this issue on osX, this seems to
      manifest on windows which complains ERROR_ACCESS_DENIED.

+ The proposal here is to overwrite Bolt.Options' OpenFile with a custom
  implementation that appends the O_CREATE file only if the file does
  not exist - sticking to bolt's design decision.
+ Should fix blevesearch#1623
  • Loading branch information
abhinavdangeti committed Sep 14, 2021
1 parent a74298a commit 97d3c88
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions index/scorch/scorch.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ func (s *Scorch) openBolt() error {
var rootBoltOpt = *bolt.DefaultOptions
if s.readOnly {
rootBoltOpt.ReadOnly = true
rootBoltOpt.OpenFile = func(path string, flag int, mode os.FileMode) (*os.File, error) {
// Bolt appends an O_CREATE flag regardless.
// See - https://github.com/etcd-io/bbolt/blob/v1.3.5/db.go#L210
// Use os.O_RDONLY only if path exists (#1623)
if _, err := os.Stat(path); os.IsNotExist(err) {
return os.OpenFile(path, flag, mode)
}
return os.OpenFile(path, os.O_RDONLY, mode)
}
} else {
if s.path != "" {
err := os.MkdirAll(s.path, 0700)
Expand Down

0 comments on commit 97d3c88

Please sign in to comment.