extend virtual folders support to all storage backends

Fixes #241
This commit is contained in:
Nicola Murino
2021-03-21 19:15:47 +01:00
parent 0286da2356
commit d6dc3a507e
70 changed files with 6825 additions and 3740 deletions

View File

@@ -76,36 +76,51 @@ func (c *scpCommand) handleRecursiveUpload() error {
numDirs := 0
destPath := c.getDestPath()
for {
fs, err := c.connection.User.GetFilesystemForPath(destPath, c.connection.ID)
if err != nil {
c.connection.Log(logger.LevelWarn, "error uploading file %#v: %+v", destPath, err)
c.sendErrorMessage(nil, fmt.Errorf("unable to get fs for path %#v", destPath))
return err
}
command, err := c.getNextUploadProtocolMessage()
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
c.sendErrorMessage(fs, err)
return err
}
if strings.HasPrefix(command, "E") {
numDirs--
c.connection.Log(logger.LevelDebug, "received end dir command, num dirs: %v", numDirs)
if numDirs < 0 {
return errors.New("unacceptable end dir command")
err = errors.New("unacceptable end dir command")
c.sendErrorMessage(nil, err)
return err
}
// the destination dir is now the parent directory
destPath = path.Join(destPath, "..")
} else {
sizeToRead, name, err := c.parseUploadMessage(command)
sizeToRead, name, err := c.parseUploadMessage(fs, command)
if err != nil {
return err
}
if strings.HasPrefix(command, "D") {
numDirs++
destPath = path.Join(destPath, name)
err = c.handleCreateDir(destPath)
fs, err = c.connection.User.GetFilesystemForPath(destPath, c.connection.ID)
if err != nil {
c.connection.Log(logger.LevelWarn, "error uploading file %#v: %+v", destPath, err)
c.sendErrorMessage(nil, fmt.Errorf("unable to get fs for path %#v", destPath))
return err
}
err = c.handleCreateDir(fs, destPath)
if err != nil {
return err
}
c.connection.Log(logger.LevelDebug, "received start dir command, num dirs: %v destPath: %#v", numDirs, destPath)
} else if strings.HasPrefix(command, "C") {
err = c.handleUpload(c.getFileUploadDestPath(destPath, name), sizeToRead)
err = c.handleUpload(c.getFileUploadDestPath(fs, destPath, name), sizeToRead)
if err != nil {
return err
}
@@ -118,21 +133,27 @@ func (c *scpCommand) handleRecursiveUpload() error {
}
}
func (c *scpCommand) handleCreateDir(dirPath string) error {
func (c *scpCommand) handleCreateDir(fs vfs.Fs, dirPath string) error {
c.connection.UpdateLastActivity()
p, err := c.connection.Fs.ResolvePath(dirPath)
p, err := fs.ResolvePath(dirPath)
if err != nil {
c.connection.Log(logger.LevelWarn, "error creating dir: %#v, invalid file path, err: %v", dirPath, err)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
if !c.connection.User.HasPerm(dataprovider.PermCreateDirs, path.Dir(dirPath)) {
c.connection.Log(logger.LevelWarn, "error creating dir: %#v, permission denied", dirPath)
c.sendErrorMessage(common.ErrPermissionDenied)
c.sendErrorMessage(fs, common.ErrPermissionDenied)
return common.ErrPermissionDenied
}
err = c.createDir(p)
info, err := c.connection.DoStat(dirPath, 1)
if err == nil && info.IsDir() {
return nil
}
err = c.createDir(fs, p)
if err != nil {
return err
}
@@ -156,14 +177,14 @@ func (c *scpCommand) getUploadFileData(sizeToRead int64, transfer *transfer) err
for {
n, err := c.connection.channel.Read(buf)
if err != nil {
c.sendErrorMessage(err)
c.sendErrorMessage(transfer.Fs, err)
transfer.TransferError(err)
transfer.Close()
return err
}
_, err = transfer.WriteAt(buf[:n], sizeToRead-remaining)
if err != nil {
c.sendErrorMessage(err)
c.sendErrorMessage(transfer.Fs, err)
transfer.Close()
return err
}
@@ -184,33 +205,33 @@ func (c *scpCommand) getUploadFileData(sizeToRead int64, transfer *transfer) err
}
err = transfer.Close()
if err != nil {
c.sendErrorMessage(err)
c.sendErrorMessage(transfer.Fs, err)
return err
}
return nil
}
func (c *scpCommand) handleUploadFile(resolvedPath, filePath string, sizeToRead int64, isNewFile bool, fileSize int64, requestPath string) error {
func (c *scpCommand) handleUploadFile(fs vfs.Fs, resolvedPath, filePath string, sizeToRead int64, isNewFile bool, fileSize int64, requestPath string) error {
quotaResult := c.connection.HasSpace(isNewFile, false, requestPath)
if !quotaResult.HasSpace {
err := fmt.Errorf("denying file write due to quota limits")
c.connection.Log(logger.LevelWarn, "error uploading file: %#v, err: %v", filePath, err)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
maxWriteSize, _ := c.connection.GetMaxWriteSize(quotaResult, false, fileSize)
maxWriteSize, _ := c.connection.GetMaxWriteSize(quotaResult, false, fileSize, fs.IsUploadResumeSupported())
file, w, cancelFn, err := c.connection.Fs.Create(filePath, 0)
file, w, cancelFn, err := fs.Create(filePath, 0)
if err != nil {
c.connection.Log(logger.LevelError, "error creating file %#v: %v", resolvedPath, err)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
initialSize := int64(0)
if !isNewFile {
if vfs.IsLocalOrSFTPFs(c.connection.Fs) {
if vfs.IsLocalOrSFTPFs(fs) {
vfolder, err := c.connection.User.GetVirtualFolderForPath(path.Dir(requestPath))
if err == nil {
dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, 0, -fileSize, false) //nolint:errcheck
@@ -228,10 +249,10 @@ func (c *scpCommand) handleUploadFile(resolvedPath, filePath string, sizeToRead
}
}
vfs.SetPathPermissions(c.connection.Fs, filePath, c.connection.User.GetUID(), c.connection.User.GetGID())
vfs.SetPathPermissions(fs, filePath, c.connection.User.GetUID(), c.connection.User.GetGID())
baseTransfer := common.NewBaseTransfer(file, c.connection.BaseConnection, cancelFn, resolvedPath, requestPath,
common.TransferUpload, 0, initialSize, maxWriteSize, isNewFile, c.connection.Fs)
common.TransferUpload, 0, initialSize, maxWriteSize, isNewFile, fs)
t := newTransfer(baseTransfer, w, nil, nil)
return c.getUploadFileData(sizeToRead, t)
@@ -240,67 +261,66 @@ func (c *scpCommand) handleUploadFile(resolvedPath, filePath string, sizeToRead
func (c *scpCommand) handleUpload(uploadFilePath string, sizeToRead int64) error {
c.connection.UpdateLastActivity()
var err error
fs, p, err := c.connection.GetFsAndResolvedPath(uploadFilePath)
if err != nil {
c.connection.Log(logger.LevelWarn, "error uploading file: %#v, err: %v", uploadFilePath, err)
c.sendErrorMessage(nil, err)
return err
}
if !c.connection.User.IsFileAllowed(uploadFilePath) {
c.connection.Log(logger.LevelWarn, "writing file %#v is not allowed", uploadFilePath)
c.sendErrorMessage(common.ErrPermissionDenied)
c.sendErrorMessage(fs, common.ErrPermissionDenied)
return common.ErrPermissionDenied
}
p, err := c.connection.Fs.ResolvePath(uploadFilePath)
if err != nil {
c.connection.Log(logger.LevelWarn, "error uploading file: %#v, err: %v", uploadFilePath, err)
c.sendErrorMessage(err)
return err
}
filePath := p
if common.Config.IsAtomicUploadEnabled() && c.connection.Fs.IsAtomicUploadSupported() {
filePath = c.connection.Fs.GetAtomicUploadPath(p)
if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
filePath = fs.GetAtomicUploadPath(p)
}
stat, statErr := c.connection.Fs.Lstat(p)
if (statErr == nil && stat.Mode()&os.ModeSymlink != 0) || c.connection.Fs.IsNotExist(statErr) {
stat, statErr := fs.Lstat(p)
if (statErr == nil && stat.Mode()&os.ModeSymlink != 0) || fs.IsNotExist(statErr) {
if !c.connection.User.HasPerm(dataprovider.PermUpload, path.Dir(uploadFilePath)) {
c.connection.Log(logger.LevelWarn, "cannot upload file: %#v, permission denied", uploadFilePath)
c.sendErrorMessage(common.ErrPermissionDenied)
c.sendErrorMessage(fs, common.ErrPermissionDenied)
return common.ErrPermissionDenied
}
return c.handleUploadFile(p, filePath, sizeToRead, true, 0, uploadFilePath)
return c.handleUploadFile(fs, p, filePath, sizeToRead, true, 0, uploadFilePath)
}
if statErr != nil {
c.connection.Log(logger.LevelError, "error performing file stat %#v: %v", p, statErr)
c.sendErrorMessage(statErr)
c.sendErrorMessage(fs, statErr)
return statErr
}
if stat.IsDir() {
c.connection.Log(logger.LevelWarn, "attempted to open a directory for writing to: %#v", p)
err = fmt.Errorf("Attempted to open a directory for writing: %#v", p)
c.sendErrorMessage(err)
err = fmt.Errorf("attempted to open a directory for writing: %#v", p)
c.sendErrorMessage(fs, err)
return err
}
if !c.connection.User.HasPerm(dataprovider.PermOverwrite, uploadFilePath) {
c.connection.Log(logger.LevelWarn, "cannot overwrite file: %#v, permission denied", uploadFilePath)
c.sendErrorMessage(common.ErrPermissionDenied)
c.sendErrorMessage(fs, common.ErrPermissionDenied)
return common.ErrPermissionDenied
}
if common.Config.IsAtomicUploadEnabled() && c.connection.Fs.IsAtomicUploadSupported() {
err = c.connection.Fs.Rename(p, filePath)
if common.Config.IsAtomicUploadEnabled() && fs.IsAtomicUploadSupported() {
err = fs.Rename(p, filePath)
if err != nil {
c.connection.Log(logger.LevelError, "error renaming existing file for atomic upload, source: %#v, dest: %#v, err: %v",
p, filePath, err)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
}
return c.handleUploadFile(p, filePath, sizeToRead, false, stat.Size(), uploadFilePath)
return c.handleUploadFile(fs, p, filePath, sizeToRead, false, stat.Size(), uploadFilePath)
}
func (c *scpCommand) sendDownloadProtocolMessages(dirPath string, stat os.FileInfo) error {
func (c *scpCommand) sendDownloadProtocolMessages(virtualDirPath string, stat os.FileInfo) error {
var err error
if c.sendFileTime() {
modTime := stat.ModTime().UnixNano() / 1000000000
@@ -315,12 +335,9 @@ func (c *scpCommand) sendDownloadProtocolMessages(dirPath string, stat os.FileIn
}
}
dirName := filepath.Base(dirPath)
for _, v := range c.connection.User.VirtualFolders {
if v.MappedPath == dirPath {
dirName = path.Base(v.VirtualPath)
break
}
dirName := path.Base(virtualDirPath)
if dirName == "/" || dirName == "." {
dirName = c.connection.User.Username
}
fileMode := fmt.Sprintf("D%v 0 %v\n", getFileModeAsString(stat.Mode(), stat.IsDir()), dirName)
@@ -334,23 +351,23 @@ func (c *scpCommand) sendDownloadProtocolMessages(dirPath string, stat os.FileIn
// We send first all the files in the root directory and then the directories.
// For each directory we recursively call this method again
func (c *scpCommand) handleRecursiveDownload(dirPath string, stat os.FileInfo) error {
func (c *scpCommand) handleRecursiveDownload(fs vfs.Fs, dirPath, virtualPath string, stat os.FileInfo) error {
var err error
if c.isRecursive() {
c.connection.Log(logger.LevelDebug, "recursive download, dir path: %#v", dirPath)
err = c.sendDownloadProtocolMessages(dirPath, stat)
c.connection.Log(logger.LevelDebug, "recursive download, dir path %#v virtual path %#v", dirPath, virtualPath)
err = c.sendDownloadProtocolMessages(virtualPath, stat)
if err != nil {
return err
}
files, err := c.connection.Fs.ReadDir(dirPath)
files = c.connection.User.AddVirtualDirs(files, c.connection.Fs.GetRelativePath(dirPath))
files, err := fs.ReadDir(dirPath)
if err != nil {
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
files = c.connection.User.AddVirtualDirs(files, fs.GetRelativePath(dirPath))
var dirs []string
for _, file := range files {
filePath := c.connection.Fs.GetRelativePath(c.connection.Fs.Join(dirPath, file.Name()))
filePath := fs.GetRelativePath(fs.Join(dirPath, file.Name()))
if file.Mode().IsRegular() || file.Mode()&os.ModeSymlink != 0 {
err = c.handleDownload(filePath)
if err != nil {
@@ -361,7 +378,7 @@ func (c *scpCommand) handleRecursiveDownload(dirPath string, stat os.FileInfo) e
}
}
if err != nil {
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
for _, dir := range dirs {
@@ -371,25 +388,21 @@ func (c *scpCommand) handleRecursiveDownload(dirPath string, stat os.FileInfo) e
}
}
if err != nil {
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
err = c.sendProtocolMessage("E\n")
if err != nil {
return err
}
err = c.readConfirmationMessage()
if err != nil {
return err
}
return err
return c.readConfirmationMessage()
}
err = fmt.Errorf("Unable to send directory for non recursive copy")
c.sendErrorMessage(err)
err = fmt.Errorf("unable to send directory for non recursive copy")
c.sendErrorMessage(nil, err)
return err
}
func (c *scpCommand) sendDownloadFileData(filePath string, stat os.FileInfo, transfer *transfer) error {
func (c *scpCommand) sendDownloadFileData(fs vfs.Fs, filePath string, stat os.FileInfo, transfer *transfer) error {
var err error
if c.sendFileTime() {
modTime := stat.ModTime().UnixNano() / 1000000000
@@ -403,8 +416,8 @@ func (c *scpCommand) sendDownloadFileData(filePath string, stat os.FileInfo, tra
return err
}
}
if vfs.IsCryptOsFs(c.connection.Fs) {
stat = c.connection.Fs.(*vfs.CryptFs).ConvertFileInfo(stat)
if vfs.IsCryptOsFs(fs) {
stat = fs.(*vfs.CryptFs).ConvertFileInfo(stat)
}
fileSize := stat.Size()
@@ -435,7 +448,7 @@ func (c *scpCommand) sendDownloadFileData(filePath string, stat os.FileInfo, tra
}
}
if err != io.EOF {
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
err = c.sendConfirmationMessage()
@@ -450,55 +463,62 @@ func (c *scpCommand) handleDownload(filePath string) error {
c.connection.UpdateLastActivity()
var err error
p, err := c.connection.Fs.ResolvePath(filePath)
fs, err := c.connection.User.GetFilesystemForPath(filePath, c.connection.ID)
if err != nil {
err := fmt.Errorf("Invalid file path")
c.connection.Log(logger.LevelWarn, "error downloading file %#v: %+v", filePath, err)
c.sendErrorMessage(nil, fmt.Errorf("unable to get fs for path %#v", filePath))
return err
}
p, err := fs.ResolvePath(filePath)
if err != nil {
err := fmt.Errorf("invalid file path %#v", filePath)
c.connection.Log(logger.LevelWarn, "error downloading file: %#v, invalid file path", filePath)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
var stat os.FileInfo
if stat, err = c.connection.Fs.Stat(p); err != nil {
if stat, err = fs.Stat(p); err != nil {
c.connection.Log(logger.LevelWarn, "error downloading file: %#v->%#v, err: %v", filePath, p, err)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
if stat.IsDir() {
if !c.connection.User.HasPerm(dataprovider.PermDownload, filePath) {
c.connection.Log(logger.LevelWarn, "error downloading dir: %#v, permission denied", filePath)
c.sendErrorMessage(common.ErrPermissionDenied)
c.sendErrorMessage(fs, common.ErrPermissionDenied)
return common.ErrPermissionDenied
}
err = c.handleRecursiveDownload(p, stat)
err = c.handleRecursiveDownload(fs, p, filePath, stat)
return err
}
if !c.connection.User.HasPerm(dataprovider.PermDownload, path.Dir(filePath)) {
c.connection.Log(logger.LevelWarn, "error downloading dir: %#v, permission denied", filePath)
c.sendErrorMessage(common.ErrPermissionDenied)
c.sendErrorMessage(fs, common.ErrPermissionDenied)
return common.ErrPermissionDenied
}
if !c.connection.User.IsFileAllowed(filePath) {
c.connection.Log(logger.LevelWarn, "reading file %#v is not allowed", filePath)
c.sendErrorMessage(common.ErrPermissionDenied)
c.sendErrorMessage(fs, common.ErrPermissionDenied)
return common.ErrPermissionDenied
}
file, r, cancelFn, err := c.connection.Fs.Open(p, 0)
file, r, cancelFn, err := fs.Open(p, 0)
if err != nil {
c.connection.Log(logger.LevelError, "could not open file %#v for reading: %v", p, err)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
baseTransfer := common.NewBaseTransfer(file, c.connection.BaseConnection, cancelFn, p, filePath,
common.TransferDownload, 0, 0, 0, false, c.connection.Fs)
common.TransferDownload, 0, 0, 0, false, fs)
t := newTransfer(baseTransfer, nil, r, nil)
err = c.sendDownloadFileData(p, stat, t)
err = c.sendDownloadFileData(fs, p, stat, t)
// we need to call Close anyway and return close error if any and
// if we have no previous error
if err == nil {
@@ -578,9 +598,13 @@ func (c *scpCommand) readProtocolMessage() (string, error) {
// send an error message and close the channel
//nolint:errcheck // we don't check write errors here, we have to close the channel anyway
func (c *scpCommand) sendErrorMessage(err error) {
func (c *scpCommand) sendErrorMessage(fs vfs.Fs, err error) {
c.connection.channel.Write(errMsg)
c.connection.channel.Write([]byte(c.connection.GetFsError(err).Error()))
if fs != nil {
c.connection.channel.Write([]byte(c.connection.GetFsError(fs, err).Error()))
} else {
c.connection.channel.Write([]byte(err.Error()))
}
c.connection.channel.Write(newLine)
c.connection.channel.Close()
}
@@ -625,22 +649,14 @@ func (c *scpCommand) getNextUploadProtocolMessage() (string, error) {
return command, err
}
func (c *scpCommand) createDir(dirPath string) error {
var err error
var isDir bool
isDir, err = vfs.IsDirectory(c.connection.Fs, dirPath)
if err == nil && isDir {
// if this is a virtual dir the resolved path will exist, we don't need a specific check
// TODO: remember to check if it's okay when we'll add virtual folders support to cloud backends
c.connection.Log(logger.LevelDebug, "directory %#v already exists", dirPath)
return nil
}
if err = c.connection.Fs.Mkdir(dirPath); err != nil {
func (c *scpCommand) createDir(fs vfs.Fs, dirPath string) error {
err := fs.Mkdir(dirPath)
if err != nil {
c.connection.Log(logger.LevelError, "error creating dir %#v: %v", dirPath, err)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return err
}
vfs.SetPathPermissions(c.connection.Fs, dirPath, c.connection.User.GetUID(), c.connection.User.GetGID())
vfs.SetPathPermissions(fs, dirPath, c.connection.User.GetUID(), c.connection.User.GetGID())
return err
}
@@ -649,7 +665,7 @@ func (c *scpCommand) createDir(dirPath string) error {
// or:
// C0644 6 testfile
// and returns file size and file/directory name
func (c *scpCommand) parseUploadMessage(command string) (int64, string, error) {
func (c *scpCommand) parseUploadMessage(fs vfs.Fs, command string) (int64, string, error) {
var size int64
var name string
var err error
@@ -657,7 +673,7 @@ func (c *scpCommand) parseUploadMessage(command string) (int64, string, error) {
err = fmt.Errorf("unknown or invalid upload message: %v args: %v user: %v",
command, c.args, c.connection.User.Username)
c.connection.Log(logger.LevelWarn, "error: %v", err)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return size, name, err
}
parts := strings.SplitN(command, " ", 3)
@@ -665,26 +681,26 @@ func (c *scpCommand) parseUploadMessage(command string) (int64, string, error) {
size, err = strconv.ParseInt(parts[1], 10, 64)
if err != nil {
c.connection.Log(logger.LevelWarn, "error getting size from upload message: %v", err)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return size, name, err
}
name = parts[2]
if name == "" {
err = fmt.Errorf("error getting name from upload message, cannot be empty")
c.connection.Log(logger.LevelWarn, "error: %v", err)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return size, name, err
}
} else {
err = fmt.Errorf("unable to split upload message: %#v", command)
c.connection.Log(logger.LevelWarn, "error: %v", err)
c.sendErrorMessage(err)
c.sendErrorMessage(fs, err)
return size, name, err
}
return size, name, err
}
func (c *scpCommand) getFileUploadDestPath(scpDestPath, fileName string) string {
func (c *scpCommand) getFileUploadDestPath(fs vfs.Fs, scpDestPath, fileName string) string {
if !c.isRecursive() {
// if the upload is not recursive and the destination path does not end with "/"
// then scpDestPath is the wanted filename, for example:
@@ -695,8 +711,8 @@ func (c *scpCommand) getFileUploadDestPath(scpDestPath, fileName string) string
// but if scpDestPath is an existing directory then we put the uploaded file
// inside that directory this is as scp command works, for example:
// scp fileName.txt user@127.0.0.1:/existing_dir
if p, err := c.connection.Fs.ResolvePath(scpDestPath); err == nil {
if stat, err := c.connection.Fs.Stat(p); err == nil {
if p, err := fs.ResolvePath(scpDestPath); err == nil {
if stat, err := fs.Stat(p); err == nil {
if stat.IsDir() {
return path.Join(scpDestPath, fileName)
}