2010-12-03 05:34:57 +01: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
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
func sameFile(fs1, fs2 *fileStat) bool {
|
|
|
|
return fs1.sys.Dev == fs2.sys.Dev && fs1.sys.Ino == fs2.sys.Ino
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
func rename(oldname, newname string) error {
|
|
|
|
e := syscall.Rename(oldname, newname)
|
|
|
|
if e != nil {
|
|
|
|
return &LinkError{"rename", oldname, newname, e}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// File represents an open file descriptor.
|
|
|
|
type File struct {
|
2011-12-13 00:40:51 +01:00
|
|
|
*file
|
|
|
|
}
|
|
|
|
|
|
|
|
// file is the real representation of *File.
|
|
|
|
// The extra level of indirection ensures that no clients of os
|
|
|
|
// can overwrite this data, which could cause the finalizer
|
|
|
|
// to close the wrong file descriptor.
|
|
|
|
type file struct {
|
2011-09-16 17:47:21 +02:00
|
|
|
fd int
|
|
|
|
name string
|
|
|
|
dirinfo *dirInfo // nil unless directory being read
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fd returns the integer Unix file descriptor referencing the open file.
|
2015-01-15 01:27:56 +01:00
|
|
|
// The file descriptor is valid only until f.Close is called or f is garbage collected.
|
2012-03-02 17:38:43 +01:00
|
|
|
func (f *File) Fd() uintptr {
|
2011-12-13 20:16:27 +01:00
|
|
|
if f == nil {
|
2012-03-02 17:38:43 +01:00
|
|
|
return ^(uintptr(0))
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
return uintptr(f.fd)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFile returns a new File with the given file descriptor and name.
|
2012-03-02 17:38:43 +01:00
|
|
|
func NewFile(fd uintptr, name string) *File {
|
|
|
|
fdi := int(fd)
|
|
|
|
if fdi < 0 {
|
2011-09-16 17:47:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
f := &File{&file{fd: fdi, name: name}}
|
2011-12-13 00:40:51 +01:00
|
|
|
runtime.SetFinalizer(f.file, (*file).close)
|
2011-09-16 17:47:21 +02:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Auxiliary information if the File describes a directory
|
|
|
|
type dirInfo struct {
|
2011-12-07 02:11:29 +01:00
|
|
|
buf []byte // buffer for directory I/O
|
2010-12-03 05:34:57 +01:00
|
|
|
dir *syscall.DIR // from opendir
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
// epipecheck raises SIGPIPE if we get an EPIPE error on standard
|
|
|
|
// output or standard error. See the SIGPIPE docs in os/signal, and
|
|
|
|
// issue 11845.
|
2012-10-03 07:27:36 +02:00
|
|
|
func epipecheck(file *File, e error) {
|
2016-02-03 22:58:02 +01:00
|
|
|
if e == syscall.EPIPE && (file.fd == 1 || file.fd == 2) {
|
|
|
|
sigpipe()
|
2012-10-03 07:27:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// DevNull is the name of the operating system's ``null device.''
|
|
|
|
// On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
|
|
|
|
const DevNull = "/dev/null"
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
// OpenFile is the generalized open call; most users will use Open
|
2016-07-22 20:15:38 +02:00
|
|
|
// or Create instead. It opens the named file with specified flag
|
|
|
|
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
|
2011-04-07 19:09:10 +02:00
|
|
|
// methods on the returned File can be used for I/O.
|
2012-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2015-10-31 01:59:47 +01:00
|
|
|
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
|
|
|
|
chmod := false
|
|
|
|
if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
|
|
|
|
if _, err := Stat(name); IsNotExist(err) {
|
|
|
|
chmod = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
var r int
|
|
|
|
for {
|
|
|
|
var e error
|
|
|
|
r, e = syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
|
|
|
|
if e == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// On OS X, sigaction(2) doesn't guarantee that SA_RESTART will cause
|
|
|
|
// open(2) to be restarted for regular files. This is easy to reproduce on
|
|
|
|
// fuse file systems (see http://golang.org/issue/11180).
|
|
|
|
if runtime.GOOS == "darwin" && e == syscall.EINTR {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
return nil, &PathError{"open", name, e}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
// open(2) itself won't handle the sticky bit on *BSD and Solaris
|
|
|
|
if chmod {
|
|
|
|
Chmod(name, perm)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// There's a race here with fork/exec, which we are
|
2016-07-22 20:15:38 +02:00
|
|
|
// content to live with. See ../syscall/exec_unix.go.
|
2014-07-19 10:53:52 +02:00
|
|
|
if !supportsCloseOnExec {
|
2010-12-03 05:34:57 +01:00
|
|
|
syscall.CloseOnExec(r)
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
return NewFile(uintptr(r), name), nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the File, rendering it unusable for I/O.
|
2011-12-03 03:17:34 +01:00
|
|
|
// It returns an error, if any.
|
2011-12-13 20:16:27 +01:00
|
|
|
func (f *File) Close() error {
|
2013-11-06 20:49:01 +01:00
|
|
|
if f == nil {
|
|
|
|
return ErrInvalid
|
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
return f.file.close()
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (file *file) close() error {
|
2010-12-03 05:34:57 +01:00
|
|
|
if file == nil || file.fd < 0 {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-12-03 03:17:34 +01:00
|
|
|
var err error
|
2011-12-13 00:40:51 +01:00
|
|
|
if e := syscall.Close(file.fd); e != nil {
|
|
|
|
err = &PathError{"close", file.name, e}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-01-28 01:01:08 +01:00
|
|
|
|
|
|
|
if file.dirinfo != nil {
|
2012-12-06 03:00:13 +01:00
|
|
|
syscall.Entersyscall()
|
|
|
|
i := libc_closedir(file.dirinfo.dir)
|
|
|
|
errno := syscall.GetErrno()
|
|
|
|
syscall.Exitsyscall()
|
|
|
|
file.dirinfo = nil
|
|
|
|
if i < 0 && err == nil {
|
|
|
|
err = &PathError{"closedir", file.name, errno}
|
2011-01-28 01:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
file.fd = -1 // so it can't be closed again
|
|
|
|
|
|
|
|
// no need for a finalizer anymore
|
|
|
|
runtime.SetFinalizer(file, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns the FileInfo structure describing file.
|
2012-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2015-10-31 01:59:47 +01:00
|
|
|
func (f *File) Stat() (FileInfo, error) {
|
2013-11-06 20:49:01 +01:00
|
|
|
if f == nil {
|
|
|
|
return nil, ErrInvalid
|
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
var fs fileStat
|
|
|
|
err := syscall.Fstat(f.fd, &fs.sys)
|
2011-12-13 20:16:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"stat", f.name, err}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
fillFileStatFromSys(&fs, f.name)
|
|
|
|
return &fs, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Stat returns a FileInfo describing the named file.
|
|
|
|
// If there is an error, it will be of type *PathError.
|
2015-10-31 01:59:47 +01:00
|
|
|
func Stat(name string) (FileInfo, error) {
|
2016-02-03 22:58:02 +01:00
|
|
|
var fs fileStat
|
|
|
|
err := syscall.Stat(name, &fs.sys)
|
2011-12-13 20:16:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"stat", name, err}
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
fillFileStatFromSys(&fs, name)
|
|
|
|
return &fs, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Lstat returns a FileInfo describing the named file.
|
|
|
|
// If the file is a symbolic link, the returned FileInfo
|
2016-07-22 20:15:38 +02:00
|
|
|
// describes the symbolic link. Lstat makes no attempt to follow the link.
|
2012-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2015-10-31 01:59:47 +01:00
|
|
|
func Lstat(name string) (FileInfo, error) {
|
2016-02-03 22:58:02 +01:00
|
|
|
var fs fileStat
|
|
|
|
err := syscall.Lstat(name, &fs.sys)
|
2011-12-13 20:16:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, &PathError{"lstat", name, err}
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
fillFileStatFromSys(&fs, name)
|
|
|
|
return &fs, nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func (f *File) readdir(n int) (fi []FileInfo, err error) {
|
2011-12-13 20:16:27 +01:00
|
|
|
dirname := f.name
|
2010-12-03 05:34:57 +01:00
|
|
|
if dirname == "" {
|
|
|
|
dirname = "."
|
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
names, err := f.Readdirnames(n)
|
2014-06-05 01:15:33 +02:00
|
|
|
fi = make([]FileInfo, 0, len(names))
|
|
|
|
for _, filename := range names {
|
|
|
|
fip, lerr := lstat(dirname + "/" + filename)
|
|
|
|
if IsNotExist(lerr) {
|
|
|
|
// File disappeared between readdir + stat.
|
|
|
|
// Just treat it as if it didn't exist.
|
2013-11-27 02:05:38 +01:00
|
|
|
continue
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2014-06-05 01:15:33 +02:00
|
|
|
if lerr != nil {
|
|
|
|
return fi, lerr
|
|
|
|
}
|
|
|
|
fi = append(fi, fip)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
return fi, err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// Darwin and FreeBSD can't read or write 2GB+ at a time,
|
|
|
|
// even on 64-bit systems. See golang.org/issue/7812.
|
|
|
|
// Use 1GB instead of, say, 2GB-1, to keep subsequent
|
|
|
|
// reads aligned.
|
|
|
|
const (
|
|
|
|
needsMaxRW = runtime.GOOS == "darwin" || runtime.GOOS == "freebsd"
|
|
|
|
maxRW = 1 << 30
|
|
|
|
)
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// read reads up to len(b) bytes from the File.
|
|
|
|
// It returns the number of bytes read and an error, if any.
|
2011-12-13 00:40:51 +01:00
|
|
|
func (f *File) read(b []byte) (n int, err error) {
|
2014-07-19 10:53:52 +02:00
|
|
|
if needsMaxRW && len(b) > maxRW {
|
|
|
|
b = b[:maxRW]
|
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
return fixCount(syscall.Read(f.fd, b))
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// pread reads len(b) bytes from the File starting at byte offset off.
|
|
|
|
// It returns the number of bytes read and the error, if any.
|
2014-07-19 10:53:52 +02:00
|
|
|
// EOF is signaled by a zero count with err set to nil.
|
2011-12-13 00:40:51 +01:00
|
|
|
func (f *File) pread(b []byte, off int64) (n int, err error) {
|
2014-07-19 10:53:52 +02:00
|
|
|
if needsMaxRW && len(b) > maxRW {
|
|
|
|
b = b[:maxRW]
|
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
return fixCount(syscall.Pread(f.fd, b, off))
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// write writes len(b) bytes to the File.
|
|
|
|
// It returns the number of bytes written and an error, if any.
|
2011-12-13 00:40:51 +01:00
|
|
|
func (f *File) write(b []byte) (n int, err error) {
|
2012-03-31 00:09:55 +02:00
|
|
|
for {
|
2014-07-19 10:53:52 +02:00
|
|
|
bcap := b
|
|
|
|
if needsMaxRW && len(bcap) > maxRW {
|
|
|
|
bcap = bcap[:maxRW]
|
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
m, err := fixCount(syscall.Write(f.fd, bcap))
|
2012-03-31 00:09:55 +02:00
|
|
|
n += m
|
|
|
|
|
|
|
|
// If the syscall wrote some data but not all (short write)
|
|
|
|
// or it returned EINTR, then assume it stopped early for
|
|
|
|
// reasons that are uninteresting to the caller, and try again.
|
2014-07-19 10:53:52 +02:00
|
|
|
if 0 < m && m < len(bcap) || err == syscall.EINTR {
|
|
|
|
b = b[m:]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if needsMaxRW && len(bcap) != len(b) && err == nil {
|
2012-03-31 00:09:55 +02:00
|
|
|
b = b[m:]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, err
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// pwrite writes len(b) bytes to the File starting at byte offset off.
|
|
|
|
// It returns the number of bytes written and an error, if any.
|
2011-12-13 00:40:51 +01:00
|
|
|
func (f *File) pwrite(b []byte, off int64) (n int, err error) {
|
2014-07-19 10:53:52 +02:00
|
|
|
if needsMaxRW && len(b) > maxRW {
|
|
|
|
b = b[:maxRW]
|
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
return fixCount(syscall.Pwrite(f.fd, b, off))
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// seek sets the offset for the next Read or Write on file to offset, interpreted
|
|
|
|
// according to whence: 0 means relative to the origin of the file, 1 means
|
|
|
|
// relative to the current offset, and 2 means relative to the end.
|
|
|
|
// It returns the new offset and an error, if any.
|
2011-12-13 00:40:51 +01:00
|
|
|
func (f *File) seek(offset int64, whence int) (ret int64, err error) {
|
2011-05-20 02:18:15 +02:00
|
|
|
return syscall.Seek(f.fd, offset, whence)
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Truncate changes the size of the named file.
|
|
|
|
// If the file is a symbolic link, it changes the size 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 Truncate(name string, size int64) error {
|
2011-12-13 00:40:51 +01:00
|
|
|
if e := syscall.Truncate(name, size); e != nil {
|
|
|
|
return &PathError{"truncate", name, e}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2011-03-30 17:33:16 +02:00
|
|
|
|
2012-01-13 06:11:45 +01:00
|
|
|
// Remove removes the named file or directory.
|
2012-03-02 17:38:43 +01:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2012-01-13 06:11:45 +01:00
|
|
|
func Remove(name string) error {
|
|
|
|
// 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 e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
e1 := syscall.Rmdir(name)
|
|
|
|
if e1 == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both failed: figure out which error to return.
|
|
|
|
// OS X and Linux differ on whether unlink(dir)
|
2016-07-22 20:15:38 +02:00
|
|
|
// returns EISDIR, so can't use that. However,
|
2012-01-13 06:11:45 +01:00
|
|
|
// 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.
|
|
|
|
if e1 != syscall.ENOTDIR {
|
|
|
|
e = e1
|
|
|
|
}
|
|
|
|
return &PathError{"remove", name, e}
|
|
|
|
}
|
|
|
|
|
2011-03-30 17:33:16 +02:00
|
|
|
// basename removes trailing slashes and the leading directory name from path name
|
|
|
|
func basename(name string) string {
|
|
|
|
i := len(name) - 1
|
|
|
|
// Remove trailing slashes
|
|
|
|
for ; i > 0 && name[i] == '/'; i-- {
|
|
|
|
name = name[:i]
|
|
|
|
}
|
|
|
|
// Remove leading directory name
|
|
|
|
for i--; i >= 0; i-- {
|
|
|
|
if name[i] == '/' {
|
|
|
|
name = name[i+1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// TempDir returns the default directory to use for temporary files.
|
|
|
|
func TempDir() string {
|
|
|
|
dir := Getenv("TMPDIR")
|
|
|
|
if dir == "" {
|
2015-01-15 01:27:56 +01:00
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
dir = "/data/local/tmp"
|
|
|
|
} else {
|
|
|
|
dir = "/tmp"
|
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
2015-01-15 01:27:56 +01:00
|
|
|
|
|
|
|
// Link creates newname as a hard link to the oldname file.
|
|
|
|
// If there is an error, it will be of type *LinkError.
|
|
|
|
func Link(oldname, newname string) error {
|
|
|
|
e := syscall.Link(oldname, newname)
|
|
|
|
if e != nil {
|
|
|
|
return &LinkError{"link", oldname, newname, e}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink creates newname as a symbolic link to oldname.
|
|
|
|
// If there is an error, it will be of type *LinkError.
|
|
|
|
func Symlink(oldname, newname string) error {
|
|
|
|
e := syscall.Symlink(oldname, newname)
|
|
|
|
if e != nil {
|
|
|
|
return &LinkError{"symlink", oldname, newname, e}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|