2024-07-03 18:05:17 -04:00
|
|
|
package files
|
2024-06-18 18:21:03 -04:00
|
|
|
|
|
|
|
import (
|
2024-06-28 14:27:01 -04:00
|
|
|
"fmt"
|
2024-06-18 18:21:03 -04:00
|
|
|
"io/fs"
|
2024-06-20 14:40:28 -04:00
|
|
|
"math/rand"
|
2024-06-18 18:21:03 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-06-28 13:45:44 -04:00
|
|
|
"git.burning.moe/celediel/gt/internal/dirs"
|
2024-06-18 18:21:03 -04:00
|
|
|
"git.burning.moe/celediel/gt/internal/filter"
|
2024-06-28 14:27:01 -04:00
|
|
|
"git.burning.moe/celediel/gt/internal/prompt"
|
2024-07-03 18:05:17 -04:00
|
|
|
|
2024-06-28 14:27:01 -04:00
|
|
|
"github.com/charmbracelet/huh"
|
2024-06-18 18:21:03 -04:00
|
|
|
"github.com/charmbracelet/log"
|
2024-06-21 01:43:21 -04:00
|
|
|
"github.com/dustin/go-humanize"
|
2024-06-18 18:21:03 -04:00
|
|
|
"gitlab.com/tymonx/go-formatter/formatter"
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-06-20 14:40:28 -04:00
|
|
|
random_str_length int = 8
|
2024-06-18 18:21:03 -04:00
|
|
|
trash_info_ext string = ".trashinfo"
|
|
|
|
trash_info_sec string = "Trash Info"
|
|
|
|
trash_info_path string = "Path"
|
|
|
|
trash_info_date string = "DeletionDate"
|
|
|
|
trash_info_date_fmt string = "2006-01-02T15:04:05"
|
|
|
|
trash_info_template string = `[Trash Info]
|
|
|
|
Path={path}
|
2024-06-20 12:18:30 -04:00
|
|
|
DeletionDate={date}
|
|
|
|
`
|
2024-06-18 18:21:03 -04:00
|
|
|
)
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
type TrashInfo struct {
|
2024-06-18 18:21:03 -04:00
|
|
|
name, ogpath string
|
|
|
|
path, trashinfo string
|
2024-06-18 21:08:52 -04:00
|
|
|
isdir bool
|
2024-06-18 18:21:03 -04:00
|
|
|
trashed time.Time
|
|
|
|
filesize int64
|
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
func (t TrashInfo) Name() string { return t.name }
|
|
|
|
func (t TrashInfo) TrashPath() string { return t.path }
|
|
|
|
func (t TrashInfo) Path() string { return t.ogpath }
|
|
|
|
func (t TrashInfo) TrashInfo() string { return t.trashinfo }
|
|
|
|
func (t TrashInfo) Date() time.Time { return t.trashed }
|
|
|
|
func (t TrashInfo) IsDir() bool { return t.isdir }
|
2024-07-15 18:06:53 -04:00
|
|
|
func (t TrashInfo) Filesize() int64 {
|
|
|
|
if t.isdir {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return t.filesize
|
|
|
|
}
|
2024-06-18 18:21:03 -04:00
|
|
|
|
2024-07-15 18:32:33 -04:00
|
|
|
func (t TrashInfo) String() string {
|
2024-07-15 21:46:39 -04:00
|
|
|
return t.name + t.path + t.ogpath + t.trashinfo
|
2024-07-15 18:32:33 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
func FindTrash(trashdir, ogdir string, f *filter.Filter) (files Files, outerr error) {
|
2024-06-18 18:21:03 -04:00
|
|
|
outerr = filepath.WalkDir(trashdir, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("what happened?? what is %s?", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore self, directories, and non trashinfo files
|
|
|
|
if path == trashdir || d.IsDir() || filepath.Ext(path) != trash_info_ext {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// trashinfo is just an ini file, so
|
|
|
|
c, err := ini.Load(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if s := c.Section(trash_info_sec); s != nil {
|
|
|
|
basepath := s.Key(trash_info_path).String()
|
2024-06-19 18:55:01 -04:00
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
filename := filepath.Base(basepath)
|
|
|
|
trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trash_info_ext, "", 1)
|
2024-06-20 14:40:28 -04:00
|
|
|
info, err := os.Stat(trashedpath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error reading %s: %s", trashedpath, err)
|
|
|
|
}
|
2024-06-18 18:21:03 -04:00
|
|
|
|
|
|
|
s := s.Key(trash_info_date).Value()
|
|
|
|
date, err := time.ParseInLocation(trash_info_date_fmt, s, time.Local)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-18 20:02:49 -04:00
|
|
|
if ogdir != "" && filepath.Dir(basepath) != ogdir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-15 23:15:20 -04:00
|
|
|
if f.Match(info) {
|
2024-06-18 18:21:03 -04:00
|
|
|
log.Debugf("%s: deleted on %s", filename, date.Format(trash_info_date_fmt))
|
2024-07-03 18:05:17 -04:00
|
|
|
files = append(files, TrashInfo{
|
2024-06-18 18:21:03 -04:00
|
|
|
name: filename,
|
|
|
|
path: trashedpath,
|
|
|
|
ogpath: basepath,
|
|
|
|
trashinfo: path,
|
|
|
|
trashed: date,
|
2024-06-18 21:08:52 -04:00
|
|
|
isdir: info.IsDir(),
|
2024-06-18 18:21:03 -04:00
|
|
|
filesize: info.Size(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
log.Debugf("(ignored) %s: deleted on %s", filename, date.Format(trash_info_date_fmt))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if outerr != nil {
|
2024-07-03 18:05:17 -04:00
|
|
|
return Files{}, outerr
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
func Restore(files Files) (restored int, err error) {
|
|
|
|
for _, maybeFile := range files {
|
|
|
|
file, ok := maybeFile.(TrashInfo)
|
|
|
|
if !ok {
|
|
|
|
return restored, fmt.Errorf("bad file?? %s", maybeFile.Name())
|
|
|
|
}
|
|
|
|
|
2024-06-28 13:45:44 -04:00
|
|
|
var outpath string = dirs.UnEscape(file.ogpath)
|
2024-06-28 14:27:01 -04:00
|
|
|
var cancel bool
|
2024-06-28 13:45:44 -04:00
|
|
|
log.Infof("restoring %s back to %s\n", file.name, outpath)
|
2024-06-28 14:27:01 -04:00
|
|
|
if _, e := os.Stat(outpath); e == nil {
|
|
|
|
outpath, cancel = promptNewPath(outpath)
|
|
|
|
}
|
2024-07-15 18:59:40 -04:00
|
|
|
|
2024-06-28 14:27:01 -04:00
|
|
|
if cancel {
|
|
|
|
continue
|
|
|
|
}
|
2024-07-15 18:59:40 -04:00
|
|
|
|
|
|
|
basedir := filepath.Dir(outpath)
|
|
|
|
if _, e := os.Stat(basedir); e != nil {
|
|
|
|
if err = os.MkdirAll(basedir, fs.FileMode(0755)); err != nil {
|
|
|
|
return restored, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-28 13:45:44 -04:00
|
|
|
if err = os.Rename(file.path, outpath); err != nil {
|
2024-06-18 18:21:03 -04:00
|
|
|
return restored, err
|
|
|
|
}
|
2024-07-15 18:59:40 -04:00
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
if err = os.Remove(file.trashinfo); err != nil {
|
|
|
|
return restored, err
|
|
|
|
}
|
2024-07-15 18:59:40 -04:00
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
restored++
|
|
|
|
}
|
|
|
|
return restored, err
|
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
func Remove(files Files) (removed int, err error) {
|
|
|
|
for _, maybeFile := range files {
|
|
|
|
file, ok := maybeFile.(TrashInfo)
|
|
|
|
if !ok {
|
|
|
|
return removed, fmt.Errorf("bad file?? %s", maybeFile.Name())
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
log.Infof("removing %s permanently forever!!!", file.name)
|
|
|
|
if err = os.Remove(file.path); err != nil {
|
2024-06-18 19:56:03 -04:00
|
|
|
if i, e := os.Stat(file.path); e == nil && i.IsDir() {
|
|
|
|
err = os.RemoveAll(file.path)
|
|
|
|
if err != nil {
|
|
|
|
return removed, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return removed, err
|
|
|
|
}
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
if err = os.Remove(file.trashinfo); err != nil {
|
|
|
|
return removed, err
|
|
|
|
}
|
|
|
|
removed++
|
|
|
|
}
|
|
|
|
return removed, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TrashFile(trashDir, name string) error {
|
2024-06-21 01:43:21 -04:00
|
|
|
trashinfo_filename, out_path := ensureUniqueName(filepath.Base(name), trashDir)
|
2024-06-20 14:40:28 -04:00
|
|
|
|
|
|
|
// TODO: write across filesystems
|
2024-06-18 18:21:03 -04:00
|
|
|
if err := os.Rename(name, out_path); err != nil {
|
2024-06-30 18:06:52 -04:00
|
|
|
if strings.Contains(err.Error(), "invalid cross-device link") {
|
|
|
|
return fmt.Errorf("not trashing file '%s': On different filesystem from trash directory", name)
|
|
|
|
}
|
2024-06-18 18:21:03 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
trash_info, err := formatter.Format(trash_info_template, formatter.Named{
|
|
|
|
"path": name,
|
|
|
|
"date": time.Now().Format(trash_info_date_fmt),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.WriteFile(trashinfo_filename, []byte(trash_info), fs.FileMode(0600)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TrashFiles(trashDir string, files ...string) (trashed int, err error) {
|
|
|
|
for _, file := range files {
|
|
|
|
if err = TrashFile(trashDir, file); err != nil {
|
|
|
|
return trashed, err
|
|
|
|
}
|
|
|
|
trashed++
|
|
|
|
}
|
|
|
|
return trashed, err
|
|
|
|
}
|
|
|
|
|
2024-06-25 00:54:46 -04:00
|
|
|
func randomFilename(length int) string {
|
2024-06-20 14:40:28 -04:00
|
|
|
out := strings.Builder{}
|
|
|
|
for range length {
|
|
|
|
out.WriteByte(randomChar())
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomChar() byte {
|
|
|
|
const chars string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
|
|
|
return chars[rand.Intn(len(chars))]
|
|
|
|
}
|
2024-06-21 01:43:21 -04:00
|
|
|
|
|
|
|
func ensureUniqueName(filename, trashDir string) (string, string) {
|
|
|
|
var (
|
|
|
|
filedir = filepath.Join(trashDir, "files")
|
|
|
|
infodir = filepath.Join(trashDir, "info")
|
|
|
|
)
|
|
|
|
|
|
|
|
info := filepath.Join(infodir, filename+trash_info_ext)
|
|
|
|
if _, err := os.Stat(info); os.IsNotExist(err) {
|
|
|
|
// doesn't exist, so use it
|
|
|
|
path := filepath.Join(filedir, filename)
|
|
|
|
return info, path
|
|
|
|
} else {
|
|
|
|
// otherwise, try random suffixes until one works
|
|
|
|
log.Debugf("%s exists in trash, generating random name", filename)
|
|
|
|
var tries int
|
|
|
|
for {
|
|
|
|
tries++
|
2024-06-25 00:54:46 -04:00
|
|
|
rando := randomFilename(random_str_length)
|
2024-06-21 01:43:21 -04:00
|
|
|
new_name := filepath.Join(infodir, filename+rando+trash_info_ext)
|
|
|
|
if _, err := os.Stat(new_name); os.IsNotExist(err) {
|
|
|
|
path := filepath.Join(filedir, filename+rando)
|
|
|
|
log.Debugf("settled on random name %s%s on the %s try", filename, rando, humanize.Ordinal(tries))
|
|
|
|
return new_name, path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-28 14:27:01 -04:00
|
|
|
|
|
|
|
func promptNewPath(path string) (string, bool) {
|
|
|
|
for {
|
|
|
|
answer := prompt.AskRune(fmt.Sprintf("file %s exists, overwrite, rename, or cancel?", path), "o/r/c")
|
|
|
|
switch answer {
|
|
|
|
case 'o', 'O':
|
|
|
|
return path, false
|
|
|
|
case 'r', 'R':
|
|
|
|
if err := huh.NewInput().
|
|
|
|
Title("input a new filename").
|
|
|
|
Value(&path).
|
|
|
|
Run(); err != nil {
|
|
|
|
return path, false
|
|
|
|
}
|
|
|
|
if _, e := os.Stat(path); e != nil {
|
|
|
|
return path, false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return path, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|