mirror of
https://github.com/vbatts/go-mtree.git
synced 2024-11-05 08:25:58 +00:00
decc72b335
This commit contains added features to go-mtree that allows user to create an mtree spec with '-T' option when specifying a tar archive. Users can also validate an mtree spec against a tar archive with an mtree spec. Also for the test archive, there is a mixture of files, and folders (empty & non-empty), and symlinks (broken & unbroken). Signed-off-by: Stephen Chung <schung@redhat.com>
190 lines
4.7 KiB
Go
190 lines
4.7 KiB
Go
package mtree
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
)
|
|
|
|
// Result of a Check
|
|
type Result struct {
|
|
// list of any failures in the Check
|
|
Failures []Failure `json:"failures"`
|
|
Missing []Entry
|
|
Extra []Entry
|
|
}
|
|
|
|
// Failure of a particular keyword for a path
|
|
type Failure struct {
|
|
Path string `json:"path"`
|
|
Keyword string `json:"keyword"`
|
|
Expected string `json:"expected"`
|
|
Got string `json:"got"`
|
|
}
|
|
|
|
// String returns a "pretty" formatting for a Failure
|
|
func (f Failure) String() string {
|
|
return fmt.Sprintf("%q: keyword %q: expected %s; got %s", f.Path, f.Keyword, f.Expected, f.Got)
|
|
}
|
|
|
|
// Check a root directory path against the DirectoryHierarchy, regarding only
|
|
// 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)
|
|
}
|
|
|
|
if err := os.Chdir(root); err != nil {
|
|
return nil, err
|
|
}
|
|
sort.Sort(byPos(creator.DH.Entries))
|
|
|
|
var result Result
|
|
for i, e := range creator.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:
|
|
filename := e.Path()
|
|
info, err := os.Lstat(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var kvs KeyVals
|
|
if creator.curSet != nil {
|
|
kvs = MergeSet(creator.curSet.Keywords, e.Keywords)
|
|
} else {
|
|
kvs = NewKeyVals(e.Keywords)
|
|
}
|
|
|
|
for _, kv := range kvs {
|
|
keywordFunc, ok := KeywordFuncs[kv.Keyword()]
|
|
if !ok {
|
|
return nil, fmt.Errorf("Unknown keyword %q for file %q", kv.Keyword(), e.Path())
|
|
}
|
|
if keywords != nil && !inSlice(kv.Keyword(), keywords) {
|
|
continue
|
|
}
|
|
fh, err := os.Open(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
curKeyVal, err := keywordFunc(filename, info, fh)
|
|
if err != nil {
|
|
fh.Close()
|
|
return nil, err
|
|
}
|
|
fh.Close()
|
|
if string(kv) != curKeyVal {
|
|
failure := Failure{Path: e.Path(), Keyword: kv.Keyword(), Expected: kv.Value(), Got: KeyVal(curKeyVal).Value()}
|
|
result.Failures = append(result.Failures, failure)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
// TarCheck is the tar equivalent of checking a file hierarchy spec against a tar stream to
|
|
// determine if files have been changed.
|
|
func TarCheck(tarDH, dh *DirectoryHierarchy, keywords []string) (*Result, error) {
|
|
var result Result
|
|
var err error
|
|
var tarRoot *Entry
|
|
|
|
for _, e := range tarDH.Entries {
|
|
if e.Name == "." {
|
|
tarRoot = &e
|
|
break
|
|
}
|
|
}
|
|
tarRoot.Next = &Entry{
|
|
Name: "seen",
|
|
Type: CommentType,
|
|
}
|
|
curDir := tarRoot
|
|
creator := dhCreator{DH: dh}
|
|
sort.Sort(byPos(creator.DH.Entries))
|
|
|
|
var outOfTree bool
|
|
for i, e := range creator.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:
|
|
if outOfTree {
|
|
return &result, fmt.Errorf("No parent node from %s", e.Path())
|
|
}
|
|
// TODO: handle the case where "." is not the first Entry to be found
|
|
tarEntry := curDir.Descend(e.Name)
|
|
if tarEntry == nil {
|
|
result.Missing = append(result.Missing, e)
|
|
continue
|
|
}
|
|
|
|
tarEntry.Next = &Entry{
|
|
Type: CommentType,
|
|
Name: "seen",
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// actual
|
|
var tarkvs KeyVals
|
|
if tarEntry.Set != nil {
|
|
tarkvs = MergeSet(tarEntry.Set.Keywords, tarEntry.Keywords)
|
|
} else {
|
|
tarkvs = NewKeyVals(tarEntry.Keywords)
|
|
}
|
|
|
|
for _, kv := range kvs {
|
|
if _, ok := KeywordFuncs[kv.Keyword()]; !ok {
|
|
return nil, fmt.Errorf("Unknown keyword %q for file %q", kv.Keyword(), e.Path())
|
|
}
|
|
if keywords != nil && !inSlice(kv.Keyword(), keywords) {
|
|
continue
|
|
}
|
|
if tarkv := tarkvs.Has(kv.Keyword()); tarkv != emptyKV {
|
|
if string(tarkv) != string(kv) {
|
|
failure := Failure{Path: tarEntry.Path(), Keyword: kv.Keyword(), Expected: kv.Value(), Got: tarkv.Value()}
|
|
result.Failures = append(result.Failures, failure)
|
|
}
|
|
}
|
|
}
|
|
// Step into a directory
|
|
if tarEntry.Prev != nil {
|
|
curDir = tarEntry
|
|
}
|
|
case DotDotType:
|
|
if outOfTree {
|
|
return &result, fmt.Errorf("No parent node.")
|
|
}
|
|
curDir = curDir.Ascend()
|
|
if curDir == nil {
|
|
outOfTree = true
|
|
}
|
|
}
|
|
}
|
|
result.Extra = filter(tarRoot, func(e *Entry) bool {
|
|
return e.Next == nil
|
|
})
|
|
return &result, err
|
|
}
|