2011-04-07 19:09:10 +02: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.
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
2011-12-13 20:16:27 +01:00
|
|
|
"time"
|
2011-04-07 19:09:10 +02:00
|
|
|
)
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
func sigpipe() // implemented in package runtime
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Link creates newname as a hard link to the oldname file.
|
2012-03-02 21:01:37 +01:00
|
|
|
// If there is an error, it will be of type *LinkError.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Link(oldname, newname string) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
e := syscall.Link(oldname, newname)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
|
|
|
return &LinkError{"link", oldname, newname, e}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Symlink creates newname as a symbolic link to oldname.
|
2012-03-02 21:01:37 +01:00
|
|
|
// If there is an error, it will be of type *LinkError.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Symlink(oldname, newname string) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
e := syscall.Symlink(oldname, newname)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
|
|
|
return &LinkError{"symlink", oldname, newname, e}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Readlink returns the destination of the named symbolic link.
|
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Readlink(name string) (string, error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
for len := 128; ; len *= 2 {
|
|
|
|
b := make([]byte, len)
|
|
|
|
n, e := syscall.Readlink(name, b)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
|
|
|
return "", &PathError{"readlink", name, e}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
if n < len {
|
|
|
|
return string(b[0:n]), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
func rename(oldname, newname string) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
e := syscall.Rename(oldname, newname)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
|
|
|
return &LinkError{"rename", oldname, newname, e}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01: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-07 19:09:10 +02: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-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2012-01-25 22:54:22 +01:00
|
|
|
func Chmod(name string, mode FileMode) error {
|
|
|
|
if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
return &PathError{"chmod", name, e}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod changes the mode of the file to mode.
|
2012-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2012-01-25 22:54:22 +01:00
|
|
|
func (f *File) Chmod(mode FileMode) error {
|
2013-11-06 20:49:01 +01:00
|
|
|
if f == nil {
|
|
|
|
return ErrInvalid
|
|
|
|
}
|
2012-01-25 22:54:22 +01:00
|
|
|
if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
return &PathError{"chmod", f.name, e}
|
2011-04-07 19:09:10 +02: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-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Chown(name string, uid, gid int) error {
|
2011-12-13 00:40:51 +01:00
|
|
|
if e := syscall.Chown(name, uid, gid); e != nil {
|
|
|
|
return &PathError{"chown", name, e}
|
2011-04-07 19:09:10 +02: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-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Lchown(name string, uid, gid int) error {
|
2011-12-13 00:40:51 +01:00
|
|
|
if e := syscall.Lchown(name, uid, gid); e != nil {
|
|
|
|
return &PathError{"lchown", name, e}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chown changes the numeric uid and gid of the named file.
|
2012-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (f *File) Chown(uid, gid int) error {
|
2013-11-06 20:49:01 +01:00
|
|
|
if f == nil {
|
|
|
|
return ErrInvalid
|
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
if e := syscall.Fchown(f.fd, uid, gid); e != nil {
|
|
|
|
return &PathError{"chown", f.name, e}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate changes the size of the file.
|
|
|
|
// It does not change the I/O offset.
|
2012-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (f *File) Truncate(size int64) error {
|
2013-11-06 20:49:01 +01:00
|
|
|
if f == nil {
|
|
|
|
return ErrInvalid
|
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
if e := syscall.Ftruncate(f.fd, size); e != nil {
|
|
|
|
return &PathError{"truncate", f.name, e}
|
2011-04-07 19:09:10 +02: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.
|
2011-12-13 20:16:27 +01:00
|
|
|
func (f *File) Sync() (err error) {
|
|
|
|
if f == nil {
|
2014-06-05 01:15:33 +02:00
|
|
|
return ErrInvalid
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
if e := syscall.Fsync(f.fd); e != nil {
|
2011-04-07 19:09:10 +02:00
|
|
|
return NewSyscallError("fsync", e)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chtimes changes the access and modification times of the named
|
|
|
|
// file, similar to the Unix utime() or utimes() functions.
|
|
|
|
//
|
2011-12-13 20:16:27 +01:00
|
|
|
// The underlying filesystem may truncate or round the values to a
|
|
|
|
// less precise time unit.
|
2012-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-12-13 20:16:27 +01:00
|
|
|
func Chtimes(name string, atime time.Time, mtime time.Time) error {
|
2012-12-22 02:15:33 +01:00
|
|
|
var utimes [2]syscall.Timespec
|
|
|
|
utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
|
|
|
|
utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
|
|
|
|
if e := syscall.UtimesNano(name, utimes[0:]); e != nil {
|
2011-12-13 00:40:51 +01:00
|
|
|
return &PathError{"chtimes", name, e}
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|