Skip to content

Commit

Permalink
Merge pull request #550 from tanishqsinghal7/ts-deleteallresources
Browse files Browse the repository at this point in the history
Implemented Delete All Resources Function
  • Loading branch information
puellanivis authored May 19, 2023
2 parents 34d66dd + 78e49ba commit 72a484f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
39 changes: 39 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 72a484f

Please sign in to comment.