2010-12-03 05:34:57 +01: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 (
|
2011-12-03 03:17:34 +01:00
|
|
|
"io"
|
2010-12-03 05:34:57 +01:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2012-02-07 20:26:30 +01:00
|
|
|
//extern opendir
|
|
|
|
func libc_opendir(*byte) *syscall.DIR
|
|
|
|
|
|
|
|
//extern closedir
|
|
|
|
func libc_closedir(*syscall.DIR) int
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// FIXME: pathconf returns long, not int.
|
2012-02-07 20:26:30 +01:00
|
|
|
//extern pathconf
|
|
|
|
func libc_pathconf(*byte, int) int
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
func clen(n []byte) int {
|
|
|
|
for i := 0; i < len(n); i++ {
|
|
|
|
if n[i] == 0 {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(n)
|
|
|
|
}
|
|
|
|
|
2012-02-07 20:26:30 +01:00
|
|
|
var elen int
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func (file *File) readdirnames(n int) (names []string, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
if elen == 0 {
|
2012-02-07 20:26:30 +01:00
|
|
|
var dummy syscall.Dirent
|
2012-03-29 05:53:13 +02:00
|
|
|
elen = (int(unsafe.Offsetof(dummy.Name)) +
|
2012-02-07 20:26:30 +01:00
|
|
|
libc_pathconf(syscall.StringBytePtr(file.name), syscall.PC_NAME_MAX) +
|
|
|
|
1)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if file.dirinfo == nil {
|
|
|
|
file.dirinfo = new(dirInfo)
|
|
|
|
file.dirinfo.buf = make([]byte, elen)
|
2012-06-13 06:47:25 +02:00
|
|
|
p := syscall.StringBytePtr(file.name)
|
|
|
|
syscall.Entersyscall()
|
|
|
|
r := libc_opendir(p)
|
|
|
|
syscall.Exitsyscall()
|
|
|
|
file.dirinfo.dir = r
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-08-23 02:20:48 +02:00
|
|
|
entry_dirent := (*syscall.Dirent)(unsafe.Pointer(&file.dirinfo.buf[0]))
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
size := n
|
2012-10-23 06:31:11 +02:00
|
|
|
if size <= 0 {
|
2010-12-03 05:34:57 +01:00
|
|
|
size = 100
|
2011-09-16 17:47:21 +02:00
|
|
|
n = -1
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
names = make([]string, 0, size) // Empty with room to grow.
|
|
|
|
|
|
|
|
dir := file.dirinfo.dir
|
|
|
|
if dir == nil {
|
|
|
|
return names, NewSyscallError("opendir", syscall.GetErrno())
|
2012-02-07 20:26:30 +01:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
for n != 0 {
|
2010-12-03 05:34:57 +01:00
|
|
|
var result *syscall.Dirent
|
2012-06-13 06:47:25 +02:00
|
|
|
pr := &result
|
|
|
|
syscall.Entersyscall()
|
|
|
|
i := libc_readdir_r(dir, entry_dirent, pr)
|
|
|
|
syscall.Exitsyscall()
|
2011-09-16 17:47:21 +02:00
|
|
|
if i != 0 {
|
|
|
|
return names, NewSyscallError("readdir_r", i)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
if result == nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
break // EOF
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
var name = string(result.Name[0:clen(result.Name[0:])])
|
2012-02-07 20:26:30 +01:00
|
|
|
if name == "." || name == ".." { // Useless names
|
2010-12-03 05:34:57 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
names = append(names, name)
|
2011-09-16 17:47:21 +02:00
|
|
|
n--
|
|
|
|
}
|
|
|
|
if n >= 0 && len(names) == 0 {
|
2011-12-03 03:17:34 +01:00
|
|
|
return names, io.EOF
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
return names, nil
|
|
|
|
}
|