Skip to content

Commit

Permalink
Delete folders when empty for localfs
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin Pentecost committed Jul 15, 2021
1 parent 30e5456 commit 5791025
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
26 changes: 24 additions & 2 deletions localfs/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package localfs

import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -278,11 +279,32 @@ func (l *LocalStore) Get(ctx context.Context, o string) (cloudstorage.Object, er
// Delete the object from underlying store.
func (l *LocalStore) Delete(ctx context.Context, obj string) error {
fo := path.Join(l.storepath, obj)
os.Remove(fo)
if err := os.Remove(fo); err != nil {
return err
}
mf := fo + ".metadata"
if cloudstorage.Exists(mf) {
os.Remove(mf)
if err := os.Remove(mf); err != nil {
return err
}
}

// When the last item in a folder is deleted, the folder
// should also be deleted. This matches the behavior in GCS.
dir, err := os.Open(l.storepath)
if err != nil {
return fmt.Errorf("failed to open store dir=%s err=%w", l.storepath, err)
}
if _, err = dir.Readdirnames(1); errors.Is(err, io.EOF) {
dir.Close()
// it's empty, so remove it.
if err := os.Remove(l.storepath); err != nil {
return err
}
} else {
dir.Close()
}

return nil
}

Expand Down
16 changes: 16 additions & 0 deletions testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func Clearstore(t TestingT, store cloudstorage.Store) {
}

func RunTests(t TestingT, s cloudstorage.Store, conf *cloudstorage.Config) {
// Ensure testing dirs are clean.
Clearstore(t, s)
defer Clearstore(t, s)

t.Logf("running store setup: type:%v", s.Type())
StoreSetup(t, s)
Expand Down Expand Up @@ -136,6 +139,7 @@ func deleteIfExists(store cloudstorage.Store, filePath string) {
obj.Delete()
}
}

func StoreSetup(t TestingT, store cloudstorage.Store) {

// Ensure the store has a String identifying store type
Expand All @@ -150,6 +154,12 @@ func BasicRW(t TestingT, store cloudstorage.Store) {
// Read the object from store, delete if it exists
deleteIfExists(store, "prefix/test.csv")

// Store should be empty
all, err := store.List(context.Background(), cloudstorage.NewQueryAll())
assert.NoError(t, err)
assert.NotNil(t, all)
assert.Empty(t, all.Objects)

// Create a new object and write to it.
obj, err := store.NewObject("prefix/test.csv")
assert.Equal(t, nil, err)
Expand Down Expand Up @@ -190,6 +200,12 @@ func BasicRW(t TestingT, store cloudstorage.Store) {

assert.Equal(t, testcsv, string(bytes))

// Store should be not empty
all, err = store.List(context.Background(), cloudstorage.NewQueryAll())
assert.NoError(t, err)
assert.NotNil(t, all)
assert.NotEmpty(t, all.Objects)

// Now delete again
err = obj2.Delete()
assert.Equal(t, nil, err)
Expand Down

0 comments on commit 5791025

Please sign in to comment.