2016-03-16 19:59:34 +00:00
|
|
|
package mtree
|
|
|
|
|
2016-03-18 20:31:12 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
2016-07-22 22:01:54 +00:00
|
|
|
"strings"
|
2016-03-18 20:31:12 +00:00
|
|
|
)
|
|
|
|
|
2016-04-05 20:20:04 +00:00
|
|
|
// Result of a Check
|
2016-03-16 19:59:34 +00:00
|
|
|
type Result struct {
|
2016-07-20 22:09:27 +00:00
|
|
|
// list of any failures in the Check
|
|
|
|
Failures []Failure `json:"failures"`
|
2016-07-25 19:39:55 +00:00
|
|
|
Missing []Entry `json:"missing,omitempty"`
|
|
|
|
Extra []Entry `json:"extra,omitempty"`
|
2016-03-16 19:59:34 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 20:20:04 +00:00
|
|
|
// Failure of a particular keyword for a path
|
2016-04-05 15:44:55 +00:00
|
|
|
type Failure struct {
|
2016-07-20 22:09:27 +00:00
|
|
|
Path string `json:"path"`
|
|
|
|
Keyword string `json:"keyword"`
|
|
|
|
Expected string `json:"expected"`
|
|
|
|
Got string `json:"got"`
|
2016-04-05 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 20:20:04 +00:00
|
|
|
// String returns a "pretty" formatting for a Failure
|
2016-04-05 15:44:55 +00:00
|
|
|
func (f Failure) String() string {
|
|
|
|
return fmt.Sprintf("%q: keyword %q: expected %s; got %s", f.Path, f.Keyword, f.Expected, f.Got)
|
|
|
|
}
|
2016-03-18 20:31:12 +00:00
|
|
|
|
2016-04-12 20:49:52 +00:00
|
|
|
// 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) {
|
2016-03-18 20:31:12 +00:00
|
|
|
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))
|
|
|
|
|
2016-04-05 15:44:55 +00:00
|
|
|
var result Result
|
2016-07-15 12:58:26 +00:00
|
|
|
for i, e := range creator.DH.Entries {
|
2016-03-18 20:31:12 +00:00
|
|
|
switch e.Type {
|
|
|
|
case SpecialType:
|
|
|
|
if e.Name == "/set" {
|
2016-07-15 12:58:26 +00:00
|
|
|
creator.curSet = &creator.DH.Entries[i]
|
2016-03-18 20:31:12 +00:00
|
|
|
} else if e.Name == "/unset" {
|
|
|
|
creator.curSet = nil
|
|
|
|
}
|
2016-03-23 20:58:16 +00:00
|
|
|
case RelativeType, FullType:
|
2016-07-21 01:18:27 +00:00
|
|
|
pathname, err := e.Path()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info, err := os.Lstat(pathname)
|
2016-03-18 20:31:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-23 20:58:16 +00:00
|
|
|
|
|
|
|
var kvs KeyVals
|
|
|
|
if creator.curSet != nil {
|
|
|
|
kvs = MergeSet(creator.curSet.Keywords, e.Keywords)
|
|
|
|
} else {
|
|
|
|
kvs = NewKeyVals(e.Keywords)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
2016-07-22 22:01:54 +00:00
|
|
|
kw := kv.Keyword()
|
|
|
|
// 'tar_time' keyword evaluation wins against 'time' keyword evaluation
|
|
|
|
if kv.Keyword() == "time" && inSlice("tar_time", keywords) {
|
|
|
|
kw = "tar_time"
|
|
|
|
tartime := fmt.Sprintf("%s.%s", (strings.Split(kv.Value(), ".")[0]), "000000000")
|
|
|
|
kv = KeyVal(KeyVal(kw).ChangeValue(tartime))
|
|
|
|
}
|
|
|
|
|
|
|
|
keywordFunc, ok := KeywordFuncs[kw]
|
2016-03-23 20:58:16 +00:00
|
|
|
if !ok {
|
2016-07-21 01:18:27 +00:00
|
|
|
return nil, fmt.Errorf("Unknown keyword %q for file %q", kv.Keyword(), pathname)
|
2016-03-23 20:58:16 +00:00
|
|
|
}
|
2016-07-14 16:21:47 +00:00
|
|
|
if keywords != nil && !inSlice(kv.Keyword(), keywords) {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-21 01:18:27 +00:00
|
|
|
fh, err := os.Open(pathname)
|
2016-03-23 20:58:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-21 01:18:27 +00:00
|
|
|
curKeyVal, err := keywordFunc(pathname, info, fh)
|
2016-04-15 22:39:18 +00:00
|
|
|
if err != nil {
|
|
|
|
fh.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fh.Close()
|
2016-03-23 20:58:16 +00:00
|
|
|
if string(kv) != curKeyVal {
|
2016-07-21 01:18:27 +00:00
|
|
|
failure := Failure{Path: pathname, Keyword: kv.Keyword(), Expected: kv.Value(), Got: KeyVal(curKeyVal).Value()}
|
2016-04-05 15:44:55 +00:00
|
|
|
result.Failures = append(result.Failures, failure)
|
2016-03-23 20:58:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-18 20:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-05 15:44:55 +00:00
|
|
|
return &result, nil
|
2016-03-16 19:59:34 +00:00
|
|
|
}
|
2016-06-28 20:40:35 +00:00
|
|
|
|
|
|
|
// 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:
|
2016-07-21 01:18:27 +00:00
|
|
|
pathname, err := e.Path()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-28 20:40:35 +00:00
|
|
|
if outOfTree {
|
2016-07-21 01:18:27 +00:00
|
|
|
return &result, fmt.Errorf("No parent node from %s", pathname)
|
2016-06-28 20:40:35 +00:00
|
|
|
}
|
|
|
|
// 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 {
|
2016-07-21 01:18:27 +00:00
|
|
|
|
2016-06-28 20:40:35 +00:00
|
|
|
if _, ok := KeywordFuncs[kv.Keyword()]; !ok {
|
2016-07-21 01:18:27 +00:00
|
|
|
return nil, fmt.Errorf("Unknown keyword %q for file %q", kv.Keyword(), pathname)
|
2016-06-28 20:40:35 +00:00
|
|
|
}
|
|
|
|
if keywords != nil && !inSlice(kv.Keyword(), keywords) {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-21 01:18:27 +00:00
|
|
|
tarpath, err := tarEntry.Path()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-28 20:40:35 +00:00
|
|
|
if tarkv := tarkvs.Has(kv.Keyword()); tarkv != emptyKV {
|
|
|
|
if string(tarkv) != string(kv) {
|
2016-07-21 01:18:27 +00:00
|
|
|
failure := Failure{Path: tarpath, Keyword: kv.Keyword(), Expected: kv.Value(), Got: tarkv.Value()}
|
2016-06-28 20:40:35 +00:00
|
|
|
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
|
|
|
|
}
|