github.com/rakyll/go@v0.0.0-20170216000551-64c02460d703/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 name.
    39  func NewFile(fd uintptr, name string) *File {
    40  	fdi := int(fd)
    41  	if fdi < 0 {
    42  		return nil
    43  	}
    44  	f := &File{&file{fd: fdi, name: name}}
    45  	runtime.SetFinalizer(f.file, (*file).close)
    46  	return f
    47  }
    48  
    49  // Auxiliary information if the File describes a directory
    50  type dirInfo struct {
    51  	buf  [syscall.STATMAX]byte // buffer for directory I/O
    52  	nbuf int                   // length of buf; return value from Read
    53  	bufp int                   // location of next record in buf.
    54  }
    55  
    56  func epipecheck(file *File, e error) {
    57  }
    58  
    59  // DevNull is the name of the operating system's ``null device.''
    60  // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
    61  const DevNull = "/dev/null"
    62  
    63  // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
    64  func syscallMode(i FileMode) (o uint32) {
    65  	o |= uint32(i.Perm())
    66  	if i&ModeAppend != 0 {
    67  		o |= syscall.DMAPPEND
    68  	}
    69  	if i&ModeExclusive != 0 {
    70  		o |= syscall.DMEXCL
    71  	}
    72  	if i&ModeTemporary != 0 {
    73  		o |= syscall.DMTMP
    74  	}
    75  	return
    76  }
    77  
    78  // OpenFile is the generalized open call; most users will use Open
    79  // or Create instead. It opens the named file with specified flag
    80  // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
    81  // methods on the returned File can be used for I/O.
    82  // If there is an error, it will be of type *PathError.
    83  func OpenFile(name string, flag int, perm FileMode) (*File, error) {
    84  	var (
    85  		fd     int
    86  		e      error
    87  		create bool
    88  		excl   bool
    89  		trunc  bool
    90  		append bool
    91  	)
    92  
    93  	if flag&O_CREATE == O_CREATE {
    94  		flag = flag & ^O_CREATE
    95  		create = true
    96  	}
    97  	if flag&O_EXCL == O_EXCL {
    98  		excl = true
    99  	}
   100  	if flag&O_TRUNC == O_TRUNC {
   101  		trunc = true
   102  	}
   103  	// O_APPEND is emulated on Plan 9
   104  	if flag&O_APPEND == O_APPEND {
   105  		flag = flag &^ O_APPEND
   106  		append = true
   107  	}
   108  
   109  	if (create && trunc) || excl {
   110  		fd, e = syscall.Create(name, flag, syscallMode(perm))
   111  	} else {
   112  		fd, e = syscall.Open(name, flag)
   113  		if e != nil && create {
   114  			var e1 error
   115  			fd, e1 = syscall.Create(name, flag, syscallMode(perm))
   116  			if e1 == nil {
   117  				e = nil
   118  			}
   119  		}
   120  	}
   121  
   122  	if e != nil {
   123  		return nil, &PathError{"open", name, e}
   124  	}
   125  
   126  	if append {
   127  		if _, e = syscall.Seek(fd, 0, io.SeekEnd); e != nil {
   128  			return nil, &PathError{"seek", name, e}
   129  		}
   130  	}
   131  
   132  	return NewFile(uintptr(fd), name), nil
   133  }
   134  
   135  // Close closes the File, rendering it unusable for I/O.
   136  // It returns an error, if any.
   137  func (f *File) Close() error {
   138  	if err := f.checkValid("close"); err != nil {
   139  		return err
   140  	}
   141  	return f.file.close()
   142  }
   143  
   144  func (file *file) close() error {
   145  	if file == nil || file.fd == badFd {
   146  		return ErrInvalid
   147  	}
   148  	var err error
   149  	if e := syscall.Close(file.fd); e != nil {
   150  		err = &PathError{"close", file.name, e}
   151  	}
   152  	file.fd = badFd // so it can't be closed again
   153  
   154  	// no need for a finalizer anymore
   155  	runtime.SetFinalizer(file, nil)
   156  	return err
   157  }
   158  
   159  // Stat returns the FileInfo structure describing file.
   160  // If there is an error, it will be of type *PathError.
   161  func (f *File) Stat() (FileInfo, error) {
   162  	if f == nil {
   163  		return nil, ErrInvalid
   164  	}
   165  	d, err := dirstat(f)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  	return fileInfoFromStat(d), nil
   170  }
   171  
   172  // Truncate changes the size of the file.
   173  // It does not change the I/O offset.
   174  // If there is an error, it will be of type *PathError.
   175  func (f *File) Truncate(size int64) error {
   176  	if f == nil {
   177  		return ErrInvalid
   178  	}
   179  
   180  	var d syscall.Dir
   181  	d.Null()
   182  	d.Length = size
   183  
   184  	var buf [syscall.STATFIXLEN]byte
   185  	n, err := d.Marshal(buf[:])
   186  	if err != nil {
   187  		return &PathError{"truncate", f.name, err}
   188  	}
   189  	if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
   190  		return &PathError{"truncate", f.name, err}
   191  	}
   192  	return nil
   193  }
   194  
   195  const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | ModePerm)
   196  
   197  // Chmod changes the mode of the file to mode.
   198  // If there is an error, it will be of type *PathError.
   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  // Chmod changes the mode of the named file to mode.
   377  // If the file is a symbolic link, it changes the mode of the link's target.
   378  // If there is an error, it will be of type *PathError.
   379  func Chmod(name string, mode FileMode) error {
   380  	var d syscall.Dir
   381  
   382  	odir, e := dirstat(name)
   383  	if e != nil {
   384  		return &PathError{"chmod", name, e}
   385  	}
   386  	d.Null()
   387  	d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
   388  
   389  	var buf [syscall.STATFIXLEN]byte
   390  	n, err := d.Marshal(buf[:])
   391  	if err != nil {
   392  		return &PathError{"chmod", name, err}
   393  	}
   394  	if err = syscall.Wstat(name, buf[:n]); err != nil {
   395  		return &PathError{"chmod", name, err}
   396  	}
   397  	return nil
   398  }
   399  
   400  // Chtimes changes the access and modification times of the named
   401  // file, similar to the Unix utime() or utimes() functions.
   402  //
   403  // The underlying filesystem may truncate or round the values to a
   404  // less precise time unit.
   405  // If there is an error, it will be of type *PathError.
   406  func Chtimes(name string, atime time.Time, mtime time.Time) error {
   407  	var d syscall.Dir
   408  
   409  	d.Null()
   410  	d.Atime = uint32(atime.Unix())
   411  	d.Mtime = uint32(mtime.Unix())
   412  
   413  	var buf [syscall.STATFIXLEN]byte
   414  	n, err := d.Marshal(buf[:])
   415  	if err != nil {
   416  		return &PathError{"chtimes", name, err}
   417  	}
   418  	if err = syscall.Wstat(name, buf[:n]); err != nil {
   419  		return &PathError{"chtimes", name, err}
   420  	}
   421  	return nil
   422  }
   423  
   424  // Pipe returns a connected pair of Files; reads from r return bytes
   425  // written to w. It returns the files and an error, if any.
   426  func Pipe() (r *File, w *File, err error) {
   427  	var p [2]int
   428  
   429  	if e := syscall.Pipe(p[0:]); e != nil {
   430  		return nil, nil, NewSyscallError("pipe", e)
   431  	}
   432  
   433  	return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
   434  }
   435  
   436  // not supported on Plan 9
   437  
   438  // Link creates newname as a hard link to the oldname file.
   439  // If there is an error, it will be of type *LinkError.
   440  func Link(oldname, newname string) error {
   441  	return &LinkError{"link", oldname, newname, syscall.EPLAN9}
   442  }
   443  
   444  // Symlink creates newname as a symbolic link to oldname.
   445  // If there is an error, it will be of type *LinkError.
   446  func Symlink(oldname, newname string) error {
   447  	return &LinkError{"symlink", oldname, newname, syscall.EPLAN9}
   448  }
   449  
   450  // Readlink returns the destination of the named symbolic link.
   451  // If there is an error, it will be of type *PathError.
   452  func Readlink(name string) (string, error) {
   453  	return "", &PathError{"readlink", name, syscall.EPLAN9}
   454  }
   455  
   456  // Chown changes the numeric uid and gid of the named file.
   457  // If the file is a symbolic link, it changes the uid and gid of the link's target.
   458  // If there is an error, it will be of type *PathError.
   459  func Chown(name string, uid, gid int) error {
   460  	return &PathError{"chown", name, syscall.EPLAN9}
   461  }
   462  
   463  // Lchown changes the numeric uid and gid of the named file.
   464  // If the file is a symbolic link, it changes the uid and gid of the link itself.
   465  // If there is an error, it will be of type *PathError.
   466  func Lchown(name string, uid, gid int) error {
   467  	return &PathError{"lchown", name, syscall.EPLAN9}
   468  }
   469  
   470  // Chown changes the numeric uid and gid of the named file.
   471  // If there is an error, it will be of type *PathError.
   472  func (f *File) Chown(uid, gid int) error {
   473  	if f == nil {
   474  		return ErrInvalid
   475  	}
   476  	return &PathError{"chown", f.name, syscall.EPLAN9}
   477  }
   478  
   479  // TempDir returns the default directory to use for temporary files.
   480  func TempDir() string {
   481  	return "/tmp"
   482  }
   483  
   484  // Chdir changes the current working directory to the file,
   485  // which must be a directory.
   486  // If there is an error, it will be of type *PathError.
   487  func (f *File) Chdir() error {
   488  	if err := f.checkValid("chdir"); err != nil {
   489  		return err
   490  	}
   491  	if e := syscall.Fchdir(f.fd); e != nil {
   492  		return &PathError{"chdir", f.name, e}
   493  	}
   494  	return nil
   495  }
   496  
   497  // checkValid checks whether f is valid for use.
   498  // If not, it returns an appropriate error, perhaps incorporating the operation name op.
   499  func (f *File) checkValid(op string) error {
   500  	if f == nil {
   501  		return ErrInvalid
   502  	}
   503  	if f.fd == badFd {
   504  		return &PathError{op, f.name, ErrClosed}
   505  	}
   506  	return nil
   507  }