2020-07-24 21:39:38 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/metrics"
|
2020-08-22 08:12:00 +00:00
|
|
|
"github.com/drakkan/sftpgo/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
|
2020-07-24 21:39:38 +00:00
|
|
|
ID uint64
|
2020-12-14 13:52:36 +00:00
|
|
|
BytesSent int64
|
|
|
|
BytesReceived int64
|
2020-08-22 08:12:00 +00:00
|
|
|
Fs vfs.Fs
|
2020-11-17 18:36:39 +00:00
|
|
|
File vfs.File
|
2020-07-24 21:39:38 +00:00
|
|
|
Connection *BaseConnection
|
|
|
|
cancelFn func()
|
|
|
|
fsPath string
|
2020-12-14 13:52:36 +00:00
|
|
|
requestPath string
|
2020-07-24 21:39:38 +00:00
|
|
|
start time.Time
|
2020-12-14 13:52:36 +00:00
|
|
|
MaxWriteSize int64
|
2020-07-24 21:39:38 +00:00
|
|
|
MinWriteOffset int64
|
|
|
|
InitialSize int64
|
|
|
|
isNewFile bool
|
2020-12-14 13:52:36 +00:00
|
|
|
transferType int
|
2020-08-11 21:56:10 +00:00
|
|
|
AbortTransfer int32
|
2020-07-24 21:39:38 +00:00
|
|
|
sync.Mutex
|
|
|
|
ErrTransfer error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBaseTransfer returns a new BaseTransfer and adds it to the given connection
|
2020-11-17 18:36:39 +00:00
|
|
|
func NewBaseTransfer(file vfs.File, conn *BaseConnection, cancelFn func(), fsPath, requestPath string, transferType int,
|
2020-08-22 08:12:00 +00:00
|
|
|
minWriteOffset, initialSize, maxWriteSize int64, isNewFile bool, fs vfs.Fs) *BaseTransfer {
|
2020-07-24 21:39:38 +00:00
|
|
|
t := &BaseTransfer{
|
|
|
|
ID: conn.GetTransferID(),
|
|
|
|
File: file,
|
|
|
|
Connection: conn,
|
|
|
|
cancelFn: cancelFn,
|
|
|
|
fsPath: fsPath,
|
|
|
|
start: time.Now(),
|
|
|
|
transferType: transferType,
|
|
|
|
MinWriteOffset: minWriteOffset,
|
|
|
|
InitialSize: initialSize,
|
|
|
|
isNewFile: isNewFile,
|
|
|
|
requestPath: requestPath,
|
|
|
|
BytesSent: 0,
|
|
|
|
BytesReceived: 0,
|
2020-08-22 08:12:00 +00:00
|
|
|
MaxWriteSize: maxWriteSize,
|
2020-08-11 21:56:10 +00:00
|
|
|
AbortTransfer: 0,
|
2020-08-22 08:12:00 +00:00
|
|
|
Fs: fs,
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
conn.AddTransfer(t)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetID returns the transfer ID
|
|
|
|
func (t *BaseTransfer) GetID() uint64 {
|
|
|
|
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 {
|
|
|
|
return atomic.LoadInt64(&t.BytesSent)
|
|
|
|
}
|
|
|
|
return atomic.LoadInt64(&t.BytesReceived)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStartTime returns the start time
|
|
|
|
func (t *BaseTransfer) GetStartTime() time.Time {
|
|
|
|
return t.start
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
// SignalClose signals that the transfer should be closed.
|
|
|
|
// For same protocols, for example WebDAV, we have no
|
|
|
|
// access to the network connection, so we use this method
|
|
|
|
// to make the next read or write to fail
|
|
|
|
func (t *BaseTransfer) SignalClose() {
|
|
|
|
atomic.StoreInt32(&(t.AbortTransfer), 1)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
if t.File != nil {
|
|
|
|
return t.File.Name()
|
|
|
|
}
|
|
|
|
return t.fsPath
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
metrics.TransferCompleted(atomic.LoadInt64(&t.BytesSent), atomic.LoadInt64(&t.BytesReceived), t.transferType, t.ErrTransfer)
|
|
|
|
atomic.StoreInt64(&t.BytesReceived, 0)
|
|
|
|
}
|
|
|
|
t.Unlock()
|
|
|
|
}
|
|
|
|
t.Connection.Log(logger.LevelDebug, "file %#v truncated to size %v max write size %v new initial size %v err: %v",
|
|
|
|
fsPath, size, t.MaxWriteSize, t.InitialSize, err)
|
|
|
|
return initialSize, err
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
2020-08-22 08:12:00 +00:00
|
|
|
if size == 0 && atomic.LoadInt64(&t.BytesSent) == 0 {
|
2020-08-20 11:54:36 +00:00
|
|
|
// for cloud providers the file is always truncated to zero, we don't support append/resume for uploads
|
2021-04-03 14:00:55 +00:00
|
|
|
// for buffered SFTP we can have buffered bytes so we returns an error
|
|
|
|
if !vfs.IsBufferedSFTPFs(t.Fs) {
|
|
|
|
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
|
|
|
|
t.Connection.Log(logger.LevelWarn, "Unexpected error for transfer, path: %#v, error: \"%v\" bytes sent: %v, "+
|
|
|
|
"bytes received: %v transfer running since %v ms", t.fsPath, t.ErrTransfer, atomic.LoadInt64(&t.BytesSent),
|
|
|
|
atomic.LoadInt64(&t.BytesReceived), elapsed)
|
|
|
|
}
|
|
|
|
|
2020-12-05 12:48:13 +00:00
|
|
|
func (t *BaseTransfer) getUploadFileSize() (int64, error) {
|
|
|
|
var fileSize int64
|
|
|
|
info, err := t.Fs.Stat(t.fsPath)
|
|
|
|
if err == nil {
|
|
|
|
fileSize = info.Size()
|
|
|
|
}
|
|
|
|
if vfs.IsCryptOsFs(t.Fs) && t.ErrTransfer != nil {
|
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 {
|
|
|
|
t.Connection.Log(logger.LevelWarn, "error removing partial crypto file %#v: %v", t.fsPath, errDelete)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fileSize, err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
numFiles := 0
|
|
|
|
if t.isNewFile {
|
|
|
|
numFiles = 1
|
|
|
|
}
|
|
|
|
metrics.TransferCompleted(atomic.LoadInt64(&t.BytesSent), atomic.LoadInt64(&t.BytesReceived), t.transferType, t.ErrTransfer)
|
|
|
|
if t.ErrTransfer == ErrQuotaExceeded && t.File != nil {
|
|
|
|
// if quota is exceeded we try to remove the partial file for uploads to local filesystem
|
2021-03-21 18:15:47 +00:00
|
|
|
err = t.Fs.Remove(t.File.Name(), false)
|
2020-07-24 21:39:38 +00:00
|
|
|
if err == nil {
|
|
|
|
numFiles--
|
|
|
|
atomic.StoreInt64(&t.BytesReceived, 0)
|
|
|
|
t.MinWriteOffset = 0
|
|
|
|
}
|
|
|
|
t.Connection.Log(logger.LevelWarn, "upload denied due to space limit, delete temporary file: %#v, deletion error: %v",
|
|
|
|
t.File.Name(), err)
|
|
|
|
} else if t.transferType == TransferUpload && t.File != nil && t.File.Name() != t.fsPath {
|
|
|
|
if t.ErrTransfer == nil || Config.UploadMode == UploadModeAtomicWithResume {
|
2021-03-21 18:15:47 +00:00
|
|
|
err = t.Fs.Rename(t.File.Name(), t.fsPath)
|
2020-07-24 21:39:38 +00:00
|
|
|
t.Connection.Log(logger.LevelDebug, "atomic upload completed, rename: %#v -> %#v, error: %v",
|
|
|
|
t.File.Name(), t.fsPath, err)
|
|
|
|
} else {
|
2021-03-21 18:15:47 +00:00
|
|
|
err = t.Fs.Remove(t.File.Name(), false)
|
2020-07-24 21:39:38 +00:00
|
|
|
t.Connection.Log(logger.LevelWarn, "atomic upload completed with error: \"%v\", delete temporary file: %#v, "+
|
|
|
|
"deletion error: %v", t.ErrTransfer, t.File.Name(), err)
|
|
|
|
if err == nil {
|
|
|
|
numFiles--
|
|
|
|
atomic.StoreInt64(&t.BytesReceived, 0)
|
|
|
|
t.MinWriteOffset = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elapsed := time.Since(t.start).Nanoseconds() / 1000000
|
|
|
|
if t.transferType == TransferDownload {
|
|
|
|
logger.TransferLog(downloadLogSender, t.fsPath, elapsed, atomic.LoadInt64(&t.BytesSent), t.Connection.User.Username,
|
|
|
|
t.Connection.ID, t.Connection.protocol)
|
2021-05-11 10:45:14 +00:00
|
|
|
ExecuteActionNotification(&t.Connection.User, operationDownload, t.fsPath, "", "", t.Connection.protocol,
|
2020-07-24 21:39:38 +00:00
|
|
|
atomic.LoadInt64(&t.BytesSent), t.ErrTransfer)
|
|
|
|
} else {
|
2020-08-22 08:12:00 +00:00
|
|
|
fileSize := atomic.LoadInt64(&t.BytesReceived) + t.MinWriteOffset
|
2020-12-05 12:48:13 +00:00
|
|
|
if statSize, err := t.getUploadFileSize(); err == nil {
|
|
|
|
fileSize = statSize
|
2020-08-22 08:12:00 +00:00
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
t.Connection.Log(logger.LevelDebug, "uploaded file size %v", fileSize)
|
2020-08-22 08:12:00 +00:00
|
|
|
t.updateQuota(numFiles, fileSize)
|
2020-07-24 21:39:38 +00:00
|
|
|
logger.TransferLog(uploadLogSender, t.fsPath, elapsed, atomic.LoadInt64(&t.BytesReceived), t.Connection.User.Username,
|
|
|
|
t.Connection.ID, t.Connection.protocol)
|
2021-05-11 10:45:14 +00:00
|
|
|
ExecuteActionNotification(&t.Connection.User, operationUpload, t.fsPath, "", "", t.Connection.protocol, fileSize,
|
|
|
|
t.ErrTransfer)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
if t.ErrTransfer != nil {
|
|
|
|
t.Connection.Log(logger.LevelWarn, "transfer error: %v, path: %#v", t.ErrTransfer, t.fsPath)
|
|
|
|
if err == nil {
|
|
|
|
err = t.ErrTransfer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-22 08:12:00 +00:00
|
|
|
func (t *BaseTransfer) updateQuota(numFiles int, fileSize int64) bool {
|
2020-07-24 21:39:38 +00:00
|
|
|
// S3 uploads are atomic, if there is an error nothing is uploaded
|
|
|
|
if t.File == nil && t.ErrTransfer != nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-08-22 08:12:00 +00:00
|
|
|
sizeDiff := fileSize - t.InitialSize
|
|
|
|
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
|
|
|
|
trasferredBytes = atomic.LoadInt64(&t.BytesSent)
|
|
|
|
} else {
|
|
|
|
wantedBandwidth = t.Connection.User.UploadBandwidth
|
|
|
|
trasferredBytes = atomic.LoadInt64(&t.BytesReceived)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|