Compare commits

..

No commits in common. "1339d642dd5a4225d5b717c94452d811c67d5a30" and "03c09e1da6253a4b468b2c44b01c26e4f09ed394" have entirely different histories.

14 changed files with 264 additions and 661 deletions

View file

@ -23,7 +23,7 @@ Find files on the filesystem based on the filter flags and any filename args.
*--recursive*, *-r*
operate on files recursively
*--work-dir* **dir**, *-w* **dir**
*--work-dir* dir, *-w* dir
operate on files in this directory
*--hidden*, *-h*
@ -38,7 +38,7 @@ Find files in the trash based on the filter flags and any filename args.
*--non-interactive*, *-n*
list files and quit
*--original-path* **dir**, *-O* **dir**
*--original-path* dir, *-O* dir
list files trashed from this directory
### restore / re
@ -50,8 +50,8 @@ Find files in the trash based on the filter flags and any filename args.
*--all*, *-a*
restore all files in trash
*--original-path* **dir**, *-O* **dir**
*restore* files trashed from this directory
*--original-path* dir, *-O* dir
restore files trashed from this directory
### clean / cl
@ -62,7 +62,7 @@ Find files in the trash based on the filter flags and any filename args.
*--all*, *-a*
clean all files in trash
*--original-path* **dir**, *-O* **dir**
*--original-path* dir, *-O* dir
remove files trashed from this directory
## Flags
@ -72,30 +72,30 @@ remove files trashed from this directory
*--confirm*, *-c*
ask for confirmation before executing any action
*--log* **level**, *-l* **level**
*--log* level, *-l* level
set log level
### Filter flags (usable with all commands)
*--match* **pattern**, *-m* **pattern**
*--match* pattern, *-m* pattern
operate on files matching regex pattern
*--glob* **pattern**, *-m* **pattern**
*--glob* pattern, *-m* pattern
operate on files matching glob
*--not-match* **pattern**, *-M* **pattern**
*--not-match* pattern, *-M* pattern
operate on files not matching regex pattern
*--not-glob* **pattern**, *-G* **pattern**
*--not-glob* pattern, *-G* pattern
operate on files not matching glob
*--on* **date**, *-O* **date**
*--on* date, *-O* date
operate on files modified on date
*--before* **date**, *-B* **date**
*--before* date, *-B* date
operate on files modified before date
*--after* **date**, *-A* **date**
*--after* date, *-A* date
operate on files modified after date
*--files-only*, *-F*
@ -104,13 +104,13 @@ operate on files only
*--dirs-only*, *-D*
operate on directories only
*--min-size* **size**, *-N* **size**
*--min-size* size, *-N* size
operate on files larger than size
*--max-size* **size**, *-X* **size**
*--max-size* size, *-X* size
operate on files smaller than size
*--mode* **mode**, *-x* **mode**
*--mode* mode, *-x* mode
operate on files matching mode mode
See also gt(1) or `gt --help`.

1
go.mod
View file

@ -12,7 +12,6 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/ijt/go-anytime v1.9.2
github.com/lithammer/fuzzysearch v1.1.8
github.com/moby/sys/mountinfo v0.7.2
github.com/urfave/cli/v2 v2.27.3
gitlab.com/tymonx/go-formatter v1.5.1
golang.org/x/term v0.22.0

2
go.sum
View file

@ -58,8 +58,6 @@ github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6T
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=
github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=

2
gt.1
View file

@ -5,7 +5,7 @@
.nh
.ad l
.\" Begin generated content:
.TH "gt" "1" "2024-07-31" "gt version v0.0.3" "User Commands"
.TH "gt" "1" "2024-07-31" "gt version v0.0.2" "User Commands"
.PP
.SH NAME
.PP

View file

@ -1,8 +1,8 @@
gt(1) ["gt version v0.0.3" ["User Commands"]]
gt(1) ["gt version v0.0.2" ["User Commands"]]
# NAME
gt \- manual page for gt version 0.0.3
gt \- manual page for gt version 0.0.2
# DESCRIPTION
@ -20,7 +20,7 @@ g(o)t(rash) is a simple, command line program to interface with the XDG Trash. F
## VERSION:
0.0.3
0.0.2
## AUTHOR:

View file

@ -1,4 +1,4 @@
// Package dirs provides functions to sanitize directory and file names.
// Package dirs provides functions sanitize directory names.
package dirs
import (
@ -7,20 +7,14 @@ import (
"strings"
)
const (
sep = string(os.PathSeparator)
space = " "
spacep = "%20"
newline = "\n"
newlinep = "%0A"
)
const sep = string(os.PathSeparator)
var (
home = os.Getenv("HOME")
pwd, _ = os.Getwd()
)
// UnExpand returns dir after unexpanding some directory shortcuts
// UnExpand returns dir after expanding some directory shortcuts
//
// $HOME -> ~
//
@ -41,7 +35,7 @@ func UnExpand(dir, workdir string) (outdir string) {
outdir = strings.Replace(outdir, home, "~", 1)
outdir = PercentDecode(outdir)
outdir = UnEscape(outdir)
if outdir == "" {
outdir = "/"
@ -50,25 +44,17 @@ func UnExpand(dir, workdir string) (outdir string) {
return
}
func PercentDecode(input string) (output string) {
output = strings.ReplaceAll(input, spacep, space)
output = strings.ReplaceAll(output, newlinep, newline)
return
func UnEscape(input string) string {
return strings.ReplaceAll(input, "%20", " ")
}
func PercentEncode(input string) (output string) {
output = strings.ReplaceAll(input, space, spacep)
output = strings.ReplaceAll(output, newline, newlinep)
return
}
func cleanDir(dir, pwd string) string {
func cleanDir(dir, pwd string) (out string) {
if strings.HasPrefix(dir, ".") {
return filepath.Clean(dir)
out = filepath.Clean(dir)
} else if !strings.HasPrefix(dir, sep) {
return filepath.Join(pwd, dir)
out = filepath.Join(pwd, dir)
} else {
out = dir
}
return dir
return
}

View file

@ -1,154 +0,0 @@
package files
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"git.burning.moe/celediel/gt/internal/dirs"
"github.com/charmbracelet/log"
)
const (
directorysizes = "directorysizes"
length int = 3
)
var (
loadedDirSizes directorySizes
)
func init() {
loadedDirSizes = readDirectorySizesFromFile()
}
type directorySize struct {
size int64
mtime int64
name string
}
type directorySizes map[string]directorySize
func WriteDirectorySizes() {
loadedDirSizes = updateDirectorySizes(loadedDirSizes)
writeDirectorySizes(loadedDirSizes)
}
func readDirectorySizesFromFile() directorySizes {
dirSizes := directorySizes{}
for _, trash := range getAllTrashes() {
dsf := filepath.Join(trash, directorysizes)
if _, err := os.Lstat(dsf); os.IsNotExist(err) {
continue
}
file, err := os.Open(dsf)
if err != nil {
log.Error(err)
continue
}
defer file.Close()
var (
size int64
mtime int64
name string
)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
split := strings.Split(line, " ")
if len(split) != length {
log.Errorf("malformed line '%s' in %s", line, dsf)
continue
}
size, err = strconv.ParseInt(split[0], 10, 64)
if err != nil {
log.Errorf("size %s can't be int?", split[0])
continue
}
mtime, err = strconv.ParseInt(split[1], 10, 64)
if err != nil {
log.Errorf("mtime %s can't be int?", split[1])
continue
}
name = dirs.PercentDecode(split[2])
dirSize := directorySize{
size: size,
mtime: mtime,
name: name,
}
dirSizes[name] = dirSize
}
}
return dirSizes
}
func updateDirectorySizes(ds directorySizes) directorySizes {
newDs := directorySizes{}
for k, v := range ds {
newDs[k] = v
}
for _, trash := range getAllTrashes() {
files, err := os.ReadDir(filepath.Join(trash, "files"))
if err != nil {
log.Error(err)
continue
}
for _, file := range files {
if _, ok := loadedDirSizes[file.Name()]; ok {
continue
}
info, err := file.Info()
if err != nil {
log.Error(err)
continue
}
if !info.IsDir() {
continue
}
newDs[info.Name()] = directorySize{
size: calculateDirSize(filepath.Join(trash, "files", info.Name())),
mtime: info.ModTime().Unix(),
name: info.Name(),
}
}
}
return newDs
}
func writeDirectorySizes(dirSizes directorySizes) {
// TODO: make this less bad
for _, trash := range getAllTrashes() {
var lines []string
out := filepath.Join(trash, directorysizes)
files, err := os.ReadDir(filepath.Join(trash, "files"))
if err != nil {
log.Error(err)
continue
}
for _, file := range files {
if dirSize, ok := dirSizes[file.Name()]; ok {
lines = append(lines, fmt.Sprintf("%d %d ", dirSize.size, dirSize.mtime)+dirs.PercentEncode(file.Name()))
}
}
err = os.WriteFile(out, []byte(strings.Join(lines, "\n")), noExecutePerm)
if err != nil {
log.Error(err)
}
}
}

View file

@ -26,7 +26,12 @@ func (f DiskFile) Path() string { return f.path }
func (f DiskFile) Date() time.Time { return f.modified }
func (f DiskFile) IsDir() bool { return f.isdir }
func (f DiskFile) Mode() fs.FileMode { return f.mode }
func (f DiskFile) Filesize() int64 { return f.filesize }
func (f DiskFile) Filesize() int64 {
if f.isdir {
return 0
}
return f.filesize
}
func (f DiskFile) String() string {
// this is unique enough because two files can't be named the same in the same directory
@ -48,29 +53,21 @@ func NewDisk(path string) (DiskFile, error) {
name := filepath.Base(abs)
basePath := filepath.Dir(abs)
actualPath := filepath.Join(basePath, name)
var size int64
if info.IsDir() {
size = calculateDirSize(actualPath)
} else {
size = info.Size()
}
log.Debugf("%s (base:%s) (size:%s) (modified:%s) exists",
name, basePath, humanize.Bytes(uint64(size)), info.ModTime())
name, basePath, humanize.Bytes(uint64(info.Size())), info.ModTime())
return DiskFile{
name: name,
path: actualPath,
filesize: size,
path: filepath.Join(basePath, name),
filesize: info.Size(),
modified: info.ModTime(),
isdir: info.IsDir(),
mode: info.Mode(),
}, nil
}
func FindDisk(dir string, recursive bool, fltr *filter.Filter) Files {
func FindDisk(dir string, recursive bool, fltr *filter.Filter) (Files, error) {
var files Files
dir = filepath.Clean(dir)
if dir == "." || dir == "" {
@ -94,7 +91,7 @@ func FindDisk(dir string, recursive bool, fltr *filter.Filter) Files {
files = append(files, readDir(dir, fltr)...)
}
return files
return files, nil
}
// isInHiddenDir checks `path` and parent directories
@ -133,17 +130,10 @@ func walkDir(dir string, fltr *filter.Filter) Files {
name := dirEntry.Name()
info, _ := dirEntry.Info()
if fltr.Match(info) {
var size int64
if info.IsDir() {
size = calculateDirSize(filepath.Join(actualPath, name))
} else {
size = info.Size()
}
files = append(files, DiskFile{
path: actualPath,
name: name,
filesize: size,
filesize: info.Size(),
modified: info.ModTime(),
isdir: info.IsDir(),
mode: info.Mode(),
@ -153,7 +143,7 @@ func walkDir(dir string, fltr *filter.Filter) Files {
})
if err != nil {
log.Errorf("error walking directory %s: %s", dir, err)
return nil
return Files{}
}
return files
}
@ -162,7 +152,7 @@ func readDir(dir string, fltr *filter.Filter) Files {
var files Files
fs, err := os.ReadDir(dir)
if err != nil {
return nil
return Files{}
}
for _, file := range fs {
name := file.Name()
@ -173,28 +163,17 @@ func readDir(dir string, fltr *filter.Filter) Files {
info, err := file.Info()
if err != nil {
return nil
return Files{}
}
path := filepath.Dir(filepath.Join(dir, name))
actualPath, e := filepath.Abs(path)
if e != nil {
continue
}
if fltr.Match(info) {
var size int64
if info.IsDir() {
size = calculateDirSize(filepath.Join(actualPath, name))
} else {
size = info.Size()
}
files = append(files, DiskFile{
name: name,
path: filepath.Join(actualPath, name),
path: filepath.Join(path, name),
modified: info.ModTime(),
filesize: size,
filesize: info.Size(),
isdir: info.IsDir(),
mode: info.Mode(),
})

View file

@ -3,15 +3,11 @@ package files
import (
"cmp"
"fmt"
"io/fs"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/charmbracelet/log"
)
type File interface {
@ -26,35 +22,6 @@ type File interface {
type Files []File
func (fls Files) String() string {
var out = strings.Builder{}
for _, file := range fls {
out.WriteString(fmt.Sprintf("%s\t%s\t%s\n",
file.Date().Format(time.RFC3339), file.Name(), file.Path(),
))
}
return out.String()
}
func (fls Files) TotalSize() int64 {
var size int64
for _, file := range fls {
if file.IsDir() {
if d, ok := loadedDirSizes[file.Name()]; ok {
log.Debugf("%s: got %d from directorysizes", file.Name(), d.size)
size += d.size
continue
} else {
size += calculateDirSize(file.Path())
}
}
size += file.Filesize()
}
return size
}
func SortByModified(a, b File) int {
if a.Date().Before(b.Date()) {
return 1
@ -74,11 +41,15 @@ func SortByModifiedReverse(a, b File) int {
}
func SortBySize(a, b File) int {
return cmp.Compare(a.Filesize(), b.Filesize())
as := getSortingSize(a)
bs := getSortingSize(b)
return cmp.Compare(as, bs)
}
func SortBySizeReverse(a, b File) int {
return cmp.Compare(b.Filesize(), a.Filesize())
as := getSortingSize(a)
bs := getSortingSize(b)
return cmp.Compare(bs, as)
}
func SortByName(a, b File) int {
@ -141,36 +112,9 @@ func doNameSort(a, b File) int {
return cmp.Compare(aname, bname)
}
func calculateDirSize(path string) int64 {
var size int64
info, err := os.Lstat(path)
if err != nil {
log.Error(err)
return 0
func getSortingSize(f File) int64 {
if f.IsDir() {
return -1
}
if !info.IsDir() {
return 0
}
files, err := os.ReadDir(path)
if err != nil {
log.Error(err)
return 0
}
for _, file := range files {
filePath := filepath.Join(path, file.Name())
info, err := os.Lstat(filePath)
if err != nil {
log.Error(err)
return 0
}
if info.IsDir() {
size += calculateDirSize(filePath)
} else {
size += info.Size()
}
}
return size
return f.Filesize()
}

View file

@ -5,9 +5,7 @@ import (
"io/fs"
"math/rand"
"os"
"os/user"
"path/filepath"
"slices"
"strings"
"time"
@ -15,20 +13,16 @@ import (
"git.burning.moe/celediel/gt/internal/filter"
"git.burning.moe/celediel/gt/internal/prompt"
"github.com/adrg/xdg"
"github.com/charmbracelet/log"
"github.com/dustin/go-humanize"
"github.com/moby/sys/mountinfo"
"gitlab.com/tymonx/go-formatter/formatter"
"gopkg.in/ini.v1"
)
const (
executePerm = fs.FileMode(0755)
noExecutePerm = fs.FileMode(0644)
noExecuteUserPerm = fs.FileMode(0600)
randomStrLength int = 8
trashName string = ".Trash"
trashInfoExt string = ".trashinfo"
trashInfoSec string = "Trash Info"
trashInfoPath string = "Path"
@ -40,8 +34,6 @@ DeletionDate={date}
`
)
var homeTrash = filepath.Join(xdg.DataHome, "Trash")
type TrashInfo struct {
name, ogpath string
path, trashinfo string
@ -58,209 +50,77 @@ 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 }
func (t TrashInfo) Mode() fs.FileMode { return t.mode }
func (t TrashInfo) Filesize() int64 { return t.filesize }
func (t TrashInfo) Filesize() int64 {
if t.isdir {
return 0
}
return t.filesize
}
func (t TrashInfo) String() string {
return t.name + t.path + t.ogpath + t.trashinfo
}
func FindInAllTrashes(ogdir string, fltr *filter.Filter) Files {
func FindTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
var files Files
for _, trash := range getAllTrashes() {
fls, err := findTrash(trash, ogdir, fltr)
outerr := filepath.WalkDir(trashdir, func(path string, dirEntry fs.DirEntry, err error) error {
if err != nil {
log.Errorf("error reading trash dir '%s': %s", trash, err)
continue
}
files = append(files, fls...)
}
return files
}
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 {
return fmt.Errorf("restored %d files before error %w", restored, err)
}
fmt.Fprintf(os.Stdout, "restored %d files\n", restored)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
}
return nil
}
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 {
return fmt.Errorf("removed %d files before error %w", removed, err)
}
fmt.Fprintf(os.Stdout, "removed %d files\n", removed)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
}
return nil
}
func ConfirmTrash(confirm bool, fs Files) error {
if !confirm || prompt.YesNo(fmt.Sprintf("trash %d selected files?", len(fs))) {
tfs := make([]string, 0, len(fs))
for _, file := range fs {
tfs = append(tfs, file.Path())
log.Debugf("what happened?? what is %s?", err)
return err
}
trashed := trashFiles(tfs)
var s string
if trashed > 1 {
s = "s"
// ignore self, directories, and non trashinfo files
if path == trashdir || dirEntry.IsDir() || filepath.Ext(path) != trashInfoExt {
return nil
}
fmt.Fprintf(os.Stdout, "trashed %d file%s\n", trashed, s)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
return nil
}
return nil
}
func findTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
log.Debugf("searching for trashinfo files in %s", trashdir)
var files Files
infodir := filepath.Join(trashdir, "info")
entries, err := os.ReadDir(infodir)
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.IsDir() || filepath.Ext(entry.Name()) != trashInfoExt {
continue
}
path := filepath.Join(infodir, entry.Name())
// trashinfo is just an ini file, so
trashInfo, err := ini.Load(path)
c, err := ini.Load(path)
if err != nil {
log.Errorf("error reading %s: %s", path, err)
continue
return err
}
if section := c.Section(trashInfoSec); section != nil {
basepath := section.Key(trashInfoPath).String()
section := trashInfo.Section(trashInfoSec)
if section == nil {
continue
}
filename := filepath.Base(basepath)
trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trashInfoExt, "", 1)
info, err := os.Lstat(trashedpath)
if err != nil {
log.Errorf("error reading %s: %s", trashedpath, err)
return nil
}
basepath := dirs.PercentDecode(section.Key(trashInfoPath).String())
if !strings.HasPrefix(basepath, string(os.PathSeparator)) {
root, err := getRoot(trashdir)
if err == nil {
basepath = filepath.Join(root, basepath)
s := section.Key(trashInfoDate).Value()
date, err := time.ParseInLocation(trashInfoDateFmt, s, time.Local)
if err != nil {
return err
}
if ogdir != "" && filepath.Dir(basepath) != ogdir {
return nil
}
if fltr.Match(info) {
files = append(files, TrashInfo{
name: filename,
path: trashedpath,
ogpath: basepath,
trashinfo: path,
trashed: date,
isdir: info.IsDir(),
filesize: info.Size(),
})
}
}
filename := filepath.Base(basepath)
trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trashInfoExt, "", 1)
info, err := os.Lstat(trashedpath)
if err != nil {
log.Errorf("error reading '%s': %s", trashedpath, err)
continue
}
s := section.Key(trashInfoDate).Value()
date, err := time.ParseInLocation(trashInfoDateFmt, s, time.Local)
if err != nil {
log.Errorf("error parsing date '%s' in trashinfo file '%s': %s", s, path, err)
continue
}
if ogdir != "" && filepath.Dir(basepath) != ogdir {
continue
}
var size int64
if d, ok := loadedDirSizes[info.Name()]; ok {
size = d.size
} else if info.IsDir() {
size = calculateDirSize(trashedpath)
} else {
size = info.Size()
}
if fltr.Match(info) {
files = append(files, TrashInfo{
name: filename,
path: trashedpath,
ogpath: basepath,
trashinfo: path,
trashed: date,
isdir: info.IsDir(),
filesize: size,
})
}
return nil
})
if outerr != nil {
return Files{}, outerr
}
return files, nil
}
func trashFile(filename string) error {
trashDir, err := getTrashDir(filename)
if err != nil {
return err
}
trashInfoFilename, outPath := getTrashFilenames(filepath.Base(filename), trashDir)
if err := os.Rename(filename, outPath); err != nil {
return err
}
var path string
if trashDir == homeTrash {
path = filename
} else {
root, err := getRoot(trashDir)
if err != nil {
path = filename
} else {
path = strings.Replace(filename, root+string(os.PathSeparator), "", 1)
}
}
log.Debugf("fucking %s %s %s", filename, trashDir, path)
trashInfo, err := formatter.Format(trashInfoTemplate, formatter.Named{
"path": path,
"date": time.Now().Format(trashInfoDateFmt),
})
if err != nil {
return err
}
if err := os.WriteFile(trashInfoFilename, []byte(trashInfo), noExecuteUserPerm); err != nil {
return err
}
return nil
}
func trashFiles(files []string) (trashed int) {
for _, file := range files {
if err := trashFile(file); err != nil {
log.Errorf("cannot trash '%s': %s", file, err)
continue
}
trashed++
}
return
}
func restore(files Files) (restored int, err error) {
func Restore(files Files) (restored int, err error) {
for _, maybeFile := range files {
file, ok := maybeFile.(TrashInfo)
if !ok {
@ -268,7 +128,7 @@ func restore(files Files) (restored int, err error) {
}
var cancel bool
outpath := dirs.PercentDecode(file.ogpath)
outpath := dirs.UnEscape(file.ogpath)
log.Infof("restoring %s back to %s\n", file.name, outpath)
if _, e := os.Lstat(outpath); e == nil {
outpath, cancel = prompt.NewPath(outpath)
@ -279,7 +139,7 @@ func restore(files Files) (restored int, err error) {
}
basedir := filepath.Dir(outpath)
if _, e := os.Lstat(basedir); e != nil {
if _, e := os.Stat(basedir); e != nil {
if err = os.MkdirAll(basedir, executePerm); err != nil {
return restored, err
}
@ -298,7 +158,21 @@ func restore(files Files) (restored int, err error) {
return restored, err
}
func remove(files Files) (removed int, err error) {
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 {
return fmt.Errorf("restored %d files before error %w", restored, err)
}
fmt.Fprintf(os.Stdout, "restored %d files\n", restored)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
}
return nil
}
func Remove(files Files) (removed int, err error) {
for _, maybeFile := range files {
file, ok := maybeFile.(TrashInfo)
if !ok {
@ -323,7 +197,82 @@ func remove(files Files) (removed int, err error) {
return removed, err
}
func randomString(length int) string {
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 {
return fmt.Errorf("removed %d files before error %w", removed, err)
}
fmt.Fprintf(os.Stdout, "removed %d files\n", removed)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
}
return nil
}
func TrashFile(trashDir, name string) error {
trashinfoFilename, outPath := ensureUniqueName(filepath.Base(name), trashDir)
if err := os.Rename(name, outPath); err != nil {
if strings.Contains(err.Error(), "invalid cross-device link") {
// TODO: use $topdir/.Trash as per XDG spec
// TODO: maybe figure out if filesystem is truly different or is a btrfs subvolume
return err
}
return err
}
trashInfo, err := formatter.Format(trashInfoTemplate, formatter.Named{
"path": name,
"date": time.Now().Format(trashInfoDateFmt),
})
if err != nil {
return err
}
if err := os.WriteFile(trashinfoFilename, []byte(trashInfo), noExecuteUserPerm); err != nil {
return err
}
return nil
}
func TrashFiles(trashDir string, files ...string) (trashed int) {
for _, file := range files {
if err := TrashFile(trashDir, file); err != nil {
log.Errorf("cannot trash '%s': %s", file, err)
continue
}
trashed++
}
return trashed
}
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())
}
trashed := TrashFiles(trashDir, tfs...)
var files string
if trashed == 1 {
files = "file"
} else {
files = "files"
}
fmt.Fprintf(os.Stdout, "trashed %d %s\n", trashed, files)
} else {
fmt.Fprintf(os.Stdout, "not doing anything\n")
return nil
}
return nil
}
func randomFilename(length int) string {
out := strings.Builder{}
for range length {
out.WriteByte(randomChar())
@ -336,17 +285,17 @@ func randomChar() byte {
return chars[rand.Intn(len(chars))]
}
func getTrashFilenames(filename, trashDir string) (string, string) {
func ensureUniqueName(filename, trashDir string) (string, string) {
var (
filedir = filepath.Join(trashDir, "files")
infodir = filepath.Join(trashDir, "info")
)
info := filepath.Join(infodir, filename+trashInfoExt)
if _, err := os.Lstat(info); os.IsNotExist(err) {
if _, err := os.Stat(info); os.IsNotExist(err) {
// doesn't exist, so use it
path := filepath.Join(filedir, filename)
return dirs.PercentEncode(info), dirs.PercentEncode(path)
return info, path
}
// otherwise, try random suffixes until one works
@ -354,106 +303,12 @@ func getTrashFilenames(filename, trashDir string) (string, string) {
var tries int
for {
tries++
rando := randomString(randomStrLength)
newInfo := filepath.Join(infodir, filename+rando+trashInfoExt)
newFile := filepath.Join(filedir, filename+rando)
_, infoErr := os.Lstat(newInfo)
_, fileErr := os.Lstat(newFile)
if os.IsNotExist(infoErr) && os.IsNotExist(fileErr) {
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 dirs.PercentEncode(newInfo), dirs.PercentEncode(path)
return newName, path
}
}
}
func getTrashDir(filename string) (string, error) {
root, err := getRoot(filename)
if err != nil {
return "", err
}
var trashDir string
if strings.Contains(filename, xdg.Home) {
trashDir = filepath.Join(xdg.DataHome, trashName[1:])
} else {
trashDir = filepath.Clean(filepath.Join(root, trashName))
}
if _, err := os.Lstat(trashDir); err != nil {
usr, _ := user.Current()
trashDir += "-" + usr.Uid
if err := os.Mkdir(trashDir, executePerm); err != nil {
return "", fmt.Errorf("%s%s does not exist and creation of %s failed", root, trashName, trashDir)
}
}
if link, err := os.Readlink(trashDir); err == nil && link != "" {
return "", fmt.Errorf("trash dir %s is a symbolic link", trashDir)
}
return trashDir, nil
}
func getRoot(path string) (string, error) {
var roots []string
// populate a list of mountpoints on the system
_, err := mountinfo.GetMounts(func(i *mountinfo.Info) (skip bool, stop bool) {
roots = append(roots, i.Mountpoint)
return false, false
})
if err != nil {
log.Error(err)
}
var depth uint8 = 1 // 255 seems a reasonable recursion maximum
current := path
// recursively search upwards by using filepath.Clean and ..
for {
if depth == 0 {
return path, fmt.Errorf("reached max depth getting root of %s", path)
}
current = filepath.Clean(current)
if current == string(os.PathSeparator) || slices.Contains(roots, current) {
return current, nil
}
current += string(os.PathSeparator) + ".."
depth++
}
}
func getAllTrashes() []string {
trashes := []string{homeTrash}
usr, err := user.Current()
if err != nil {
log.Error(err)
}
_, err = mountinfo.GetMounts(func(mount *mountinfo.Info) (skip bool, stop bool) {
point := mount.Mountpoint
trashDir := filepath.Clean(filepath.Join(point, trashName))
userTrashDir := trashDir + "-" + usr.Uid
if _, err := os.Lstat(trashDir); err == nil {
trashes = append(trashes, trashDir)
}
if _, err := os.Lstat(userTrashDir); err == nil {
trashes = append(trashes, userTrashDir)
}
return false, false
})
if err != nil {
log.Errorf("error reading mounts: %s", err)
return nil
}
return trashes
}

View file

@ -73,7 +73,6 @@ type model struct {
keys keyMap
selected map[string]bool
selectsize int64
totalsize int64
readonly bool
once bool
filtering bool
@ -87,8 +86,8 @@ type model struct {
fltrfiles files.Files
}
func newModel(fls files.Files, selectall, readonly, once bool, workdir string, mode modes.Mode) model {
m := model{
func newModel(fls []files.File, selectall, readonly, once bool, workdir string, mode modes.Mode) (m model) {
m = model{
keys: defaultKeyMap(),
readonly: readonly,
once: once,
@ -96,7 +95,6 @@ func newModel(fls files.Files, selectall, readonly, once bool, workdir string, m
selected: map[string]bool{},
selectsize: 0,
files: fls,
totalsize: fls.TotalSize(),
}
m.termwidth, m.termheight = termSizes()
@ -118,7 +116,7 @@ func newModel(fls files.Files, selectall, readonly, once bool, workdir string, m
m.selectAll()
}
return m
return
}
type keyMap struct {
@ -328,13 +326,11 @@ func (m model) header() string {
styleKey(m.keys.clfl),
styleKey(m.keys.apfl),
}
dot = darkesttext.Render("•")
wideDot = darkesttext.Render(" • ")
keysFmt = strings.Join(keys, wideDot)
selectFmt = strings.Join(selectKeys, wideDot)
filterFmt = strings.Join(filterKeys, wideDot)
totalSize = humanize.Bytes(uint64(m.totalsize))
selectedSize = fmt.Sprintf("%s/%s", humanize.Bytes(uint64(m.selectsize)), totalSize)
dot = darkesttext.Render("•")
wideDot = darkesttext.Render(" • ")
keysFmt = strings.Join(keys, wideDot)
selectFmt = strings.Join(selectKeys, wideDot)
filterFmt = strings.Join(filterKeys, wideDot)
)
switch {
@ -342,23 +338,24 @@ func (m model) header() string {
right = fmt.Sprintf(" Filtering %s %s", dot, filterFmt)
case m.mode == modes.Interactive:
right = fmt.Sprintf(" %s %s %s", keysFmt, dot, selectFmt)
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, selectedSize)
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, humanize.Bytes(uint64(m.selectsize)))
case m.mode == modes.Listing:
var filtered string
if m.filter != "" || m.filtering {
filtered = " (filtered)"
}
right = fmt.Sprintf(" Showing%s %d files in trash (%s)", filtered, len(m.fltrfiles), totalSize)
right = fmt.Sprintf(" Showing%s %d files in trash", filtered, len(m.fltrfiles))
default:
var wd string
if m.workdir != "" {
wd = " in " + dirs.UnExpand(m.workdir, "")
}
right = fmt.Sprintf(" %s%s %s %s", m.mode.String(), wd, dot, selectFmt)
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, selectedSize)
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, humanize.Bytes(uint64(m.selectsize)))
}
spacerWidth = (m.termwidth - lipgloss.Width(right) - lipgloss.Width(left)) + 1
// offset of 2 again because of table border
spacerWidth = m.termwidth - lipgloss.Width(right) - lipgloss.Width(left) - poffset
if spacerWidth <= 0 {
spacerWidth = 1 // always at least one space
}
@ -665,14 +662,14 @@ func Show(fls files.Files, once bool, workdir string) error {
func newRow(file files.File, workdir string) table.Row {
var time, size string
name := file.Name()
time = humanize.Time(file.Date())
if file.IsDir() {
name += string(os.PathSeparator)
size = bar
} else {
size = humanize.Bytes(uint64(file.Filesize()))
}
size = humanize.Bytes(uint64(file.Filesize()))
return table.Row{
dirs.PercentDecode(name),
dirs.UnEscape(file.Name()),
dirs.UnExpand(filepath.Dir(file.Path()), workdir),
time,
size,

View file

@ -52,7 +52,7 @@ func NewPath(path string) (string, bool) {
Run(); err != nil {
return path, false
}
if _, e := os.Lstat(path); e != nil {
if _, e := os.Stat(path); e != nil {
return path, false
}
default:

View file

@ -1,5 +1,5 @@
binary := "gt"
version := "0.0.3"
version := "0.0.2"
build_dir := "bin"
dist_dir := "dist"
cmd := "."

75
main.go
View file

@ -4,7 +4,6 @@ package main
import (
"fmt"
"io/fs"
"math"
"os"
"path/filepath"
"slices"
@ -15,7 +14,6 @@ import (
"git.burning.moe/celediel/gt/internal/filter"
"git.burning.moe/celediel/gt/internal/interactive"
"git.burning.moe/celediel/gt/internal/interactive/modes"
"golang.org/x/term"
"github.com/adrg/xdg"
"github.com/charmbracelet/log"
@ -25,7 +23,7 @@ import (
const (
appname string = "gt"
appsubtitle string = "xdg trash cli"
appversion string = "v0.0.3"
appversion string = "v0.0.2"
appdesc string = `A small command line program to interface with the
Freedesktop.org / XDG trash specification.
@ -36,8 +34,8 @@ See gt(1) for more information.`
)
var (
fltr *filter.Filter
loglvl string
fltr *filter.Filter
onArg, beforeArg, afterArg string
globArg, patternArg string
unGlobArg, unPatternArg string
@ -47,13 +45,10 @@ var (
askconfirm, all bool
workdir, ogdir cli.Path
recursive bool
isTerminal bool
trashDir = filepath.Join(xdg.DataHome, "Trash")
beforeAll = func(_ *cli.Context) error {
if term.IsTerminal(int(os.Stdout.Fd())) && term.IsTerminal(int(os.Stdin.Fd())) {
isTerminal = true
}
// setup log
log.SetReportTimestamp(true)
log.SetTimeFormat(time.TimeOnly)
@ -67,19 +62,19 @@ var (
log.Errorf("unknown log level '%s' (possible values: debug, info, warn, error, fatal, default: warn)", loglvl)
}
if !isTerminal {
log.SetLevel(math.MaxInt32)
}
// ensure personal trash directories exist
homeTrash := filepath.Join(xdg.DataHome, "Trash")
if _, e := os.Lstat(filepath.Join(homeTrash, "info")); os.IsNotExist(e) {
if err := os.MkdirAll(filepath.Join(homeTrash, "info"), executePerm); err != nil {
// ensure trash directories exist
if _, e := os.Stat(trashDir); os.IsNotExist(e) {
if err := os.Mkdir(trashDir, executePerm); err != nil {
return err
}
}
if _, e := os.Lstat(filepath.Join(homeTrash, "files")); os.IsNotExist(e) {
if err := os.MkdirAll(filepath.Join(homeTrash, "files"), executePerm); err != nil {
if _, e := os.Stat(filepath.Join(trashDir, "info")); os.IsNotExist(e) {
if err := os.Mkdir(filepath.Join(trashDir, "info"), executePerm); err != nil {
return err
}
}
if _, e := os.Stat(filepath.Join(trashDir, "files")); os.IsNotExist(e) {
if err := os.Mkdir(filepath.Join(trashDir, "files"), executePerm); err != nil {
return err
}
}
@ -112,8 +107,10 @@ var (
mode modes.Mode
err error
)
infiles = files.FindInAllTrashes(ogdir, fltr)
infiles, err = files.FindTrash(trashDir, ogdir, fltr)
if err != nil {
return err
}
if len(infiles) <= 0 {
var msg string
if fltr.Blank() {
@ -124,11 +121,6 @@ var (
fmt.Fprintln(os.Stdout, msg)
return nil
}
if !isTerminal {
fmt.Fprint(os.Stdout, infiles.String())
return nil
}
selected, mode, err = interactive.Select(infiles, false, false, workdir, modes.Interactive)
if err != nil {
return err
@ -166,7 +158,7 @@ var (
}
filesToTrash = append(filesToTrash, file)
}
return files.ConfirmTrash(askconfirm, filesToTrash)
return files.ConfirmTrash(askconfirm, filesToTrash, trashDir)
}
beforeCommands = func(ctx *cli.Context) (err error) {
@ -195,7 +187,6 @@ var (
}
after = func(_ *cli.Context) error {
files.WriteDirectorySizes()
return nil
}
@ -220,7 +211,10 @@ var (
// if none of the args were files, then find files based on filter
if len(filesToTrash) == 0 {
fls := files.FindDisk(workdir, recursive, fltr)
fls, err := files.FindDisk(workdir, recursive, fltr)
if err != nil {
return err
}
if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to trash")
return nil
@ -237,7 +231,7 @@ var (
return nil
}
return files.ConfirmTrash(askconfirm, selected)
return files.ConfirmTrash(askconfirm, selected, trashDir)
},
}
@ -248,7 +242,9 @@ var (
Flags: slices.Concat(listFlags, trashedFlags, filterFlags),
Before: beforeCommands,
Action: func(_ *cli.Context) error {
fls := files.FindInAllTrashes(ogdir, fltr)
log.Debugf("searching in directory %s for files", trashDir)
fls, err := files.FindTrash(trashDir, ogdir, fltr)
var msg string
log.Debugf("filter '%s' is blank? %t in %s", fltr, fltr.Blank(), ogdir)
@ -261,11 +257,8 @@ var (
if len(fls) == 0 {
fmt.Fprintln(os.Stdout, msg)
return nil
}
if !isTerminal {
fmt.Fprint(os.Stdout, fls.String())
return nil
} else if err != nil {
return err
}
return interactive.Show(fls, noInterArg, workdir)
@ -280,10 +273,14 @@ var (
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
Before: beforeCommands,
Action: func(_ *cli.Context) error {
fls := files.FindInAllTrashes(ogdir, fltr)
log.Debugf("searching in directory %s for files", trashDir)
fls, err := files.FindTrash(trashDir, ogdir, fltr)
if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to restore")
return nil
} else if err != nil {
return err
}
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Restoring)
@ -307,10 +304,12 @@ var (
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
Before: beforeCommands,
Action: func(_ *cli.Context) error {
fls := files.FindInAllTrashes(ogdir, fltr)
fls, err := files.FindTrash(trashDir, ogdir, fltr)
if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to clean")
return nil
} else if err != nil {
return err
}
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Cleaning)