From 722ec1da888cf0a795acd15bf1c020d49ad20633 Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Mon, 23 Aug 2021 09:52:45 +0800 Subject: [PATCH] fix: Return file information if ls parameter refers to a file (#17) --- client/dirs.go | 47 +++++++++++++++++++++++++---------------------- client/files.go | 18 +++++------------- tests/ftp_test.go | 18 +++++++++--------- 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/client/dirs.go b/client/dirs.go index 9b9daf1..09d4d4f 100644 --- a/client/dirs.go +++ b/client/dirs.go @@ -30,7 +30,6 @@ func (c *Handler) handleCWD() { } p := c.absPath(c.param) - _, err := c.getDirInfo(p) if err != nil { c.WriteMessage(StatusActionNotTaken, fmt.Sprintf("CD issue: %v", err)) @@ -103,7 +102,7 @@ func (c *Handler) handleLIST() { fileInfos, err := c.listFile(dir) if err != nil { - c.WriteMessage(StatusActionNotTaken, err.Error()) + c.WriteMessage(StatusFileActionNotTaken, err.Error()) return } @@ -124,40 +123,44 @@ func (c *Handler) handleLIST() { } func (c *Handler) listFile(p string) ([]*fileInfo, error) { - iterator, err := c.storager.List(p) + object, err := c.storager.Stat(p) if err != nil { return nil, err } var files []*fileInfo - for { - o, err := iterator.Next() + if object.GetMode().IsDir() { + iterator, err := c.storager.List(p) if err != nil { - if errors.Is(err, types.IterateDone) { - break - } else { - return nil, err + return nil, err + } + + for { + o, err := iterator.Next() + if err != nil { + if errors.Is(err, types.IterateDone) { + break + } else { + return nil, err + } } + files = append(files, &fileInfo{o}) } - files = append(files, &fileInfo{o}) + } else { + files = append(files, &fileInfo{object}) } - return files, nil -} -func fileStat(file *fileInfo) string { - return fmt.Sprintf( - "%s 1 ftp ftp %12d %s %s", - file.Mode(), - file.Size(), - file.ModTime().Format(" Jan _2 15:04 "), - file.Name(), - ) + return files, nil } func (c *Handler) dirList(w io.Writer, files []*fileInfo) { for _, file := range files { - stat := fileStat(file) - if _, err := fmt.Fprintf(w, "%s\r\n", stat); err != nil { + if _, err := fmt.Fprintf(w, "%s 1 ftp ftp %12d %s %s\r\n", + file.Mode(), + file.Size(), + file.ModTime().Format(" Jan _2 15:04"), + file.Name(), + ); err != nil { return } } diff --git a/client/files.go b/client/files.go index 5dd075d..f47abad 100644 --- a/client/files.go +++ b/client/files.go @@ -193,20 +193,12 @@ func (c *Handler) handleSIZE() { func (c *Handler) handleSTATFile() { path := c.absPath(c.param) - c.writeLine("213-Status follows:") - if object, err := c.storager.Stat(path); err == nil { - if object.GetMode()&types.ModeDir == 1 { - fileInfos, err := c.listFile(path) - if err != nil { - c.WriteMessage(StatusActionNotTaken, err.Error()) - return - } - c.dirList(c.writer, fileInfos) - } else { - c.writeLine(fileStat(&fileInfo{object})) - } + fileInfos, err := c.listFile(path) + if err != nil { + c.WriteMessage(StatusFileActionNotTaken, err.Error()) + return } - + c.dirList(c.writer, fileInfos) c.writeLine("213 End of status") } diff --git a/tests/ftp_test.go b/tests/ftp_test.go index bee9f26..f99fa5d 100644 --- a/tests/ftp_test.go +++ b/tests/ftp_test.go @@ -135,8 +135,8 @@ func (t *ftpServerBaseCommandTest) TestListFiles() { fileList := tk.List(conn, "") assert.Equal(t.T(), []string{ - "d--------- 1 ftp ftp 0 Jan 1 00:00 test", - "d--------- 1 ftp ftp 0 Jan 1 00:00 test1", + "d--------- 1 ftp ftp 0 Jan 1 00:00 test", + "d--------- 1 ftp ftp 0 Jan 1 00:00 test1", }, fileList) } @@ -150,13 +150,13 @@ func (t *ftpServerBaseCommandTest) TestDeleteFile() { tk.MustSuccess(conn, "mkd test1") fileList := tk.List(conn, "") assert.Equal(t.T(), []string{ - "d--------- 1 ftp ftp 0 Jan 1 00:00 test", - "d--------- 1 ftp ftp 0 Jan 1 00:00 test1", + "d--------- 1 ftp ftp 0 Jan 1 00:00 test", + "d--------- 1 ftp ftp 0 Jan 1 00:00 test1", }, fileList) tk.MustSuccess(conn, "RMD test1") fileList = tk.List(conn, "") assert.Equal(t.T(), []string{ - "d--------- 1 ftp ftp 0 Jan 1 00:00 test", + "d--------- 1 ftp ftp 0 Jan 1 00:00 test", }, fileList) } @@ -169,7 +169,7 @@ func (t *ftpServerBaseCommandTest) TestRenameFile() { tk.Store(conn, "file1", []byte("file1 content")) fileList := tk.List(conn, "") assert.Equal(t.T(), []string{ - "-rwxrwxrwx 1 ftp ftp 13 Jan 1 00:00 file1", + "-rwxrwxrwx 1 ftp ftp 13 Jan 1 00:00 file1", }, fileList) tk.MustSuccess(conn, "rnfr file1") @@ -177,7 +177,7 @@ func (t *ftpServerBaseCommandTest) TestRenameFile() { fileList = tk.List(conn, "") assert.Equal(t.T(), []string{ - "-rwxrwxrwx 1 ftp ftp 13 Jan 1 00:00 test", + "-rwxrwxrwx 1 ftp ftp 13 Jan 1 00:00 test", }, fileList) tk.MustFailure(conn, "rnto test1") @@ -192,8 +192,8 @@ func (t *ftpServerBaseCommandTest) TestStoreFile() { tk.Store(conn, "file1", []byte("file1 content")) fileList := tk.List(conn, "") assert.Equal(t.T(), []string{ - "-rwxrwxrwx 1 ftp ftp 13 Jan 1 00:00 file1", - "d--------- 1 ftp ftp 0 Jan 1 00:00 test", + "-rwxrwxrwx 1 ftp ftp 13 Jan 1 00:00 file1", + "d--------- 1 ftp ftp 0 Jan 1 00:00 test", }, fileList) tk.Send(conn, "size file1").Success("13")