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

PBM-1329 Write to temporary file name and sync before renaming #954

Merged
merged 2 commits into from
Aug 5, 2024
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
39 changes: 26 additions & 13 deletions pbm/storage/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ func (*FS) Type() storage.Type {
return storage.Filesystem
}

func (fs *FS) Save(name string, data io.Reader, _ int64) error {
filepath := path.Join(fs.root, name)

func WriteSync(filepath string, data io.Reader) error {
err := os.MkdirAll(path.Dir(filepath), os.ModeDir|0o755)
if err != nil {
return errors.Wrapf(err, "create path %s", path.Dir(filepath))
Expand All @@ -103,7 +101,23 @@ func (fs *FS) Save(name string, data io.Reader, _ int64) error {
return errors.Wrapf(err, "copy file <%s>", filepath)
}

return errors.Wrap(fw.Sync(), "write to file")
err = fw.Sync()
return errors.Wrapf(err, "sync file <%s>", filepath)
}


func (fs *FS) Save(name string, data io.Reader, _ int64) error {
filepath := path.Join(fs.root, name+".tmp")
finalpath := path.Join(fs.root, name)

err := WriteSync(filepath, data)
if err != nil {
os.Remove(filepath)
return errors.Wrapf(err, "write-sync %s", path.Dir(filepath))
}

err = os.Rename(filepath, finalpath)
return errors.Wrapf(err, "rename <%s> to <%s>", filepath, finalpath)
}

func (fs *FS) SourceReader(name string) (io.ReadCloser, error) {
Expand Down Expand Up @@ -174,18 +188,17 @@ func (fs *FS) Copy(src, dst string) error {
return errors.Wrap(err, "open src")
}

destFilename := path.Join(fs.root, dst)
err = os.MkdirAll(path.Dir(destFilename), os.ModeDir|0o755)
if err != nil {
return errors.Wrap(err, "create dst dir")
}
destFilename := path.Join(fs.root, dst+".tmp")
finalFilename := path.Join(fs.root, dst)

to, err := os.Create(destFilename)
err = WriteSync(destFilename, from)
if err != nil {
return errors.Wrap(err, "create dst")
os.Remove(destFilename)
return errors.Wrapf(err, "write-sync %s", path.Dir(destFilename))
}
_, err = io.Copy(to, from)
return err

err = os.Rename(destFilename, finalFilename)
return errors.Wrapf(err, "rename <%s> to <%s>", destFilename, finalFilename)
}

// Delete deletes given file from FS.
Expand Down
Loading