Commit f74f50e0 authored by Yuval Pavel Zholkover's avatar Yuval Pavel Zholkover Committed by Rob Pike

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
parent 73d57642
...@@ -9,35 +9,46 @@ import ( ...@@ -9,35 +9,46 @@ import (
) )
// 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, in directory order. // returns an array of up to n FileInfo structures, as would be returned
// Subsequent calls on the same file will yield further FileInfos. // by Lstat, in directory order. Subsequent calls on the same file will yield
// A negative count means to read until EOF. // further FileInfos.
// Readdir returns the array and an Error, if any. //
func (file *File) Readdir(count int) (fi []FileInfo, err Error) { // 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 this file has no dirinfo, create one.
if file.dirinfo == nil { if file.dirinfo == nil {
file.dirinfo = new(dirInfo) file.dirinfo = new(dirInfo)
} }
d := file.dirinfo d := file.dirinfo
size := count size := n
if size < 0 { if size <= 0 {
size = 100 size = 100
n = -1
} }
result := make([]FileInfo, 0, size) // Empty with room to grow. result := make([]FileInfo, 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 e Error var e Error
d.nbuf, e = file.Read(d.buf[:]) d.nbuf, e = file.Read(d.buf[:])
if e != nil && e != EOF { if e != nil && e != EOF {
return nil, &PathError{"readdir", file.name, e} return result, &PathError{"readdir", file.name, e}
} }
if e == EOF { if e == EOF {
break break
} }
if d.nbuf < syscall.STATFIXLEN { 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) { ...@@ -45,39 +56,44 @@ func (file *File) Readdir(count int) (fi []FileInfo, err Error) {
m, _ := gbit16(d.buf[d.bufp:]) m, _ := gbit16(d.buf[d.bufp:])
m += 2 m += 2
if m < syscall.STATFIXLEN { 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)]) dir, e := UnmarshalDir(d.buf[d.bufp : d.bufp+int(m)])
if e != nil { if e != nil {
return nil, &PathError{"readdir", file.name, e} return result, &PathError{"readdir", file.name, e}
} }
var f FileInfo var f FileInfo
fileInfoFromStat(&f, dir) fileInfoFromStat(&f, dir)
result = append(result, f) result = append(result, f)
d.bufp += int(m) d.bufp += int(m)
count-- n--
} }
return result, nil
}
// Readdirnames returns an array of up to count file names residing in the if n >= 0 && len(result) == 0 {
// directory associated with file. A negative count will return all of them. return result, EOF
// 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
} }
return result, nil
}
// 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)) names = make([]string, len(fi))
err = nil
for i := range fi { for i := range fi {
names[i] = fi[i].Name names[i] = fi[i].Name
} }
return return
} }
......
...@@ -6,11 +6,6 @@ package os ...@@ -6,11 +6,6 @@ package os
func (file *File) Readdirnames(n int) (names []string, err Error) { func (file *File) Readdirnames(n int) (names []string, err Error) {
fis, err := file.Readdir(n) 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)) names = make([]string, len(fis))
for i, fi := range fis { for i, fi := range fis {
names[i] = fi.Name names[i] = fi.Name
......
...@@ -69,12 +69,12 @@ func (file *File) Stat() (fi *FileInfo, err Error) { ...@@ -69,12 +69,12 @@ 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 n 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.
// //
// If n > 0, Readdir returns at most n names. In this case, if // 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 // 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. // 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 // 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) { ...@@ -89,11 +89,7 @@ func (file *File) Readdir(n int) (fi []FileInfo, err Error) {
dirname = "." dirname = "."
} }
dirname += "/" dirname += "/"
wantAll := n < 0 names, err := file.Readdirnames(n)
names, namesErr := file.Readdirnames(n)
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 {
fip, err := Lstat(dirname + filename) fip, err := Lstat(dirname + filename)
...@@ -103,9 +99,6 @@ func (file *File) Readdir(n int) (fi []FileInfo, err Error) { ...@@ -103,9 +99,6 @@ func (file *File) Readdir(n int) (fi []FileInfo, err Error) {
fi[i] = *fip fi[i] = *fip
} }
} }
if !wantAll && namesErr != EOF {
err = namesErr
}
return return
} }
......
...@@ -123,12 +123,12 @@ func (file *File) Stat() (fi *FileInfo, err Error) { ...@@ -123,12 +123,12 @@ 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 n 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.
// //
// If n > 0, Readdir returns at most n names. In this case, if // 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 // 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. // 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 // If n <= 0, Readdir returns all the FileInfo from the directory in
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment