Skip to content

Commit 37da213

Browse files
committed
handle files: does not send an error if the transfer could not be open
if TransferOpen fails we already sent an error to the client, even before the previous commit. In handle dirs we don't send an error if TransferOpen fails, in handle files we could send an error instead, so check the error type.
1 parent 3a6cac7 commit 37da213

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

client_handler.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package ftpserver
33

44
import (
55
"bufio"
6-
"errors"
76
"fmt"
87
"io"
98
"net"
@@ -13,6 +12,14 @@ import (
1312
"github.com/fclairamb/ftpserverlib/log"
1413
)
1514

15+
type openTransferError struct {
16+
err string
17+
}
18+
19+
func (e *openTransferError) Error() string {
20+
return fmt.Sprintf("Unable to open transfer: %s", e.err)
21+
}
22+
1623
// nolint: maligned
1724
type clientHandler struct {
1825
id uint32 // ID of the client
@@ -254,16 +261,20 @@ func (c *clientHandler) writeMessage(code int, message string) {
254261

255262
func (c *clientHandler) TransferOpen() (net.Conn, error) {
256263
if c.transfer == nil {
257-
c.writeMessage(StatusActionNotTaken, "No passive connection declared")
258-
return nil, errors.New("no passive connection declared")
264+
err := &openTransferError{err: "No passive connection declared"}
265+
c.writeMessage(StatusActionNotTaken, err.Error())
266+
267+
return nil, err
259268
}
260269

261270
conn, err := c.transfer.Open()
262271
if err != nil {
263272
c.logger.Warn(
264273
"Unable to open transfer",
265274
"error", err)
266-
c.writeMessage(StatusCannotOpenDataConnection, fmt.Sprintf("Unable to open transfer: %v", err))
275+
276+
err = &openTransferError{err: err.Error()}
277+
c.writeMessage(StatusCannotOpenDataConnection, err.Error())
267278

268279
return conn, err
269280
}

handle_files.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func (c *clientHandler) handleRETR() error {
3232
// To make sure we don't miss any step, we execute everything in order
3333
func (c *clientHandler) transferFile(write bool, append bool) {
3434
var file afero.File
35-
3635
var err error
3736

3837
path := c.absPath(c.param)
@@ -77,9 +76,7 @@ func (c *clientHandler) transferFile(write bool, append bool) {
7776
defer c.TransferClose()
7877

7978
// Copy the data
80-
8179
var in io.Reader
82-
8380
var out io.Writer
8481

8582
if write { // ... from the connection to the file
@@ -107,8 +104,11 @@ func (c *clientHandler) transferFile(write bool, append bool) {
107104
}
108105

109106
if err != nil {
110-
c.writeMessage(StatusActionNotTaken, "Could not transfer file: "+err.Error())
111-
return
107+
// if the transfer could not be open we already sent an error
108+
if _, isOpenTransferErr := err.(*openTransferError); !isOpenTransferErr {
109+
c.writeMessage(StatusActionNotTaken, "Could not transfer file: "+err.Error())
110+
return
111+
}
112112
}
113113
}
114114

0 commit comments

Comments
 (0)