aboutsummaryrefslogtreecommitdiffstats
path: root/finder
diff options
context:
space:
mode:
authorColin Cross2017-12-21 17:39:26 -0600
committerColin Cross2017-12-22 15:56:17 -0600
commita88c883e3efc19ba677c8bcca65fce73d057ada7 (patch)
tree4bbadbd1b60d75ef616605572b737e4505db93f9 /finder
parent8d6395c09d868751c85082d71356ad790e924df9 (diff)
downloadplatform-build-soong-a88c883e3efc19ba677c8bcca65fce73d057ada7.tar.gz
platform-build-soong-a88c883e3efc19ba677c8bcca65fce73d057ada7.tar.xz
platform-build-soong-a88c883e3efc19ba677c8bcca65fce73d057ada7.zip
Add a DirEntryInfo interface that is a subset of os.FileInfo
ioutil.ReadDir returns []os.FileInfo, which contains information on each entry in the directory that is only available by calling os.Lstat on the entry. Finder only the name and type (regular, directory or symlink) of the files, which on Linux kernels >= 2.6.4 is available in the return values of syscall.Getdents. In preparation for using syscall.Getdents, switch filesystem.ReadDir to return an interface that only contains the information that will be available from syscall.Getdents. Bug: 70897635 Test: m checkbuild Change-Id: Id2749d709a0f7b5a61abedde68549d4bf208a568
Diffstat (limited to 'finder')
-rw-r--r--finder/finder.go2
-rw-r--r--finder/fs/fs.go33
2 files changed, 29 insertions, 6 deletions
diff --git a/finder/finder.go b/finder/finder.go
index ce174757..89be0f5f 100644
--- a/finder/finder.go
+++ b/finder/finder.go
@@ -1384,7 +1384,7 @@ func (f *Finder) listDirSync(dir *pathMap) {
1384 f.onFsError(path, err) 1384 f.onFsError(path, err)
1385 // if listing the contents of the directory fails (presumably due to 1385 // if listing the contents of the directory fails (presumably due to
1386 // permission denied), then treat the directory as empty 1386 // permission denied), then treat the directory as empty
1387 children = []os.FileInfo{} 1387 children = nil
1388 } 1388 }
1389 1389
1390 var subdirs []string 1390 var subdirs []string
diff --git a/finder/fs/fs.go b/finder/fs/fs.go
index eff8ad07..3de54865 100644
--- a/finder/fs/fs.go
+++ b/finder/fs/fs.go
@@ -51,7 +51,7 @@ type FileSystem interface {
51 // getting information about files 51 // getting information about files
52 Open(name string) (file io.ReadCloser, err error) 52 Open(name string) (file io.ReadCloser, err error)
53 Lstat(path string) (stats os.FileInfo, err error) 53 Lstat(path string) (stats os.FileInfo, err error)
54 ReadDir(path string) (contents []os.FileInfo, err error) 54 ReadDir(path string) (contents []DirEntryInfo, err error)
55 55
56 InodeNumber(info os.FileInfo) (number uint64, err error) 56 InodeNumber(info os.FileInfo) (number uint64, err error)
57 DeviceNumber(info os.FileInfo) (number uint64, err error) 57 DeviceNumber(info os.FileInfo) (number uint64, err error)
@@ -67,18 +67,39 @@ type FileSystem interface {
67 ViewId() (id string) // Some unique id of the user accessing the filesystem 67 ViewId() (id string) // Some unique id of the user accessing the filesystem
68} 68}
69 69
70// DentryInfo is a subset of the functionality available through os.FileInfo that might be able
71// to be gleaned through only a syscall.Getdents without requiring a syscall.Lstat of every file.
72type DirEntryInfo interface {
73 Name() string
74 Mode() os.FileMode // the file type encoded as an os.FileMode
75 IsDir() bool
76}
77
78var _ DirEntryInfo = os.FileInfo(nil)
79
70// osFs implements FileSystem using the local disk. 80// osFs implements FileSystem using the local disk.
71type osFs struct{} 81type osFs struct{}
72 82
83var _ FileSystem = (*osFs)(nil)
84
73func (osFs) Open(name string) (io.ReadCloser, error) { return os.Open(name) } 85func (osFs) Open(name string) (io.ReadCloser, error) { return os.Open(name) }
74 86
75func (osFs) Lstat(path string) (stats os.FileInfo, err error) { 87func (osFs) Lstat(path string) (stats os.FileInfo, err error) {
76 return os.Lstat(path) 88 return os.Lstat(path)
77} 89}
78 90
79func (osFs) ReadDir(path string) (contents []os.FileInfo, err error) { 91func (osFs) ReadDir(path string) (contents []DirEntryInfo, err error) {
80 return ioutil.ReadDir(path) 92 entries, err := ioutil.ReadDir(path)
93 if err != nil {
94 return nil, err
95 }
96 for _, entry := range entries {
97 contents = append(contents, entry)
98 }
99
100 return contents, nil
81} 101}
102
82func (osFs) Rename(oldPath string, newPath string) error { 103func (osFs) Rename(oldPath string, newPath string) error {
83 return os.Rename(oldPath, newPath) 104 return os.Rename(oldPath, newPath)
84} 105}
@@ -154,6 +175,8 @@ type MockFs struct {
154 aggregatesLock sync.Mutex 175 aggregatesLock sync.Mutex
155} 176}
156 177
178var _ FileSystem = (*MockFs)(nil)
179
157type mockInode struct { 180type mockInode struct {
158 modTime time.Time 181 modTime time.Time
159 permTime time.Time 182 permTime time.Time
@@ -475,7 +498,7 @@ func (m *MockFs) PermTime(info os.FileInfo) (when time.Time, err error) {
475 fmt.Errorf("%v is not a mockFileInfo", info) 498 fmt.Errorf("%v is not a mockFileInfo", info)
476} 499}
477 500
478func (m *MockFs) ReadDir(path string) (contents []os.FileInfo, err error) { 501func (m *MockFs) ReadDir(path string) (contents []DirEntryInfo, err error) {
479 // update aggregates 502 // update aggregates
480 m.aggregatesLock.Lock() 503 m.aggregatesLock.Lock()
481 m.ReadDirCalls = append(m.ReadDirCalls, path) 504 m.ReadDirCalls = append(m.ReadDirCalls, path)
@@ -486,7 +509,7 @@ func (m *MockFs) ReadDir(path string) (contents []os.FileInfo, err error) {
486 if err != nil { 509 if err != nil {
487 return nil, err 510 return nil, err
488 } 511 }
489 results := []os.FileInfo{} 512 results := []DirEntryInfo{}
490 dir, err := m.getDir(path, false) 513 dir, err := m.getDir(path, false)
491 if err != nil { 514 if err != nil {
492 return nil, err 515 return nil, err