Refactor file scanning and add file counting

This commit is contained in:
2024-02-02 11:15:05 +01:00
parent 0386c0dceb
commit c57102011e

38
main.go
View File

@@ -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 dont 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()
}
}