1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2025-10-05 12:51:00 +00:00

vis: refactored code to reflect using vis/unvis for file names

Added some more test cases for `vis`ing and `unvis`ing
strings, and a test case that walks/checks a directory with
filenames that require encoding. Had to change Path() and String()
to account for possible errors Vis() and Unvis() could return.

Signed-off-by: Stephen Chung <schung@redhat.com>
This commit is contained in:
Stephen Chung 2016-07-20 21:18:27 -04:00
parent 9bec5e46b7
commit c4d09ce5f7
7 changed files with 129 additions and 34 deletions

View file

@ -19,7 +19,11 @@ func (dh DirectoryHierarchy) WriteTo(w io.Writer) (n int64, err error) {
sort.Sort(byPos(dh.Entries))
var sum int64
for _, e := range dh.Entries {
i, err := io.WriteString(w, e.String()+"\n")
str, err := e.String()
if err != nil {
return sum, err
}
i, err := io.WriteString(w, str+"\n")
if err != nil {
return sum, err
}
@ -47,28 +51,39 @@ type Entry struct {
}
// Path provides the full path of the file, despite RelativeType or FullType
func (e Entry) Path() string {
if e.Parent == nil || e.Type == FullType {
return filepath.Clean(e.Name)
func (e Entry) Path() (string, error) {
decodedName, err := Unvis(e.Name)
if err != nil {
return "", err
}
return filepath.Clean(filepath.Join(e.Parent.Path(), e.Name))
if e.Parent == nil || e.Type == FullType {
return filepath.Clean(decodedName), nil
}
parentName, err := e.Parent.Path()
if err != nil {
return "", err
}
return filepath.Clean(filepath.Join(parentName, decodedName)), nil
}
func (e Entry) String() string {
func (e Entry) String() (string, error) {
if e.Raw != "" {
return e.Raw
return e.Raw, nil
}
if e.Type == BlankType {
return ""
return "", nil
}
if e.Type == DotDotType {
return e.Name
return e.Name, nil
}
decodedName, err := Unvis(e.Name)
if err != nil {
return "", err
}
// TODO(vbatts) if type is RelativeType and a keyword of not type=dir
if e.Type == SpecialType || e.Type == FullType || inSlice("type=dir", e.Keywords) {
return fmt.Sprintf("%s %s", e.Name, strings.Join(e.Keywords, " "))
return fmt.Sprintf("%s %s", decodedName, strings.Join(e.Keywords, " ")), nil
}
return fmt.Sprintf(" %s %s", e.Name, strings.Join(e.Keywords, " "))
return fmt.Sprintf(" %s %s", decodedName, strings.Join(e.Keywords, " ")), nil
}
// EntryType are the formats of lines in an mtree spec file