2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2020-08-11 21:56:10 +00:00
|
|
|
"errors"
|
2020-07-24 21:39:38 +00:00
|
|
|
"fmt"
|
2022-12-27 17:51:53 +00:00
|
|
|
"io"
|
2024-05-10 13:09:00 +00:00
|
|
|
"io/fs"
|
2020-07-24 21:39:38 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2024-07-24 16:17:55 +00:00
|
|
|
"slices"
|
2020-07-24 21:39:38 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2021-06-28 17:40:04 +00:00
|
|
|
ftpserver "github.com/fclairamb/ftpserverlib"
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/pkg/sftp"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2020-07-24 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BaseConnection defines common fields for a connection using any supported protocol
|
|
|
|
type BaseConnection struct {
|
2020-12-14 13:52:36 +00:00
|
|
|
// last activity for this connection.
|
2021-09-04 10:11:04 +00:00
|
|
|
// Since this field is accessed atomically we put it as first element of the struct to achieve 64 bit alignment
|
2022-08-30 13:47:41 +00:00
|
|
|
lastActivity atomic.Int64
|
2022-08-21 17:01:08 +00:00
|
|
|
uploadDone atomic.Bool
|
|
|
|
downloadDone atomic.Bool
|
2021-09-04 10:11:04 +00:00
|
|
|
// unique ID for a transfer.
|
|
|
|
// This field is accessed atomically so we put it at the beginning of the struct to achieve 64 bit alignment
|
2022-08-30 13:47:41 +00:00
|
|
|
transferID atomic.Int64
|
2020-07-24 21:39:38 +00:00
|
|
|
// Unique identifier for the connection
|
|
|
|
ID string
|
|
|
|
// user associated with this connection if any
|
|
|
|
User dataprovider.User
|
|
|
|
// start time for this connection
|
2021-06-01 20:28:43 +00:00
|
|
|
startTime time.Time
|
|
|
|
protocol string
|
|
|
|
remoteAddr string
|
2021-07-24 18:11:17 +00:00
|
|
|
localAddr string
|
2020-07-24 21:39:38 +00:00
|
|
|
sync.RWMutex
|
|
|
|
activeTransfers []ActiveTransfer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBaseConnection returns a new BaseConnection
|
2021-07-24 18:11:17 +00:00
|
|
|
func NewBaseConnection(id, protocol, localAddr, remoteAddr string, user dataprovider.User) *BaseConnection {
|
2021-02-16 18:11:36 +00:00
|
|
|
connID := id
|
2024-07-24 16:27:13 +00:00
|
|
|
if slices.Contains(supportedProtocols, protocol) {
|
2022-03-30 08:59:25 +00:00
|
|
|
connID = fmt.Sprintf("%s_%s", protocol, id)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-12-10 17:43:26 +00:00
|
|
|
user.UploadBandwidth, user.DownloadBandwidth = user.GetBandwidthForIP(util.GetIPFromRemoteAddress(remoteAddr), connID)
|
2022-08-30 13:47:41 +00:00
|
|
|
c := &BaseConnection{
|
|
|
|
ID: connID,
|
|
|
|
User: user,
|
|
|
|
startTime: time.Now(),
|
|
|
|
protocol: protocol,
|
|
|
|
localAddr: localAddr,
|
|
|
|
remoteAddr: remoteAddr,
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
c.transferID.Store(0)
|
|
|
|
c.lastActivity.Store(time.Now().UnixNano())
|
|
|
|
|
|
|
|
return c
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log outputs a log entry to the configured logger
|
2022-05-19 17:49:51 +00:00
|
|
|
func (c *BaseConnection) Log(level logger.LogLevel, format string, v ...any) {
|
2020-07-24 21:39:38 +00:00
|
|
|
logger.Log(level, c.protocol, c.ID, format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransferID returns an unique transfer ID for this connection
|
2022-01-20 17:19:20 +00:00
|
|
|
func (c *BaseConnection) GetTransferID() int64 {
|
2022-08-30 13:47:41 +00:00
|
|
|
return c.transferID.Add(1)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetID returns the connection ID
|
|
|
|
func (c *BaseConnection) GetID() string {
|
|
|
|
return c.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsername returns the authenticated username associated with this connection if any
|
|
|
|
func (c *BaseConnection) GetUsername() string {
|
|
|
|
return c.User.Username
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
// GetRole returns the role for the user associated with this connection
|
|
|
|
func (c *BaseConnection) GetRole() string {
|
|
|
|
return c.User.Role
|
|
|
|
}
|
|
|
|
|
2022-04-14 17:07:41 +00:00
|
|
|
// GetMaxSessions returns the maximum number of concurrent sessions allowed
|
|
|
|
func (c *BaseConnection) GetMaxSessions() int {
|
|
|
|
return c.User.MaxSessions
|
|
|
|
}
|
|
|
|
|
2024-03-17 10:30:03 +00:00
|
|
|
// isAccessAllowed returns true if the user's access conditions are met
|
|
|
|
func (c *BaseConnection) isAccessAllowed() bool {
|
|
|
|
if err := c.User.CheckLoginConditions(); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// GetProtocol returns the protocol for the connection
|
|
|
|
func (c *BaseConnection) GetProtocol() string {
|
|
|
|
return c.protocol
|
|
|
|
}
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
// GetRemoteIP returns the remote ip address
|
|
|
|
func (c *BaseConnection) GetRemoteIP() string {
|
|
|
|
return util.GetIPFromRemoteAddress(c.remoteAddr)
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// SetProtocol sets the protocol for this connection
|
|
|
|
func (c *BaseConnection) SetProtocol(protocol string) {
|
|
|
|
c.protocol = protocol
|
2024-07-24 16:27:13 +00:00
|
|
|
if slices.Contains(supportedProtocols, c.protocol) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.ID = fmt.Sprintf("%v_%v", c.protocol, c.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConnectionTime returns the initial connection time
|
|
|
|
func (c *BaseConnection) GetConnectionTime() time.Time {
|
|
|
|
return c.startTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateLastActivity updates last activity for this connection
|
|
|
|
func (c *BaseConnection) UpdateLastActivity() {
|
2022-08-30 13:47:41 +00:00
|
|
|
c.lastActivity.Store(time.Now().UnixNano())
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLastActivity returns the last connection activity
|
|
|
|
func (c *BaseConnection) GetLastActivity() time.Time {
|
2022-08-30 13:47:41 +00:00
|
|
|
return time.Unix(0, c.lastActivity.Load())
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
// CloseFS closes the underlying fs
|
|
|
|
func (c *BaseConnection) CloseFS() error {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.User.CloseFs()
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// AddTransfer associates a new transfer to this connection
|
|
|
|
func (c *BaseConnection) AddTransfer(t ActiveTransfer) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
c.activeTransfers = append(c.activeTransfers, t)
|
|
|
|
c.Log(logger.LevelDebug, "transfer added, id: %v, active transfers: %v", t.GetID(), len(c.activeTransfers))
|
2022-01-30 10:42:36 +00:00
|
|
|
if t.HasSizeLimit() {
|
2022-01-20 17:19:20 +00:00
|
|
|
folderName := ""
|
|
|
|
if t.GetType() == TransferUpload {
|
|
|
|
vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(t.GetVirtualPath()))
|
|
|
|
if err == nil {
|
|
|
|
if !vfolder.IsIncludedInUserQuota() {
|
|
|
|
folderName = vfolder.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go transfersChecker.AddTransfer(dataprovider.ActiveTransfer{
|
|
|
|
ID: t.GetID(),
|
|
|
|
Type: t.GetType(),
|
|
|
|
ConnID: c.ID,
|
|
|
|
Username: c.GetUsername(),
|
|
|
|
FolderName: folderName,
|
2022-01-30 10:42:36 +00:00
|
|
|
IP: c.GetRemoteIP(),
|
2022-01-20 17:19:20 +00:00
|
|
|
TruncatedSize: t.GetTruncatedSize(),
|
|
|
|
CreatedAt: util.GetTimeAsMsSinceEpoch(time.Now()),
|
|
|
|
UpdatedAt: util.GetTimeAsMsSinceEpoch(time.Now()),
|
|
|
|
})
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTransfer removes the specified transfer from the active ones
|
|
|
|
func (c *BaseConnection) RemoveTransfer(t ActiveTransfer) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
if t.HasSizeLimit() {
|
2022-01-20 17:19:20 +00:00
|
|
|
go transfersChecker.RemoveTransfer(t.GetID(), c.ID)
|
|
|
|
}
|
|
|
|
|
2022-01-16 08:50:23 +00:00
|
|
|
for idx, transfer := range c.activeTransfers {
|
|
|
|
if transfer.GetID() == t.GetID() {
|
|
|
|
lastIdx := len(c.activeTransfers) - 1
|
|
|
|
c.activeTransfers[idx] = c.activeTransfers[lastIdx]
|
|
|
|
c.activeTransfers[lastIdx] = nil
|
|
|
|
c.activeTransfers = c.activeTransfers[:lastIdx]
|
|
|
|
c.Log(logger.LevelDebug, "transfer removed, id: %v active transfers: %v", t.GetID(), len(c.activeTransfers))
|
|
|
|
return
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-16 08:50:23 +00:00
|
|
|
c.Log(logger.LevelWarn, "transfer to remove with id %v not found!", t.GetID())
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
// SignalTransferClose makes the transfer fail on the next read/write with the
|
|
|
|
// specified error
|
|
|
|
func (c *BaseConnection) SignalTransferClose(transferID int64, err error) {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
for _, t := range c.activeTransfers {
|
|
|
|
if t.GetID() == transferID {
|
|
|
|
c.Log(logger.LevelInfo, "signal transfer close for transfer id %v", transferID)
|
|
|
|
t.SignalClose(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// GetTransfers returns the active transfers
|
|
|
|
func (c *BaseConnection) GetTransfers() []ConnectionTransfer {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
transfers := make([]ConnectionTransfer, 0, len(c.activeTransfers))
|
|
|
|
for _, t := range c.activeTransfers {
|
|
|
|
var operationType string
|
2020-08-11 21:56:10 +00:00
|
|
|
switch t.GetType() {
|
|
|
|
case TransferDownload:
|
2020-07-24 21:39:38 +00:00
|
|
|
operationType = operationDownload
|
2020-08-11 21:56:10 +00:00
|
|
|
case TransferUpload:
|
|
|
|
operationType = operationUpload
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
transfers = append(transfers, ConnectionTransfer{
|
2022-01-30 10:42:36 +00:00
|
|
|
ID: t.GetID(),
|
|
|
|
OperationType: operationType,
|
|
|
|
StartTime: util.GetTimeAsMsSinceEpoch(t.GetStartTime()),
|
|
|
|
Size: t.GetSize(),
|
|
|
|
VirtualPath: t.GetVirtualPath(),
|
|
|
|
HasSizeLimit: t.HasSizeLimit(),
|
|
|
|
ULSize: t.GetUploadedSize(),
|
|
|
|
DLSize: t.GetDownloadedSize(),
|
2020-07-24 21:39:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return transfers
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// SignalTransfersAbort signals to the active transfers to exit as soon as possible
|
|
|
|
func (c *BaseConnection) SignalTransfersAbort() error {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
if len(c.activeTransfers) == 0 {
|
|
|
|
return errors.New("no active transfer found")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range c.activeTransfers {
|
2022-01-20 17:19:20 +00:00
|
|
|
t.SignalClose(ErrTransferAborted)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-22 12:52:17 +00:00
|
|
|
func (c *BaseConnection) getRealFsPath(fsPath string) string {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
for _, t := range c.activeTransfers {
|
2022-08-10 16:41:59 +00:00
|
|
|
if p := t.GetRealFsPath(fsPath); p != "" {
|
2020-08-22 12:52:17 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fsPath
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:18:36 +00:00
|
|
|
func (c *BaseConnection) setTimes(fsPath string, atime time.Time, mtime time.Time) bool {
|
2021-11-26 18:00:44 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
for _, t := range c.activeTransfers {
|
|
|
|
if t.SetTimes(fsPath, atime, mtime) {
|
2021-12-16 17:18:36 +00:00
|
|
|
return true
|
2021-11-26 18:00:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-16 17:18:36 +00:00
|
|
|
return false
|
2021-11-26 18:00:44 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 08:12:00 +00:00
|
|
|
func (c *BaseConnection) truncateOpenHandle(fsPath string, size int64) (int64, error) {
|
2020-08-20 11:54:36 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
for _, t := range c.activeTransfers {
|
2020-08-22 08:12:00 +00:00
|
|
|
initialSize, err := t.Truncate(fsPath, size)
|
2020-08-20 11:54:36 +00:00
|
|
|
if err != errTransferMismatch {
|
2020-08-22 08:12:00 +00:00
|
|
|
return initialSize, err
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 08:12:00 +00:00
|
|
|
return 0, errNoTransfer
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
// ListDir reads the directory matching virtualPath and returns a list of directory entries
|
2024-02-15 19:53:56 +00:00
|
|
|
func (c *BaseConnection) ListDir(virtualPath string) (*DirListerAt, error) {
|
2020-07-24 21:39:38 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermListItems, virtualPath) {
|
|
|
|
return nil, c.GetPermissionDeniedError()
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
lister, err := fs.ReadDir(fsPath)
|
2020-07-24 21:39:38 +00:00
|
|
|
if err != nil {
|
2021-11-06 13:13:20 +00:00
|
|
|
c.Log(logger.LevelDebug, "error listing directory: %+v", err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, c.GetFsError(fs, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
return &DirListerAt{
|
|
|
|
virtualPath: virtualPath,
|
2024-10-16 17:04:09 +00:00
|
|
|
conn: c,
|
|
|
|
fs: fs,
|
2024-02-15 19:53:56 +00:00
|
|
|
info: c.User.GetVirtualFoldersInfo(virtualPath),
|
|
|
|
lister: lister,
|
|
|
|
}, nil
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 11:14:53 +00:00
|
|
|
// CheckParentDirs tries to create the specified directory and any missing parent dirs
|
|
|
|
func (c *BaseConnection) CheckParentDirs(virtualPath string) error {
|
2022-11-01 11:22:54 +00:00
|
|
|
fs, err := c.User.GetFilesystemForPath(virtualPath, c.GetID())
|
2021-12-19 11:14:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fs.HasVirtualFolders() {
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
if _, err := c.DoStat(virtualPath, 0, false); !c.IsNotExistError(err) {
|
2021-12-19 11:14:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
dirs := util.GetDirsForVirtualPath(virtualPath)
|
|
|
|
for idx := len(dirs) - 1; idx >= 0; idx-- {
|
2022-11-01 11:22:54 +00:00
|
|
|
fs, err = c.User.GetFilesystemForPath(dirs[idx], c.GetID())
|
2021-12-19 11:14:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fs.HasVirtualFolders() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err = c.createDirIfMissing(dirs[idx]); err != nil {
|
2022-12-27 17:51:53 +00:00
|
|
|
return fmt.Errorf("unable to check/create missing parent dir %q for virtual path %q: %w",
|
2021-12-19 11:14:53 +00:00
|
|
|
dirs[idx], virtualPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-06 16:22:09 +00:00
|
|
|
// GetCreateChecks returns the checks for creating new files
|
2023-10-22 14:09:30 +00:00
|
|
|
func (c *BaseConnection) GetCreateChecks(virtualPath string, isNewFile bool, isResume bool) int {
|
|
|
|
result := 0
|
2023-04-06 16:22:09 +00:00
|
|
|
if !isNewFile {
|
2023-10-22 14:09:30 +00:00
|
|
|
if isResume {
|
|
|
|
result += vfs.CheckResume
|
|
|
|
}
|
|
|
|
return result
|
2023-04-06 16:22:09 +00:00
|
|
|
}
|
|
|
|
if !c.User.HasPerm(dataprovider.PermCreateDirs, path.Dir(virtualPath)) {
|
2023-10-22 14:09:30 +00:00
|
|
|
result += vfs.CheckParentDir
|
|
|
|
return result
|
2023-04-06 16:22:09 +00:00
|
|
|
}
|
2023-10-22 14:09:30 +00:00
|
|
|
return result
|
2023-04-06 16:22:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// CreateDir creates a new directory at the specified fsPath
|
2022-01-15 16:16:49 +00:00
|
|
|
func (c *BaseConnection) CreateDir(virtualPath string, checkFilePatterns bool) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermCreateDirs, path.Dir(virtualPath)) {
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
if checkFilePatterns {
|
|
|
|
if ok, _ := c.User.IsFileAllowed(virtualPath); !ok {
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
if c.User.IsVirtualFolder(virtualPath) {
|
2022-12-27 17:51:53 +00:00
|
|
|
c.Log(logger.LevelWarn, "mkdir not allowed %q is a virtual folder", virtualPath)
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
startTime := time.Now()
|
2021-03-21 18:15:47 +00:00
|
|
|
if err := fs.Mkdir(fsPath); err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "error creating dir: %q error: %+v", fsPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fs, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
vfs.SetPathPermissions(fs, fsPath, c.User.GetUID(), c.User.GetGID())
|
2023-02-12 17:56:53 +00:00
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-07-24 18:11:17 +00:00
|
|
|
logger.CommandLog(mkdirLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "", -1,
|
2023-02-12 17:56:53 +00:00
|
|
|
c.localAddr, c.remoteAddr, elapsed)
|
2023-08-12 16:51:47 +00:00
|
|
|
ExecuteActionNotification(c, operationMkdir, fsPath, virtualPath, "", "", "", 0, nil, elapsed, nil) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// IsRemoveFileAllowed returns an error if removing this file is not allowed
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) IsRemoveFileAllowed(virtualPath string) error {
|
2021-11-14 15:23:33 +00:00
|
|
|
if !c.User.HasAnyPerm([]string{dataprovider.PermDeleteFiles, dataprovider.PermDelete}, path.Dir(virtualPath)) {
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
if ok, policy := c.User.IsFileAllowed(virtualPath); !ok {
|
2022-12-27 17:51:53 +00:00
|
|
|
c.Log(logger.LevelDebug, "removing file %q is not allowed", virtualPath)
|
2022-01-15 16:16:49 +00:00
|
|
|
return c.GetErrorForDeniedFile(policy)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveFile removes a file at the specified fsPath
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) RemoveFile(fs vfs.Fs, fsPath, virtualPath string, info os.FileInfo) error {
|
|
|
|
if err := c.IsRemoveFileAllowed(virtualPath); err != nil {
|
2020-08-11 21:56:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
size := info.Size()
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err := ExecutePreAction(c, operationPreDelete, fsPath, virtualPath, size, 0)
|
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelDebug, "delete for file %q denied by pre action: %v", virtualPath, err)
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
updateQuota := true
|
2023-02-12 17:56:53 +00:00
|
|
|
startTime := time.Now()
|
2023-01-02 14:59:00 +00:00
|
|
|
if err := fs.Remove(fsPath, false); err != nil {
|
|
|
|
if status > 0 && fs.IsNotExist(err) {
|
|
|
|
// file removed in the pre-action, if the file was deleted from the EventManager the quota is already updated
|
|
|
|
c.Log(logger.LevelDebug, "file deleted from the hook, status: %d", status)
|
|
|
|
updateQuota = (status == 1)
|
|
|
|
} else {
|
2022-12-27 17:51:53 +00:00
|
|
|
c.Log(logger.LevelError, "failed to remove file/symlink %q: %+v", fsPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fs, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-07-24 18:11:17 +00:00
|
|
|
logger.CommandLog(removeLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "", -1,
|
2023-02-12 17:56:53 +00:00
|
|
|
c.localAddr, c.remoteAddr, elapsed)
|
2023-01-02 14:59:00 +00:00
|
|
|
if updateQuota && info.Mode()&os.ModeSymlink == 0 {
|
2020-07-24 21:39:38 +00:00
|
|
|
vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(virtualPath))
|
|
|
|
if err == nil {
|
2024-07-13 17:30:40 +00:00
|
|
|
dataprovider.UpdateUserFolderQuota(&vfolder, &c.User, -1, -size, false)
|
2020-07-24 21:39:38 +00:00
|
|
|
} else {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.User, -1, -size, false) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-12 16:51:47 +00:00
|
|
|
ExecuteActionNotification(c, operationDelete, fsPath, virtualPath, "", "", "", size, nil, elapsed, nil) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// IsRemoveDirAllowed returns an error if removing this directory is not allowed
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) IsRemoveDirAllowed(fs vfs.Fs, fsPath, virtualPath string) error {
|
2022-12-27 17:51:53 +00:00
|
|
|
if virtualPath == "/" || fs.GetRelativePath(fsPath) == "/" {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelWarn, "removing root dir is not allowed")
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
if c.User.IsVirtualFolder(virtualPath) {
|
2022-12-27 17:51:53 +00:00
|
|
|
c.Log(logger.LevelWarn, "removing a virtual folder is not allowed: %q", virtualPath)
|
|
|
|
return fmt.Errorf("removing virtual folders is not allowed: %w", c.GetPermissionDeniedError())
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
if c.User.HasVirtualFoldersInside(virtualPath) {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelWarn, "removing a directory with a virtual folder inside is not allowed: %q", virtualPath)
|
2022-12-27 17:51:53 +00:00
|
|
|
return fmt.Errorf("cannot remove directory %q with virtual folders inside: %w", virtualPath, c.GetOpUnsupportedError())
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
if c.User.IsMappedPath(fsPath) {
|
2022-12-27 17:51:53 +00:00
|
|
|
c.Log(logger.LevelWarn, "removing a directory mapped as virtual folder is not allowed: %q", fsPath)
|
|
|
|
return fmt.Errorf("removing the directory %q mapped as virtual folder is not allowed: %w",
|
|
|
|
virtualPath, c.GetPermissionDeniedError())
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-11-14 15:23:33 +00:00
|
|
|
if !c.User.HasAnyPerm([]string{dataprovider.PermDeleteDirs, dataprovider.PermDelete}, path.Dir(virtualPath)) {
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
if ok, policy := c.User.IsFileAllowed(virtualPath); !ok {
|
2022-12-27 17:51:53 +00:00
|
|
|
c.Log(logger.LevelDebug, "removing directory %q is not allowed", virtualPath)
|
2022-01-15 16:16:49 +00:00
|
|
|
return c.GetErrorForDeniedFile(policy)
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveDir removes a directory at the specified fsPath
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) RemoveDir(virtualPath string) error {
|
|
|
|
fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.IsRemoveDirAllowed(fs, fsPath, virtualPath); err != nil {
|
2020-08-11 21:56:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
var fi os.FileInfo
|
2021-03-21 18:15:47 +00:00
|
|
|
if fi, err = fs.Lstat(fsPath); err != nil {
|
2020-07-31 17:24:57 +00:00
|
|
|
// see #149
|
2021-03-21 18:15:47 +00:00
|
|
|
if fs.IsNotExist(err) && fs.HasVirtualFolders() {
|
2020-07-31 17:24:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelError, "failed to remove a dir %q: stat error: %+v", fsPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fs, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2020-11-17 18:36:39 +00:00
|
|
|
if !fi.IsDir() || fi.Mode()&os.ModeSymlink != 0 {
|
2022-10-27 06:27:44 +00:00
|
|
|
c.Log(logger.LevelError, "cannot remove %q is not a directory", fsPath)
|
2020-09-19 08:14:30 +00:00
|
|
|
return c.GetGenericError(nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 17:56:53 +00:00
|
|
|
startTime := time.Now()
|
2021-03-21 18:15:47 +00:00
|
|
|
if err := fs.Remove(fsPath, true); err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "failed to remove directory %q: %+v", fsPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fs, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-07-24 18:11:17 +00:00
|
|
|
logger.CommandLog(rmdirLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "", "", -1,
|
2023-02-12 17:56:53 +00:00
|
|
|
c.localAddr, c.remoteAddr, elapsed)
|
2023-08-12 16:51:47 +00:00
|
|
|
ExecuteActionNotification(c, operationRmdir, fsPath, virtualPath, "", "", "", 0, nil, elapsed, nil) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
func (c *BaseConnection) doRecursiveRemoveDirEntry(virtualPath string, info os.FileInfo, recursion int) error {
|
2022-12-27 17:51:53 +00:00
|
|
|
fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
return c.doRecursiveRemove(fs, fsPath, virtualPath, info, recursion)
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
2022-10-27 06:27:44 +00:00
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
func (c *BaseConnection) doRecursiveRemove(fs vfs.Fs, fsPath, virtualPath string, info os.FileInfo, recursion int) error {
|
2022-12-27 17:51:53 +00:00
|
|
|
if info.IsDir() {
|
2024-02-15 19:53:56 +00:00
|
|
|
if recursion >= util.MaxRecursion {
|
|
|
|
c.Log(logger.LevelError, "recursive rename failed, recursion too depth: %d", recursion)
|
|
|
|
return util.ErrRecursionTooDeep
|
|
|
|
}
|
|
|
|
recursion++
|
|
|
|
lister, err := c.ListDir(virtualPath)
|
2022-12-27 17:51:53 +00:00
|
|
|
if err != nil {
|
2024-02-15 19:53:56 +00:00
|
|
|
return fmt.Errorf("unable to get lister for dir %q: %w", virtualPath, err)
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
defer lister.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
entries, err := lister.Next(vfs.ListerBatchSize)
|
|
|
|
finished := errors.Is(err, io.EOF)
|
|
|
|
if err != nil && !finished {
|
|
|
|
return fmt.Errorf("unable to get content for dir %q: %w", virtualPath, err)
|
|
|
|
}
|
|
|
|
for _, fi := range entries {
|
|
|
|
targetPath := path.Join(virtualPath, fi.Name())
|
|
|
|
if err := c.doRecursiveRemoveDirEntry(targetPath, fi, recursion); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if finished {
|
|
|
|
lister.Close()
|
|
|
|
break
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
return c.RemoveDir(virtualPath)
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
return c.RemoveFile(fs, fsPath, virtualPath, info)
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 17:51:53 +00:00
|
|
|
// RemoveAll removes the specified path and any children it contains
|
|
|
|
func (c *BaseConnection) RemoveAll(virtualPath string) error {
|
|
|
|
fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-27 06:27:44 +00:00
|
|
|
|
2022-12-27 17:51:53 +00:00
|
|
|
fi, err := fs.Lstat(fsPath)
|
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelDebug, "failed to remove path %q: stat error: %+v", fsPath, err)
|
|
|
|
return c.GetFsError(fs, err)
|
|
|
|
}
|
|
|
|
if fi.IsDir() && fi.Mode()&os.ModeSymlink == 0 {
|
|
|
|
if err := c.IsRemoveDirAllowed(fs, fsPath, virtualPath); err != nil {
|
2022-10-27 06:27:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
return c.doRecursiveRemove(fs, fsPath, virtualPath, fi, 0)
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
|
|
|
return c.RemoveFile(fs, fsPath, virtualPath, fi)
|
|
|
|
}
|
2022-10-27 06:27:44 +00:00
|
|
|
|
2022-12-30 18:30:16 +00:00
|
|
|
func (c *BaseConnection) checkCopy(srcInfo, dstInfo os.FileInfo, virtualSource, virtualTarget string) error {
|
|
|
|
_, fsSourcePath, err := c.GetFsAndResolvedPath(virtualSource)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, fsTargetPath, err := c.GetFsAndResolvedPath(virtualTarget)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
if srcInfo.IsDir() {
|
|
|
|
if dstInfo != nil && !dstInfo.IsDir() {
|
|
|
|
return fmt.Errorf("cannot overwrite file %q with dir %q: %w", virtualTarget, virtualSource, c.GetOpUnsupportedError())
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
if util.IsDirOverlapped(virtualSource, virtualTarget, true, "/") {
|
|
|
|
return fmt.Errorf("nested copy %q => %q is not supported: %w", virtualSource, virtualTarget, c.GetOpUnsupportedError())
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
if util.IsDirOverlapped(fsSourcePath, fsTargetPath, true, c.User.FsConfig.GetPathSeparator()) {
|
|
|
|
c.Log(logger.LevelWarn, "nested fs copy %q => %q not allowed", fsSourcePath, fsTargetPath)
|
|
|
|
return fmt.Errorf("nested fs copy is not supported: %w", c.GetOpUnsupportedError())
|
|
|
|
}
|
2022-10-27 06:27:44 +00:00
|
|
|
return nil
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
|
|
|
if dstInfo != nil && dstInfo.IsDir() {
|
|
|
|
return fmt.Errorf("cannot overwrite file %q with dir %q: %w", virtualSource, virtualTarget, c.GetOpUnsupportedError())
|
|
|
|
}
|
2022-12-30 18:30:16 +00:00
|
|
|
if fsSourcePath == fsTargetPath {
|
|
|
|
return fmt.Errorf("the copy source and target cannot be the same: %w", c.GetOpUnsupportedError())
|
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-08 16:44:26 +00:00
|
|
|
func (c *BaseConnection) copyFile(virtualSourcePath, virtualTargetPath string, srcInfo os.FileInfo) error {
|
2024-02-20 17:19:09 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermCopy, virtualSourcePath) || !c.User.HasPerm(dataprovider.PermCopy, virtualTargetPath) {
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
if ok, _ := c.User.IsFileAllowed(virtualTargetPath); !ok {
|
|
|
|
return fmt.Errorf("file %q is not allowed: %w", virtualTargetPath, c.GetPermissionDeniedError())
|
|
|
|
}
|
2023-04-15 12:16:26 +00:00
|
|
|
if c.IsSameResource(virtualSourcePath, virtualTargetPath) {
|
2023-01-07 15:28:46 +00:00
|
|
|
fs, fsTargetPath, err := c.GetFsAndResolvedPath(virtualTargetPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if copier, ok := fs.(vfs.FsFileCopier); ok {
|
|
|
|
_, fsSourcePath, err := c.GetFsAndResolvedPath(virtualSourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-12 07:43:23 +00:00
|
|
|
startTime := time.Now()
|
2024-08-08 16:44:26 +00:00
|
|
|
numFiles, sizeDiff, err := copier.CopyFile(fsSourcePath, fsTargetPath, srcInfo)
|
2024-03-12 07:43:23 +00:00
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
|
|
|
updateUserQuotaAfterFileWrite(c, virtualTargetPath, numFiles, sizeDiff)
|
|
|
|
logger.CommandLog(copyLogSender, fsSourcePath, fsTargetPath, c.User.Username, "", c.ID, c.protocol, -1, -1,
|
2024-08-08 16:44:26 +00:00
|
|
|
"", "", "", srcInfo.Size(), c.localAddr, c.remoteAddr, elapsed)
|
|
|
|
ExecuteActionNotification(c, operationCopy, fsSourcePath, virtualSourcePath, fsTargetPath, virtualTargetPath, "", srcInfo.Size(), err, elapsed, nil) //nolint:errcheck
|
2024-03-12 07:43:23 +00:00
|
|
|
return err
|
2023-01-07 15:28:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 17:51:53 +00:00
|
|
|
reader, rCancelFn, err := getFileReader(c, virtualSourcePath)
|
2022-10-27 06:27:44 +00:00
|
|
|
if err != nil {
|
2022-12-27 17:51:53 +00:00
|
|
|
return fmt.Errorf("unable to get reader for path %q: %w", virtualSourcePath, err)
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
defer rCancelFn()
|
|
|
|
defer reader.Close()
|
2022-10-27 06:27:44 +00:00
|
|
|
|
2024-08-08 16:44:26 +00:00
|
|
|
writer, numFiles, truncatedSize, wCancelFn, err := getFileWriter(c, virtualTargetPath, srcInfo.Size())
|
2022-12-27 17:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get writer for path %q: %w", virtualTargetPath, err)
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
defer wCancelFn()
|
|
|
|
|
2023-02-12 17:56:53 +00:00
|
|
|
startTime := time.Now()
|
2022-12-27 17:51:53 +00:00
|
|
|
_, err = io.Copy(writer, reader)
|
2023-02-12 17:56:53 +00:00
|
|
|
return closeWriterAndUpdateQuota(writer, c, virtualSourcePath, virtualTargetPath, numFiles, truncatedSize,
|
|
|
|
err, operationCopy, startTime)
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
2022-10-27 06:27:44 +00:00
|
|
|
|
2022-12-27 17:51:53 +00:00
|
|
|
func (c *BaseConnection) doRecursiveCopy(virtualSourcePath, virtualTargetPath string, srcInfo os.FileInfo,
|
2024-02-15 19:53:56 +00:00
|
|
|
createTargetDir bool, recursion int,
|
2022-12-27 17:51:53 +00:00
|
|
|
) error {
|
|
|
|
if srcInfo.IsDir() {
|
2024-02-15 19:53:56 +00:00
|
|
|
if recursion >= util.MaxRecursion {
|
|
|
|
c.Log(logger.LevelError, "recursive copy failed, recursion too depth: %d", recursion)
|
|
|
|
return util.ErrRecursionTooDeep
|
|
|
|
}
|
|
|
|
recursion++
|
2022-12-27 17:51:53 +00:00
|
|
|
if createTargetDir {
|
|
|
|
if err := c.CreateDir(virtualTargetPath, false); err != nil {
|
|
|
|
return fmt.Errorf("unable to create directory %q: %w", virtualTargetPath, err)
|
|
|
|
}
|
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
lister, err := c.ListDir(virtualSourcePath)
|
2022-10-27 06:27:44 +00:00
|
|
|
if err != nil {
|
2024-02-15 19:53:56 +00:00
|
|
|
return fmt.Errorf("unable to get lister for dir %q: %w", virtualSourcePath, err)
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
defer lister.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
entries, err := lister.Next(vfs.ListerBatchSize)
|
|
|
|
finished := errors.Is(err, io.EOF)
|
|
|
|
if err != nil && !finished {
|
|
|
|
return fmt.Errorf("unable to get contents for dir %q: %w", virtualSourcePath, err)
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
if err := c.recursiveCopyEntries(virtualSourcePath, virtualTargetPath, entries, recursion); err != nil {
|
2022-12-27 17:51:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-02-15 19:53:56 +00:00
|
|
|
if finished {
|
|
|
|
return nil
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
|
|
|
if !srcInfo.Mode().IsRegular() {
|
|
|
|
c.Log(logger.LevelInfo, "skipping copy for non regular file %q", virtualSourcePath)
|
|
|
|
return nil
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 16:44:26 +00:00
|
|
|
return c.copyFile(virtualSourcePath, virtualTargetPath, srcInfo)
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
func (c *BaseConnection) recursiveCopyEntries(virtualSourcePath, virtualTargetPath string, entries []os.FileInfo, recursion int) error {
|
|
|
|
for _, info := range entries {
|
|
|
|
sourcePath := path.Join(virtualSourcePath, info.Name())
|
|
|
|
targetPath := path.Join(virtualTargetPath, info.Name())
|
|
|
|
targetInfo, err := c.DoStat(targetPath, 1, false)
|
|
|
|
if err == nil {
|
|
|
|
if info.IsDir() && targetInfo.IsDir() {
|
|
|
|
c.Log(logger.LevelDebug, "target copy dir %q already exists", targetPath)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil && !c.IsNotExistError(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.checkCopy(info, targetInfo, sourcePath, targetPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.doRecursiveCopy(sourcePath, targetPath, info, true, recursion); err != nil {
|
|
|
|
if c.IsNotExistError(err) {
|
|
|
|
c.Log(logger.LevelInfo, "skipping copy for source path %q: %v", sourcePath, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-27 17:51:53 +00:00
|
|
|
// Copy virtualSourcePath to virtualTargetPath
|
|
|
|
func (c *BaseConnection) Copy(virtualSourcePath, virtualTargetPath string) error {
|
|
|
|
copyFromSource := strings.HasSuffix(virtualSourcePath, "/")
|
|
|
|
copyInTarget := strings.HasSuffix(virtualTargetPath, "/")
|
|
|
|
virtualSourcePath = path.Clean(virtualSourcePath)
|
|
|
|
virtualTargetPath = path.Clean(virtualTargetPath)
|
|
|
|
if virtualSourcePath == virtualTargetPath {
|
|
|
|
return fmt.Errorf("the copy source and target cannot be the same: %w", c.GetOpUnsupportedError())
|
|
|
|
}
|
|
|
|
srcInfo, err := c.DoStat(virtualSourcePath, 1, false)
|
2022-10-27 06:27:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
if srcInfo.Mode()&os.ModeSymlink != 0 {
|
|
|
|
return fmt.Errorf("copying symlinks is not supported: %w", c.GetOpUnsupportedError())
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
dstInfo, err := c.DoStat(virtualTargetPath, 1, false)
|
|
|
|
if err == nil && !copyFromSource {
|
|
|
|
copyInTarget = dstInfo.IsDir()
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
if err != nil && !c.IsNotExistError(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
destPath := virtualTargetPath
|
|
|
|
if copyInTarget {
|
|
|
|
destPath = path.Join(virtualTargetPath, path.Base(virtualSourcePath))
|
|
|
|
dstInfo, err = c.DoStat(destPath, 1, false)
|
|
|
|
if err != nil && !c.IsNotExistError(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createTargetDir := true
|
|
|
|
if dstInfo != nil && dstInfo.IsDir() {
|
|
|
|
createTargetDir = false
|
|
|
|
}
|
2022-12-30 18:30:16 +00:00
|
|
|
if err := c.checkCopy(srcInfo, dstInfo, virtualSourcePath, destPath); err != nil {
|
2022-12-27 17:51:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.CheckParentDirs(path.Dir(destPath)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
done := make(chan bool)
|
|
|
|
defer close(done)
|
|
|
|
go keepConnectionAlive(c, done, 2*time.Minute)
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
return c.doRecursiveCopy(virtualSourcePath, destPath, srcInfo, createTargetDir, 0)
|
2022-10-27 06:27:44 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
// Rename renames (moves) virtualSourcePath to virtualTargetPath
|
|
|
|
func (c *BaseConnection) Rename(virtualSourcePath, virtualTargetPath string) error {
|
2024-08-09 18:18:33 +00:00
|
|
|
return c.renameInternal(virtualSourcePath, virtualTargetPath, false, vfs.CheckParentDir)
|
2023-01-03 17:13:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 18:18:33 +00:00
|
|
|
func (c *BaseConnection) renameInternal(virtualSourcePath, virtualTargetPath string,
|
|
|
|
checkParentDestination bool, checks int,
|
|
|
|
) error {
|
2021-12-28 11:03:52 +00:00
|
|
|
if virtualSourcePath == virtualTargetPath {
|
|
|
|
return fmt.Errorf("the rename source and target cannot be the same: %w", c.GetOpUnsupportedError())
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fsSrc, fsSourcePath, err := c.GetFsAndResolvedPath(virtualSourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fsDst, fsTargetPath, err := c.GetFsAndResolvedPath(virtualTargetPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
startTime := time.Now()
|
2021-03-21 18:15:47 +00:00
|
|
|
srcInfo, err := fsSrc.Lstat(fsSourcePath)
|
2020-07-24 21:39:38 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fsSrc, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if !c.isRenamePermitted(fsSrc, fsDst, fsSourcePath, fsTargetPath, virtualSourcePath, virtualTargetPath, srcInfo) {
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
initialSize := int64(-1)
|
2021-03-21 18:15:47 +00:00
|
|
|
if dstInfo, err := fsDst.Lstat(fsTargetPath); err == nil {
|
2023-01-03 17:13:01 +00:00
|
|
|
checkParentDestination = false
|
2020-07-24 21:39:38 +00:00
|
|
|
if dstInfo.IsDir() {
|
2022-07-17 14:02:45 +00:00
|
|
|
c.Log(logger.LevelWarn, "attempted to rename %q overwriting an existing directory %q",
|
2020-07-24 21:39:38 +00:00
|
|
|
fsSourcePath, fsTargetPath)
|
|
|
|
return c.GetOpUnsupportedError()
|
|
|
|
}
|
|
|
|
// we are overwriting an existing file/symlink
|
|
|
|
if dstInfo.Mode().IsRegular() {
|
|
|
|
initialSize = dstInfo.Size()
|
|
|
|
}
|
|
|
|
if !c.User.HasPerm(dataprovider.PermOverwrite, path.Dir(virtualTargetPath)) {
|
2022-10-27 06:27:44 +00:00
|
|
|
c.Log(logger.LevelDebug, "renaming %q -> %q is not allowed. Target exists but the user %q"+
|
2021-03-21 18:15:47 +00:00
|
|
|
"has no overwrite permission", virtualSourcePath, virtualTargetPath, c.User.Username)
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if srcInfo.IsDir() {
|
2022-12-27 17:51:53 +00:00
|
|
|
if err := c.checkFolderRename(fsSrc, fsDst, fsSourcePath, fsTargetPath, virtualSourcePath, virtualTargetPath, srcInfo); err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return err
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if !c.hasSpaceForRename(fsSrc, virtualSourcePath, virtualTargetPath, initialSize, fsSourcePath) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c.Log(logger.LevelInfo, "denying cross rename due to space limit")
|
2020-09-19 08:14:30 +00:00
|
|
|
return c.GetGenericError(ErrQuotaExceeded)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2023-01-03 17:13:01 +00:00
|
|
|
if checkParentDestination {
|
|
|
|
c.CheckParentDirs(path.Dir(virtualTargetPath)) //nolint:errcheck
|
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
done := make(chan bool)
|
|
|
|
defer close(done)
|
|
|
|
go keepConnectionAlive(c, done, 2*time.Minute)
|
|
|
|
|
2024-08-09 18:18:33 +00:00
|
|
|
files, size, err := fsDst.Rename(fsSourcePath, fsTargetPath, checks)
|
2023-01-06 11:33:50 +00:00
|
|
|
if err != nil {
|
2022-12-27 17:51:53 +00:00
|
|
|
c.Log(logger.LevelError, "failed to rename %q -> %q: %+v", fsSourcePath, fsTargetPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fsSrc, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
vfs.SetPathPermissions(fsDst, fsTargetPath, c.User.GetUID(), c.User.GetGID())
|
2023-02-12 17:56:53 +00:00
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
2023-01-06 11:33:50 +00:00
|
|
|
c.updateQuotaAfterRename(fsDst, virtualSourcePath, virtualTargetPath, fsTargetPath, initialSize, files, size) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
logger.CommandLog(renameLogSender, fsSourcePath, fsTargetPath, c.User.Username, "", c.ID, c.protocol, -1, -1,
|
2023-02-12 17:56:53 +00:00
|
|
|
"", "", "", -1, c.localAddr, c.remoteAddr, elapsed)
|
2022-06-16 16:42:17 +00:00
|
|
|
ExecuteActionNotification(c, operationRename, fsSourcePath, virtualSourcePath, fsTargetPath, //nolint:errcheck
|
2023-08-12 16:51:47 +00:00
|
|
|
virtualTargetPath, "", 0, nil, elapsed, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSymlink creates fsTargetPath as a symbolic link to fsSourcePath
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) CreateSymlink(virtualSourcePath, virtualTargetPath string) error {
|
2022-09-30 17:23:54 +00:00
|
|
|
var relativePath string
|
|
|
|
if !path.IsAbs(virtualSourcePath) {
|
|
|
|
relativePath = virtualSourcePath
|
|
|
|
virtualSourcePath = path.Join(path.Dir(virtualTargetPath), relativePath)
|
|
|
|
c.Log(logger.LevelDebug, "link relative path %q resolved as %q, target path %q",
|
|
|
|
relativePath, virtualSourcePath, virtualTargetPath)
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
if c.isCrossFoldersRequest(virtualSourcePath, virtualTargetPath) {
|
|
|
|
c.Log(logger.LevelWarn, "cross folder symlink is not supported, src: %v dst: %v", virtualSourcePath, virtualTargetPath)
|
2020-09-19 08:14:30 +00:00
|
|
|
return c.GetOpUnsupportedError()
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
// we cannot have a cross folder request here so only one fs is enough
|
|
|
|
fs, fsSourcePath, err := c.GetFsAndResolvedPath(virtualSourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fsTargetPath, err := fs.ResolvePath(virtualTargetPath)
|
|
|
|
if err != nil {
|
|
|
|
return c.GetFsError(fs, err)
|
|
|
|
}
|
|
|
|
if fs.GetRelativePath(fsSourcePath) == "/" {
|
2022-01-15 16:16:49 +00:00
|
|
|
c.Log(logger.LevelError, "symlinking root dir is not allowed")
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if fs.GetRelativePath(fsTargetPath) == "/" {
|
2022-01-15 16:16:49 +00:00
|
|
|
c.Log(logger.LevelError, "symlinking to root dir is not allowed")
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermCreateSymlinks, path.Dir(virtualTargetPath)) {
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
ok, policy := c.User.IsFileAllowed(virtualSourcePath)
|
|
|
|
if !ok && policy == sdk.DenyPolicyHide {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "symlink source path %q is not allowed", virtualSourcePath)
|
2022-01-15 16:16:49 +00:00
|
|
|
return c.GetNotExistError()
|
|
|
|
}
|
|
|
|
if ok, _ = c.User.IsFileAllowed(virtualTargetPath); !ok {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "symlink target path %q is not allowed", virtualTargetPath)
|
2022-01-15 16:16:49 +00:00
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2022-09-30 17:23:54 +00:00
|
|
|
if relativePath != "" {
|
|
|
|
fsSourcePath = relativePath
|
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
startTime := time.Now()
|
2021-03-21 18:15:47 +00:00
|
|
|
if err := fs.Symlink(fsSourcePath, fsTargetPath); err != nil {
|
2023-02-12 17:56:53 +00:00
|
|
|
c.Log(logger.LevelError, "failed to create symlink %q -> %q: %+v", fsSourcePath, fsTargetPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fs, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
2021-06-01 20:28:43 +00:00
|
|
|
logger.CommandLog(symlinkLogSender, fsSourcePath, fsTargetPath, c.User.Username, "", c.ID, c.protocol, -1, -1, "",
|
2023-02-12 17:56:53 +00:00
|
|
|
"", "", -1, c.localAddr, c.remoteAddr, elapsed)
|
2020-07-24 21:39:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) getPathForSetStatPerms(fs vfs.Fs, fsPath, virtualPath string) string {
|
2020-07-24 21:39:38 +00:00
|
|
|
pathForPerms := virtualPath
|
2021-03-21 18:15:47 +00:00
|
|
|
if fi, err := fs.Lstat(fsPath); err == nil {
|
2020-07-24 21:39:38 +00:00
|
|
|
if fi.IsDir() {
|
|
|
|
pathForPerms = path.Dir(virtualPath)
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 11:54:36 +00:00
|
|
|
return pathForPerms
|
|
|
|
}
|
|
|
|
|
2022-10-10 16:53:58 +00:00
|
|
|
func (c *BaseConnection) doStatInternal(virtualPath string, mode int, checkFilePatterns,
|
|
|
|
convertResult bool,
|
|
|
|
) (os.FileInfo, error) {
|
2021-03-21 18:15:47 +00:00
|
|
|
// for some vfs we don't create intermediary folders so we cannot simply check
|
2024-02-15 19:53:56 +00:00
|
|
|
// if virtualPath is a virtual folder. Allowing stat for hidden virtual folders
|
|
|
|
// is by purpose.
|
2021-03-21 18:15:47 +00:00
|
|
|
vfolders := c.User.GetVirtualFoldersInPath(path.Dir(virtualPath))
|
|
|
|
if _, ok := vfolders[virtualPath]; ok {
|
2022-08-16 15:59:13 +00:00
|
|
|
return vfs.NewFileInfo(virtualPath, true, 0, time.Unix(0, 0), false), nil
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
2022-11-18 17:12:37 +00:00
|
|
|
if checkFilePatterns && virtualPath != "/" {
|
2022-01-15 16:16:49 +00:00
|
|
|
ok, policy := c.User.IsFileAllowed(virtualPath)
|
|
|
|
if !ok && policy == sdk.DenyPolicyHide {
|
|
|
|
return nil, c.GetNotExistError()
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
|
2020-12-05 12:48:13 +00:00
|
|
|
var info os.FileInfo
|
2021-03-21 18:15:47 +00:00
|
|
|
|
|
|
|
fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
|
|
|
|
if err != nil {
|
2023-01-07 15:28:46 +00:00
|
|
|
return nil, err
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 16:23:00 +00:00
|
|
|
if mode == 1 {
|
2021-03-21 18:15:47 +00:00
|
|
|
info, err = fs.Lstat(c.getRealFsPath(fsPath))
|
2020-12-05 12:48:13 +00:00
|
|
|
} else {
|
2021-03-21 18:15:47 +00:00
|
|
|
info, err = fs.Stat(c.getRealFsPath(fsPath))
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-12-27 17:51:53 +00:00
|
|
|
if !fs.IsNotExist(err) {
|
|
|
|
c.Log(logger.LevelWarn, "stat error for path %q: %+v", virtualPath, err)
|
|
|
|
}
|
2023-01-07 15:28:46 +00:00
|
|
|
return nil, c.GetFsError(fs, err)
|
2020-12-05 12:48:13 +00:00
|
|
|
}
|
2022-10-10 16:53:58 +00:00
|
|
|
if convertResult && vfs.IsCryptOsFs(fs) {
|
2021-03-21 18:15:47 +00:00
|
|
|
info = fs.(*vfs.CryptFs).ConvertFileInfo(info)
|
2020-08-25 16:23:00 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return info, nil
|
2020-08-25 16:23:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 16:53:58 +00:00
|
|
|
// DoStat execute a Stat if mode = 0, Lstat if mode = 1
|
|
|
|
func (c *BaseConnection) DoStat(virtualPath string, mode int, checkFilePatterns bool) (os.FileInfo, error) {
|
|
|
|
return c.doStatInternal(virtualPath, mode, checkFilePatterns, true)
|
|
|
|
}
|
|
|
|
|
2021-12-19 11:14:53 +00:00
|
|
|
func (c *BaseConnection) createDirIfMissing(name string) error {
|
2022-01-15 16:16:49 +00:00
|
|
|
_, err := c.DoStat(name, 0, false)
|
2021-12-19 11:14:53 +00:00
|
|
|
if c.IsNotExistError(err) {
|
2022-01-15 16:16:49 +00:00
|
|
|
return c.CreateDir(name, false)
|
2021-12-19 11:14:53 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) ignoreSetStat(fs vfs.Fs) bool {
|
2020-08-20 11:54:36 +00:00
|
|
|
if Config.SetstatMode == 1 {
|
2020-11-12 09:39:46 +00:00
|
|
|
return true
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if Config.SetstatMode == 2 && !vfs.IsLocalOrSFTPFs(fs) && !vfs.IsCryptOsFs(fs) {
|
2020-11-12 09:39:46 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) handleChmod(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
|
2020-11-13 17:40:18 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermChmod, pathForPerms) {
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if c.ignoreSetStat(fs) {
|
2020-11-13 17:40:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
startTime := time.Now()
|
2021-03-21 18:15:47 +00:00
|
|
|
if err := fs.Chmod(c.getRealFsPath(fsPath), attributes.Mode); err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "failed to chmod path %q, mode: %v, err: %+v", fsPath, attributes.Mode.String(), err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fs, err)
|
2020-11-13 17:40:18 +00:00
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
2020-11-13 17:40:18 +00:00
|
|
|
logger.CommandLog(chmodLogSender, fsPath, "", c.User.Username, attributes.Mode.String(), c.ID, c.protocol,
|
2023-02-12 17:56:53 +00:00
|
|
|
-1, -1, "", "", "", -1, c.localAddr, c.remoteAddr, elapsed)
|
2020-11-13 17:40:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) handleChown(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
|
2020-11-13 17:40:18 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermChown, pathForPerms) {
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
if c.ignoreSetStat(fs) {
|
2020-11-13 17:40:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
startTime := time.Now()
|
2021-03-21 18:15:47 +00:00
|
|
|
if err := fs.Chown(c.getRealFsPath(fsPath), attributes.UID, attributes.GID); err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "failed to chown path %q, uid: %v, gid: %v, err: %+v", fsPath, attributes.UID,
|
2020-11-13 17:40:18 +00:00
|
|
|
attributes.GID, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fs, err)
|
2020-11-13 17:40:18 +00:00
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
2020-11-13 17:40:18 +00:00
|
|
|
logger.CommandLog(chownLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, attributes.UID, attributes.GID,
|
2023-02-12 17:56:53 +00:00
|
|
|
"", "", "", -1, c.localAddr, c.remoteAddr, elapsed)
|
2020-11-13 17:40:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) handleChtimes(fs vfs.Fs, fsPath, pathForPerms string, attributes *StatAttributes) error {
|
2020-11-13 17:40:18 +00:00
|
|
|
if !c.User.HasPerm(dataprovider.PermChtimes, pathForPerms) {
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2021-12-16 17:18:36 +00:00
|
|
|
if Config.SetstatMode == 1 {
|
2020-11-13 17:40:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
startTime := time.Now()
|
2021-12-16 17:18:36 +00:00
|
|
|
isUploading := c.setTimes(fsPath, attributes.Atime, attributes.Mtime)
|
|
|
|
if err := fs.Chtimes(c.getRealFsPath(fsPath), attributes.Atime, attributes.Mtime, isUploading); err != nil {
|
|
|
|
c.setTimes(fsPath, time.Time{}, time.Time{})
|
|
|
|
if errors.Is(err, vfs.ErrVfsUnsupported) && Config.SetstatMode == 2 {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "failed to chtimes for path %q, access time: %v, modification time: %v, err: %+v",
|
2020-11-13 17:40:18 +00:00
|
|
|
fsPath, attributes.Atime, attributes.Mtime, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fs, err)
|
2020-11-13 17:40:18 +00:00
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
2020-11-13 17:40:18 +00:00
|
|
|
accessTimeString := attributes.Atime.Format(chtimesFormat)
|
|
|
|
modificationTimeString := attributes.Mtime.Format(chtimesFormat)
|
|
|
|
logger.CommandLog(chtimesLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1,
|
2023-02-12 17:56:53 +00:00
|
|
|
accessTimeString, modificationTimeString, "", -1, c.localAddr, c.remoteAddr, elapsed)
|
2020-11-13 17:40:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:39:46 +00:00
|
|
|
// SetStat set StatAttributes for the specified fsPath
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) SetStat(virtualPath string, attributes *StatAttributes) error {
|
2022-01-15 16:16:49 +00:00
|
|
|
if ok, policy := c.User.IsFileAllowed(virtualPath); !ok {
|
|
|
|
return c.GetErrorForDeniedFile(policy)
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, fsPath, err := c.GetFsAndResolvedPath(virtualPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pathForPerms := c.getPathForSetStatPerms(fs, fsPath, virtualPath)
|
2020-08-20 11:54:36 +00:00
|
|
|
|
2021-11-24 10:55:14 +00:00
|
|
|
if attributes.Flags&StatAttrTimes != 0 {
|
|
|
|
if err = c.handleChtimes(fs, fsPath, pathForPerms, attributes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 10:55:14 +00:00
|
|
|
if attributes.Flags&StatAttrPerms != 0 {
|
|
|
|
if err = c.handleChmod(fs, fsPath, pathForPerms, attributes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 10:55:14 +00:00
|
|
|
if attributes.Flags&StatAttrUIDGID != 0 {
|
|
|
|
if err = c.handleChown(fs, fsPath, pathForPerms, attributes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if attributes.Flags&StatAttrSize != 0 {
|
|
|
|
if !c.User.HasPerm(dataprovider.PermOverwrite, pathForPerms) {
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
startTime := time.Now()
|
2021-11-24 10:55:14 +00:00
|
|
|
if err = c.truncateFile(fs, fsPath, virtualPath, attributes.Size); err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "failed to truncate path %q, size: %v, err: %+v", fsPath, attributes.Size, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fs, err)
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
2021-06-01 20:28:43 +00:00
|
|
|
logger.CommandLog(truncateLogSender, fsPath, "", c.User.Username, "", c.ID, c.protocol, -1, -1, "", "",
|
2023-02-12 17:56:53 +00:00
|
|
|
"", attributes.Size, c.localAddr, c.remoteAddr, elapsed)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) truncateFile(fs vfs.Fs, fsPath, virtualPath string, size int64) error {
|
2020-08-20 11:54:36 +00:00
|
|
|
// check first if we have an open transfer for the given path and try to truncate the file already opened
|
|
|
|
// if we found no transfer we truncate by path.
|
2020-08-22 08:12:00 +00:00
|
|
|
var initialSize int64
|
2020-08-20 12:44:38 +00:00
|
|
|
var err error
|
2020-08-22 08:12:00 +00:00
|
|
|
initialSize, err = c.truncateOpenHandle(fsPath, size)
|
2020-08-20 11:54:36 +00:00
|
|
|
if err == errNoTransfer {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelDebug, "file path %q not found in active transfers, execute trucate by path", fsPath)
|
2020-08-22 08:12:00 +00:00
|
|
|
var info os.FileInfo
|
2021-03-21 18:15:47 +00:00
|
|
|
info, err = fs.Stat(fsPath)
|
2020-08-22 08:12:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
initialSize = info.Size()
|
2021-03-21 18:15:47 +00:00
|
|
|
err = fs.Truncate(fsPath, size)
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
2022-06-11 08:41:34 +00:00
|
|
|
if err == nil && vfs.HasTruncateSupport(fs) {
|
2020-08-22 08:12:00 +00:00
|
|
|
sizeDiff := initialSize - size
|
|
|
|
vfolder, err := c.User.GetVirtualFolderForPath(path.Dir(virtualPath))
|
|
|
|
if err == nil {
|
2024-07-13 17:30:40 +00:00
|
|
|
dataprovider.UpdateUserFolderQuota(&vfolder, &c.User, 0, -sizeDiff, false)
|
2020-08-22 08:12:00 +00:00
|
|
|
} else {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.User, 0, -sizeDiff, false) //nolint:errcheck
|
2020-08-22 08:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-20 11:54:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-12 10:04:48 +00:00
|
|
|
func (c *BaseConnection) checkRecursiveRenameDirPermissions(fsSrc, fsDst vfs.Fs, sourcePath, targetPath,
|
|
|
|
virtualSourcePath, virtualTargetPath string, fi os.FileInfo,
|
|
|
|
) error {
|
|
|
|
if !c.User.HasPermissionsInside(virtualSourcePath) &&
|
|
|
|
!c.User.HasPermissionsInside(virtualTargetPath) {
|
|
|
|
if !c.isRenamePermitted(fsSrc, fsDst, sourcePath, targetPath, virtualSourcePath, virtualTargetPath, fi) {
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelInfo, "rename %q -> %q is not allowed, virtual destination path: %q",
|
2022-06-12 10:04:48 +00:00
|
|
|
sourcePath, targetPath, virtualTargetPath)
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
// if all rename permissions are granted we have finished, otherwise we have to walk
|
|
|
|
// because we could have the rename dir permission but not the rename file and the dir to
|
|
|
|
// rename could contain files
|
|
|
|
if c.User.HasPermsRenameAll(path.Dir(virtualSourcePath)) && c.User.HasPermsRenameAll(path.Dir(virtualTargetPath)) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 08:41:34 +00:00
|
|
|
return fsSrc.Walk(sourcePath, func(walkedPath string, info os.FileInfo, err error) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetFsError(fsSrc, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2023-10-25 17:05:37 +00:00
|
|
|
if walkedPath != sourcePath && !vfs.IsRenameAtomic(fsSrc) && Config.RenameMode == 0 {
|
2022-10-27 06:27:44 +00:00
|
|
|
c.Log(logger.LevelInfo, "cannot rename non empty directory %q on this filesystem", virtualSourcePath)
|
|
|
|
return c.GetOpUnsupportedError()
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
dstPath := strings.Replace(walkedPath, sourcePath, targetPath, 1)
|
2021-03-21 18:15:47 +00:00
|
|
|
virtualSrcPath := fsSrc.GetRelativePath(walkedPath)
|
|
|
|
virtualDstPath := fsDst.GetRelativePath(dstPath)
|
|
|
|
if !c.isRenamePermitted(fsSrc, fsDst, walkedPath, dstPath, virtualSrcPath, virtualDstPath, info) {
|
2022-10-27 06:27:44 +00:00
|
|
|
c.Log(logger.LevelInfo, "rename %q -> %q is not allowed, virtual destination path: %q",
|
2020-07-24 21:39:38 +00:00
|
|
|
walkedPath, dstPath, virtualDstPath)
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.GetPermissionDeniedError()
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) hasRenamePerms(virtualSourcePath, virtualTargetPath string, fi os.FileInfo) bool {
|
2021-11-14 15:23:33 +00:00
|
|
|
if c.User.HasPermsRenameAll(path.Dir(virtualSourcePath)) &&
|
|
|
|
c.User.HasPermsRenameAll(path.Dir(virtualTargetPath)) {
|
2020-07-24 21:39:38 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-11-14 15:23:33 +00:00
|
|
|
if fi == nil {
|
2022-02-26 11:19:09 +00:00
|
|
|
// we don't know if this is a file or a directory and we don't have all the rename perms, return false
|
|
|
|
return false
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-11-14 15:23:33 +00:00
|
|
|
if fi.IsDir() {
|
2022-02-26 11:19:09 +00:00
|
|
|
perms := []string{
|
|
|
|
dataprovider.PermRenameDirs,
|
|
|
|
dataprovider.PermRename,
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2022-02-26 11:19:09 +00:00
|
|
|
return c.User.HasAnyPerm(perms, path.Dir(virtualSourcePath)) &&
|
|
|
|
c.User.HasAnyPerm(perms, path.Dir(virtualTargetPath))
|
2021-11-14 15:23:33 +00:00
|
|
|
}
|
|
|
|
// file or symlink
|
2022-02-26 11:19:09 +00:00
|
|
|
perms := []string{
|
|
|
|
dataprovider.PermRenameFiles,
|
|
|
|
dataprovider.PermRename,
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2022-02-26 11:19:09 +00:00
|
|
|
return c.User.HasAnyPerm(perms, path.Dir(virtualSourcePath)) &&
|
|
|
|
c.User.HasAnyPerm(perms, path.Dir(virtualTargetPath))
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 17:51:53 +00:00
|
|
|
func (c *BaseConnection) checkFolderRename(fsSrc, fsDst vfs.Fs, fsSourcePath, fsTargetPath, virtualSourcePath,
|
|
|
|
virtualTargetPath string, fi os.FileInfo) error {
|
|
|
|
if util.IsDirOverlapped(virtualSourcePath, virtualTargetPath, true, "/") {
|
|
|
|
c.Log(logger.LevelDebug, "renaming the folder %q->%q is not supported: nested folders",
|
|
|
|
virtualSourcePath, virtualTargetPath)
|
2023-11-06 18:10:35 +00:00
|
|
|
return fmt.Errorf("nested rename %q => %q is not supported: %w",
|
|
|
|
virtualSourcePath, virtualTargetPath, c.GetOpUnsupportedError())
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
|
|
|
if util.IsDirOverlapped(fsSourcePath, fsTargetPath, true, c.User.FsConfig.GetPathSeparator()) {
|
|
|
|
c.Log(logger.LevelDebug, "renaming the folder %q->%q is not supported: nested fs folders",
|
|
|
|
fsSourcePath, fsTargetPath)
|
2023-11-06 18:10:35 +00:00
|
|
|
return fmt.Errorf("nested fs rename %q => %q is not supported: %w",
|
|
|
|
fsSourcePath, fsTargetPath, c.GetOpUnsupportedError())
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
|
|
|
if c.User.HasVirtualFoldersInside(virtualSourcePath) {
|
|
|
|
c.Log(logger.LevelDebug, "renaming the folder %q is not supported: it has virtual folders inside it",
|
|
|
|
virtualSourcePath)
|
2023-11-06 18:10:35 +00:00
|
|
|
return fmt.Errorf("folder %q has virtual folders inside it: %w", virtualSourcePath, c.GetOpUnsupportedError())
|
2022-12-27 17:51:53 +00:00
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
if c.User.HasVirtualFoldersInside(virtualTargetPath) {
|
|
|
|
c.Log(logger.LevelDebug, "renaming the folder %q is not supported, the target %q has virtual folders inside it",
|
|
|
|
virtualSourcePath, virtualTargetPath)
|
2023-11-06 18:10:35 +00:00
|
|
|
return fmt.Errorf("folder %q has virtual folders inside it: %w", virtualTargetPath, c.GetOpUnsupportedError())
|
2023-01-06 11:33:50 +00:00
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
if err := c.checkRecursiveRenameDirPermissions(fsSrc, fsDst, fsSourcePath, fsTargetPath,
|
|
|
|
virtualSourcePath, virtualTargetPath, fi); err != nil {
|
|
|
|
c.Log(logger.LevelDebug, "error checking recursive permissions before renaming %q: %+v", fsSourcePath, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-12 10:04:48 +00:00
|
|
|
func (c *BaseConnection) isRenamePermitted(fsSrc, fsDst vfs.Fs, fsSourcePath, fsTargetPath, virtualSourcePath,
|
|
|
|
virtualTargetPath string, fi os.FileInfo,
|
|
|
|
) bool {
|
2023-04-15 12:16:26 +00:00
|
|
|
if !c.IsSameResource(virtualSourcePath, virtualTargetPath) {
|
2023-12-28 17:43:07 +00:00
|
|
|
c.Log(logger.LevelInfo, "rename %q->%q is not allowed: the paths must be on the same resource",
|
2021-03-21 18:15:47 +00:00
|
|
|
virtualSourcePath, virtualTargetPath)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.User.IsMappedPath(fsSourcePath) && vfs.IsLocalOrCryptoFs(fsSrc) {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelWarn, "renaming a directory mapped as virtual folder is not allowed: %q", fsSourcePath)
|
2021-03-21 18:15:47 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.User.IsMappedPath(fsTargetPath) && vfs.IsLocalOrCryptoFs(fsDst) {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelWarn, "renaming to a directory mapped as virtual folder is not allowed: %q", fsTargetPath)
|
2021-03-21 18:15:47 +00:00
|
|
|
return false
|
|
|
|
}
|
2022-08-10 16:41:59 +00:00
|
|
|
if virtualSourcePath == "/" || virtualTargetPath == "/" || fsSrc.GetRelativePath(fsSourcePath) == "/" {
|
2021-03-21 18:15:47 +00:00
|
|
|
c.Log(logger.LevelWarn, "renaming root dir is not allowed")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.User.IsVirtualFolder(virtualSourcePath) || c.User.IsVirtualFolder(virtualTargetPath) {
|
|
|
|
c.Log(logger.LevelWarn, "renaming a virtual folder is not allowed")
|
|
|
|
return false
|
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
isSrcAllowed, _ := c.User.IsFileAllowed(virtualSourcePath)
|
|
|
|
isDstAllowed, _ := c.User.IsFileAllowed(virtualTargetPath)
|
|
|
|
if !isSrcAllowed || !isDstAllowed {
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelDebug, "renaming source: %q to target: %q not allowed", virtualSourcePath,
|
2022-01-15 16:16:49 +00:00
|
|
|
virtualTargetPath)
|
|
|
|
return false
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
return c.hasRenamePerms(virtualSourcePath, virtualTargetPath, fi)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BaseConnection) hasSpaceForRename(fs vfs.Fs, virtualSourcePath, virtualTargetPath string, initialSize int64,
|
2020-07-24 21:39:38 +00:00
|
|
|
fsSourcePath string) bool {
|
|
|
|
if dataprovider.GetQuotaTracking() == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
sourceFolder, errSrc := c.User.GetVirtualFolderForPath(path.Dir(virtualSourcePath))
|
|
|
|
dstFolder, errDst := c.User.GetVirtualFolderForPath(path.Dir(virtualTargetPath))
|
|
|
|
if errSrc != nil && errDst != nil {
|
|
|
|
// rename inside the user home dir
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if errSrc == nil && errDst == nil {
|
|
|
|
// rename between virtual folders
|
2021-03-21 18:15:47 +00:00
|
|
|
if sourceFolder.Name == dstFolder.Name {
|
2020-07-24 21:39:38 +00:00
|
|
|
// rename inside the same virtual folder
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if errSrc != nil && dstFolder.IsIncludedInUserQuota() {
|
|
|
|
// rename between user root dir and a virtual folder included in user quota
|
|
|
|
return true
|
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
if errDst != nil && sourceFolder.IsIncludedInUserQuota() {
|
|
|
|
// rename between a virtual folder included in user quota and the user root dir
|
|
|
|
return true
|
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
quotaResult, _ := c.HasSpace(true, false, virtualTargetPath)
|
2023-01-06 11:33:50 +00:00
|
|
|
if quotaResult.HasSpace && quotaResult.QuotaSize == 0 && quotaResult.QuotaFiles == 0 {
|
|
|
|
// no quota restrictions
|
|
|
|
return true
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return c.hasSpaceForCrossRename(fs, quotaResult, initialSize, fsSourcePath)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// hasSpaceForCrossRename checks the quota after a rename between different folders
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) hasSpaceForCrossRename(fs vfs.Fs, quotaResult vfs.QuotaCheckResult, initialSize int64, sourcePath string) bool {
|
2020-07-24 21:39:38 +00:00
|
|
|
if !quotaResult.HasSpace && initialSize == -1 {
|
|
|
|
// we are over quota and this is not a file replace
|
|
|
|
return false
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fi, err := fs.Lstat(sourcePath)
|
2020-07-24 21:39:38 +00:00
|
|
|
if err != nil {
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelError, "cross rename denied, stat error for path %q: %v", sourcePath, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
var sizeDiff int64
|
|
|
|
var filesDiff int
|
|
|
|
if fi.Mode().IsRegular() {
|
|
|
|
sizeDiff = fi.Size()
|
|
|
|
filesDiff = 1
|
|
|
|
if initialSize != -1 {
|
|
|
|
sizeDiff -= initialSize
|
|
|
|
filesDiff = 0
|
|
|
|
}
|
|
|
|
} else if fi.IsDir() {
|
2021-03-21 18:15:47 +00:00
|
|
|
filesDiff, sizeDiff, err = fs.GetDirSize(sourcePath)
|
2020-07-24 21:39:38 +00:00
|
|
|
if err != nil {
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelError, "cross rename denied, error getting size for directory %q: %v", sourcePath, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !quotaResult.HasSpace && initialSize != -1 {
|
|
|
|
// we are over quota but we are overwriting an existing file so we check if the quota size after the rename is ok
|
|
|
|
if quotaResult.QuotaSize == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelDebug, "cross rename overwrite, source %q, used size %d, size to add %d",
|
2020-07-24 21:39:38 +00:00
|
|
|
sourcePath, quotaResult.UsedSize, sizeDiff)
|
|
|
|
quotaResult.UsedSize += sizeDiff
|
|
|
|
return quotaResult.GetRemainingSize() >= 0
|
|
|
|
}
|
|
|
|
if quotaResult.QuotaFiles > 0 {
|
|
|
|
remainingFiles := quotaResult.GetRemainingFiles()
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelDebug, "cross rename, source %q remaining file %d to add %d", sourcePath,
|
2020-07-24 21:39:38 +00:00
|
|
|
remainingFiles, filesDiff)
|
|
|
|
if remainingFiles < filesDiff {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if quotaResult.QuotaSize > 0 {
|
|
|
|
remainingSize := quotaResult.GetRemainingSize()
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelDebug, "cross rename, source %q remaining size %d to add %d", sourcePath,
|
2020-07-24 21:39:38 +00:00
|
|
|
remainingSize, sizeDiff)
|
|
|
|
if remainingSize < sizeDiff {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-08-16 18:17:02 +00:00
|
|
|
// GetMaxWriteSize returns the allowed size for an upload or an error
|
|
|
|
// if no enough size is available for a resume/append
|
2022-01-30 10:42:36 +00:00
|
|
|
func (c *BaseConnection) GetMaxWriteSize(quotaResult vfs.QuotaCheckResult, isResume bool, fileSize int64,
|
|
|
|
isUploadResumeSupported bool,
|
|
|
|
) (int64, error) {
|
2020-08-16 18:17:02 +00:00
|
|
|
maxWriteSize := quotaResult.GetRemainingSize()
|
|
|
|
|
|
|
|
if isResume {
|
2021-03-21 18:15:47 +00:00
|
|
|
if !isUploadResumeSupported {
|
2020-08-16 18:17:02 +00:00
|
|
|
return 0, c.GetOpUnsupportedError()
|
|
|
|
}
|
|
|
|
if c.User.Filters.MaxUploadFileSize > 0 && c.User.Filters.MaxUploadFileSize <= fileSize {
|
2021-06-28 17:40:04 +00:00
|
|
|
return 0, c.GetQuotaExceededError()
|
2020-08-16 18:17:02 +00:00
|
|
|
}
|
|
|
|
if c.User.Filters.MaxUploadFileSize > 0 {
|
|
|
|
maxUploadSize := c.User.Filters.MaxUploadFileSize - fileSize
|
|
|
|
if maxUploadSize < maxWriteSize || maxWriteSize == 0 {
|
|
|
|
maxWriteSize = maxUploadSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if maxWriteSize > 0 {
|
|
|
|
maxWriteSize += fileSize
|
|
|
|
}
|
|
|
|
if c.User.Filters.MaxUploadFileSize > 0 && (c.User.Filters.MaxUploadFileSize < maxWriteSize || maxWriteSize == 0) {
|
|
|
|
maxWriteSize = c.User.Filters.MaxUploadFileSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxWriteSize, nil
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
// GetTransferQuota returns the data transfers quota
|
|
|
|
func (c *BaseConnection) GetTransferQuota() dataprovider.TransferQuota {
|
|
|
|
result, _, _ := c.checkUserQuota()
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BaseConnection) checkUserQuota() (dataprovider.TransferQuota, int, int64) {
|
2023-06-16 19:06:21 +00:00
|
|
|
ul, dl, total := c.User.GetDataTransferLimits()
|
2022-01-30 10:42:36 +00:00
|
|
|
result := dataprovider.TransferQuota{
|
|
|
|
ULSize: ul,
|
|
|
|
DLSize: dl,
|
|
|
|
TotalSize: total,
|
|
|
|
AllowedULSize: 0,
|
|
|
|
AllowedDLSize: 0,
|
|
|
|
AllowedTotalSize: 0,
|
|
|
|
}
|
|
|
|
if !c.User.HasTransferQuotaRestrictions() {
|
|
|
|
return result, -1, -1
|
|
|
|
}
|
|
|
|
usedFiles, usedSize, usedULSize, usedDLSize, err := dataprovider.GetUsedQuota(c.User.Username)
|
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "error getting used quota for %q: %v", c.User.Username, err)
|
2022-01-30 10:42:36 +00:00
|
|
|
result.AllowedTotalSize = -1
|
|
|
|
return result, -1, -1
|
|
|
|
}
|
|
|
|
if result.TotalSize > 0 {
|
|
|
|
result.AllowedTotalSize = result.TotalSize - (usedULSize + usedDLSize)
|
|
|
|
}
|
|
|
|
if result.ULSize > 0 {
|
|
|
|
result.AllowedULSize = result.ULSize - usedULSize
|
|
|
|
}
|
|
|
|
if result.DLSize > 0 {
|
|
|
|
result.AllowedDLSize = result.DLSize - usedDLSize
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, usedFiles, usedSize
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// HasSpace checks user's quota usage
|
2022-01-30 10:42:36 +00:00
|
|
|
func (c *BaseConnection) HasSpace(checkFiles, getUsage bool, requestPath string) (vfs.QuotaCheckResult,
|
|
|
|
dataprovider.TransferQuota,
|
|
|
|
) {
|
2020-07-24 21:39:38 +00:00
|
|
|
result := vfs.QuotaCheckResult{
|
|
|
|
HasSpace: true,
|
|
|
|
AllowedSize: 0,
|
|
|
|
AllowedFiles: 0,
|
|
|
|
UsedSize: 0,
|
|
|
|
UsedFiles: 0,
|
|
|
|
QuotaSize: 0,
|
|
|
|
QuotaFiles: 0,
|
|
|
|
}
|
|
|
|
if dataprovider.GetQuotaTracking() == 0 {
|
2022-01-30 10:42:36 +00:00
|
|
|
return result, dataprovider.TransferQuota{}
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
transferQuota, usedFiles, usedSize := c.checkUserQuota()
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
var err error
|
|
|
|
var vfolder vfs.VirtualFolder
|
|
|
|
vfolder, err = c.User.GetVirtualFolderForPath(path.Dir(requestPath))
|
|
|
|
if err == nil && !vfolder.IsIncludedInUserQuota() {
|
2021-02-11 18:45:52 +00:00
|
|
|
if vfolder.HasNoQuotaRestrictions(checkFiles) && !getUsage {
|
2022-01-30 10:42:36 +00:00
|
|
|
return result, transferQuota
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
result.QuotaSize = vfolder.QuotaSize
|
|
|
|
result.QuotaFiles = vfolder.QuotaFiles
|
2021-02-01 18:04:15 +00:00
|
|
|
result.UsedFiles, result.UsedSize, err = dataprovider.GetUsedVirtualFolderQuota(vfolder.Name)
|
2020-07-24 21:39:38 +00:00
|
|
|
} else {
|
2021-02-11 18:45:52 +00:00
|
|
|
if c.User.HasNoQuotaRestrictions(checkFiles) && !getUsage {
|
2022-01-30 10:42:36 +00:00
|
|
|
return result, transferQuota
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
result.QuotaSize = c.User.QuotaSize
|
|
|
|
result.QuotaFiles = c.User.QuotaFiles
|
2022-01-30 10:42:36 +00:00
|
|
|
if usedSize == -1 {
|
|
|
|
result.UsedFiles, result.UsedSize, _, _, err = dataprovider.GetUsedQuota(c.User.Username)
|
|
|
|
} else {
|
|
|
|
err = nil
|
|
|
|
result.UsedFiles = usedFiles
|
|
|
|
result.UsedSize = usedSize
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
c.Log(logger.LevelError, "error getting used quota for %q request path %q: %v", c.User.Username, requestPath, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
result.HasSpace = false
|
2022-01-30 10:42:36 +00:00
|
|
|
return result, transferQuota
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
result.AllowedFiles = result.QuotaFiles - result.UsedFiles
|
|
|
|
result.AllowedSize = result.QuotaSize - result.UsedSize
|
|
|
|
if (checkFiles && result.QuotaFiles > 0 && result.UsedFiles >= result.QuotaFiles) ||
|
|
|
|
(result.QuotaSize > 0 && result.UsedSize >= result.QuotaSize) {
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelDebug, "quota exceed for user %q, request path %q, num files: %d/%d, size: %d/%d check files: %t",
|
2020-07-24 21:39:38 +00:00
|
|
|
c.User.Username, requestPath, result.UsedFiles, result.QuotaFiles, result.UsedSize, result.QuotaSize, checkFiles)
|
|
|
|
result.HasSpace = false
|
2022-01-30 10:42:36 +00:00
|
|
|
return result, transferQuota
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
return result, transferQuota
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 12:16:26 +00:00
|
|
|
// IsSameResource returns true if source and target paths are on the same resource
|
|
|
|
func (c *BaseConnection) IsSameResource(virtualSourcePath, virtualTargetPath string) bool {
|
2021-03-21 18:15:47 +00:00
|
|
|
sourceFolder, errSrc := c.User.GetVirtualFolderForPath(virtualSourcePath)
|
|
|
|
dstFolder, errDst := c.User.GetVirtualFolderForPath(virtualTargetPath)
|
|
|
|
if errSrc != nil && errDst != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if errSrc == nil && errDst == nil {
|
|
|
|
if sourceFolder.Name == dstFolder.Name {
|
|
|
|
return true
|
|
|
|
}
|
2022-08-15 19:39:04 +00:00
|
|
|
// we have different folders, check if they point to the same resource
|
|
|
|
return sourceFolder.FsConfig.IsSameResource(dstFolder.FsConfig)
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
if errSrc == nil {
|
2022-08-15 19:39:04 +00:00
|
|
|
return sourceFolder.FsConfig.IsSameResource(c.User.FsConfig)
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
2022-08-15 19:39:04 +00:00
|
|
|
return dstFolder.FsConfig.IsSameResource(c.User.FsConfig)
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
func (c *BaseConnection) isCrossFoldersRequest(virtualSourcePath, virtualTargetPath string) bool {
|
|
|
|
sourceFolder, errSrc := c.User.GetVirtualFolderForPath(virtualSourcePath)
|
|
|
|
dstFolder, errDst := c.User.GetVirtualFolderForPath(virtualTargetPath)
|
|
|
|
if errSrc != nil && errDst != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if errSrc == nil && errDst == nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
return sourceFolder.Name != dstFolder.Name
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-16 18:11:36 +00:00
|
|
|
func (c *BaseConnection) updateQuotaMoveBetweenVFolders(sourceFolder, dstFolder *vfs.VirtualFolder, initialSize,
|
2020-07-24 21:39:38 +00:00
|
|
|
filesSize int64, numFiles int) {
|
2021-03-21 18:15:47 +00:00
|
|
|
if sourceFolder.Name == dstFolder.Name {
|
2020-07-24 21:39:38 +00:00
|
|
|
// both files are inside the same virtual folder
|
|
|
|
if initialSize != -1 {
|
2024-07-13 17:30:40 +00:00
|
|
|
dataprovider.UpdateUserFolderQuota(dstFolder, &c.User, -numFiles, -initialSize, false)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// files are inside different virtual folders
|
2024-07-13 17:30:40 +00:00
|
|
|
dataprovider.UpdateUserFolderQuota(sourceFolder, &c.User, -numFiles, -filesSize, false)
|
2020-07-24 21:39:38 +00:00
|
|
|
if initialSize == -1 {
|
2024-07-13 17:30:40 +00:00
|
|
|
dataprovider.UpdateUserFolderQuota(dstFolder, &c.User, numFiles, filesSize, false)
|
|
|
|
return
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2024-07-13 17:30:40 +00:00
|
|
|
// we cannot have a directory here, initialSize != -1 only for files
|
|
|
|
dataprovider.UpdateUserFolderQuota(dstFolder, &c.User, 0, filesSize-initialSize, false)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 18:11:36 +00:00
|
|
|
func (c *BaseConnection) updateQuotaMoveFromVFolder(sourceFolder *vfs.VirtualFolder, initialSize, filesSize int64, numFiles int) {
|
2020-07-24 21:39:38 +00:00
|
|
|
// move between a virtual folder and the user home dir
|
2024-07-13 17:30:40 +00:00
|
|
|
dataprovider.UpdateUserFolderQuota(sourceFolder, &c.User, -numFiles, -filesSize, false)
|
2020-07-24 21:39:38 +00:00
|
|
|
if initialSize == -1 {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.User, numFiles, filesSize, false) //nolint:errcheck
|
2024-07-13 17:30:40 +00:00
|
|
|
return
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2024-07-13 17:30:40 +00:00
|
|
|
// we cannot have a directory here, initialSize != -1 only for files
|
|
|
|
dataprovider.UpdateUserQuota(&c.User, 0, filesSize-initialSize, false) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 18:11:36 +00:00
|
|
|
func (c *BaseConnection) updateQuotaMoveToVFolder(dstFolder *vfs.VirtualFolder, initialSize, filesSize int64, numFiles int) {
|
2020-07-24 21:39:38 +00:00
|
|
|
// move between the user home dir and a virtual folder
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.User, -numFiles, -filesSize, false) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
if initialSize == -1 {
|
2024-07-13 17:30:40 +00:00
|
|
|
dataprovider.UpdateUserFolderQuota(dstFolder, &c.User, numFiles, filesSize, false)
|
|
|
|
return
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2024-07-13 17:30:40 +00:00
|
|
|
// we cannot have a directory here, initialSize != -1 only for files
|
|
|
|
dataprovider.UpdateUserFolderQuota(dstFolder, &c.User, 0, filesSize-initialSize, false)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 11:33:50 +00:00
|
|
|
func (c *BaseConnection) updateQuotaAfterRename(fs vfs.Fs, virtualSourcePath, virtualTargetPath, targetPath string,
|
|
|
|
initialSize int64, numFiles int, filesSize int64,
|
|
|
|
) error {
|
2021-03-21 18:15:47 +00:00
|
|
|
if dataprovider.GetQuotaTracking() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
// we don't allow to overwrite an existing directory so targetPath can be:
|
|
|
|
// - a new file, a symlink is as a new file here
|
|
|
|
// - a file overwriting an existing one
|
|
|
|
// - a new directory
|
|
|
|
// initialSize != -1 only when overwriting files
|
|
|
|
sourceFolder, errSrc := c.User.GetVirtualFolderForPath(path.Dir(virtualSourcePath))
|
|
|
|
dstFolder, errDst := c.User.GetVirtualFolderForPath(path.Dir(virtualTargetPath))
|
|
|
|
if errSrc != nil && errDst != nil {
|
|
|
|
// both files are contained inside the user home dir
|
|
|
|
if initialSize != -1 {
|
2021-03-21 18:15:47 +00:00
|
|
|
// we cannot have a directory here, we are overwriting an existing file
|
|
|
|
// we need to subtract the size of the overwritten file from the user quota
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&c.User, -1, -initialSize, false) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-06 11:33:50 +00:00
|
|
|
if filesSize == -1 {
|
|
|
|
// fs.Rename didn't return the affected files/sizes, we need to calculate them
|
|
|
|
numFiles = 1
|
|
|
|
if fi, err := fs.Stat(targetPath); err == nil {
|
|
|
|
if fi.Mode().IsDir() {
|
|
|
|
numFiles, filesSize, err = fs.GetDirSize(targetPath)
|
|
|
|
if err != nil {
|
|
|
|
c.Log(logger.LevelError, "failed to update quota after rename, error scanning moved folder %q: %+v",
|
|
|
|
targetPath, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
filesSize = fi.Size()
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelError, "failed to update quota after renaming, file %q stat error: %+v", targetPath, err)
|
|
|
|
return err
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelDebug, "calculated renamed files: %d, size: %d bytes", numFiles, filesSize)
|
2020-07-24 21:39:38 +00:00
|
|
|
} else {
|
2023-01-06 11:33:50 +00:00
|
|
|
c.Log(logger.LevelDebug, "returned renamed files: %d, size: %d bytes", numFiles, filesSize)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
if errSrc == nil && errDst == nil {
|
2021-02-16 18:11:36 +00:00
|
|
|
c.updateQuotaMoveBetweenVFolders(&sourceFolder, &dstFolder, initialSize, filesSize, numFiles)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
if errSrc == nil && errDst != nil {
|
2021-02-16 18:11:36 +00:00
|
|
|
c.updateQuotaMoveFromVFolder(&sourceFolder, initialSize, filesSize, numFiles)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
if errSrc != nil && errDst == nil {
|
2021-02-16 18:11:36 +00:00
|
|
|
c.updateQuotaMoveToVFolder(&dstFolder, initialSize, filesSize, numFiles)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-19 11:14:53 +00:00
|
|
|
// IsNotExistError returns true if the specified fs error is not exist for the connection protocol
|
|
|
|
func (c *BaseConnection) IsNotExistError(err error) bool {
|
|
|
|
switch c.protocol {
|
|
|
|
case ProtocolSFTP:
|
|
|
|
return errors.Is(err, sftp.ErrSSHFxNoSuchFile)
|
2022-02-19 09:53:35 +00:00
|
|
|
case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolOIDC, ProtocolHTTPShare, ProtocolDataRetention:
|
2021-12-19 11:14:53 +00:00
|
|
|
return errors.Is(err, os.ErrNotExist)
|
|
|
|
default:
|
|
|
|
return errors.Is(err, ErrNotExist)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
// GetErrorForDeniedFile return permission denied or not exist error based on the specified policy
|
|
|
|
func (c *BaseConnection) GetErrorForDeniedFile(policy int) error {
|
|
|
|
switch policy {
|
|
|
|
case sdk.DenyPolicyHide:
|
|
|
|
return c.GetNotExistError()
|
|
|
|
default:
|
|
|
|
return c.GetPermissionDeniedError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// GetPermissionDeniedError returns an appropriate permission denied error for the connection protocol
|
|
|
|
func (c *BaseConnection) GetPermissionDeniedError() error {
|
2023-02-28 17:01:09 +00:00
|
|
|
return getPermissionDeniedError(c.protocol)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetNotExistError returns an appropriate not exist error for the connection protocol
|
|
|
|
func (c *BaseConnection) GetNotExistError() error {
|
|
|
|
switch c.protocol {
|
|
|
|
case ProtocolSFTP:
|
|
|
|
return sftp.ErrSSHFxNoSuchFile
|
2022-02-19 09:53:35 +00:00
|
|
|
case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolOIDC, ProtocolHTTPShare, ProtocolDataRetention:
|
2020-08-11 21:56:10 +00:00
|
|
|
return os.ErrNotExist
|
2020-07-24 21:39:38 +00:00
|
|
|
default:
|
|
|
|
return ErrNotExist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOpUnsupportedError returns an appropriate operation not supported error for the connection protocol
|
|
|
|
func (c *BaseConnection) GetOpUnsupportedError() error {
|
|
|
|
switch c.protocol {
|
|
|
|
case ProtocolSFTP:
|
|
|
|
return sftp.ErrSSHFxOpUnsupported
|
|
|
|
default:
|
|
|
|
return ErrOpUnsupported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
func getQuotaExceededError(protocol string) error {
|
|
|
|
switch protocol {
|
2021-08-08 17:30:21 +00:00
|
|
|
case ProtocolSFTP:
|
2024-05-14 17:10:36 +00:00
|
|
|
return fmt.Errorf("%w: %w", sftp.ErrSSHFxFailure, ErrQuotaExceeded)
|
2021-06-28 17:40:04 +00:00
|
|
|
case ProtocolFTP:
|
|
|
|
return ftpserver.ErrStorageExceeded
|
|
|
|
default:
|
|
|
|
return ErrQuotaExceeded
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
func getReadQuotaExceededError(protocol string) error {
|
|
|
|
switch protocol {
|
|
|
|
case ProtocolSFTP:
|
2024-05-14 17:10:36 +00:00
|
|
|
return fmt.Errorf("%w: %w", sftp.ErrSSHFxFailure, ErrReadQuotaExceeded)
|
2022-01-30 10:42:36 +00:00
|
|
|
default:
|
|
|
|
return ErrReadQuotaExceeded
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
// GetQuotaExceededError returns an appropriate storage limit exceeded error for the connection protocol
|
|
|
|
func (c *BaseConnection) GetQuotaExceededError() error {
|
|
|
|
return getQuotaExceededError(c.protocol)
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
// GetReadQuotaExceededError returns an appropriate read quota limit exceeded error for the connection protocol
|
|
|
|
func (c *BaseConnection) GetReadQuotaExceededError() error {
|
|
|
|
return getReadQuotaExceededError(c.protocol)
|
|
|
|
}
|
|
|
|
|
2021-06-28 17:40:04 +00:00
|
|
|
// IsQuotaExceededError returns true if the given error is a quota exceeded error
|
|
|
|
func (c *BaseConnection) IsQuotaExceededError(err error) bool {
|
|
|
|
switch c.protocol {
|
2021-08-08 17:30:21 +00:00
|
|
|
case ProtocolSFTP:
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if errors.Is(err, ErrQuotaExceeded) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return errors.Is(err, sftp.ErrSSHFxFailure) && strings.Contains(err.Error(), ErrQuotaExceeded.Error())
|
2021-06-28 17:40:04 +00:00
|
|
|
case ProtocolFTP:
|
2021-08-08 17:30:21 +00:00
|
|
|
return errors.Is(err, ftpserver.ErrStorageExceeded) || errors.Is(err, ErrQuotaExceeded)
|
2021-06-28 17:40:04 +00:00
|
|
|
default:
|
|
|
|
return errors.Is(err, ErrQuotaExceeded)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 17:10:36 +00:00
|
|
|
func isSFTPGoError(err error) bool {
|
|
|
|
return errors.Is(err, ErrPermissionDenied) || errors.Is(err, ErrNotExist) || errors.Is(err, ErrOpUnsupported) ||
|
|
|
|
errors.Is(err, ErrQuotaExceeded) || errors.Is(err, ErrReadQuotaExceeded) ||
|
|
|
|
errors.Is(err, vfs.ErrStorageSizeUnavailable) || errors.Is(err, ErrShuttingDown)
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// GetGenericError returns an appropriate generic error for the connection protocol
|
2020-09-19 08:14:30 +00:00
|
|
|
func (c *BaseConnection) GetGenericError(err error) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
switch c.protocol {
|
|
|
|
case ProtocolSFTP:
|
2024-05-14 17:10:36 +00:00
|
|
|
if errors.Is(err, vfs.ErrStorageSizeUnavailable) || errors.Is(err, ErrOpUnsupported) || errors.Is(err, sftp.ErrSSHFxOpUnsupported) {
|
|
|
|
return fmt.Errorf("%w: %w", sftp.ErrSSHFxOpUnsupported, err)
|
2021-08-08 17:30:21 +00:00
|
|
|
}
|
2024-05-14 17:10:36 +00:00
|
|
|
if isSFTPGoError(err) {
|
|
|
|
return fmt.Errorf("%w: %w", sftp.ErrSSHFxFailure, err)
|
2022-10-22 09:56:41 +00:00
|
|
|
}
|
2021-08-08 17:30:21 +00:00
|
|
|
if err != nil {
|
2024-05-10 13:09:00 +00:00
|
|
|
var pathError *fs.PathError
|
|
|
|
if errors.As(err, &pathError) {
|
|
|
|
c.Log(logger.LevelError, "generic path error: %+v", pathError)
|
|
|
|
return fmt.Errorf("%w: %v %v", sftp.ErrSSHFxFailure, pathError.Op, pathError.Err.Error())
|
2021-08-08 17:30:21 +00:00
|
|
|
}
|
2022-06-28 20:08:16 +00:00
|
|
|
c.Log(logger.LevelError, "generic error: %+v", err)
|
2021-02-11 18:45:52 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
return sftp.ErrSSHFxFailure
|
|
|
|
default:
|
2024-05-14 17:10:36 +00:00
|
|
|
if isSFTPGoError(err) {
|
2020-09-19 08:14:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-01 11:22:54 +00:00
|
|
|
c.Log(logger.LevelError, "generic error: %+v", err)
|
2020-07-24 21:39:38 +00:00
|
|
|
return ErrGenericFailure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFsError converts a filesystem error to a protocol error
|
2021-03-21 18:15:47 +00:00
|
|
|
func (c *BaseConnection) GetFsError(fs vfs.Fs, err error) error {
|
|
|
|
if fs.IsNotExist(err) {
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.GetNotExistError()
|
2021-03-21 18:15:47 +00:00
|
|
|
} else if fs.IsPermission(err) {
|
2020-07-24 21:39:38 +00:00
|
|
|
return c.GetPermissionDeniedError()
|
2021-03-21 18:15:47 +00:00
|
|
|
} else if fs.IsNotSupported(err) {
|
2020-11-12 09:39:46 +00:00
|
|
|
return c.GetOpUnsupportedError()
|
2020-07-24 21:39:38 +00:00
|
|
|
} else if err != nil {
|
2020-09-19 08:14:30 +00:00
|
|
|
return c.GetGenericError(err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
|
2022-12-27 17:51:53 +00:00
|
|
|
func (c *BaseConnection) getNotificationStatus(err error) int {
|
|
|
|
if err == nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if c.IsQuotaExceededError(err) {
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
// GetFsAndResolvedPath returns the fs and the fs path matching virtualPath
|
|
|
|
func (c *BaseConnection) GetFsAndResolvedPath(virtualPath string) (vfs.Fs, string, error) {
|
|
|
|
fs, err := c.User.GetFilesystemForPath(virtualPath, c.ID)
|
|
|
|
if err != nil {
|
2021-04-01 16:53:48 +00:00
|
|
|
if c.protocol == ProtocolWebDAV && strings.Contains(err.Error(), vfs.ErrSFTPLoop.Error()) {
|
|
|
|
// if there is an SFTP loop we return a permission error, for WebDAV, so the problematic folder
|
|
|
|
// will not be listed
|
2023-12-10 15:40:13 +00:00
|
|
|
return nil, "", util.NewI18nError(c.GetPermissionDeniedError(), util.I18nError403Message)
|
2021-04-01 16:53:48 +00:00
|
|
|
}
|
2022-11-01 11:22:54 +00:00
|
|
|
return nil, "", c.GetGenericError(err)
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 09:56:41 +00:00
|
|
|
if isShuttingDown.Load() {
|
|
|
|
return nil, "", c.GetFsError(fs, ErrShuttingDown)
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
fsPath, err := fs.ResolvePath(virtualPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", c.GetFsError(fs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs, fsPath, nil
|
|
|
|
}
|
2023-01-06 11:33:50 +00:00
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
// DirListerAt defines a directory lister implementing the ListAt method.
|
|
|
|
type DirListerAt struct {
|
|
|
|
virtualPath string
|
2024-10-16 17:04:09 +00:00
|
|
|
conn *BaseConnection
|
|
|
|
fs vfs.Fs
|
2024-02-15 19:53:56 +00:00
|
|
|
info []os.FileInfo
|
|
|
|
mu sync.Mutex
|
|
|
|
lister vfs.DirLister
|
|
|
|
}
|
|
|
|
|
2024-07-24 16:17:55 +00:00
|
|
|
// Prepend adds the given os.FileInfo as first element of the internal cache
|
|
|
|
func (l *DirListerAt) Prepend(fi os.FileInfo) {
|
2024-02-15 19:53:56 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
2024-07-24 16:17:55 +00:00
|
|
|
l.info = slices.Insert(l.info, 0, fi)
|
2024-02-15 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListAt implements sftp.ListerAt
|
|
|
|
func (l *DirListerAt) ListAt(f []os.FileInfo, _ int64) (int, error) {
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
|
|
|
if len(f) == 0 {
|
|
|
|
return 0, errors.New("invalid ListAt destination, zero size")
|
|
|
|
}
|
|
|
|
if len(f) <= len(l.info) {
|
|
|
|
files := make([]os.FileInfo, 0, len(f))
|
2024-07-24 16:17:55 +00:00
|
|
|
for idx := range l.info {
|
2024-02-15 19:53:56 +00:00
|
|
|
files = append(files, l.info[idx])
|
|
|
|
if len(files) == len(f) {
|
2024-07-24 16:17:55 +00:00
|
|
|
l.info = l.info[idx+1:]
|
2024-02-15 19:53:56 +00:00
|
|
|
n := copy(f, files)
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
limit := len(f) - len(l.info)
|
|
|
|
files, err := l.Next(limit)
|
|
|
|
n := copy(f, files)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next reads the directory and returns a slice of up to n FileInfo values.
|
|
|
|
func (l *DirListerAt) Next(limit int) ([]os.FileInfo, error) {
|
|
|
|
for {
|
|
|
|
files, err := l.lister.Next(limit)
|
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
2024-10-16 17:04:09 +00:00
|
|
|
l.conn.Log(logger.LevelDebug, "error retrieving directory entries: %+v", err)
|
|
|
|
return files, l.conn.GetFsError(l.fs, err)
|
2024-02-15 19:53:56 +00:00
|
|
|
}
|
2024-10-16 17:04:09 +00:00
|
|
|
files = l.conn.User.FilterListDir(files, l.virtualPath)
|
2024-02-15 19:53:56 +00:00
|
|
|
if len(l.info) > 0 {
|
2024-07-24 16:17:55 +00:00
|
|
|
files = slices.Concat(l.info, files)
|
2024-02-15 19:53:56 +00:00
|
|
|
l.info = nil
|
|
|
|
}
|
|
|
|
if err != nil || len(files) > 0 {
|
|
|
|
return files, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the DirListerAt
|
|
|
|
func (l *DirListerAt) Close() error {
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
|
|
|
return l.lister.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *DirListerAt) convertError(err error) error {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-28 17:01:09 +00:00
|
|
|
func getPermissionDeniedError(protocol string) error {
|
|
|
|
switch protocol {
|
|
|
|
case ProtocolSFTP:
|
|
|
|
return sftp.ErrSSHFxPermissionDenied
|
|
|
|
case ProtocolWebDAV, ProtocolFTP, ProtocolHTTP, ProtocolOIDC, ProtocolHTTPShare, ProtocolDataRetention:
|
|
|
|
return os.ErrPermission
|
|
|
|
default:
|
|
|
|
return ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 11:33:50 +00:00
|
|
|
func keepConnectionAlive(c *BaseConnection, done chan bool, interval time.Duration) {
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer func() {
|
|
|
|
ticker.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
c.UpdateLastActivity()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|