1
0
mirror of https://github.com/golang/go synced 2024-11-21 18:34:44 -07:00

Make unix Readdir and windows Readdirnames return partially successful results on error.

Make plan 9 Readdir & Readdirnames return os.EOF at end.
Also fix typos in the unix and windows comments.

R=golang-dev, fshahriar, bradfitz, rsc, r
CC=golang-dev
https://golang.org/cl/4557053
This commit is contained in:
Yuval Pavel Zholkover 2011-06-01 13:12:37 +10:00 committed by Rob Pike
parent 73d57642a4
commit f74f50e046
4 changed files with 49 additions and 45 deletions

View File

@ -9,35 +9,46 @@ import (
)
// Readdir reads the contents of the directory associated with file and
// returns an array of up to count FileInfo structures, in directory order.
// Subsequent calls on the same file will yield further FileInfos.
// A negative count means to read until EOF.
// Readdir returns the array and an Error, if any.
func (file *File) Readdir(count int) (fi []FileInfo, err Error) {
// returns an array of up to n FileInfo structures, as would be returned
// by Lstat, in directory order. Subsequent calls on the same file will yield
// further FileInfos.
//
// If n > 0, Readdir returns at most n FileInfo structures. In this case, if
// 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 this file has no dirinfo, create one.
if file.dirinfo == nil {
file.dirinfo = new(dirInfo)
}
d := file.dirinfo
size := count
if size < 0 {
size := n
if size <= 0 {
size = 100
n = -1
}
result := make([]FileInfo, 0, size) // Empty with room to grow.
for count != 0 {
for n != 0 {
// Refill the buffer if necessary
if d.bufp >= d.nbuf {
d.bufp = 0
var e Error
d.nbuf, e = file.Read(d.buf[:])
if e != nil && e != EOF {
return nil, &PathError{"readdir", file.name, e}
return result, &PathError{"readdir", file.name, e}
}
if e == EOF {
break
}
if d.nbuf < syscall.STATFIXLEN {
return nil, &PathError{"readdir", file.name, Eshortstat}
return result, &PathError{"readdir", file.name, Eshortstat}
}
}
@ -45,39 +56,44 @@ func (file *File) Readdir(count int) (fi []FileInfo, err Error) {
m, _ := gbit16(d.buf[d.bufp:])
m += 2
if m < syscall.STATFIXLEN {
return nil, &PathError{"readdir", file.name, Eshortstat}
return result, &PathError{"readdir", file.name, Eshortstat}
}
dir, e := UnmarshalDir(d.buf[d.bufp : d.bufp+int(m)])
if e != nil {
return nil, &PathError{"readdir", file.name, e}
return result, &PathError{"readdir", file.name, e}
}
var f FileInfo
fileInfoFromStat(&f, dir)
result = append(result, f)
d.bufp += int(m)
count--
n--
}
if n >= 0 && len(result) == 0 {
return result, EOF
}
return result, nil
}
// Readdirnames returns an array of up to count file names residing in the
// directory associated with file. A negative count will return all of them.
// Readdir returns the array and an Error, if any.
func (file *File) Readdirnames(count int) (names []string, err Error) {
fi, e := file.Readdir(count)
if e != nil {
return []string{}, e
}
// Readdirnames reads and returns a slice of names from the directory f.
//
// If n > 0, Readdirnames returns at most n names. In this case, if
// 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, 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 (file *File) Readdirnames(n int) (names []string, err Error) {
fi, err := file.Readdir(n)
names = make([]string, len(fi))
err = nil
for i := range fi {
names[i] = fi[i].Name
}
return
}

View File

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

View File

@ -69,12 +69,12 @@ func (file *File) Stat() (fi *FileInfo, err Error) {
}
// 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 n FileInfo structures, as would be returned
// by Lstat, in directory order. Subsequent calls on the same file will yield
// further FileInfos.
//
// If n > 0, Readdir returns at most n names. In this case, if
// Readdirnames returns an empty slice, it will return a non-nil error
// If n > 0, Readdir returns at most n FileInfo structures. In this case, if
// Readdir 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
@ -89,11 +89,7 @@ func (file *File) Readdir(n int) (fi []FileInfo, err Error) {
dirname = "."
}
dirname += "/"
wantAll := n < 0
names, namesErr := file.Readdirnames(n)
if namesErr != nil && !wantAll {
return nil, namesErr
}
names, err := file.Readdirnames(n)
fi = make([]FileInfo, len(names))
for i, filename := range names {
fip, err := Lstat(dirname + filename)
@ -103,9 +99,6 @@ func (file *File) Readdir(n int) (fi []FileInfo, err Error) {
fi[i] = *fip
}
}
if !wantAll && namesErr != EOF {
err = namesErr
}
return
}

View File

@ -123,12 +123,12 @@ func (file *File) Stat() (fi *FileInfo, err Error) {
}
// 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 n FileInfo structures, as would be returned
// by Lstat, in directory order. Subsequent calls on the same file will yield
// further FileInfos.
//
// If n > 0, Readdir returns at most n names. In this case, if
// Readdirnames returns an empty slice, it will return a non-nil error
// If n > 0, Readdir returns at most n FileInfo structures. In this case, if
// Readdir 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