mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-07 14:50:55 +03:00
sftpd: add support for upload resume
we support resume only if the client sets the correct offset while resuming the upload. Based on the specs the offset is optional for resume, but all the tested clients sets a right offset. If an invalid offset is given we interrupt the transfer with the error "Invalid write offset ..." See https://github.com/pkg/sftp/issues/295 This commit add a new upload mode: "atomic with resume support", this acts as atomic but if there is an upload error the temporary file is renamed to the requested path and not deleted, this way a client can reconnect and resume the upload
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package sftpd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -14,27 +15,23 @@ const (
|
||||
transferDownload
|
||||
)
|
||||
|
||||
const (
|
||||
uploadModeStandard = iota
|
||||
uploadModeAtomic
|
||||
)
|
||||
|
||||
// Transfer contains the transfer details for an upload or a download.
|
||||
// It implements the io Reader and Writer interface to handle files downloads and uploads
|
||||
type Transfer struct {
|
||||
file *os.File
|
||||
path string
|
||||
start time.Time
|
||||
bytesSent int64
|
||||
bytesReceived int64
|
||||
user dataprovider.User
|
||||
connectionID string
|
||||
transferType int
|
||||
lastActivity time.Time
|
||||
isNewFile bool
|
||||
protocol string
|
||||
transferError error
|
||||
isFinished bool
|
||||
file *os.File
|
||||
path string
|
||||
start time.Time
|
||||
bytesSent int64
|
||||
bytesReceived int64
|
||||
user dataprovider.User
|
||||
connectionID string
|
||||
transferType int
|
||||
lastActivity time.Time
|
||||
isNewFile bool
|
||||
protocol string
|
||||
transferError error
|
||||
isFinished bool
|
||||
minWriteOffset int64
|
||||
}
|
||||
|
||||
// TransferError is called if there is an unexpected error.
|
||||
@@ -60,6 +57,10 @@ func (t *Transfer) ReadAt(p []byte, off int64) (n int, err error) {
|
||||
// It handles upload bandwidth throttling too
|
||||
func (t *Transfer) WriteAt(p []byte, off int64) (n int, err error) {
|
||||
t.lastActivity = time.Now()
|
||||
if off < t.minWriteOffset {
|
||||
logger.Warn(logSender, t.connectionID, "Invalid write offset %v minimum valid value %v", off, t.minWriteOffset)
|
||||
return 0, fmt.Errorf("Invalid write offset %v", off)
|
||||
}
|
||||
written, e := t.file.WriteAt(p, off)
|
||||
t.bytesReceived += int64(written)
|
||||
t.handleThrottle()
|
||||
@@ -82,7 +83,7 @@ func (t *Transfer) Close() error {
|
||||
numFiles = 1
|
||||
}
|
||||
if t.transferType == transferUpload && t.file.Name() != t.path {
|
||||
if t.transferError == nil {
|
||||
if t.transferError == nil || uploadMode == uploadModeAtomicWithResume {
|
||||
err = os.Rename(t.file.Name(), t.path)
|
||||
logger.Debug(logSender, t.connectionID, "atomic upload completed, rename: %#v -> %#v, error: %v",
|
||||
t.file.Name(), t.path, err)
|
||||
|
||||
Reference in New Issue
Block a user