// Package vfs is the file-system abstraction every panel, transfer and preview // works through (design §2). One interface covers the local disk (localfs), // SFTP (sftpfs) and read-only archives (archivefs); the helpers in this package // (Walk, RemoveTree, DirSize, SameFile, Within) are written once against it. // // All methods take a context. LocalFS mostly ignores it; SFTPFS uses it to stop // waiting on a hung server. Paths are interpreted by FS.Paths(): posix for sftp // and archives, the native separator for local. package vfs import ( "context" "io" "io/fs" "time" ) // Kind identifies the implementation behind an FS. type Kind uint8 const ( KindLocal Kind = iota KindSFTP KindArchive ) func (k Kind) String() string { switch k { case KindLocal: return "local" case KindSFTP: return "sftp" case KindArchive: return "archive" } return "unknown" } // VolumeID: equal VolumeIDs mean "the same file namespace" (best effort). // local: "local"; sftp: "ssh:" + SHA256 host-key fingerprint (not the HostName // string — N-P0-5); archive: "archive:" + outer VolumeID + ":" + archive path. type VolumeID string // Caps describes what an FS can do. Callers check Caps instead of Kind. type Caps struct { ReadOnly bool // all mutating methods return ErrReadOnly AtomicReplace bool // Rename(..., Replace) is atomic (local: rename(2); sftp: posix-rename@openssh.com) NoReplace bool // Rename(..., NoReplace) is atomic (local: renameat2 RENAME_NOREPLACE; sftp: SSH_FXP_RENAME v3) Symlinks bool Chmod bool Chtimes bool Fsync bool // File.Sync works (local: yes; sftp: fsync@openssh.com) Trash bool // the FS implements Trasher Exec bool // files can be executed (local only) } // RenameMode selects what Rename does when the target exists. type RenameMode uint8 const ( // NoReplace fails with ErrExist if the target exists. NoReplace RenameMode = iota // Replace atomically replaces an existing target where Caps.AtomicReplace. Replace ) // CreateMode selects what Create does when the file exists. type CreateMode uint8 const ( // CreateExcl fails with ErrExist if the file exists (the transfer engine // only uses this, for its temp files). CreateExcl CreateMode = iota // CreateTrunc truncates an existing file. CreateTrunc ) // File is an open file. Sync returns ErrUnsupported when !Caps.Fsync. type File interface { io.Reader io.Writer io.ReaderAt io.Closer Stat() (FileInfo, error) Sync() error } // FS is one file namespace. type FS interface { Kind() Kind Volume() VolumeID Caps() Caps Label() string // for the UI: "local", "bob@prod", "foo.zip" Paths() PathOps // path rules: posix (sftp, archive) or native (local) // Stat follows symlinks; Lstat does not. For a symlink Lstat also fills // FileInfo.Target the same way ReadDir does. Stat(ctx context.Context, p string) (FileInfo, error) Lstat(ctx context.Context, p string) (FileInfo, error) // ReadDir has lstat semantics; for symlinks the implementation fills // Target (local: Stat; sftp: parallel Stat, at most 16 in flight). // The order is unspecified; the panel sorts. ReadDir(ctx context.Context, p string) ([]FileInfo, error) ReadLink(ctx context.Context, p string) (string, error) // RealPath returns the absolute path with all symlinks resolved. RealPath(ctx context.Context, p string) (string, error) Open(ctx context.Context, p string) (File, error) Create(ctx context.Context, p string, perm fs.FileMode, mode CreateMode) (File, error) Mkdir(ctx context.Context, p string, perm fs.FileMode) error // Remove deletes a file, a symlink or an EMPTY directory. It never // follows symlinks. Remove(ctx context.Context, p string) error Rename(ctx context.Context, from, to string, mode RenameMode) error Symlink(ctx context.Context, target, p string) error Chmod(ctx context.Context, p string, mode fs.FileMode) error Chtimes(ctx context.Context, p string, atime, mtime time.Time) error } // Optional capabilities, discovered by type assertion. // Trasher moves p to the FS's trash (local: freedesktop trash). type Trasher interface { Trash(ctx context.Context, p string) error } // BirthTimer reports the creation time of p, if the FS knows it. ReadDir does // not compute it (it is expensive); callers ask only when a "created" column or // sort is enabled. type BirthTimer interface { BirthTime(ctx context.Context, p string) (time.Time, bool) } // Execer runs a command next to the files (sftpfs over a conn with exec), for // server-side transfer optimisations. The implementation quotes arguments. type Execer interface { Exec(ctx context.Context, argv []string) (stdout []byte, err error) }