Merge pull request #31 from offen/exclude-symlink-candidates

Exclude symlinks from candidates when pruning local files
This commit is contained in:
Frederik Ring 2021-10-31 20:07:51 +01:00 committed by GitHub
commit c391230be6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -503,17 +503,21 @@ func (s *script) pruneOldBackups() error {
} }
if _, err := os.Stat(s.c.BackupArchive); !os.IsNotExist(err) { if _, err := os.Stat(s.c.BackupArchive); !os.IsNotExist(err) {
candidates, err := filepath.Glob( globPattern := path.Join(
path.Join(s.c.BackupArchive, fmt.Sprintf("%s*", s.c.BackupPruningPrefix)), s.c.BackupArchive,
fmt.Sprintf("%s*", s.c.BackupPruningPrefix),
) )
globMatches, err := filepath.Glob(globPattern)
if err != nil { if err != nil {
return fmt.Errorf( 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 var candidates []os.FileInfo
for _, candidate := range candidates { for _, candidate := range globMatches {
fi, err := os.Stat(candidate) fi, err := os.Stat(candidate)
if err != nil { if err != nil {
return fmt.Errorf( return fmt.Errorf(
@ -522,8 +526,14 @@ func (s *script) pruneOldBackups() error {
err, 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) matches = append(matches, candidate)
} }
} }
@ -531,7 +541,7 @@ func (s *script) pruneOldBackups() error {
if len(matches) != 0 && len(matches) != len(candidates) { if len(matches) != 0 && len(matches) != len(candidates) {
var removeErrors []error var removeErrors []error
for _, candidate := range matches { for _, candidate := range matches {
if err := os.Remove(candidate); err != nil { if err := os.Remove(candidate.Name()); err != nil {
removeErrors = append(removeErrors, err) removeErrors = append(removeErrors, err)
} }
} }