From 479d87b91a74b20a024e72114c07d3e89cd51c66 Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 5 Jun 2017 23:52:51 +0100 Subject: [PATCH] added recursively deleting folders added tests for recursive delete added change dir to fix test + refactor fixed path issues changes directory now instead of deleting by path proftpd fix added file edge case + more tests added directory does not exist test added correct directory after delete test fixed correct directory test renamed test directories + files missed a renamed --- client_test.go | 188 +++++++++++++++++++++++++++++++++++++++++++++++++ ftp.go | 35 +++++++++ 2 files changed, 223 insertions(+) diff --git a/client_test.go b/client_test.go index 8297e49..ed1ecaa 100644 --- a/client_test.go +++ b/client_test.go @@ -268,3 +268,191 @@ func TestWrongLogin(t *testing.T) { t.Fatal("expected error, got nil") } } + +func TestDeleteDirRecur(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + c, err := DialTimeout("localhost:21", 5*time.Second) + if err != nil { + t.Fatal(err) + } + + err = c.Login("anonymous", "anonymous") + if err != nil { + t.Fatal(err) + } + + err = c.NoOp() + if err != nil { + t.Error(err) + } + + err = c.ChangeDir("incoming") + if err != nil { + t.Error(err) + } + + err = c.MakeDir("testDir") + if err != nil { + t.Error(err) + } + + err = c.ChangeDir("testDir") + if err != nil { + t.Error(err) + } + + err = c.MakeDir("anotherDir") + if err != nil { + t.Error(err) + } + + data := bytes.NewBufferString("test text") + err = c.Stor("fileTest", data) + if err != nil { + t.Error(err) + } + + err = c.ChangeDirToParent() + if err != nil { + t.Error(err) + } + err = c.RemoveDirRecur("testDir") + if err != nil { + t.Error(err) + } + dir, err := c.CurrentDir() + if err != nil { + t.Error(err) + } else { + if dir != "/incoming" { + t.Error("Wrong dir: " + dir) + } + } + + err = c.ChangeDir("testDir") + if err == nil { + t.Fatal("expected error, got nil") + } + + err = c.Logout() + if err != nil { + if protoErr := err.(*textproto.Error); protoErr != nil { + if protoErr.Code != StatusNotImplemented { + t.Error(err) + } + } else { + t.Error(err) + } + } + + c.Quit() +} + +func TestFileDeleteDirRecur(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + + c, err := DialTimeout("localhost:21", 5*time.Second) + if err != nil { + t.Fatal(err) + } + + err = c.Login("anonymous", "anonymous") + if err != nil { + t.Fatal(err) + } + + err = c.ChangeDir("incoming") + if err != nil { + t.Error(err) + } + + data := bytes.NewBufferString(testData) + err = c.Stor("testFile", data) + if err != nil { + t.Error(err) + } + + err = c.RemoveDirRecur("testFile") + if err == nil { + t.Fatal("expected error got nill") + } + + dir, err := c.CurrentDir() + if err != nil { + t.Error(err) + } else { + if dir != "/incoming" { + t.Error("Wrong dir: " + dir) + } + } + + err = c.Delete("testFile") + if err != nil { + t.Error(err) + } + + err = c.Logout() + if err != nil { + if protoErr := err.(*textproto.Error); protoErr != nil { + if protoErr.Code != StatusNotImplemented { + t.Error(err) + } + } else { + t.Error(err) + } + } + + c.Quit() +} + +func TestMissingFolderDeleteDirRecur(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + + c, err := DialTimeout("localhost:21", 5*time.Second) + if err != nil { + t.Fatal(err) + } + + err = c.Login("anonymous", "anonymous") + if err != nil { + t.Fatal(err) + } + + err = c.ChangeDir("incoming") + if err != nil { + t.Error(err) + } + + err = c.RemoveDirRecur("test") + if err == nil { + t.Fatal("expected error got nill") + } + + dir, err := c.CurrentDir() + if err != nil { + t.Error(err) + } else { + if dir != "/incoming" { + t.Error("Wrong dir: " + dir) + } + } + + err = c.Logout() + if err != nil { + if protoErr := err.(*textproto.Error); protoErr != nil { + if protoErr.Code != StatusNotImplemented { + t.Error(err) + } + } else { + t.Error(err) + } + } + + c.Quit() +} diff --git a/ftp.go b/ftp.go index c29cb38..1803423 100644 --- a/ftp.go +++ b/ftp.go @@ -496,6 +496,41 @@ func (c *ServerConn) Delete(path string) error { return err } +// RemoveDirRecur deletes a non-empty folder recursively using +// RemoveDir and Delete +func (c *ServerConn) RemoveDirRecur(path string) error { + err := c.ChangeDir(path) + if err != nil { + return err + } + currentDir, err := c.CurrentDir() + if err != nil { + return err + } + entries, err := c.List(currentDir) + for _, entry := range entries { + if entry.Name != ".." && entry.Name != "." { + if entry.Type == EntryTypeFolder { + err = c.RemoveDirRecur(currentDir + "/" + entry.Name) + if err != nil { + return err + } + } else { + err = c.Delete(entry.Name) + if err != nil { + return err + } + } + } + } + err = c.ChangeDirToParent() + if err != nil { + return err + } + err = c.RemoveDir(currentDir) + return err +} + // MakeDir issues a MKD FTP command to create the specified directory on the // remote FTP server. func (c *ServerConn) MakeDir(path string) error {