Post-review fixes for file/zk recursive empty prefix delete

This commit is contained in:
Paweł Rozlach 2016-10-05 14:08:00 +02:00 committed by Jeff Mitchell
parent bab1471345
commit 33b4683dfd
2 changed files with 13 additions and 5 deletions

View File

@ -41,6 +41,10 @@ func newFileBackend(conf map[string]string, logger log.Logger) (Backend, error)
}
func (b *FileBackend) Delete(path string) error {
if path == "" {
return nil
}
b.l.Lock()
defer b.l.Unlock()
@ -49,7 +53,7 @@ func (b *FileBackend) Delete(path string) error {
err := os.Remove(fullPath)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("Failed to remove `%s`: %v", fullPath, err)
return fmt.Errorf("Failed to remove %q: %v", fullPath, err)
}
err = b.cleanupLogicalPath(path)
@ -60,9 +64,9 @@ func (b *FileBackend) Delete(path string) error {
// cleanupLogicalPath is used to remove all empty nodes, begining with deepest
// one, aborting on first non-empty one, up to top-level node.
func (b *FileBackend) cleanupLogicalPath(path string) error {
nodes := strings.Split(path, "/")
nodes := strings.Split(path, fmt.Sprintf("%c", os.PathSeparator))
for i := len(nodes) - 1; i > 0; i-- {
fullPath := b.Path + "/" + strings.Join(nodes[:i], "/")
fullPath := filepath.Join(b.Path, filepath.Join(nodes[:i]...))
dir, err := os.Open(fullPath)
if err != nil {

View File

@ -242,13 +242,17 @@ func (c *ZookeeperBackend) Get(key string) (*Entry, error) {
func (c *ZookeeperBackend) Delete(key string) error {
defer metrics.MeasureSince([]string{"zookeeper", "delete"}, time.Now())
if key == "" {
return nil
}
// Delete the full path
fullPath := c.nodePath(key)
err := c.client.Delete(fullPath, -1)
// Mask if the node does not exist
if err != nil && err != zk.ErrNoNode {
return fmt.Errorf("Failed to remove `%s`: %v", fullPath, err)
return fmt.Errorf("Failed to remove %q: %v", fullPath, err)
}
err = c.cleanupLogicalPath(key)
@ -285,7 +289,7 @@ func (c *ZookeeperBackend) List(prefix string) ([]string, error) {
// and append the slash which is what Vault depends on
// for iteration
if stat.DataLength > 0 && stat.NumChildren > 0 {
msgFmt := "Node %s is both of data and leaf type ??"
msgFmt := "Node %q is both of data and leaf type ??"
panic(fmt.Sprintf(msgFmt, childPath))
} else if stat.DataLength == 0 {
// No, we cannot differentiate here on number of children as node