diff --git a/main.go b/main.go index 2a8d8e5..025f9e3 100644 --- a/main.go +++ b/main.go @@ -67,41 +67,6 @@ func getEnv(key, def string) string { 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() { log.Println("Scanning archive...") 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) { 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) { log.Printf("Deleting file %s...", path) @@ -225,6 +150,7 @@ func cleanRoot() { } } } + func isDirEmpty(dirPath string) (bool, error) { var empty = true var ferr error = nil @@ -257,6 +183,7 @@ type Constants struct { var constants = &Constants{} +// region main func main() { log.SetFlags(log.Lmicroseconds | log.Lshortfile) // Important: Access times don’t accumulate. @@ -311,9 +238,89 @@ func main() { func doRun() { log.Printf("Running at %s", time.Now().Format("15:04:05")) scanRoot() - scanArchive() - cleanRoot() + // scanArchive() + // cleanRoot() log.Printf("Archived %d files, deleted %d files", numFilesArchived, numFilesDeleted) numFilesArchived = 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++ } \ No newline at end of file