2009-06-04 13:33:57 -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.
|
|
|
|
|
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 16:48:57 -04:00
|
|
|
// +build darwin freebsd linux openbsd
|
|
|
|
|
|
2009-06-04 13:33:57 -07:00
|
|
|
package os
|
|
|
|
|
|
|
|
|
|
import (
|
2011-11-01 21:49:08 -04:00
|
|
|
"io"
|
2009-12-15 15:40:16 -08:00
|
|
|
"syscall"
|
2009-06-04 13:33:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2011-03-17 13:57:36 -04:00
|
|
|
blockSize = 4096
|
2009-06-04 13:33:57 -07:00
|
|
|
)
|
|
|
|
|
|
2011-05-16 09:26:16 -07:00
|
|
|
// Readdirnames reads and returns a slice of names from the directory f.
|
|
|
|
|
//
|
|
|
|
|
// If n > 0, Readdirnames returns at most n names. In this case, if
|
|
|
|
|
// Readdirnames returns an empty slice, it will return a non-nil error
|
2011-11-03 14:01:30 -07:00
|
|
|
// explaining why. At the end of a directory, the error is io.EOF.
|
2011-05-16 09:26:16 -07:00
|
|
|
//
|
|
|
|
|
// If n <= 0, Readdirnames returns all the names from the directory in
|
|
|
|
|
// a single slice. In this case, if Readdirnames succeeds (reads all
|
|
|
|
|
// the way to the end of the directory), it returns the slice and a
|
2011-11-01 22:58:09 -04:00
|
|
|
// nil error. If it encounters an error before the end of the
|
2011-05-16 09:26:16 -07:00
|
|
|
// directory, Readdirnames returns the names read until that point and
|
|
|
|
|
// a non-nil error.
|
2011-11-01 21:49:08 -04:00
|
|
|
func (f *File) Readdirnames(n int) (names []string, err error) {
|
2009-06-04 13:33:57 -07:00
|
|
|
// If this file has no dirinfo, create one.
|
2011-05-16 09:26:16 -07:00
|
|
|
if f.dirinfo == nil {
|
|
|
|
|
f.dirinfo = new(dirInfo)
|
2009-06-04 13:33:57 -07:00
|
|
|
// The buffer must be at least a block long.
|
2011-05-16 09:26:16 -07:00
|
|
|
f.dirinfo.buf = make([]byte, blockSize)
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
2011-05-16 09:26:16 -07:00
|
|
|
d := f.dirinfo
|
|
|
|
|
|
|
|
|
|
size := n
|
2011-05-27 12:58:59 -07:00
|
|
|
if size <= 0 {
|
2009-11-09 12:07:39 -08:00
|
|
|
size = 100
|
2011-05-27 12:58:59 -07:00
|
|
|
n = -1
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
2011-05-16 09:26:16 -07:00
|
|
|
|
2009-12-15 15:40:16 -08:00
|
|
|
names = make([]string, 0, size) // Empty with room to grow.
|
2011-05-16 09:26:16 -07:00
|
|
|
for n != 0 {
|
2009-06-04 13:33:57 -07:00
|
|
|
// Refill the buffer if necessary
|
|
|
|
|
if d.bufp >= d.nbuf {
|
2009-12-15 15:40:16 -08:00
|
|
|
d.bufp = 0
|
2011-04-06 15:44:40 -04:00
|
|
|
var errno int
|
2011-05-16 09:26:16 -07:00
|
|
|
d.nbuf, errno = syscall.ReadDirent(f.fd, d.buf)
|
2009-06-04 13:33:57 -07:00
|
|
|
if errno != 0 {
|
2011-04-06 15:44:40 -04:00
|
|
|
return names, NewSyscallError("readdirent", errno)
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
2009-06-25 20:24:55 -07:00
|
|
|
if d.nbuf <= 0 {
|
2009-12-15 15:40:16 -08:00
|
|
|
break // EOF
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
2011-04-06 15:44:40 -04:00
|
|
|
|
2009-06-04 13:33:57 -07:00
|
|
|
// Drain the buffer
|
2011-04-06 15:44:40 -04:00
|
|
|
var nb, nc int
|
2011-05-16 09:26:16 -07:00
|
|
|
nb, nc, names = syscall.ParseDirent(d.buf[d.bufp:d.nbuf], n, names)
|
2011-04-06 15:44:40 -04:00
|
|
|
d.bufp += nb
|
2011-05-16 09:26:16 -07:00
|
|
|
n -= nc
|
|
|
|
|
}
|
2011-05-27 12:58:59 -07:00
|
|
|
if n >= 0 && len(names) == 0 {
|
2011-11-01 21:49:08 -04:00
|
|
|
return names, io.EOF
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
return names, nil
|
2009-06-04 13:33:57 -07:00
|
|
|
}
|