2011-04-02 15:28:58 -06:00
|
|
|
// Copyright 2011 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.
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
2017-10-17 14:57:34 -06:00
|
|
|
"internal/poll"
|
2016-04-05 12:22:53 -06:00
|
|
|
"io"
|
2011-04-02 15:28:58 -06:00
|
|
|
"runtime"
|
|
|
|
"syscall"
|
2012-01-25 01:15:44 -07:00
|
|
|
"time"
|
2011-04-02 15:28:58 -06:00
|
|
|
)
|
|
|
|
|
2016-10-28 11:01:51 -06:00
|
|
|
// fixLongPath is a noop on non-Windows platforms.
|
|
|
|
func fixLongPath(path string) string {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
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 10:20:59 -07: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 {
|
2019-03-11 00:58:20 -06:00
|
|
|
fd int
|
|
|
|
name string
|
|
|
|
dirinfo *dirInfo // nil unless directory being read
|
|
|
|
appendMode bool // whether file is opened for appending
|
2011-07-05 00:01:29 -06:00
|
|
|
}
|
|
|
|
|
2014-11-06 07:36:51 -07:00
|
|
|
// Fd returns the integer Plan 9 file descriptor referencing the open file.
|
2020-09-24 00:57:00 -06: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 01:46:42 -06:00
|
|
|
// As an alternative, see the f.SyscallConn method.
|
2012-02-16 12:04:51 -07:00
|
|
|
func (f *File) Fd() uintptr {
|
|
|
|
if f == nil {
|
|
|
|
return ^(uintptr(0))
|
2011-07-05 00:01:29 -06:00
|
|
|
}
|
2012-02-16 12:04:51 -07:00
|
|
|
return uintptr(f.fd)
|
2011-07-05 00:01:29 -06:00
|
|
|
}
|
|
|
|
|
2017-04-20 03:13:55 -06: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
|
|
|
|
// descriptor.
|
2012-02-16 12:04:51 -07:00
|
|
|
func NewFile(fd uintptr, name string) *File {
|
|
|
|
fdi := int(fd)
|
|
|
|
if fdi < 0 {
|
2011-07-05 00:01:29 -06:00
|
|
|
return nil
|
|
|
|
}
|
2012-02-16 12:04:51 -07:00
|
|
|
f := &File{&file{fd: fdi, name: name}}
|
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 10:20:59 -07:00
|
|
|
runtime.SetFinalizer(f.file, (*file).close)
|
2011-07-05 00:01:29 -06:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2011-04-12 17:58:56 -06:00
|
|
|
// Auxiliary information if the File describes a directory
|
|
|
|
type dirInfo struct {
|
|
|
|
buf [syscall.STATMAX]byte // buffer for directory I/O
|
|
|
|
nbuf int // length of buf; return value from Read
|
|
|
|
bufp int // location of next record in buf.
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func epipecheck(file *File, e error) {
|
2011-04-02 15:28:58 -06: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"
|
|
|
|
|
2012-01-20 21:01:29 -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&ModeAppend != 0 {
|
|
|
|
o |= syscall.DMAPPEND
|
|
|
|
}
|
|
|
|
if i&ModeExclusive != 0 {
|
|
|
|
o |= syscall.DMEXCL
|
|
|
|
}
|
|
|
|
if i&ModeTemporary != 0 {
|
|
|
|
o |= syscall.DMTMP
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-11 17:41:37 -07:00
|
|
|
// openFileNolog is the Plan 9 implementation of OpenFile.
|
|
|
|
func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
|
2011-06-14 09:20:34 -06:00
|
|
|
var (
|
|
|
|
fd int
|
2011-11-16 15:37:54 -07:00
|
|
|
e error
|
2011-06-14 09:20:34 -06:00
|
|
|
create bool
|
|
|
|
excl bool
|
|
|
|
trunc bool
|
|
|
|
append bool
|
|
|
|
)
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2011-04-05 00:42:14 -06:00
|
|
|
if flag&O_CREATE == O_CREATE {
|
2011-06-14 09:20:34 -06:00
|
|
|
flag = flag & ^O_CREATE
|
|
|
|
create = true
|
|
|
|
}
|
|
|
|
if flag&O_EXCL == O_EXCL {
|
|
|
|
excl = true
|
|
|
|
}
|
|
|
|
if flag&O_TRUNC == O_TRUNC {
|
|
|
|
trunc = true
|
|
|
|
}
|
|
|
|
// O_APPEND is emulated on Plan 9
|
|
|
|
if flag&O_APPEND == O_APPEND {
|
|
|
|
flag = flag &^ O_APPEND
|
|
|
|
append = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (create && trunc) || excl {
|
2012-01-20 21:01:29 -07:00
|
|
|
fd, e = syscall.Create(name, flag, syscallMode(perm))
|
2011-04-02 15:28:58 -06:00
|
|
|
} else {
|
|
|
|
fd, e = syscall.Open(name, flag)
|
2019-11-09 14:02:46 -07:00
|
|
|
if IsNotExist(e) && create {
|
2020-04-21 04:00:53 -06:00
|
|
|
fd, e = syscall.Create(name, flag, syscallMode(perm))
|
|
|
|
if e != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return nil, &PathError{Op: "create", Path: name, Err: e}
|
2011-06-14 09:20:34 -06:00
|
|
|
}
|
|
|
|
}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if e != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return nil, &PathError{Op: "open", Path: name, Err: e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
2011-06-14 09:20:34 -06:00
|
|
|
if append {
|
2016-04-05 12:22:53 -06:00
|
|
|
if _, e = syscall.Seek(fd, 0, io.SeekEnd); e != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return nil, &PathError{Op: "seek", Path: name, Err: e}
|
2011-06-14 09:20:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-16 12:04:51 -07:00
|
|
|
return NewFile(uintptr(fd), name), nil
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the File, rendering it unusable for I/O.
|
2018-07-10 07:47:44 -06:00
|
|
|
// On files that support SetDeadline, any pending I/O operations will
|
|
|
|
// be canceled and return immediately with an error.
|
2019-06-04 12:07:57 -06:00
|
|
|
// Close will return an error if it has already been called.
|
2012-05-01 23:44:38 -06:00
|
|
|
func (f *File) Close() error {
|
2016-10-06 22:46:56 -06:00
|
|
|
if err := f.checkValid("close"); err != nil {
|
|
|
|
return err
|
2013-08-19 22:33:03 -06:00
|
|
|
}
|
2012-05-01 23:44:38 -06:00
|
|
|
return f.file.close()
|
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 10:20:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (file *file) close() error {
|
2016-10-06 22:46:56 -06:00
|
|
|
if file == nil || file.fd == badFd {
|
2012-02-16 16:04:29 -07:00
|
|
|
return ErrInvalid
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2011-11-01 19:49:08 -06:00
|
|
|
var err error
|
2011-04-02 15:28:58 -06:00
|
|
|
if e := syscall.Close(file.fd); e != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
err = &PathError{Op: "close", Path: file.name, Err: e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2016-10-06 22:46:56 -06:00
|
|
|
file.fd = badFd // so it can't be closed again
|
2011-04-02 15:28:58 -06:00
|
|
|
|
|
|
|
// no need for a finalizer anymore
|
|
|
|
runtime.SetFinalizer(file, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns the FileInfo structure describing file.
|
2012-05-01 23:44:38 -06:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2015-07-17 09:26:29 -06:00
|
|
|
func (f *File) Stat() (FileInfo, error) {
|
2013-08-19 22:33:03 -06:00
|
|
|
if f == nil {
|
|
|
|
return nil, ErrInvalid
|
|
|
|
}
|
2011-06-14 09:20:34 -06:00
|
|
|
d, err := dirstat(f)
|
2011-11-13 20:44:52 -07:00
|
|
|
if err != nil {
|
2011-06-14 09:20:34 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2011-12-12 14:14:00 -07:00
|
|
|
return fileInfoFromStat(d), nil
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate changes the size of the file.
|
|
|
|
// It does not change the I/O offset.
|
2012-05-01 23:44:38 -06: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 {
|
2013-08-19 22:33:03 -06:00
|
|
|
if f == nil {
|
|
|
|
return ErrInvalid
|
|
|
|
}
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2013-08-19 22:33:03 -06:00
|
|
|
var d syscall.Dir
|
2012-11-26 16:26:46 -07:00
|
|
|
d.Null()
|
|
|
|
d.Length = size
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2012-11-26 16:26:46 -07:00
|
|
|
var buf [syscall.STATFIXLEN]byte
|
|
|
|
n, err := d.Marshal(buf[:])
|
|
|
|
if err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "truncate", Path: f.name, Err: err}
|
2012-11-26 16:26:46 -07:00
|
|
|
}
|
|
|
|
if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "truncate", Path: f.name, Err: err}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-01-20 21:01:29 -07:00
|
|
|
const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | ModePerm)
|
|
|
|
|
2017-06-29 16:47:29 -06:00
|
|
|
func (f *File) chmod(mode FileMode) error {
|
2013-08-19 22:33:03 -06:00
|
|
|
if f == nil {
|
|
|
|
return ErrInvalid
|
|
|
|
}
|
2012-11-26 16:26:46 -07:00
|
|
|
var d syscall.Dir
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2011-06-14 09:20:34 -06:00
|
|
|
odir, e := dirstat(f)
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "chmod", Path: f.name, Err: e}
|
2011-06-14 09:20:34 -06:00
|
|
|
}
|
2012-01-20 21:01:29 -07:00
|
|
|
d.Null()
|
|
|
|
d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
|
2012-11-26 16:26:46 -07:00
|
|
|
|
|
|
|
var buf [syscall.STATFIXLEN]byte
|
|
|
|
n, err := d.Marshal(buf[:])
|
|
|
|
if err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "chmod", Path: f.name, Err: err}
|
2012-11-26 16:26:46 -07:00
|
|
|
}
|
|
|
|
if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "chmod", Path: f.name, Err: err}
|
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 {
|
2011-04-02 15:28:58 -06:00
|
|
|
if f == nil {
|
2012-02-16 16:04:29 -07:00
|
|
|
return ErrInvalid
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2012-11-26 16:26:46 -07:00
|
|
|
var d syscall.Dir
|
2011-04-02 15:28:58 -06:00
|
|
|
d.Null()
|
|
|
|
|
2012-11-26 16:26:46 -07:00
|
|
|
var buf [syscall.STATFIXLEN]byte
|
|
|
|
n, err := d.Marshal(buf[:])
|
|
|
|
if err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "sync", Path: f.name, Err: err}
|
2012-11-26 16:26:46 -07:00
|
|
|
}
|
|
|
|
if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "sync", Path: f.name, Err: err}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-04-26 02:09:46 -06:00
|
|
|
// read reads up to len(b) bytes from the File.
|
|
|
|
// It returns the number of bytes read and an error, if any.
|
2011-11-16 15:37:54 -07:00
|
|
|
func (f *File) read(b []byte) (n int, err error) {
|
2017-02-10 16:17:38 -07:00
|
|
|
n, e := fixCount(syscall.Read(f.fd, b))
|
|
|
|
if n == 0 && len(b) > 0 && e == nil {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
return n, e
|
2011-04-26 02:09:46 -06: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.
|
|
|
|
// EOF is signaled by a zero count with err set to nil.
|
2011-11-16 15:37:54 -07:00
|
|
|
func (f *File) pread(b []byte, off int64) (n int, err error) {
|
2017-02-10 16:17:38 -07:00
|
|
|
n, e := fixCount(syscall.Pread(f.fd, b, off))
|
|
|
|
if n == 0 && len(b) > 0 && e == nil {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
return n, e
|
2011-04-26 02:09:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// write writes len(b) bytes to the File.
|
|
|
|
// It returns the number of bytes written and an error, if any.
|
2013-02-22 15:06:25 -07:00
|
|
|
// Since Plan 9 preserves message boundaries, never allow
|
|
|
|
// a zero-byte write.
|
2011-11-16 15:37:54 -07:00
|
|
|
func (f *File) write(b []byte) (n int, err error) {
|
2014-10-28 15:44:59 -06:00
|
|
|
if len(b) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2014-10-28 13:00:13 -06:00
|
|
|
return fixCount(syscall.Write(f.fd, b))
|
2011-04-26 02:09:46 -06: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.
|
2013-02-22 15:06:25 -07:00
|
|
|
// Since Plan 9 preserves message boundaries, never allow
|
|
|
|
// a zero-byte write.
|
2011-11-16 15:37:54 -07:00
|
|
|
func (f *File) pwrite(b []byte, off int64) (n int, err error) {
|
2013-02-22 15:06:25 -07:00
|
|
|
if len(b) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2014-10-28 13:00:13 -06:00
|
|
|
return fixCount(syscall.Pwrite(f.fd, b, off))
|
2011-04-26 02:09:46 -06: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-16 15:37:54 -07:00
|
|
|
func (f *File) seek(offset int64, whence int) (ret int64, err error) {
|
2020-03-01 18:40:35 -07: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 = nil
|
|
|
|
}
|
2011-04-26 02:09:46 -06:00
|
|
|
return syscall.Seek(f.fd, offset, whence)
|
|
|
|
}
|
|
|
|
|
2011-04-02 15:28:58 -06: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-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 Truncate(name string, size int64) error {
|
2012-11-26 16:26:46 -07:00
|
|
|
var d syscall.Dir
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2012-11-26 16:26:46 -07:00
|
|
|
d.Null()
|
|
|
|
d.Length = size
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2012-11-26 16:26:46 -07:00
|
|
|
var buf [syscall.STATFIXLEN]byte
|
|
|
|
n, err := d.Marshal(buf[:])
|
|
|
|
if err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "truncate", Path: name, Err: err}
|
2012-11-26 16:26:46 -07:00
|
|
|
}
|
|
|
|
if err = syscall.Wstat(name, buf[:n]); err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "truncate", Path: name, Err: err}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the named file or directory.
|
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 Remove(name string) error {
|
2011-11-13 20:44:52 -07:00
|
|
|
if e := syscall.Remove(name); e != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "remove", Path: name, Err: e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-20 17:22:10 -07:00
|
|
|
// HasPrefix from the strings package.
|
|
|
|
func hasPrefix(s, prefix string) bool {
|
|
|
|
return len(s) >= len(prefix) && s[0:len(prefix)] == prefix
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:16:23 -06:00
|
|
|
// LastIndexByte from the strings package.
|
2013-12-20 17:22:10 -07:00
|
|
|
func lastIndex(s string, sep byte) int {
|
|
|
|
for i := len(s) - 1; i >= 0; i-- {
|
|
|
|
if s[i] == sep {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2013-12-09 21:25:13 -07:00
|
|
|
func rename(oldname, newname string) error {
|
2013-12-20 17:22:10 -07:00
|
|
|
dirname := oldname[:lastIndex(oldname, '/')+1]
|
|
|
|
if hasPrefix(newname, dirname) {
|
2013-12-19 13:20:03 -07:00
|
|
|
newname = newname[len(dirname):]
|
2014-02-19 23:59:38 -07:00
|
|
|
} else {
|
|
|
|
return &LinkError{"rename", oldname, newname, ErrInvalid}
|
2013-12-19 13:20:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If newname still contains slashes after removing the oldname
|
|
|
|
// prefix, the rename is cross-directory and must be rejected.
|
2016-01-06 23:45:59 -07:00
|
|
|
if lastIndex(newname, '/') >= 0 {
|
|
|
|
return &LinkError{"rename", oldname, newname, ErrInvalid}
|
|
|
|
}
|
2013-12-19 13:20:03 -07:00
|
|
|
|
2012-11-26 16:26:46 -07:00
|
|
|
var d syscall.Dir
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2012-11-26 16:26:46 -07:00
|
|
|
d.Null()
|
2011-04-02 15:28:58 -06:00
|
|
|
d.Name = newname
|
|
|
|
|
2013-02-28 15:20:42 -07:00
|
|
|
buf := make([]byte, syscall.STATFIXLEN+len(d.Name))
|
2012-11-26 16:26:46 -07:00
|
|
|
n, err := d.Marshal(buf[:])
|
|
|
|
if err != nil {
|
2013-12-19 13:20:03 -07:00
|
|
|
return &LinkError{"rename", oldname, newname, err}
|
2012-11-26 16:26:46 -07:00
|
|
|
}
|
2016-01-06 23:45:59 -07:00
|
|
|
|
|
|
|
// If newname already exists and is not a directory, rename replaces it.
|
|
|
|
f, err := Stat(dirname + newname)
|
|
|
|
if err == nil && !f.IsDir() {
|
|
|
|
Remove(dirname + newname)
|
|
|
|
}
|
|
|
|
|
2012-11-26 16:26:46 -07:00
|
|
|
if err = syscall.Wstat(oldname, buf[:n]); err != nil {
|
2013-12-19 13:20:03 -07:00
|
|
|
return &LinkError{"rename", oldname, newname, err}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:47:29 -06:00
|
|
|
// See docs in file.go:Chmod.
|
|
|
|
func chmod(name string, mode FileMode) error {
|
2012-11-26 16:26:46 -07:00
|
|
|
var d syscall.Dir
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2011-06-14 09:20:34 -06:00
|
|
|
odir, e := dirstat(name)
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "chmod", Path: name, Err: e}
|
2011-06-14 09:20:34 -06:00
|
|
|
}
|
2012-01-20 21:01:29 -07:00
|
|
|
d.Null()
|
|
|
|
d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
|
2012-11-26 16:26:46 -07:00
|
|
|
|
|
|
|
var buf [syscall.STATFIXLEN]byte
|
|
|
|
n, err := d.Marshal(buf[:])
|
|
|
|
if err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "chmod", Path: name, Err: err}
|
2012-11-26 16:26:46 -07:00
|
|
|
}
|
|
|
|
if err = syscall.Wstat(name, buf[:n]); err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "chmod", Path: name, Err: err}
|
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.
|
|
|
|
//
|
2012-01-25 01:15:44 -07:00
|
|
|
// The underlying filesystem may truncate or round the values to a
|
|
|
|
// less precise time unit.
|
2012-05-01 23:44:38 -06:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2012-01-25 01:15:44 -07:00
|
|
|
func Chtimes(name string, atime time.Time, mtime time.Time) error {
|
2012-11-26 16:26:46 -07:00
|
|
|
var d syscall.Dir
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2012-11-26 16:26:46 -07:00
|
|
|
d.Null()
|
2012-01-25 01:15:44 -07:00
|
|
|
d.Atime = uint32(atime.Unix())
|
|
|
|
d.Mtime = uint32(mtime.Unix())
|
2011-04-02 15:28:58 -06:00
|
|
|
|
2012-11-26 16:26:46 -07:00
|
|
|
var buf [syscall.STATFIXLEN]byte
|
|
|
|
n, err := d.Marshal(buf[:])
|
|
|
|
if err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "chtimes", Path: name, Err: err}
|
2012-11-26 16:26:46 -07:00
|
|
|
}
|
|
|
|
if err = syscall.Wstat(name, buf[:n]); err != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "chtimes", Path: name, Err: err}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-05-01 23:44:38 -06:00
|
|
|
// Pipe returns a connected pair of Files; reads from r return bytes
|
|
|
|
// written to w. It returns the files and an error, if any.
|
2011-11-01 19:49:08 -06:00
|
|
|
func Pipe() (r *File, w *File, err error) {
|
2011-04-02 15:28:58 -06:00
|
|
|
var p [2]int
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
if e := syscall.Pipe(p[0:]); e != nil {
|
2011-04-02 15:28:58 -06:00
|
|
|
return nil, nil, NewSyscallError("pipe", e)
|
|
|
|
}
|
|
|
|
|
2012-02-16 12:04:51 -07:00
|
|
|
return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// not supported on Plan 9
|
|
|
|
|
2012-05-01 23:44:38 -06:00
|
|
|
// Link creates newname as a hard link to the oldname file.
|
2012-02-18 05:45:43 -07:00
|
|
|
// If there is an error, it will be of type *LinkError.
|
2011-11-01 19:49:08 -06:00
|
|
|
func Link(oldname, newname string) error {
|
2012-05-01 23:44:38 -06:00
|
|
|
return &LinkError{"link", oldname, newname, syscall.EPLAN9}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
2012-02-18 05:45:43 -07:00
|
|
|
// Symlink creates newname as a symbolic link to oldname.
|
|
|
|
// If there is an error, it will be of type *LinkError.
|
2011-11-01 19:49:08 -06:00
|
|
|
func Symlink(oldname, newname string) error {
|
2012-05-01 23:44:38 -06:00
|
|
|
return &LinkError{"symlink", oldname, newname, syscall.EPLAN9}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
2012-05-01 23:44:38 -06:00
|
|
|
// Readlink returns the destination of the named symbolic link.
|
|
|
|
// 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) {
|
2020-07-03 10:25:49 -06:00
|
|
|
return "", &PathError{Op: "readlink", Path: name, Err: syscall.EPLAN9}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
2012-05-01 23:44:38 -06:00
|
|
|
// 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.
|
2018-04-11 16:51:17 -06:00
|
|
|
// A uid or gid of -1 means to not change that value.
|
2012-05-01 23:44:38 -06:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2018-04-11 16:51:17 -06:00
|
|
|
//
|
|
|
|
// On Windows or Plan 9, Chown always returns the syscall.EWINDOWS or
|
|
|
|
// EPLAN9 error, wrapped in *PathError.
|
2011-11-01 19:49:08 -06:00
|
|
|
func Chown(name string, uid, gid int) error {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "chown", Path: name, Err: syscall.EPLAN9}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
2012-05-01 23:44:38 -06:00
|
|
|
// 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.
|
|
|
|
// 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 {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "lchown", Path: name, Err: syscall.EPLAN9}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
2012-05-01 23:44:38 -06:00
|
|
|
// Chown changes the numeric uid and gid of the named file.
|
|
|
|
// 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 {
|
2013-08-19 22:33:03 -06:00
|
|
|
if f == nil {
|
|
|
|
return ErrInvalid
|
|
|
|
}
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "chown", Path: f.name, Err: syscall.EPLAN9}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2011-11-14 12:06:50 -07:00
|
|
|
|
2017-06-14 12:29:49 -06:00
|
|
|
func tempDir() string {
|
2018-08-17 16:05:46 -06:00
|
|
|
dir := Getenv("TMPDIR")
|
|
|
|
if dir == "" {
|
|
|
|
dir = "/tmp"
|
|
|
|
}
|
|
|
|
return dir
|
|
|
|
|
2011-11-14 12:06:50 -07:00
|
|
|
}
|
2017-02-10 16:17:38 -07:00
|
|
|
|
|
|
|
// Chdir changes the current working directory to the file,
|
|
|
|
// which must be a directory.
|
|
|
|
// If there is an error, it will be of type *PathError.
|
|
|
|
func (f *File) Chdir() error {
|
|
|
|
if err := f.checkValid("chdir"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if e := syscall.Fchdir(f.fd); e != nil {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: "chdir", Path: f.name, Err: e}
|
2017-02-10 16:17:38 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:57:34 -06:00
|
|
|
// setDeadline sets the read and write deadline.
|
|
|
|
func (f *File) setDeadline(time.Time) error {
|
|
|
|
if err := f.checkValid("SetDeadline"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return poll.ErrNoDeadline
|
|
|
|
}
|
|
|
|
|
|
|
|
// setReadDeadline sets the read deadline.
|
|
|
|
func (f *File) setReadDeadline(time.Time) error {
|
|
|
|
if err := f.checkValid("SetReadDeadline"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return poll.ErrNoDeadline
|
|
|
|
}
|
|
|
|
|
|
|
|
// setWriteDeadline sets the write deadline.
|
|
|
|
func (f *File) setWriteDeadline(time.Time) error {
|
|
|
|
if err := f.checkValid("SetWriteDeadline"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return poll.ErrNoDeadline
|
|
|
|
}
|
|
|
|
|
2017-02-10 16:17:38 -07:00
|
|
|
// checkValid checks whether f is valid for use.
|
|
|
|
// If not, it returns an appropriate error, perhaps incorporating the operation name op.
|
|
|
|
func (f *File) checkValid(op string) error {
|
|
|
|
if f == nil {
|
|
|
|
return ErrInvalid
|
|
|
|
}
|
|
|
|
if f.fd == badFd {
|
2020-07-03 10:25:49 -06:00
|
|
|
return &PathError{Op: op, Path: f.name, Err: ErrClosed}
|
2017-02-10 16:17:38 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-20 16:09:41 -07:00
|
|
|
|
|
|
|
type rawConn struct{}
|
|
|
|
|
|
|
|
func (c *rawConn) Control(f func(uintptr)) error {
|
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConn) Read(f func(uintptr) bool) error {
|
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConn) Write(f func(uintptr) bool) error {
|
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRawConn(file *File) (*rawConn, error) {
|
|
|
|
return nil, syscall.EPLAN9
|
|
|
|
}
|
2020-08-18 17:46:24 -06:00
|
|
|
|
|
|
|
func ignoringEINTR(fn func() error) error {
|
|
|
|
return fn()
|
|
|
|
}
|