Files
alan c61490aeb0 Add rename_path, move_path and delete_path for home files
The chat Files panel needs to rename, move and delete entries. Three
websocket requests, dispatched beside list_directory, operate on one
entry inside the operator's home and reply with path_mutated {path} or
an error frame (does not exist, already exists, is not empty, outside
the home directory, cannot be moved into itself).

The entry's parent folder and the move destination are resolved through
symlinks and must stay inside home; the entry's own symlink is never
followed, so a link is renamed, moved or removed as a link. The home
directory itself, anything outside it and pseudo filesystems are
refused. A folder is deleted only when empty unless recursive is set;
os.RemoveAll unlinks symlinks rather than descending into them.
2026-11-09 01:32:31 +00:00

218 lines
6.8 KiB
Go

package server
import (
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// handleHomeFileMutation answers rename_path, move_path and delete_path. It
// returns true when the request was one of them; the reply (path_mutated or
// error, echoing the request ID) is written from its own goroutine so a slow
// recursive delete never stalls the connection's other traffic.
func handleHomeFileMutation(request protocol.Message, write func(protocol.Message) error) bool {
switch request.Type {
case protocol.RenamePath, protocol.MovePath, protocol.DeletePath:
default:
return false
}
go func() {
path, err := applyHomeFileMutation(request)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.PathMutated, ID: request.ID, PathMutated: &protocol.PathMutatedResult{Path: path}})
}()
return true
}
func applyHomeFileMutation(request protocol.Message) (string, error) {
home, err := OperatorHomeDirectory()
if err != nil {
return "", err
}
switch request.Type {
case protocol.RenamePath:
if request.RenamePath == nil {
return "", errors.New("rename_path body is missing")
}
return renameHomePath(home, request.RenamePath.Path, request.RenamePath.Name)
case protocol.MovePath:
if request.MovePath == nil {
return "", errors.New("move_path body is missing")
}
return moveHomePath(home, request.MovePath.Path, request.MovePath.Into)
case protocol.DeletePath:
if request.DeletePath == nil {
return "", errors.New("delete_path body is missing")
}
return deleteHomePath(home, request.DeletePath.Path, request.DeletePath.Recursive)
}
return "", errors.New("unsupported mutation " + request.Type)
}
// homeEntry is one existing entry inside home. Its parent folder is resolved
// through symlinks and checked against home; the entry's own name is not
// followed, so a symlink is operated on as a link and never as its target.
type homeEntry struct {
home string
realHome string
shown string // the path under home, as the operator sees it
real string // the resolved parent joined with the entry's name
info fs.FileInfo
}
func locateHomeEntry(home, requested string) (homeEntry, error) {
home = filepath.Clean(home)
realHome, err := filepath.EvalSymlinks(home)
if err != nil {
return homeEntry{}, errors.New("resolve home directory: " + err.Error())
}
target, err := expandHomePath(home, requested)
if err != nil {
return homeEntry{}, err
}
if target == home || target == realHome {
return homeEntry{}, errors.New("the home directory itself cannot be renamed, moved or deleted")
}
name := filepath.Base(target)
if name == "." || name == ".." || name == string(filepath.Separator) {
return homeEntry{}, errors.New("path " + requested + " does not name an entry")
}
relParent, realParent, err := resolveInsideHome(realHome, filepath.Dir(target))
if err != nil {
return homeEntry{}, err
}
if specialFilesystem(realParent) {
return homeEntry{}, errors.New(target + " is on a special filesystem and cannot be changed")
}
entry := homeEntry{
home: home,
realHome: realHome,
shown: filepath.Join(home, relParent, name),
real: filepath.Join(realParent, name),
}
entry.info, err = os.Lstat(entry.real)
if err != nil {
return homeEntry{}, describePathError(entry.shown, err)
}
return entry, nil
}
// renameHomePath gives the entry a new plain name in its own folder. An
// existing name is refused, except the entry's own (a case-only rename on a
// case-insensitive filesystem).
func renameHomePath(home, requested, newName string) (string, error) {
entry, err := locateHomeEntry(home, requested)
if err != nil {
return "", err
}
name, err := uploadFileName(newName)
if err != nil {
return "", err
}
dest := filepath.Join(filepath.Dir(entry.real), name)
shown := filepath.Join(filepath.Dir(entry.shown), name)
if existing, err := os.Lstat(dest); err == nil && !os.SameFile(existing, entry.info) {
return "", errors.New(shown + " already exists")
}
if err := os.Rename(entry.real, dest); err != nil {
return "", describePathError(entry.shown, err)
}
return shown, nil
}
// moveHomePath moves the entry, keeping its name, into another folder inside
// home. The destination folder is resolved through symlinks and must stay
// inside home; an existing name there is refused, as is moving a folder into
// itself or a descendant.
func moveHomePath(home, requested, into string) (string, error) {
entry, err := locateHomeEntry(home, requested)
if err != nil {
return "", err
}
intoTarget, err := expandHomePath(entry.home, into)
if err != nil {
return "", err
}
relInto, realInto, err := resolveInsideHome(entry.realHome, intoTarget)
if err != nil {
return "", err
}
shownInto := filepath.Join(entry.home, relInto)
info, err := os.Stat(realInto)
if err != nil {
return "", describePathError(shownInto, err)
}
if !info.IsDir() {
return "", errors.New(shownInto + " is not a folder")
}
if specialFilesystem(realInto) {
return "", errors.New(shownInto + " is on a special filesystem and cannot be changed")
}
if entry.info.IsDir() && (realInto == entry.real || strings.HasPrefix(realInto, entry.real+string(filepath.Separator))) {
return "", errors.New(entry.shown + " cannot be moved into itself")
}
dest := filepath.Join(realInto, entry.info.Name())
shown := filepath.Join(shownInto, entry.info.Name())
if _, err := os.Lstat(dest); err == nil {
return "", errors.New(shown + " already exists")
}
if err := os.Rename(entry.real, dest); err != nil {
return "", describePathError(entry.shown, err)
}
return shown, nil
}
// deleteHomePath removes a file or symlink (the link, never its target), an
// empty folder, or with recursive a folder and its contents. os.RemoveAll
// unlinks symlinks it meets rather than descending into them.
func deleteHomePath(home, requested string, recursive bool) (string, error) {
entry, err := locateHomeEntry(home, requested)
if err != nil {
return "", err
}
if !entry.info.IsDir() {
if err := os.Remove(entry.real); err != nil {
return "", describePathError(entry.shown, err)
}
return entry.shown, nil
}
if recursive {
if err := os.RemoveAll(entry.real); err != nil {
return "", describePathError(entry.shown, err)
}
return entry.shown, nil
}
empty, err := directoryEmpty(entry.real)
if err != nil {
return "", describePathError(entry.shown, err)
}
if !empty {
return "", errors.New(entry.shown + " " + protocol.FolderNotEmpty)
}
if err := os.Remove(entry.real); err != nil {
return "", describePathError(entry.shown, err)
}
return entry.shown, nil
}
func directoryEmpty(path string) (bool, error) {
dir, err := os.Open(path)
if err != nil {
return false, err
}
defer dir.Close()
_, err = dir.ReadDir(1)
if errors.Is(err, io.EOF) {
return true, nil
}
return false, err
}