Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have file backend remove empty dirs. #1821

Merged
merged 1 commit into from Aug 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 38 additions & 5 deletions physical/file.go
Expand Up @@ -3,6 +3,7 @@ package physical
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -43,14 +44,46 @@ func (b *FileBackend) Delete(k string) error {
defer b.l.Unlock()

path, key := b.path(k)
path = filepath.Join(path, key)
fullPath := filepath.Join(path, key)

err := os.Remove(path)
if err != nil && os.IsNotExist(err) {
err = nil
// If the path doesn't exist return success; this is in line with Vault's
// expected behavior and we don't want to check for an empty directory if
// we couldn't even find the path in the first place.
err := os.Remove(fullPath)
if err != nil {
if os.IsNotExist(err) {
return nil
} else {
return err
}
}

// Check for the directory being empty and remove if so, with another
// additional guard for the path not existing
dir, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil
} else {
return err
}
}

list, err := dir.Readdir(1)
dir.Close()
if err != nil && err != io.EOF {
return err
}

// If we have no entries, it's an empty directory; remove it
if err == io.EOF || list == nil || len(list) == 0 {
err = os.Remove(path)
if err != nil {
return err
}
}

return err
return nil
}

func (b *FileBackend) Get(k string) (*Entry, error) {
Expand Down
50 changes: 50 additions & 0 deletions physical/physical_test.go
Expand Up @@ -124,6 +124,18 @@ func testBackend(t *testing.T, b Backend) {
t.Fatalf("err: %v", err)
}

keys, err = b.List("")
if err != nil {
t.Fatalf("err: %v", err)
}
if len(keys) != 2 {
t.Fatalf("bad: %v", keys)
}
sort.Strings(keys)
if keys[0] != "foo" || keys[1] != "foo/" {
t.Fatalf("bad: %v", keys)
}

// Delete with children should work
err = b.Delete("foo")
if err != nil {
Expand All @@ -138,6 +150,44 @@ func testBackend(t *testing.T, b Backend) {
if out == nil {
t.Fatalf("missing child")
}

// Make a second nested entry to test prefix removal
e = &Entry{Key: "foo/zip", Value: []byte("zap")}
err = b.Put(e)
if err != nil {
t.Fatalf("err: %v", err)
}

// Delete should not remove the prefix
err = b.Delete("foo/bar")
if err != nil {
t.Fatalf("err: %v", err)
}

keys, err = b.List("")
if err != nil {
t.Fatalf("err: %v", err)
}
if len(keys) != 1 {
t.Fatalf("bad: %v", keys)
}
if keys[0] != "foo/" {
t.Fatalf("bad: %v", keys)
}

// Delete should remove the prefix
err = b.Delete("foo/zip")
if err != nil {
t.Fatalf("err: %v", err)
}

keys, err = b.List("")
if err != nil {
t.Fatalf("err: %v", err)
}
if len(keys) != 0 {
t.Fatalf("bad: %v", keys)
}
}

func testBackend_ListPrefix(t *testing.T, b Backend) {
Expand Down