Skip to content

Commit

Permalink
clean empty staging directory
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed May 4, 2019
1 parent cc49ee0 commit 1616f43
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions backend/app/store/image/fs_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (f *FileSystem) Cleanup(ctx context.Context, ttl time.Duration) error {
return nil
}

err := filepath.Walk(f.Staging, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(f.Staging, func(fpath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
Expand All @@ -131,8 +131,10 @@ func (f *FileSystem) Cleanup(ctx context.Context, ttl time.Duration) error {
}
age := time.Since(info.ModTime())
if age > ttl {
log.Printf("[INFO] remove staging image %s, age %v", path, age)
return os.Remove(path)
log.Printf("[INFO] remove staging image %s, age %v", fpath, age)
rmErr := os.Remove(fpath)
_ = os.Remove(path.Dir(fpath)) // try to remove directory
return rmErr
}
return nil
})
Expand Down
4 changes: 4 additions & 0 deletions backend/app/store/image/fs_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"math/rand"
"os"
"path"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -268,6 +269,9 @@ func TestFsStore_Cleanup(t *testing.T) {

_, err = os.Stat(img1)
assert.NotNil(t, err, "no file on staging anymore")
_, err = os.Stat(path.Dir(img1))
assert.NotNil(t, err, "no dir %s on staging anymore", path.Dir(img1))

_, err = os.Stat(img2)
assert.NoError(t, err, "file on staging")
_, err = os.Stat(img3)
Expand Down

0 comments on commit 1616f43

Please sign in to comment.