From 92b967a40c13cd8c3e6a3c48333d7eb38d6ab0b7 Mon Sep 17 00:00:00 2001 From: Radoslav Dimitrov Date: Fri, 18 Mar 2022 11:39:10 +0200 Subject: [PATCH 1/4] Fix linter errors raised by staticcheck Signed-off-by: Radoslav Dimitrov --- local_store.go | 4 +--- verify/db_test.go | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/local_store.go b/local_store.go index 86eceefd..007b705c 100644 --- a/local_store.go +++ b/local_store.go @@ -431,11 +431,9 @@ func (f *fileSystemStore) Commit(consistentSnapshot bool, versions map[string]in return err } if !info.IsDir() && isTarget(rel) && needsRemoval(rel) { - //lint:ignore SA9003 empty branch if err := os.Remove(path); err != nil { - // TODO: log / handle error + return err } - // TODO: remove empty directory } return nil } diff --git a/verify/db_test.go b/verify/db_test.go index e4bab0a3..84031ffb 100644 --- a/verify/db_test.go +++ b/verify/db_test.go @@ -36,7 +36,7 @@ func TestDelegationsDB(t *testing.T) { { testName: "invalid keys", delegations: &data.Delegations{Keys: map[string]*data.PublicKey{ - "a": &data.PublicKey{Type: data.KeySchemeEd25519}, + "a": {Type: data.KeySchemeEd25519}, }}, initErr: ErrWrongID{}, }, From 3704653aa970f5d7a5c95757d716030bffdda2ae Mon Sep 17 00:00:00 2001 From: Radoslav Dimitrov Date: Fri, 25 Mar 2022 13:04:28 +0200 Subject: [PATCH 2/4] chore: delete empty target folder when doing tuf commit Signed-off-by: Radoslav Dimitrov --- local_store.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/local_store.go b/local_store.go index 007b705c..09e7383d 100644 --- a/local_store.go +++ b/local_store.go @@ -410,6 +410,7 @@ func (f *fileSystemStore) Commit(consistentSnapshot bool, versions map[string]in } return nil } + // Checks if target file should be deleted needsRemoval := func(path string) bool { if consistentSnapshot { // strip out the hash @@ -422,6 +423,19 @@ func (f *fileSystemStore) Commit(consistentSnapshot bool, versions map[string]in _, ok := hashes[path] return !ok } + // Checks if folder is empty + folderNeedsRemoval := func(path string) bool { + f, err := os.Open(path) + if err != nil { + return false + } + defer f.Close() + _, err = f.Readdirnames(1) + if err == io.EOF { + return true + } + return false + } removeFile := func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -431,9 +445,17 @@ func (f *fileSystemStore) Commit(consistentSnapshot bool, versions map[string]in return err } if !info.IsDir() && isTarget(rel) && needsRemoval(rel) { + // Delete the target file if err := os.Remove(path); err != nil { return err } + // Delete the target folder too if it's empty + targetFolder := filepath.Dir(path) + if folderNeedsRemoval(targetFolder) { + if err := os.Remove(targetFolder); err != nil { + return err + } + } } return nil } From 941f7eb926915e7d4efbeba9dc480faf7e65289e Mon Sep 17 00:00:00 2001 From: Radoslav Dimitrov Date: Wed, 30 Mar 2022 00:42:47 +0300 Subject: [PATCH 3/4] chore: update unit test to expect a deleted targets folder Signed-off-by: Radoslav Dimitrov --- repo_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repo_test.go b/repo_test.go index 1e46147c..d614d52c 100644 --- a/repo_test.go +++ b/repo_test.go @@ -1477,7 +1477,7 @@ func (rs *RepoSuite) TestManageMultipleTargets(c *C) { c.Assert(r.Snapshot(), IsNil) c.Assert(r.Timestamp(), IsNil) c.Assert(r.Commit(), IsNil) - tmp.assertEmpty("repository/targets") + tmp.assertNotExist("repository/targets") t, err := r.topLevelTargets() c.Assert(err, IsNil) c.Assert(t.Targets, HasLen, 0) From 8c375982c9ef03cb348806dee6190d9252ac5d71 Mon Sep 17 00:00:00 2001 From: Radoslav Dimitrov Date: Wed, 30 Mar 2022 00:49:05 +0300 Subject: [PATCH 4/4] chore: fix staticcheck error for local_store.go Signed-off-by: Radoslav Dimitrov --- local_store.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/local_store.go b/local_store.go index 09e7383d..e6257e7b 100644 --- a/local_store.go +++ b/local_store.go @@ -431,10 +431,7 @@ func (f *fileSystemStore) Commit(consistentSnapshot bool, versions map[string]in } defer f.Close() _, err = f.Readdirnames(1) - if err == io.EOF { - return true - } - return false + return err == io.EOF } removeFile := func(path string, info os.FileInfo, err error) error { if err != nil {