Created cleanup functionality for syncthing (#6884)
* Add clean up for Simple File Versioning pt.1 created test * Add clean up for Simple File Versioning pt.2 Passing the test * stuck on how javascript communicates with backend * Add trash clean up for Simple File Versioning Add trash clean up functionality of to allow the user to delete backups after specified amount of days. * Fixed html and js style * Refactored cleanup test cases Refactored cleanup test cases to one file and deleted duplicated code. * Added copyright to test file * Refactor folder cleanout to utility function * change utility function to package private * refactored utility function; fixed build errors * Updated copyright year. * refactor test and logging * refactor html and js * revert style change in html * reverted changes in html and some js * checkout origin head version edit...html * checkout upstream master and correct file
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
package versioner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
@@ -293,3 +294,51 @@ func findAllVersions(fs fs.Filesystem, filePath string) []string {
|
||||
|
||||
return versions
|
||||
}
|
||||
|
||||
func cleanByDay(ctx context.Context, versionsFs fs.Filesystem, cleanoutDays int) error {
|
||||
if cleanoutDays <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := versionsFs.Lstat("."); fs.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
cutoff := time.Now().Add(time.Duration(-24*cleanoutDays) * time.Hour)
|
||||
dirTracker := make(emptyDirTracker)
|
||||
|
||||
walkFn := func(path string, info fs.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
if info.IsDir() && !info.IsSymlink() {
|
||||
dirTracker.addDir(path)
|
||||
return nil
|
||||
}
|
||||
|
||||
if info.ModTime().Before(cutoff) {
|
||||
// The file is too old; remove it.
|
||||
err = versionsFs.Remove(path)
|
||||
} else {
|
||||
// Keep this file, and remember it so we don't unnecessarily try
|
||||
// to remove this directory.
|
||||
dirTracker.addFile(path)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if err := versionsFs.Walk(".", walkFn); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dirTracker.deleteEmptyDirs(versionsFs)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user