From 0ac5f90dbb2b2fe79d663b0a3a7f04dca13c196b Mon Sep 17 00:00:00 2001 From: "tanishq.singhal" Date: Sat, 13 May 2023 13:03:00 +0530 Subject: [PATCH 1/5] Implemented Delete All Resources Function --- client.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/client.go b/client.go index fb0a110b..69cdd364 100644 --- a/client.go +++ b/client.go @@ -924,6 +924,55 @@ func (c *Client) MkdirAll(path string) error { return nil } +// DeleteAllResources delete files recursively in the directory and Recursively delete subdirectories. +// An error will be returned if no file or directory with the specified path exists +func (c *Client) DeleteAllResources(filePath string) error { + + // Get the file/directory information + fileInfo, err := c.Stat(filePath) + if err != nil { + return err + } + + if fileInfo.IsDir() { + // Delete files recursively in the directory + files, err := c.ReadDir(filePath) + if err != nil { + return err + } + + for _, file := range files { + if file.IsDir() { + // Recursively delete subdirectories + err = c.DeleteAllResources(filePath + "/" + file.Name()) + if err != nil { + return err + } + } else { + // Delete individual files + err = c.Remove(filePath + "/" + file.Name()) + if err != nil { + return err + } + } + } + + // Delete the empty directory + err = c.RemoveDirectory(filePath) + if err != nil { + return err + } + } else { + // Delete individual files + err = c.Remove(filePath) + if err != nil { + return err + } + } + + return nil +} + // File represents a remote file. type File struct { c *Client From 628da3e1184de6ca545657fc87e5f247ef2ac1c8 Mon Sep 17 00:00:00 2001 From: "tanishq.singhal" Date: Mon, 15 May 2023 11:43:30 +0530 Subject: [PATCH 2/5] Adding Tests to test the func & resolving comment --- client.go | 22 +++++++++---------- client_integration_test.go | 44 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index 69cdd364..a705867f 100644 --- a/client.go +++ b/client.go @@ -924,19 +924,19 @@ func (c *Client) MkdirAll(path string) error { return nil } -// DeleteAllResources delete files recursively in the directory and Recursively delete subdirectories. +// RemoveAll delete files recursively in the directory and Recursively delete subdirectories. // An error will be returned if no file or directory with the specified path exists -func (c *Client) DeleteAllResources(filePath string) error { +func (c *Client) RemoveAll(path string) error { // Get the file/directory information - fileInfo, err := c.Stat(filePath) + fi, err := c.Stat(path) if err != nil { return err } - if fileInfo.IsDir() { + if fi.IsDir() { // Delete files recursively in the directory - files, err := c.ReadDir(filePath) + files, err := c.ReadDir(path) if err != nil { return err } @@ -944,13 +944,13 @@ func (c *Client) DeleteAllResources(filePath string) error { for _, file := range files { if file.IsDir() { // Recursively delete subdirectories - err = c.DeleteAllResources(filePath + "/" + file.Name()) + err = c.RemoveAll(path + "/" + file.Name()) if err != nil { return err } } else { // Delete individual files - err = c.Remove(filePath + "/" + file.Name()) + err = c.Remove(path + "/" + file.Name()) if err != nil { return err } @@ -958,15 +958,15 @@ func (c *Client) DeleteAllResources(filePath string) error { } // Delete the empty directory - err = c.RemoveDirectory(filePath) + err = c.RemoveDirectory(path) if err != nil { - return err + return c.RemoveDirectory(path) } } else { // Delete individual files - err = c.Remove(filePath) + err = c.Remove(path) if err != nil { - return err + return c.Remove(path) } } diff --git a/client_integration_test.go b/client_integration_test.go index c57ae776..e567525d 100644 --- a/client_integration_test.go +++ b/client_integration_test.go @@ -651,6 +651,50 @@ func TestClientRemove(t *testing.T) { } } +func TestRemoveAll(t *testing.T) { + sftp, cmd := testClient(t, READWRITE, NODELAY) + defer cmd.Wait() + defer sftp.Close() + + // Create a temporary directory for testing + tempDir, err := ioutil.TempDir("", "sftptest-removeAll") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tempDir) + + // Create file and directory within the temporary directory + f, err := ioutil.TempFile(tempDir, "sftptest-removeAll*.txt") + if err != nil { + t.Fatal(err) + } + defer f.Close() + + d, err := ioutil.TempDir(tempDir, "sftptest-removeAll1") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(d) + + // Call the function to remove the files recursively + err = sftp.RemoveAll(tempDir) + if err != nil { + t.Fatal(err) + } + + // Check if the directories and files have been deleted + _, err = os.Stat(f.Name()) + if !os.IsNotExist(err) { + t.Errorf("File %s still exists", f.Name()) + } + + _, err = os.Stat(d) + if !os.IsNotExist(err) { + t.Errorf("Directory %s still exists", d) + } + +} + func TestClientRemoveDir(t *testing.T) { sftp, cmd := testClient(t, READWRITE, NODELAY) defer cmd.Wait() From f96a7bef9516f481f86400e4799772eec83adf37 Mon Sep 17 00:00:00 2001 From: "tanishq.singhal" Date: Mon, 15 May 2023 21:08:16 +0530 Subject: [PATCH 3/5] Handling error & Test func with working tree --- client.go | 12 +++------ client_integration_test.go | 53 ++++++++++++++++++++++++++------------ 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/client.go b/client.go index a705867f..93b0e318 100644 --- a/client.go +++ b/client.go @@ -958,19 +958,13 @@ func (c *Client) RemoveAll(path string) error { } // Delete the empty directory - err = c.RemoveDirectory(path) - if err != nil { - return c.RemoveDirectory(path) - } + return c.RemoveDirectory(path) + } else { // Delete individual files - err = c.Remove(path) - if err != nil { - return c.Remove(path) - } + return c.Remove(path) } - return nil } // File represents a remote file. diff --git a/client_integration_test.go b/client_integration_test.go index e567525d..35ccbea5 100644 --- a/client_integration_test.go +++ b/client_integration_test.go @@ -651,7 +651,7 @@ func TestClientRemove(t *testing.T) { } } -func TestRemoveAll(t *testing.T) { +func TestClientRemoveAll(t *testing.T) { sftp, cmd := testClient(t, READWRITE, NODELAY) defer cmd.Wait() defer sftp.Close() @@ -663,36 +663,55 @@ func TestRemoveAll(t *testing.T) { } defer os.RemoveAll(tempDir) - // Create file and directory within the temporary directory - f, err := ioutil.TempFile(tempDir, "sftptest-removeAll*.txt") + // Create a directory tree + dir1, err := ioutil.TempDir(tempDir, "foo") if err != nil { t.Fatal(err) } - defer f.Close() - - d, err := ioutil.TempDir(tempDir, "sftptest-removeAll1") + dir2, err := ioutil.TempDir(dir1, "bar") if err != nil { t.Fatal(err) } - defer os.RemoveAll(d) - // Call the function to remove the files recursively + // Create some files within the directory tree + file1 := tempDir + "/file1.txt" + file2 := dir1 + "/file2.txt" + file3 := dir2 + "/file3.txt" + err = ioutil.WriteFile(file1, []byte("File 1"), 0644) + if err != nil { + t.Fatalf("Failed to create file: %v", err) + } + err = ioutil.WriteFile(file2, []byte("File 2"), 0644) + if err != nil { + t.Fatalf("Failed to create file: %v", err) + } + err = ioutil.WriteFile(file3, []byte("File 3"), 0644) + if err != nil { + t.Fatalf("Failed to create file: %v", err) + } + + // Call the function to delete the files recursively err = sftp.RemoveAll(tempDir) if err != nil { - t.Fatal(err) + t.Fatalf("Failed to delete files recursively: %v", err) } // Check if the directories and files have been deleted - _, err = os.Stat(f.Name()) - if !os.IsNotExist(err) { - t.Errorf("File %s still exists", f.Name()) + if _, err := os.Stat(dir1); !os.IsNotExist(err) { + t.Errorf("Directory %s still exists", dir1) } - - _, err = os.Stat(d) - if !os.IsNotExist(err) { - t.Errorf("Directory %s still exists", d) + if _, err := os.Stat(dir2); !os.IsNotExist(err) { + t.Errorf("Directory %s still exists", dir2) + } + if _, err := os.Stat(file1); !os.IsNotExist(err) { + t.Errorf("File %s still exists", file1) + } + if _, err := os.Stat(file2); !os.IsNotExist(err) { + t.Errorf("File %s still exists", file2) + } + if _, err := os.Stat(file3); !os.IsNotExist(err) { + t.Errorf("File %s still exists", file3) } - } func TestClientRemoveDir(t *testing.T) { From d17dfb25ab87b8561c67800921d0f29d0af5db4f Mon Sep 17 00:00:00 2001 From: "tanishq.singhal" Date: Thu, 18 May 2023 22:57:23 +0530 Subject: [PATCH 4/5] Resolve Comments --- client.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 93b0e318..1d720119 100644 --- a/client.go +++ b/client.go @@ -958,13 +958,12 @@ func (c *Client) RemoveAll(path string) error { } // Delete the empty directory - return c.RemoveDirectory(path) - - } else { - // Delete individual files return c.Remove(path) + } + return nil + } // File represents a remote file. From 78e49babba678130c2decdd8acbe97f74e0e8c89 Mon Sep 17 00:00:00 2001 From: "tanishq.singhal" Date: Fri, 19 May 2023 11:38:25 +0530 Subject: [PATCH 5/5] Check --- client.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/client.go b/client.go index 1d720119..0df125e1 100644 --- a/client.go +++ b/client.go @@ -957,12 +957,9 @@ func (c *Client) RemoveAll(path string) error { } } - // Delete the empty directory - return c.Remove(path) - } - return nil + return c.Remove(path) }