Repository: pkg/sftp
Pull request #550 (2023-05-13 07:35:20)

Implemented Delete All Resources Function
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


Patch:
diff --git a/client.go b/client.go
index fb0a110..0df125e 100644
--- a/client.go
+++ b/client.go
@@ -924,6 +924,45 @@ func (c *Client) MkdirAll(path string) error {
 	return nil
 }
 
+// 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) RemoveAll(path string) error {
+
+	// Get the file/directory information
+	fi, err := c.Stat(path)
+	if err != nil {
+		return err
+	}
+
+	if fi.IsDir() {
+		// Delete files recursively in the directory
+		files, err := c.ReadDir(path)
+		if err != nil {
+			return err
+		}
+
+		for _, file := range files {
+			if file.IsDir() {
+				// Recursively delete subdirectories
+				err = c.RemoveAll(path + "/" + file.Name())
+				if err != nil {
+					return err
+				}
+			} else {
+				// Delete individual files
+				err = c.Remove(path + "/" + file.Name())
+				if err != nil {
+					return err
+				}
+			}
+		}
+
+	}
+
+	return c.Remove(path)
+
+}
+
 // File represents a remote file.
 type File struct {
 	c      *Client


Test patch:
diff --git a/client_integration_test.go b/client_integration_test.go
index c57ae77..35ccbea 100644
--- a/client_integration_test.go
+++ b/client_integration_test.go
@@ -651,6 +651,69 @@ func TestClientRemove(t *testing.T) {
 	}
 }
 
+func TestClientRemoveAll(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 a directory tree
+	dir1, err := ioutil.TempDir(tempDir, "foo")
+	if err != nil {
+		t.Fatal(err)
+	}
+	dir2, err := ioutil.TempDir(dir1, "bar")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	// 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.Fatalf("Failed to delete files recursively: %v", err)
+	}
+
+	// Check if the directories and files have been deleted
+	if _, err := os.Stat(dir1); !os.IsNotExist(err) {
+		t.Errorf("Directory %s still exists", dir1)
+	}
+	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) {
 	sftp, cmd := testClient(t, READWRITE, NODELAY)
 	defer cmd.Wait()