OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / spf13 / afero / path.go
1 // Copyright ©2015 The Go Authors
2 // Copyright ©2015 Steve Francia <spf@spf13.com>
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 package afero
17
18 import (
19         "os"
20         "path/filepath"
21         "sort"
22 )
23
24 // readDirNames reads the directory named by dirname and returns
25 // a sorted list of directory entries.
26 // adapted from https://golang.org/src/path/filepath/path.go
27 func readDirNames(fs Fs, dirname string) ([]string, error) {
28         f, err := fs.Open(dirname)
29         if err != nil {
30                 return nil, err
31         }
32         names, err := f.Readdirnames(-1)
33         f.Close()
34         if err != nil {
35                 return nil, err
36         }
37         sort.Strings(names)
38         return names, nil
39 }
40
41 // walk recursively descends path, calling walkFn
42 // adapted from https://golang.org/src/path/filepath/path.go
43 func walk(fs Fs, path string, info os.FileInfo, walkFn filepath.WalkFunc) error {
44         err := walkFn(path, info, nil)
45         if err != nil {
46                 if info.IsDir() && err == filepath.SkipDir {
47                         return nil
48                 }
49                 return err
50         }
51
52         if !info.IsDir() {
53                 return nil
54         }
55
56         names, err := readDirNames(fs, path)
57         if err != nil {
58                 return walkFn(path, info, err)
59         }
60
61         for _, name := range names {
62                 filename := filepath.Join(path, name)
63                 fileInfo, err := lstatIfOs(fs, filename)
64                 if err != nil {
65                         if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir {
66                                 return err
67                         }
68                 } else {
69                         err = walk(fs, filename, fileInfo, walkFn)
70                         if err != nil {
71                                 if !fileInfo.IsDir() || err != filepath.SkipDir {
72                                         return err
73                                 }
74                         }
75                 }
76         }
77         return nil
78 }
79
80 // if the filesystem is OsFs use Lstat, else use fs.Stat
81 func lstatIfOs(fs Fs, path string) (info os.FileInfo, err error) {
82         _, ok := fs.(*OsFs)
83         if ok {
84                 info, err = os.Lstat(path)
85         } else {
86                 info, err = fs.Stat(path)
87         }
88         return
89 }
90
91 // Walk walks the file tree rooted at root, calling walkFn for each file or
92 // directory in the tree, including root. All errors that arise visiting files
93 // and directories are filtered by walkFn. The files are walked in lexical
94 // order, which makes the output deterministic but means that for very
95 // large directories Walk can be inefficient.
96 // Walk does not follow symbolic links.
97
98 func (a Afero) Walk(root string, walkFn filepath.WalkFunc) error {
99         return Walk(a.Fs, root, walkFn)
100 }
101
102 func Walk(fs Fs, root string, walkFn filepath.WalkFunc) error {
103         info, err := lstatIfOs(fs, root)
104         if err != nil {
105                 return walkFn(root, nil, err)
106         }
107         return walk(fs, root, info, walkFn)
108 }