Skip to content

Commit

Permalink
Merge pull request #72 from ipfs/fix/ensure-close-diskusage
Browse files Browse the repository at this point in the history
fix: ensure that we close the diskusage file, even if we fail to rename it
  • Loading branch information
Stebalien authored Apr 10, 2020
2 parents 2284a52 + a63d692 commit 8e4cd22
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,11 +973,16 @@ func (fs *Datastore) writeDiskUsageFile(du int64, doSync bool) {
}

removed := false
closed := false
defer func() {
if !closed {
_ = tmp.Close()
}
if !removed {
// silence errcheck
_ = os.Remove(tmp.Name())
}

}()

toWrite := fs.storedValue
Expand All @@ -999,6 +1004,7 @@ func (fs *Datastore) writeDiskUsageFile(du int64, doSync bool) {
log.Warnw("cound not write disk usage", "error", err)
return
}
closed = true

if err := os.Rename(tmp.Name(), filepath.Join(fs.path, DiskUsageFile)); err != nil {
log.Warnw("cound not write disk usage", "error", err)
Expand Down

0 comments on commit 8e4cd22

Please sign in to comment.