2009-02-08 11:18:50 -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 16:40:16 -07:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2009-02-08 11:18:50 -07:00
|
|
|
)
|
|
|
|
|
2009-02-10 12:55:48 -07:00
|
|
|
const (
|
2009-12-15 16:40:16 -07:00
|
|
|
blockSize = 4096 // TODO(r): use statfs
|
2009-02-10 12:55:48 -07:00
|
|
|
)
|
|
|
|
|
2009-02-08 11:18:50 -07:00
|
|
|
func clen(n []byte) int {
|
|
|
|
for i := 0; i < len(n); i++ {
|
|
|
|
if n[i] == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return i
|
2009-02-08 11:18:50 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return len(n)
|
2009-02-08 11:18:50 -07:00
|
|
|
}
|
|
|
|
|
2009-06-29 16:13:56 -06:00
|
|
|
func (file *File) Readdirnames(count int) (names []string, err Error) {
|
2009-03-11 13:51:10 -06:00
|
|
|
// If this file has no dirinfo, create one.
|
|
|
|
if file.dirinfo == nil {
|
2009-12-15 16:40:16 -07:00
|
|
|
file.dirinfo = new(dirInfo)
|
2009-02-10 12:55:48 -07:00
|
|
|
// The buffer must be at least a block long.
|
|
|
|
// TODO(r): use fstatfs to find fs block size.
|
2009-12-15 16:40:16 -07:00
|
|
|
file.dirinfo.buf = make([]byte, blockSize)
|
2009-02-10 12:55:48 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
d := file.dirinfo
|
|
|
|
size := count
|
2009-02-10 12:55:48 -07:00
|
|
|
if size < 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
size = 100
|
2009-02-10 12:55:48 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
names = make([]string, 0, size) // Empty with room to grow.
|
2009-02-10 12:55:48 -07:00
|
|
|
for count != 0 {
|
|
|
|
// Refill the buffer if necessary
|
2009-04-14 19:52:39 -06:00
|
|
|
if d.bufp >= d.nbuf {
|
2009-12-15 16:40:16 -07:00
|
|
|
var errno int
|
|
|
|
d.nbuf, errno = syscall.Getdents(file.fd, d.buf)
|
2009-06-25 21:24:55 -06:00
|
|
|
if errno != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return names, NewSyscallError("getdents", errno)
|
2009-02-08 11:18:50 -07:00
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
if d.nbuf <= 0 {
|
2009-12-15 16:40:16 -07:00
|
|
|
break // EOF
|
2009-02-10 12:55:48 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
d.bufp = 0
|
2009-02-10 12:55:48 -07:00
|
|
|
}
|
|
|
|
// Drain the buffer
|
|
|
|
for count != 0 && d.bufp < d.nbuf {
|
2009-12-15 16:40:16 -07:00
|
|
|
dirent := (*syscall.Dirent)(unsafe.Pointer(&d.buf[d.bufp]))
|
|
|
|
d.bufp += int(dirent.Reclen)
|
|
|
|
if dirent.Ino == 0 { // File absent in directory.
|
2009-11-09 13:07:39 -07:00
|
|
|
continue
|
2009-02-08 11:18:50 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
bytes := (*[len(dirent.Name)]byte)(unsafe.Pointer(&dirent.Name[0]))
|
|
|
|
var name = string(bytes[0:clen(bytes)])
|
|
|
|
if name == "." || name == ".." { // Useless names
|
2009-11-09 13:07:39 -07:00
|
|
|
continue
|
2009-04-07 01:40:50 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
count--
|
2009-02-08 11:18:50 -07:00
|
|
|
if len(names) == cap(names) {
|
2009-12-15 16:40:16 -07:00
|
|
|
nnames := make([]string, len(names), 2*len(names))
|
2009-02-08 11:18:50 -07:00
|
|
|
for i := 0; i < len(names); i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
nnames[i] = names[i]
|
2009-02-08 11:18:50 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
names = nnames
|
2009-02-08 11:18:50 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
names = names[0 : len(names)+1]
|
|
|
|
names[len(names)-1] = name
|
2009-02-08 11:18:50 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return names, nil
|
2009-02-08 11:18:50 -07:00
|
|
|
}
|