Skip to content

Commit

Permalink
Merge pull request #31 from offen/exclude-symlink-candidates
Browse files Browse the repository at this point in the history
Exclude symlinks from candidates when pruning local files
  • Loading branch information
m90 authored Oct 31, 2021
2 parents 5245b58 + f946f36 commit c391230
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,17 +503,21 @@ func (s *script) pruneOldBackups() error {
}

if _, err := os.Stat(s.c.BackupArchive); !os.IsNotExist(err) {
candidates, err := filepath.Glob(
path.Join(s.c.BackupArchive, fmt.Sprintf("%s*", s.c.BackupPruningPrefix)),
globPattern := path.Join(
s.c.BackupArchive,
fmt.Sprintf("%s*", s.c.BackupPruningPrefix),
)
globMatches, err := filepath.Glob(globPattern)
if err != nil {
return fmt.Errorf(
"pruneOldBackups: error looking up matching files, starting with: %w", err,
"pruneOldBackups: error looking up matching files using pattern %s: %w",
globPattern,
err,
)
}

var matches []string
for _, candidate := range candidates {
var candidates []os.FileInfo
for _, candidate := range globMatches {
fi, err := os.Stat(candidate)
if err != nil {
return fmt.Errorf(
Expand All @@ -522,16 +526,22 @@ func (s *script) pruneOldBackups() error {
err,
)
}
if fi.Mode() != os.ModeSymlink {
candidates = append(candidates, fi)
}
}

if fi.Mode() != os.ModeSymlink && fi.ModTime().Before(deadline) {
var matches []os.FileInfo
for _, candidate := range candidates {
if candidate.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); err != nil {
if err := os.Remove(candidate.Name()); err != nil {
removeErrors = append(removeErrors, err)
}
}
Expand Down

0 comments on commit c391230

Please sign in to comment.