Skip to content

Commit

Permalink
Merge pull request #206 from moov-io/sync-filesystem-storage
Browse files Browse the repository at this point in the history
storage: fsync writes
  • Loading branch information
adamdecaf authored Oct 19, 2023
2 parents 5fe2de2 + 53c3529 commit b65191c
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions internal/storage/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storage

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -76,7 +77,7 @@ func (fs *filesystem) ReplaceFile(oldpath, newpath string) error {

// file doesn't exist, so write newpath
if _, err := os.Stat(oldpath); err != nil && os.IsNotExist(err) {
return os.WriteFile(newpath, nil, 0600)
return write(newpath, nil)
}

// move the existing file
Expand Down Expand Up @@ -111,5 +112,20 @@ func (fs *filesystem) WriteFile(path string, contents []byte) error {
return err
}

return os.WriteFile(filepath.Join(dir, path), contents, 0600)
return write(filepath.Join(dir, path), contents)
}

func write(where string, data []byte) error {
fd, err := os.OpenFile(where, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("creating %s failed: %w", where, err)
}
defer fd.Close()

_, err = fd.Write(data)
if err != nil {
return fmt.Errorf("writing %s failed: %w", where, err)
}

return fd.Sync()
}

0 comments on commit b65191c

Please sign in to comment.