Skip to content

Commit

Permalink
os.FileInfo cannot be used for deleting files as it does not contain …
Browse files Browse the repository at this point in the history
…a full path
  • Loading branch information
m90 committed Nov 2, 2021
1 parent c391230 commit 3193e88
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func (s *script) pruneOldBackups() error {
)
}

var candidates []os.FileInfo
var candidates []string
for _, candidate := range globMatches {
fi, err := os.Stat(candidate)
if err != nil {
Expand All @@ -527,21 +527,29 @@ func (s *script) pruneOldBackups() error {
)
}
if fi.Mode() != os.ModeSymlink {
candidates = append(candidates, fi)
candidates = append(candidates, candidate)
}
}

var matches []os.FileInfo
var matches []string
for _, candidate := range candidates {
if candidate.ModTime().Before(deadline) {
fi, err := os.Stat(candidate)
if err != nil {
return fmt.Errorf(
"pruneOldBackups: error calling stat on file %s: %w",
candidate,
err,
)
}
if fi.ModTime().Before(deadline) {
matches = append(matches, candidate)
}
}

if len(matches) != 0 && len(matches) != len(candidates) {
var removeErrors []error
for _, candidate := range matches {
if err := os.Remove(candidate.Name()); err != nil {
for _, match := range matches {
if err := os.Remove(match); err != nil {
removeErrors = append(removeErrors, err)
}
}
Expand Down

0 comments on commit 3193e88

Please sign in to comment.