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 (
|
|
|
|
"runtime"
|
2013-12-19 13:20:03 -07:00
|
|
|
"strings"
|
2011-04-02 15:28:58 -06:00
|
|
|
"syscall"
|
2012-01-25 01:15:44 -07:00
|
|
|
"time"
|
2011-04-02 15:28:58 -06:00
|
|
|
)
|
|
|
|
|
2011-07-05 00:01:29 -06:00
|
|
|
// File represents an open file descriptor.
|
|
|
|
type File struct {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-07-05 00:01:29 -06:00
|
|
|
fd int
|
|
|
|
name string
|
|
|
|
dirinfo *dirInfo // nil unless directory being read
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fd returns the integer Unix file descriptor referencing the open file.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// NewFile returns a new File with the given file descriptor and name.
|
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
|
|
|
|
}
|
|
|
|
|
2011-04-05 00:42:14 -06:00
|
|
|
// OpenFile is the generalized open call; most users will use Open
|
|
|
|
// or Create instead. It opens the named file with specified flag
|
|
|
|
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
|
|
|
|
// methods on the returned File can be used for I/O.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2012-01-20 21:01:29 -07:00
|
|
|
func OpenFile(name string, flag int, perm FileMode) (file *File, err 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)
|
2011-06-14 09:20:34 -06:00
|
|
|
if e != nil && create {
|
2011-11-16 15:37:54 -07:00
|
|
|
var e1 error
|
2012-01-20 21:01:29 -07:00
|
|
|
fd, e1 = syscall.Create(name, flag, syscallMode(perm))
|
2011-06-14 09:20:34 -06:00
|
|
|
if e1 == nil {
|
|
|
|
e = nil
|
|
|
|
}
|
|
|
|
}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
return nil, &PathError{"open", name, e}
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:20:34 -06:00
|
|
|
if append {
|
|
|
|
if _, e = syscall.Seek(fd, 0, SEEK_END); e != nil {
|
|
|
|
return nil, &PathError{"seek", name, e}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2011-11-01 19:49:08 -06:00
|
|
|
// It returns an error, if any.
|
2012-05-01 23:44:38 -06:00
|
|
|
func (f *File) Close() error {
|
2013-08-19 22:33:03 -06:00
|
|
|
if f == nil {
|
|
|
|
return ErrInvalid
|
|
|
|
}
|
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 {
|
2011-04-02 15:28:58 -06:00
|
|
|
if file == nil || file.fd < 0 {
|
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
|
|
|
syscall.ForkLock.RLock()
|
|
|
|
if e := syscall.Close(file.fd); e != nil {
|
|
|
|
err = &PathError{"close", file.name, e}
|
|
|
|
}
|
|
|
|
syscall.ForkLock.RUnlock()
|
|
|
|
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-05-01 23:44:38 -06:00
|
|
|
// If there is an error, it will be of type *PathError.
|
|
|
|
func (f *File) Stat() (fi FileInfo, err 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 {
|
|
|
|
return &PathError{"truncate", f.name, err}
|
|
|
|
}
|
|
|
|
if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
|
|
|
|
return &PathError{"truncate", f.name, 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)
|
|
|
|
|
2011-04-02 15:28:58 -06:00
|
|
|
// Chmod changes the mode of the file to mode.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2012-01-20 21:01:29 -07: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 {
|
2011-06-14 09:20:34 -06:00
|
|
|
return &PathError{"chmod", f.name, e}
|
|
|
|
}
|
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 {
|
|
|
|
return &PathError{"chmod", f.name, err}
|
|
|
|
}
|
|
|
|
if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
|
|
|
|
return &PathError{"chmod", f.name, 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.
|
2011-11-01 19:49:08 -06:00
|
|
|
func (f *File) Sync() (err 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 {
|
|
|
|
return NewSyscallError("fsync", err)
|
|
|
|
}
|
|
|
|
if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
|
|
|
|
return NewSyscallError("fsync", 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) {
|
2011-04-26 02:09:46 -06:00
|
|
|
return syscall.Read(f.fd, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2011-04-26 02:09:46 -06:00
|
|
|
return syscall.Pread(f.fd, b, off)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2013-02-22 15:06:25 -07:00
|
|
|
if len(b) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2011-04-26 02:09:46 -06:00
|
|
|
return syscall.Write(f.fd, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2011-04-26 02:09:46 -06:00
|
|
|
return syscall.Pwrite(f.fd, b, off)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
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 {
|
|
|
|
return &PathError{"truncate", name, err}
|
|
|
|
}
|
|
|
|
if err = syscall.Wstat(name, buf[:n]); err != nil {
|
|
|
|
return &PathError{"truncate", name, 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 {
|
2011-04-02 15:28:58 -06:00
|
|
|
return &PathError{"remove", name, e}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-09 21:25:13 -07:00
|
|
|
func rename(oldname, newname string) error {
|
2013-12-19 13:20:03 -07:00
|
|
|
dirname := oldname[:strings.LastIndex(oldname, "/")+1]
|
|
|
|
if strings.HasPrefix(newname, dirname) {
|
|
|
|
newname = newname[len(dirname):]
|
|
|
|
}
|
|
|
|
|
|
|
|
// If newname still contains slashes after removing the oldname
|
|
|
|
// prefix, the rename is cross-directory and must be rejected.
|
|
|
|
// This case is caught by d.Marshal below.
|
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod changes the mode of the named file to mode.
|
2012-05-01 23:44:38 -06:00
|
|
|
// If the file is a symbolic link, it changes the mode of the link's target.
|
2012-02-08 22:55:36 -07:00
|
|
|
// If there is an error, it will be of type *PathError.
|
2012-01-20 21:01:29 -07:00
|
|
|
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 {
|
2011-06-14 09:20:34 -06:00
|
|
|
return &PathError{"chmod", name, e}
|
|
|
|
}
|
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 {
|
|
|
|
return &PathError{"chmod", name, err}
|
|
|
|
}
|
|
|
|
if err = syscall.Wstat(name, buf[:n]); err != nil {
|
|
|
|
return &PathError{"chmod", name, 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 {
|
|
|
|
return &PathError{"chtimes", name, err}
|
|
|
|
}
|
|
|
|
if err = syscall.Wstat(name, buf[:n]); err != nil {
|
|
|
|
return &PathError{"chtimes", name, 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
|
|
|
|
|
|
|
|
syscall.ForkLock.RLock()
|
2011-11-13 20:44:52 -07:00
|
|
|
if e := syscall.Pipe(p[0:]); e != nil {
|
2011-04-02 15:28:58 -06:00
|
|
|
syscall.ForkLock.RUnlock()
|
|
|
|
return nil, nil, NewSyscallError("pipe", e)
|
|
|
|
}
|
|
|
|
syscall.ForkLock.RUnlock()
|
|
|
|
|
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) {
|
2012-05-01 23:44:38 -06:00
|
|
|
return "", &PathError{"readlink", name, 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.
|
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-11-01 19:49:08 -06:00
|
|
|
func Chown(name string, uid, gid int) error {
|
2012-05-01 23:44:38 -06:00
|
|
|
return &PathError{"chown", name, 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 {
|
2012-05-01 23:44:38 -06:00
|
|
|
return &PathError{"lchown", name, 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
|
|
|
|
}
|
2012-05-01 23:44:38 -06:00
|
|
|
return &PathError{"chown", f.name, syscall.EPLAN9}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2011-11-14 12:06:50 -07:00
|
|
|
|
|
|
|
// TempDir returns the default directory to use for temporary files.
|
|
|
|
func TempDir() string {
|
|
|
|
return "/tmp"
|
|
|
|
}
|