Unverified Commit 86e95e60 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #69 from ipfs/fix/cleanup-temporary-files

feat: put all temporary files in the same directory and clean them up
parents 149d5680 5b09ebfb
...@@ -113,6 +113,7 @@ type Datastore struct { ...@@ -113,6 +113,7 @@ type Datastore struct {
diskUsage int64 diskUsage int64
path string path string
tempPath string
shardStr string shardStr string
getDir ShardFunc getDir ShardFunc
...@@ -196,7 +197,6 @@ func (o *opResult) Finish(ok bool) { ...@@ -196,7 +197,6 @@ func (o *opResult) Finish(ok bool) {
} }
func Create(path string, fun *ShardIdV1) error { func Create(path string, fun *ShardIdV1) error {
err := os.Mkdir(path, 0755) err := os.Mkdir(path, 0755)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
return err return err
...@@ -238,6 +238,17 @@ func Open(path string, syncFiles bool) (*Datastore, error) { ...@@ -238,6 +238,17 @@ func Open(path string, syncFiles bool) (*Datastore, error) {
return nil, err 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) shardId, err := ReadShardFunc(path)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -245,6 +256,7 @@ func Open(path string, syncFiles bool) (*Datastore, error) { ...@@ -245,6 +256,7 @@ func Open(path string, syncFiles bool) (*Datastore, error) {
fs := &Datastore{ fs := &Datastore{
path: path, path: path,
tempPath: tempPath,
shardStr: shardId.String(), shardStr: shardId.String(),
getDir: shardId.Func(), getDir: shardId.Func(),
sync: syncFiles, sync: syncFiles,
...@@ -452,7 +464,7 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error { ...@@ -452,7 +464,7 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
return err return err
} }
tmp, err := ioutil.TempFile(dir, "put-") tmp, err := fs.tempFile()
if err != nil { if err != nil {
return err return err
} }
...@@ -528,7 +540,7 @@ func (fs *Datastore) putMany(data map[datastore.Key][]byte) error { ...@@ -528,7 +540,7 @@ func (fs *Datastore) putMany(data map[datastore.Key][]byte) error {
} }
dirsToSync = append(dirsToSync, dir) dirsToSync = append(dirsToSync, dir)
tmp, err := ioutil.TempFile(dir, "put-") tmp, err := fs.tempFile()
if err != nil { if err != nil {
return err return err
} }
...@@ -954,7 +966,7 @@ func (fs *Datastore) checkpointLoop() { ...@@ -954,7 +966,7 @@ func (fs *Datastore) checkpointLoop() {
} }
func (fs *Datastore) writeDiskUsageFile(du int64, doSync bool) { func (fs *Datastore) writeDiskUsageFile(du int64, doSync bool) {
tmp, err := ioutil.TempFile(fs.path, "du-") tmp, err := fs.tempFile()
if err != nil { if err != nil {
log.Warnw("could not write disk usage", "error", err) log.Warnw("could not write disk usage", "error", err)
return return
...@@ -1037,6 +1049,11 @@ func (fs *Datastore) Accuracy() string { ...@@ -1037,6 +1049,11 @@ func (fs *Datastore) Accuracy() string {
return string(fs.storedValue.Accuracy) 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 { func (fs *Datastore) walk(path string, qrb *query.ResultBuilder) error {
dir, err := os.Open(path) dir, err := os.Open(path)
if err != nil { if err != nil {
......
...@@ -187,7 +187,7 @@ func testStorage(p *params, t *testing.T) { ...@@ -187,7 +187,7 @@ func testStorage(p *params, t *testing.T) {
return err return err
} }
switch path { switch path {
case ".", "..", "SHARDING", flatfs.DiskUsageFile: case ".", "..", "SHARDING", flatfs.DiskUsageFile, ".temp":
// ignore // ignore
case "_README": case "_README":
_, err := ioutil.ReadFile(absPath) _, err := ioutil.ReadFile(absPath)
...@@ -950,9 +950,8 @@ func TestNoCluster(t *testing.T) { ...@@ -950,9 +950,8 @@ func TestNoCluster(t *testing.T) {
tolerance := math.Floor(idealFilesPerDir * 0.25) tolerance := math.Floor(idealFilesPerDir * 0.25)
count := 0 count := 0
for _, dir := range dirs { for _, dir := range dirs {
if dir.Name() == flatfs.SHARDING_FN || switch dir.Name() {
dir.Name() == flatfs.README_FN || case flatfs.SHARDING_FN, flatfs.README_FN, flatfs.DiskUsageFile, ".temp":
dir.Name() == flatfs.DiskUsageFile {
continue continue
} }
count += 1 count += 1
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment