From 7bbfa218f9c33ad946822aac2ee4b1f23164b59c Mon Sep 17 00:00:00 2001 From: vahid-sohrabloo Date: Sun, 19 Feb 2017 17:14:20 +0330 Subject: [PATCH 1/3] Add Size Command --- ftp.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ftp.go b/ftp.go index 81c04db..219d987 100644 --- a/ftp.go +++ b/ftp.go @@ -407,6 +407,21 @@ func (c *ServerConn) CurrentDir() (string, error) { return msg[start+1 : end], nil } +// FileSize issues a SIZE FTP command, which Returns the size of the file +func (c *ServerConn) FileSize(path string) (int, error) { + _, msg, err := c.cmd(StatusFile, "SIZE %s", path) + if err != nil { + return 0, err + } + + size, err := strconv.Atoi(msg) + if err != nil { + return 0, err + } + + return size, nil +} + // Retr issues a RETR FTP command to fetch the specified file from the remote // FTP server. // From 468423d44aad91768b349739006a79a94fbfaf0e Mon Sep 17 00:00:00 2001 From: Vahid Sohrabloo Date: Mon, 20 Feb 2017 00:13:08 +0330 Subject: [PATCH 2/3] Change file Size type from int to int64 and add test for it --- client_test.go | 13 +++++++++++++ ftp.go | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/client_test.go b/client_test.go index a713556..161dc11 100644 --- a/client_test.go +++ b/client_test.go @@ -96,6 +96,19 @@ func testConn(t *testing.T, disableEPSV bool) { r.Close() } + fileSize, err := c.FileSize("tset") + if err != nil { + t.Error(err) + } + if fileSize != 14 { + t.Errorf("file size %q, expected %q", fileSize, 14) + } + + fileSize, err = c.FileSize("not-found") + if err == nil { + t.Fatal("expected error, got nil") + } + err = c.Delete("tset") if err != nil { t.Error(err) diff --git a/ftp.go b/ftp.go index 219d987..03dd7c7 100644 --- a/ftp.go +++ b/ftp.go @@ -408,13 +408,13 @@ func (c *ServerConn) CurrentDir() (string, error) { } // FileSize issues a SIZE FTP command, which Returns the size of the file -func (c *ServerConn) FileSize(path string) (int, error) { +func (c *ServerConn) FileSize(path string) (int64, error) { _, msg, err := c.cmd(StatusFile, "SIZE %s", path) if err != nil { return 0, err } - size, err := strconv.Atoi(msg) + size, err := strconv.ParseInt(msg, 10, 64) if err != nil { return 0, err } From 7e0dbe8c484f39ca85f59e4f85067a0fba994d96 Mon Sep 17 00:00:00 2001 From: Vahid Sohrabloo Date: Mon, 20 Feb 2017 09:04:20 +0330 Subject: [PATCH 3/3] Add more test in FileSize --- client_test.go | 14 ++++++++++++++ ftp.go | 7 +------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/client_test.go b/client_test.go index 161dc11..1f9cf81 100644 --- a/client_test.go +++ b/client_test.go @@ -104,6 +104,20 @@ func testConn(t *testing.T, disableEPSV bool) { t.Errorf("file size %q, expected %q", fileSize, 14) } + data = bytes.NewBufferString("") + err = c.Stor("tset", data) + if err != nil { + t.Error(err) + } + + fileSize, err = c.FileSize("tset") + if err != nil { + t.Error(err) + } + if fileSize != 0 { + t.Errorf("file size %q, expected %q", fileSize, 0) + } + fileSize, err = c.FileSize("not-found") if err == nil { t.Fatal("expected error, got nil") diff --git a/ftp.go b/ftp.go index 03dd7c7..b2b491e 100644 --- a/ftp.go +++ b/ftp.go @@ -414,12 +414,7 @@ func (c *ServerConn) FileSize(path string) (int64, error) { return 0, err } - size, err := strconv.ParseInt(msg, 10, 64) - if err != nil { - return 0, err - } - - return size, nil + return strconv.ParseInt(msg, 10, 64) } // Retr issues a RETR FTP command to fetch the specified file from the remote