2009-06-04 14:33:57 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:40:16 -07:00
|
|
|
"syscall"
|
2009-06-04 14:33:57 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2011-03-17 11:57:36 -06:00
|
|
|
blockSize = 4096
|
2009-06-04 14:33:57 -06:00
|
|
|
)
|
|
|
|
|
2011-05-16 10:26:16 -06:00
|
|
|
// 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 (f *File) Readdirnames(n int) (names []string, err Error) {
|
2009-06-04 14:33:57 -06:00
|
|
|
// If this file has no dirinfo, create one.
|
2011-05-16 10:26:16 -06:00
|
|
|
if f.dirinfo == nil {
|
|
|
|
f.dirinfo = new(dirInfo)
|
2009-06-04 14:33:57 -06:00
|
|
|
// The buffer must be at least a block long.
|
2011-05-16 10:26:16 -06:00
|
|
|
f.dirinfo.buf = make([]byte, blockSize)
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|
2011-05-16 10:26:16 -06:00
|
|
|
d := f.dirinfo
|
|
|
|
|
|
|
|
size := n
|
2011-05-27 13:58:59 -06:00
|
|
|
if size <= 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
size = 100
|
2011-05-27 13:58:59 -06:00
|
|
|
n = -1
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|
2011-05-16 10:26:16 -06:00
|
|
|
|
2009-12-15 16:40:16 -07:00
|
|
|
names = make([]string, 0, size) // Empty with room to grow.
|
2011-05-16 10:26:16 -06:00
|
|
|
for n != 0 {
|
2009-06-04 14:33:57 -06:00
|
|
|
// Refill the buffer if necessary
|
|
|
|
if d.bufp >= d.nbuf {
|
2009-12-15 16:40:16 -07:00
|
|
|
d.bufp = 0
|
2011-04-06 13:44:40 -06:00
|
|
|
var errno int
|
2011-05-16 10:26:16 -06:00
|
|
|
d.nbuf, errno = syscall.ReadDirent(f.fd, d.buf)
|
2009-06-04 14:33:57 -06:00
|
|
|
if errno != 0 {
|
2011-04-06 13:44:40 -06:00
|
|
|
return names, NewSyscallError("readdirent", errno)
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
if d.nbuf <= 0 {
|
2009-12-15 16:40:16 -07:00
|
|
|
break // EOF
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|
|
|
|
}
|
2011-04-06 13:44:40 -06:00
|
|
|
|
2009-06-04 14:33:57 -06:00
|
|
|
// Drain the buffer
|
2011-04-06 13:44:40 -06:00
|
|
|
var nb, nc int
|
2011-05-16 10:26:16 -06:00
|
|
|
nb, nc, names = syscall.ParseDirent(d.buf[d.bufp:d.nbuf], n, names)
|
2011-04-06 13:44:40 -06:00
|
|
|
d.bufp += nb
|
2011-05-16 10:26:16 -06:00
|
|
|
n -= nc
|
|
|
|
}
|
2011-05-27 13:58:59 -06:00
|
|
|
if n >= 0 && len(names) == 0 {
|
2011-05-16 10:26:16 -06:00
|
|
|
return names, EOF
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return names, nil
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|