From 2da8e34079b01cf51eb65b5216ba43d8b33efc08 Mon Sep 17 00:00:00 2001 From: "Andrew C. Bodine" Date: Tue, 16 Dec 2014 15:06:35 -0800 Subject: [PATCH] Closes #9311 Handles container id/name collisions against daemon functionalities according to #8069 Signed-off-by: Andrew C. Bodine --- truncindex/truncindex.go | 22 ++++++++++------------ truncindex/truncindex_test.go | 5 +++++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/truncindex/truncindex.go b/truncindex/truncindex.go index eec5597..73c7e24 100644 --- a/truncindex/truncindex.go +++ b/truncindex/truncindex.go @@ -10,10 +10,8 @@ import ( ) var ( - // ErrNoID is thrown when attempting to use empty prefixes - ErrNoID = errors.New("prefix can't be empty") - // ErrDuplicateID is thrown when a duplicated id was found - ErrDuplicateID = errors.New("multiple IDs were found") + ErrEmptyPrefix = errors.New("Prefix can't be empty") + ErrAmbiguousPrefix = errors.New("Multiple IDs found with provided prefix") ) func init() { @@ -47,7 +45,7 @@ func (idx *TruncIndex) addID(id string) error { return fmt.Errorf("illegal character: ' '") } if id == "" { - return ErrNoID + return ErrEmptyPrefix } if _, exists := idx.ids[id]; exists { return fmt.Errorf("id already exists: '%s'", id) @@ -87,26 +85,26 @@ func (idx *TruncIndex) Delete(id string) error { // Get retrieves an ID from the TruncIndex. If there are multiple IDs // with the given prefix, an error is thrown. func (idx *TruncIndex) Get(s string) (string, error) { - idx.RLock() - defer idx.RUnlock() + if s == "" { + return "", ErrEmptyPrefix + } var ( id string ) - if s == "" { - return "", ErrNoID - } subTreeVisitFunc := func(prefix patricia.Prefix, item patricia.Item) error { if id != "" { // we haven't found the ID if there are two or more IDs id = "" - return ErrDuplicateID + return ErrAmbiguousPrefix } id = string(prefix) return nil } + idx.RLock() + defer idx.RUnlock() if err := idx.trie.VisitSubtree(patricia.Prefix(s), subTreeVisitFunc); err != nil { - return "", fmt.Errorf("no such id: %s", s) + return "", err } if id != "" { return id, nil diff --git a/truncindex/truncindex_test.go b/truncindex/truncindex_test.go index 32c41c7..8ad1634 100644 --- a/truncindex/truncindex_test.go +++ b/truncindex/truncindex_test.go @@ -59,6 +59,11 @@ func TestTruncIndex(t *testing.T) { assertIndexGet(t, index, id[:4], "", true) assertIndexGet(t, index, id[:1], "", true) + // An ambiguous id prefix should return an error + if _, err := index.Get(id[:4]); err == nil || err == nil { + t.Fatal("An ambiguous id prefix should return an error") + } + // 7 characters should NOT conflict assertIndexGet(t, index, id[:7], id, false) assertIndexGet(t, index, id2[:7], id2, false)