Skip to content

Commit

Permalink
Merge pull request #69 from ipfs/fix/cleanup-temporary-files
Browse files Browse the repository at this point in the history
feat: put all temporary files in the same directory and clean them up
  • Loading branch information
Stebalien committed Apr 1, 2020
2 parents 149d568 + 5b09ebf commit 86e95e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
27 changes: 22 additions & 5 deletions flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ type Datastore struct {
// (see https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
diskUsage int64

path string
path string
tempPath string

shardStr string
getDir ShardFunc
Expand Down Expand Up @@ -196,7 +197,6 @@ func (o *opResult) Finish(ok bool) {
}

func Create(path string, fun *ShardIdV1) error {

err := os.Mkdir(path, 0755)
if err != nil && !os.IsExist(err) {
return err
Expand Down Expand Up @@ -238,13 +238,25 @@ func Open(path string, syncFiles bool) (*Datastore, error) {
return nil, err
}

tempPath := filepath.Join(path, ".temp")
err = os.RemoveAll(tempPath)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("failed to remove temporary directory: %w", err)
}

err = os.Mkdir(tempPath, 0755)
if err != nil {
return nil, fmt.Errorf("failed to create temporary directory: %w", err)
}

shardId, err := ReadShardFunc(path)
if err != nil {
return nil, err
}

fs := &Datastore{
path: path,
tempPath: tempPath,
shardStr: shardId.String(),
getDir: shardId.Func(),
sync: syncFiles,
Expand Down Expand Up @@ -452,7 +464,7 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
return err
}

tmp, err := ioutil.TempFile(dir, "put-")
tmp, err := fs.tempFile()
if err != nil {
return err
}
Expand Down Expand Up @@ -528,7 +540,7 @@ func (fs *Datastore) putMany(data map[datastore.Key][]byte) error {
}
dirsToSync = append(dirsToSync, dir)

tmp, err := ioutil.TempFile(dir, "put-")
tmp, err := fs.tempFile()
if err != nil {
return err
}
Expand Down Expand Up @@ -954,7 +966,7 @@ func (fs *Datastore) checkpointLoop() {
}

func (fs *Datastore) writeDiskUsageFile(du int64, doSync bool) {
tmp, err := ioutil.TempFile(fs.path, "du-")
tmp, err := fs.tempFile()
if err != nil {
log.Warnw("could not write disk usage", "error", err)
return
Expand Down Expand Up @@ -1037,6 +1049,11 @@ func (fs *Datastore) Accuracy() string {
return string(fs.storedValue.Accuracy)
}

func (fs *Datastore) tempFile() (*os.File, error) {
file, err := ioutil.TempFile(fs.tempPath, "temp-")
return file, err
}

func (fs *Datastore) walk(path string, qrb *query.ResultBuilder) error {
dir, err := os.Open(path)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions flatfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func testStorage(p *params, t *testing.T) {
return err
}
switch path {
case ".", "..", "SHARDING", flatfs.DiskUsageFile:
case ".", "..", "SHARDING", flatfs.DiskUsageFile, ".temp":
// ignore
case "_README":
_, err := ioutil.ReadFile(absPath)
Expand Down Expand Up @@ -950,9 +950,8 @@ func TestNoCluster(t *testing.T) {
tolerance := math.Floor(idealFilesPerDir * 0.25)
count := 0
for _, dir := range dirs {
if dir.Name() == flatfs.SHARDING_FN ||
dir.Name() == flatfs.README_FN ||
dir.Name() == flatfs.DiskUsageFile {
switch dir.Name() {
case flatfs.SHARDING_FN, flatfs.README_FN, flatfs.DiskUsageFile, ".temp":
continue
}
count += 1
Expand Down

0 comments on commit 86e95e6

Please sign in to comment.