2021-09-09 20:25:25 +08:00
|
|
|
//go:build !plan9
|
2019-05-11 02:00:18 +08:00
|
|
|
// +build !plan9
|
|
|
|
|
|
|
|
package sftp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/sftp"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
2019-05-11 02:00:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// vfsHandler converts the VFS to be served by SFTP
|
|
|
|
type vfsHandler struct {
|
|
|
|
*vfs.VFS
|
|
|
|
}
|
|
|
|
|
|
|
|
// vfsHandler returns a Handlers object with the test handlers.
|
2019-08-01 05:20:46 +08:00
|
|
|
func newVFSHandler(vfs *vfs.VFS) sftp.Handlers {
|
2019-05-11 02:00:18 +08:00
|
|
|
v := vfsHandler{VFS: vfs}
|
|
|
|
return sftp.Handlers{
|
|
|
|
FileGet: v,
|
|
|
|
FilePut: v,
|
|
|
|
FileCmd: v,
|
|
|
|
FileList: v,
|
2019-08-01 05:20:46 +08:00
|
|
|
}
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v vfsHandler) Fileread(r *sftp.Request) (io.ReaderAt, error) {
|
|
|
|
file, err := v.OpenFile(r.Filepath, os.O_RDONLY, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v vfsHandler) Filewrite(r *sftp.Request) (io.WriterAt, error) {
|
|
|
|
file, err := v.OpenFile(r.Filepath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v vfsHandler) Filecmd(r *sftp.Request) error {
|
|
|
|
switch r.Method {
|
|
|
|
case "Setstat":
|
|
|
|
attr := r.Attributes()
|
|
|
|
if attr.Mtime != 0 {
|
|
|
|
modTime := time.Unix(int64(attr.Mtime), 0)
|
2020-04-08 22:48:57 +08:00
|
|
|
err := v.Chtimes(r.Filepath, modTime, modTime)
|
2019-05-11 02:00:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case "Rename":
|
|
|
|
err := v.Rename(r.Filepath, r.Target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "Rmdir", "Remove":
|
2020-04-08 22:48:57 +08:00
|
|
|
err := v.Remove(r.Filepath)
|
2019-05-11 02:00:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "Mkdir":
|
2020-04-08 22:48:57 +08:00
|
|
|
err := v.Mkdir(r.Filepath, 0777)
|
2019-05-11 02:00:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "Symlink":
|
|
|
|
// FIXME
|
|
|
|
// _, err := v.fetch(r.Filepath)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// link := newMemFile(r.Target, false)
|
|
|
|
// link.symlink = r.Filepath
|
|
|
|
// v.files[r.Target] = link
|
2019-10-10 00:43:03 +08:00
|
|
|
return sftp.ErrSshFxOpUnsupported
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
2019-10-10 00:43:03 +08:00
|
|
|
return nil
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type listerat []os.FileInfo
|
|
|
|
|
|
|
|
// Modeled after strings.Reader's ReadAt() implementation
|
|
|
|
func (f listerat) ListAt(ls []os.FileInfo, offset int64) (int, error) {
|
|
|
|
var n int
|
|
|
|
if offset >= int64(len(f)) {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
n = copy(ls, f[offset:])
|
|
|
|
if n < len(ls) {
|
|
|
|
return n, io.EOF
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v vfsHandler) Filelist(r *sftp.Request) (l sftp.ListerAt, err error) {
|
|
|
|
var node vfs.Node
|
|
|
|
var handle vfs.Handle
|
|
|
|
switch r.Method {
|
|
|
|
case "List":
|
|
|
|
node, err = v.Stat(r.Filepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !node.IsDir() {
|
|
|
|
return nil, syscall.ENOTDIR
|
|
|
|
}
|
|
|
|
handle, err = node.Open(os.O_RDONLY)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(handle, &err)
|
|
|
|
fis, err := handle.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return listerat(fis), nil
|
|
|
|
case "Stat":
|
|
|
|
node, err = v.Stat(r.Filepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return listerat([]os.FileInfo{node}), nil
|
|
|
|
case "Readlink":
|
|
|
|
// FIXME
|
|
|
|
// if file.symlink != "" {
|
|
|
|
// file, err = v.fetch(file.symlink)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return listerat([]os.FileInfo{file}), nil
|
|
|
|
}
|
2019-10-09 16:51:04 +08:00
|
|
|
return nil, sftp.ErrSshFxOpUnsupported
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|