2010-04-13 16:30:11 -07: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.
|
|
|
|
|
|
2019-10-08 19:19:13 +00:00
|
|
|
// +build aix darwin dragonfly freebsd js,wasm linux 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 16:48:57 -04:00
|
|
|
|
2010-04-13 16:30:11 -07:00
|
|
|
package os
|
|
|
|
|
|
|
|
|
|
import (
|
2017-02-10 15:17:38 -08:00
|
|
|
"internal/poll"
|
2018-03-11 19:11:33 +02:00
|
|
|
"internal/syscall/unix"
|
2010-04-13 16:30:11 -07:00
|
|
|
"runtime"
|
|
|
|
|
"syscall"
|
|
|
|
|
)
|
|
|
|
|
|
2016-10-28 13:01:51 -04:00
|
|
|
// fixLongPath is a noop on non-Windows platforms.
|
|
|
|
|
func fixLongPath(path string) string {
|
|
|
|
|
return path
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-26 12:10:11 -08:00
|
|
|
func rename(oldname, newname string) error {
|
2016-10-18 12:34:19 -04:00
|
|
|
fi, err := Lstat(newname)
|
|
|
|
|
if err == nil && fi.IsDir() {
|
2017-05-18 22:50:35 +00:00
|
|
|
// There are two independent errors this function can return:
|
|
|
|
|
// one for a bad oldname, and one for a bad newname.
|
|
|
|
|
// At this point we've determined the newname is bad.
|
|
|
|
|
// But just in case oldname is also bad, prioritize returning
|
|
|
|
|
// the oldname error because that's what we did historically.
|
2019-10-31 23:24:26 +00:00
|
|
|
// However, if the old name and new name are not the same, yet
|
|
|
|
|
// they refer to the same file, it implies a case-only
|
|
|
|
|
// rename on a case-insensitive filesystem, which is ok.
|
|
|
|
|
if ofi, err := Lstat(oldname); err != nil {
|
2017-05-18 22:50:35 +00:00
|
|
|
if pe, ok := err.(*PathError); ok {
|
|
|
|
|
err = pe.Err
|
2017-04-13 11:23:35 -05:00
|
|
|
}
|
|
|
|
|
return &LinkError{"rename", oldname, newname, err}
|
2019-10-31 23:24:26 +00:00
|
|
|
} else if newname == oldname || !SameFile(fi, ofi) {
|
|
|
|
|
return &LinkError{"rename", oldname, newname, syscall.EEXIST}
|
2017-04-13 11:23:35 -05:00
|
|
|
}
|
2016-10-18 12:34:19 -04:00
|
|
|
}
|
2020-08-18 16:46:24 -07:00
|
|
|
err = ignoringEINTR(func() error {
|
|
|
|
|
return syscall.Rename(oldname, newname)
|
|
|
|
|
})
|
2017-04-13 11:23:35 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return &LinkError{"rename", oldname, newname, err}
|
2015-02-26 12:10:11 -08:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 12:20:59 -05:00
|
|
|
// 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 {
|
2017-09-23 22:00:53 -07:00
|
|
|
pfd poll.FD
|
|
|
|
|
name string
|
|
|
|
|
dirinfo *dirInfo // nil unless directory being read
|
|
|
|
|
nonblock bool // whether we set nonblocking mode
|
|
|
|
|
stdoutOrErr bool // whether this is stdout or stderr
|
2019-03-11 13:58:20 +07:00
|
|
|
appendMode bool // whether file is opened for appending
|
2011-07-01 10:18:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fd returns the integer Unix file descriptor referencing the open file.
|
2020-09-24 08:57:00 +02:00
|
|
|
// If f is closed, the file descriptor becomes invalid.
|
|
|
|
|
// If f is garbage collected, a finalizer may close the file descriptor,
|
|
|
|
|
// making it invalid; see runtime.SetFinalizer for more information on when
|
|
|
|
|
// a finalizer might be run. On Unix systems this will cause the SetDeadline
|
|
|
|
|
// methods to stop working.
|
|
|
|
|
//
|
2020-09-28 09:46:42 +02:00
|
|
|
// As an alternative, see the f.SyscallConn method.
|
2012-02-10 14:16:15 +11:00
|
|
|
func (f *File) Fd() uintptr {
|
2011-12-01 11:23:39 -08:00
|
|
|
if f == nil {
|
2012-02-10 14:16:15 +11:00
|
|
|
return ^(uintptr(0))
|
2011-07-01 10:18:07 -04:00
|
|
|
}
|
2017-02-10 15:17:38 -08:00
|
|
|
|
|
|
|
|
// If we put the file descriptor into nonblocking mode,
|
|
|
|
|
// then set it to blocking mode before we return it,
|
|
|
|
|
// because historically we have always returned a descriptor
|
|
|
|
|
// opened in blocking mode. The File will continue to work,
|
|
|
|
|
// but any blocking operation will tie up a thread.
|
|
|
|
|
if f.nonblock {
|
2017-12-13 06:32:06 -08:00
|
|
|
f.pfd.SetBlocking()
|
2017-02-10 15:17:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uintptr(f.pfd.Sysfd)
|
2011-07-01 10:18:07 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-20 11:13:55 +02:00
|
|
|
// NewFile returns a new File with the given file descriptor and
|
|
|
|
|
// name. The returned value will be nil if fd is not a valid file
|
2018-03-11 19:11:33 +02:00
|
|
|
// descriptor. On Unix systems, if the file descriptor is in
|
|
|
|
|
// non-blocking mode, NewFile will attempt to return a pollable File
|
|
|
|
|
// (one for which the SetDeadline methods work).
|
2012-02-10 14:16:15 +11:00
|
|
|
func NewFile(fd uintptr, name string) *File {
|
2018-03-11 19:11:33 +02:00
|
|
|
kind := kindNewFile
|
|
|
|
|
if nb, err := unix.IsNonblock(int(fd)); err == nil && nb {
|
|
|
|
|
kind = kindNonBlock
|
|
|
|
|
}
|
|
|
|
|
return newFile(fd, name, kind)
|
2017-02-10 15:17:38 -08:00
|
|
|
}
|
|
|
|
|
|
2017-10-17 13:57:34 -07:00
|
|
|
// newFileKind describes the kind of file to newFile.
|
|
|
|
|
type newFileKind int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
kindNewFile newFileKind = iota
|
|
|
|
|
kindOpenFile
|
|
|
|
|
kindPipe
|
2018-03-11 19:11:33 +02:00
|
|
|
kindNonBlock
|
2017-10-17 13:57:34 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// newFile is like NewFile, but if called from OpenFile or Pipe
|
|
|
|
|
// (as passed in the kind parameter) it tries to add the file to
|
|
|
|
|
// the runtime poller.
|
|
|
|
|
func newFile(fd uintptr, name string, kind newFileKind) *File {
|
2012-02-10 14:16:15 +11:00
|
|
|
fdi := int(fd)
|
|
|
|
|
if fdi < 0 {
|
2011-07-01 10:18:07 -04:00
|
|
|
return nil
|
|
|
|
|
}
|
2017-02-10 15:17:38 -08:00
|
|
|
f := &File{&file{
|
|
|
|
|
pfd: poll.FD{
|
|
|
|
|
Sysfd: fdi,
|
|
|
|
|
IsStream: true,
|
|
|
|
|
ZeroReadIsEOF: true,
|
|
|
|
|
},
|
2017-09-23 22:00:53 -07:00
|
|
|
name: name,
|
|
|
|
|
stdoutOrErr: fdi == 1 || fdi == 2,
|
2017-02-10 15:17:38 -08:00
|
|
|
}}
|
|
|
|
|
|
2018-06-13 11:56:15 -07:00
|
|
|
pollable := kind == kindOpenFile || kind == kindPipe || kind == kindNonBlock
|
|
|
|
|
|
2018-03-11 19:11:33 +02:00
|
|
|
// If the caller passed a non-blocking filedes (kindNonBlock),
|
|
|
|
|
// we assume they know what they are doing so we allow it to be
|
|
|
|
|
// used with kqueue.
|
2019-01-07 02:06:38 +02:00
|
|
|
if kind == kindOpenFile {
|
|
|
|
|
switch runtime.GOOS {
|
2020-09-16 16:59:58 -04:00
|
|
|
case "darwin", "ios", "dragonfly", "freebsd", "netbsd", "openbsd":
|
2019-04-19 15:41:38 +03:00
|
|
|
var st syscall.Stat_t
|
2020-08-18 16:46:24 -07:00
|
|
|
err := ignoringEINTR(func() error {
|
|
|
|
|
return syscall.Fstat(fdi, &st)
|
|
|
|
|
})
|
2019-04-19 15:41:38 +03:00
|
|
|
typ := st.Mode & syscall.S_IFMT
|
2019-01-09 10:04:58 -08:00
|
|
|
// Don't try to use kqueue with regular files on *BSDs.
|
|
|
|
|
// On FreeBSD a regular file is always
|
|
|
|
|
// reported as ready for writing.
|
|
|
|
|
// On Dragonfly, NetBSD and OpenBSD the fd is signaled
|
|
|
|
|
// only once as ready (both read and write).
|
|
|
|
|
// Issue 19093.
|
2019-04-19 15:41:38 +03:00
|
|
|
// Also don't add directories to the netpoller.
|
|
|
|
|
if err == nil && (typ == syscall.S_IFREG || typ == syscall.S_IFDIR) {
|
2019-01-07 02:06:38 +02:00
|
|
|
pollable = false
|
|
|
|
|
}
|
2019-01-09 10:04:58 -08:00
|
|
|
|
2019-01-07 02:06:38 +02:00
|
|
|
// In addition to the behavior described above for regular files,
|
|
|
|
|
// on Darwin, kqueue does not work properly with fifos:
|
|
|
|
|
// closing the last writer does not cause a kqueue event
|
|
|
|
|
// for any readers. See issue #24164.
|
2020-09-16 16:59:58 -04:00
|
|
|
if (runtime.GOOS == "darwin" || runtime.GOOS == "ios") && typ == syscall.S_IFIFO {
|
2019-01-07 02:06:38 +02:00
|
|
|
pollable = false
|
|
|
|
|
}
|
2018-06-13 11:56:15 -07:00
|
|
|
}
|
2017-02-10 15:17:38 -08:00
|
|
|
}
|
|
|
|
|
|
2017-04-07 15:53:19 -07:00
|
|
|
if err := f.pfd.Init("file", pollable); err != nil {
|
|
|
|
|
// An error here indicates a failure to register
|
|
|
|
|
// with the netpoll system. That can happen for
|
|
|
|
|
// a file descriptor that is not supported by
|
|
|
|
|
// epoll/kqueue; for example, disk files on
|
|
|
|
|
// GNU/Linux systems. We assume that any real error
|
|
|
|
|
// will show up in later I/O.
|
|
|
|
|
} else if pollable {
|
|
|
|
|
// We successfully registered with netpoll, so put
|
|
|
|
|
// the file into nonblocking mode.
|
|
|
|
|
if err := syscall.SetNonblock(fdi, true); err == nil {
|
|
|
|
|
f.nonblock = true
|
2017-02-10 15:17:38 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 12:20:59 -05:00
|
|
|
runtime.SetFinalizer(f.file, (*file).close)
|
2011-07-01 10:18:07 -04:00
|
|
|
return f
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-28 11:29:22 -08: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-07-27 22:21:33 +10:00
|
|
|
func epipecheck(file *File, e error) {
|
2017-09-23 22:00:53 -07:00
|
|
|
if e == syscall.EPIPE && file.stdoutOrErr {
|
2015-12-28 11:29:22 -08:00
|
|
|
sigpipe()
|
2012-07-27 22:21:33 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-03 13:03:50 -07: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"
|
|
|
|
|
|
2017-12-11 16:41:37 -08:00
|
|
|
// openFileNolog is the Unix implementation of OpenFile.
|
2019-01-29 16:34:27 -08:00
|
|
|
// Changes here should be reflected in openFdAt, if relevant.
|
2017-12-11 16:41:37 -08:00
|
|
|
func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
|
2017-12-16 19:06:10 +02:00
|
|
|
setSticky := false
|
2014-12-16 08:22:17 -08:00
|
|
|
if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
|
|
|
|
|
if _, err := Stat(name); IsNotExist(err) {
|
2017-12-16 19:06:10 +02:00
|
|
|
setSticky = true
|
2014-12-16 08:22:17 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-17 16:28:15 +10:00
|
|
|
var r int
|
|
|
|
|
for {
|
|
|
|
|
var e error
|
|
|
|
|
r, e = syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
|
|
|
|
|
if e == nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 14:35:07 -07:00
|
|
|
// We have to check EINTR here, per issues 11180 and 39237.
|
|
|
|
|
if e == syscall.EINTR {
|
2015-09-17 16:28:15 +10:00
|
|
|
continue
|
2015-09-17 08:32:09 +10:00
|
|
|
}
|
|
|
|
|
|
2020-07-03 12:25:49 -04:00
|
|
|
return nil, &PathError{Op: "open", Path: name, Err: e}
|
2010-04-13 16:30:11 -07:00
|
|
|
}
|
|
|
|
|
|
2014-12-16 08:22:17 -08:00
|
|
|
// open(2) itself won't handle the sticky bit on *BSD and Solaris
|
2017-12-16 19:06:10 +02:00
|
|
|
if setSticky {
|
|
|
|
|
setStickyBit(name)
|
2014-12-16 08:22:17 -08:00
|
|
|
}
|
|
|
|
|
|
2010-04-13 16:30:11 -07:00
|
|
|
// There's a race here with fork/exec, which we are
|
2016-03-01 23:21:55 +00:00
|
|
|
// content to live with. See ../syscall/exec_unix.go.
|
2014-03-04 09:27:29 +09:00
|
|
|
if !supportsCloseOnExec {
|
2010-04-13 16:30:11 -07:00
|
|
|
syscall.CloseOnExec(r)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 13:57:34 -07:00
|
|
|
return newFile(uintptr(r), name, kindOpenFile), nil
|
2010-04-13 16:30:11 -07:00
|
|
|
}
|
|
|
|
|
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 12:20:59 -05:00
|
|
|
func (file *file) close() error {
|
2017-04-24 21:49:26 -07:00
|
|
|
if file == nil {
|
2012-02-17 10:04:29 +11:00
|
|
|
return syscall.EINVAL
|
2010-04-13 16:30:11 -07:00
|
|
|
}
|
2018-12-08 16:45:29 +01:00
|
|
|
if file.dirinfo != nil {
|
|
|
|
|
file.dirinfo.close()
|
|
|
|
|
}
|
2011-11-01 21:49:08 -04:00
|
|
|
var err error
|
2017-02-10 15:17:38 -08:00
|
|
|
if e := file.pfd.Close(); e != nil {
|
2017-04-25 17:47:34 -07:00
|
|
|
if e == poll.ErrFileClosing {
|
|
|
|
|
e = ErrClosed
|
|
|
|
|
}
|
2020-07-03 12:25:49 -04:00
|
|
|
err = &PathError{Op: "close", Path: file.name, Err: e}
|
2010-04-13 16:30:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// no need for a finalizer anymore
|
|
|
|
|
runtime.SetFinalizer(file, nil)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-26 18:09:46 +10: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-11-13 22:44:52 -05:00
|
|
|
func (f *File) seek(offset int64, whence int) (ret int64, err error) {
|
2020-02-11 17:49:52 -08:00
|
|
|
if f.dirinfo != nil {
|
|
|
|
|
// Free cached dirinfo, so we allocate a new one if we
|
|
|
|
|
// access this file as a directory again. See #35767 and #37161.
|
|
|
|
|
f.dirinfo.close()
|
|
|
|
|
f.dirinfo = nil
|
|
|
|
|
}
|
2017-02-10 15:17:38 -08:00
|
|
|
ret, err = f.pfd.Seek(offset, whence)
|
|
|
|
|
runtime.KeepAlive(f)
|
|
|
|
|
return ret, err
|
2011-04-26 18:09:46 +10:00
|
|
|
}
|
|
|
|
|
|
2010-04-26 23:17:14 -07: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-02-09 16:55:36 +11:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-11-01 21:49:08 -04:00
|
|
|
func Truncate(name string, size int64) error {
|
2020-08-18 16:46:24 -07:00
|
|
|
e := ignoringEINTR(func() error {
|
|
|
|
|
return syscall.Truncate(name, size)
|
|
|
|
|
})
|
|
|
|
|
if e != nil {
|
2020-07-03 12:25:49 -04:00
|
|
|
return &PathError{Op: "truncate", Path: name, Err: e}
|
2010-04-26 23:17:14 -07:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2011-03-29 14:23:36 -04:00
|
|
|
|
2018-08-03 09:55:44 -07:00
|
|
|
// Remove removes the named file or (empty) directory.
|
2012-02-09 16:55:36 +11:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-12-20 11:52:20 +11: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.
|
2020-08-18 16:46:24 -07:00
|
|
|
e := ignoringEINTR(func() error {
|
|
|
|
|
return syscall.Unlink(name)
|
|
|
|
|
})
|
2011-12-20 11:52:20 +11:00
|
|
|
if e == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-08-18 16:46:24 -07:00
|
|
|
e1 := ignoringEINTR(func() error {
|
|
|
|
|
return syscall.Rmdir(name)
|
|
|
|
|
})
|
2011-12-20 11:52:20 +11:00
|
|
|
if e1 == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Both failed: figure out which error to return.
|
|
|
|
|
// OS X and Linux differ on whether unlink(dir)
|
2016-03-01 23:21:55 +00:00
|
|
|
// returns EISDIR, so can't use that. However,
|
2011-12-20 11:52:20 +11: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
|
|
|
|
|
}
|
2020-07-03 12:25:49 -04:00
|
|
|
return &PathError{Op: "remove", Path: name, Err: e}
|
2011-12-20 11:52:20 +11:00
|
|
|
}
|
|
|
|
|
|
2017-06-14 18:29:49 +00:00
|
|
|
func tempDir() string {
|
2011-11-14 14:06:50 -05:00
|
|
|
dir := Getenv("TMPDIR")
|
|
|
|
|
if dir == "" {
|
2014-07-09 14:12:30 -04:00
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
|
dir = "/data/local/tmp"
|
|
|
|
|
} else {
|
|
|
|
|
dir = "/tmp"
|
|
|
|
|
}
|
2011-11-14 14:06:50 -05:00
|
|
|
}
|
|
|
|
|
return dir
|
|
|
|
|
}
|
2014-07-17 17:02:46 +10: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 {
|
2020-08-18 16:46:24 -07:00
|
|
|
e := ignoringEINTR(func() error {
|
|
|
|
|
return syscall.Link(oldname, newname)
|
|
|
|
|
})
|
2014-07-17 17:02:46 +10:00
|
|
|
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 {
|
2020-08-18 16:46:24 -07:00
|
|
|
e := ignoringEINTR(func() error {
|
|
|
|
|
return syscall.Symlink(oldname, newname)
|
|
|
|
|
})
|
2014-07-17 17:02:46 +10:00
|
|
|
if e != nil {
|
|
|
|
|
return &LinkError{"symlink", oldname, newname, e}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-12-08 16:45:29 +01:00
|
|
|
|
2019-02-28 20:21:32 +11:00
|
|
|
// Readlink returns the destination of the named symbolic link.
|
|
|
|
|
// If there is an error, it will be of type *PathError.
|
|
|
|
|
func Readlink(name string) (string, error) {
|
|
|
|
|
for len := 128; ; len *= 2 {
|
|
|
|
|
b := make([]byte, len)
|
2020-08-18 16:46:24 -07:00
|
|
|
var (
|
|
|
|
|
n int
|
|
|
|
|
e error
|
|
|
|
|
)
|
|
|
|
|
for {
|
|
|
|
|
n, e = fixCount(syscall.Readlink(name, b))
|
|
|
|
|
if e != syscall.EINTR {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-28 20:21:32 +11:00
|
|
|
// buffer too small
|
|
|
|
|
if runtime.GOOS == "aix" && e == syscall.ERANGE {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if e != nil {
|
2020-07-03 12:25:49 -04:00
|
|
|
return "", &PathError{Op: "readlink", Path: name, Err: e}
|
2019-02-28 20:21:32 +11:00
|
|
|
}
|
|
|
|
|
if n < len {
|
|
|
|
|
return string(b[0:n]), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-09 11:49:59 -04:00
|
|
|
|
|
|
|
|
type unixDirent struct {
|
|
|
|
|
parent string
|
|
|
|
|
name string
|
|
|
|
|
typ FileMode
|
|
|
|
|
info FileInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *unixDirent) Name() string { return d.name }
|
|
|
|
|
func (d *unixDirent) IsDir() bool { return d.typ.IsDir() }
|
|
|
|
|
func (d *unixDirent) Type() FileMode { return d.typ }
|
|
|
|
|
|
|
|
|
|
func (d *unixDirent) Info() (FileInfo, error) {
|
|
|
|
|
if d.info != nil {
|
|
|
|
|
return d.info, nil
|
|
|
|
|
}
|
|
|
|
|
return lstat(d.parent + "/" + d.name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newUnixDirent(parent, name string, typ FileMode) (DirEntry, error) {
|
|
|
|
|
ude := &unixDirent{
|
|
|
|
|
parent: parent,
|
|
|
|
|
name: name,
|
|
|
|
|
typ: typ,
|
|
|
|
|
}
|
|
|
|
|
if typ != ^FileMode(0) && !testingForceReadDirLstat {
|
|
|
|
|
return ude, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info, err := lstat(parent + "/" + name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ude.typ = info.Mode().Type()
|
|
|
|
|
ude.info = info
|
|
|
|
|
return ude, nil
|
|
|
|
|
}
|