From 8dce57e169255608b46bb563bb7de1581908aea6 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 30 Nov 2011 12:04:16 -0500 Subject: os: new FileInfo, FileMode types + update tree R=golang-dev, r, r, gri, bradfitz, iant, iant, nigeltao, n13m3y3r CC=golang-dev https://golang.org/cl/5416060 --- src/pkg/path/filepath/match.go | 2 +- src/pkg/path/filepath/path.go | 32 ++++++++++++++------------------ src/pkg/path/filepath/path_test.go | 9 +++++---- 3 files changed, 20 insertions(+), 23 deletions(-) (limited to 'src/pkg/path/filepath') diff --git a/src/pkg/path/filepath/match.go b/src/pkg/path/filepath/match.go index 8cf1f9ad10..c3678f541d 100644 --- a/src/pkg/path/filepath/match.go +++ b/src/pkg/path/filepath/match.go @@ -260,7 +260,7 @@ func glob(dir, pattern string, matches []string) (m []string, e error) { if err != nil { return } - if !fi.IsDirectory() { + if !fi.IsDir() { return } d, err := os.Open(dir) diff --git a/src/pkg/path/filepath/path.go b/src/pkg/path/filepath/path.go index 3656227ff0..e3d6c342ca 100644 --- a/src/pkg/path/filepath/path.go +++ b/src/pkg/path/filepath/path.go @@ -223,7 +223,7 @@ func EvalSymlinks(path string) (string, error) { if err != nil { return "", err } - if !fi.IsSymlink() { + if fi.Mode()&os.ModeSymlink == 0 { b.WriteString(p) if path != "" { b.WriteRune(Separator) @@ -345,19 +345,19 @@ var SkipDir = errors.New("skip this directory") // sole exception is that if path is a directory and the function returns the // special value SkipDir, the contents of the directory are skipped // and processing continues as usual on the next file. -type WalkFunc func(path string, info *os.FileInfo, err error) error +type WalkFunc func(path string, info os.FileInfo, err error) error // walk recursively descends path, calling w. -func walk(path string, info *os.FileInfo, walkFn WalkFunc) error { +func walk(path string, info os.FileInfo, walkFn WalkFunc) error { err := walkFn(path, info, nil) if err != nil { - if info.IsDirectory() && err == SkipDir { + if info.IsDir() && err == SkipDir { return nil } return err } - if !info.IsDirectory() { + if !info.IsDir() { return nil } @@ -367,7 +367,7 @@ func walk(path string, info *os.FileInfo, walkFn WalkFunc) error { } for _, fileInfo := range list { - if err = walk(Join(path, fileInfo.Name), fileInfo, walkFn); err != nil { + if err = walk(Join(path, fileInfo.Name()), fileInfo, walkFn); err != nil { return err } } @@ -390,7 +390,7 @@ func Walk(root string, walkFn WalkFunc) error { // readDir reads the directory named by dirname and returns // a sorted list of directory entries. // Copied from io/ioutil to avoid the circular import. -func readDir(dirname string) ([]*os.FileInfo, error) { +func readDir(dirname string) ([]os.FileInfo, error) { f, err := os.Open(dirname) if err != nil { return nil, err @@ -400,20 +400,16 @@ func readDir(dirname string) ([]*os.FileInfo, error) { if err != nil { return nil, err } - fi := make(fileInfoList, len(list)) - for i := range list { - fi[i] = &list[i] - } - sort.Sort(fi) - return fi, nil + sort.Sort(byName(list)) + return list, nil } -// A dirList implements sort.Interface. -type fileInfoList []*os.FileInfo +// byName implements sort.Interface. +type byName []os.FileInfo -func (f fileInfoList) Len() int { return len(f) } -func (f fileInfoList) Less(i, j int) bool { return f[i].Name < f[j].Name } -func (f fileInfoList) Swap(i, j int) { f[i], f[j] = f[j], f[i] } +func (f byName) Len() int { return len(f) } +func (f byName) Less(i, j int) bool { return f[i].Name() < f[j].Name() } +func (f byName) Swap(i, j int) { f[i], f[j] = f[j], f[i] } // Base returns the last element of path. // Trailing path separators are removed before extracting the last element. diff --git a/src/pkg/path/filepath/path_test.go b/src/pkg/path/filepath/path_test.go index 983cc85c8e..fab5adc102 100644 --- a/src/pkg/path/filepath/path_test.go +++ b/src/pkg/path/filepath/path_test.go @@ -318,7 +318,7 @@ func checkMarks(t *testing.T, report bool) { // Assumes that each node name is unique. Good enough for a test. // If clear is true, any incoming error is cleared before return. The errors // are always accumulated, though. -func mark(path string, info *os.FileInfo, err error, errors *[]error, clear bool) error { +func mark(path string, info os.FileInfo, err error, errors *[]error, clear bool) error { if err != nil { *errors = append(*errors, err) if clear { @@ -326,8 +326,9 @@ func mark(path string, info *os.FileInfo, err error, errors *[]error, clear bool } return err } + name := info.Name() walkTree(tree, tree.name, func(path string, n *Node) { - if n.name == info.Name { + if n.name == name { n.mark++ } }) @@ -338,7 +339,7 @@ func TestWalk(t *testing.T) { makeTree(t) errors := make([]error, 0, 10) clear := true - markFn := func(path string, info *os.FileInfo, err error) error { + markFn := func(path string, info os.FileInfo, err error) error { return mark(path, info, err, &errors, clear) } // Expect no errors. @@ -600,7 +601,7 @@ func TestAbs(t *testing.T) { t.Errorf("Abs(%q) error: %v", path, err) } absinfo, err := os.Stat(abspath) - if err != nil || absinfo.Ino != info.Ino { + if err != nil || !absinfo.(*os.FileStat).SameFile(info.(*os.FileStat)) { t.Errorf("Abs(%q)=%q, not the same file", path, abspath) } if !filepath.IsAbs(abspath) { -- cgit v1.3