Add STAT command support and tests

This commit is contained in:
namashin 2024-07-10 10:53:59 +09:00
parent ebfb9b5098
commit fa993fbfbf
3 changed files with 41 additions and 0 deletions

View File

@ -417,3 +417,29 @@ func TestDialWithDialer(t *testing.T) {
assert.Equal(t, true, dialerCalled)
}
func TestServerConn_Stat(t *testing.T) {
mock, c := openConn(t, "127.0.0.1")
defer func() {
err := c.Quit()
assert.NoError(t, err)
mock.Close()
}()
err := c.MakeDir(testDir)
assert.NoError(t, err)
testFile := testDir + "/testfile.txt"
data := bytes.NewBufferString(testData)
err = c.Stor(testFile, data)
assert.NoError(t, err)
status, err := c.Stat(testDir)
assert.NoError(t, err)
assert.NotEmpty(t, status, "Expected non-empty status for directory")
status, err = c.Stat(testFile)
assert.NoError(t, err)
assert.NotEmpty(t, status, "Expected non-empty status for file")
}

View File

@ -279,6 +279,12 @@ func (mock *ftpMock) listen() {
case "QUIT":
mock.printfLine("221 Goodbye.")
return
case "STAT":
if len(cmdParts) > 1 {
mock.printfLine("213-Status follows:\r\n%s\r\n213 End of status", strings.Join(cmdParts[1:], " "))
} else {
mock.printfLine("213-Status follows:\r\nFTP server status\r\n213 End of status")
}
default:
mock.printfLine("500 Unknown command %s.", cmdParts[0])
}

9
ftp.go
View File

@ -1123,6 +1123,15 @@ func (c *ServerConn) Quit() error {
return errs.ErrorOrNil()
}
// Stat issues a STAT FTP command to obtain the status of a file or directory.
func (c *ServerConn) Stat(path string) (string, error) {
_, msg, err := c.cmd(StatusFile, "STAT %s", path)
if err != nil {
return "", err
}
return msg, nil
}
// Read implements the io.Reader interface on a FTP data connection.
func (r *Response) Read(buf []byte) (int, error) {
return r.conn.Read(buf)