Skip to content

Commit

Permalink
fix: Return file information if ls parameter refers to a file (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongjiwei authored Aug 23, 2021
1 parent 399411f commit 722ec1d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
47 changes: 25 additions & 22 deletions client/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
}
Expand Down
18 changes: 5 additions & 13 deletions client/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
18 changes: 9 additions & 9 deletions tests/ftp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -169,15 +169,15 @@ 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")
tk.MustSuccess(conn, "rnto test")

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")
Expand All @@ -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")

Expand Down

0 comments on commit 722ec1d

Please sign in to comment.