From cfbb737a7806d9722fc67c4de48ca83cf376c3e9 Mon Sep 17 00:00:00 2001 From: Adam Shannon Date: Thu, 1 Feb 2024 09:48:47 -0600 Subject: [PATCH] fix: if conn.Stat returns no file Delete is successful --- client.go | 11 +++++++++-- client_test.go | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index b06e63ac..c9392d26 100644 --- a/client.go +++ b/client.go @@ -338,10 +338,16 @@ 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) @@ -349,6 +355,7 @@ func (c *client) Delete(path string) error { return fmt.Errorf("sftp: delete: %w", err) } } + return nil // not found } diff --git a/client_test.go b/client_test.go index 3e10398d..ab8ef6ba 100644 --- a/client_test.go +++ b/client_test.go @@ -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())