transfer.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (C) 2019-2022 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package ftpd
  15. import (
  16. "errors"
  17. "io"
  18. "sync/atomic"
  19. "github.com/eikenb/pipeat"
  20. "github.com/drakkan/sftpgo/v2/common"
  21. "github.com/drakkan/sftpgo/v2/vfs"
  22. )
  23. // transfer contains the transfer details for an upload or a download.
  24. // It implements the ftpserver.FileTransfer interface to handle files downloads and uploads
  25. type transfer struct {
  26. *common.BaseTransfer
  27. writer io.WriteCloser
  28. reader io.ReadCloser
  29. isFinished bool
  30. expectedOffset int64
  31. }
  32. func newTransfer(baseTransfer *common.BaseTransfer, pipeWriter *vfs.PipeWriter, pipeReader *pipeat.PipeReaderAt,
  33. expectedOffset int64) *transfer {
  34. var writer io.WriteCloser
  35. var reader io.ReadCloser
  36. if baseTransfer.File != nil {
  37. writer = baseTransfer.File
  38. reader = baseTransfer.File
  39. } else if pipeWriter != nil {
  40. writer = pipeWriter
  41. } else if pipeReader != nil {
  42. reader = pipeReader
  43. }
  44. return &transfer{
  45. BaseTransfer: baseTransfer,
  46. writer: writer,
  47. reader: reader,
  48. isFinished: false,
  49. expectedOffset: expectedOffset,
  50. }
  51. }
  52. // Read reads the contents to downloads.
  53. func (t *transfer) Read(p []byte) (n int, err error) {
  54. t.Connection.UpdateLastActivity()
  55. n, err = t.reader.Read(p)
  56. atomic.AddInt64(&t.BytesSent, int64(n))
  57. if err == nil {
  58. err = t.CheckRead()
  59. }
  60. if err != nil && err != io.EOF {
  61. t.TransferError(err)
  62. return
  63. }
  64. t.HandleThrottle()
  65. return
  66. }
  67. // Write writes the uploaded contents.
  68. func (t *transfer) Write(p []byte) (n int, err error) {
  69. t.Connection.UpdateLastActivity()
  70. n, err = t.writer.Write(p)
  71. atomic.AddInt64(&t.BytesReceived, int64(n))
  72. if err == nil {
  73. err = t.CheckWrite()
  74. }
  75. if err != nil {
  76. t.TransferError(err)
  77. return
  78. }
  79. t.HandleThrottle()
  80. return
  81. }
  82. // Seek sets the offset to resume an upload or a download
  83. func (t *transfer) Seek(offset int64, whence int) (int64, error) {
  84. t.Connection.UpdateLastActivity()
  85. if t.File != nil {
  86. ret, err := t.File.Seek(offset, whence)
  87. if err != nil {
  88. t.TransferError(err)
  89. }
  90. return ret, err
  91. }
  92. if t.reader != nil && t.expectedOffset == offset && whence == io.SeekStart {
  93. return offset, nil
  94. }
  95. t.TransferError(errors.New("seek is unsupported for this transfer"))
  96. return 0, common.ErrOpUnsupported
  97. }
  98. // Close it is called when the transfer is completed.
  99. func (t *transfer) Close() error {
  100. if err := t.setFinished(); err != nil {
  101. return err
  102. }
  103. err := t.closeIO()
  104. errBaseClose := t.BaseTransfer.Close()
  105. if errBaseClose != nil {
  106. err = errBaseClose
  107. }
  108. return t.Connection.GetFsError(t.Fs, err)
  109. }
  110. func (t *transfer) closeIO() error {
  111. var err error
  112. if t.File != nil {
  113. err = t.File.Close()
  114. } else if t.writer != nil {
  115. err = t.writer.Close()
  116. t.Lock()
  117. // we set ErrTransfer here so quota is not updated, in this case the uploads are atomic
  118. if err != nil && t.ErrTransfer == nil {
  119. t.ErrTransfer = err
  120. }
  121. t.Unlock()
  122. } else if t.reader != nil {
  123. err = t.reader.Close()
  124. }
  125. return err
  126. }
  127. func (t *transfer) setFinished() error {
  128. t.Lock()
  129. defer t.Unlock()
  130. if t.isFinished {
  131. return common.ErrTransferClosed
  132. }
  133. t.isFinished = true
  134. return nil
  135. }