Skip to content

Commit

Permalink
chore: delete empty target folder when doing tuf commit
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov committed Mar 25, 2022
1 parent 92b967a commit 3704653
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions local_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 3704653

Please sign in to comment.