2024-06-18 18:21:03 -04:00
|
|
|
// Package files finds and displays files on disk
|
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-06-19 23:25:04 -04:00
|
|
|
"strings"
|
2024-06-18 18:21:03 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.burning.moe/celediel/gt/internal/filter"
|
|
|
|
"github.com/charmbracelet/log"
|
2024-06-22 00:52:59 -04:00
|
|
|
"github.com/dustin/go-humanize"
|
2024-06-18 18:21:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
name, path string
|
|
|
|
filesize int64
|
|
|
|
modified time.Time
|
2024-06-18 21:08:52 -04:00
|
|
|
isdir bool
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Files []File
|
|
|
|
|
|
|
|
func (f File) Name() string { return f.name }
|
|
|
|
func (f File) Path() string { return f.path }
|
2024-06-18 20:01:38 -04:00
|
|
|
func (f File) Filename() string { return filepath.Join(f.path, f.name) }
|
2024-06-18 18:21:03 -04:00
|
|
|
func (f File) Modified() time.Time { return f.modified }
|
|
|
|
func (f File) Filesize() int64 { return f.filesize }
|
2024-06-18 21:08:52 -04:00
|
|
|
func (f File) IsDir() bool { return f.isdir }
|
2024-06-18 18:21:03 -04:00
|
|
|
|
2024-06-22 00:52:59 -04:00
|
|
|
func New(path string) (File, error) {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return File{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
abs, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("couldn't get absolute path for %s", path)
|
|
|
|
abs = path
|
|
|
|
}
|
|
|
|
|
|
|
|
name := filepath.Base(abs)
|
|
|
|
base_path := filepath.Dir(abs)
|
|
|
|
|
|
|
|
log.Debugf("%s (base:%s) (size:%s) (modified:%s) exists",
|
|
|
|
name, base_path, humanize.Bytes(uint64(info.Size())), info.ModTime())
|
|
|
|
|
|
|
|
return File{
|
|
|
|
name: name,
|
|
|
|
path: base_path,
|
|
|
|
filesize: info.Size(),
|
|
|
|
modified: info.ModTime(),
|
|
|
|
isdir: info.IsDir(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
func Find(dir string, recursive bool, f *filter.Filter) (files Files, err error) {
|
|
|
|
if dir == "." || dir == "" {
|
|
|
|
var d string
|
|
|
|
if d, err = os.Getwd(); err != nil {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
dir = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 20:02:06 -04:00
|
|
|
var recursively string
|
|
|
|
if recursive {
|
|
|
|
recursively = " recursively"
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("gonna find files%s in %s matching %s", recursively, dir, f)
|
2024-06-18 18:21:03 -04:00
|
|
|
|
|
|
|
if recursive {
|
|
|
|
files = append(files, walk_dir(dir, f)...)
|
|
|
|
} else {
|
|
|
|
files = append(files, read_dir(dir, f)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-19 23:25:04 -04:00
|
|
|
// is_in_recursive_dir checks `path` and parent directories
|
|
|
|
// of `path` up to `base` for a hidden parent
|
|
|
|
func is_in_recursive_dir(base, path string) bool {
|
|
|
|
me := path
|
|
|
|
for {
|
|
|
|
me = filepath.Clean(me)
|
|
|
|
if me == base {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(filepath.Base(me), ".") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
me += string(os.PathSeparator) + ".."
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
func walk_dir(dir string, f *filter.Filter) (files Files) {
|
|
|
|
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
|
2024-06-19 23:24:15 -04:00
|
|
|
if dir == path {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-30 21:57:11 -04:00
|
|
|
if is_in_recursive_dir(dir, path) && !f.ShowHidden() {
|
2024-06-19 23:25:04 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
p, e := filepath.Abs(path)
|
|
|
|
if e != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-19 23:25:04 -04:00
|
|
|
|
2024-06-18 20:02:06 -04:00
|
|
|
name := d.Name()
|
2024-06-18 18:21:03 -04:00
|
|
|
info, _ := d.Info()
|
2024-06-30 21:39:23 -04:00
|
|
|
if f.Match(name, info.ModTime(), info.Size(), info.IsDir()) {
|
2024-06-18 20:02:06 -04:00
|
|
|
log.Debugf("found matching file: %s %s", name, info.ModTime())
|
2024-06-19 23:25:04 -04:00
|
|
|
i, err := os.Stat(p)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("error in file stat: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-18 20:02:06 -04:00
|
|
|
files = append(files, File{
|
|
|
|
path: filepath.Dir(p),
|
|
|
|
name: name,
|
|
|
|
filesize: i.Size(),
|
|
|
|
modified: i.ModTime(),
|
2024-06-18 21:08:52 -04:00
|
|
|
isdir: i.IsDir(),
|
2024-06-18 20:02:06 -04:00
|
|
|
})
|
2024-06-18 18:21:03 -04:00
|
|
|
} else {
|
2024-06-18 20:02:06 -04:00
|
|
|
log.Debugf("ignoring file %s (%s)", name, info.ModTime())
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-06-19 23:25:04 -04:00
|
|
|
log.Errorf("error walking directory %s: %s", dir, err)
|
2024-06-18 18:21:03 -04:00
|
|
|
return []File{}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func read_dir(dir string, f *filter.Filter) (files Files) {
|
|
|
|
fs, err := os.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return []File{}
|
|
|
|
}
|
|
|
|
for _, file := range fs {
|
|
|
|
name := file.Name()
|
|
|
|
|
|
|
|
if name == dir {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := file.Info()
|
|
|
|
if err != nil {
|
|
|
|
return []File{}
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Dir(filepath.Join(dir, name))
|
|
|
|
|
2024-06-30 21:39:23 -04:00
|
|
|
if f.Match(name, info.ModTime(), info.Size(), info.IsDir()) {
|
2024-06-18 20:02:06 -04:00
|
|
|
log.Debugf("found matching file: %s %s", name, info.ModTime())
|
2024-06-18 18:21:03 -04:00
|
|
|
files = append(files, File{
|
|
|
|
name: name,
|
|
|
|
path: path,
|
|
|
|
modified: info.ModTime(),
|
|
|
|
filesize: info.Size(),
|
2024-06-18 21:08:52 -04:00
|
|
|
isdir: info.IsDir(),
|
2024-06-18 18:21:03 -04:00
|
|
|
})
|
|
|
|
} else {
|
2024-06-18 20:02:06 -04:00
|
|
|
log.Debugf("ignoring file %s (%s)", name, info.ModTime())
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func SortByModified(a, b File) int {
|
|
|
|
if a.modified.After(b.modified) {
|
|
|
|
return 1
|
|
|
|
} else if a.modified.Before(b.modified) {
|
|
|
|
return -1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SortByModifiedReverse(a, b File) int {
|
|
|
|
if a.modified.Before(b.modified) {
|
|
|
|
return 1
|
|
|
|
} else if a.modified.After(b.modified) {
|
|
|
|
return -1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SortBySize(a, b File) int {
|
|
|
|
if a.filesize > b.filesize {
|
|
|
|
return 1
|
|
|
|
} else if a.filesize < b.filesize {
|
|
|
|
return -1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SortBySizeReverse(a, b File) int {
|
|
|
|
if a.filesize < b.filesize {
|
|
|
|
return 1
|
|
|
|
} else if a.filesize > b.filesize {
|
|
|
|
return -1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|