summaryrefslogtreecommitdiff
path: root/src/pkg/os/dir_linux_386.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-06-09 09:53:44 -0700
committerRob Pike <r@golang.org>2009-06-09 09:53:44 -0700
commit7249ea4df2b4f12a4e7ed446f270cea87e4ffd34 (patch)
tree7032a11d0cac2ae4d3e90f7a189b575b5a50f848 /src/pkg/os/dir_linux_386.go
parentacf6ef7a82b3fe61516a1bac4563706552bdf078 (diff)
downloadgolang-7249ea4df2b4f12a4e7ed446f270cea87e4ffd34.tar.gz
mv src/lib to src/pkg
tests: all.bash passes, gobuild still works, godoc still works. R=rsc OCL=30096 CL=30102
Diffstat (limited to 'src/pkg/os/dir_linux_386.go')
-rw-r--r--src/pkg/os/dir_linux_386.go83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/pkg/os/dir_linux_386.go b/src/pkg/os/dir_linux_386.go
new file mode 100644
index 000000000..c4594a52d
--- /dev/null
+++ b/src/pkg/os/dir_linux_386.go
@@ -0,0 +1,83 @@
+// 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.
+
+// TODO(rsc): Once the porting dust settles, consider
+// whether this file should be dir_linux.go (and similarly
+// dir_darwin.go) instead of having one copy per architecture.
+
+package os
+
+import (
+ "os";
+ "syscall";
+ "unsafe";
+)
+
+const (
+ blockSize = 4096 // TODO(r): use statfs
+)
+
+func clen(n []byte) int {
+ for i := 0; i < len(n); i++ {
+ if n[i] == 0 {
+ return i
+ }
+ }
+ return len(n)
+}
+
+// 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.nbuf, errno = syscall.Getdents(file.fd, d.buf);
+ if d.nbuf < 0 {
+ return names, ErrnoToError(errno)
+ }
+ if d.nbuf == 0 {
+ break // EOF
+ }
+ d.bufp = 0;
+ }
+ // Drain the buffer
+ for count != 0 && d.bufp < d.nbuf {
+ dirent := (*syscall.Dirent)(unsafe.Pointer(&d.buf[d.bufp]));
+ 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:clen(bytes)]);
+ 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;
+}