1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:44:43 -07:00

missing darwin files; g4 nothave.

R=r
DELTA=115  (115 added, 0 deleted, 0 changed)
OCL=29884
CL=29888
This commit is contained in:
Russ Cox 2009-06-04 13:33:57 -07:00
parent 4be7067f42
commit f30fcf32ac
2 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,76 @@
// 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 (
"os";
"syscall";
"unsafe";
)
const (
blockSize = 4096 // TODO(r): use statfs
)
// Negative count means read until EOF.
func readdirnames(file *File, count int) (names []string, err Error) {
// If this file has no dirinfo, create one.
if file.dirinfo == nil {
file.dirinfo = new(dirInfo);
// The buffer must be at least a block long.
// TODO(r): use fstatfs to find fs block size.
file.dirinfo.buf = make([]byte, blockSize);
}
d := file.dirinfo;
size := count;
if size < 0 {
size = 100
}
names = make([]string, 0, size); // Empty with room to grow.
for count != 0 {
// Refill the buffer if necessary
if d.bufp >= d.nbuf {
var errno int;
d.bufp = 0;
// Final argument is (basep *uintptr) and the syscall doesn't take nil.
d.nbuf, errno = syscall.Getdirentries(file.fd, d.buf, new(uintptr));
if errno != 0 {
d.nbuf = 0;
return names, ErrnoToError(errno)
}
if d.nbuf == 0 {
break // EOF
}
}
// Drain the buffer
for count != 0 && d.bufp < d.nbuf {
dirent := (*syscall.Dirent)(unsafe.Pointer(&d.buf[d.bufp]));
if dirent.Reclen == 0 {
d.bufp = d.nbuf;
break
}
d.bufp += int(dirent.Reclen);
if dirent.Ino == 0 { // File absent in directory.
continue
}
bytes := (*[len(dirent.Name)]byte)(unsafe.Pointer(&dirent.Name[0]));
var name = string(bytes[0:dirent.Namlen]);
if name == "." || name == ".." { // Useless names
continue
}
count--;
if len(names) == cap(names) {
nnames := make([]string, len(names), 2*len(names));
for i := 0; i < len(names); i++ {
nnames[i] = names[i]
}
names = nnames;
}
names = names[0:len(names)+1];
names[len(names)-1] = name;
}
}
return names, nil
}

View File

@ -0,0 +1,41 @@
// 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.
// 386, Darwin
package os
import syscall "syscall"
import os "os"
func isSymlink(stat *syscall.Stat_t) bool {
return stat.Mode & syscall.S_IFMT == syscall.S_IFLNK
}
func dirFromStat(name string, dir *Dir, lstat, stat *syscall.Stat_t) *Dir {
dir.Dev = uint64(stat.Dev);
dir.Ino = stat.Ino;
dir.Nlink = uint64(stat.Nlink);
dir.Mode = uint32(stat.Mode);
dir.Uid = stat.Uid;
dir.Gid = stat.Gid;
dir.Rdev = uint64(stat.Rdev);
dir.Size = uint64(stat.Size);
dir.Blksize = uint64(stat.Blksize);
dir.Blocks = uint64(stat.Blocks);
dir.Atime_ns = uint64(syscall.TimespecToNsec(stat.Atimespec));
dir.Mtime_ns = uint64(syscall.TimespecToNsec(stat.Mtimespec));
dir.Ctime_ns = uint64(syscall.TimespecToNsec(stat.Ctimespec));
for i := len(name) - 1; i >= 0; i-- {
if name[i] == '/' {
name = name[i+1:len(name)];
break;
}
}
dir.Name = name;
if isSymlink(lstat) && !isSymlink(stat) {
dir.FollowedSymlink = true;
}
return dir;
}