From 36372dd3c8dc8d6333d751fb7d1939e25cc39a78 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Wed, 27 Jul 2016 22:03:43 +1000 Subject: [PATCH] mtree: remove use of dhCreator for iterators Fix a bug in the parser that caused all iterators to have to handle the /set and /unset semantics separately. In addition, provide a helper function to correctly generate the merged set of keywords for a particular entry. Signed-off-by: Aleksa Sarai --- check.go | 45 +++++++++------------------------------------ entry.go | 13 +++++++++++++ parse.go | 1 + 3 files changed, 23 insertions(+), 36 deletions(-) diff --git a/check.go b/check.go index 5586973..6a20848 100644 --- a/check.go +++ b/check.go @@ -32,7 +32,6 @@ func (f Failure) String() string { // the available keywords from the list and each entry in the hierarchy. // If keywords is nil, the check all present in the DirectoryHierarchy func Check(root string, dh *DirectoryHierarchy, keywords []string) (*Result, error) { - creator := dhCreator{DH: dh} curDir, err := os.Getwd() if err == nil { defer os.Chdir(curDir) @@ -41,16 +40,11 @@ func Check(root string, dh *DirectoryHierarchy, keywords []string) (*Result, err if err := os.Chdir(root); err != nil { return nil, err } - sort.Sort(byPos(creator.DH.Entries)) + sort.Sort(byPos(dh.Entries)) + var result Result - for i, e := range creator.DH.Entries { + for _, e := range dh.Entries { switch e.Type { - case SpecialType: - if e.Name == "/set" { - creator.curSet = &creator.DH.Entries[i] - } else if e.Name == "/unset" { - creator.curSet = nil - } case RelativeType, FullType: pathname, err := e.Path() if err != nil { @@ -61,12 +55,8 @@ func Check(root string, dh *DirectoryHierarchy, keywords []string) (*Result, err return nil, err } - var kvs KeyVals - if creator.curSet != nil { - kvs = MergeSet(creator.curSet.Keywords, e.Keywords) - } else { - kvs = NewKeyVals(e.Keywords) - } + kvs := e.AllKeys() + for _, kv := range kvs { kw := kv.Keyword() // 'tar_time' keyword evaluation wins against 'time' keyword evaluation @@ -133,18 +123,11 @@ func TarCheck(tarDH, dh *DirectoryHierarchy, keywords []string) (*Result, error) Type: CommentType, } curDir := tarRoot - creator := dhCreator{DH: dh} - sort.Sort(byPos(creator.DH.Entries)) + sort.Sort(byPos(dh.Entries)) var outOfTree bool - for i, e := range creator.DH.Entries { + for _, e := range dh.Entries { switch e.Type { - case SpecialType: - if e.Name == "/set" { - creator.curSet = &creator.DH.Entries[i] - } else if e.Name == "/unset" { - creator.curSet = nil - } case RelativeType, FullType: pathname, err := e.Path() if err != nil { @@ -166,20 +149,10 @@ func TarCheck(tarDH, dh *DirectoryHierarchy, keywords []string) (*Result, error) } // expected values from file hierarchy spec - var kvs KeyVals - if creator.curSet != nil { - kvs = MergeSet(creator.curSet.Keywords, e.Keywords) - } else { - kvs = NewKeyVals(e.Keywords) - } + kvs := e.AllKeys() // actual - var tarkvs KeyVals - if tarEntry.Set != nil { - tarkvs = MergeSet(tarEntry.Set.Keywords, tarEntry.Keywords) - } else { - tarkvs = NewKeyVals(tarEntry.Keywords) - } + tarkvs := tarEntry.AllKeys() for _, kv := range kvs { // TODO: keep track of symlinks diff --git a/entry.go b/entry.go index 8273e8d..055e9a0 100644 --- a/entry.go +++ b/entry.go @@ -100,6 +100,19 @@ func (e Entry) String() string { return fmt.Sprintf(" %s %s", e.Name, strings.Join(e.Keywords, " ")) } +// AllKeys returns the full set of KeyVals for the given entry, based on the +// /set keys as well as the entry-local keys. Entry-local keys always take +// precedence. +func (e Entry) AllKeys() KeyVals { + var kv KeyVals + if e.Set != nil { + kv = MergeSet(e.Set.Keywords, e.Keywords) + } else { + kv = NewKeyVals(e.Keywords) + } + return kv +} + // EntryType are the formats of lines in an mtree spec file type EntryType int diff --git a/parse.go b/parse.go index 77987c6..9e1f9bb 100644 --- a/parse.go +++ b/parse.go @@ -93,6 +93,7 @@ func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) { } } } + e.Set = creator.curSet default: // TODO(vbatts) log a warning? continue