aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Pavel Zholkover <paulzhol@gmail.com>2011-06-01 13:12:37 +1000
committerRob Pike <r@golang.org>2011-06-01 13:12:37 +1000
commitf74f50e046cbbb651586005c0ea73dfc8fe828c4 (patch)
treefe53ee84ec18a3c5301f1e51314f940733c49a1c
parent73d57642a4dbb34bae8619f5aacf3756c814d5d6 (diff)
downloadgo-f74f50e046cbbb651586005c0ea73dfc8fe828c4.tar.gz
go-f74f50e046cbbb651586005c0ea73dfc8fe828c4.zip
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
-rw-r--r--src/pkg/os/dir_plan9.go68
-rw-r--r--src/pkg/os/dir_windows.go5
-rw-r--r--src/pkg/os/file_unix.go15
-rw-r--r--src/pkg/os/file_windows.go6
4 files changed, 49 insertions, 45 deletions
diff --git a/src/pkg/os/dir_plan9.go b/src/pkg/os/dir_plan9.go
index 14decfce10..bbc2cb6472 100644
--- a/src/pkg/os/dir_plan9.go
+++ b/src/pkg/os/dir_plan9.go
@@ -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--
}
- 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
+ if n >= 0 && len(result) == 0 {
+ return result, EOF
}
+ 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))
- err = nil
-
for i := range fi {
names[i] = fi[i].Name
}
-
return
}
diff --git a/src/pkg/os/dir_windows.go b/src/pkg/os/dir_windows.go
index a4df9d3eab..d76e88fdb7 100644
--- a/src/pkg/os/dir_windows.go
+++ b/src/pkg/os/dir_windows.go
@@ -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
diff --git a/src/pkg/os/file_unix.go b/src/pkg/os/file_unix.go
index c65c5b3ffd..def9b3bf07 100644
--- a/src/pkg/os/file_unix.go
+++ b/src/pkg/os/file_unix.go
@@ -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
}
diff --git a/src/pkg/os/file_windows.go b/src/pkg/os/file_windows.go
index ac37b8e2d3..80886f6f52 100644
--- a/src/pkg/os/file_windows.go
+++ b/src/pkg/os/file_windows.go
@@ -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