]> Cypherpunks repositories - gostls13.git/commitdiff
Make unix Readdir and windows Readdirnames return partially successful results on...
authorYuval Pavel Zholkover <paulzhol@gmail.com>
Wed, 1 Jun 2011 03:12:37 +0000 (13:12 +1000)
committerRob Pike <r@golang.org>
Wed, 1 Jun 2011 03:12:37 +0000 (13:12 +1000)
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

src/pkg/os/dir_plan9.go
src/pkg/os/dir_windows.go
src/pkg/os/file_unix.go
src/pkg/os/file_windows.go

index 14decfce105c89a7c90d9db09a08caf4a43e2374..bbc2cb6472639cbeeb3e13e2265f094f89b7d870 100644 (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--
        }
-       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
 }
 
index a4df9d3eab7009999b5bddede0330aacc1b9bb9b..d76e88fdb77357ba38f41c7a0deb93d61ab89d8a 100644 (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
index c65c5b3ffd734ee1e95b16660811bdc0eba4a6d5..def9b3bf07de9ca95fb7b900ffe13fb8652e3ea1 100644 (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
 }
 
index ac37b8e2d3496d88368b3526dc00a4ec579fe4b9..80886f6f525d7fe27beda2e1a68752bb1254e854 100644 (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