Compare commits

..

10 commits
v0.0.2 ... main

Author SHA1 Message Date
Lilian Jónsdóttir 884c20ce61 version bump - v0.0.3
- add support for .Trash and .Trash-$(id -u) directories on non-home partitions
- add support for reading from and writing to $trashDir/directorysizes
2024-08-14 19:02:33 -07:00
Lilian Jónsdóttir cb47a01884 add support for reading/writing $trashDir/directorysizes
- show total trash size in header
2024-08-14 19:01:54 -07:00
Lilian Jónsdóttir 6c3abd8d98 listing prints plainly if in non-interative terminal 2024-08-14 15:48:06 -07:00
Lilian Jónsdóttir 34eef6a228 replace os.Stat with os.Lstat 2024-08-14 15:08:50 -07:00
Lilian Jónsdóttir 50f4dbda8a some clean up 2024-08-14 15:08:50 -07:00
Lilian Jónsdóttir d8ec25b36d use percent encoding on filenames 2024-08-05 23:09:52 -07:00
Lilian Jónsdóttir d6f066095b unexport and rearrange some functions 2024-08-05 23:06:37 -07:00
Lilian Jónsdóttir 4a10d7794f add support for $topdir Trash directories 2024-08-05 22:40:11 -07:00
Lilian Jónsdóttir cc973694e5 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
2024-08-05 22:02:56 -07:00
Lilian Jónsdóttir 44da4acbbd update readme 2024-07-31 10:12:06 -07:00
14 changed files with 664 additions and 269 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,6 +12,7 @@ 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,6 +58,8 @@ 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=

6
gt.1
View file

@ -5,11 +5,11 @@
.nh
.ad l
.\" Begin generated content:
.TH "gt" "1" "2024-07-31" "gt version v0.0.2" "User Commands"
.TH "gt" "1" "2024-08-15" "gt version v0.0.3" "User Commands"
.PP
.SH NAME
.PP
gt - manual page for gt version 0.\&0.\&2
gt - manual page for gt version 0.\&0.\&3
.PP
.SH DESCRIPTION
.PP
@ -27,7 +27,7 @@ g(o)t(rash) is a simple, command line program to interface with the XDG Trash.\&
.PP
.SS VERSION:
.PP
0.\&0.\&2
0.\&0.\&3
.PP
.SS AUTHOR:
.PP

View file

@ -1,8 +1,8 @@
gt(1) ["gt version v0.0.2" ["User Commands"]]
gt(1) ["gt version v0.0.3" ["User Commands"]]
# NAME
gt \- manual page for gt version 0.0.2
gt \- manual page for gt version 0.0.3
# 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.2
0.0.3
## AUTHOR:

View file

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

View file

@ -0,0 +1,154 @@
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,12 +26,7 @@ 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 {
if f.isdir {
return 0
}
return f.filesize
}
func (f DiskFile) Filesize() int64 { 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
@ -53,21 +48,29 @@ 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(info.Size())), info.ModTime())
name, basePath, humanize.Bytes(uint64(size)), info.ModTime())
return DiskFile{
name: name,
path: filepath.Join(basePath, name),
filesize: info.Size(),
path: actualPath,
filesize: size,
modified: info.ModTime(),
isdir: info.IsDir(),
mode: info.Mode(),
}, nil
}
func FindDisk(dir string, recursive bool, fltr *filter.Filter) (Files, error) {
func FindDisk(dir string, recursive bool, fltr *filter.Filter) Files {
var files Files
dir = filepath.Clean(dir)
if dir == "." || dir == "" {
@ -91,7 +94,7 @@ func FindDisk(dir string, recursive bool, fltr *filter.Filter) (Files, error) {
files = append(files, readDir(dir, fltr)...)
}
return files, nil
return files
}
// isInHiddenDir checks `path` and parent directories
@ -130,10 +133,17 @@ 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: info.Size(),
filesize: size,
modified: info.ModTime(),
isdir: info.IsDir(),
mode: info.Mode(),
@ -143,7 +153,7 @@ func walkDir(dir string, fltr *filter.Filter) Files {
})
if err != nil {
log.Errorf("error walking directory %s: %s", dir, err)
return Files{}
return nil
}
return files
}
@ -152,7 +162,7 @@ func readDir(dir string, fltr *filter.Filter) Files {
var files Files
fs, err := os.ReadDir(dir)
if err != nil {
return Files{}
return nil
}
for _, file := range fs {
name := file.Name()
@ -163,17 +173,28 @@ func readDir(dir string, fltr *filter.Filter) Files {
info, err := file.Info()
if err != nil {
return Files{}
return nil
}
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(path, name),
path: filepath.Join(actualPath, name),
modified: info.ModTime(),
filesize: info.Size(),
filesize: size,
isdir: info.IsDir(),
mode: info.Mode(),
})

View file

@ -3,11 +3,15 @@ package files
import (
"cmp"
"fmt"
"io/fs"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/charmbracelet/log"
)
type File interface {
@ -22,6 +26,33 @@ 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
}
}
size += file.Filesize()
}
return size
}
func SortByModified(a, b File) int {
if a.Date().Before(b.Date()) {
return 1
@ -41,15 +72,11 @@ func SortByModifiedReverse(a, b File) int {
}
func SortBySize(a, b File) int {
as := getSortingSize(a)
bs := getSortingSize(b)
return cmp.Compare(as, bs)
return cmp.Compare(a.Filesize(), b.Filesize())
}
func SortBySizeReverse(a, b File) int {
as := getSortingSize(a)
bs := getSortingSize(b)
return cmp.Compare(bs, as)
return cmp.Compare(b.Filesize(), a.Filesize())
}
func SortByName(a, b File) int {
@ -112,9 +139,36 @@ func doNameSort(a, b File) int {
return cmp.Compare(aname, bname)
}
func getSortingSize(f File) int64 {
if f.IsDir() {
return -1
func calculateDirSize(path string) int64 {
var size int64
info, err := os.Lstat(path)
if err != nil {
log.Error(err)
return 0
}
return f.Filesize()
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
}

View file

@ -5,7 +5,9 @@ import (
"io/fs"
"math/rand"
"os"
"os/user"
"path/filepath"
"slices"
"strings"
"time"
@ -13,16 +15,20 @@ 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"
@ -34,6 +40,8 @@ DeletionDate={date}
`
)
var homeTrash = filepath.Join(xdg.DataHome, "Trash")
type TrashInfo struct {
name, ogpath string
path, trashinfo string
@ -50,54 +58,139 @@ 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 {
if t.isdir {
return 0
}
return t.filesize
}
func (t TrashInfo) Filesize() int64 { return t.filesize }
func (t TrashInfo) String() string {
return t.name + t.path + t.ogpath + t.trashinfo
}
func FindTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
func FindInAllTrashes(ogdir string, fltr *filter.Filter) Files {
var files Files
outerr := filepath.WalkDir(trashdir, func(path string, dirEntry fs.DirEntry, err error) error {
for _, trash := range getAllTrashes() {
fls, err := findTrash(trash, ogdir, fltr)
if err != nil {
log.Debugf("what happened?? what is %s?", err)
return err
log.Errorf("error reading trash dir '%s': %s", trash, err)
continue
}
files = append(files, fls...)
}
// ignore self, directories, and non trashinfo files
if path == trashdir || dirEntry.IsDir() || filepath.Ext(path) != trashInfoExt {
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())
}
trashed := trashFiles(tfs)
var s string
if trashed > 1 {
s = "s"
}
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
c, err := ini.Load(path)
trashInfo, err := ini.Load(path)
if err != nil {
return err
log.Errorf("error reading %s: %s", path, err)
continue
}
section := trashInfo.Section(trashInfoSec)
if section == nil {
continue
}
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)
}
}
if section := c.Section(trashInfoSec); section != nil {
basepath := section.Key(trashInfoPath).String()
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
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 {
return err
log.Errorf("error parsing date '%s' in trashinfo file '%s': %s", s, path, err)
continue
}
if ogdir != "" && filepath.Dir(basepath) != ogdir {
return nil
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) {
@ -108,19 +201,66 @@ func FindTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
trashinfo: path,
trashed: date,
isdir: info.IsDir(),
filesize: info.Size(),
filesize: size,
})
}
}
return nil
})
if outerr != nil {
return Files{}, outerr
}
return files, nil
}
func Restore(files Files) (restored int, err error) {
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) {
for _, maybeFile := range files {
file, ok := maybeFile.(TrashInfo)
if !ok {
@ -128,7 +268,7 @@ func Restore(files Files) (restored int, err error) {
}
var cancel bool
outpath := dirs.UnEscape(file.ogpath)
outpath := dirs.PercentDecode(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)
@ -139,7 +279,7 @@ func Restore(files Files) (restored int, err error) {
}
basedir := filepath.Dir(outpath)
if _, e := os.Stat(basedir); e != nil {
if _, e := os.Lstat(basedir); e != nil {
if err = os.MkdirAll(basedir, executePerm); err != nil {
return restored, err
}
@ -158,21 +298,7 @@ func Restore(files Files) (restored int, err error) {
return restored, err
}
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) {
func remove(files Files) (removed int, err error) {
for _, maybeFile := range files {
file, ok := maybeFile.(TrashInfo)
if !ok {
@ -197,82 +323,7 @@ func Remove(files Files) (removed int, err error) {
return removed, err
}
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 {
func randomString(length int) string {
out := strings.Builder{}
for range length {
out.WriteByte(randomChar())
@ -285,17 +336,17 @@ func randomChar() byte {
return chars[rand.Intn(len(chars))]
}
func ensureUniqueName(filename, trashDir string) (string, string) {
func getTrashFilenames(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.Stat(info); os.IsNotExist(err) {
if _, err := os.Lstat(info); os.IsNotExist(err) {
// doesn't exist, so use it
path := filepath.Join(filedir, filename)
return info, path
return dirs.PercentEncode(info), dirs.PercentEncode(path)
}
// otherwise, try random suffixes until one works
@ -303,12 +354,106 @@ func ensureUniqueName(filename, trashDir string) (string, string) {
var tries int
for {
tries++
rando := randomFilename(randomStrLength)
newName := filepath.Join(infodir, filename+rando+trashInfoExt)
if _, err := os.Stat(newName); os.IsNotExist(err) {
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) {
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
return dirs.PercentEncode(newInfo), dirs.PercentEncode(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,6 +73,7 @@ type model struct {
keys keyMap
selected map[string]bool
selectsize int64
totalsize int64
readonly bool
once bool
filtering bool
@ -86,8 +87,8 @@ type model struct {
fltrfiles files.Files
}
func newModel(fls []files.File, selectall, readonly, once bool, workdir string, mode modes.Mode) (m model) {
m = model{
func newModel(fls files.Files, selectall, readonly, once bool, workdir string, mode modes.Mode) model {
m := model{
keys: defaultKeyMap(),
readonly: readonly,
once: once,
@ -95,6 +96,7 @@ func newModel(fls []files.File, selectall, readonly, once bool, workdir string,
selected: map[string]bool{},
selectsize: 0,
files: fls,
totalsize: fls.TotalSize(),
}
m.termwidth, m.termheight = termSizes()
@ -116,7 +118,7 @@ func newModel(fls []files.File, selectall, readonly, once bool, workdir string,
m.selectAll()
}
return
return m
}
type keyMap struct {
@ -331,6 +333,8 @@ func (m model) header() string {
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)
)
switch {
@ -338,24 +342,23 @@ 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, humanize.Bytes(uint64(m.selectsize)))
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, selectedSize)
case m.mode == modes.Listing:
var filtered string
if m.filter != "" || m.filtering {
filtered = " (filtered)"
}
right = fmt.Sprintf(" Showing%s %d files in trash", filtered, len(m.fltrfiles))
right = fmt.Sprintf(" Showing%s %d files in trash (%s)", filtered, len(m.fltrfiles), totalSize)
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, humanize.Bytes(uint64(m.selectsize)))
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.fltrfiles), dot, selectedSize)
}
// offset of 2 again because of table border
spacerWidth = m.termwidth - lipgloss.Width(right) - lipgloss.Width(left) - poffset
spacerWidth = (m.termwidth - lipgloss.Width(right) - lipgloss.Width(left)) + 1
if spacerWidth <= 0 {
spacerWidth = 1 // always at least one space
}
@ -662,14 +665,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() {
size = bar
} else {
size = humanize.Bytes(uint64(file.Filesize()))
name += string(os.PathSeparator)
}
size = humanize.Bytes(uint64(file.Filesize()))
return table.Row{
dirs.UnEscape(file.Name()),
dirs.PercentDecode(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.Stat(path); e != nil {
if _, e := os.Lstat(path); e != nil {
return path, false
}
default:

View file

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

75
main.go
View file

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