feat: implement configurable delete modes

This commit is contained in:
OpenClaw Agent
2026-09-21 13:38:18 +08:00
parent 633c833d1c
commit add2a22416
7 changed files with 102 additions and 5 deletions
+35
View File
@@ -2,12 +2,20 @@ package storage
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/Bios-Marcel/wastebasket"
)
type Trashable interface {
Trash(path string) error
MoveToTrash(path, trashRoot string) error
}
type Storage interface {
Stat(path string) (os.FileInfo, error)
Open(path string) (*os.File, error)
@@ -67,6 +75,33 @@ func (s Local) Remove(path string) error {
}
return os.RemoveAll(p)
}
func (s Local) Trash(path string) error {
p, e := s.real(path)
if e != nil {
return e
}
return wastebasket.Trash(p)
}
func (s Local) MoveToTrash(path, trashRoot string) error {
p, e := s.real(path)
if e != nil {
return e
}
if e = os.MkdirAll(trashRoot, 0755); e != nil {
return e
}
name := filepath.Base(p)
target := filepath.Join(trashRoot, name)
for i := 1; ; i++ {
if _, e = os.Lstat(target); os.IsNotExist(e) {
break
}
target = filepath.Join(trashRoot, fmt.Sprintf("%s.%d", name, i))
}
return os.Rename(p, target)
}
func (s Local) Rename(a, b string) error {
pa, e := s.real(a)
if e != nil {