2011-04-02 15:28:58 -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.
|
|
|
|
|
all: merge NaCl branch (part 1)
See golang.org/s/go13nacl for design overview.
This CL is the mostly mechanical changes from rsc's Go 1.2 based NaCl branch, specifically 39cb35750369 to 500771b477cf from https://code.google.com/r/rsc-go13nacl. This CL does not include working NaCl support, there are probably two or three more large merges to come.
CL 15750044 is not included as it involves more invasive changes to the linker which will need to be merged separately.
The exact change lists included are
15050047: syscall: support for Native Client
15360044: syscall: unzip implementation for Native Client
15370044: syscall: Native Client SRPC implementation
15400047: cmd/dist, cmd/go, go/build, test: support for Native Client
15410048: runtime: support for Native Client
15410049: syscall: file descriptor table for Native Client
15410050: syscall: in-memory file system for Native Client
15440048: all: update +build lines for Native Client port
15540045: cmd/6g, cmd/8g, cmd/gc: support for Native Client
15570045: os: support for Native Client
15680044: crypto/..., hash/crc32, reflect, sync/atomic: support for amd64p32
15690044: net: support for Native Client
15690048: runtime: support for fake time like on Go Playground
15690051: build: disable various tests on Native Client
LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/68150047
2014-02-25 07:47:42 -07:00
|
|
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
|
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
|
|
|
|
2011-04-02 15:28:58 -06:00
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
2011-11-30 10:01:46 -07:00
|
|
|
"time"
|
2011-04-02 15:28:58 -06:00
|
|
|
)
|
|
|
|
|
2011-04-25 14:58:00 -06:00
|
|
|
func sigpipe() // implemented in package runtime
|
|
|
|
|
2012-02-12 20:21:39 -07:00
|
|
|
// Readlink returns the destination of the named symbolic link.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-11-01 19:49:08 -06:00
|
|
|
func Readlink(name string) (string, error) {
|
2011-04-02 15:28:58 -06:00
|
|
|
for len := 128; ; len *= 2 {
|
|
|
|
b := make([]byte, len)
|
2016-10-28 11:01:51 -06:00
|
|
|
n, e := fixCount(syscall.Readlink(fixLongPath(name), b))
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
|
|
|
return "", &PathError{"readlink", name, e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
if n < len {
|
|
|
|
return string(b[0:n]), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:45:18 -07:00
|
|
|
// syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
|
|
|
|
func syscallMode(i FileMode) (o uint32) {
|
|
|
|
o |= uint32(i.Perm())
|
|
|
|
if i&ModeSetuid != 0 {
|
|
|
|
o |= syscall.S_ISUID
|
|
|
|
}
|
|
|
|
if i&ModeSetgid != 0 {
|
|
|
|
o |= syscall.S_ISGID
|
|
|
|
}
|
|
|
|
if i&ModeSticky != 0 {
|
|
|
|
o |= syscall.S_ISVTX
|
|
|
|
}
|
|
|
|
// No mapping for Go's ModeTemporary (plan9 only).
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-04-02 15:28:58 -06:00
|
|
|
// Chmod changes the mode of the named file to mode.
|
|
|
|
// If the file is a symbolic link, it changes the mode of the link's target.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2012-01-19 16:45:18 -07:00
|
|
|
func Chmod(name string, mode FileMode) error {
|
|
|
|
if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
|
2011-11-13 20:44:52 -07:00
|
|
|
return &PathError{"chmod", name, e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod changes the mode of the file to mode.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2012-01-19 16:45:18 -07:00
|
|
|
func (f *File) Chmod(mode FileMode) error {
|
2016-10-06 22:46:56 -06:00
|
|
|
if err := f.checkValid("chmod"); err != nil {
|
|
|
|
return err
|
2013-08-19 22:33:03 -06:00
|
|
|
}
|
2012-01-19 16:45:18 -07:00
|
|
|
if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
|
2011-11-13 20:44:52 -07:00
|
|
|
return &PathError{"chmod", f.name, e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chown changes the numeric uid and gid of the named file.
|
|
|
|
// If the file is a symbolic link, it changes the uid and gid of the link's target.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-11-01 19:49:08 -06:00
|
|
|
func Chown(name string, uid, gid int) error {
|
2011-11-13 20:44:52 -07:00
|
|
|
if e := syscall.Chown(name, uid, gid); e != nil {
|
|
|
|
return &PathError{"chown", name, e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lchown changes the numeric uid and gid of the named file.
|
|
|
|
// If the file is a symbolic link, it changes the uid and gid of the link itself.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-11-01 19:49:08 -06:00
|
|
|
func Lchown(name string, uid, gid int) error {
|
2011-11-13 20:44:52 -07:00
|
|
|
if e := syscall.Lchown(name, uid, gid); e != nil {
|
|
|
|
return &PathError{"lchown", name, e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chown changes the numeric uid and gid of the named file.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-11-01 19:49:08 -06:00
|
|
|
func (f *File) Chown(uid, gid int) error {
|
2016-10-06 22:46:56 -06:00
|
|
|
if err := f.checkValid("chown"); err != nil {
|
|
|
|
return err
|
2013-08-19 22:33:03 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
if e := syscall.Fchown(f.fd, uid, gid); e != nil {
|
|
|
|
return &PathError{"chown", f.name, e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate changes the size of the file.
|
|
|
|
// It does not change the I/O offset.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-11-01 19:49:08 -06:00
|
|
|
func (f *File) Truncate(size int64) error {
|
2016-10-06 22:46:56 -06:00
|
|
|
if err := f.checkValid("truncate"); err != nil {
|
|
|
|
return err
|
2013-08-19 22:33:03 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
if e := syscall.Ftruncate(f.fd, size); e != nil {
|
|
|
|
return &PathError{"truncate", f.name, e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync commits the current contents of the file to stable storage.
|
|
|
|
// Typically, this means flushing the file system's in-memory copy
|
|
|
|
// of recently written data to disk.
|
2015-07-17 09:26:29 -06:00
|
|
|
func (f *File) Sync() error {
|
2016-10-06 22:46:56 -06:00
|
|
|
if err := f.checkValid("sync"); err != nil {
|
|
|
|
return err
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2011-12-01 12:23:39 -07:00
|
|
|
if e := syscall.Fsync(f.fd); e != nil {
|
2016-10-06 22:46:56 -06:00
|
|
|
return &PathError{"sync", f.name, e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chtimes changes the access and modification times of the named
|
|
|
|
// file, similar to the Unix utime() or utimes() functions.
|
|
|
|
//
|
2011-11-30 10:01:46 -07:00
|
|
|
// The underlying filesystem may truncate or round the values to a
|
|
|
|
// less precise time unit.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-11-30 10:01:46 -07:00
|
|
|
func Chtimes(name string, atime time.Time, mtime time.Time) error {
|
2012-12-13 14:02:39 -07:00
|
|
|
var utimes [2]syscall.Timespec
|
|
|
|
utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
|
|
|
|
utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
|
2016-10-28 11:01:51 -06:00
|
|
|
if e := syscall.UtimesNano(fixLongPath(name), utimes[0:]); e != nil {
|
2011-11-13 20:44:52 -07:00
|
|
|
return &PathError{"chtimes", name, e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|