func archiveFiles(cfg *conf.Config, dirStr string, rootStat os.FileInfo) (int, error) { count := 0 fileList, err := filepath.Glob(filepath.Join(dirStr, cfg.Pattern)) if err != nil { return 0, err } archiveTime := cfg.ArchiveTime() for _, fname := range fileList { fs, e := os.Stat(fname) if e != nil { fmt.Printf("Error! Failed to consider %s for archiving: %s\n", fname, e) continue } if fs.Name() == conf.ConfName || fs.IsDir() { continue } if fs.ModTime().Before(archiveTime) { if newname, e := archiveFile(fname, cfg, rootStat, fs); e != nil { fmt.Printf("Error! Failed to archive %s - %s\n", fname, e) } else { util.Verbosef("Moved %s --> %s\n", fname, newname) count += 1 } } } return count, nil }
func (cfg *Config) EachYear(rootpath string, maxDate time.Time, f func(path string, maxDate time.Time) (int, error)) (int, error) { archive, err := os.Open(cfg.ArchivePath(rootpath)) if err != nil { if os.IsNotExist(err) { err = nil //return but without an error } return 0, err } defer archive.Close() years, err := archive.Readdirnames(0) if err != nil { return 0, err } count := 0 for _, yearStr := range years { yearPath := filepath.Join(archive.Name(), yearStr) year, err := strconv.Atoi(yearStr) if err != nil { util.Verbosef("Warning. Skipping archive folder - %s\n", yearPath) continue } if year <= maxDate.Year() { c, err := f(yearPath, maxDate) if err != nil { fmt.Println(err) } else { count += c } } } return count, nil }
func purgeMonth(path string, maxDate time.Time) (int, error) { if strings.HasSuffix(path, ".zip") { err := os.Remove(path) if err != nil { return 0, err } util.Verbosef("Purged %s\n", path) return 1, nil } else { return purgeInFolder(path, maxDate, "*") } }
func zipArchive(monthPath string, maxDate time.Time) (int, error) { if !strings.HasSuffix(monthPath, ".zip") { err := util.ZipFolder(monthPath) if err != nil { return 0, fmt.Errorf("Error! Failed to zip %s - %s\n", monthPath, err) } err = os.RemoveAll(monthPath) if err != nil { fmt.Printf("Warning. Failed to remove month (%s) folder after zipping - %s\n", monthPath, err) } else { util.Verbosef("Zipped %s\n", monthPath) } return 1, nil } else { return 0, nil } }
func purgeInFolder(path string, maxDate time.Time, pattern string) (int, error) { dir, err := os.Open(path) if err != nil { return 0, fmt.Errorf("Error! Could not access archive folder %s - %s", path, err) } defer dir.Close() fileList, err := filepath.Glob(filepath.Join(path, pattern)) if err != nil { return 0, fmt.Errorf("Error! Could not access archive folder %s - %s", path, err) } count := 0 for _, f := range fileList { fi, err := os.Stat(f) if err != nil { fmt.Printf("Error! Failed to get stats for %s - %s\n", f, err) } if fi.Name() == conf.ConfName { continue } if fi.ModTime().Before(maxDate) { filePath := filepath.Join(path, fi.Name()) err := os.Remove(filePath) if err != nil { fmt.Printf("Error! Failed to purge %s - %s\n", filePath, err) } else { util.Verbosef("Purged %s\n", filePath) count += 1 } } } return count, nil }