mirror of
https://github.com/golang/go.git
synced 2025-10-27 06:44:13 +00:00
add Readdir: returns an array of Dir structures
R=rsc DELTA=200 (176 added, 12 deleted, 12 changed) OCL=24680 CL=24680
This commit is contained in:
parent
c8d59c1fb2
commit
aba4c75408
3 changed files with 188 additions and 24 deletions
|
|
@ -40,9 +40,9 @@ func Readdirnames(fd *FD, count int) (names []string, err *os.Error) {
|
|||
if count == 0 {
|
||||
break
|
||||
}
|
||||
dir := unsafe.Pointer((uintptr(unsafe.Pointer(&buf[0])) + i)).(*syscall.Dirent);
|
||||
w = uintptr(dir.Reclen);
|
||||
if dir.Ino == 0 {
|
||||
dirent := unsafe.Pointer((uintptr(unsafe.Pointer(&buf[0])) + i)).(*syscall.Dirent);
|
||||
w = uintptr(dirent.Reclen);
|
||||
if dirent.Ino == 0 {
|
||||
continue
|
||||
}
|
||||
count--;
|
||||
|
|
@ -54,8 +54,69 @@ func Readdirnames(fd *FD, count int) (names []string, err *os.Error) {
|
|||
names = nnames;
|
||||
}
|
||||
names = names[0:len(names)+1];
|
||||
names[len(names)-1] = string(dir.Name[0:clen(dir.Name)]);
|
||||
names[len(names)-1] = string(dirent.Name[0:clen(dirent.Name)]);
|
||||
}
|
||||
}
|
||||
return names, nil;
|
||||
}
|
||||
|
||||
// TODO(r): Readdir duplicates a lot of Readdirnames. The other way would
|
||||
// be to have Readdir (which could then be portable) call Readdirnames and
|
||||
// then do the Stats. The existing design was chosen to avoid allocating a
|
||||
// throwaway names array, but the issue should be revisited once we have
|
||||
// a better handle on what that overhead is with a strong garbage collector.
|
||||
// Also, it's possible given the nature of the Unix kernel that interleaving
|
||||
// reads of the directory with stats (as done here) would work better than
|
||||
// one big read of the directory followed by a long run of Stat calls.
|
||||
|
||||
// Negative count means read until EOF.
|
||||
func Readdir(fd *FD, count int) (dirs []Dir, err *os.Error) {
|
||||
dirname := fd.name;
|
||||
if dirname == "" {
|
||||
dirname = ".";
|
||||
}
|
||||
dirname += "/";
|
||||
// The buffer must be at least a block long.
|
||||
// TODO(r): use fstatfs to find fs block size.
|
||||
var buf = make([]syscall.Dirent, 8192/unsafe.Sizeof(*new(syscall.Dirent)));
|
||||
dirs = make([]Dir, 0, 100); // TODO: could be smarter about size
|
||||
for {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
ret, err2 := syscall.Getdents(fd.fd, &buf[0], int64(len(buf) * unsafe.Sizeof(buf[0])));
|
||||
if ret < 0 || err2 != 0 {
|
||||
return dirs, os.ErrnoToError(err2)
|
||||
}
|
||||
if ret == 0 {
|
||||
break
|
||||
}
|
||||
for w, i := uintptr(0),uintptr(0); i < uintptr(ret); i += w {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
dirent := unsafe.Pointer((uintptr(unsafe.Pointer(&buf[0])) + i)).(*syscall.Dirent);
|
||||
w = uintptr(dirent.Reclen);
|
||||
if dirent.Ino == 0 {
|
||||
continue
|
||||
}
|
||||
count--;
|
||||
if len(dirs) == cap(dirs) {
|
||||
ndirs := make([]Dir, len(dirs), 2*len(dirs));
|
||||
for i := 0; i < len(dirs); i++ {
|
||||
ndirs[i] = dirs[i]
|
||||
}
|
||||
dirs = ndirs;
|
||||
}
|
||||
dirs = dirs[0:len(dirs)+1];
|
||||
filename := string(dirent.Name[0:clen(dirent.Name)]);
|
||||
dirp, err := Stat(dirname + filename);
|
||||
if dirp == nil || err != nil {
|
||||
dirs[len(dirs)-1].Name = filename; // rest will be zeroed out
|
||||
} else {
|
||||
dirs[len(dirs)-1] = *dirp;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dirs, nil;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue