2016-03-11 20:53:20 +00:00
|
|
|
package mtree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DirectoryHierarchy is the mapped structure for an mtree directory hierarchy
|
|
|
|
// spec
|
|
|
|
type DirectoryHierarchy struct {
|
|
|
|
Entries []Entry
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo simplifies the output of the resulting hierarchy spec
|
|
|
|
func (dh DirectoryHierarchy) WriteTo(w io.Writer) (n int64, err error) {
|
|
|
|
sort.Sort(byPos(dh.Entries))
|
|
|
|
var sum int64
|
|
|
|
for _, e := range dh.Entries {
|
2016-07-21 01:18:27 +00:00
|
|
|
str := e.String()
|
|
|
|
i, err := io.WriteString(w, str+"\n")
|
2016-03-11 20:53:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return sum, err
|
|
|
|
}
|
|
|
|
sum += int64(i)
|
|
|
|
}
|
|
|
|
return sum, nil
|
|
|
|
}
|
2016-07-29 14:18:17 +00:00
|
|
|
|
2016-11-16 19:43:05 +00:00
|
|
|
// UsedKeywords collects and returns all the keywords used in a
|
2016-07-29 14:18:17 +00:00
|
|
|
// a DirectoryHierarchy
|
2016-11-18 00:47:31 +00:00
|
|
|
func (dh DirectoryHierarchy) UsedKeywords() []Keyword {
|
|
|
|
usedkeywords := []Keyword{}
|
2016-11-16 19:15:42 +00:00
|
|
|
for _, e := range dh.Entries {
|
|
|
|
switch e.Type {
|
|
|
|
case FullType, RelativeType, SpecialType:
|
|
|
|
if e.Type != SpecialType || e.Name == "/set" {
|
|
|
|
kvs := e.Keywords
|
|
|
|
for _, kv := range kvs {
|
2017-09-29 10:33:41 +00:00
|
|
|
kw := KeyVal(kv).Keyword().Prefix()
|
2016-11-18 00:47:31 +00:00
|
|
|
if !InKeywordSlice(kw, usedkeywords) {
|
|
|
|
usedkeywords = append(usedkeywords, KeywordSynonym(string(kw)))
|
2016-07-29 14:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 19:15:42 +00:00
|
|
|
return usedkeywords
|
2016-07-29 14:18:17 +00:00
|
|
|
}
|