mirror of
https://github.com/vbatts/go-mtree.git
synced 2024-11-22 00:15:39 +00:00
check: an initial pass at a validation check
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
6db2f462a1
commit
2fd41fb43f
5 changed files with 101 additions and 13 deletions
39
check.go
39
check.go
|
@ -8,6 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Result struct {
|
type Result struct {
|
||||||
|
// XXX perhaps this is a list of the failed files and keywords?
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrNotAllClear = fmt.Errorf("some keyword check failed validation")
|
var ErrNotAllClear = fmt.Errorf("some keyword check failed validation")
|
||||||
|
@ -24,6 +25,7 @@ func Check(root string, dh *DirectoryHierarchy) (*Result, error) {
|
||||||
}
|
}
|
||||||
sort.Sort(byPos(creator.DH.Entries))
|
sort.Sort(byPos(creator.DH.Entries))
|
||||||
|
|
||||||
|
var failed bool
|
||||||
for _, e := range creator.DH.Entries {
|
for _, e := range creator.DH.Entries {
|
||||||
switch e.Type {
|
switch e.Type {
|
||||||
case SpecialType:
|
case SpecialType:
|
||||||
|
@ -32,20 +34,39 @@ func Check(root string, dh *DirectoryHierarchy) (*Result, error) {
|
||||||
} else if e.Name == "/unset" {
|
} else if e.Name == "/unset" {
|
||||||
creator.curSet = nil
|
creator.curSet = nil
|
||||||
}
|
}
|
||||||
case DotDotType:
|
case RelativeType, FullType:
|
||||||
// TODO step
|
info, err := os.Lstat(filepath.Join(root, e.Path()))
|
||||||
case RelativeType:
|
|
||||||
// TODO determine path, and check keywords
|
|
||||||
// or maybe to Chdir when type=dir?
|
|
||||||
case FullType:
|
|
||||||
info, err := os.Lstat(filepath.Join(root, e.Name))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// TODO check against keywords present
|
|
||||||
_ = info
|
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())
|
||||||
|
}
|
||||||
|
curKeyVal, err := keywordFunc(filepath.Join(root, e.Path()), info)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if string(kv) != curKeyVal {
|
||||||
|
failed = true
|
||||||
|
fmt.Printf("%q: keyword %q: expected %s; got %s", e.Path(), kv.Keyword(), kv.Value(), KeyVal(curKeyVal).Value())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if failed {
|
||||||
|
return nil, ErrNotAllClear
|
||||||
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
20
check_test.go
Normal file
20
check_test.go
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package mtree
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCheck(t *testing.T) {
|
||||||
|
dh, err := Walk(".", nil, append(DefaultKeywords, "sha1"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := Check(".", dh)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
//log.Fatalf("%#v", dh)
|
||||||
|
log.Fatalf("%#v", res)
|
||||||
|
}
|
|
@ -47,7 +47,7 @@ type Entry struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Entry) Path() string {
|
func (e Entry) Path() string {
|
||||||
if e.Parent == nil {
|
if e.Parent == nil || e.Type == FullType {
|
||||||
return e.Name
|
return e.Name
|
||||||
}
|
}
|
||||||
return filepath.Join(e.Parent.Path(), e.Name)
|
return filepath.Join(e.Parent.Path(), e.Name)
|
||||||
|
|
46
keywords.go
46
keywords.go
|
@ -66,6 +66,52 @@ func keywordSelector(keyval, words []string) []string {
|
||||||
return retList
|
return retList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewKeyVals constructs a list of KeyVal from the list of strings, like "keyword=value"
|
||||||
|
func NewKeyVals(keyvals []string) KeyVals {
|
||||||
|
kvs := make(KeyVals, len(keyvals))
|
||||||
|
for i := range keyvals {
|
||||||
|
kvs[i] = KeyVal(keyvals[i])
|
||||||
|
}
|
||||||
|
return kvs
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyVals is a list of KeyVal
|
||||||
|
type KeyVals []KeyVal
|
||||||
|
|
||||||
|
// Has the "keyword" present in the list of KeyVal, and returns the
|
||||||
|
// corresponding KeyVal, else an empty string.
|
||||||
|
func (kvs KeyVals) Has(keyword string) KeyVal {
|
||||||
|
for i := range kvs {
|
||||||
|
if kvs[i].Keyword() == keyword {
|
||||||
|
return kvs[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return emptyKV
|
||||||
|
}
|
||||||
|
|
||||||
|
var emptyKV = KeyVal("")
|
||||||
|
|
||||||
|
// MergeSet takes the current setKeyVals, and then applies the entryKeyVals
|
||||||
|
// such that the entry's values win. The union is returned.
|
||||||
|
func MergeSet(setKeyVals, entryKeyVals []string) KeyVals {
|
||||||
|
retList := NewKeyVals(append([]string{}, setKeyVals...))
|
||||||
|
eKVs := NewKeyVals(entryKeyVals)
|
||||||
|
seenKeywords := []string{}
|
||||||
|
for i := range retList {
|
||||||
|
word := retList[i].Keyword()
|
||||||
|
if ekv := eKVs.Has(word); ekv != emptyKV {
|
||||||
|
retList[i] = ekv
|
||||||
|
}
|
||||||
|
seenKeywords = append(seenKeywords, word)
|
||||||
|
}
|
||||||
|
for i := range eKVs {
|
||||||
|
if !inSlice(eKVs[i].Keyword(), seenKeywords) {
|
||||||
|
retList = append(retList, eKVs[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return retList
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// DefaultKeywords has the several default keyword producers (uid, gid,
|
// DefaultKeywords has the several default keyword producers (uid, gid,
|
||||||
// mode, nlink, type, size, mtime)
|
// mode, nlink, type, size, mtime)
|
||||||
|
|
7
walk.go
7
walk.go
|
@ -20,9 +20,9 @@ type dhCreator struct {
|
||||||
|
|
||||||
var defaultSetKeywords = []string{"type=file", "nlink=1", "flags=none", "mode=0664"}
|
var defaultSetKeywords = []string{"type=file", "nlink=1", "flags=none", "mode=0664"}
|
||||||
|
|
||||||
//
|
// Walk from root directory and assemble the DirectoryHierarchy. excludes
|
||||||
// To be able to do a "walk" that produces an outcome with `/set ...` would
|
// provided are used to skip paths. keywords are the set to collect from the
|
||||||
// need a more linear walk, which this can not ensure.
|
// walked paths. The recommended default list is DefaultKeywords.
|
||||||
func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHierarchy, error) {
|
func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHierarchy, error) {
|
||||||
creator := dhCreator{DH: &DirectoryHierarchy{}}
|
creator := dhCreator{DH: &DirectoryHierarchy{}}
|
||||||
// TODO insert signature and metadata comments first (user, machine, tree, date)
|
// TODO insert signature and metadata comments first (user, machine, tree, date)
|
||||||
|
@ -107,6 +107,7 @@ func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHie
|
||||||
e := Entry{
|
e := Entry{
|
||||||
Name: filepath.Base(path),
|
Name: filepath.Base(path),
|
||||||
Pos: len(creator.DH.Entries),
|
Pos: len(creator.DH.Entries),
|
||||||
|
Type: RelativeType,
|
||||||
Set: creator.curSet,
|
Set: creator.curSet,
|
||||||
Parent: creator.curDir,
|
Parent: creator.curDir,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue