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 (
|
|
|
|
"errors"
|
2024-05-10 13:09:00 +00:00
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
2020-07-24 21:39:38 +00:00
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
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/metric"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2020-07-24 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrTransferClosed defines the error returned for a closed transfer
|
|
|
|
ErrTransferClosed = errors.New("transfer already closed")
|
|
|
|
)
|
|
|
|
|
|
|
|
// BaseTransfer contains protocols common transfer details for an upload or a download.
|
2020-08-11 21:56:10 +00:00
|
|
|
type BaseTransfer struct { //nolint:maligned
|
2022-01-20 17:19:20 +00:00
|
|
|
ID int64
|
2022-08-30 13:47:41 +00:00
|
|
|
BytesSent atomic.Int64
|
|
|
|
BytesReceived atomic.Int64
|
2021-05-31 19:45:29 +00:00
|
|
|
Fs vfs.Fs
|
|
|
|
File vfs.File
|
|
|
|
Connection *BaseConnection
|
|
|
|
cancelFn func()
|
|
|
|
fsPath string
|
|
|
|
effectiveFsPath string
|
|
|
|
requestPath string
|
2021-08-10 11:07:38 +00:00
|
|
|
ftpMode string
|
2021-05-31 19:45:29 +00:00
|
|
|
start time.Time
|
|
|
|
MaxWriteSize int64
|
|
|
|
MinWriteOffset int64
|
|
|
|
InitialSize int64
|
2022-01-20 17:19:20 +00:00
|
|
|
truncatedSize int64
|
2021-05-31 19:45:29 +00:00
|
|
|
isNewFile bool
|
|
|
|
transferType int
|
2022-08-30 13:47:41 +00:00
|
|
|
AbortTransfer atomic.Bool
|
2021-11-26 18:00:44 +00:00
|
|
|
aTime time.Time
|
|
|
|
mTime time.Time
|
2022-01-30 10:42:36 +00:00
|
|
|
transferQuota dataprovider.TransferQuota
|
2023-08-12 16:51:47 +00:00
|
|
|
metadata map[string]string
|
2020-07-24 21:39:38 +00:00
|
|
|
sync.Mutex
|
2022-01-20 17:19:20 +00:00
|
|
|
errAbort error
|
2020-07-24 21:39:38 +00:00
|
|
|
ErrTransfer error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBaseTransfer returns a new BaseTransfer and adds it to the given connection
|
2021-05-31 19:45:29 +00:00
|
|
|
func NewBaseTransfer(file vfs.File, conn *BaseConnection, cancelFn func(), fsPath, effectiveFsPath, requestPath string,
|
2022-01-20 17:19:20 +00:00
|
|
|
transferType int, minWriteOffset, initialSize, maxWriteSize, truncatedSize int64, isNewFile bool, fs vfs.Fs,
|
2022-01-30 10:42:36 +00:00
|
|
|
transferQuota dataprovider.TransferQuota,
|
2022-01-20 17:19:20 +00:00
|
|
|
) *BaseTransfer {
|
2020-07-24 21:39:38 +00:00
|
|
|
t := &BaseTransfer{
|
2021-05-31 19:45:29 +00:00
|
|
|
ID: conn.GetTransferID(),
|
|
|
|
File: file,
|
|
|
|
Connection: conn,
|
|
|
|
cancelFn: cancelFn,
|
|
|
|
fsPath: fsPath,
|
|
|
|
effectiveFsPath: effectiveFsPath,
|
|
|
|
start: time.Now(),
|
|
|
|
transferType: transferType,
|
|
|
|
MinWriteOffset: minWriteOffset,
|
|
|
|
InitialSize: initialSize,
|
|
|
|
isNewFile: isNewFile,
|
|
|
|
requestPath: requestPath,
|
|
|
|
MaxWriteSize: maxWriteSize,
|
2022-01-20 17:19:20 +00:00
|
|
|
truncatedSize: truncatedSize,
|
2022-01-30 10:42:36 +00:00
|
|
|
transferQuota: transferQuota,
|
2021-05-31 19:45:29 +00:00
|
|
|
Fs: fs,
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
t.AbortTransfer.Store(false)
|
|
|
|
t.BytesSent.Store(0)
|
|
|
|
t.BytesReceived.Store(0)
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
conn.AddTransfer(t)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
// GetTransferQuota returns data transfer quota limits
|
|
|
|
func (t *BaseTransfer) GetTransferQuota() dataprovider.TransferQuota {
|
|
|
|
return t.transferQuota
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:07:38 +00:00
|
|
|
// SetFtpMode sets the FTP mode for the current transfer
|
|
|
|
func (t *BaseTransfer) SetFtpMode(mode string) {
|
|
|
|
t.ftpMode = mode
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// GetID returns the transfer ID
|
2022-01-20 17:19:20 +00:00
|
|
|
func (t *BaseTransfer) GetID() int64 {
|
2020-07-24 21:39:38 +00:00
|
|
|
return t.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetType returns the transfer type
|
|
|
|
func (t *BaseTransfer) GetType() int {
|
|
|
|
return t.transferType
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSize returns the transferred size
|
|
|
|
func (t *BaseTransfer) GetSize() int64 {
|
|
|
|
if t.transferType == TransferDownload {
|
2022-08-30 13:47:41 +00:00
|
|
|
return t.BytesSent.Load()
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
return t.BytesReceived.Load()
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
// GetDownloadedSize returns the transferred size
|
|
|
|
func (t *BaseTransfer) GetDownloadedSize() int64 {
|
2022-08-30 13:47:41 +00:00
|
|
|
return t.BytesSent.Load()
|
2022-01-20 17:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUploadedSize returns the transferred size
|
|
|
|
func (t *BaseTransfer) GetUploadedSize() int64 {
|
2022-08-30 13:47:41 +00:00
|
|
|
return t.BytesReceived.Load()
|
2022-01-20 17:19:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// GetStartTime returns the start time
|
|
|
|
func (t *BaseTransfer) GetStartTime() time.Time {
|
|
|
|
return t.start
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
// GetAbortError returns the error to send to the client if the transfer was aborted
|
|
|
|
func (t *BaseTransfer) GetAbortError() error {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
|
|
|
if t.errAbort != nil {
|
|
|
|
return t.errAbort
|
|
|
|
}
|
|
|
|
return getQuotaExceededError(t.Connection.protocol)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignalClose signals that the transfer should be closed after the next read/write.
|
|
|
|
// The optional error argument allow to send a specific error, otherwise a generic
|
|
|
|
// transfer aborted error is sent
|
|
|
|
func (t *BaseTransfer) SignalClose(err error) {
|
|
|
|
t.Lock()
|
|
|
|
t.errAbort = err
|
|
|
|
t.Unlock()
|
2022-08-30 13:47:41 +00:00
|
|
|
t.AbortTransfer.Store(true)
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
// GetTruncatedSize returns the truncated sized if this is an upload overwriting
|
|
|
|
// an existing file
|
|
|
|
func (t *BaseTransfer) GetTruncatedSize() int64 {
|
|
|
|
return t.truncatedSize
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
// HasSizeLimit returns true if there is an upload or download size limit
|
|
|
|
func (t *BaseTransfer) HasSizeLimit() bool {
|
|
|
|
if t.MaxWriteSize > 0 {
|
|
|
|
return true
|
|
|
|
}
|
2022-01-31 18:30:25 +00:00
|
|
|
if t.transferQuota.HasSizeLimits() {
|
2022-01-30 10:42:36 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2022-01-20 17:19:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// GetVirtualPath returns the transfer virtual path
|
|
|
|
func (t *BaseTransfer) GetVirtualPath() string {
|
|
|
|
return t.requestPath
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// GetFsPath returns the transfer filesystem path
|
|
|
|
func (t *BaseTransfer) GetFsPath() string {
|
|
|
|
return t.fsPath
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// SetTimes stores access and modification times if fsPath matches the current file
|
2021-11-26 18:00:44 +00:00
|
|
|
func (t *BaseTransfer) SetTimes(fsPath string, atime time.Time, mtime time.Time) bool {
|
|
|
|
if fsPath == t.GetFsPath() {
|
|
|
|
t.aTime = atime
|
|
|
|
t.mTime = mtime
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-08-22 12:52:17 +00:00
|
|
|
// GetRealFsPath returns the real transfer filesystem path.
|
|
|
|
// If atomic uploads are enabled this differ from fsPath
|
|
|
|
func (t *BaseTransfer) GetRealFsPath(fsPath string) string {
|
|
|
|
if fsPath == t.GetFsPath() {
|
2023-05-16 16:08:14 +00:00
|
|
|
if t.File != nil || vfs.IsLocalOsFs(t.Fs) {
|
|
|
|
return t.effectiveFsPath
|
2020-08-22 12:52:17 +00:00
|
|
|
}
|
|
|
|
return t.fsPath
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-08-12 16:51:47 +00:00
|
|
|
// SetMetadata sets the metadata for the file
|
|
|
|
func (t *BaseTransfer) SetMetadata(val map[string]string) {
|
|
|
|
t.metadata = val
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// SetCancelFn sets the cancel function for the transfer
|
|
|
|
func (t *BaseTransfer) SetCancelFn(cancelFn func()) {
|
|
|
|
t.cancelFn = cancelFn
|
|
|
|
}
|
|
|
|
|
2023-04-08 12:55:04 +00:00
|
|
|
// ConvertError accepts an error that occurs during a read or write and
|
|
|
|
// converts it into a more understandable form for the client if it is a
|
|
|
|
// well-known type of error
|
|
|
|
func (t *BaseTransfer) ConvertError(err error) error {
|
2024-05-10 13:09:00 +00:00
|
|
|
var pathError *fs.PathError
|
|
|
|
if errors.As(err, &pathError) {
|
|
|
|
return fmt.Errorf("%s %s: %s", pathError.Op, t.GetVirtualPath(), pathError.Err.Error())
|
|
|
|
}
|
2024-05-14 17:10:36 +00:00
|
|
|
return t.Connection.GetFsError(t.Fs, err)
|
2023-04-08 12:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
// CheckRead returns an error if read if not allowed
|
|
|
|
func (t *BaseTransfer) CheckRead() error {
|
|
|
|
if t.transferQuota.AllowedDLSize == 0 && t.transferQuota.AllowedTotalSize == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if t.transferQuota.AllowedTotalSize > 0 {
|
2022-08-30 13:47:41 +00:00
|
|
|
if t.BytesSent.Load()+t.BytesReceived.Load() > t.transferQuota.AllowedTotalSize {
|
2022-01-30 10:42:36 +00:00
|
|
|
return t.Connection.GetReadQuotaExceededError()
|
|
|
|
}
|
|
|
|
} else if t.transferQuota.AllowedDLSize > 0 {
|
2022-08-30 13:47:41 +00:00
|
|
|
if t.BytesSent.Load() > t.transferQuota.AllowedDLSize {
|
2022-01-30 10:42:36 +00:00
|
|
|
return t.Connection.GetReadQuotaExceededError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckWrite returns an error if write if not allowed
|
|
|
|
func (t *BaseTransfer) CheckWrite() error {
|
2022-08-30 13:47:41 +00:00
|
|
|
if t.MaxWriteSize > 0 && t.BytesReceived.Load() > t.MaxWriteSize {
|
2022-01-30 10:42:36 +00:00
|
|
|
return t.Connection.GetQuotaExceededError()
|
|
|
|
}
|
|
|
|
if t.transferQuota.AllowedULSize == 0 && t.transferQuota.AllowedTotalSize == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if t.transferQuota.AllowedTotalSize > 0 {
|
2022-08-30 13:47:41 +00:00
|
|
|
if t.BytesSent.Load()+t.BytesReceived.Load() > t.transferQuota.AllowedTotalSize {
|
2022-01-30 10:42:36 +00:00
|
|
|
return t.Connection.GetQuotaExceededError()
|
|
|
|
}
|
|
|
|
} else if t.transferQuota.AllowedULSize > 0 {
|
2022-08-30 13:47:41 +00:00
|
|
|
if t.BytesReceived.Load() > t.transferQuota.AllowedULSize {
|
2022-01-30 10:42:36 +00:00
|
|
|
return t.Connection.GetQuotaExceededError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 11:54:36 +00:00
|
|
|
// Truncate changes the size of the opened file.
|
|
|
|
// Supported for local fs only
|
2020-08-22 08:12:00 +00:00
|
|
|
func (t *BaseTransfer) Truncate(fsPath string, size int64) (int64, error) {
|
2020-08-20 11:54:36 +00:00
|
|
|
if fsPath == t.GetFsPath() {
|
|
|
|
if t.File != nil {
|
2020-08-22 08:12:00 +00:00
|
|
|
initialSize := t.InitialSize
|
|
|
|
err := t.File.Truncate(size)
|
|
|
|
if err == nil {
|
|
|
|
t.Lock()
|
|
|
|
t.InitialSize = size
|
|
|
|
if t.MaxWriteSize > 0 {
|
|
|
|
sizeDiff := initialSize - size
|
|
|
|
t.MaxWriteSize += sizeDiff
|
2022-08-30 13:47:41 +00:00
|
|
|
metric.TransferCompleted(t.BytesSent.Load(), t.BytesReceived.Load(),
|
2022-06-14 17:37:25 +00:00
|
|
|
t.transferType, t.ErrTransfer, vfs.IsSFTPFs(t.Fs))
|
2022-01-31 18:30:25 +00:00
|
|
|
if t.transferQuota.HasSizeLimits() {
|
|
|
|
go func(ulSize, dlSize int64, user dataprovider.User) {
|
|
|
|
dataprovider.UpdateUserTransferQuota(&user, ulSize, dlSize, false) //nolint:errcheck
|
2022-08-30 13:47:41 +00:00
|
|
|
}(t.BytesReceived.Load(), t.BytesSent.Load(), t.Connection.User)
|
2022-01-31 18:30:25 +00:00
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
t.BytesReceived.Store(0)
|
2020-08-22 08:12:00 +00:00
|
|
|
}
|
|
|
|
t.Unlock()
|
|
|
|
}
|
2023-02-27 18:02:43 +00:00
|
|
|
t.Connection.Log(logger.LevelDebug, "file %q truncated to size %v max write size %v new initial size %v err: %v",
|
2020-08-22 08:12:00 +00:00
|
|
|
fsPath, size, t.MaxWriteSize, t.InitialSize, err)
|
|
|
|
return initialSize, err
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
if size == 0 && t.BytesSent.Load() == 0 {
|
2023-05-16 16:08:14 +00:00
|
|
|
// for cloud providers the file is always truncated to zero, we don't support append/resume for uploads.
|
|
|
|
// For buffered SFTP and local fs we can have buffered bytes so we returns an error
|
|
|
|
if !vfs.IsBufferedLocalOrSFTPFs(t.Fs) {
|
2021-04-03 14:00:55 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
2021-04-03 14:00:55 +00:00
|
|
|
return 0, vfs.ErrVfsUnsupported
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
2020-08-22 08:12:00 +00:00
|
|
|
return 0, errTransferMismatch
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// TransferError is called if there is an unexpected error.
|
|
|
|
// For example network or client issues
|
|
|
|
func (t *BaseTransfer) TransferError(err error) {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
if t.ErrTransfer != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.ErrTransfer = err
|
|
|
|
if t.cancelFn != nil {
|
|
|
|
t.cancelFn()
|
|
|
|
}
|
|
|
|
elapsed := time.Since(t.start).Nanoseconds() / 1000000
|
2023-02-27 18:02:43 +00:00
|
|
|
t.Connection.Log(logger.LevelError, "Unexpected error for transfer, path: %q, error: \"%v\" bytes sent: %v, "+
|
2022-08-30 13:47:41 +00:00
|
|
|
"bytes received: %v transfer running since %v ms", t.fsPath, t.ErrTransfer, t.BytesSent.Load(),
|
|
|
|
t.BytesReceived.Load(), elapsed)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 17:34:15 +00:00
|
|
|
func (t *BaseTransfer) getUploadFileSize() (int64, int, error) {
|
2020-12-05 12:48:13 +00:00
|
|
|
var fileSize int64
|
2022-10-10 17:34:15 +00:00
|
|
|
var deletedFiles int
|
|
|
|
|
2020-12-05 12:48:13 +00:00
|
|
|
info, err := t.Fs.Stat(t.fsPath)
|
|
|
|
if err == nil {
|
|
|
|
fileSize = info.Size()
|
|
|
|
}
|
2022-10-10 17:34:15 +00:00
|
|
|
if t.ErrTransfer != nil && vfs.IsCryptOsFs(t.Fs) {
|
2021-03-21 18:15:47 +00:00
|
|
|
errDelete := t.Fs.Remove(t.fsPath, false)
|
2020-12-05 12:48:13 +00:00
|
|
|
if errDelete != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
t.Connection.Log(logger.LevelWarn, "error removing partial crypto file %q: %v", t.fsPath, errDelete)
|
2022-10-10 17:34:15 +00:00
|
|
|
} else {
|
|
|
|
fileSize = 0
|
|
|
|
deletedFiles = 1
|
|
|
|
t.BytesReceived.Store(0)
|
|
|
|
t.MinWriteOffset = 0
|
2020-12-05 12:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 17:34:15 +00:00
|
|
|
return fileSize, deletedFiles, err
|
2020-12-05 12:48:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 11:48:56 +00:00
|
|
|
// return 1 if the file is outside the user home dir
|
2022-04-02 16:32:46 +00:00
|
|
|
func (t *BaseTransfer) checkUploadOutsideHomeDir(err error) int {
|
2022-04-03 11:48:56 +00:00
|
|
|
if err == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2024-05-27 17:35:48 +00:00
|
|
|
if t.ErrTransfer == nil {
|
|
|
|
t.ErrTransfer = err
|
|
|
|
}
|
2022-04-03 11:48:56 +00:00
|
|
|
if Config.TempPath == "" {
|
|
|
|
return 0
|
2022-04-02 16:32:46 +00:00
|
|
|
}
|
2022-04-03 11:48:56 +00:00
|
|
|
err = t.Fs.Remove(t.effectiveFsPath, false)
|
2023-02-27 18:02:43 +00:00
|
|
|
t.Connection.Log(logger.LevelWarn, "upload in temp path cannot be renamed, delete temporary file: %q, deletion error: %v",
|
2022-04-03 11:48:56 +00:00
|
|
|
t.effectiveFsPath, err)
|
|
|
|
// the file is outside the home dir so don't update the quota
|
2022-08-30 13:47:41 +00:00
|
|
|
t.BytesReceived.Store(0)
|
2022-04-03 11:48:56 +00:00
|
|
|
t.MinWriteOffset = 0
|
|
|
|
return 1
|
2022-04-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// Close it is called when the transfer is completed.
|
2020-08-11 21:56:10 +00:00
|
|
|
// It logs the transfer info, updates the user quota (for uploads)
|
|
|
|
// and executes any defined action.
|
2020-07-24 21:39:38 +00:00
|
|
|
// If there is an error no action will be executed and, in atomic mode,
|
|
|
|
// we try to delete the temporary file
|
|
|
|
func (t *BaseTransfer) Close() error {
|
|
|
|
defer t.Connection.RemoveTransfer(t)
|
|
|
|
|
|
|
|
var err error
|
2022-10-10 17:34:15 +00:00
|
|
|
numFiles := t.getUploadedFiles()
|
2022-08-30 13:47:41 +00:00
|
|
|
metric.TransferCompleted(t.BytesSent.Load(), t.BytesReceived.Load(),
|
2022-06-14 17:37:25 +00:00
|
|
|
t.transferType, t.ErrTransfer, vfs.IsSFTPFs(t.Fs))
|
2022-01-31 18:30:25 +00:00
|
|
|
if t.transferQuota.HasSizeLimits() {
|
2022-08-30 13:47:41 +00:00
|
|
|
dataprovider.UpdateUserTransferQuota(&t.Connection.User, t.BytesReceived.Load(), //nolint:errcheck
|
|
|
|
t.BytesSent.Load(), false)
|
2022-01-31 18:30:25 +00:00
|
|
|
}
|
2023-05-16 16:08:14 +00:00
|
|
|
if (t.File != nil || vfs.IsLocalOsFs(t.Fs)) && t.Connection.IsQuotaExceededError(t.ErrTransfer) {
|
2020-07-24 21:39:38 +00:00
|
|
|
// if quota is exceeded we try to remove the partial file for uploads to local filesystem
|
2023-05-16 16:08:14 +00:00
|
|
|
err = t.Fs.Remove(t.effectiveFsPath, false)
|
2020-07-24 21:39:38 +00:00
|
|
|
if err == nil {
|
2022-08-30 13:47:41 +00:00
|
|
|
t.BytesReceived.Store(0)
|
2020-07-24 21:39:38 +00:00
|
|
|
t.MinWriteOffset = 0
|
|
|
|
}
|
2023-02-27 18:02:43 +00:00
|
|
|
t.Connection.Log(logger.LevelWarn, "upload denied due to space limit, delete temporary file: %q, deletion error: %v",
|
2023-05-16 16:08:14 +00:00
|
|
|
t.effectiveFsPath, err)
|
|
|
|
} else if t.isAtomicUpload() {
|
2023-10-25 17:05:37 +00:00
|
|
|
if t.ErrTransfer == nil || Config.UploadMode&UploadModeAtomicWithResume != 0 {
|
2023-01-06 11:33:50 +00:00
|
|
|
_, _, err = t.Fs.Rename(t.effectiveFsPath, t.fsPath)
|
2023-02-27 18:02:43 +00:00
|
|
|
t.Connection.Log(logger.LevelDebug, "atomic upload completed, rename: %q -> %q, error: %v",
|
2021-05-31 19:45:29 +00:00
|
|
|
t.effectiveFsPath, t.fsPath, err)
|
2022-04-02 16:32:46 +00:00
|
|
|
// the file must be removed if it is uploaded to a path outside the home dir and cannot be renamed
|
2022-10-10 17:34:15 +00:00
|
|
|
t.checkUploadOutsideHomeDir(err)
|
2020-07-24 21:39:38 +00:00
|
|
|
} else {
|
2021-05-31 19:45:29 +00:00
|
|
|
err = t.Fs.Remove(t.effectiveFsPath, false)
|
2023-02-27 18:02:43 +00:00
|
|
|
t.Connection.Log(logger.LevelWarn, "atomic upload completed with error: \"%v\", delete temporary file: %q, deletion error: %v",
|
2022-04-02 16:32:46 +00:00
|
|
|
t.ErrTransfer, t.effectiveFsPath, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
if err == nil {
|
2022-08-30 13:47:41 +00:00
|
|
|
t.BytesReceived.Store(0)
|
2020-07-24 21:39:38 +00:00
|
|
|
t.MinWriteOffset = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elapsed := time.Since(t.start).Nanoseconds() / 1000000
|
2022-08-21 17:01:08 +00:00
|
|
|
var uploadFileSize int64
|
2020-07-24 21:39:38 +00:00
|
|
|
if t.transferType == TransferDownload {
|
2022-08-30 13:47:41 +00:00
|
|
|
logger.TransferLog(downloadLogSender, t.fsPath, elapsed, t.BytesSent.Load(), t.Connection.User.Username,
|
2024-05-27 17:35:48 +00:00
|
|
|
t.Connection.ID, t.Connection.protocol, t.Connection.localAddr, t.Connection.remoteAddr, t.ftpMode,
|
|
|
|
t.ErrTransfer)
|
2022-06-16 16:42:17 +00:00
|
|
|
ExecuteActionNotification(t.Connection, operationDownload, t.fsPath, t.requestPath, "", "", "", //nolint:errcheck
|
2023-08-12 16:51:47 +00:00
|
|
|
t.BytesSent.Load(), t.ErrTransfer, elapsed, t.metadata)
|
2020-07-24 21:39:38 +00:00
|
|
|
} else {
|
2022-10-10 17:34:15 +00:00
|
|
|
statSize, deletedFiles, errStat := t.getUploadFileSize()
|
|
|
|
if errStat == nil {
|
2022-08-21 17:01:08 +00:00
|
|
|
uploadFileSize = statSize
|
2022-10-10 17:34:15 +00:00
|
|
|
} else {
|
|
|
|
uploadFileSize = t.BytesReceived.Load() + t.MinWriteOffset
|
|
|
|
if t.Fs.IsNotExist(errStat) {
|
|
|
|
uploadFileSize = 0
|
|
|
|
numFiles--
|
|
|
|
}
|
2020-08-22 08:12:00 +00:00
|
|
|
}
|
2022-10-10 17:34:15 +00:00
|
|
|
numFiles -= deletedFiles
|
|
|
|
t.Connection.Log(logger.LevelDebug, "upload file size %d, num files %d, deleted files %d, fs path %q",
|
|
|
|
uploadFileSize, numFiles, deletedFiles, t.fsPath)
|
2023-02-12 17:56:53 +00:00
|
|
|
numFiles, uploadFileSize = t.executeUploadHook(numFiles, uploadFileSize, elapsed)
|
2022-08-21 17:01:08 +00:00
|
|
|
t.updateQuota(numFiles, uploadFileSize)
|
2021-11-26 18:00:44 +00:00
|
|
|
t.updateTimes()
|
2022-08-30 13:47:41 +00:00
|
|
|
logger.TransferLog(uploadLogSender, t.fsPath, elapsed, t.BytesReceived.Load(), t.Connection.User.Username,
|
2024-05-27 17:35:48 +00:00
|
|
|
t.Connection.ID, t.Connection.protocol, t.Connection.localAddr, t.Connection.remoteAddr, t.ftpMode,
|
|
|
|
t.ErrTransfer)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
if t.ErrTransfer != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
t.Connection.Log(logger.LevelError, "transfer error: %v, path: %q", t.ErrTransfer, t.fsPath)
|
2020-07-24 21:39:38 +00:00
|
|
|
if err == nil {
|
|
|
|
err = t.ErrTransfer
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 17:56:53 +00:00
|
|
|
t.updateTransferTimestamps(uploadFileSize, elapsed)
|
2020-07-24 21:39:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-16 16:08:14 +00:00
|
|
|
func (t *BaseTransfer) isAtomicUpload() bool {
|
|
|
|
return t.transferType == TransferUpload && t.effectiveFsPath != t.fsPath
|
|
|
|
}
|
|
|
|
|
2023-02-12 17:56:53 +00:00
|
|
|
func (t *BaseTransfer) updateTransferTimestamps(uploadFileSize, elapsed int64) {
|
2022-08-21 17:01:08 +00:00
|
|
|
if t.ErrTransfer != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if t.transferType == TransferUpload {
|
|
|
|
if t.Connection.User.FirstUpload == 0 && !t.Connection.uploadDone.Load() {
|
|
|
|
if err := dataprovider.UpdateUserTransferTimestamps(t.Connection.User.Username, true); err == nil {
|
|
|
|
t.Connection.uploadDone.Store(true)
|
|
|
|
ExecuteActionNotification(t.Connection, operationFirstUpload, t.fsPath, t.requestPath, "", //nolint:errcheck
|
2023-08-12 16:51:47 +00:00
|
|
|
"", "", uploadFileSize, t.ErrTransfer, elapsed, t.metadata)
|
2022-08-21 17:01:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
if t.Connection.User.FirstDownload == 0 && !t.Connection.downloadDone.Load() && t.BytesSent.Load() > 0 {
|
2022-08-21 17:01:08 +00:00
|
|
|
if err := dataprovider.UpdateUserTransferTimestamps(t.Connection.User.Username, false); err == nil {
|
|
|
|
t.Connection.downloadDone.Store(true)
|
|
|
|
ExecuteActionNotification(t.Connection, operationFirstDownload, t.fsPath, t.requestPath, "", //nolint:errcheck
|
2023-08-12 16:51:47 +00:00
|
|
|
"", "", t.BytesSent.Load(), t.ErrTransfer, elapsed, t.metadata)
|
2022-08-21 17:01:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 17:56:53 +00:00
|
|
|
func (t *BaseTransfer) executeUploadHook(numFiles int, fileSize, elapsed int64) (int, int64) {
|
2022-06-16 16:42:17 +00:00
|
|
|
err := ExecuteActionNotification(t.Connection, operationUpload, t.fsPath, t.requestPath, "", "", "",
|
2023-08-12 16:51:47 +00:00
|
|
|
fileSize, t.ErrTransfer, elapsed, t.metadata)
|
2022-06-16 16:42:17 +00:00
|
|
|
if err != nil {
|
|
|
|
if t.ErrTransfer == nil {
|
|
|
|
t.ErrTransfer = err
|
|
|
|
}
|
|
|
|
// try to remove the uploaded file
|
|
|
|
err = t.Fs.Remove(t.fsPath, false)
|
|
|
|
if err == nil {
|
|
|
|
numFiles--
|
|
|
|
fileSize = 0
|
2022-08-30 13:47:41 +00:00
|
|
|
t.BytesReceived.Store(0)
|
2022-06-16 16:42:17 +00:00
|
|
|
t.MinWriteOffset = 0
|
|
|
|
} else {
|
|
|
|
t.Connection.Log(logger.LevelWarn, "unable to remove path %q after upload hook failure: %v", t.fsPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return numFiles, fileSize
|
|
|
|
}
|
|
|
|
|
2022-10-10 17:34:15 +00:00
|
|
|
func (t *BaseTransfer) getUploadedFiles() int {
|
|
|
|
numFiles := 0
|
|
|
|
if t.isNewFile {
|
|
|
|
numFiles = 1
|
|
|
|
}
|
|
|
|
return numFiles
|
|
|
|
}
|
|
|
|
|
2021-11-26 18:00:44 +00:00
|
|
|
func (t *BaseTransfer) updateTimes() {
|
|
|
|
if !t.aTime.IsZero() && !t.mTime.IsZero() {
|
2024-02-17 18:49:34 +00:00
|
|
|
err := t.Fs.Chtimes(t.fsPath, t.aTime, t.mTime, false)
|
2023-02-27 18:02:43 +00:00
|
|
|
t.Connection.Log(logger.LevelDebug, "set times for file %q, atime: %v, mtime: %v, err: %v",
|
2021-11-26 18:00:44 +00:00
|
|
|
t.fsPath, t.aTime, t.mTime, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 08:12:00 +00:00
|
|
|
func (t *BaseTransfer) updateQuota(numFiles int, fileSize int64) bool {
|
2022-10-10 17:34:15 +00:00
|
|
|
// Uploads on some filesystem (S3 and similar) are atomic, if there is an error nothing is uploaded
|
|
|
|
if t.File == nil && t.ErrTransfer != nil && vfs.HasImplicitAtomicUploads(t.Fs) {
|
2020-07-24 21:39:38 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-08-22 08:12:00 +00:00
|
|
|
sizeDiff := fileSize - t.InitialSize
|
2022-10-10 17:34:15 +00:00
|
|
|
if t.transferType == TransferUpload && (numFiles != 0 || sizeDiff != 0) {
|
2020-07-24 21:39:38 +00:00
|
|
|
vfolder, err := t.Connection.User.GetVirtualFolderForPath(path.Dir(t.requestPath))
|
|
|
|
if err == nil {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateVirtualFolderQuota(&vfolder.BaseVirtualFolder, numFiles, //nolint:errcheck
|
2020-08-22 08:12:00 +00:00
|
|
|
sizeDiff, false)
|
2020-07-24 21:39:38 +00:00
|
|
|
if vfolder.IsIncludedInUserQuota() {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&t.Connection.User, numFiles, sizeDiff, false) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateUserQuota(&t.Connection.User, numFiles, sizeDiff, false) //nolint:errcheck
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleThrottle manage bandwidth throttling
|
|
|
|
func (t *BaseTransfer) HandleThrottle() {
|
|
|
|
var wantedBandwidth int64
|
|
|
|
var trasferredBytes int64
|
|
|
|
if t.transferType == TransferDownload {
|
|
|
|
wantedBandwidth = t.Connection.User.DownloadBandwidth
|
2022-08-30 13:47:41 +00:00
|
|
|
trasferredBytes = t.BytesSent.Load()
|
2020-07-24 21:39:38 +00:00
|
|
|
} else {
|
|
|
|
wantedBandwidth = t.Connection.User.UploadBandwidth
|
2022-08-30 13:47:41 +00:00
|
|
|
trasferredBytes = t.BytesReceived.Load()
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
if wantedBandwidth > 0 {
|
|
|
|
// real and wanted elapsed as milliseconds, bytes as kilobytes
|
|
|
|
realElapsed := time.Since(t.start).Nanoseconds() / 1000000
|
2021-01-17 21:29:08 +00:00
|
|
|
// trasferredBytes / 1024 = KB/s, we multiply for 1000 to get milliseconds
|
|
|
|
wantedElapsed := 1000 * (trasferredBytes / 1024) / wantedBandwidth
|
2020-07-24 21:39:38 +00:00
|
|
|
if wantedElapsed > realElapsed {
|
|
|
|
toSleep := time.Duration(wantedElapsed - realElapsed)
|
|
|
|
time.Sleep(toSleep * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|