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

@ -50,7 +50,11 @@ func Check(root string, dh *DirectoryHierarchy, keywords []string) (*Result, err
creator.curSet = nil creator.curSet = nil
} }
case RelativeType, FullType: case RelativeType, FullType:
info, err := os.Lstat(e.Path()) pathname, err := e.Path()
if err != nil {
return nil, err
}
info, err := os.Lstat(pathname)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -65,17 +69,17 @@ func Check(root string, dh *DirectoryHierarchy, keywords []string) (*Result, err
for _, kv := range kvs { for _, kv := range kvs {
keywordFunc, ok := KeywordFuncs[kv.Keyword()] keywordFunc, ok := KeywordFuncs[kv.Keyword()]
if !ok { if !ok {
return nil, fmt.Errorf("Unknown keyword %q for file %q", kv.Keyword(), e.Path()) return nil, fmt.Errorf("Unknown keyword %q for file %q", kv.Keyword(), pathname)
} }
if keywords != nil && !inSlice(kv.Keyword(), keywords) { if keywords != nil && !inSlice(kv.Keyword(), keywords) {
continue continue
} }
curKeyVal, err := keywordFunc(filepath.Join(root, e.Path()), info) curKeyVal, err := keywordFunc(filepath.Join(root, pathname), info)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if string(kv) != curKeyVal { if string(kv) != curKeyVal {
failure := Failure{Path: e.Path(), Keyword: kv.Keyword(), Expected: kv.Value(), Got: KeyVal(curKeyVal).Value()} failure := Failure{Path: pathname, Keyword: kv.Keyword(), Expected: kv.Value(), Got: KeyVal(curKeyVal).Value()}
result.Failures = append(result.Failures, failure) result.Failures = append(result.Failures, failure)
} }
} }

View file

@ -208,3 +208,38 @@ func TestIgnoreComments(t *testing.T) {
t.Fatal(res.Failures) t.Fatal(res.Failures)
} }
} }
func TestCheckNeedsEncoding(t *testing.T) {
dir, err := ioutil.TempDir("", "test-needs-encoding")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
fh, err := os.Create(filepath.Join(dir, "file[ "))
if err != nil {
t.Fatal(err)
}
if err := fh.Close(); err != nil {
t.Error(err)
}
fh, err = os.Create(filepath.Join(dir, " , should work"))
if err != nil {
t.Fatal(err)
}
if err := fh.Close(); err != nil {
t.Error(err)
}
dh, err := Walk(dir, nil, DefaultKeywords)
if err != nil {
t.Fatal(err)
}
res, err := Check(dir, dh, nil)
if err != nil {
t.Fatal(err)
}
if len(res.Failures) > 0 {
t.Fatal(res.Failures)
}
}

View file

@ -19,7 +19,11 @@ func (dh DirectoryHierarchy) WriteTo(w io.Writer) (n int64, err error) {
sort.Sort(byPos(dh.Entries)) sort.Sort(byPos(dh.Entries))
var sum int64 var sum int64
for _, e := range dh.Entries { 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 { if err != nil {
return sum, err return sum, err
} }
@ -47,28 +51,39 @@ type Entry struct {
} }
// Path provides the full path of the file, despite RelativeType or FullType // Path provides the full path of the file, despite RelativeType or FullType
func (e Entry) Path() string { func (e Entry) Path() (string, error) {
if e.Parent == nil || e.Type == FullType { decodedName, err := Unvis(e.Name)
return filepath.Clean(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 != "" { if e.Raw != "" {
return e.Raw return e.Raw, nil
} }
if e.Type == BlankType { if e.Type == BlankType {
return "" return "", nil
} }
if e.Type == DotDotType { 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) { 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 // EntryType are the formats of lines in an mtree spec file

View file

@ -1,15 +1,23 @@
package mtree package mtree
// #include "vis.h" // #include "vis.h"
// #include <stdlib.h>
import "C" import "C"
import "fmt" import (
"fmt"
"unsafe"
)
func Unvis(str string) (string, error) { // Unvis is a wrapper for the C implementation of unvis, which decodes a string
dst := new(C.char) // that potentially has characters that are encoded with Vis
ret := C.strunvis(dst, C.CString(str)) func Unvis(src string) (string, error) {
if ret == 0 { cDst, cSrc := C.CString(string(make([]byte, len(src)+1))), C.CString(src)
return "", fmt.Errorf("failed to encode string") defer C.free(unsafe.Pointer(cDst))
defer C.free(unsafe.Pointer(cSrc))
ret := C.strunvis(cDst, cSrc)
if ret == -1 {
return "", fmt.Errorf("failed to decode: %q", src)
} }
return C.GoString(dst), nil return C.GoString(cDst), nil
} }

26
vis.go
View file

@ -1,14 +1,26 @@
package mtree package mtree
// #include "vis.h" // #include "vis.h"
// #include <stdlib.h>
import "C" import "C"
import "fmt" import (
"fmt"
"math"
"unsafe"
)
func Vis(str string) (string, error) { // Vis is a wrapper of the C implementation of the function vis, which encodes
dst := new(C.char) // a character with a particular format/style
ret := C.strvis(dst, C.CString(str), C.VIS_WHITE|C.VIS_OCTAL|C.VIS_GLOB) func Vis(src string) (string, error) {
if ret == 0 { // dst needs to be 4 times the length of str, must check appropriate size
return "", fmt.Errorf("failed to encode string") if uint32(len(src)*4+1) >= math.MaxUint32/4 {
return "", fmt.Errorf("failed to encode: %q", src)
} }
return C.GoString(dst), nil dst := string(make([]byte, 4*len(src)+1))
cDst, cSrc := C.CString(dst), C.CString(src)
defer C.free(unsafe.Pointer(cDst))
defer C.free(unsafe.Pointer(cSrc))
C.strvis(cDst, cSrc, C.VIS_WHITE|C.VIS_OCTAL|C.VIS_GLOB)
return C.GoString(cDst), nil
} }

View file

@ -9,13 +9,17 @@ func TestVis(t *testing.T) {
{"[", "\\133"}, {"[", "\\133"},
{" ", "\\040"}, {" ", "\\040"},
{" ", "\\011"}, {" ", "\\011"},
{"dir with space", "dir\\040with\\040space"},
{"consec spaces", "consec\\040\\040\\040spaces"},
{"trailingsymbol[", "trailingsymbol\\133"},
{" [ leadingsymbols", "\\040\\133\\040leadingsymbols"},
{"no_need_for_encoding", "no_need_for_encoding"},
} }
for i := range testset { for i := range testset {
got, err := Vis(testset[i].Src) got, err := Vis(testset[i].Src)
if err != nil { if err != nil {
t.Errorf("working with %q: %s", testset[i].Src, err) t.Errorf("working with %q: %s", testset[i].Src, err)
continue
} }
if got != testset[i].Dest { if got != testset[i].Dest {
t.Errorf("expected %#v; got %#v", testset[i].Dest, got) t.Errorf("expected %#v; got %#v", testset[i].Dest, got)
@ -33,3 +37,13 @@ func TestVis(t *testing.T) {
} }
} }
} }
// The resulting string of Vis output could potentially be four times longer than
// the original. Vis must handle this possibility.
func TestVisLength(t *testing.T) {
testString := "All work and no play makes Jack a dull boy\n"
for i := 0; i < 20; i++ {
Vis(testString)
testString = testString + testString
}
}

13
walk.go
View file

@ -53,9 +53,13 @@ func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHie
// Insert a comment of the full path of the directory's name // Insert a comment of the full path of the directory's name
if creator.curDir != nil { if creator.curDir != nil {
dirname, err := creator.curDir.Path()
if err != nil {
return err
}
creator.DH.Entries = append(creator.DH.Entries, Entry{ creator.DH.Entries = append(creator.DH.Entries, Entry{
Pos: len(creator.DH.Entries), Pos: len(creator.DH.Entries),
Raw: "# " + filepath.Join(creator.curDir.Path(), entryPathName), Raw: "# " + filepath.Join(dirname, entryPathName),
Type: CommentType, Type: CommentType,
}) })
} else { } else {
@ -113,9 +117,12 @@ func Walk(root string, exlcudes []ExcludeFunc, keywords []string) (*DirectoryHie
} }
} }
} }
encodedEntryName, err := Vis(entryPathName)
if err != nil {
return err
}
e := Entry{ e := Entry{
Name: entryPathName, Name: encodedEntryName,
Pos: len(creator.DH.Entries), Pos: len(creator.DH.Entries),
Type: RelativeType, Type: RelativeType,
Set: creator.curSet, Set: creator.curSet,