Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call fsync from FS backend #21

Merged
merged 4 commits into from
Mar 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion backends/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ func (b *Backend) Store(ctx context.Context, name string, data []byte) error {
}
fullPath := filepath.Join(b.rootPath, name)
tmpPath := fullPath + ".tmp" // ignored by List()
if err := os.WriteFile(tmpPath, data, 0o644); err != nil {
if err := writeFile(tmpPath, data); err != nil {
return err
}
if err := syncDir(b.rootPath); err != nil {
return err
}
return os.Rename(tmpPath, fullPath)
Expand Down Expand Up @@ -123,3 +126,38 @@ func init() {
return New(opt)
})
}

func writeFile(name string, data []byte) error {
f, err := os.Create(name)
if err != nil {
return err
}
defer f.Close()
if _, err = f.Write(data); err != nil {
return err
}
if err = f.Sync(); err != nil {
return err
}
return nil
}

func syncDir(name string) error {
dir, err := os.Open(name)
if err != nil {
return err
}
info, err := dir.Stat()
if err != nil {
_ = dir.Close()
return err
wojas marked this conversation as resolved.
Show resolved Hide resolved
}
if !info.IsDir() {
_ = dir.Close()
return fmt.Errorf("%s: not a dir", name)
}
if err := dir.Sync(); err != nil {
return err
}
return dir.Close()
}