2009-06-04 14:33:57 -06: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.
|
|
|
|
|
2018-12-08 08:45:29 -07:00
|
|
|
// +build aix darwin,!arm,!arm64 dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris
|
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 14:48:57 -06:00
|
|
|
|
2009-06-04 14:33:57 -06:00
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
2011-11-01 19:49:08 -06:00
|
|
|
"io"
|
2017-02-10 16:17:38 -07:00
|
|
|
"runtime"
|
2009-12-15 16:40:16 -07:00
|
|
|
"syscall"
|
2009-06-04 14:33:57 -06:00
|
|
|
)
|
|
|
|
|
2018-12-08 08:45:29 -07:00
|
|
|
// Auxiliary information if the File describes a directory
|
|
|
|
type dirInfo struct {
|
|
|
|
buf []byte // buffer for directory I/O
|
|
|
|
nbuf int // length of buf; return value from Getdirentries
|
|
|
|
bufp int // location of next record in buf.
|
|
|
|
}
|
|
|
|
|
2009-06-04 14:33:57 -06:00
|
|
|
const (
|
2018-06-29 15:38:56 -06:00
|
|
|
// More than 5760 to work around https://golang.org/issue/24015.
|
|
|
|
blockSize = 8192
|
2009-06-04 14:33:57 -06:00
|
|
|
)
|
|
|
|
|
2018-12-08 08:45:29 -07:00
|
|
|
func (d *dirInfo) close() {}
|
2016-08-14 10:39:00 -06:00
|
|
|
|
2012-01-16 22:51:54 -07:00
|
|
|
func (f *File) readdirnames(n int) (names []string, err error) {
|
2009-06-04 14:33:57 -06:00
|
|
|
// If this file has no dirinfo, create one.
|
2011-05-16 10:26:16 -06:00
|
|
|
if f.dirinfo == nil {
|
|
|
|
f.dirinfo = new(dirInfo)
|
2009-06-04 14:33:57 -06:00
|
|
|
// The buffer must be at least a block long.
|
2011-05-16 10:26:16 -06:00
|
|
|
f.dirinfo.buf = make([]byte, blockSize)
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|
2011-05-16 10:26:16 -06:00
|
|
|
d := f.dirinfo
|
|
|
|
|
|
|
|
size := n
|
2011-05-27 13:58:59 -06:00
|
|
|
if size <= 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
size = 100
|
2011-05-27 13:58:59 -06:00
|
|
|
n = -1
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|
2011-05-16 10:26:16 -06:00
|
|
|
|
2009-12-15 16:40:16 -07:00
|
|
|
names = make([]string, 0, size) // Empty with room to grow.
|
2011-05-16 10:26:16 -06:00
|
|
|
for n != 0 {
|
2009-06-04 14:33:57 -06:00
|
|
|
// Refill the buffer if necessary
|
|
|
|
if d.bufp >= d.nbuf {
|
2009-12-15 16:40:16 -07:00
|
|
|
d.bufp = 0
|
2011-11-13 20:44:52 -07:00
|
|
|
var errno error
|
2017-02-10 16:17:38 -07:00
|
|
|
d.nbuf, errno = f.pfd.ReadDirent(d.buf)
|
|
|
|
runtime.KeepAlive(f)
|
2011-11-13 20:44:52 -07:00
|
|
|
if errno != nil {
|
2017-02-10 16:17:38 -07:00
|
|
|
return names, wrapSyscallError("readdirent", errno)
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
if d.nbuf <= 0 {
|
2009-12-15 16:40:16 -07:00
|
|
|
break // EOF
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|
|
|
|
}
|
2011-04-06 13:44:40 -06:00
|
|
|
|
2009-06-04 14:33:57 -06:00
|
|
|
// Drain the buffer
|
2011-04-06 13:44:40 -06:00
|
|
|
var nb, nc int
|
2011-05-16 10:26:16 -06:00
|
|
|
nb, nc, names = syscall.ParseDirent(d.buf[d.bufp:d.nbuf], n, names)
|
2011-04-06 13:44:40 -06:00
|
|
|
d.bufp += nb
|
2011-05-16 10:26:16 -06:00
|
|
|
n -= nc
|
|
|
|
}
|
2011-05-27 13:58:59 -06:00
|
|
|
if n >= 0 && len(names) == 0 {
|
2011-11-01 19:49:08 -06:00
|
|
|
return names, io.EOF
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return names, nil
|
2009-06-04 14:33:57 -06:00
|
|
|
}
|