2016-08-02 19:57:51 +00:00
|
|
|
package mtree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"sort"
|
2017-06-15 18:54:58 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-08-02 19:57:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultUpdateKeywords is the default set of keywords that can take updates to the files on disk
|
|
|
|
var DefaultUpdateKeywords = []Keyword{
|
|
|
|
"uid",
|
|
|
|
"gid",
|
|
|
|
"mode",
|
|
|
|
"time",
|
2017-06-15 18:54:58 +00:00
|
|
|
"xattr",
|
2016-08-02 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update attempts to set the attributes of root directory path, given the values of `keywords` in dh DirectoryHierarchy.
|
|
|
|
func Update(root string, dh *DirectoryHierarchy, keywords []Keyword, fs FsEval) ([]InodeDelta, 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))
|
|
|
|
|
|
|
|
results := []InodeDelta{}
|
|
|
|
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
|
|
|
|
}
|
2017-06-15 18:54:58 +00:00
|
|
|
logrus.Debugf("%#v", e)
|
2016-08-02 19:57:51 +00:00
|
|
|
continue
|
|
|
|
case RelativeType, FullType:
|
|
|
|
e.Set = creator.curSet
|
|
|
|
pathname, err := e.Path()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter the keywords to update on the file, from the keywords available for this entry:
|
2017-06-15 18:54:58 +00:00
|
|
|
var kvToUpdate []KeyVal
|
|
|
|
kvToUpdate = keyvalSelector(e.AllKeys(), keywords)
|
|
|
|
logrus.Debugf("kvToUpdate(%q): %#v", pathname, kvToUpdate)
|
2016-08-02 19:57:51 +00:00
|
|
|
|
2017-06-15 18:54:58 +00:00
|
|
|
for _, kv := range kvToUpdate {
|
|
|
|
if !InKeywordSlice(kv.Keyword().Prefix(), keywordPrefixes(keywords)) {
|
2016-08-02 19:57:51 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-06-15 18:54:58 +00:00
|
|
|
logrus.Debugf("finding function for %q (%q)", kv.Keyword(), kv.Keyword().Prefix())
|
|
|
|
ukFunc, ok := UpdateKeywordFuncs[kv.Keyword().Prefix()]
|
2016-08-02 19:57:51 +00:00
|
|
|
if !ok {
|
2017-06-15 18:54:58 +00:00
|
|
|
logrus.Debugf("no UpdateKeywordFunc for %s; skipping", kv.Keyword())
|
2016-08-02 19:57:51 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-06-26 18:24:10 +00:00
|
|
|
if _, err := ukFunc(pathname, kv); err != nil {
|
2016-08-02 19:57:51 +00:00
|
|
|
results = append(results, InodeDelta{
|
|
|
|
diff: ErrorDifference,
|
|
|
|
path: pathname,
|
|
|
|
old: e,
|
|
|
|
keys: []KeyDelta{
|
|
|
|
{
|
|
|
|
diff: ErrorDifference,
|
|
|
|
name: kv.Keyword(),
|
|
|
|
err: err,
|
|
|
|
},
|
|
|
|
}})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result of an "update" returns the produced results
|
|
|
|
type Result struct {
|
|
|
|
Path string
|
|
|
|
Keyword Keyword
|
|
|
|
Got string
|
|
|
|
}
|