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-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-07-30 14:43:54 -04:00
|
|
|
executePerm = fs.FileMode(0755)
|
|
|
|
noExecuteUserPerm = fs.FileMode(0600)
|
|
|
|
randomStrLength int = 8
|
|
|
|
trashInfoExt string = ".trashinfo"
|
|
|
|
trashInfoSec string = "Trash Info"
|
|
|
|
trashInfoPath string = "Path"
|
|
|
|
trashInfoDate string = "DeletionDate"
|
|
|
|
trashInfoDateFmt string = "2006-01-02T15:04:05"
|
|
|
|
trashInfoTemplate string = `[Trash Info]
|
2024-06-18 18:21:03 -04:00
|
|
|
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-16 00:05:21 -04:00
|
|
|
mode fs.FileMode
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
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-16 00:05:21 -04:00
|
|
|
func (t TrashInfo) Mode() fs.FileMode { return t.mode }
|
2024-07-15 18:06:53 -04:00
|
|
|
func (t TrashInfo) Filesize() int64 {
|
|
|
|
if t.isdir {
|
2024-07-21 23:54:22 -04:00
|
|
|
return 0
|
2024-07-15 18:06:53 -04:00
|
|
|
}
|
|
|
|
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-30 14:43:54 -04:00
|
|
|
func FindTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
|
|
|
|
var files Files
|
|
|
|
outerr := filepath.WalkDir(trashdir, func(path string, dirEntry fs.DirEntry, err error) error {
|
2024-06-18 18:21:03 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Debugf("what happened?? what is %s?", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore self, directories, and non trashinfo files
|
2024-07-30 14:43:54 -04:00
|
|
|
if path == trashdir || dirEntry.IsDir() || filepath.Ext(path) != trashInfoExt {
|
2024-06-18 18:21:03 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// trashinfo is just an ini file, so
|
|
|
|
c, err := ini.Load(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-30 14:43:54 -04:00
|
|
|
if section := c.Section(trashInfoSec); section != nil {
|
|
|
|
basepath := section.Key(trashInfoPath).String()
|
2024-06-19 18:55:01 -04:00
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
filename := filepath.Base(basepath)
|
2024-07-30 14:43:54 -04:00
|
|
|
trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trashInfoExt, "", 1)
|
2024-07-16 01:41:34 -04:00
|
|
|
info, err := os.Lstat(trashedpath)
|
2024-06-20 14:40:28 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error reading %s: %s", trashedpath, err)
|
2024-07-16 00:31:22 -04:00
|
|
|
return nil
|
2024-06-20 14:40:28 -04:00
|
|
|
}
|
2024-06-18 18:21:03 -04:00
|
|
|
|
2024-07-30 14:43:54 -04:00
|
|
|
s := section.Key(trashInfoDate).Value()
|
|
|
|
date, err := time.ParseInLocation(trashInfoDateFmt, s, time.Local)
|
2024-06-18 18:21:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-18 20:02:49 -04:00
|
|
|
if ogdir != "" && filepath.Dir(basepath) != ogdir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-30 14:43:54 -04:00
|
|
|
if fltr.Match(info) {
|
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(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if outerr != nil {
|
2024-07-03 18:05:17 -04:00
|
|
|
return Files{}, outerr
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
2024-07-30 14:43:54 -04:00
|
|
|
return files, nil
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
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 14:27:01 -04:00
|
|
|
var cancel bool
|
2024-07-30 14:43:54 -04:00
|
|
|
outpath := dirs.UnEscape(file.ogpath)
|
2024-06-28 13:45:44 -04:00
|
|
|
log.Infof("restoring %s back to %s\n", file.name, outpath)
|
2024-07-16 01:41:34 -04:00
|
|
|
if _, e := os.Lstat(outpath); e == nil {
|
2024-07-27 19:42:23 -04:00
|
|
|
outpath, cancel = prompt.NewPath(outpath)
|
2024-06-28 14:27:01 -04:00
|
|
|
}
|
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 {
|
2024-07-30 14:43:54 -04:00
|
|
|
if err = os.MkdirAll(basedir, executePerm); err != nil {
|
2024-07-15 18:59:40 -04:00
|
|
|
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-27 19:42:23 -04:00
|
|
|
func ConfirmRestore(confirm bool, fs Files) error {
|
|
|
|
if !confirm || prompt.YesNo(fmt.Sprintf("restore %d selected files?", len(fs))) {
|
|
|
|
log.Info("doing the thing")
|
|
|
|
restored, err := Restore(fs)
|
|
|
|
if err != nil {
|
2024-07-30 14:43:54 -04:00
|
|
|
return fmt.Errorf("restored %d files before error %w", restored, err)
|
2024-07-27 19:42:23 -04:00
|
|
|
}
|
2024-07-30 14:43:54 -04:00
|
|
|
fmt.Fprintf(os.Stdout, "restored %d files\n", restored)
|
2024-07-27 19:42:23 -04:00
|
|
|
} else {
|
2024-07-30 14:43:54 -04:00
|
|
|
fmt.Fprintf(os.Stdout, "not doing anything\n")
|
2024-07-27 19:42:23 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if err = os.Remove(file.path); err != nil {
|
2024-07-16 01:41:34 -04:00
|
|
|
if i, e := os.Lstat(file.path); e == nil && i.IsDir() {
|
2024-06-18 19:56:03 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-27 19:42:23 -04:00
|
|
|
func ConfirmClean(confirm bool, fs Files) error {
|
|
|
|
if prompt.YesNo(fmt.Sprintf("remove %d selected files permanently from the trash?", len(fs))) &&
|
|
|
|
(!confirm || prompt.YesNo(fmt.Sprintf("really remove all these %d selected files permanently from the trash forever??", len(fs)))) {
|
|
|
|
removed, err := Remove(fs)
|
|
|
|
if err != nil {
|
2024-07-30 14:43:54 -04:00
|
|
|
return fmt.Errorf("removed %d files before error %w", removed, err)
|
2024-07-27 19:42:23 -04:00
|
|
|
}
|
2024-07-30 14:43:54 -04:00
|
|
|
fmt.Fprintf(os.Stdout, "removed %d files\n", removed)
|
2024-07-27 19:42:23 -04:00
|
|
|
} else {
|
2024-07-30 14:43:54 -04:00
|
|
|
fmt.Fprintf(os.Stdout, "not doing anything\n")
|
2024-07-27 19:42:23 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
func TrashFile(trashDir, name string) error {
|
2024-07-30 14:43:54 -04:00
|
|
|
trashinfoFilename, outPath := ensureUniqueName(filepath.Base(name), trashDir)
|
2024-06-20 14:40:28 -04:00
|
|
|
|
2024-07-30 14:43:54 -04:00
|
|
|
if err := os.Rename(name, outPath); err != nil {
|
2024-06-30 18:06:52 -04:00
|
|
|
if strings.Contains(err.Error(), "invalid cross-device link") {
|
2024-07-30 16:58:06 -04:00
|
|
|
// TODO: use $topdir/.Trash as per XDG spec
|
|
|
|
// TODO: maybe figure out if filesystem is truly different or is a btrfs subvolume
|
|
|
|
return err
|
2024-06-30 18:06:52 -04:00
|
|
|
}
|
2024-06-18 18:21:03 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-30 14:43:54 -04:00
|
|
|
trashInfo, err := formatter.Format(trashInfoTemplate, formatter.Named{
|
2024-06-18 18:21:03 -04:00
|
|
|
"path": name,
|
2024-07-30 14:43:54 -04:00
|
|
|
"date": time.Now().Format(trashInfoDateFmt),
|
2024-06-18 18:21:03 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-30 14:43:54 -04:00
|
|
|
if err := os.WriteFile(trashinfoFilename, []byte(trashInfo), noExecuteUserPerm); err != nil {
|
2024-06-18 18:21:03 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-30 16:48:52 -04:00
|
|
|
func TrashFiles(trashDir string, files ...string) (trashed int) {
|
2024-06-18 18:21:03 -04:00
|
|
|
for _, file := range files {
|
2024-07-30 16:48:52 -04:00
|
|
|
if err := TrashFile(trashDir, file); err != nil {
|
2024-07-30 16:58:06 -04:00
|
|
|
log.Errorf("cannot trash '%s': %s", file, err)
|
2024-07-30 16:48:52 -04:00
|
|
|
continue
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
trashed++
|
|
|
|
}
|
2024-07-30 16:48:52 -04:00
|
|
|
return trashed
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
2024-07-27 19:42:23 -04:00
|
|
|
func ConfirmTrash(confirm bool, fs Files, trashDir string) error {
|
|
|
|
if !confirm || prompt.YesNo(fmt.Sprintf("trash %d selected files?", len(fs))) {
|
|
|
|
tfs := make([]string, 0, len(fs))
|
|
|
|
for _, file := range fs {
|
|
|
|
log.Debugf("gonna trash %s", file.Path())
|
|
|
|
tfs = append(tfs, file.Path())
|
|
|
|
}
|
|
|
|
|
2024-07-30 16:48:52 -04:00
|
|
|
trashed := TrashFiles(trashDir, tfs...)
|
|
|
|
|
2024-07-30 14:43:54 -04:00
|
|
|
var files string
|
2024-07-27 19:42:23 -04:00
|
|
|
if trashed == 1 {
|
2024-07-30 14:43:54 -04:00
|
|
|
files = "file"
|
2024-07-27 19:42:23 -04:00
|
|
|
} else {
|
2024-07-30 14:43:54 -04:00
|
|
|
files = "files"
|
2024-07-27 19:42:23 -04:00
|
|
|
}
|
2024-07-30 14:43:54 -04:00
|
|
|
fmt.Fprintf(os.Stdout, "trashed %d %s\n", trashed, files)
|
2024-07-27 19:42:23 -04:00
|
|
|
} else {
|
2024-07-30 14:43:54 -04:00
|
|
|
fmt.Fprintf(os.Stdout, "not doing anything\n")
|
2024-07-27 19:42:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
)
|
|
|
|
|
2024-07-30 14:43:54 -04:00
|
|
|
info := filepath.Join(infodir, filename+trashInfoExt)
|
2024-06-21 01:43:21 -04:00
|
|
|
if _, err := os.Stat(info); os.IsNotExist(err) {
|
|
|
|
// doesn't exist, so use it
|
|
|
|
path := filepath.Join(filedir, filename)
|
|
|
|
return info, path
|
2024-07-31 03:13:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, try random suffixes until one works
|
|
|
|
log.Debugf("%s exists in trash, generating random name", filename)
|
|
|
|
var tries int
|
|
|
|
for {
|
|
|
|
tries++
|
|
|
|
rando := randomFilename(randomStrLength)
|
|
|
|
newName := filepath.Join(infodir, filename+rando+trashInfoExt)
|
|
|
|
if _, err := os.Stat(newName); 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 newName, path
|
2024-06-21 01:43:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|