2009-06-04 13:33:57 -07: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 15:40:16 -08:00
|
|
|
"syscall"
|
|
|
|
|
"unsafe"
|
2009-06-04 13:33:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2011-03-17 13:57:36 -04:00
|
|
|
blockSize = 4096
|
2009-06-04 13:33:57 -07:00
|
|
|
)
|
|
|
|
|
|
2009-08-12 13:18:37 -07:00
|
|
|
// Readdirnames reads the contents of the directory associated with file and
|
|
|
|
|
// returns an array of up to count names, in directory order. Subsequent
|
|
|
|
|
// calls on the same file will yield further names.
|
|
|
|
|
// A negative count means to read until EOF.
|
|
|
|
|
// Readdirnames returns the array and an Error, if any.
|
2009-06-29 15:13:56 -07:00
|
|
|
func (file *File) Readdirnames(count int) (names []string, err Error) {
|
2009-06-04 13:33:57 -07:00
|
|
|
// If this file has no dirinfo, create one.
|
|
|
|
|
if file.dirinfo == nil {
|
2009-12-15 15:40:16 -08:00
|
|
|
file.dirinfo = new(dirInfo)
|
2009-06-04 13:33:57 -07:00
|
|
|
// The buffer must be at least a block long.
|
2009-12-15 15:40:16 -08:00
|
|
|
file.dirinfo.buf = make([]byte, blockSize)
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
d := file.dirinfo
|
|
|
|
|
size := count
|
2009-06-04 13:33:57 -07:00
|
|
|
if size < 0 {
|
2009-11-09 12:07:39 -08:00
|
|
|
size = 100
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
names = make([]string, 0, size) // Empty with room to grow.
|
2009-06-04 13:33:57 -07:00
|
|
|
for count != 0 {
|
|
|
|
|
// Refill the buffer if necessary
|
|
|
|
|
if d.bufp >= d.nbuf {
|
2009-12-15 15:40:16 -08:00
|
|
|
var errno int
|
|
|
|
|
d.bufp = 0
|
2009-06-04 13:33:57 -07:00
|
|
|
// Final argument is (basep *uintptr) and the syscall doesn't take nil.
|
2009-12-15 15:40:16 -08:00
|
|
|
d.nbuf, errno = syscall.Getdirentries(file.fd, d.buf, new(uintptr))
|
2009-06-04 13:33:57 -07:00
|
|
|
if errno != 0 {
|
2009-12-15 15:40:16 -08:00
|
|
|
d.nbuf = 0
|
|
|
|
|
return names, NewSyscallError("getdirentries", errno)
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
2009-06-25 20:24:55 -07:00
|
|
|
if d.nbuf <= 0 {
|
2009-12-15 15:40:16 -08:00
|
|
|
break // EOF
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Drain the buffer
|
|
|
|
|
for count != 0 && d.bufp < d.nbuf {
|
2009-12-15 15:40:16 -08:00
|
|
|
dirent := (*syscall.Dirent)(unsafe.Pointer(&d.buf[d.bufp]))
|
2009-06-04 13:33:57 -07:00
|
|
|
if dirent.Reclen == 0 {
|
2009-12-15 15:40:16 -08:00
|
|
|
d.bufp = d.nbuf
|
|
|
|
|
break
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
d.bufp += int(dirent.Reclen)
|
|
|
|
|
if dirent.Ino == 0 { // File absent in directory.
|
2009-11-09 12:07:39 -08:00
|
|
|
continue
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
2010-07-01 17:49:28 -07:00
|
|
|
bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0]))
|
2009-12-15 15:40:16 -08:00
|
|
|
var name = string(bytes[0:dirent.Namlen])
|
|
|
|
|
if name == "." || name == ".." { // Useless names
|
2009-11-09 12:07:39 -08:00
|
|
|
continue
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
count--
|
2010-10-27 19:47:23 -07:00
|
|
|
names = append(names, name)
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
return names, nil
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|