1
0
mirror of https://github.com/golang/go synced 2024-11-19 21:14:43 -07:00

os: make Readdir & Readdirnames return os.EOF at end

Fixes #678

R=rsc, r, alex.brainman, bsiegert, jdpoirier
CC=golang-dev
https://golang.org/cl/4536058
This commit is contained in:
Brad Fitzpatrick 2011-05-16 09:26:16 -07:00
parent 703b092779
commit 4da5cd4cd6
6 changed files with 88 additions and 37 deletions

View File

@ -12,30 +12,40 @@ const (
blockSize = 4096 blockSize = 4096
) )
// Readdirnames reads the contents of the directory associated with file and // Readdirnames reads and returns a slice of names from the directory f.
// returns an array of up to count names, in directory order. Subsequent //
// calls on the same file will yield further names. // If n > 0, Readdirnames returns at most n names. In this case, if
// A negative count means to read until EOF. // Readdirnames returns an empty slice, it will return a non-nil error
// Readdirnames returns the array and an Error, if any. // explaining why. At the end of a directory, the error is os.EOF.
func (file *File) Readdirnames(count int) (names []string, err Error) { //
// If n <= 0, Readdirnames returns all the names from the directory in
// a single slice. In this case, if Readdirnames succeeds (reads all
// the way to the end of the directory), it returns the slice and a
// nil os.Error. If it encounters an error before the end of the
// directory, Readdirnames returns the names read until that point and
// a non-nil error.
func (f *File) Readdirnames(n int) (names []string, err Error) {
// If this file has no dirinfo, create one. // If this file has no dirinfo, create one.
if file.dirinfo == nil { if f.dirinfo == nil {
file.dirinfo = new(dirInfo) f.dirinfo = new(dirInfo)
// The buffer must be at least a block long. // The buffer must be at least a block long.
file.dirinfo.buf = make([]byte, blockSize) f.dirinfo.buf = make([]byte, blockSize)
} }
d := file.dirinfo d := f.dirinfo
size := count wantAll := n < 0
size := n
if size < 0 { if size < 0 {
size = 100 size = 100
} }
names = make([]string, 0, size) // Empty with room to grow. names = make([]string, 0, size) // Empty with room to grow.
for count != 0 { for n != 0 {
// Refill the buffer if necessary // Refill the buffer if necessary
if d.bufp >= d.nbuf { if d.bufp >= d.nbuf {
d.bufp = 0 d.bufp = 0
var errno int var errno int
d.nbuf, errno = syscall.ReadDirent(file.fd, d.buf) d.nbuf, errno = syscall.ReadDirent(f.fd, d.buf)
if errno != 0 { if errno != 0 {
return names, NewSyscallError("readdirent", errno) return names, NewSyscallError("readdirent", errno)
} }
@ -46,9 +56,12 @@ func (file *File) Readdirnames(count int) (names []string, err Error) {
// Drain the buffer // Drain the buffer
var nb, nc int var nb, nc int
nb, nc, names = syscall.ParseDirent(d.buf[d.bufp:d.nbuf], count, names) nb, nc, names = syscall.ParseDirent(d.buf[d.bufp:d.nbuf], n, names)
d.bufp += nb d.bufp += nb
count -= nc n -= nc
}
if !wantAll && len(names) == 0 {
return names, EOF
} }
return names, nil return names, nil
} }

View File

@ -4,14 +4,16 @@
package os package os
func (file *File) Readdirnames(count int) (names []string, err Error) { func (file *File) Readdirnames(n int) (names []string, err Error) {
fis, e := file.Readdir(count) fis, err := file.Readdir(n)
if e != nil { // If n > 0 and we get an error, we return now.
return nil, e // If n < 0, we return whatever we got + any error.
if n > 0 && e != nil {
return nil, err
} }
names = make([]string, len(fis)) names = make([]string, len(fis))
for i, fi := range fis { for i, fi := range fis {
names[i] = fi.Name names[i] = fi.Name
} }
return names, nil return names, err
} }

View File

@ -70,19 +70,29 @@ func (file *File) Stat() (fi *FileInfo, err Error) {
// Readdir reads the contents of the directory associated with file and // Readdir reads the contents of the directory associated with file and
// returns an array of up to count FileInfo structures, as would be returned // returns an array of up to count FileInfo structures, as would be returned
// by Lstat, in directory order. Subsequent calls on the same file will yield // by Lstat, in directory order. Subsequent calls on the same file will yield
// further FileInfos. // further FileInfos.
// A negative count means to read until EOF. //
// Readdir returns the array and an Error, if any. // If n > 0, Readdir returns at most n names. In this case, if
func (file *File) Readdir(count int) (fi []FileInfo, err Error) { // Readdirnames returns an empty slice, it will return a non-nil error
// explaining why. At the end of a directory, the error is os.EOF.
//
// If n <= 0, Readdir returns all the FileInfo from the directory in
// a single slice. In this case, if Readdir succeeds (reads all
// the way to the end of the directory), it returns the slice and a
// nil os.Error. If it encounters an error before the end of the
// directory, Readdir returns the FileInfo read until that point
// and a non-nil error.
func (file *File) Readdir(n int) (fi []FileInfo, err Error) {
dirname := file.name dirname := file.name
if dirname == "" { if dirname == "" {
dirname = "." dirname = "."
} }
dirname += "/" dirname += "/"
names, err1 := file.Readdirnames(count) wantAll := n < 0
if err1 != nil { names, namesErr := file.Readdirnames(n)
return nil, err1 if namesErr != nil && !wantAll {
return nil, namesErr
} }
fi = make([]FileInfo, len(names)) fi = make([]FileInfo, len(names))
for i, filename := range names { for i, filename := range names {
@ -93,6 +103,9 @@ func (file *File) Readdir(count int) (fi []FileInfo, err Error) {
fi[i] = *fip fi[i] = *fip
} }
} }
if !wantAll && namesErr != EOF {
err = namesErr
}
return return
} }

View File

@ -124,11 +124,20 @@ func (file *File) Stat() (fi *FileInfo, err Error) {
// Readdir reads the contents of the directory associated with file and // Readdir reads the contents of the directory associated with file and
// returns an array of up to count FileInfo structures, as would be returned // returns an array of up to count FileInfo structures, as would be returned
// by Lstat, in directory order. Subsequent calls on the same file will yield // by Lstat, in directory order. Subsequent calls on the same file will yield
// further FileInfos. // further FileInfos.
// A negative count means to read until EOF. //
// Readdir returns the array and an Error, if any. // If n > 0, Readdir returns at most n names. In this case, if
func (file *File) Readdir(count int) (fi []FileInfo, err Error) { // Readdirnames returns an empty slice, it will return a non-nil error
// explaining why. At the end of a directory, the error is os.EOF.
//
// If n <= 0, Readdir returns all the FileInfo from the directory in
// a single slice. In this case, if Readdir succeeds (reads all
// the way to the end of the directory), it returns the slice and a
// nil os.Error. If it encounters an error before the end of the
// directory, Readdir returns the FileInfo read until that point
// and a non-nil error.
func (file *File) Readdir(n int) (fi []FileInfo, err Error) {
if file == nil || file.fd < 0 { if file == nil || file.fd < 0 {
return nil, EINVAL return nil, EINVAL
} }
@ -136,12 +145,13 @@ func (file *File) Readdir(count int) (fi []FileInfo, err Error) {
return nil, &PathError{"Readdir", file.name, ENOTDIR} return nil, &PathError{"Readdir", file.name, ENOTDIR}
} }
di := file.dirinfo di := file.dirinfo
size := count wantAll := n < 0
size := n
if size < 0 { if size < 0 {
size = 100 size = 100
} }
fi = make([]FileInfo, 0, size) // Empty with room to grow. fi = make([]FileInfo, 0, size) // Empty with room to grow.
for count != 0 { for n != 0 {
if di.usefirststat { if di.usefirststat {
di.usefirststat = false di.usefirststat = false
} else { } else {
@ -150,7 +160,11 @@ func (file *File) Readdir(count int) (fi []FileInfo, err Error) {
if e == syscall.ERROR_NO_MORE_FILES { if e == syscall.ERROR_NO_MORE_FILES {
break break
} else { } else {
return nil, &PathError{"FindNextFile", file.name, Errno(e)} err = &PathError{"FindNextFile", file.name, Errno(e)}
if !wantAll {
fi = nil
}
return
} }
} }
} }
@ -159,9 +173,12 @@ func (file *File) Readdir(count int) (fi []FileInfo, err Error) {
if f.Name == "." || f.Name == ".." { // Useless names if f.Name == "." || f.Name == ".." { // Useless names
continue continue
} }
count-- n--
fi = append(fi, f) fi = append(fi, f)
} }
if !wantAll && len(fi) == 0 {
return fi, EOF
}
return fi, nil return fi, nil
} }

View File

@ -236,11 +236,14 @@ func smallReaddirnames(file *File, length int, t *testing.T) []string {
count := 0 count := 0
for { for {
d, err := file.Readdirnames(1) d, err := file.Readdirnames(1)
if err == EOF {
break
}
if err != nil { if err != nil {
t.Fatalf("readdir %q failed: %v", file.Name(), err) t.Fatalf("readdirnames %q failed: %v", file.Name(), err)
} }
if len(d) == 0 { if len(d) == 0 {
break t.Fatalf("readdirnames %q returned empty slice and no error")
} }
names[count] = d[0] names[count] = d[0]
count++ count++

View File

@ -95,6 +95,9 @@ func RemoveAll(path string) Error {
err = err1 err = err1
} }
} }
if err1 == EOF {
break
}
// If Readdirnames returned an error, use it. // If Readdirnames returned an error, use it.
if err == nil { if err == nil {
err = err1 err = err1