github.com/karrick/go@v0.0.0-20170817181416-d5b0ec858b37/src/os/file_plan9.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package os
     6  
     7  import (
     8  	"io"
     9  	"runtime"
    10  	"syscall"
    11  	"time"
    12  )
    13  
    14  // fixLongPath is a noop on non-Windows platforms.
    15  func fixLongPath(path string) string {
    16  	return path
    17  }
    18  
    19  // file is the real representation of *File.
    20  // The extra level of indirection ensures that no clients of os
    21  // can overwrite this data, which could cause the finalizer
    22  // to close the wrong file descriptor.
    23  type file struct {
    24  	fd      int
    25  	name    string
    26  	dirinfo *dirInfo // nil unless directory being read
    27  }
    28  
    29  // Fd returns the integer Plan 9 file descriptor referencing the open file.
    30  // The file descriptor is valid only until f.Close is called or f is garbage collected.
    31  func (f *File) Fd() uintptr {
    32  	if f == nil {
    33  		return ^(uintptr(0))
    34  	}
    35  	return uintptr(f.fd)
    36  }
    37  
    38  // NewFile returns a new File with the given file descriptor and
    39  // name. The returned value will be nil if fd is not a valid file
    40  // descriptor.
    41  func NewFile(fd uintptr, name string) *File {
    42  	fdi := int(fd)
    43  	if fdi < 0 {
    44  		return nil
    45  	}
    46  	f := &File{&file{fd: fdi, name: name}}
    47  	runtime.SetFinalizer(f.file, (*file).close)
    48  	return f
    49  }
    50  
    51  // Auxiliary information if the File describes a directory
    52  type dirInfo struct {
    53  	buf  [syscall.STATMAX]byte // buffer for directory I/O
    54  	nbuf int                   // length of buf; return value from Read
    55  	bufp int                   // location of next record in buf.
    56  }
    57  
    58  func epipecheck(file *File, e error) {
    59  }
    60  
    61  // DevNull is the name of the operating system's ``null device.''
    62  // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
    63  const DevNull = "/dev/null"
    64  
    65  // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
    66  func syscallMode(i FileMode) (o uint32) {
    67  	o |= uint32(i.Perm())
    68  	if i&ModeAppend != 0 {
    69  		o |= syscall.DMAPPEND
    70  	}
    71  	if i&ModeExclusive != 0 {
    72  		o |= syscall.DMEXCL
    73  	}
    74  	if i&ModeTemporary != 0 {
    75  		o |= syscall.DMTMP
    76  	}
    77  	return
    78  }
    79  
    80  // OpenFile is the generalized open call; most users will use Open
    81  // or Create instead. It opens the named file with specified flag
    82  // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
    83  // methods on the returned File can be used for I/O.
    84  // If there is an error, it will be of type *PathError.
    85  func OpenFile(name string, flag int, perm FileMode) (*File, error) {
    86  	var (
    87  		fd     int
    88  		e      error
    89  		create bool
    90  		excl   bool
    91  		trunc  bool
    92  		append bool
    93  	)
    94  
    95  	if flag&O_CREATE == O_CREATE {
    96  		flag = flag & ^O_CREATE
    97  		create = true
    98  	}
    99  	if flag&O_EXCL == O_EXCL {
   100  		excl = true
   101  	}
   102  	if flag&O_TRUNC == O_TRUNC {
   103  		trunc = true
   104  	}
   105  	// O_APPEND is emulated on Plan 9
   106  	if flag&O_APPEND == O_APPEND {
   107  		flag = flag &^ O_APPEND
   108  		append = true
   109  	}
   110  
   111  	if (create && trunc) || excl {
   112  		fd, e = syscall.Create(name, flag, syscallMode(perm))
   113  	} else {
   114  		fd, e = syscall.Open(name, flag)
   115  		if e != nil && create {
   116  			var e1 error
   117  			fd, e1 = syscall.Create(name, flag, syscallMode(perm))
   118  			if e1 == nil {
   119  				e = nil
   120  			}
   121  		}
   122  	}
   123  
   124  	if e != nil {
   125  		return nil, &PathError{"open", name, e}
   126  	}
   127  
   128  	if append {
   129  		if _, e = syscall.Seek(fd, 0, io.SeekEnd); e != nil {
   130  			return nil, &PathError{"seek", name, e}
   131  		}
   132  	}
   133  
   134  	return NewFile(uintptr(fd), name), nil
   135  }
   136  
   137  // Close closes the File, rendering it unusable for I/O.
   138  // It returns an error, if any.
   139  func (f *File) Close() error {
   140  	if err := f.checkValid("close"); err != nil {
   141  		return err
   142  	}
   143  	return f.file.close()
   144  }
   145  
   146  func (file *file) close() error {
   147  	if file == nil || file.fd == badFd {
   148  		return ErrInvalid
   149  	}
   150  	var err error
   151  	if e := syscall.Close(file.fd); e != nil {
   152  		err = &PathError{"close", file.name, e}
   153  	}
   154  	file.fd = badFd // so it can't be closed again
   155  
   156  	// no need for a finalizer anymore
   157  	runtime.SetFinalizer(file, nil)
   158  	return err
   159  }
   160  
   161  // Stat returns the FileInfo structure describing file.
   162  // If there is an error, it will be of type *PathError.
   163  func (f *File) Stat() (FileInfo, error) {
   164  	if f == nil {
   165  		return nil, ErrInvalid
   166  	}
   167  	d, err := dirstat(f)
   168  	if err != nil {
   169  		return nil, err
   170  	}
   171  	return fileInfoFromStat(d), nil
   172  }
   173  
   174  // Truncate changes the size of the file.
   175  // It does not change the I/O offset.
   176  // If there is an error, it will be of type *PathError.
   177  func (f *File) Truncate(size int64) error {
   178  	if f == nil {
   179  		return ErrInvalid
   180  	}
   181  
   182  	var d syscall.Dir
   183  	d.Null()
   184  	d.Length = size
   185  
   186  	var buf [syscall.STATFIXLEN]byte
   187  	n, err := d.Marshal(buf[:])
   188  	if err != nil {
   189  		return &PathError{"truncate", f.name, err}
   190  	}
   191  	if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
   192  		return &PathError{"truncate", f.name, err}
   193  	}
   194  	return nil
   195  }
   196  
   197  const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | ModePerm)
   198  
   199  func (f *File) chmod(mode FileMode) error {
   200  	if f == nil {
   201  		return ErrInvalid
   202  	}
   203  	var d syscall.Dir
   204  
   205  	odir, e := dirstat(f)
   206  	if e != nil {
   207  		return &PathError{"chmod", f.name, e}
   208  	}
   209  	d.Null()
   210  	d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
   211  
   212  	var buf [syscall.STATFIXLEN]byte
   213  	n, err := d.Marshal(buf[:])
   214  	if err != nil {
   215  		return &PathError{"chmod", f.name, err}
   216  	}
   217  	if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
   218  		return &PathError{"chmod", f.name, err}
   219  	}
   220  	return nil
   221  }
   222  
   223  // Sync commits the current contents of the file to stable storage.
   224  // Typically, this means flushing the file system's in-memory copy
   225  // of recently written data to disk.
   226  func (f *File) Sync() error {
   227  	if f == nil {
   228  		return ErrInvalid
   229  	}
   230  	var d syscall.Dir
   231  	d.Null()
   232  
   233  	var buf [syscall.STATFIXLEN]byte
   234  	n, err := d.Marshal(buf[:])
   235  	if err != nil {
   236  		return NewSyscallError("fsync", err)
   237  	}
   238  	if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
   239  		return NewSyscallError("fsync", err)
   240  	}
   241  	return nil
   242  }
   243  
   244  // read reads up to len(b) bytes from the File.
   245  // It returns the number of bytes read and an error, if any.
   246  func (f *File) read(b []byte) (n int, err error) {
   247  	n, e := fixCount(syscall.Read(f.fd, b))
   248  	if n == 0 && len(b) > 0 && e == nil {
   249  		return 0, io.EOF
   250  	}
   251  	return n, e
   252  }
   253  
   254  // pread reads len(b) bytes from the File starting at byte offset off.
   255  // It returns the number of bytes read and the error, if any.
   256  // EOF is signaled by a zero count with err set to nil.
   257  func (f *File) pread(b []byte, off int64) (n int, err error) {
   258  	n, e := fixCount(syscall.Pread(f.fd, b, off))
   259  	if n == 0 && len(b) > 0 && e == nil {
   260  		return 0, io.EOF
   261  	}
   262  	return n, e
   263  }
   264  
   265  // write writes len(b) bytes to the File.
   266  // It returns the number of bytes written and an error, if any.
   267  // Since Plan 9 preserves message boundaries, never allow
   268  // a zero-byte write.
   269  func (f *File) write(b []byte) (n int, err error) {
   270  	if len(b) == 0 {
   271  		return 0, nil
   272  	}
   273  	return fixCount(syscall.Write(f.fd, b))
   274  }
   275  
   276  // pwrite writes len(b) bytes to the File starting at byte offset off.
   277  // It returns the number of bytes written and an error, if any.
   278  // Since Plan 9 preserves message boundaries, never allow
   279  // a zero-byte write.
   280  func (f *File) pwrite(b []byte, off int64) (n int, err error) {
   281  	if len(b) == 0 {
   282  		return 0, nil
   283  	}
   284  	return fixCount(syscall.Pwrite(f.fd, b, off))
   285  }
   286  
   287  // seek sets the offset for the next Read or Write on file to offset, interpreted
   288  // according to whence: 0 means relative to the origin of the file, 1 means
   289  // relative to the current offset, and 2 means relative to the end.
   290  // It returns the new offset and an error, if any.
   291  func (f *File) seek(offset int64, whence int) (ret int64, err error) {
   292  	return syscall.Seek(f.fd, offset, whence)
   293  }
   294  
   295  // Truncate changes the size of the named file.
   296  // If the file is a symbolic link, it changes the size of the link's target.
   297  // If there is an error, it will be of type *PathError.
   298  func Truncate(name string, size int64) error {
   299  	var d syscall.Dir
   300  
   301  	d.Null()
   302  	d.Length = size
   303  
   304  	var buf [syscall.STATFIXLEN]byte
   305  	n, err := d.Marshal(buf[:])
   306  	if err != nil {
   307  		return &PathError{"truncate", name, err}
   308  	}
   309  	if err = syscall.Wstat(name, buf[:n]); err != nil {
   310  		return &PathError{"truncate", name, err}
   311  	}
   312  	return nil
   313  }
   314  
   315  // Remove removes the named file or directory.
   316  // If there is an error, it will be of type *PathError.
   317  func Remove(name string) error {
   318  	if e := syscall.Remove(name); e != nil {
   319  		return &PathError{"remove", name, e}
   320  	}
   321  	return nil
   322  }
   323  
   324  // HasPrefix from the strings package.
   325  func hasPrefix(s, prefix string) bool {
   326  	return len(s) >= len(prefix) && s[0:len(prefix)] == prefix
   327  }
   328  
   329  // LastIndexByte from the strings package.
   330  func lastIndex(s string, sep byte) int {
   331  	for i := len(s) - 1; i >= 0; i-- {
   332  		if s[i] == sep {
   333  			return i
   334  		}
   335  	}
   336  	return -1
   337  }
   338  
   339  func rename(oldname, newname string) error {
   340  	dirname := oldname[:lastIndex(oldname, '/')+1]
   341  	if hasPrefix(newname, dirname) {
   342  		newname = newname[len(dirname):]
   343  	} else {
   344  		return &LinkError{"rename", oldname, newname, ErrInvalid}
   345  	}
   346  
   347  	// If newname still contains slashes after removing the oldname
   348  	// prefix, the rename is cross-directory and must be rejected.
   349  	if lastIndex(newname, '/') >= 0 {
   350  		return &LinkError{"rename", oldname, newname, ErrInvalid}
   351  	}
   352  
   353  	var d syscall.Dir
   354  
   355  	d.Null()
   356  	d.Name = newname
   357  
   358  	buf := make([]byte, syscall.STATFIXLEN+len(d.Name))
   359  	n, err := d.Marshal(buf[:])
   360  	if err != nil {
   361  		return &LinkError{"rename", oldname, newname, err}
   362  	}
   363  
   364  	// If newname already exists and is not a directory, rename replaces it.
   365  	f, err := Stat(dirname + newname)
   366  	if err == nil && !f.IsDir() {
   367  		Remove(dirname + newname)
   368  	}
   369  
   370  	if err = syscall.Wstat(oldname, buf[:n]); err != nil {
   371  		return &LinkError{"rename", oldname, newname, err}
   372  	}
   373  	return nil
   374  }
   375  
   376  // See docs in file.go:Chmod.
   377  func chmod(name string, mode FileMode) error {
   378  	var d syscall.Dir
   379  
   380  	odir, e := dirstat(name)
   381  	if e != nil {
   382  		return &PathError{"chmod", name, e}
   383  	}
   384  	d.Null()
   385  	d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
   386  
   387  	var buf [syscall.STATFIXLEN]byte
   388  	n, err := d.Marshal(buf[:])
   389  	if err != nil {
   390  		return &PathError{"chmod", name, err}
   391  	}
   392  	if err = syscall.Wstat(name, buf[:n]); err != nil {
   393  		return &PathError{"chmod", name, err}
   394  	}
   395  	return nil
   396  }
   397  
   398  // Chtimes changes the access and modification times of the named
   399  // file, similar to the Unix utime() or utimes() functions.
   400  //
   401  // The underlying filesystem may truncate or round the values to a
   402  // less precise time unit.
   403  // If there is an error, it will be of type *PathError.
   404  func Chtimes(name string, atime time.Time, mtime time.Time) error {
   405  	var d syscall.Dir
   406  
   407  	d.Null()
   408  	d.Atime = uint32(atime.Unix())
   409  	d.Mtime = uint32(mtime.Unix())
   410  
   411  	var buf [syscall.STATFIXLEN]byte
   412  	n, err := d.Marshal(buf[:])
   413  	if err != nil {
   414  		return &PathError{"chtimes", name, err}
   415  	}
   416  	if err = syscall.Wstat(name, buf[:n]); err != nil {
   417  		return &PathError{"chtimes", name, err}
   418  	}
   419  	return nil
   420  }
   421  
   422  // Pipe returns a connected pair of Files; reads from r return bytes
   423  // written to w. It returns the files and an error, if any.
   424  func Pipe() (r *File, w *File, err error) {
   425  	var p [2]int
   426  
   427  	if e := syscall.Pipe(p[0:]); e != nil {
   428  		return nil, nil, NewSyscallError("pipe", e)
   429  	}
   430  
   431  	return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
   432  }
   433  
   434  // not supported on Plan 9
   435  
   436  // Link creates newname as a hard link to the oldname file.
   437  // If there is an error, it will be of type *LinkError.
   438  func Link(oldname, newname string) error {
   439  	return &LinkError{"link", oldname, newname, syscall.EPLAN9}
   440  }
   441  
   442  // Symlink creates newname as a symbolic link to oldname.
   443  // If there is an error, it will be of type *LinkError.
   444  func Symlink(oldname, newname string) error {
   445  	return &LinkError{"symlink", oldname, newname, syscall.EPLAN9}
   446  }
   447  
   448  // Readlink returns the destination of the named symbolic link.
   449  // If there is an error, it will be of type *PathError.
   450  func Readlink(name string) (string, error) {
   451  	return "", &PathError{"readlink", name, syscall.EPLAN9}
   452  }
   453  
   454  // Chown changes the numeric uid and gid of the named file.
   455  // If the file is a symbolic link, it changes the uid and gid of the link's target.
   456  // If there is an error, it will be of type *PathError.
   457  func Chown(name string, uid, gid int) error {
   458  	return &PathError{"chown", name, syscall.EPLAN9}
   459  }
   460  
   461  // Lchown changes the numeric uid and gid of the named file.
   462  // If the file is a symbolic link, it changes the uid and gid of the link itself.
   463  // If there is an error, it will be of type *PathError.
   464  func Lchown(name string, uid, gid int) error {
   465  	return &PathError{"lchown", name, syscall.EPLAN9}
   466  }
   467  
   468  // Chown changes the numeric uid and gid of the named file.
   469  // If there is an error, it will be of type *PathError.
   470  func (f *File) Chown(uid, gid int) error {
   471  	if f == nil {
   472  		return ErrInvalid
   473  	}
   474  	return &PathError{"chown", f.name, syscall.EPLAN9}
   475  }
   476  
   477  func tempDir() string {
   478  	return "/tmp"
   479  }
   480  
   481  // Chdir changes the current working directory to the file,
   482  // which must be a directory.
   483  // If there is an error, it will be of type *PathError.
   484  func (f *File) Chdir() error {
   485  	if err := f.checkValid("chdir"); err != nil {
   486  		return err
   487  	}
   488  	if e := syscall.Fchdir(f.fd); e != nil {
   489  		return &PathError{"chdir", f.name, e}
   490  	}
   491  	return nil
   492  }
   493  
   494  // checkValid checks whether f is valid for use.
   495  // If not, it returns an appropriate error, perhaps incorporating the operation name op.
   496  func (f *File) checkValid(op string) error {
   497  	if f == nil {
   498  		return ErrInvalid
   499  	}
   500  	if f.fd == badFd {
   501  		return &PathError{op, f.name, ErrClosed}
   502  	}
   503  	return nil
   504  }