2016-03-11 20:53:20 +00:00
|
|
|
package mtree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
2016-04-06 16:48:35 +00:00
|
|
|
"path/filepath"
|
2016-03-11 20:53:20 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-03-16 19:59:34 +00:00
|
|
|
// ParseSpec reads a stream of an mtree specification, and returns the DirectoryHierarchy
|
2016-03-11 20:53:20 +00:00
|
|
|
func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) {
|
|
|
|
s := bufio.NewScanner(r)
|
|
|
|
i := int(0)
|
2016-03-18 20:30:54 +00:00
|
|
|
creator := dhCreator{
|
|
|
|
DH: &DirectoryHierarchy{},
|
|
|
|
}
|
2016-03-11 20:53:20 +00:00
|
|
|
for s.Scan() {
|
|
|
|
str := s.Text()
|
|
|
|
e := Entry{Pos: i}
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(str, "#"):
|
|
|
|
e.Raw = str
|
|
|
|
if strings.HasPrefix(str, "#mtree") {
|
|
|
|
e.Type = SignatureType
|
|
|
|
} else {
|
|
|
|
e.Type = CommentType
|
|
|
|
// from here, the comment could be "# key: value" metadata
|
|
|
|
// or a relative path hint
|
|
|
|
}
|
|
|
|
case str == "":
|
|
|
|
e.Type = BlankType
|
|
|
|
// nothing else to do here
|
|
|
|
case strings.HasPrefix(str, "/"):
|
|
|
|
e.Type = SpecialType
|
|
|
|
// collapse any escaped newlines
|
|
|
|
for {
|
|
|
|
if strings.HasSuffix(str, `\`) {
|
|
|
|
str = str[:len(str)-1]
|
|
|
|
s.Scan()
|
|
|
|
str += s.Text()
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// parse the options
|
|
|
|
f := strings.Fields(str)
|
|
|
|
e.Name = f[0]
|
|
|
|
e.Keywords = f[1:]
|
2016-03-18 20:30:54 +00:00
|
|
|
if e.Name == "/set" {
|
|
|
|
creator.curSet = &e
|
|
|
|
} else if e.Name == "/unset" {
|
|
|
|
creator.curSet = nil
|
|
|
|
}
|
2016-03-11 20:53:20 +00:00
|
|
|
case len(strings.Fields(str)) > 0 && strings.Fields(str)[0] == "..":
|
|
|
|
e.Type = DotDotType
|
|
|
|
e.Raw = str
|
2016-04-05 20:20:04 +00:00
|
|
|
if creator.curDir != nil {
|
|
|
|
creator.curDir = creator.curDir.Parent
|
|
|
|
}
|
2016-03-11 20:53:20 +00:00
|
|
|
// nothing else to do here
|
|
|
|
case len(strings.Fields(str)) > 0:
|
|
|
|
// collapse any escaped newlines
|
|
|
|
for {
|
|
|
|
if strings.HasSuffix(str, `\`) {
|
|
|
|
str = str[:len(str)-1]
|
|
|
|
s.Scan()
|
|
|
|
str += s.Text()
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// parse the options
|
|
|
|
f := strings.Fields(str)
|
2016-04-06 16:48:35 +00:00
|
|
|
e.Name = filepath.Clean(f[0])
|
|
|
|
if strings.Contains(e.Name, "/") {
|
2016-03-11 20:53:20 +00:00
|
|
|
e.Type = FullType
|
|
|
|
} else {
|
|
|
|
e.Type = RelativeType
|
|
|
|
}
|
|
|
|
e.Keywords = f[1:]
|
2016-04-05 20:20:04 +00:00
|
|
|
e.Parent = creator.curDir
|
2016-03-18 20:30:54 +00:00
|
|
|
for i := range e.Keywords {
|
|
|
|
kv := KeyVal(e.Keywords[i])
|
|
|
|
if kv.Keyword() == "type" {
|
|
|
|
if kv.Value() == "dir" {
|
|
|
|
creator.curDir = &e
|
|
|
|
} else {
|
|
|
|
creator.curEnt = &e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 20:53:20 +00:00
|
|
|
default:
|
|
|
|
// TODO(vbatts) log a warning?
|
|
|
|
continue
|
|
|
|
}
|
2016-03-18 20:30:54 +00:00
|
|
|
creator.DH.Entries = append(creator.DH.Entries, e)
|
2016-03-11 20:53:20 +00:00
|
|
|
i++
|
|
|
|
}
|
2016-03-18 20:30:54 +00:00
|
|
|
return creator.DH, s.Err()
|
2016-03-11 20:53:20 +00:00
|
|
|
}
|