summaryrefslogtreecommitdiff
path: root/src/os/dir_windows.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2020-10-09 11:49:59 -0400
committerRuss Cox <rsc@golang.org>2020-10-20 00:59:20 +0000
commita4ede9f9a6254360d39d0f45aec133c355ac6b2a (patch)
treeb7e25cc247303143957f1937a9cfdd3e79fd8004 /src/os/dir_windows.go
parent8fe372c7b36b4d078c871a26e10b427c41275ecd (diff)
downloadgo-git-a4ede9f9a6254360d39d0f45aec133c355ac6b2a.tar.gz
os: add File.ReadDir method and DirEntry type
ReadDir provides a portable, efficient way to read a directory and discover the type of directory entries. This enables a more efficient file system walk, yet to be added. See #41467 for the proposal review for the API. Fixes #41467. Change-Id: I461a526793ae46df48821aa448b04f1705546739 Reviewed-on: https://go-review.googlesource.com/c/go/+/261540 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/os/dir_windows.go')
-rw-r--r--src/os/dir_windows.go51
1 files changed, 25 insertions, 26 deletions
diff --git a/src/os/dir_windows.go b/src/os/dir_windows.go
index 9e5d6bd505..1c3f2f0d57 100644
--- a/src/os/dir_windows.go
+++ b/src/os/dir_windows.go
@@ -10,20 +10,14 @@ import (
"syscall"
)
-func (file *File) readdir(n int) (fi []FileInfo, err error) {
- if file == nil {
- return nil, syscall.EINVAL
- }
+func (file *File) readdir(n int, mode readdirMode) (names []string, dirents []DirEntry, infos []FileInfo, err error) {
if !file.isdir() {
- return nil, &PathError{"Readdir", file.name, syscall.ENOTDIR}
+ return nil, nil, nil, &PathError{"readdir", file.name, syscall.ENOTDIR}
}
wantAll := n <= 0
- size := n
if wantAll {
n = -1
- size = 100
}
- fi = make([]FileInfo, 0, size) // Empty with room to grow.
d := &file.dirinfo.data
for n != 0 && !file.dirinfo.isempty {
if file.dirinfo.needdata {
@@ -34,9 +28,6 @@ func (file *File) readdir(n int) (fi []FileInfo, err error) {
break
} else {
err = &PathError{"FindNextFile", file.name, e}
- if !wantAll {
- fi = nil
- }
return
}
}
@@ -46,24 +37,32 @@ func (file *File) readdir(n int) (fi []FileInfo, err error) {
if name == "." || name == ".." { // Useless names
continue
}
- f := newFileStatFromWin32finddata(d)
- f.name = name
- f.path = file.dirinfo.path
- f.appendNameToPath = true
+ if mode == readdirName {
+ names = append(names, name)
+ } else {
+ f := newFileStatFromWin32finddata(d)
+ f.name = name
+ f.path = file.dirinfo.path
+ f.appendNameToPath = true
+ if mode == readdirDirEntry {
+ dirents = append(dirents, dirEntry{f})
+ } else {
+ infos = append(infos, f)
+ }
+ }
n--
- fi = append(fi, f)
}
- if !wantAll && len(fi) == 0 {
- return fi, io.EOF
+ if !wantAll && len(names)+len(dirents)+len(infos) == 0 {
+ return nil, nil, nil, io.EOF
}
- return fi, nil
+ return names, dirents, infos, nil
}
-func (file *File) readdirnames(n int) (names []string, err error) {
- fis, err := file.Readdir(n)
- names = make([]string, len(fis))
- for i, fi := range fis {
- names[i] = fi.Name()
- }
- return names, err
+type dirEntry struct {
+ fs *fileStat
}
+
+func (de dirEntry) Name() string { return de.fs.Name() }
+func (de dirEntry) IsDir() bool { return de.fs.IsDir() }
+func (de dirEntry) Type() FileMode { return de.fs.Mode().Type() }
+func (de dirEntry) Info() (FileInfo, error) { return de.fs, nil }