This commit is contained in:
Vincent Batts 2016-04-15 18:39:18 -04:00
parent d5aab78911
commit 0b85ce1f81
11 changed files with 529 additions and 131 deletions

73
walk.go
View file

@ -1,6 +1,7 @@
package mtree
import (
"io"
"os"
"path/filepath"
"sort"
@ -11,13 +12,6 @@ import (
// returns true, then the path is not included in the spec.
type ExcludeFunc func(path string, info os.FileInfo) bool
type dhCreator struct {
DH *DirectoryHierarchy
curSet *Entry
curDir *Entry
curEnt *Entry
}
var defaultSetKeywords = []string{"type=file", "nlink=1", "flags=none", "mode=0664"}
// Walk from root directory and assemble the DirectoryHierarchy. excludes
@ -68,9 +62,24 @@ func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHie
Keywords: keywordSelector(defaultSetKeywords, keywords),
}
for _, keyword := range SetKeywords {
if str, err := KeywordFuncs[keyword](path, info); err == nil && str != "" {
e.Keywords = append(e.Keywords, str)
} else if err != nil {
err := func() error {
var r io.Reader
if info.Mode().IsRegular() {
fh, err := os.Open(path)
if err != nil {
return err
}
defer fh.Close()
r = fh
}
if str, err := KeywordFuncs[keyword](path, info, r); err == nil && str != "" {
e.Keywords = append(e.Keywords, str)
} else if err != nil {
return err
}
return nil
}()
if err != nil {
return err
}
}
@ -80,9 +89,26 @@ func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHie
// check the attributes of the /set keywords and re-set if changed
klist := []string{}
for _, keyword := range SetKeywords {
if str, err := KeywordFuncs[keyword](path, info); err == nil && str != "" {
klist = append(klist, str)
} else if err != nil {
err := func() error {
var r io.Reader
if info.Mode().IsRegular() {
fh, err := os.Open(path)
if err != nil {
return err
}
defer fh.Close()
r = fh
}
str, err := KeywordFuncs[keyword](path, info, r)
if err != nil {
return err
}
if str != "" {
klist = append(klist, str)
}
return nil
}()
if err != nil {
return err
}
}
@ -114,11 +140,26 @@ func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHie
Parent: creator.curDir,
}
for _, keyword := range keywords {
if str, err := KeywordFuncs[keyword](path, info); err == nil && str != "" {
if !inSlice(str, creator.curSet.Keywords) {
err := func() error {
var r io.Reader
if info.Mode().IsRegular() {
fh, err := os.Open(path)
if err != nil {
return err
}
defer fh.Close()
r = fh
}
str, err := KeywordFuncs[keyword](path, info, r)
if err != nil {
return err
}
if str != "" && !inSlice(str, creator.curSet.Keywords) {
e.Keywords = append(e.Keywords, str)
}
} else if err != nil {
return nil
}()
if err != nil {
return err
}
}