Skip to content

Commit

Permalink
Fix linter errors raised by staticcheck (#236)
Browse files Browse the repository at this point in the history
* Fix linter errors raised by staticcheck

Signed-off-by: Radoslav Dimitrov <[email protected]>

* chore: delete empty target folder when doing tuf commit

Signed-off-by: Radoslav Dimitrov <[email protected]>

* chore: update unit test to expect a deleted targets folder

Signed-off-by: Radoslav Dimitrov <[email protected]>

* chore: fix staticcheck error for local_store.go

Signed-off-by: Radoslav Dimitrov <[email protected]>
rdimitrov authored Mar 30, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 14b188b commit 2b4cbfe
Showing 3 changed files with 22 additions and 5 deletions.
23 changes: 20 additions & 3 deletions local_store.go
Original file line number Diff line number Diff line change
@@ -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,16 @@ 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)
return err == io.EOF
}
removeFile := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
@@ -431,11 +442,17 @@ func (f *fileSystemStore) Commit(consistentSnapshot bool, versions map[string]in
return err
}
if !info.IsDir() && isTarget(rel) && needsRemoval(rel) {
//lint:ignore SA9003 empty branch
// Delete the target file
if err := os.Remove(path); err != nil {
// TODO: log / handle error
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
}
}
// TODO: remove empty directory
}
return nil
}
2 changes: 1 addition & 1 deletion repo_test.go
Original file line number Diff line number Diff line change
@@ -1490,7 +1490,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)
2 changes: 1 addition & 1 deletion verify/db_test.go
Original file line number Diff line number Diff line change
@@ -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{},
},

0 comments on commit 2b4cbfe

Please sign in to comment.