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 <asarai@suse.de>
This commit is contained in:
Aleksa Sarai 2016-07-27 22:03:43 +10:00
parent 4f26a7e254
commit b5ed81cd52
No known key found for this signature in database
GPG key ID: 9E18AA267DDB8DB4
3 changed files with 23 additions and 36 deletions

View file

@ -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

View file

@ -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

View file

@ -93,6 +93,7 @@ func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) {
}
}
}
e.Set = creator.curSet
default:
// TODO(vbatts) log a warning?
continue