From 4e8e5fed7da01a013e90f721d70b9c2222130301 Mon Sep 17 00:00:00 2001 From: remusb Date: Sat, 9 Jun 2018 14:52:31 +0300 Subject: [PATCH] cache: clean remaining empty folders from temp upload path --- backend/cache/cache_upload_test.go | 53 ++++++++++++++++++++++++++---- backend/cache/handle.go | 17 ++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/backend/cache/cache_upload_test.go b/backend/cache/cache_upload_test.go index b3107ae92..e6072f392 100644 --- a/backend/cache/cache_upload_test.go +++ b/backend/cache/cache_upload_test.go @@ -3,7 +3,6 @@ package cache_test import ( - "io/ioutil" "math/rand" "os" "path" @@ -105,8 +104,8 @@ func TestInternalUploadMoveExistingFile(t *testing.T) { require.Len(t, de1, 1) time.Sleep(time.Second * 5) - _ = os.Remove(path.Join(runInstance.tmpUploadDir, id, runInstance.encryptRemoteIfNeeded(t, "one/test"))) - require.NoError(t, err) + //_ = os.Remove(path.Join(runInstance.tmpUploadDir, id, runInstance.encryptRemoteIfNeeded(t, "one/test"))) + //require.NoError(t, err) err = runInstance.dirMove(t, rootFs, "one/test", "second/test") require.NoError(t, err) @@ -117,6 +116,49 @@ func TestInternalUploadMoveExistingFile(t *testing.T) { require.Len(t, de1, 1) } +func TestInternalUploadTempPathCleaned(t *testing.T) { + id := fmt.Sprintf("tiutpc%v", time.Now().Unix()) + rootFs, boltDb := runInstance.newCacheFs(t, remoteName, id, true, true, + nil, + map[string]string{"cache-tmp-upload-path": path.Join(runInstance.tmpUploadDir, id), "cache-tmp-wait-time": "5s"}) + defer runInstance.cleanupFs(t, rootFs, boltDb) + + err := rootFs.Mkdir("one") + require.NoError(t, err) + err = rootFs.Mkdir("one/test") + require.NoError(t, err) + err = rootFs.Mkdir("second") + require.NoError(t, err) + + // create some rand test data + testSize := int64(1048576) + testReader := runInstance.randomReader(t, testSize) + testReader2 := runInstance.randomReader(t, testSize) + runInstance.writeObjectReader(t, rootFs, "one/test/data.bin", testReader) + runInstance.writeObjectReader(t, rootFs, "second/data.bin", testReader2) + + runInstance.completeAllBackgroundUploads(t, rootFs, "one/test/data.bin") + _, err = os.Stat(path.Join(runInstance.tmpUploadDir, id, runInstance.encryptRemoteIfNeeded(t, "one/test"))) + require.True(t, os.IsNotExist(err)) + _, err = os.Stat(path.Join(runInstance.tmpUploadDir, id, runInstance.encryptRemoteIfNeeded(t, "one"))) + require.True(t, os.IsNotExist(err)) + _, err = os.Stat(path.Join(runInstance.tmpUploadDir, id, runInstance.encryptRemoteIfNeeded(t, "second"))) + require.False(t, os.IsNotExist(err)) + + runInstance.completeAllBackgroundUploads(t, rootFs, "second/data.bin") + _, err = os.Stat(path.Join(runInstance.tmpUploadDir, id, runInstance.encryptRemoteIfNeeded(t, "second/data.bin"))) + require.True(t, os.IsNotExist(err)) + + de1, err := runInstance.list(t, rootFs, "one/test") + require.NoError(t, err) + require.Len(t, de1, 1) + + // check if it can be read + de1, err = runInstance.list(t, rootFs, "second") + require.NoError(t, err) + require.Len(t, de1, 1) +} + func TestInternalUploadQueueMoreFiles(t *testing.T) { id := fmt.Sprintf("tiuqmf%v", time.Now().Unix()) rootFs, boltDb := runInstance.newCacheFs(t, remoteName, id, true, true, @@ -158,9 +200,8 @@ func TestInternalUploadQueueMoreFiles(t *testing.T) { runInstance.completeAllBackgroundUploads(t, rootFs, lastFile) // retry until we have no more temp files and fail if they don't go down to 0 - tf, err := ioutil.ReadDir(path.Join(runInstance.tmpUploadDir, id, runInstance.encryptRemoteIfNeeded(t, "test"))) - require.NoError(t, err) - require.Len(t, tf, 0) + _, err = os.Stat(path.Join(runInstance.tmpUploadDir, id, runInstance.encryptRemoteIfNeeded(t, "test"))) + require.True(t, os.IsNotExist(err)) // check if cache lists all files de1, err = runInstance.list(t, rootFs, "test") diff --git a/backend/cache/handle.go b/backend/cache/handle.go index e63007484..0f32194da 100644 --- a/backend/cache/handle.go +++ b/backend/cache/handle.go @@ -629,6 +629,23 @@ func (b *backgroundWriter) run() { fs.Errorf(remote, "background upload: %v", err) continue } + // clean empty dirs up to root + thisDir := cleanPath(path.Dir(remote)) + for thisDir != "" { + thisList, err := b.fs.tempFs.List(thisDir) + if err != nil { + break + } + if len(thisList) > 0 { + break + } + err = b.fs.tempFs.Rmdir(thisDir) + fs.Debugf(thisDir, "cleaned from temp path") + if err != nil { + break + } + thisDir = cleanPath(path.Dir(thisDir)) + } fs.Infof(remote, "background upload: uploaded entry") err = b.fs.cache.removePendingUpload(absPath) if err != nil && !strings.Contains(err.Error(), "pending upload not found") {