Refactor root scanning

This commit is contained in:
2024-06-22 14:10:37 +02:00
parent b95d9f7ff0
commit 4a30a15921

161
main.go
View File

@@ -67,41 +67,6 @@ func getEnv(key, def string) string {
return def return def
} }
func scanRoot() {
log.Println("Scanning root directory...")
filepath.Walk(constants.ROOT, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("Error scanning %s: %s", path, err)
return nil
}
path = filepath.ToSlash(path)
if path == constants.ROOT {
log.Printf("Skipping root directory %s...", path)
return nil
}
// I forgot why this code was here... It doesn't make sense to me now
// if info.IsDir() {
// log.Printf("Skipping directory %s...", 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
for _, ignoredDir := range constants.IGNORED_DIRECTORIES {
log.Println(constants.IGNORED_DIRECTORIES, len(constants.IGNORED_DIRECTORIES))
if strings.HasPrefix(path, ignoredDir) {
log.Printf("Ignoring directory %s", path)
return filepath.SkipDir
}
}
processFile(path, info)
return nil
})
}
func scanArchive() { func scanArchive() {
log.Println("Scanning archive...") log.Println("Scanning archive...")
filepath.Walk(constants.ROOT_ARCHIVE, func(path string, info os.FileInfo, err error) error { filepath.Walk(constants.ROOT_ARCHIVE, func(path string, info os.FileInfo, err error) error {
@@ -122,30 +87,6 @@ func scanArchive() {
}) })
} }
func processFile(path string, info os.FileInfo) {
now := time.Now().UnixMilli()
log.Printf("Processing file %s...", path)
timeType := "accessed"
if constants.USE_MODTIME {
timeType = "modified"
}
var fileTime int64
if constants.USE_MODTIME {
fileTime = times.Get(info).ModTime().UnixMilli()
} else {
fileTime = times.Get(info).AccessTime().UnixMilli()
}
timeDelta := now - fileTime
log.Printf("File %s last %s at %d, %dms ago", path, timeType, fileTime, timeDelta)
if timeDelta > constants.ARCHIVE_THRESHOLD {
log.Printf("File %s was %s more than %dms ago, archiving...", path, timeType, constants.ARCHIVE_THRESHOLD)
archiveFile(path)
}
}
func processArchiveFile(path string, info os.FileInfo) { func processArchiveFile(path string, info os.FileInfo) {
now := time.Now().UnixMilli() now := time.Now().UnixMilli()
@@ -173,22 +114,6 @@ func processArchiveFile(path string, info os.FileInfo) {
} }
} }
func archiveFile(path string) {
newPath := constants.ROOT_ARCHIVE + strings.Replace(path, constants.ROOT, "", 1)
log.Printf("Archiving file %s to %s...", path, newPath)
err := os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
if err != nil {
log.Printf("Error creating directory %s: %s", filepath.Dir(newPath), err)
return
}
// err := os.Rename(path, newPath)
// if err != nil {
// log.Printf("Error archiving file %s: %s", path, err)
// return
// }
numFilesArchived++
}
func deleteFile(path string) { func deleteFile(path string) {
log.Printf("Deleting file %s...", path) log.Printf("Deleting file %s...", path)
@@ -225,6 +150,7 @@ func cleanRoot() {
} }
} }
} }
func isDirEmpty(dirPath string) (bool, error) { func isDirEmpty(dirPath string) (bool, error) {
var empty = true var empty = true
var ferr error = nil var ferr error = nil
@@ -257,6 +183,7 @@ type Constants struct {
var constants = &Constants{} var constants = &Constants{}
// region main
func main() { func main() {
log.SetFlags(log.Lmicroseconds | log.Lshortfile) log.SetFlags(log.Lmicroseconds | log.Lshortfile)
// Important: Access times dont accumulate. // Important: Access times dont accumulate.
@@ -311,9 +238,89 @@ func main() {
func doRun() { func doRun() {
log.Printf("Running at %s", time.Now().Format("15:04:05")) log.Printf("Running at %s", time.Now().Format("15:04:05"))
scanRoot() scanRoot()
scanArchive() // scanArchive()
cleanRoot() // cleanRoot()
log.Printf("Archived %d files, deleted %d files", numFilesArchived, numFilesDeleted) log.Printf("Archived %d files, deleted %d files", numFilesArchived, numFilesDeleted)
numFilesArchived = 0 numFilesArchived = 0
numFilesDeleted = 0 numFilesDeleted = 0
} }
// region scanRoot
func scanRoot() {
log.Println("Scanning root directory...")
filepath.Walk(constants.ROOT, func(path string, info os.FileInfo, err error) error {
log.Printf("Scanning file %s...", path)
if err != nil {
log.Printf("Error scanning %s: %s", path, err)
return nil
}
path = filepath.ToSlash(path)
if path == constants.ROOT {
log.Printf("Skipping root directory %s...", path)
return nil
}
// I forgot why this code was here... It doesn't make sense to me now
// if info.IsDir() {
// log.Printf("Skipping directory %s...", 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
for _, ignoredDir := range constants.IGNORED_DIRECTORIES {
// log.Println(constants.IGNORED_DIRECTORIES, len(constants.IGNORED_DIRECTORIES))
if strings.HasPrefix(path, ignoredDir) {
log.Printf("Ignoring directory %s", path)
return filepath.SkipDir
}
}
go processFile(path, info)
return nil
})
}
func processFile(path string, info os.FileInfo) {
now := time.Now().UnixMilli()
log.Printf("Processing file %s...", path)
timeType := "accessed"
if constants.USE_MODTIME {
timeType = "modified"
}
var fileTime int64
if constants.USE_MODTIME {
fileTime = times.Get(info).ModTime().UnixMilli()
} else {
fileTime = times.Get(info).AccessTime().UnixMilli()
}
timeDelta := now - fileTime
fileTimeFormatted := time.UnixMilli(fileTime).Format("15:04:05")
timeDeltaFormatted := time.Duration(timeDelta) * time.Millisecond
log.Printf("File %s last %s at %s, %s ago", path, timeType, fileTimeFormatted, timeDeltaFormatted)
if timeDelta > constants.ARCHIVE_THRESHOLD.Milliseconds() {
log.Printf("File %s was %s more than %s ago, archiving...", path, timeType, constants.ARCHIVE_THRESHOLD)
go archiveFile(path)
}
}
func archiveFile(path string) {
newPath := constants.ROOT_ARCHIVE + strings.Replace(path, constants.ROOT, "", 1)
log.Printf("Archiving file %s to %s...", path, newPath)
err := os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
if err != nil {
log.Printf("Error creating directory %s: %s", filepath.Dir(newPath), err)
return
}
// err := os.Rename(path, newPath)
// if err != nil {
// log.Printf("Error archiving file %s: %s", path, err)
// return
// }
numFilesArchived++
}