Skip to content

Commit

Permalink
fix(MLST): Prefix MLST entries with a space (#353)
Browse files Browse the repository at this point in the history
* Prefix MLST entries with a space
* Fix lint issue in handleMLST
* Rename ẁriteMLSxOutput to writeMLSxEntry

Signed-off-by: Thomas Hallgren <thomas@datawire.io>
  • Loading branch information
thallgren committed Aug 21, 2022
1 parent 3af5d5d commit ba66bb2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
4 changes: 2 additions & 2 deletions handle_dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,14 @@ func (c *clientHandler) dirTransferMLSD(w io.Writer, files []os.FileInfo) error
}

for _, file := range files {
if err := c.writeMLSxOutput(w, file); err != nil {
if err := c.writeMLSxEntry(w, file); err != nil {
return err
}
}

return nil
}
func (c *clientHandler) writeMLSxOutput(w io.Writer, file os.FileInfo) error {
func (c *clientHandler) writeMLSxEntry(w io.Writer, file os.FileInfo) error {
var listType string
if file.IsDir() {
listType = "dir"
Expand Down
11 changes: 7 additions & 4 deletions handle_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,17 +466,20 @@ func (c *clientHandler) handleMLST(param string) error {

path := c.absPath(param)

if info, err := c.driver.Stat(path); err == nil {
info, err := c.driver.Stat(path)
if err == nil {
defer c.multilineAnswer(StatusFileOK, "File details")()

if errWrite := c.writeMLSxOutput(c.writer, info); errWrite != nil {
return errWrite
// Each MLSx entry must start with a space when returned in a multiline answer
if err = c.writer.WriteByte(' '); err == nil {
err = c.writeMLSxEntry(c.writer, info)
}
} else {
c.writeMessage(StatusActionNotTaken, fmt.Sprintf("Could not list: %v", err))
err = nil
}

return nil
return err
}

func (c *clientHandler) handleALLO(param string) error {
Expand Down
34 changes: 34 additions & 0 deletions handle_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,40 @@ func TestSTATFile(t *testing.T) {
require.Equal(t, StatusFileActionNotTaken, rc)
}

func TestMLST(t *testing.T) {
s := NewTestServer(t, true)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")

defer func() { panicOnError(c.Close()) }()

// Creating a tiny file
ftpUpload(t, c, createTemporaryFile(t, 10), "file")

raw, err := c.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")

defer func() { require.NoError(t, raw.Close()) }()

rc, rsp, err := raw.SendCommand("MLST file")
require.NoError(t, err)
require.Equal(t, StatusFileOK, rc)

lines := strings.Split(rsp, "\n")
require.Len(t, lines, 3)
path := validMLSxEntryPattern.FindStringSubmatch(lines[1] + "\r\n")

if len(path) != 2 {
t.Errorf("Valid MLST response example did not pass validation: \"%s\"", lines[1])
} else if path[1] != "file" {
t.Errorf("Validation returned incorrect pathentry: got \"%s\", want \"%s\"", path, "file")
}
}

func TestMDTM(t *testing.T) {
s := NewTestServer(t, true)
conf := goftp.Config{
Expand Down

0 comments on commit ba66bb2

Please sign in to comment.