Skip to content

Commit ba66bb2

Browse files
authored
fix(MLST): Prefix MLST entries with a space (#353)
* Prefix MLST entries with a space * Fix lint issue in handleMLST * Rename ẁriteMLSxOutput to writeMLSxEntry Signed-off-by: Thomas Hallgren <[email protected]>
1 parent 3af5d5d commit ba66bb2

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

handle_dirs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,14 @@ func (c *clientHandler) dirTransferMLSD(w io.Writer, files []os.FileInfo) error
285285
}
286286

287287
for _, file := range files {
288-
if err := c.writeMLSxOutput(w, file); err != nil {
288+
if err := c.writeMLSxEntry(w, file); err != nil {
289289
return err
290290
}
291291
}
292292

293293
return nil
294294
}
295-
func (c *clientHandler) writeMLSxOutput(w io.Writer, file os.FileInfo) error {
295+
func (c *clientHandler) writeMLSxEntry(w io.Writer, file os.FileInfo) error {
296296
var listType string
297297
if file.IsDir() {
298298
listType = "dir"

handle_files.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -466,17 +466,20 @@ func (c *clientHandler) handleMLST(param string) error {
466466

467467
path := c.absPath(param)
468468

469-
if info, err := c.driver.Stat(path); err == nil {
469+
info, err := c.driver.Stat(path)
470+
if err == nil {
470471
defer c.multilineAnswer(StatusFileOK, "File details")()
471472

472-
if errWrite := c.writeMLSxOutput(c.writer, info); errWrite != nil {
473-
return errWrite
473+
// Each MLSx entry must start with a space when returned in a multiline answer
474+
if err = c.writer.WriteByte(' '); err == nil {
475+
err = c.writeMLSxEntry(c.writer, info)
474476
}
475477
} else {
476478
c.writeMessage(StatusActionNotTaken, fmt.Sprintf("Could not list: %v", err))
479+
err = nil
477480
}
478481

479-
return nil
482+
return err
480483
}
481484

482485
func (c *clientHandler) handleALLO(param string) error {

handle_files_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,40 @@ func TestSTATFile(t *testing.T) {
362362
require.Equal(t, StatusFileActionNotTaken, rc)
363363
}
364364

365+
func TestMLST(t *testing.T) {
366+
s := NewTestServer(t, true)
367+
conf := goftp.Config{
368+
User: authUser,
369+
Password: authPass,
370+
}
371+
c, err := goftp.DialConfig(conf, s.Addr())
372+
require.NoError(t, err, "Couldn't connect")
373+
374+
defer func() { panicOnError(c.Close()) }()
375+
376+
// Creating a tiny file
377+
ftpUpload(t, c, createTemporaryFile(t, 10), "file")
378+
379+
raw, err := c.OpenRawConn()
380+
require.NoError(t, err, "Couldn't open raw connection")
381+
382+
defer func() { require.NoError(t, raw.Close()) }()
383+
384+
rc, rsp, err := raw.SendCommand("MLST file")
385+
require.NoError(t, err)
386+
require.Equal(t, StatusFileOK, rc)
387+
388+
lines := strings.Split(rsp, "\n")
389+
require.Len(t, lines, 3)
390+
path := validMLSxEntryPattern.FindStringSubmatch(lines[1] + "\r\n")
391+
392+
if len(path) != 2 {
393+
t.Errorf("Valid MLST response example did not pass validation: \"%s\"", lines[1])
394+
} else if path[1] != "file" {
395+
t.Errorf("Validation returned incorrect pathentry: got \"%s\", want \"%s\"", path, "file")
396+
}
397+
}
398+
365399
func TestMDTM(t *testing.T) {
366400
s := NewTestServer(t, true)
367401
conf := goftp.Config{

0 commit comments

Comments
 (0)