2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 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-29 19:56:56 +00:00
|
|
|
package ftpd
|
|
|
|
|
|
|
|
import (
|
2020-08-03 16:03:09 +00:00
|
|
|
"errors"
|
2020-07-29 19:56:56 +00:00
|
|
|
"io"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2020-07-29 19:56:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// transfer contains the transfer details for an upload or a download.
|
|
|
|
// It implements the ftpserver.FileTransfer interface to handle files downloads and uploads
|
|
|
|
type transfer struct {
|
|
|
|
*common.BaseTransfer
|
2020-08-03 16:03:09 +00:00
|
|
|
writer io.WriteCloser
|
|
|
|
reader io.ReadCloser
|
|
|
|
isFinished bool
|
|
|
|
expectedOffset int64
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 16:51:47 +00:00
|
|
|
func newTransfer(baseTransfer *common.BaseTransfer, pipeWriter *vfs.PipeWriter, pipeReader *vfs.PipeReader,
|
2020-08-22 08:12:00 +00:00
|
|
|
expectedOffset int64) *transfer {
|
2020-07-29 19:56:56 +00:00
|
|
|
var writer io.WriteCloser
|
|
|
|
var reader io.ReadCloser
|
|
|
|
if baseTransfer.File != nil {
|
|
|
|
writer = baseTransfer.File
|
|
|
|
reader = baseTransfer.File
|
|
|
|
} else if pipeWriter != nil {
|
|
|
|
writer = pipeWriter
|
|
|
|
} else if pipeReader != nil {
|
|
|
|
reader = pipeReader
|
|
|
|
}
|
|
|
|
return &transfer{
|
2020-08-03 16:03:09 +00:00
|
|
|
BaseTransfer: baseTransfer,
|
|
|
|
writer: writer,
|
|
|
|
reader: reader,
|
|
|
|
isFinished: false,
|
|
|
|
expectedOffset: expectedOffset,
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads the contents to downloads.
|
|
|
|
func (t *transfer) Read(p []byte) (n int, err error) {
|
|
|
|
t.Connection.UpdateLastActivity()
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
n, err = t.reader.Read(p)
|
2022-08-30 13:47:41 +00:00
|
|
|
t.BytesSent.Add(int64(n))
|
2020-07-29 19:56:56 +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 {
|
|
|
|
t.TransferError(err)
|
2023-04-08 12:55:04 +00:00
|
|
|
err = t.ConvertError(err)
|
2020-10-25 07:18:48 +00:00
|
|
|
return
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
t.HandleThrottle()
|
2020-10-25 07:18:48 +00:00
|
|
|
return
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes the uploaded contents.
|
|
|
|
func (t *transfer) Write(p []byte) (n int, err error) {
|
|
|
|
t.Connection.UpdateLastActivity()
|
|
|
|
|
2020-10-25 07:18:48 +00:00
|
|
|
n, err = t.writer.Write(p)
|
2022-08-30 13:47:41 +00:00
|
|
|
t.BytesReceived.Add(int64(n))
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
if err == nil {
|
|
|
|
err = t.CheckWrite()
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.TransferError(err)
|
2023-04-08 12:55:04 +00:00
|
|
|
err = t.ConvertError(err)
|
2020-10-25 07:18:48 +00:00
|
|
|
return
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
t.HandleThrottle()
|
2020-10-25 07:18:48 +00:00
|
|
|
return
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seek sets the offset to resume an upload or a download
|
|
|
|
func (t *transfer) Seek(offset int64, whence int) (int64, error) {
|
2020-08-11 21:56:10 +00:00
|
|
|
t.Connection.UpdateLastActivity()
|
2020-07-29 19:56:56 +00:00
|
|
|
if t.File != nil {
|
2020-08-03 16:03:09 +00:00
|
|
|
ret, err := t.File.Seek(offset, whence)
|
|
|
|
if err != nil {
|
|
|
|
t.TransferError(err)
|
|
|
|
}
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
if t.reader != nil && t.expectedOffset == offset && whence == io.SeekStart {
|
|
|
|
return offset, nil
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2020-08-03 16:03:09 +00:00
|
|
|
t.TransferError(errors.New("seek is unsupported for this transfer"))
|
2020-07-29 19:56:56 +00:00
|
|
|
return 0, common.ErrOpUnsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close it is called when the transfer is completed.
|
|
|
|
func (t *transfer) Close() error {
|
|
|
|
if err := t.setFinished(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := t.closeIO()
|
|
|
|
errBaseClose := t.BaseTransfer.Close()
|
|
|
|
if errBaseClose != nil {
|
|
|
|
err = errBaseClose
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
return t.Connection.GetFsError(t.Fs, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *transfer) closeIO() error {
|
|
|
|
var err error
|
|
|
|
if t.File != nil {
|
|
|
|
err = t.File.Close()
|
|
|
|
} else if t.writer != nil {
|
|
|
|
err = t.writer.Close()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
t.Unlock()
|
|
|
|
} else if t.reader != nil {
|
|
|
|
err = t.reader.Close()
|
2023-08-12 16:51:47 +00:00
|
|
|
if metadater, ok := t.reader.(vfs.Metadater); ok {
|
|
|
|
t.BaseTransfer.SetMetadata(metadater.Metadata())
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *transfer) setFinished() error {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
if t.isFinished {
|
|
|
|
return common.ErrTransferClosed
|
|
|
|
}
|
|
|
|
t.isFinished = true
|
|
|
|
return nil
|
|
|
|
}
|