Skip to content

Commit

Permalink
Replacing os.Create+os.Chmod with a call to os.Openfile that allows s…
Browse files Browse the repository at this point in the history
…etting the file permissions in a single call.
  • Loading branch information
ctrix committed Nov 10, 2021
1 parent dfa9208 commit bad3346
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ func (l *Log) load() error {
})
l.firstIndex = 1
l.lastIndex = 0
l.sfile, err = os.Create(l.segments[0].path)
if err == nil {
l.sfile.Chmod(l.opts.FilePerms)
}
l.sfile, err = os.OpenFile(l.segments[0].path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, l.opts.FilePerms)
return err
}
// Open existing log. Clean up log if START of END segments exists.
Expand Down Expand Up @@ -346,11 +343,10 @@ func (l *Log) cycle() error {
path: filepath.Join(l.path, segmentName(l.lastIndex+1)),
}
var err error
l.sfile, err = os.Create(s.path)
l.sfile, err = os.OpenFile(s.path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, l.opts.FilePerms)
if err != nil {
return err
}
l.sfile.Chmod(l.opts.FilePerms)
l.segments = append(l.segments, s)
return nil
}
Expand Down Expand Up @@ -733,11 +729,10 @@ func (l *Log) truncateFront(index uint64) (err error) {
// Create a temp file contains the truncated segment.
tempName := filepath.Join(l.path, "TEMP")
err = func() error {
f, err := os.Create(tempName)
f, err := os.OpenFile(tempName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, l.opts.FilePerms)
if err != nil {
return err
}
f.Chmod(l.opts.FilePerms)
defer f.Close()
if _, err := f.Write(ebuf); err != nil {
return err
Expand Down Expand Up @@ -840,11 +835,10 @@ func (l *Log) truncateBack(index uint64) (err error) {
// Create a temp file contains the truncated segment.
tempName := filepath.Join(l.path, "TEMP")
err = func() error {
f, err := os.Create(tempName)
f, err := os.OpenFile(tempName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, l.opts.FilePerms)
if err != nil {
return err
}
f.Chmod(l.opts.FilePerms)
defer f.Close()
if _, err := f.Write(ebuf); err != nil {
return err
Expand Down

0 comments on commit bad3346

Please sign in to comment.