diff --git a/main.go b/main.go index 025f9e3..956e80d 100644 --- a/main.go +++ b/main.go @@ -67,110 +67,6 @@ func getEnv(key, def string) string { return def } -func scanArchive() { - log.Println("Scanning archive...") - filepath.Walk(constants.ROOT_ARCHIVE, func(path string, info os.FileInfo, err error) error { - log.Printf("Scanning archive file %s...", path) - if err != nil { - log.Printf("Error scanning %s: %s", path, err) - return nil - } - path = filepath.ToSlash(path) - - if path == constants.ROOT_ARCHIVE { - log.Printf("Skipping root directory %s...", path) - return nil - } - - processArchiveFile(path, info) - return nil - }) -} - -func processArchiveFile(path string, info os.FileInfo) { - now := time.Now().UnixMilli() - - 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 - int64(fileTime) - - fileTimeFormatted := time.UnixMilli(fileTime).Format("15:04:05.000000") - timeDeltaFormatted := time.Duration(timeDelta) * time.Millisecond - log.Printf("File %s last %s at %s, %s ago", path, timeType, fileTimeFormatted, timeDeltaFormatted) - - if timeDelta > constants.DELETE_THRESHOLD.Milliseconds() { - log.Printf("File %s was %s more than %dms ago, deleting...", path, timeType, constants.DELETE_THRESHOLD) - deleteFile(path) - } -} - - -func deleteFile(path string) { - log.Printf("Deleting file %s...", path) - // err := os.Remove(path) - // if err != nil { - // log.Printf("Error deleting file %s: %s", path, err) - // return - // } - numFilesDeleted++ -} - -func cleanRoot() { - files, err := os.ReadDir(constants.ROOT) - if err != nil { - log.Printf("Error reading root directory %s: %s", constants.ROOT, err) - return - } - for _, file := range files { - if !file.IsDir() { - continue - } - empty, err := isDirEmpty(constants.ROOT + "/" + file.Name()) - if err != nil { - log.Printf("Error checking if directory %s is empty: %s", file.Name(), err) - continue - } - log.Printf("Directory %s isempty: %t", file.Name(), empty) - if empty { - log.Printf("Deleting empty directory %s", file.Name()) - err := os.RemoveAll(constants.ROOT + "/" + file.Name()) - if err != nil { - log.Printf("Error deleting empty directory %s: %s", file.Name(), err) - } - } - } -} - -func isDirEmpty(dirPath string) (bool, error) { - var empty = true - var ferr error = nil - - filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { - if err != nil { - log.Printf("Error scanning %s: %s", path, err) - ferr = err - return nil - } - if !info.IsDir() { - empty = false - log.Printf("Directory %s is not empty, found %s", dirPath, path) - return filepath.SkipAll - } - return nil - }) - return empty, ferr -} - type Constants struct { ROOT string ROOT_ARCHIVE string @@ -192,7 +88,7 @@ func main() { // where X represents the duration it can exist in the folder, // and Y represents the duration it can exist in the archive. - ROOT := filepath.ToSlash(strings.TrimSpace(getEnv("ROOT", "C:/tmp"))) + ROOT := filepath.ToSlash(strings.TrimSpace(getEnv("ROOT", "/tmp"))) ROOT_ARCHIVE := filepath.ToSlash(strings.TrimSpace(getEnv("ROOT_ARCHIVE", ROOT+"/archive"))) os.Mkdir(ROOT_ARCHIVE, os.ModePerm) IGNORED_DIRECTORIES := []string{} @@ -229,17 +125,17 @@ func main() { doRun() for { - os.Exit(0) + // os.Exit(0) time.Sleep(SCAN_INTERVAL) doRun() } } func doRun() { - log.Printf("Running at %s", time.Now().Format("15:04:05")) + log.Printf("Running at %s", time.Now().Format(time.DateTime)) scanRoot() - // scanArchive() - // cleanRoot() + scanArchive() + cleanRoot() log.Printf("Archived %d files, deleted %d files", numFilesArchived, numFilesDeleted) numFilesArchived = 0 numFilesDeleted = 0 @@ -299,7 +195,7 @@ func processFile(path string, info os.FileInfo) { } timeDelta := now - fileTime - fileTimeFormatted := time.UnixMilli(fileTime).Format("15:04:05") + fileTimeFormatted := time.UnixMilli(fileTime).Format(time.DateTime) 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() { @@ -317,10 +213,115 @@ func archiveFile(path string) { 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 - // } + err = os.Rename(path, newPath) + if err != nil { + log.Printf("Error archiving file %s: %s", path, err) + return + } numFilesArchived++ +} + +// region scanArchive +func scanArchive() { + log.Println("Scanning archive...") + filepath.Walk(constants.ROOT_ARCHIVE, func(path string, info os.FileInfo, err error) error { + log.Printf("Scanning archive file %s...", path) + if err != nil { + log.Printf("Error scanning %s: %s", path, err) + return nil + } + path = filepath.ToSlash(path) + + if path == constants.ROOT_ARCHIVE { + log.Printf("Skipping root directory %s...", path) + return nil + } + + go processArchiveFile(path, info) + return nil + }) +} + +func processArchiveFile(path string, info os.FileInfo) { + now := time.Now().UnixMilli() + + 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 - int64(fileTime) + + fileTimeFormatted := time.UnixMilli(fileTime).Format(time.DateTime) + timeDeltaFormatted := time.Duration(timeDelta) * time.Millisecond + log.Printf("File %s last %s at %s, %s ago", path, timeType, fileTimeFormatted, timeDeltaFormatted) + + if timeDelta > constants.DELETE_THRESHOLD.Milliseconds() { + log.Printf("File %s was %s more than %s ago, deleting...", path, timeType, constants.DELETE_THRESHOLD) + go deleteFile(path) + } +} + +func deleteFile(path string) { + log.Printf("Deleting file %s...", path) + err := os.Remove(path) + if err != nil { + log.Printf("Error deleting file %s: %s", path, err) + return + } + numFilesDeleted++ +} + +// region cleanRoot +func cleanRoot() { + files, err := os.ReadDir(constants.ROOT) + if err != nil { + log.Printf("Error reading root directory %s: %s", constants.ROOT, err) + return + } + for _, file := range files { + if !file.IsDir() { + continue + } + empty, err := isDirEmpty(constants.ROOT + "/" + file.Name()) + if err != nil { + log.Printf("Error checking if directory %s is empty: %s", file.Name(), err) + continue + } + log.Printf("Directory %s isempty: %t", file.Name(), empty) + if empty { + log.Printf("Deleting empty directory %s", file.Name()) + err := os.RemoveAll(constants.ROOT + "/" + file.Name()) + if err != nil { + log.Printf("Error deleting empty directory %s: %s", file.Name(), err) + } + } + } +} + +func isDirEmpty(dirPath string) (bool, error) { + var empty = true + var ferr error = nil + + filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { + if err != nil { + log.Printf("Error scanning %s: %s", path, err) + ferr = err + return nil + } + if !info.IsDir() { + empty = false + log.Printf("Directory %s is not empty, found %s", dirPath, path) + return filepath.SkipAll + } + return nil + }) + return empty, ferr } \ No newline at end of file