Skip to content

Commit

Permalink
fix: if conn.Stat returns no file Delete is successful
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Feb 1, 2024
1 parent 7ea3ea4 commit cfbb737
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,17 +338,24 @@ func (c *client) Delete(path string) error {
}

info, err := conn.Stat(path)
err = c.clearConnectionOnError(err)
if err != nil && !os.IsNotExist(err) {
if err != nil {
if os.IsNotExist(err) {
return nil // file doesn't exist
}

// The error is something else related to STAT so return that
err = c.clearConnectionOnError(err)
return fmt.Errorf("sftp: delete stat: %w", err)
}

if info != nil {
err := conn.Remove(path)
err = c.clearConnectionOnError(err)
if err != nil {
return fmt.Errorf("sftp: delete: %w", err)
}
}

return nil // not found
}

Expand Down
8 changes: 8 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ func TestClient(t *testing.T) {
require.NoError(t, file.Close())
})

t.Run("Delete", func(t *testing.T) {
err := client.Delete("/missing.txt")
require.NoError(t, err)

err = client.Delete("/no-existing-dir/missing.txt")
require.NoError(t, err)
})

t.Run("Skip chmod after upload", func(t *testing.T) {
// upload file
fileName := fmt.Sprintf("/upload/%d.txt", time.Now().Unix())
Expand Down

0 comments on commit cfbb737

Please sign in to comment.