2011-04-07 19:09:10 +02: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.
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
2011-12-03 03:17:34 +01:00
|
|
|
"io"
|
2011-04-07 19:09:10 +02:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
blockSize = 4096
|
|
|
|
)
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func (f *File) readdirnames(n int) (names []string, err error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
// If this file has no dirinfo, create one.
|
2011-09-16 17:47:21 +02:00
|
|
|
if f.dirinfo == nil {
|
|
|
|
f.dirinfo = new(dirInfo)
|
2011-04-07 19:09:10 +02:00
|
|
|
// The buffer must be at least a block long.
|
2011-09-16 17:47:21 +02:00
|
|
|
f.dirinfo.buf = make([]byte, blockSize)
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
d := f.dirinfo
|
|
|
|
|
|
|
|
size := n
|
|
|
|
if size <= 0 {
|
2011-04-07 19:09:10 +02:00
|
|
|
size = 100
|
2011-09-16 17:47:21 +02:00
|
|
|
n = -1
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
names = make([]string, 0, size) // Empty with room to grow.
|
2011-09-16 17:47:21 +02:00
|
|
|
for n != 0 {
|
2011-04-07 19:09:10 +02:00
|
|
|
// Refill the buffer if necessary
|
|
|
|
if d.bufp >= d.nbuf {
|
|
|
|
d.bufp = 0
|
2011-12-13 00:40:51 +01:00
|
|
|
var errno error
|
2011-09-16 17:47:21 +02:00
|
|
|
d.nbuf, errno = syscall.ReadDirent(f.fd, d.buf)
|
2011-12-13 00:40:51 +01:00
|
|
|
if errno != nil {
|
2011-04-07 19:09:10 +02:00
|
|
|
return names, NewSyscallError("readdirent", errno)
|
|
|
|
}
|
|
|
|
if d.nbuf <= 0 {
|
|
|
|
break // EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drain the buffer
|
|
|
|
var nb, nc int
|
2011-09-16 17:47:21 +02:00
|
|
|
nb, nc, names = syscall.ParseDirent(d.buf[d.bufp:d.nbuf], n, names)
|
2011-04-07 19:09:10 +02:00
|
|
|
d.bufp += nb
|
2011-09-16 17:47:21 +02:00
|
|
|
n -= nc
|
|
|
|
}
|
|
|
|
if n >= 0 && len(names) == 0 {
|
2011-12-03 03:17:34 +01:00
|
|
|
return names, io.EOF
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
return names, nil
|
|
|
|
}
|