From c57102011e29ef81c714f29bc9f849c7e2c08c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Majdand=C5=BEi=C4=87?= Date: Fri, 2 Feb 2024 11:15:05 +0100 Subject: [PATCH] Refactor file scanning and add file counting --- main.go | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index c922be1..a2c7041 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,9 @@ var timeUnits = map[string]int64{ "y": 1000 * 60 * 60 * 24 * 365, } +var numFilesArchived = 0 +var numFilesDeleted = 0 + var valueRegex, _ = regexp.Compile(`\d+`) var unitRegex, _ = regexp.Compile(`[a-zA-Z]+`) @@ -78,10 +81,11 @@ func scanRoot() { return nil } - if info.IsDir() { - log.Printf("Skipping directory %s...\n", path) - return filepath.SkipDir - } + // I forgot why this code was here... It doesn't make sense to me now + // if info.IsDir() { + // log.Printf("Skipping directory %s...\n", path) + // return filepath.SkipDir + // } // We hope that IGNORED_DIRECTORIES is a small list, so we can afford to iterate over it // In fact iteration should be faster for small lists rather than hashing @@ -121,6 +125,7 @@ func processFile(path string, info os.FileInfo) { var now = time.Now().UnixMilli() var fileATime int64 = times.Get(info).AccessTime().UnixMilli() + // var fileATime int64 = times.Get(info).ModTime().UnixMilli() var accessTimeDelta = now - fileATime log.Printf("File %s last accessed at %d, %dms ago\n", path, fileATime, accessTimeDelta) if accessTimeDelta > constants.ARCHIVE_THRESHOLD { @@ -133,6 +138,7 @@ func processArchiveFile(path string, info os.FileInfo) { var now = time.Now().UnixMilli() var fileATime int64 = times.Get(info).AccessTime().UnixMilli() + // var fileATime int64 = times.Get(info).ModTime().UnixMilli() var accessTimeDelta = now - fileATime log.Printf("File %s last accessed at %d, %dms ago\n", path, fileATime, accessTimeDelta) if accessTimeDelta > constants.DELETE_THRESHOLD { @@ -152,6 +158,7 @@ func archiveFile(path string) { log.Printf("Error archiving file %s: %s\n", path, err) return } + numFilesArchived++ } func deleteFile(path string) { @@ -162,6 +169,7 @@ func deleteFile(path string) { log.Printf("Error deleting file %s: %s\n", path, err) return } + numFilesDeleted++ } func cleanRoot() { @@ -218,10 +226,24 @@ type Constants struct { SCAN_INTERVAL time.Duration } +func doRun() { + scanRoot() + scanArchive() + cleanRoot() + log.Printf("Archived %d files, deleted %d files\n", numFilesArchived, numFilesDeleted) + numFilesArchived = 0 + numFilesDeleted = 0 +} + var constants = Constants{} func main() { log.SetFlags(0b111) + // Important: Access times don’t accumulate. + // This implies that archiving the file won't alter its access time. + // Therefore, assign X as the ARCHIVE_TIME and X + Y as the DELETE_TIME, + // where X represents the duration it can exist in the folder, + // and Y represents the duration it can exist in the archive. var ROOT = filepath.ToSlash(strings.TrimSpace(getEnv("ROOT", "/tmp"))) var ROOT_ARCHIVE = filepath.ToSlash(strings.TrimSpace(getEnv("ROOT_ARCHIVE", ROOT+"/archive"))) @@ -255,14 +277,10 @@ func main() { log.Printf("DELETE_THRESHOLD: %d\n", DELETE_THRESHOLD) log.Printf("SCAN_INTERVAL: %d\n", SCAN_INTERVAL.Milliseconds()) - scanRoot() - scanArchive() - cleanRoot() + doRun() for { log.Printf("Running at %d", time.Now().UnixMilli()) time.Sleep(SCAN_INTERVAL) - scanRoot() - scanArchive() - cleanRoot() + doRun() } }