Skip to content

Commit

Permalink
Merge pull request #340 from typerat/fix-gcsfs-removeall-notfound
Browse files Browse the repository at this point in the history
Fix gcsfs RemoveAll
  • Loading branch information
mbertschler authored Mar 11, 2022
2 parents 450b30f + b25fb10 commit 931302a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gcsfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (o *GcsFile) Seek(newOffset int64, whence int) (int64, error) {
if (whence == 0 && newOffset == o.fhOffset) || (whence == 1 && newOffset == 0) {
return o.fhOffset, nil
}
log.Printf("WARNING: Seek beavhior triggered, highly inefficent. Offset before seek is at %d\n", o.fhOffset)
log.Printf("WARNING: Seek behavior triggered, highly inefficent. Offset before seek is at %d\n", o.fhOffset)

//Fore the reader/writers to be reopened (at correct offset)
err := o.Sync()
Expand Down
5 changes: 5 additions & 0 deletions gcsfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,14 @@ func (fs *Fs) RemoveAll(path string) error {
}

pathInfo, err := fs.Stat(path)
if errors.Is(err, ErrFileNotFound) {
// return early if file doesn't exist
return nil
}
if err != nil {
return err
}

if !pathInfo.IsDir() {
return fs.Remove(path)
}
Expand Down
32 changes: 32 additions & 0 deletions gcsfs/gcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,3 +804,35 @@ func TestGcsMkdirAll(t *testing.T) {
}
})
}

func TestGcsRemoveAll(t *testing.T) {
t.Run("non-existent", func(t *testing.T) {
err := gcsAfs.RemoveAll(filepath.Join(bucketName, "a"))
if err != nil {
t.Fatal("error should be nil when removing non-existent file")
}
})
t.Run("success", func(t *testing.T) {
aDir := filepath.Join(bucketName, "a")
bDir := filepath.Join(aDir, "b")

err := gcsAfs.MkdirAll(bDir, 0755)
if err != nil {
t.Fatal(err)
}
_, err = gcsAfs.Stat(bDir)
if err != nil {
t.Fatal(err)
}

err = gcsAfs.RemoveAll(aDir)
if err != nil {
t.Fatalf("failed to remove the folder %s with error: %s", aDir, err)
}

_, err = gcsAfs.Stat(aDir)
if err == nil {
t.Fatalf("folder %s wasn't removed", aDir)
}
})
}

0 comments on commit 931302a

Please sign in to comment.