Skip to content

Commit 2c24d20

Browse files
authored
Merge pull request #21 from ahouene/fs-fsync
Call fsync from FS backend
2 parents 8c0d104 + 269e833 commit 2c24d20

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

backends/fs/fs.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ func (b *Backend) Store(ctx context.Context, name string, data []byte) error {
7272
}
7373
fullPath := filepath.Join(b.rootPath, name)
7474
tmpPath := fullPath + ".tmp" // ignored by List()
75-
if err := os.WriteFile(tmpPath, data, 0o644); err != nil {
75+
if err := writeFile(tmpPath, data); err != nil {
76+
return err
77+
}
78+
if err := syncDir(b.rootPath); err != nil {
7679
return err
7780
}
7881
return os.Rename(tmpPath, fullPath)
@@ -123,3 +126,38 @@ func init() {
123126
return New(opt)
124127
})
125128
}
129+
130+
func writeFile(name string, data []byte) error {
131+
f, err := os.Create(name)
132+
if err != nil {
133+
return err
134+
}
135+
defer f.Close()
136+
if _, err = f.Write(data); err != nil {
137+
return err
138+
}
139+
if err = f.Sync(); err != nil {
140+
return err
141+
}
142+
return nil
143+
}
144+
145+
func syncDir(name string) error {
146+
dir, err := os.Open(name)
147+
if err != nil {
148+
return err
149+
}
150+
info, err := dir.Stat()
151+
if err != nil {
152+
_ = dir.Close()
153+
return err
154+
}
155+
if !info.IsDir() {
156+
_ = dir.Close()
157+
return fmt.Errorf("%s: not a dir", name)
158+
}
159+
if err := dir.Sync(); err != nil {
160+
return err
161+
}
162+
return dir.Close()
163+
}

0 commit comments

Comments
 (0)