2022-04-24 17:27:28 +08:00
|
|
|
//go:build cmount && ((linux && cgo) || (darwin && cgo) || (freebsd && cgo) || windows)
|
2017-05-03 05:36:11 +08:00
|
|
|
|
|
|
|
package cmount
|
|
|
|
|
|
|
|
import (
|
2017-10-30 05:14:05 +08:00
|
|
|
"io"
|
2017-05-03 05:36:11 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
2023-01-12 00:23:40 +08:00
|
|
|
"strings"
|
2017-05-03 05:36:11 +08:00
|
|
|
"sync"
|
2021-01-29 02:17:47 +08:00
|
|
|
"sync/atomic"
|
2017-05-03 05:36:11 +08:00
|
|
|
"time"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd/mountlib"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2021-11-04 18:12:57 +08:00
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/log"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
2022-04-24 17:27:28 +08:00
|
|
|
"github.com/winfsp/cgofuse/fuse"
|
2017-05-03 05:36:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const fhUnset = ^uint64(0)
|
|
|
|
|
|
|
|
// FS represents the top level filing system
|
|
|
|
type FS struct {
|
2021-01-29 02:17:47 +08:00
|
|
|
VFS *vfs.VFS
|
|
|
|
f fs.Fs
|
2024-03-04 19:37:18 +08:00
|
|
|
opt *mountlib.Options
|
2021-01-29 02:17:47 +08:00
|
|
|
ready chan (struct{})
|
|
|
|
mu sync.Mutex // to protect the below
|
|
|
|
handles []vfs.Handle
|
2023-08-18 22:44:23 +08:00
|
|
|
destroyed atomic.Int32
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFS makes a new FS
|
2024-03-04 19:37:18 +08:00
|
|
|
func NewFS(VFS *vfs.VFS, opt *mountlib.Options) *FS {
|
2017-05-03 05:36:11 +08:00
|
|
|
fsys := &FS{
|
2020-07-23 00:58:49 +08:00
|
|
|
VFS: VFS,
|
|
|
|
f: VFS.Fs(),
|
2024-03-04 19:37:18 +08:00
|
|
|
opt: opt,
|
2017-11-03 17:32:18 +08:00
|
|
|
ready: make(chan (struct{})),
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
return fsys
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:32:18 +08:00
|
|
|
// Open a handle returning an integer file handle
|
|
|
|
func (fsys *FS) openHandle(handle vfs.Handle) (fh uint64) {
|
|
|
|
fsys.mu.Lock()
|
|
|
|
defer fsys.mu.Unlock()
|
2017-05-03 05:36:11 +08:00
|
|
|
var i int
|
2017-11-03 17:32:18 +08:00
|
|
|
var oldHandle vfs.Handle
|
|
|
|
for i, oldHandle = range fsys.handles {
|
|
|
|
if oldHandle == nil {
|
|
|
|
fsys.handles[i] = handle
|
2017-05-03 05:36:11 +08:00
|
|
|
goto found
|
|
|
|
}
|
|
|
|
}
|
2017-11-03 17:32:18 +08:00
|
|
|
fsys.handles = append(fsys.handles, handle)
|
|
|
|
i = len(fsys.handles) - 1
|
2017-05-03 05:36:11 +08:00
|
|
|
found:
|
2017-11-03 17:32:18 +08:00
|
|
|
return uint64(i)
|
2017-05-08 21:56:15 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 17:32:18 +08:00
|
|
|
// get the handle for fh, call with the lock held
|
|
|
|
func (fsys *FS) _getHandle(fh uint64) (i int, handle vfs.Handle, errc int) {
|
|
|
|
if fh > uint64(len(fsys.handles)) {
|
2017-05-08 21:56:15 +08:00
|
|
|
fs.Debugf(nil, "Bad file handle: too big: 0x%X", fh)
|
2017-05-03 05:36:11 +08:00
|
|
|
return i, nil, -fuse.EBADF
|
|
|
|
}
|
2017-11-03 17:32:18 +08:00
|
|
|
i = int(fh)
|
|
|
|
handle = fsys.handles[i]
|
|
|
|
if handle == nil {
|
|
|
|
fs.Debugf(nil, "Bad file handle: nil handle: 0x%X", fh)
|
2017-05-03 05:36:11 +08:00
|
|
|
return i, nil, -fuse.EBADF
|
|
|
|
}
|
2017-11-03 17:32:18 +08:00
|
|
|
return i, handle, 0
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 17:32:18 +08:00
|
|
|
// Get the handle for the file handle
|
|
|
|
func (fsys *FS) getHandle(fh uint64) (handle vfs.Handle, errc int) {
|
|
|
|
fsys.mu.Lock()
|
|
|
|
_, handle, errc = fsys._getHandle(fh)
|
|
|
|
fsys.mu.Unlock()
|
2017-05-03 05:36:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:32:18 +08:00
|
|
|
// Close the handle
|
|
|
|
func (fsys *FS) closeHandle(fh uint64) (errc int) {
|
|
|
|
fsys.mu.Lock()
|
|
|
|
i, _, errc := fsys._getHandle(fh)
|
2017-05-03 05:36:11 +08:00
|
|
|
if errc == 0 {
|
2017-11-03 17:32:18 +08:00
|
|
|
fsys.handles[i] = nil
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
2017-11-03 17:32:18 +08:00
|
|
|
fsys.mu.Unlock()
|
2017-05-03 05:36:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup a Node given a path
|
2017-10-29 03:01:34 +08:00
|
|
|
func (fsys *FS) lookupNode(path string) (node vfs.Node, errc int) {
|
2017-10-29 19:36:38 +08:00
|
|
|
node, err := fsys.VFS.Stat(path)
|
2017-05-03 05:36:11 +08:00
|
|
|
return node, translateError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup a Dir given a path
|
2017-10-29 03:01:34 +08:00
|
|
|
func (fsys *FS) lookupDir(path string) (dir *vfs.Dir, errc int) {
|
2017-05-03 05:36:11 +08:00
|
|
|
node, errc := fsys.lookupNode(path)
|
|
|
|
if errc != 0 {
|
|
|
|
return nil, errc
|
|
|
|
}
|
2017-10-29 03:01:34 +08:00
|
|
|
dir, ok := node.(*vfs.Dir)
|
2017-05-03 05:36:11 +08:00
|
|
|
if !ok {
|
|
|
|
return nil, -fuse.ENOTDIR
|
|
|
|
}
|
|
|
|
return dir, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup a parent Dir given a path returning the dir and the leaf
|
2017-10-29 03:01:34 +08:00
|
|
|
func (fsys *FS) lookupParentDir(filePath string) (leaf string, dir *vfs.Dir, errc int) {
|
2017-05-03 05:36:11 +08:00
|
|
|
parentDir, leaf := path.Split(filePath)
|
|
|
|
dir, errc = fsys.lookupDir(parentDir)
|
|
|
|
return leaf, dir, errc
|
|
|
|
}
|
|
|
|
|
2017-11-14 01:26:20 +08:00
|
|
|
// get a node and handle from the path or from the fh if not fhUnset
|
|
|
|
//
|
|
|
|
// handle may be nil
|
|
|
|
func (fsys *FS) getNode(path string, fh uint64) (node vfs.Node, handle vfs.Handle, errc int) {
|
2017-05-03 05:36:11 +08:00
|
|
|
if fh == fhUnset {
|
|
|
|
node, errc = fsys.lookupNode(path)
|
|
|
|
} else {
|
2017-11-14 01:26:20 +08:00
|
|
|
handle, errc = fsys.getHandle(fh)
|
2017-05-03 05:36:11 +08:00
|
|
|
if errc == 0 {
|
2017-11-14 01:26:20 +08:00
|
|
|
node = handle.Node()
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// stat fills up the stat block for Node
|
2017-10-29 03:01:34 +08:00
|
|
|
func (fsys *FS) stat(node vfs.Node, stat *fuse.Stat_t) (errc int) {
|
2017-11-03 17:32:18 +08:00
|
|
|
Size := uint64(node.Size())
|
|
|
|
Blocks := (Size + 511) / 512
|
|
|
|
modTime := node.ModTime()
|
|
|
|
Mode := node.Mode().Perm()
|
|
|
|
if node.IsDir() {
|
|
|
|
Mode |= fuse.S_IFDIR
|
|
|
|
} else {
|
|
|
|
Mode |= fuse.S_IFREG
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
//stat.Dev = 1
|
|
|
|
stat.Ino = node.Inode() // FIXME do we need to set the inode number?
|
|
|
|
stat.Mode = uint32(Mode)
|
|
|
|
stat.Nlink = 1
|
2017-10-29 19:00:56 +08:00
|
|
|
stat.Uid = fsys.VFS.Opt.UID
|
|
|
|
stat.Gid = fsys.VFS.Opt.GID
|
2017-05-03 05:36:11 +08:00
|
|
|
//stat.Rdev
|
|
|
|
stat.Size = int64(Size)
|
|
|
|
t := fuse.NewTimespec(modTime)
|
|
|
|
stat.Atim = t
|
|
|
|
stat.Mtim = t
|
|
|
|
stat.Ctim = t
|
|
|
|
stat.Blksize = 512
|
|
|
|
stat.Blocks = int64(Blocks)
|
|
|
|
stat.Birthtim = t
|
|
|
|
// fs.Debugf(nil, "stat = %+v", *stat)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init is called after the filesystem is ready
|
|
|
|
func (fsys *FS) Init() {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(fsys.f, "")("")
|
2017-05-03 05:36:11 +08:00
|
|
|
close(fsys.ready)
|
|
|
|
}
|
|
|
|
|
2017-05-09 01:05:38 +08:00
|
|
|
// Destroy is called when it is unmounted (note that depending on how
|
|
|
|
// the file system is terminated the file system may not receive the
|
|
|
|
// Destroy call).
|
2017-05-03 05:36:11 +08:00
|
|
|
func (fsys *FS) Destroy() {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(fsys.f, "")("")
|
2023-08-18 22:44:23 +08:00
|
|
|
fsys.destroyed.Store(1)
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Getattr reads the attributes for path
|
|
|
|
func (fsys *FS) Getattr(path string, stat *fuse.Stat_t, fh uint64) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "fh=0x%X", fh)("errc=%v", &errc)
|
2017-11-14 01:26:20 +08:00
|
|
|
node, _, errc := fsys.getNode(path, fh)
|
2017-05-03 05:36:11 +08:00
|
|
|
if errc == 0 {
|
|
|
|
errc = fsys.stat(node, stat)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opendir opens path as a directory
|
|
|
|
func (fsys *FS) Opendir(path string) (errc int, fh uint64) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "")("errc=%d, fh=0x%X", &errc, &fh)
|
2017-11-03 17:32:18 +08:00
|
|
|
handle, err := fsys.VFS.OpenFile(path, os.O_RDONLY, 0777)
|
2018-03-21 19:13:17 +08:00
|
|
|
if err != nil {
|
2017-11-03 17:32:18 +08:00
|
|
|
return translateError(err), fhUnset
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
2017-11-03 17:32:18 +08:00
|
|
|
return 0, fsys.openHandle(handle)
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Readdir reads the directory at dirPath
|
|
|
|
func (fsys *FS) Readdir(dirPath string,
|
|
|
|
fill func(name string, stat *fuse.Stat_t, ofst int64) bool,
|
|
|
|
ofst int64,
|
|
|
|
fh uint64) (errc int) {
|
2017-05-09 18:40:02 +08:00
|
|
|
itemsRead := -1
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(dirPath, "ofst=%d, fh=0x%X", ofst, fh)("items=%d, errc=%d", &itemsRead, &errc)
|
2017-05-03 05:36:11 +08:00
|
|
|
|
2020-07-05 19:09:49 +08:00
|
|
|
dir, errc := fsys.lookupDir(dirPath)
|
2017-05-03 05:36:11 +08:00
|
|
|
if errc != 0 {
|
|
|
|
return errc
|
|
|
|
}
|
|
|
|
|
2020-07-05 19:09:49 +08:00
|
|
|
// We can't seek in directories and FUSE should know that so
|
|
|
|
// return an error if ofst is ever set.
|
|
|
|
if ofst > 0 {
|
|
|
|
return -fuse.ESPIPE
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes, err := dir.ReadDirAll()
|
2017-05-03 05:36:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return translateError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally, create a struct stat that describes the file as
|
|
|
|
// for getattr (but FUSE only looks at st_ino and the
|
|
|
|
// file-type bits of st_mode).
|
|
|
|
//
|
2020-07-05 18:28:30 +08:00
|
|
|
// We have called host.SetCapReaddirPlus() so WinFsp will
|
2017-05-03 05:36:11 +08:00
|
|
|
// use the full stat information - a Useful optimization on
|
|
|
|
// Windows.
|
|
|
|
//
|
|
|
|
// NB we are using the first mode for readdir: The readdir
|
|
|
|
// implementation ignores the offset parameter, and passes
|
|
|
|
// zero to the filler function's offset. The filler function
|
|
|
|
// will not return '1' (unless an error happens), so the whole
|
|
|
|
// directory is read in a single readdir operation.
|
|
|
|
fill(".", nil, 0)
|
|
|
|
fill("..", nil, 0)
|
2020-07-05 19:09:49 +08:00
|
|
|
for _, node := range nodes {
|
|
|
|
name := node.Name()
|
|
|
|
if len(name) > mountlib.MaxLeafSize {
|
|
|
|
fs.Errorf(dirPath, "Name too long (%d bytes) for FUSE, skipping: %s", len(name), name)
|
|
|
|
continue
|
2017-11-03 17:32:18 +08:00
|
|
|
}
|
2020-07-05 19:09:49 +08:00
|
|
|
// We have called host.SetCapReaddirPlus() so supply the stat information
|
|
|
|
// It is very cheap at this point so supply it regardless of OS capabilities
|
|
|
|
var stat fuse.Stat_t
|
|
|
|
_ = fsys.stat(node, &stat) // not capable of returning an error
|
|
|
|
fill(name, &stat, 0)
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
2020-07-05 19:09:49 +08:00
|
|
|
itemsRead = len(nodes)
|
2017-05-03 05:36:11 +08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Releasedir finished reading the directory
|
|
|
|
func (fsys *FS) Releasedir(path string, fh uint64) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "fh=0x%X", fh)("errc=%d", &errc)
|
2017-11-03 17:32:18 +08:00
|
|
|
return fsys.closeHandle(fh)
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 08:17:24 +08:00
|
|
|
// Statfs reads overall stats on the filesystem
|
2017-05-03 05:36:11 +08:00
|
|
|
func (fsys *FS) Statfs(path string, stat *fuse.Statfs_t) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "")("stat=%+v, errc=%d", stat, &errc)
|
2017-05-03 05:36:11 +08:00
|
|
|
const blockSize = 4096
|
2020-04-09 01:29:50 +08:00
|
|
|
total, _, free := fsys.VFS.Statfs()
|
|
|
|
stat.Blocks = uint64(total) / blockSize // Total data blocks in file system.
|
|
|
|
stat.Bfree = uint64(free) / blockSize // Free blocks in file system.
|
|
|
|
stat.Bavail = stat.Bfree // Free blocks in file system if you're not root.
|
|
|
|
stat.Files = 1e9 // Total files in file system.
|
|
|
|
stat.Ffree = 1e9 // Free files in file system.
|
|
|
|
stat.Bsize = blockSize // Block size
|
|
|
|
stat.Namemax = 255 // Maximum file name length?
|
|
|
|
stat.Frsize = blockSize // Fragment size, smallest addressable data size in the file system.
|
2018-06-26 16:26:34 +08:00
|
|
|
mountlib.ClipBlocks(&stat.Blocks)
|
|
|
|
mountlib.ClipBlocks(&stat.Bfree)
|
|
|
|
mountlib.ClipBlocks(&stat.Bavail)
|
2017-05-03 05:36:11 +08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-05-07 18:55:56 +08:00
|
|
|
// OpenEx opens a file
|
|
|
|
func (fsys *FS) OpenEx(path string, fi *fuse.FileInfo_t) (errc int) {
|
|
|
|
defer log.Trace(path, "flags=0x%X", fi.Flags)("errc=%d, fh=0x%X", &errc, &fi.Fh)
|
|
|
|
fi.Fh = fhUnset
|
2017-10-30 18:14:39 +08:00
|
|
|
|
2017-11-16 17:27:26 +08:00
|
|
|
// translate the fuse flags to os flags
|
2020-05-07 18:55:56 +08:00
|
|
|
flags := translateOpenFlags(fi.Flags)
|
2017-10-30 18:14:39 +08:00
|
|
|
handle, err := fsys.VFS.OpenFile(path, flags, 0777)
|
2018-03-21 19:13:17 +08:00
|
|
|
if err != nil {
|
2020-05-07 18:55:56 +08:00
|
|
|
return translateError(err)
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
2017-10-30 18:14:39 +08:00
|
|
|
|
2020-05-07 18:55:56 +08:00
|
|
|
// If size unknown then use direct io to read
|
|
|
|
if entry := handle.Node().DirEntry(); entry != nil && entry.Size() < 0 {
|
|
|
|
fi.DirectIo = true
|
|
|
|
}
|
2024-03-04 19:37:18 +08:00
|
|
|
if fsys.opt.DirectIO {
|
|
|
|
fi.DirectIo = true
|
|
|
|
}
|
2019-09-14 20:11:21 +08:00
|
|
|
|
2020-05-07 18:55:56 +08:00
|
|
|
fi.Fh = fsys.openHandle(handle)
|
|
|
|
return 0
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
2020-05-07 18:55:56 +08:00
|
|
|
// Open opens a file
|
|
|
|
func (fsys *FS) Open(path string, flags int) (errc int, fh uint64) {
|
|
|
|
var fi = fuse.FileInfo_t{
|
|
|
|
Flags: flags,
|
|
|
|
}
|
|
|
|
errc = fsys.OpenEx(path, &fi)
|
|
|
|
return errc, fi.Fh
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateEx creates and opens a file.
|
|
|
|
func (fsys *FS) CreateEx(filePath string, mode uint32, fi *fuse.FileInfo_t) (errc int) {
|
|
|
|
defer log.Trace(filePath, "flags=0x%X, mode=0%o", fi.Flags, mode)("errc=%d, fh=0x%X", &errc, &fi.Fh)
|
|
|
|
fi.Fh = fhUnset
|
2017-05-03 05:36:11 +08:00
|
|
|
leaf, parentDir, errc := fsys.lookupParentDir(filePath)
|
|
|
|
if errc != 0 {
|
2020-05-07 18:55:56 +08:00
|
|
|
return errc
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
2020-05-07 18:55:56 +08:00
|
|
|
file, err := parentDir.Create(leaf, fi.Flags)
|
2017-11-06 20:22:45 +08:00
|
|
|
if err != nil {
|
2020-05-07 18:55:56 +08:00
|
|
|
return translateError(err)
|
2017-11-06 20:22:45 +08:00
|
|
|
}
|
2017-11-16 17:27:26 +08:00
|
|
|
// translate the fuse flags to os flags
|
2020-05-07 18:55:56 +08:00
|
|
|
flags := translateOpenFlags(fi.Flags) | os.O_CREATE
|
2017-11-06 20:22:45 +08:00
|
|
|
handle, err := file.Open(flags)
|
2017-05-03 05:36:11 +08:00
|
|
|
if err != nil {
|
2020-05-07 18:55:56 +08:00
|
|
|
return translateError(err)
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
2020-05-07 18:55:56 +08:00
|
|
|
fi.Fh = fsys.openHandle(handle)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates and opens a file.
|
|
|
|
func (fsys *FS) Create(filePath string, flags int, mode uint32) (errc int, fh uint64) {
|
|
|
|
var fi = fuse.FileInfo_t{
|
|
|
|
Flags: flags,
|
|
|
|
}
|
|
|
|
errc = fsys.CreateEx(filePath, mode, &fi)
|
|
|
|
return errc, fi.Fh
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
2017-05-09 01:05:38 +08:00
|
|
|
// Truncate truncates a file to size
|
2017-05-03 05:36:11 +08:00
|
|
|
func (fsys *FS) Truncate(path string, size int64, fh uint64) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "size=%d, fh=0x%X", size, fh)("errc=%d", &errc)
|
2017-11-14 01:26:20 +08:00
|
|
|
node, handle, errc := fsys.getNode(path, fh)
|
2017-05-03 05:36:11 +08:00
|
|
|
if errc != 0 {
|
|
|
|
return errc
|
|
|
|
}
|
2017-11-16 17:31:33 +08:00
|
|
|
var err error
|
2017-11-14 01:26:20 +08:00
|
|
|
if handle != nil {
|
2017-11-16 17:31:33 +08:00
|
|
|
err = handle.Truncate(size)
|
2017-11-14 01:26:20 +08:00
|
|
|
} else {
|
2017-11-16 17:31:33 +08:00
|
|
|
err = node.Truncate(size)
|
2017-11-14 01:26:20 +08:00
|
|
|
}
|
2017-11-16 17:31:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return translateError(err)
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:32:18 +08:00
|
|
|
// Read data from file handle
|
2017-05-03 05:36:11 +08:00
|
|
|
func (fsys *FS) Read(path string, buff []byte, ofst int64, fh uint64) (n int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "ofst=%d, fh=0x%X", ofst, fh)("n=%d", &n)
|
2017-11-03 17:32:18 +08:00
|
|
|
handle, errc := fsys.getHandle(fh)
|
2017-05-03 05:36:11 +08:00
|
|
|
if errc != 0 {
|
|
|
|
return errc
|
|
|
|
}
|
2017-10-30 05:14:05 +08:00
|
|
|
n, err := handle.ReadAt(buff, ofst)
|
|
|
|
if err == io.EOF {
|
|
|
|
} else if err != nil {
|
2017-05-03 05:36:11 +08:00
|
|
|
return translateError(err)
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:32:18 +08:00
|
|
|
// Write data to file handle
|
2017-05-03 05:36:11 +08:00
|
|
|
func (fsys *FS) Write(path string, buff []byte, ofst int64, fh uint64) (n int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "ofst=%d, fh=0x%X", ofst, fh)("n=%d", &n)
|
2017-11-03 17:32:18 +08:00
|
|
|
handle, errc := fsys.getHandle(fh)
|
2017-05-03 05:36:11 +08:00
|
|
|
if errc != 0 {
|
|
|
|
return errc
|
|
|
|
}
|
2020-02-28 22:44:15 +08:00
|
|
|
n, err := handle.WriteAt(buff, ofst)
|
2017-05-03 05:36:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return translateError(err)
|
|
|
|
}
|
2017-10-28 04:41:34 +08:00
|
|
|
return n
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
2017-05-09 01:05:38 +08:00
|
|
|
// Flush flushes an open file descriptor or path
|
2017-05-03 05:36:11 +08:00
|
|
|
func (fsys *FS) Flush(path string, fh uint64) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "fh=0x%X", fh)("errc=%d", &errc)
|
2017-11-03 17:32:18 +08:00
|
|
|
handle, errc := fsys.getHandle(fh)
|
2017-05-03 05:36:11 +08:00
|
|
|
if errc != 0 {
|
|
|
|
return errc
|
|
|
|
}
|
2017-11-03 17:32:18 +08:00
|
|
|
return translateError(handle.Flush())
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
2017-05-09 01:05:38 +08:00
|
|
|
// Release closes the file if still open
|
2017-05-03 05:36:11 +08:00
|
|
|
func (fsys *FS) Release(path string, fh uint64) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "fh=0x%X", fh)("errc=%d", &errc)
|
2017-11-03 17:32:18 +08:00
|
|
|
handle, errc := fsys.getHandle(fh)
|
2017-05-09 22:10:26 +08:00
|
|
|
if errc != 0 {
|
|
|
|
return errc
|
|
|
|
}
|
2017-11-03 17:32:18 +08:00
|
|
|
_ = fsys.closeHandle(fh)
|
|
|
|
return translateError(handle.Release())
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unlink removes a file.
|
2017-05-09 18:40:02 +08:00
|
|
|
func (fsys *FS) Unlink(filePath string) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(filePath, "")("errc=%d", &errc)
|
2017-05-03 05:36:11 +08:00
|
|
|
leaf, parentDir, errc := fsys.lookupParentDir(filePath)
|
|
|
|
if errc != 0 {
|
|
|
|
return errc
|
|
|
|
}
|
2017-10-26 22:37:45 +08:00
|
|
|
return translateError(parentDir.RemoveName(leaf))
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a directory.
|
|
|
|
func (fsys *FS) Mkdir(dirPath string, mode uint32) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(dirPath, "mode=0%o", mode)("errc=%d", &errc)
|
2017-05-03 05:36:11 +08:00
|
|
|
leaf, parentDir, errc := fsys.lookupParentDir(dirPath)
|
|
|
|
if errc != 0 {
|
|
|
|
return errc
|
|
|
|
}
|
|
|
|
_, err := parentDir.Mkdir(leaf)
|
|
|
|
return translateError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir removes a directory
|
2017-05-09 18:40:02 +08:00
|
|
|
func (fsys *FS) Rmdir(dirPath string) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(dirPath, "")("errc=%d", &errc)
|
2017-05-03 05:36:11 +08:00
|
|
|
leaf, parentDir, errc := fsys.lookupParentDir(dirPath)
|
|
|
|
if errc != 0 {
|
|
|
|
return errc
|
|
|
|
}
|
2017-10-26 22:37:45 +08:00
|
|
|
return translateError(parentDir.RemoveName(leaf))
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename renames a file.
|
|
|
|
func (fsys *FS) Rename(oldPath string, newPath string) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(oldPath, "newPath=%q", newPath)("errc=%d", &errc)
|
2017-11-03 17:32:18 +08:00
|
|
|
return translateError(fsys.VFS.Rename(oldPath, newPath))
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
2019-10-10 05:28:07 +08:00
|
|
|
// Windows sometimes seems to send times that are the epoch which is
|
|
|
|
// 1601-01-01 +/- timezone so filter out times that are earlier than
|
|
|
|
// this.
|
|
|
|
var invalidDateCutoff = time.Date(1601, 1, 2, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
2017-05-03 05:36:11 +08:00
|
|
|
// Utimens changes the access and modification times of a file.
|
|
|
|
func (fsys *FS) Utimens(path string, tmsp []fuse.Timespec) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "tmsp=%+v", tmsp)("errc=%d", &errc)
|
2017-05-03 05:36:11 +08:00
|
|
|
node, errc := fsys.lookupNode(path)
|
|
|
|
if errc != 0 {
|
|
|
|
return errc
|
|
|
|
}
|
|
|
|
if tmsp == nil || len(tmsp) < 2 {
|
2019-10-10 05:28:07 +08:00
|
|
|
fs.Debugf(path, "Utimens: Not setting time as timespec isn't complete: %v", tmsp)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
t := tmsp[1].Time()
|
|
|
|
if t.Before(invalidDateCutoff) {
|
|
|
|
fs.Debugf(path, "Utimens: Not setting out of range time: %v", t)
|
|
|
|
return 0
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
2019-10-10 05:28:07 +08:00
|
|
|
fs.Debugf(path, "Utimens: SetModTime: %v", t)
|
2017-11-03 17:32:18 +08:00
|
|
|
return translateError(node.SetModTime(t))
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
2017-05-09 20:59:21 +08:00
|
|
|
// Mknod creates a file node.
|
|
|
|
func (fsys *FS) Mknod(path string, mode uint32, dev uint64) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "mode=0x%X, dev=0x%X", mode, dev)("errc=%d", &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
return -fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fsync synchronizes file contents.
|
|
|
|
func (fsys *FS) Fsync(path string, datasync bool, fh uint64) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "datasync=%v, fh=0x%X", datasync, fh)("errc=%d", &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
// This is a no-op for rclone
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link creates a hard link to a file.
|
|
|
|
func (fsys *FS) Link(oldpath string, newpath string) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(oldpath, "newpath=%q", newpath)("errc=%d", &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
return -fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink creates a symbolic link.
|
|
|
|
func (fsys *FS) Symlink(target string, newpath string) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(target, "newpath=%q", newpath)("errc=%d", &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
return -fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readlink reads the target of a symbolic link.
|
|
|
|
func (fsys *FS) Readlink(path string) (errc int, linkPath string) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "")("linkPath=%q, errc=%d", &linkPath, &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
return -fuse.ENOSYS, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod changes the permission bits of a file.
|
|
|
|
func (fsys *FS) Chmod(path string, mode uint32) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "mode=0%o", mode)("errc=%d", &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
// This is a no-op for rclone
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chown changes the owner and group of a file.
|
|
|
|
func (fsys *FS) Chown(path string, uid uint32, gid uint32) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "uid=%d, gid=%d", uid, gid)("errc=%d", &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
// This is a no-op for rclone
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Access checks file access permissions.
|
|
|
|
func (fsys *FS) Access(path string, mask uint32) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "mask=0%o", mask)("errc=%d", &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
// This is a no-op for rclone
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fsyncdir synchronizes directory contents.
|
|
|
|
func (fsys *FS) Fsyncdir(path string, datasync bool, fh uint64) (errc int) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(path, "datasync=%v, fh=0x%X", datasync, fh)("errc=%d", &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
// This is a no-op for rclone
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setxattr sets extended attributes.
|
|
|
|
func (fsys *FS) Setxattr(path string, name string, value []byte, flags int) (errc int) {
|
2022-04-15 19:11:14 +08:00
|
|
|
defer log.Trace(path, "name=%q, value=%q, flags=%d", name, value, flags)("errc=%d", &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
return -fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getxattr gets extended attributes.
|
|
|
|
func (fsys *FS) Getxattr(path string, name string) (errc int, value []byte) {
|
2022-04-15 19:11:14 +08:00
|
|
|
defer log.Trace(path, "name=%q", name)("errc=%d, value=%q", &errc, &value)
|
2017-05-09 20:59:21 +08:00
|
|
|
return -fuse.ENOSYS, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removexattr removes extended attributes.
|
|
|
|
func (fsys *FS) Removexattr(path string, name string) (errc int) {
|
2022-04-15 19:11:14 +08:00
|
|
|
defer log.Trace(path, "name=%q", name)("errc=%d", &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
return -fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listxattr lists extended attributes.
|
|
|
|
func (fsys *FS) Listxattr(path string, fill func(name string) bool) (errc int) {
|
2022-04-15 19:11:14 +08:00
|
|
|
defer log.Trace(path, "fill=%p", fill)("errc=%d", &errc)
|
2017-05-09 20:59:21 +08:00
|
|
|
return -fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
2023-01-12 00:23:40 +08:00
|
|
|
// Getpath allows a case-insensitive file system to report the correct case of
|
|
|
|
// a file path.
|
|
|
|
func (fsys *FS) Getpath(path string, fh uint64) (errc int, normalisedPath string) {
|
|
|
|
defer log.Trace(path, "Getpath fh=%d", fh)("errc=%d, normalisedPath=%q", &errc, &normalisedPath)
|
|
|
|
node, _, errc := fsys.getNode(path, fh)
|
|
|
|
if errc != 0 {
|
|
|
|
return errc, ""
|
|
|
|
}
|
|
|
|
normalisedPath = node.Path()
|
2024-10-20 19:56:24 +08:00
|
|
|
if !strings.HasPrefix(normalisedPath, "/") {
|
2023-01-12 00:23:40 +08:00
|
|
|
normalisedPath = "/" + normalisedPath
|
|
|
|
}
|
|
|
|
return 0, normalisedPath
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:36:11 +08:00
|
|
|
// Translate errors from mountlib
|
2017-05-09 20:59:21 +08:00
|
|
|
func translateError(err error) (errc int) {
|
2017-05-03 05:36:11 +08:00
|
|
|
if err == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2021-11-04 18:12:57 +08:00
|
|
|
_, uErr := fserrors.Cause(err)
|
|
|
|
switch uErr {
|
2017-10-29 03:16:03 +08:00
|
|
|
case vfs.OK:
|
|
|
|
return 0
|
2020-04-21 23:31:43 +08:00
|
|
|
case vfs.ENOENT, fs.ErrorDirNotFound, fs.ErrorObjectNotFound:
|
2017-10-29 03:16:03 +08:00
|
|
|
return -fuse.ENOENT
|
2020-04-21 23:31:43 +08:00
|
|
|
case vfs.EEXIST, fs.ErrorDirExists:
|
2017-10-29 03:16:03 +08:00
|
|
|
return -fuse.EEXIST
|
2020-04-21 23:31:43 +08:00
|
|
|
case vfs.EPERM, fs.ErrorPermissionDenied:
|
2017-11-03 19:35:36 +08:00
|
|
|
return -fuse.EPERM
|
|
|
|
case vfs.ECLOSED:
|
|
|
|
return -fuse.EBADF
|
|
|
|
case vfs.ENOTEMPTY:
|
|
|
|
return -fuse.ENOTEMPTY
|
2017-10-29 03:16:03 +08:00
|
|
|
case vfs.ESPIPE:
|
|
|
|
return -fuse.ESPIPE
|
|
|
|
case vfs.EBADF:
|
|
|
|
return -fuse.EBADF
|
|
|
|
case vfs.EROFS:
|
|
|
|
return -fuse.EROFS
|
2020-04-21 23:31:43 +08:00
|
|
|
case vfs.ENOSYS, fs.ErrorNotImplemented:
|
2017-10-30 18:14:39 +08:00
|
|
|
return -fuse.ENOSYS
|
2018-02-24 06:39:28 +08:00
|
|
|
case vfs.EINVAL:
|
|
|
|
return -fuse.EINVAL
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
fs.Errorf(nil, "IO error: %v", err)
|
|
|
|
return -fuse.EIO
|
|
|
|
}
|
2017-11-16 17:27:26 +08:00
|
|
|
|
|
|
|
// Translate Open Flags from FUSE to os (as used in the vfs layer)
|
|
|
|
func translateOpenFlags(inFlags int) (outFlags int) {
|
|
|
|
switch inFlags & fuse.O_ACCMODE {
|
|
|
|
case fuse.O_RDONLY:
|
|
|
|
outFlags = os.O_RDONLY
|
|
|
|
case fuse.O_WRONLY:
|
|
|
|
outFlags = os.O_WRONLY
|
|
|
|
case fuse.O_RDWR:
|
|
|
|
outFlags = os.O_RDWR
|
|
|
|
}
|
|
|
|
if inFlags&fuse.O_APPEND != 0 {
|
|
|
|
outFlags |= os.O_APPEND
|
|
|
|
}
|
|
|
|
if inFlags&fuse.O_CREAT != 0 {
|
|
|
|
outFlags |= os.O_CREATE
|
|
|
|
}
|
|
|
|
if inFlags&fuse.O_EXCL != 0 {
|
|
|
|
outFlags |= os.O_EXCL
|
|
|
|
}
|
|
|
|
if inFlags&fuse.O_TRUNC != 0 {
|
|
|
|
outFlags |= os.O_TRUNC
|
|
|
|
}
|
|
|
|
// NB O_SYNC isn't defined by fuse
|
|
|
|
return outFlags
|
|
|
|
}
|
2020-05-07 18:55:56 +08:00
|
|
|
|
|
|
|
// Make sure interfaces are satisfied
|
|
|
|
var (
|
|
|
|
_ fuse.FileSystemInterface = (*FS)(nil)
|
|
|
|
_ fuse.FileSystemOpenEx = (*FS)(nil)
|
2023-01-12 00:23:40 +08:00
|
|
|
_ fuse.FileSystemGetpath = (*FS)(nil)
|
2020-05-07 18:55:56 +08:00
|
|
|
//_ fuse.FileSystemChflags = (*FS)(nil)
|
|
|
|
//_ fuse.FileSystemSetcrtime = (*FS)(nil)
|
|
|
|
//_ fuse.FileSystemSetchgtime = (*FS)(nil)
|
|
|
|
)
|