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.
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// +build darwin freebsd linux openbsd windows
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
func sigpipe() // implemented in package runtime
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
func epipecheck(file *File, e int) {
|
|
|
|
if e == syscall.EPIPE {
|
|
|
|
file.nepipe++
|
|
|
|
if file.nepipe >= 10 {
|
2011-05-20 02:18:15 +02:00
|
|
|
sigpipe()
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file.nepipe = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the named file or directory.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Remove(name string) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
// System call interface forces us to know
|
|
|
|
// whether name is a file or directory.
|
|
|
|
// Try both: it is cheaper on average than
|
|
|
|
// doing a Stat plus the right one.
|
|
|
|
e := syscall.Unlink(name)
|
|
|
|
if !iserror(e) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
e1 := syscall.Rmdir(name)
|
|
|
|
if !iserror(e1) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both failed: figure out which error to return.
|
|
|
|
// OS X and Linux differ on whether unlink(dir)
|
|
|
|
// returns EISDIR, so can't use that. However,
|
|
|
|
// both agree that rmdir(file) returns ENOTDIR,
|
|
|
|
// so we can use that to decide which error is real.
|
|
|
|
// Rmdir might also return ENOTDIR if given a bad
|
|
|
|
// file path, like /etc/passwd/foo, but in that case,
|
|
|
|
// both errors will be ENOTDIR, so it's okay to
|
|
|
|
// use the error from unlink.
|
|
|
|
// For windows syscall.ENOTDIR is set
|
2011-10-27 01:57:58 +02:00
|
|
|
// to syscall.ERROR_PATH_NOT_FOUND, hopefully it should
|
2011-04-07 19:09:10 +02:00
|
|
|
// do the trick.
|
|
|
|
if e1 != syscall.ENOTDIR {
|
|
|
|
e = e1
|
|
|
|
}
|
|
|
|
return &PathError{"remove", name, Errno(e)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkError records an error during a link or symlink or rename
|
|
|
|
// system call and the paths that caused it.
|
|
|
|
type LinkError struct {
|
2011-12-03 03:17:34 +01:00
|
|
|
Op string
|
|
|
|
Old string
|
|
|
|
New string
|
|
|
|
Err error
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (e *LinkError) Error() string {
|
|
|
|
return e.Op + " " + e.Old + " " + e.New + ": " + e.Err.Error()
|
2011-04-07 19:09:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Link creates a hard link.
|
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)
|
|
|
|
if iserror(e) {
|
|
|
|
return &LinkError{"link", oldname, newname, Errno(e)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink creates a symbolic link.
|
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)
|
|
|
|
if iserror(e) {
|
|
|
|
return &LinkError{"symlink", oldname, newname, Errno(e)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readlink reads the contents of a symbolic link: the destination of
|
2011-12-03 03:17:34 +01:00
|
|
|
// the link. It returns the contents and an error, if any.
|
|
|
|
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)
|
|
|
|
if iserror(e) {
|
|
|
|
return "", &PathError{"readlink", name, Errno(e)}
|
|
|
|
}
|
|
|
|
if n < len {
|
|
|
|
return string(b[0:n]), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Silence 6g.
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename renames a file.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Rename(oldname, newname string) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
e := syscall.Rename(oldname, newname)
|
|
|
|
if iserror(e) {
|
|
|
|
return &LinkError{"rename", oldname, newname, Errno(e)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Chmod(name string, mode uint32) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
if e := syscall.Chmod(name, mode); iserror(e) {
|
|
|
|
return &PathError{"chmod", name, Errno(e)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod changes the mode of the file to mode.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (f *File) Chmod(mode uint32) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
if e := syscall.Fchmod(f.fd, mode); iserror(e) {
|
|
|
|
return &PathError{"chmod", f.name, Errno(e)}
|
|
|
|
}
|
|
|
|
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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Chown(name string, uid, gid int) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
if e := syscall.Chown(name, uid, gid); iserror(e) {
|
|
|
|
return &PathError{"chown", name, Errno(e)}
|
|
|
|
}
|
|
|
|
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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Lchown(name string, uid, gid int) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
if e := syscall.Lchown(name, uid, gid); iserror(e) {
|
|
|
|
return &PathError{"lchown", name, Errno(e)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chown changes the numeric uid and gid of the named file.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (f *File) Chown(uid, gid int) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
if e := syscall.Fchown(f.fd, uid, gid); iserror(e) {
|
|
|
|
return &PathError{"chown", f.name, Errno(e)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate changes the size of the file.
|
|
|
|
// It does not change the I/O offset.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (f *File) Truncate(size int64) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
if e := syscall.Ftruncate(f.fd, size); iserror(e) {
|
|
|
|
return &PathError{"truncate", f.name, Errno(e)}
|
|
|
|
}
|
|
|
|
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-03 03:17:34 +01:00
|
|
|
func (file *File) Sync() (err error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
if file == nil {
|
|
|
|
return EINVAL
|
|
|
|
}
|
|
|
|
if e := syscall.Fsync(file.fd); iserror(e) {
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
// The argument times are in nanoseconds, although the underlying
|
|
|
|
// filesystem may truncate or round the values to a more
|
|
|
|
// coarse time unit.
|
2011-12-03 03:17:34 +01:00
|
|
|
func Chtimes(name string, atime_ns int64, mtime_ns int64) error {
|
2011-04-07 19:09:10 +02:00
|
|
|
var utimes [2]syscall.Timeval
|
|
|
|
utimes[0] = syscall.NsecToTimeval(atime_ns)
|
|
|
|
utimes[1] = syscall.NsecToTimeval(mtime_ns)
|
|
|
|
if e := syscall.Utimes(name, utimes[0:]); iserror(e) {
|
|
|
|
return &PathError{"chtimes", name, Errno(e)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|