2023-10-05 01:28:41 +08:00
|
|
|
//go:build unix
|
|
|
|
|
|
|
|
package nfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
billy "github.com/go-git/go-billy/v5"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2024-06-25 15:09:53 +08:00
|
|
|
"github.com/rclone/rclone/fs/log"
|
2023-10-05 01:28:41 +08:00
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FS is our wrapper around the VFS to properly support billy.Filesystem interface
|
|
|
|
type FS struct {
|
|
|
|
vfs *vfs.VFS
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDir implements read dir
|
|
|
|
func (f *FS) ReadDir(path string) (dir []os.FileInfo, err error) {
|
2024-06-25 15:09:53 +08:00
|
|
|
defer log.Trace(path, "")("items=%d, err=%v", &dir, &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
return f.vfs.ReadDir(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create implements creating new files
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Create(filename string) (node billy.File, err error) {
|
|
|
|
defer log.Trace(filename, "")("%v, err=%v", &node, &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
return f.vfs.Create(filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens a file
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Open(filename string) (node billy.File, err error) {
|
|
|
|
defer log.Trace(filename, "")("%v, err=%v", &node, &err)
|
|
|
|
return f.vfs.Open(filename)
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenFile opens a file
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) OpenFile(filename string, flag int, perm os.FileMode) (node billy.File, err error) {
|
|
|
|
defer log.Trace(filename, "flag=0x%X, perm=%v", flag, perm)("%v, err=%v", &node, &err)
|
|
|
|
return f.vfs.OpenFile(filename, flag, perm)
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat gets the file stat
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Stat(filename string) (fi os.FileInfo, err error) {
|
|
|
|
defer log.Trace(filename, "")("fi=%v, err=%v", &fi, &err)
|
|
|
|
return f.vfs.Stat(filename)
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename renames a file
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Rename(oldpath, newpath string) (err error) {
|
|
|
|
defer log.Trace(oldpath, "newpath=%q", newpath)("err=%v", &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
return f.vfs.Rename(oldpath, newpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes a file
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Remove(filename string) (err error) {
|
|
|
|
defer log.Trace(filename, "")("err=%v", &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
return f.vfs.Remove(filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join joins path elements
|
|
|
|
func (f *FS) Join(elem ...string) string {
|
|
|
|
return path.Join(elem...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TempFile is not implemented
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) TempFile(dir, prefix string) (node billy.File, err error) {
|
|
|
|
defer log.Trace(dir, "prefix=%q", prefix)("node=%v, err=%v", &node, &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
// MkdirAll creates a directory and all the ones above it
|
|
|
|
// it does not redirect to VFS.MkDirAll because that one doesn't
|
|
|
|
// honor the permissions
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) MkdirAll(filename string, perm os.FileMode) (err error) {
|
|
|
|
defer log.Trace(filename, "perm=%v", perm)("err=%v", &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
parts := strings.Split(filename, "/")
|
|
|
|
for i := range parts {
|
|
|
|
current := strings.Join(parts[:i+1], "/")
|
|
|
|
_, err := f.Stat(current)
|
|
|
|
if err == vfs.ENOENT {
|
|
|
|
err = f.vfs.Mkdir(current, perm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lstat gets the stats for symlink
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Lstat(filename string) (fi os.FileInfo, err error) {
|
|
|
|
defer log.Trace(filename, "")("fi=%v, err=%v", &fi, &err)
|
|
|
|
return f.vfs.Stat(filename)
|
2023-10-05 01:28:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink is not supported over NFS
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Symlink(target, link string) (err error) {
|
|
|
|
defer log.Trace(target, "link=%q", link)("err=%v", &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readlink is not supported
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Readlink(link string) (result string, err error) {
|
|
|
|
defer log.Trace(link, "")("result=%q, err=%v", &result, &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
return "", os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod changes the file modes
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Chmod(name string, mode os.FileMode) (err error) {
|
|
|
|
defer log.Trace(name, "mode=%v", mode)("err=%v", &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
file, err := f.vfs.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
fs.Logf(f, "Error while closing file: %e", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return file.Chmod(mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lchown changes the owner of symlink
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Lchown(name string, uid, gid int) (err error) {
|
|
|
|
defer log.Trace(name, "uid=%d, gid=%d", uid, gid)("err=%v", &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
return f.Chown(name, uid, gid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chown changes owner of the file
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Chown(name string, uid, gid int) (err error) {
|
|
|
|
defer log.Trace(name, "uid=%d, gid=%d", uid, gid)("err=%v", &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
file, err := f.vfs.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
fs.Logf(f, "Error while closing file: %e", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return file.Chown(uid, gid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chtimes changes the acces time and modified time
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Chtimes(name string, atime time.Time, mtime time.Time) (err error) {
|
|
|
|
defer log.Trace(name, "atime=%v, mtime=%v", atime, mtime)("err=%v", &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
return f.vfs.Chtimes(name, atime, mtime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chroot is not supported in VFS
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Chroot(path string) (FS billy.Filesystem, err error) {
|
|
|
|
defer log.Trace(path, "")("FS=%v, err=%v", &FS, &err)
|
2023-10-05 01:28:41 +08:00
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
// Root returns the root of a VFS
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Root() (root string) {
|
|
|
|
defer log.Trace(nil, "")("root=%q", &root)
|
2023-10-05 01:28:41 +08:00
|
|
|
return f.vfs.Fs().Root()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Capabilities exports the filesystem capabilities
|
2024-06-25 15:09:53 +08:00
|
|
|
func (f *FS) Capabilities() (caps billy.Capability) {
|
|
|
|
defer log.Trace(nil, "")("caps=%v", &caps)
|
2023-10-05 01:28:41 +08:00
|
|
|
if f.vfs.Opt.CacheMode == vfscommon.CacheModeOff {
|
|
|
|
return billy.ReadCapability | billy.SeekCapability
|
|
|
|
}
|
|
|
|
return billy.WriteCapability | billy.ReadCapability |
|
|
|
|
billy.ReadAndWriteCapability | billy.SeekCapability | billy.TruncateCapability
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface check
|
2024-06-25 15:09:53 +08:00
|
|
|
var (
|
|
|
|
_ billy.Filesystem = (*FS)(nil)
|
|
|
|
_ billy.Change = (*FS)(nil)
|
|
|
|
)
|