2019-07-20 10:26:52 +00:00
|
|
|
package sftpd
|
|
|
|
|
|
|
|
import (
|
2019-10-09 15:33:30 +00:00
|
|
|
"fmt"
|
2019-11-26 21:26:42 +00:00
|
|
|
"io"
|
2020-07-24 21:39:38 +00:00
|
|
|
"sync/atomic"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/eikenb/pipeat"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/common"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/metric"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/vfs"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
type writerAtCloser interface {
|
|
|
|
io.WriterAt
|
|
|
|
io.Closer
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
type readerAtCloser interface {
|
|
|
|
io.ReaderAt
|
|
|
|
io.Closer
|
|
|
|
}
|
2020-01-12 07:25:08 +00:00
|
|
|
|
2020-08-31 04:45:22 +00:00
|
|
|
type failingReader struct {
|
|
|
|
innerReader readerAtCloser
|
|
|
|
errRead error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *failingReader) ReadAt(p []byte, off int64) (n int, err error) {
|
|
|
|
return 0, r.errRead
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *failingReader) Close() error {
|
2020-11-09 20:01:56 +00:00
|
|
|
if r.innerReader == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-31 04:45:22 +00:00
|
|
|
return r.innerReader.Close()
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
// transfer defines the transfer details.
|
|
|
|
// It implements the io.ReaderAt and io.WriterAt interfaces to handle SFTP downloads and uploads
|
|
|
|
type transfer struct {
|
|
|
|
*common.BaseTransfer
|
2020-08-22 08:12:00 +00:00
|
|
|
writerAt writerAtCloser
|
|
|
|
readerAt readerAtCloser
|
|
|
|
isFinished bool
|
2019-09-07 21:10:20 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 04:45:22 +00:00
|
|
|
func newTransfer(baseTransfer *common.BaseTransfer, pipeWriter *vfs.PipeWriter, pipeReader *pipeat.PipeReaderAt,
|
|
|
|
errForRead error) *transfer {
|
2020-07-24 21:39:38 +00:00
|
|
|
var writer writerAtCloser
|
|
|
|
var reader readerAtCloser
|
|
|
|
if baseTransfer.File != nil {
|
|
|
|
writer = baseTransfer.File
|
2020-08-31 04:45:22 +00:00
|
|
|
if errForRead == nil {
|
|
|
|
reader = baseTransfer.File
|
|
|
|
} else {
|
|
|
|
reader = &failingReader{
|
|
|
|
innerReader: baseTransfer.File,
|
|
|
|
errRead: errForRead,
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
} else if pipeWriter != nil {
|
|
|
|
writer = pipeWriter
|
|
|
|
} else if pipeReader != nil {
|
2020-08-31 04:45:22 +00:00
|
|
|
if errForRead == nil {
|
|
|
|
reader = pipeReader
|
|
|
|
} else {
|
|
|
|
reader = &failingReader{
|
|
|
|
innerReader: pipeReader,
|
|
|
|
errRead: errForRead,
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2020-11-09 20:01:56 +00:00
|
|
|
if baseTransfer.File == nil && errForRead != nil && pipeReader == nil {
|
|
|
|
reader = &failingReader{
|
|
|
|
innerReader: nil,
|
|
|
|
errRead: errForRead,
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
return &transfer{
|
|
|
|
BaseTransfer: baseTransfer,
|
|
|
|
writerAt: writer,
|
|
|
|
readerAt: reader,
|
|
|
|
isFinished: false,
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// ReadAt reads len(p) bytes from the File to download starting at byte offset off and updates the bytes sent.
|
|
|
|
// It handles download bandwidth throttling too
|
2020-07-24 21:39:38 +00:00
|
|
|
func (t *transfer) ReadAt(p []byte, off int64) (n int, err error) {
|
|
|
|
t.Connection.UpdateLastActivity()
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
n, err = t.readerAt.ReadAt(p, off)
|
|
|
|
atomic.AddInt64(&t.BytesSent, int64(n))
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
if err == nil {
|
|
|
|
err = t.CheckRead()
|
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil && err != io.EOF {
|
2020-08-31 17:25:17 +00:00
|
|
|
if t.GetType() == common.TransferDownload {
|
2020-10-25 07:18:48 +00:00
|
|
|
t.TransferError(err)
|
2020-08-31 17:25:17 +00:00
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
return
|
2020-01-10 18:20:22 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
t.HandleThrottle()
|
2020-10-25 07:18:48 +00:00
|
|
|
return
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// WriteAt writes len(p) bytes to the uploaded file starting at byte offset off and updates the bytes received.
|
|
|
|
// It handles upload bandwidth throttling too
|
2020-07-24 21:39:38 +00:00
|
|
|
func (t *transfer) WriteAt(p []byte, off int64) (n int, err error) {
|
|
|
|
t.Connection.UpdateLastActivity()
|
|
|
|
if off < t.MinWriteOffset {
|
2021-03-21 18:15:47 +00:00
|
|
|
err := fmt.Errorf("invalid write offset: %v minimum valid value: %v", off, t.MinWriteOffset)
|
2020-01-10 18:20:22 +00:00
|
|
|
t.TransferError(err)
|
|
|
|
return 0, err
|
2019-10-09 15:33:30 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
n, err = t.writerAt.WriteAt(p, off)
|
|
|
|
atomic.AddInt64(&t.BytesReceived, int64(n))
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
if err == nil {
|
|
|
|
err = t.CheckWrite()
|
2020-06-18 20:38:03 +00:00
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.TransferError(err)
|
|
|
|
return
|
2020-01-10 18:20:22 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
t.HandleThrottle()
|
2020-10-25 07:18:48 +00:00
|
|
|
return
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Close it is called when the transfer is completed.
|
2020-01-09 11:00:37 +00:00
|
|
|
// It closes the underlying file, logs the transfer info, updates the user quota (for uploads)
|
|
|
|
// and executes any defined action.
|
2019-09-10 16:47:21 +00:00
|
|
|
// If there is an error no action will be executed and, in atomic mode, we try to delete
|
|
|
|
// the temporary file
|
2020-07-24 21:39:38 +00:00
|
|
|
func (t *transfer) Close() error {
|
|
|
|
if err := t.setFinished(); err != nil {
|
|
|
|
return err
|
2019-09-07 21:10:20 +00:00
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
err := t.closeIO()
|
2020-07-24 21:39:38 +00:00
|
|
|
errBaseClose := t.BaseTransfer.Close()
|
|
|
|
if errBaseClose != nil {
|
|
|
|
err = errBaseClose
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return t.Connection.GetFsError(t.Fs, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
func (t *transfer) closeIO() error {
|
2020-01-19 06:41:05 +00:00
|
|
|
var err error
|
2020-07-24 21:39:38 +00:00
|
|
|
if t.File != nil {
|
|
|
|
err = t.File.Close()
|
|
|
|
} else if t.writerAt != nil {
|
2020-01-19 06:41:05 +00:00
|
|
|
err = t.writerAt.Close()
|
2020-07-24 21:39:38 +00:00
|
|
|
t.Lock()
|
|
|
|
// we set ErrTransfer here so quota is not updated, in this case the uploads are atomic
|
|
|
|
if err != nil && t.ErrTransfer == nil {
|
|
|
|
t.ErrTransfer = err
|
2020-05-19 17:17:43 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
t.Unlock()
|
2020-01-19 06:41:05 +00:00
|
|
|
} else if t.readerAt != nil {
|
|
|
|
err = t.readerAt.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
func (t *transfer) setFinished() error {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
if t.isFinished {
|
|
|
|
return common.ErrTransferClosed
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
t.isFinished = true
|
|
|
|
return nil
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
|
|
|
|
// used for ssh commands.
|
|
|
|
// It reads from src until EOF so it does not treat an EOF from Read as an error to be reported.
|
|
|
|
// EOF from Write is reported as error
|
2020-07-24 21:39:38 +00:00
|
|
|
func (t *transfer) copyFromReaderToWriter(dst io.Writer, src io.Reader) (int64, error) {
|
|
|
|
defer t.Connection.RemoveTransfer(t)
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
var written int64
|
|
|
|
var err error
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2020-08-22 08:12:00 +00:00
|
|
|
if t.MaxWriteSize < 0 {
|
2020-07-24 21:39:38 +00:00
|
|
|
return 0, common.ErrQuotaExceeded
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
isDownload := t.GetType() == common.TransferDownload
|
2019-11-26 21:26:42 +00:00
|
|
|
buf := make([]byte, 32768)
|
|
|
|
for {
|
2020-07-24 21:39:38 +00:00
|
|
|
t.Connection.UpdateLastActivity()
|
2019-11-26 21:26:42 +00:00
|
|
|
nr, er := src.Read(buf)
|
|
|
|
if nr > 0 {
|
|
|
|
nw, ew := dst.Write(buf[0:nr])
|
|
|
|
if nw > 0 {
|
|
|
|
written += int64(nw)
|
2020-07-24 21:39:38 +00:00
|
|
|
if isDownload {
|
|
|
|
atomic.StoreInt64(&t.BytesSent, written)
|
2022-01-30 10:42:36 +00:00
|
|
|
if errCheck := t.CheckRead(); errCheck != nil {
|
|
|
|
err = errCheck
|
|
|
|
break
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
} else {
|
2020-07-24 21:39:38 +00:00
|
|
|
atomic.StoreInt64(&t.BytesReceived, written)
|
2022-01-30 10:42:36 +00:00
|
|
|
if errCheck := t.CheckWrite(); errCheck != nil {
|
|
|
|
err = errCheck
|
|
|
|
break
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ew != nil {
|
|
|
|
err = ew
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nr != nw {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
if er != io.EOF {
|
|
|
|
err = er
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
t.HandleThrottle()
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
t.ErrTransfer = err
|
|
|
|
if written > 0 || err != nil {
|
2022-06-14 17:37:25 +00:00
|
|
|
metric.TransferCompleted(atomic.LoadInt64(&t.BytesSent), atomic.LoadInt64(&t.BytesReceived), t.GetType(),
|
|
|
|
t.ErrTransfer, vfs.IsSFTPFs(t.Fs))
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
return written, err
|
|
|
|
}
|