Skip to content

Commit

Permalink
PBM-1329 Ensure temp files are always removed
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielOliverRJ authored and defbin committed Aug 5, 2024
1 parent ceff3ea commit 64b3a06
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions pbm/storage/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +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+".tmp")
finalpath := 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 @@ -105,13 +102,18 @@ func (fs *FS) Save(name string, data io.Reader, _ int64) error {
}

err = fw.Sync()
if err != nil {
return errors.Wrapf(err, "sync file <%s>", filepath)
}
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 = fw.Close()
err := WriteSync(filepath, data)
if err != nil {
return errors.Wrapf(err, "close file <%s>", filepath)
os.Remove(filepath)
return errors.Wrapf(err, "write-sync %s", path.Dir(filepath))
}

err = os.Rename(filepath, finalpath)
Expand Down Expand Up @@ -188,30 +190,11 @@ func (fs *FS) Copy(src, dst string) error {

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

to, err := os.Create(destFilename)
if err != nil {
return errors.Wrap(err, "create dst")
}
defer to.Close()

_, err = io.Copy(to, from)
if err != nil {
return errors.Wrapf(err, "copy to <%s>", destFilename)
}

err = to.Sync()
if err != nil {
return errors.Wrapf(err, "sync file <%s>", destFilename)
}

err = to.Close()
err = WriteSync(destFilename, from)
if err != nil {
return errors.Wrapf(err, "close file <%s>", destFilename)
os.Remove(destFilename)
return errors.Wrapf(err, "write-sync %s", path.Dir(destFilename))
}

err = os.Rename(destFilename, finalFilename)
Expand Down

0 comments on commit 64b3a06

Please sign in to comment.