search only "info" folder for trashinfo files

swap filepath.WalkDir for os.ReadDir
I don't really know why I used WalkDir in the first place
This commit is contained in:
Lilian Jónsdóttir 2024-08-05 22:02:56 -07:00
parent 44da4acbbd
commit cc973694e5

View file

@ -62,42 +62,49 @@ func (t TrashInfo) String() string {
} }
func FindTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) { func FindTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
log.Debugf("searching for trashinfo files in %s", trashdir)
var files Files var files Files
outerr := filepath.WalkDir(trashdir, func(path string, dirEntry fs.DirEntry, err error) error {
if err != nil { infodir := filepath.Join(trashdir, "info")
log.Debugf("what happened?? what is %s?", err) dirs, err := os.ReadDir(infodir)
return err if err != nil {
return Files{}, err
}
for _, dir := range dirs {
if dir.IsDir() || filepath.Ext(dir.Name()) != trashInfoExt {
continue
} }
// ignore self, directories, and non trashinfo files path := filepath.Join(infodir, dir.Name())
if path == trashdir || dirEntry.IsDir() || filepath.Ext(path) != trashInfoExt {
return nil
}
// trashinfo is just an ini file, so // trashinfo is just an ini file, so
c, err := ini.Load(path) trashInfo, err := ini.Load(path)
if err != nil { if err != nil {
return err log.Errorf("error reading %s: %s", path, err)
continue
} }
if section := c.Section(trashInfoSec); section != nil {
if section := trashInfo.Section(trashInfoSec); section != nil {
basepath := section.Key(trashInfoPath).String() basepath := section.Key(trashInfoPath).String()
filename := filepath.Base(basepath) filename := filepath.Base(basepath)
trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trashInfoExt, "", 1) trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trashInfoExt, "", 1)
info, err := os.Lstat(trashedpath) info, err := os.Lstat(trashedpath)
if err != nil { if err != nil {
log.Errorf("error reading %s: %s", trashedpath, err) log.Errorf("error reading '%s': %s", trashedpath, err)
return nil continue
} }
s := section.Key(trashInfoDate).Value() s := section.Key(trashInfoDate).Value()
date, err := time.ParseInLocation(trashInfoDateFmt, s, time.Local) date, err := time.ParseInLocation(trashInfoDateFmt, s, time.Local)
if err != nil { if err != nil {
return err log.Errorf("error parsing date '%s' in trashinfo file '%s': %s", s, path, err)
continue
} }
if ogdir != "" && filepath.Dir(basepath) != ogdir { if ogdir != "" && filepath.Dir(basepath) != ogdir {
return nil continue
} }
if fltr.Match(info) { if fltr.Match(info) {
@ -112,11 +119,8 @@ func FindTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
}) })
} }
} }
return nil
})
if outerr != nil {
return Files{}, outerr
} }
return files, nil return files, nil
} }