refactor: flatten repository structure to root and archive research notes
This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Multi struct {
|
||||
Roots []Local
|
||||
Names []string
|
||||
}
|
||||
|
||||
func NewMulti(paths []string) Multi {
|
||||
m := Multi{}
|
||||
for _, p := range paths {
|
||||
if strings.TrimSpace(p) == "" {
|
||||
continue
|
||||
}
|
||||
abs, _ := filepath.Abs(p)
|
||||
name := filepath.Base(filepath.Clean(abs))
|
||||
if name == "." || name == string(filepath.Separator) || name == "" {
|
||||
name = "root"
|
||||
}
|
||||
base := name
|
||||
for i := 2; containsString(m.Names, name); i++ {
|
||||
name = base + "-" + strconv.Itoa(i)
|
||||
}
|
||||
m.Roots = append(m.Roots, Local{Root: abs})
|
||||
m.Names = append(m.Names, name)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func containsString(items []string, want string) bool {
|
||||
for _, item := range items {
|
||||
if item == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (m Multi) isVirtualRoot(p string) bool { return strings.Trim(p, "/") == "" }
|
||||
|
||||
func (m Multi) FindRoot(p string) (string, string, bool) {
|
||||
p = strings.Trim(strings.ReplaceAll(p, "\\", "/"), "/")
|
||||
if p == "" {
|
||||
return "", "", false
|
||||
}
|
||||
parts := strings.SplitN(p, "/", 2)
|
||||
for _, n := range m.Names {
|
||||
if parts[0] != n {
|
||||
continue
|
||||
}
|
||||
rel := ""
|
||||
if len(parts) == 2 {
|
||||
rel = parts[1]
|
||||
}
|
||||
return n, rel, true
|
||||
}
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
func (m Multi) find(p string) (Local, string, bool) {
|
||||
p = strings.Trim(strings.ReplaceAll(p, "\\", "/"), "/")
|
||||
if p == "" {
|
||||
return Local{}, "", false
|
||||
}
|
||||
parts := strings.SplitN(p, "/", 2)
|
||||
for i, n := range m.Names {
|
||||
if parts[0] != n {
|
||||
continue
|
||||
}
|
||||
rel := ""
|
||||
if len(parts) == 2 {
|
||||
rel = parts[1]
|
||||
}
|
||||
return m.Roots[i], rel, true
|
||||
}
|
||||
return Local{}, "", false
|
||||
}
|
||||
|
||||
func (m Multi) Stat(p string) (os.FileInfo, error) {
|
||||
if m.isVirtualRoot(p) {
|
||||
return virtualDirInfo{name: "."}, nil
|
||||
}
|
||||
r, rel, ok := m.find(p)
|
||||
if !ok {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return r.Stat(rel)
|
||||
}
|
||||
func (m Multi) Open(p string) (*os.File, error) {
|
||||
r, rel, ok := m.find(p)
|
||||
if !ok {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return r.Open(rel)
|
||||
}
|
||||
func (m Multi) Create(p string) (*os.File, error) {
|
||||
r, rel, ok := m.find(p)
|
||||
if !ok {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return r.Create(rel)
|
||||
}
|
||||
|
||||
func (m Multi) CreateExclusive(p string) (*os.File, error) {
|
||||
r, rel, ok := m.find(p)
|
||||
if !ok {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return r.CreateExclusive(rel)
|
||||
}
|
||||
func (m Multi) Remove(p string) error {
|
||||
r, rel, ok := m.find(p)
|
||||
if !ok || rel == "" {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
return r.Remove(rel)
|
||||
}
|
||||
|
||||
func (m Multi) Trash(p string) error {
|
||||
r, rel, ok := m.find(p)
|
||||
if !ok || rel == "" {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
return r.Trash(rel)
|
||||
}
|
||||
|
||||
func (m Multi) MoveToTrash(p, trashRoot string) error {
|
||||
r, rel, ok := m.find(p)
|
||||
if !ok || rel == "" {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
return r.MoveToTrash(rel, filepath.Join(trashRoot, m.RootName(r)))
|
||||
}
|
||||
|
||||
func (m Multi) RootName(r Local) string {
|
||||
for i := range m.Roots {
|
||||
if m.Roots[i].Root == r.Root {
|
||||
return m.Names[i]
|
||||
}
|
||||
}
|
||||
return "root"
|
||||
}
|
||||
func (m Multi) Copy(a, b string) error {
|
||||
ra, aa, oka := m.find(a)
|
||||
rb, bb, okb := m.find(b)
|
||||
if !oka || !okb || aa == "" || bb == "" || ra.Root != rb.Root {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
return ra.Copy(aa, bb)
|
||||
}
|
||||
func (m Multi) Rename(a, b string) error {
|
||||
ra, aa, oka := m.find(a)
|
||||
rb, bb, okb := m.find(b)
|
||||
if !oka || !okb || aa == "" || bb == "" || ra.Root != rb.Root {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
return ra.Rename(aa, bb)
|
||||
}
|
||||
func (m Multi) MkdirAll(p string) error {
|
||||
r, rel, ok := m.find(p)
|
||||
if !ok {
|
||||
return os.ErrNotExist
|
||||
}
|
||||
return r.MkdirAll(rel)
|
||||
}
|
||||
func (m Multi) List(p string) ([]os.FileInfo, error) {
|
||||
p = strings.Trim(strings.ReplaceAll(p, "\\", "/"), "/")
|
||||
if p == "" {
|
||||
out := make([]os.FileInfo, 0, len(m.Names))
|
||||
for _, n := range m.Names {
|
||||
out = append(out, virtualDirInfo{name: n})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
r, rel, ok := m.find(p)
|
||||
if !ok {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return r.List(rel)
|
||||
}
|
||||
|
||||
type virtualDirInfo struct{ name string }
|
||||
|
||||
func (v virtualDirInfo) Name() string { return v.name }
|
||||
func (v virtualDirInfo) Size() int64 { return 0 }
|
||||
func (v virtualDirInfo) Mode() os.FileMode { return os.ModeDir | 0555 }
|
||||
func (v virtualDirInfo) ModTime() (t time.Time) { return time.Time{} }
|
||||
func (v virtualDirInfo) IsDir() bool { return true }
|
||||
func (v virtualDirInfo) Sys() any { return nil }
|
||||
@@ -0,0 +1,89 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMultiUsesUniqueVirtualRootNames(t *testing.T) {
|
||||
a := t.TempDir()
|
||||
b := t.TempDir()
|
||||
m := NewMulti([]string{a, b})
|
||||
if len(m.Names) != 2 || m.Names[0] == m.Names[1] {
|
||||
t.Fatalf("names=%v", m.Names)
|
||||
}
|
||||
if _, _, ok := m.FindRoot(m.Names[0]); !ok {
|
||||
t.Fatal("first root not addressable")
|
||||
}
|
||||
if _, _, ok := m.FindRoot(m.Names[1]); !ok {
|
||||
t.Fatal("second root not addressable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiRenameCannotCrossRoots(t *testing.T) {
|
||||
a := t.TempDir()
|
||||
b := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(a, "src.txt"), []byte("src"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := NewMulti([]string{a, b})
|
||||
err := m.Rename(m.Names[0]+"/src.txt", m.Names[1]+"/dst.txt")
|
||||
if err == nil {
|
||||
t.Fatal("expected cross-root rename to fail")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(a, "src.txt")); err != nil {
|
||||
t.Fatalf("source disappeared: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(b, "dst.txt")); !os.IsNotExist(err) {
|
||||
t.Fatalf("destination unexpectedly created: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiRemoveCannotRemoveVirtualRoot(t *testing.T) {
|
||||
m := NewMulti([]string{t.TempDir()})
|
||||
if err := m.Remove(""); err == nil {
|
||||
t.Fatal("expected virtual root removal to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalCreateExclusiveRejectsExistingFile(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
st := Local{Root: root}
|
||||
if err := os.WriteFile(filepath.Join(root, "x.txt"), []byte("old"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f, err := st.CreateExclusive("x.txt")
|
||||
if !os.IsExist(err) || f != nil {
|
||||
t.Fatalf("expected EEXIST, file=%v err=%v", f, err)
|
||||
}
|
||||
got, err := os.ReadFile(filepath.Join(root, "x.txt"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(got) != "old" {
|
||||
t.Fatalf("existing file changed: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalCreateExclusiveCreatesMissingFile(t *testing.T) {
|
||||
st := Local{Root: t.TempDir()}
|
||||
f, err := st.CreateExclusive("nested/x.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.WriteString("new"); err != nil {
|
||||
f.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := os.ReadFile(filepath.Join(st.Root, "nested", "x.txt"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(got) != "new" {
|
||||
t.Fatalf("unexpected content: %q", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,332 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"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)
|
||||
Create(path string) (*os.File, error)
|
||||
Remove(path string) error
|
||||
Rename(oldPath, newPath string) error
|
||||
Copy(oldPath, newPath string) error
|
||||
MkdirAll(path string) error
|
||||
List(path string) ([]os.FileInfo, error)
|
||||
}
|
||||
|
||||
type ExclusiveCreator interface {
|
||||
CreateExclusive(path string) (*os.File, error)
|
||||
}
|
||||
|
||||
type Local struct{ Root string }
|
||||
|
||||
func (s Local) real(path string) (string, error) {
|
||||
root, err := filepath.Abs(s.Root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
root, err = filepath.EvalSymlinks(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
p := strings.TrimPrefix(filepath.Clean("/"+path), string(filepath.Separator))
|
||||
full := filepath.Join(root, p)
|
||||
rel, err := filepath.Rel(root, full)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
return "", errors.New("path escapes storage root")
|
||||
}
|
||||
check := full
|
||||
for {
|
||||
if _, err = os.Lstat(check); !os.IsNotExist(err) {
|
||||
break
|
||||
}
|
||||
next := filepath.Dir(check)
|
||||
if next == check {
|
||||
break
|
||||
}
|
||||
check = next
|
||||
}
|
||||
resolved, err := filepath.EvalSymlinks(check)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resolvedRel, err := filepath.Rel(root, resolved)
|
||||
if err != nil || resolvedRel == ".." || strings.HasPrefix(resolvedRel, ".."+string(filepath.Separator)) {
|
||||
return "", errors.New("path escapes storage root")
|
||||
}
|
||||
return full, nil
|
||||
}
|
||||
func (s Local) Stat(path string) (os.FileInfo, error) {
|
||||
p, e := s.real(path)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
return os.Stat(p)
|
||||
}
|
||||
func (s Local) Open(path string) (*os.File, error) {
|
||||
p, e := s.real(path)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
return os.Open(p)
|
||||
}
|
||||
func (s Local) Create(path string) (*os.File, error) {
|
||||
p, e := s.real(path)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
if e = os.MkdirAll(filepath.Dir(p), 0755); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
return os.Create(p)
|
||||
}
|
||||
|
||||
func (s Local) CreateExclusive(path string) (*os.File, error) {
|
||||
p, e := s.real(path)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
if e = os.MkdirAll(filepath.Dir(p), 0755); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
return os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
|
||||
}
|
||||
func (s Local) Remove(path string) error {
|
||||
p, e := s.real(path)
|
||||
if e == nil {
|
||||
return os.RemoveAll(p)
|
||||
}
|
||||
// A final symlink that points outside the root is still safe to delete:
|
||||
// remove the link itself, but never follow it to its target. Validate its
|
||||
// parent through real() so nested symlink escapes remain rejected.
|
||||
clean := strings.TrimPrefix(filepath.Clean("/"+path), string(filepath.Separator))
|
||||
if clean == "" {
|
||||
return e
|
||||
}
|
||||
parent := filepath.ToSlash(filepath.Dir(clean))
|
||||
if parent == "." {
|
||||
parent = ""
|
||||
}
|
||||
if _, parentErr := s.real(parent); parentErr != nil {
|
||||
return e
|
||||
}
|
||||
root, rootErr := filepath.Abs(s.Root)
|
||||
if rootErr != nil {
|
||||
return e
|
||||
}
|
||||
root, rootErr = filepath.EvalSymlinks(root)
|
||||
if rootErr != nil {
|
||||
return e
|
||||
}
|
||||
candidate := filepath.Join(root, filepath.FromSlash(clean))
|
||||
if info, lstatErr := os.Lstat(candidate); lstatErr == nil && info.Mode()&os.ModeSymlink != 0 {
|
||||
return os.Remove(candidate)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
root, e := filepath.Abs(s.Root)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
rel, e := filepath.Rel(root, p)
|
||||
if e != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
return errors.New("path escapes storage root")
|
||||
}
|
||||
if e = os.MkdirAll(trashRoot, 0755); e != nil {
|
||||
return e
|
||||
}
|
||||
// Preserve the original relative path under the CHFS trash root so
|
||||
// identically named files from different directories cannot collide.
|
||||
target := filepath.Join(trashRoot, rel)
|
||||
if e = os.MkdirAll(filepath.Dir(target), 0755); e != nil {
|
||||
return e
|
||||
}
|
||||
for i := 1; ; i++ {
|
||||
if _, e = os.Lstat(target); os.IsNotExist(e) {
|
||||
break
|
||||
}
|
||||
target = filepath.Join(filepath.Dir(target), fmt.Sprintf("%s.%d", filepath.Base(p), i))
|
||||
}
|
||||
if e = movePath(p, target); e != nil {
|
||||
return e
|
||||
}
|
||||
// Cleanup is maintenance work; a successful move must not be reported as
|
||||
// a failed delete merely because an unrelated stale trash entry is busy.
|
||||
_ = cleanupTrash(trashRoot, 30*24*time.Hour)
|
||||
return nil
|
||||
}
|
||||
|
||||
func cleanupTrash(root string, maxAge time.Duration) error {
|
||||
now := time.Now()
|
||||
return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if path == root || d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
info, err := d.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if now.Sub(info.ModTime()) > maxAge {
|
||||
return os.Remove(path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func movePath(src, dst string) error {
|
||||
if err := os.Rename(src, dst); err == nil {
|
||||
return nil
|
||||
} else if !errors.Is(err, syscall.EXDEV) {
|
||||
return err
|
||||
}
|
||||
if _, err := os.Lstat(dst); err == nil {
|
||||
return fmt.Errorf("destination already exists: %s", dst)
|
||||
} else if !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
if err := copyPath(src, dst); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.RemoveAll(src)
|
||||
}
|
||||
|
||||
func copyPath(src, dst string) error {
|
||||
info, err := os.Lstat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
target, err := os.Readlink(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Symlink(target, dst)
|
||||
}
|
||||
if info.IsDir() {
|
||||
if err := os.MkdirAll(dst, info.Mode().Perm()); err != nil {
|
||||
return err
|
||||
}
|
||||
entries, err := os.ReadDir(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if err := copyPath(filepath.Join(src, entry.Name()), filepath.Join(dst, entry.Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return os.Chtimes(dst, info.ModTime(), info.ModTime())
|
||||
}
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_EXCL, info.Mode().Perm())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, copyErr := io.Copy(out, in)
|
||||
closeErr := out.Close()
|
||||
if copyErr != nil {
|
||||
_ = os.Remove(dst)
|
||||
return copyErr
|
||||
}
|
||||
if closeErr != nil {
|
||||
_ = os.Remove(dst)
|
||||
return closeErr
|
||||
}
|
||||
return os.Chtimes(dst, info.ModTime(), info.ModTime())
|
||||
}
|
||||
|
||||
func (s Local) Copy(a, b string) error {
|
||||
pa, e := s.real(a)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
pb, e := s.real(b)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if _, e := os.Lstat(pb); e == nil {
|
||||
return os.ErrExist
|
||||
} else if !os.IsNotExist(e) {
|
||||
return e
|
||||
}
|
||||
return copyPath(pa, pb)
|
||||
}
|
||||
|
||||
func (s Local) Rename(a, b string) error {
|
||||
pa, e := s.real(a)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
pb, e := s.real(b)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
return movePath(pa, pb)
|
||||
}
|
||||
func (s Local) MkdirAll(path string) error {
|
||||
p, e := s.real(path)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
return os.MkdirAll(p, 0755)
|
||||
}
|
||||
func (s Local) List(path string) ([]os.FileInfo, error) {
|
||||
p, e := s.real(path)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
f, e := os.Open(p)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
defer f.Close()
|
||||
items, err := f.Readdir(-1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Slice(items, func(i, j int) bool { return items[i].Name() < items[j].Name() })
|
||||
return items, nil
|
||||
}
|
||||
|
||||
var _ fs.FS
|
||||
@@ -0,0 +1,101 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLocalRemoveSymlinkDoesNotDeleteTarget(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
outside := t.TempDir()
|
||||
target := filepath.Join(outside, "secret.txt")
|
||||
if err := os.WriteFile(target, []byte("keep"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
link := filepath.Join(root, "link")
|
||||
if err := os.Symlink(target, link); err != nil {
|
||||
t.Skipf("symlink unavailable: %v", err)
|
||||
}
|
||||
store := Local{Root: root}
|
||||
if err := store.Remove("link"); err != nil {
|
||||
t.Fatalf("remove symlink: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(target); err != nil {
|
||||
t.Fatalf("symlink removal must not delete target: %v", err)
|
||||
}
|
||||
if _, err := os.Lstat(link); !os.IsNotExist(err) {
|
||||
t.Fatalf("symlink should be removed, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalRejectsSymlinkEscapeAndAllowsNestedCreate(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
outside := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(outside, "secret.txt"), []byte("secret"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(outside, filepath.Join(root, "escape")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := Local{Root: root}
|
||||
if _, err := s.Open("escape/secret.txt"); err == nil {
|
||||
t.Fatal("expected symlink escape to be rejected")
|
||||
}
|
||||
f, err := s.Create("a/b/c.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("nested create failed: %v", err)
|
||||
}
|
||||
_ = f.Close()
|
||||
}
|
||||
|
||||
func TestMoveToTrashPreservesRelativePathAndCleansOldEntries(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
trash := t.TempDir()
|
||||
s := Local{Root: root}
|
||||
if err := os.MkdirAll(filepath.Join(root, "one", "two"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "one", "two", "same.txt"), []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(trash, "old"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := filepath.Join(trash, "old", "stale.txt")
|
||||
if err := os.WriteFile(old, []byte("old"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldTime := time.Now().Add(-31 * 24 * time.Hour)
|
||||
if err := os.Chtimes(old, oldTime, oldTime); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.MoveToTrash("one/two/same.txt", trash); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(trash, "one", "two", "same.txt")); err != nil {
|
||||
t.Fatalf("trash path not preserved: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(old); !os.IsNotExist(err) {
|
||||
t.Fatalf("stale trash entry was not cleaned: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyPathPreservesSymlink(t *testing.T) {
|
||||
src := filepath.Join(t.TempDir(), "link")
|
||||
dst := filepath.Join(t.TempDir(), "link")
|
||||
if err := os.Symlink("target", src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := copyPath(src, dst); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := os.Readlink(dst)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "target" {
|
||||
t.Fatalf("got symlink target %q", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user