Skip to content
This repository has been archived by the owner on Dec 29, 2024. It is now read-only.

Commit

Permalink
fix(trash): create trash dir path for individual block device
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Nov 27, 2023
1 parent 8aa4b92 commit 9e8f3b2
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 16 deletions.
14 changes: 9 additions & 5 deletions cron/trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import (
"github.com/wittano/filebot/setting"
"golang.org/x/exp/slices"
"os"
"path/filepath"
"time"
)

// TODO Improve Trash path for other block devices e.g. for NTFS devices
var TrashPath = filepath.Join(os.Getenv("HOME"), ".locale", "share", "Trash", "files")

func moveToTrashTask() {
c := setting.Flags.Config()

Expand All @@ -20,6 +16,8 @@ func moveToTrashTask() {
moveFileToTrash(dir)
}
}

setting.Logger().Debug("Complete 'moveToTrash' task", nil)
}

func moveFileToTrash(dir setting.Directory) {
Expand All @@ -35,7 +33,13 @@ func moveFileToTrash(dir setting.Directory) {
}

if isAfterDateOfRemovingFile(p, dir.After) {
go file.MoveToDestination(TrashPath, p)
trashPath, err := dir.TrashDir()
if err != nil {
setting.Logger().Error("Failed to find trash directory", err)
return
}

go file.MoveToDestination(trashPath, p)
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions cron/trash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func TestIsAfterDateOfRemovingFileButAfterTimeIsEqualZero(t *testing.T) {

func TestMoveFileToTrash(t *testing.T) {
f := test.CreateTempFile(t)
TrashPath = t.TempDir()
dir := setting.Directory{
Src: []string{f},
MoveToTrash: true,
Expand All @@ -54,7 +53,12 @@ func TestMoveFileToTrash(t *testing.T) {
t.Fatalf("File %s didn't move from original source", f)
}

newFilePath := filepath.Join(TrashPath, filepath.Base(f))
trashDir, err := dir.TrashDir()
if err != nil {
t.Fatal(err)
}

newFilePath := filepath.Join(trashDir, filepath.Base(f))

if _, err := os.Stat(newFilePath); err != nil {
t.Fatalf("File %s didn't move to destination directory. %s", f, err)
Expand Down
37 changes: 37 additions & 0 deletions internal/linux/mounted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package linux

import (
"bufio"
"os"
"strings"
)

type FileSystem struct {
Device string
MountedPoint string
Type string
}

func MountedList() (fss []FileSystem, err error) {
f, err := os.Open("/etc/mtab")
if err != nil {
return nil, err
}
defer f.Close()

scanner := bufio.NewScanner(f)

for scanner.Scan() {
buf := strings.Split(scanner.Text(), " ")

fs := FileSystem{
Device: buf[0],
MountedPoint: buf[1],
Type: buf[2],
}

fss = append(fss, fs)
}

return
}
20 changes: 20 additions & 0 deletions internal/linux/mounted_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package linux

import "testing"

func TestMountedList(t *testing.T) {
res, err := MountedList()
if err != nil {
t.Fatal(err)
}

if len(res) == 0 {
t.Fatal("Mounted list is empty")
}

elem := res[0]

if elem.MountedPoint == "" || elem.Device == "" || elem.Type == "" {
t.Fatalf("Sample element of mounted list is invalid. MountedPoint: %s, Device: %s, Type: %s", elem.MountedPoint, elem.Device, elem.Type)
}
}
2 changes: 1 addition & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (f fileStdWriter) Write(p []byte) (n int, err error) {
}

func writeToLogFile(path string, p []byte) {
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND, 0600)
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Fatalf("Failed to open log file")
}
Expand Down
62 changes: 62 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package logger

import (
"bytes"
"github.com/wittano/filebot/internal/test"
"io"
"os"
"testing"
"time"
)

func TestLogInfo(t *testing.T) {
path := test.CreateTempFile(t)
logger := NewLogger(path, ALL)

input := "Test"
logger.Info(input)

time.Sleep(10 * time.Millisecond)

f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}

res, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}

if len(res) == 0 {
t.Fatalf("Log file is empty")
}
}

func TestFileStdWriter(t *testing.T) {
path := test.CreateTempFile(t)
writer := fileStdWriter{path}

exp := []byte("test")

_, err := writer.Write(exp)
if err != nil {
t.Fatal(err)
}

time.Sleep(10 * time.Millisecond)

f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}

res, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(exp, res) {
t.Fatalf("Invalid data saved in log file. Expected %v, Actually: %v", exp, res)
}
}
52 changes: 52 additions & 0 deletions setting/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package setting
import (
"errors"
"github.com/go-playground/validator/v10"
"github.com/mitchellh/go-homedir"
"github.com/pelletier/go-toml/v2"
"github.com/wittano/filebot/internal/linux"
"github.com/wittano/filebot/path"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)

type Config struct {
Expand Down Expand Up @@ -85,6 +89,54 @@ func (d Directory) filterRealPaths(paths []string) (res []string) {
return
}

func (d Directory) TrashDir() (trashDir string, err error) {
if !d.MoveToTrash {
return "", nil
}

dir := filepath.Dir(d.Src[0])
_, err = os.Stat(dir)
if err != nil {
return
}

fs, err := linux.MountedList()
if err != nil {
return
}

trashName := ".Trash-" + strconv.Itoa(os.Getuid())

for _, device := range fs {
if strings.Contains(dir, device.MountedPoint) && device.MountedPoint != "/" {
trashDir = filepath.Join(device.MountedPoint, trashName, "files")
break
} else if device.MountedPoint == "/" && isUserRoot() {
trashDir = "/root/.Trash-0/files"
}
}

homeDir, err := homedir.Dir()
if err != nil {
return
}

if trashDir == "" {
trashDir = filepath.Join(homeDir, ".local", "share", trashName, "files")
}

err = os.MkdirAll(trashDir, 0700)
if err != nil {
return
}

return
}

func isUserRoot() bool {
return os.Getuid() == 0
}

var config *Config

func load(path string) (*Config, error) {
Expand Down
42 changes: 36 additions & 6 deletions setting/config_file_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package setting

import (
"fmt"
"github.com/wittano/filebot/internal/test"
"os"
"testing"
)

Expand All @@ -10,13 +13,16 @@ func TestLoadConfig(t *testing.T) {
t.Fatalf("Failed load conf causes %s", err)
}

dir := conf.Dirs[0]
if len(dir.Src) == 1 && dir.Src[0] != "/tmp/test" {
t.Fatalf("Invalid source paths. Expacted [ '/tmp/test' ], acually %v", dir.Src)
}
for _, dir := range conf.Dirs {
t.Run(dir.Dest, func(t *testing.T) {
if len(dir.Src) == 1 && dir.Src[0] == "" {
t.Fatalf("Invalid source paths. Expacted [ '/tmp/test' ], acually %v", dir.Src)
}

if dir.Dest != "/tmp/test2" {
t.Fatalf("Invalid destination path paths. Expacted '/tmp/test', acually %s", dir.Dest)
if dir.Dest == "" && !dir.MoveToTrash {
t.Fatalf("Invalid destination path paths. Expacted '/tmp/test', acually %s", dir.Dest)
}
})
}
}

Expand All @@ -26,3 +32,27 @@ func TestFailedLoadingConfig(t *testing.T) {
t.Fatal("Loaded setting file from invalid path")
}
}

func TestGetTrashDir(t *testing.T) {
destDir := t.TempDir()

d := Directory{
Src: []string{test.CreateTempFile(t)},
Dest: destDir,
MoveToTrash: true,
}

res, err := d.TrashDir()
if err != nil {
t.Fatal(err)
}

if res == "" {
t.Fatal("MoveToTrash field is false")
}

exp := fmt.Sprintf("/tmp/.Trash-%d/files", os.Getuid())
if exp != res {
t.Fatalf("Trash dir is diffrent. Expected: %s, Actually: %s", exp, res)
}
}
8 changes: 6 additions & 2 deletions watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package watcher
import (
"errors"
"github.com/fsnotify/fsnotify"
"github.com/wittano/filebot/cron"
"github.com/wittano/filebot/file"
"github.com/wittano/filebot/setting"
"os"
Expand Down Expand Up @@ -76,7 +75,12 @@ func (w *MyWatcher) AddFilesToObservable(config setting.Config) {
if paths != nil {
destPath := dir.Dest
if dir.Dest == "" {
destPath = cron.TrashPath
destPath, err = dir.TrashDir()
}

if err != nil {
setting.Logger().Error("Failed to find trash directory", err)
break
}

go w.fillFileObservedMap(paths, destPath)
Expand Down

0 comments on commit 9e8f3b2

Please sign in to comment.