github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/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  	"runtime"
     9  	"syscall"
    10  	"time"
    11  )
    12  
    13  // File represents an open file descriptor.
    14  type File struct {
    15  	*file
    16  }
    17  
    18  // file is the real representation of *File.
    19  // The extra level of indirection ensures that no clients of os
    20  // can overwrite this data, which could cause the finalizer
    21  // to close the wrong file descriptor.
    22  type file struct {
    23  	fd      int
    24  	name    string
    25  	dirinfo *dirInfo // nil unless directory being read
    26  }
    27  
    28  // Fd returns the integer Unix file descriptor referencing the open file.
    29  func (f *File) Fd() uintptr {
    30  	if f == nil {
    31  		return ^(uintptr(0))
    32  	}
    33  	return uintptr(f.fd)
    34  }
    35  
    36  // NewFile returns a new File with the given file descriptor and name.
    37  func NewFile(fd uintptr, name string) *File {
    38  	fdi := int(fd)
    39  	if fdi < 0 {
    40  		return nil
    41  	}
    42  	f := &File{&file{fd: fdi, name: name}}
    43  	runtime.SetFinalizer(f.file, (*file).close)
    44  	return f
    45  }
    46  
    47  // Auxiliary information if the File describes a directory
    48  type dirInfo struct {
    49  	buf  [syscall.STATMAX]byte // buffer for directory I/O
    50  	nbuf int                   // length of buf; return value from Read
    51  	bufp int                   // location of next record in buf.
    52  }
    53  
    54  func epipecheck(file *File, e error) {
    55  }
    56  
    57  // DevNull is the name of the operating system's ``null device.''
    58  // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
    59  const DevNull = "/dev/null"
    60  
    61  // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
    62  func syscallMode(i FileMode) (o uint32) {
    63  	o |= uint32(i.Perm())
    64  	if i&ModeAppend != 0 {
    65  		o |= syscall.DMAPPEND
    66  	}
    67  	if i&ModeExclusive != 0 {
    68  		o |= syscall.DMEXCL
    69  	}
    70  	if i&ModeTemporary != 0 {
    71  		o |= syscall.DMTMP
    72  	}
    73  	return
    74  }
    75  
    76  // OpenFile is the generalized open call; most users will use Open
    77  // or Create instead.  It opens the named file with specified flag
    78  // (O_RDONLY etc.) and perm, (0666 etc.) if applicable.  If successful,
    79  // methods on the returned File can be used for I/O.
    80  // If there is an error, it will be of type *PathError.
    81  func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
    82  	var (
    83  		fd     int
    84  		e      error
    85  		create bool
    86  		excl   bool
    87  		trunc  bool
    88  		append bool
    89  	)
    90  
    91  	if flag&O_CREATE == O_CREATE {
    92  		flag = flag & ^O_CREATE
    93  		create = true
    94  	}
    95  	if flag&O_EXCL == O_EXCL {
    96  		excl = true
    97  	}
    98  	if flag&O_TRUNC == O_TRUNC {
    99  		trunc = true
   100  	}
   101  	// O_APPEND is emulated on Plan 9
   102  	if flag&O_APPEND == O_APPEND {
   103  		flag = flag &^ O_APPEND
   104  		append = true
   105  	}
   106  
   107  	if (create && trunc) || excl {
   108  		fd, e = syscall.Create(name, flag, syscallMode(perm))
   109  	} else {
   110  		fd, e = syscall.Open(name, flag)
   111  		if e != nil && create {
   112  			var e1 error
   113  			fd, e1 = syscall.Create(name, flag, syscallMode(perm))
   114  			if e1 == nil {
   115  				e = nil
   116  			}
   117  		}
   118  	}
   119  
   120  	if e != nil {
   121  		return nil, &PathError{"open", name, e}
   122  	}
   123  
   124  	if append {
   125  		if _, e = syscall.Seek(fd, 0, SEEK_END); e != nil {
   126  			return nil, &PathError{"seek", name, e}
   127  		}
   128  	}
   129  
   130  	return NewFile(uintptr(fd), name), nil
   131  }
   132  
   133  // Close closes the File, rendering it unusable for I/O.
   134  // It returns an error, if any.
   135  func (f *File) Close() error {
   136  	if f == nil {
   137  		return ErrInvalid
   138  	}
   139  	return f.file.close()
   140  }
   141  
   142  func (file *file) close() error {
   143  	if file == nil || file.fd < 0 {
   144  		return ErrInvalid
   145  	}
   146  	var err error
   147  	syscall.ForkLock.RLock()
   148  	if e := syscall.Close(file.fd); e != nil {
   149  		err = &PathError{"close", file.name, e}
   150  	}
   151  	syscall.ForkLock.RUnlock()
   152  	file.fd = -1 // 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() (fi FileInfo, err 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() (err 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  	return syscall.Read(f.fd, b)
   248  }
   249  
   250  // pread reads len(b) bytes from the File starting at byte offset off.
   251  // It returns the number of bytes read and the error, if any.
   252  // EOF is signaled by a zero count with err set to nil.
   253  func (f *File) pread(b []byte, off int64) (n int, err error) {
   254  	return syscall.Pread(f.fd, b, off)
   255  }
   256  
   257  // write writes len(b) bytes to the File.
   258  // It returns the number of bytes written and an error, if any.
   259  // Since Plan 9 preserves message boundaries, never allow
   260  // a zero-byte write.
   261  func (f *File) write(b []byte) (n int, err error) {
   262  	if len(b) == 0 {
   263  		return 0, nil
   264  	}
   265  	return syscall.Write(f.fd, b)
   266  }
   267  
   268  // pwrite writes len(b) bytes to the File starting at byte offset off.
   269  // It returns the number of bytes written and an error, if any.
   270  // Since Plan 9 preserves message boundaries, never allow
   271  // a zero-byte write.
   272  func (f *File) pwrite(b []byte, off int64) (n int, err error) {
   273  	if len(b) == 0 {
   274  		return 0, nil
   275  	}
   276  	return syscall.Pwrite(f.fd, b, off)
   277  }
   278  
   279  // seek sets the offset for the next Read or Write on file to offset, interpreted
   280  // according to whence: 0 means relative to the origin of the file, 1 means
   281  // relative to the current offset, and 2 means relative to the end.
   282  // It returns the new offset and an error, if any.
   283  func (f *File) seek(offset int64, whence int) (ret int64, err error) {
   284  	return syscall.Seek(f.fd, offset, whence)
   285  }
   286  
   287  // Truncate changes the size of the named file.
   288  // If the file is a symbolic link, it changes the size of the link's target.
   289  // If there is an error, it will be of type *PathError.
   290  func Truncate(name string, size int64) error {
   291  	var d syscall.Dir
   292  
   293  	d.Null()
   294  	d.Length = size
   295  
   296  	var buf [syscall.STATFIXLEN]byte
   297  	n, err := d.Marshal(buf[:])
   298  	if err != nil {
   299  		return &PathError{"truncate", name, err}
   300  	}
   301  	if err = syscall.Wstat(name, buf[:n]); err != nil {
   302  		return &PathError{"truncate", name, err}
   303  	}
   304  	return nil
   305  }
   306  
   307  // Remove removes the named file or directory.
   308  // If there is an error, it will be of type *PathError.
   309  func Remove(name string) error {
   310  	if e := syscall.Remove(name); e != nil {
   311  		return &PathError{"remove", name, e}
   312  	}
   313  	return nil
   314  }
   315  
   316  // HasPrefix from the strings package.
   317  func hasPrefix(s, prefix string) bool {
   318  	return len(s) >= len(prefix) && s[0:len(prefix)] == prefix
   319  }
   320  
   321  // Variant of LastIndex from the strings package.
   322  func lastIndex(s string, sep byte) int {
   323  	for i := len(s) - 1; i >= 0; i-- {
   324  		if s[i] == sep {
   325  			return i
   326  		}
   327  	}
   328  	return -1
   329  }
   330  
   331  func rename(oldname, newname string) error {
   332  	dirname := oldname[:lastIndex(oldname, '/')+1]
   333  	if hasPrefix(newname, dirname) {
   334  		newname = newname[len(dirname):]
   335  	} else {
   336  		return &LinkError{"rename", oldname, newname, ErrInvalid}
   337  	}
   338  
   339  	// If newname still contains slashes after removing the oldname
   340  	// prefix, the rename is cross-directory and must be rejected.
   341  	// This case is caught by d.Marshal below.
   342  
   343  	var d syscall.Dir
   344  
   345  	d.Null()
   346  	d.Name = newname
   347  
   348  	buf := make([]byte, syscall.STATFIXLEN+len(d.Name))
   349  	n, err := d.Marshal(buf[:])
   350  	if err != nil {
   351  		return &LinkError{"rename", oldname, newname, err}
   352  	}
   353  	if err = syscall.Wstat(oldname, buf[:n]); err != nil {
   354  		return &LinkError{"rename", oldname, newname, err}
   355  	}
   356  	return nil
   357  }
   358  
   359  // Chmod changes the mode of the named file to mode.
   360  // If the file is a symbolic link, it changes the mode of the link's target.
   361  // If there is an error, it will be of type *PathError.
   362  func Chmod(name string, mode FileMode) error {
   363  	var d syscall.Dir
   364  
   365  	odir, e := dirstat(name)
   366  	if e != nil {
   367  		return &PathError{"chmod", name, e}
   368  	}
   369  	d.Null()
   370  	d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
   371  
   372  	var buf [syscall.STATFIXLEN]byte
   373  	n, err := d.Marshal(buf[:])
   374  	if err != nil {
   375  		return &PathError{"chmod", name, err}
   376  	}
   377  	if err = syscall.Wstat(name, buf[:n]); err != nil {
   378  		return &PathError{"chmod", name, err}
   379  	}
   380  	return nil
   381  }
   382  
   383  // Chtimes changes the access and modification times of the named
   384  // file, similar to the Unix utime() or utimes() functions.
   385  //
   386  // The underlying filesystem may truncate or round the values to a
   387  // less precise time unit.
   388  // If there is an error, it will be of type *PathError.
   389  func Chtimes(name string, atime time.Time, mtime time.Time) error {
   390  	var d syscall.Dir
   391  
   392  	d.Null()
   393  	d.Atime = uint32(atime.Unix())
   394  	d.Mtime = uint32(mtime.Unix())
   395  
   396  	var buf [syscall.STATFIXLEN]byte
   397  	n, err := d.Marshal(buf[:])
   398  	if err != nil {
   399  		return &PathError{"chtimes", name, err}
   400  	}
   401  	if err = syscall.Wstat(name, buf[:n]); err != nil {
   402  		return &PathError{"chtimes", name, err}
   403  	}
   404  	return nil
   405  }
   406  
   407  // Pipe returns a connected pair of Files; reads from r return bytes
   408  // written to w. It returns the files and an error, if any.
   409  func Pipe() (r *File, w *File, err error) {
   410  	var p [2]int
   411  
   412  	syscall.ForkLock.RLock()
   413  	if e := syscall.Pipe(p[0:]); e != nil {
   414  		syscall.ForkLock.RUnlock()
   415  		return nil, nil, NewSyscallError("pipe", e)
   416  	}
   417  	syscall.ForkLock.RUnlock()
   418  
   419  	return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
   420  }
   421  
   422  // not supported on Plan 9
   423  
   424  // Link creates newname as a hard link to the oldname file.
   425  // If there is an error, it will be of type *LinkError.
   426  func Link(oldname, newname string) error {
   427  	return &LinkError{"link", oldname, newname, syscall.EPLAN9}
   428  }
   429  
   430  // Symlink creates newname as a symbolic link to oldname.
   431  // If there is an error, it will be of type *LinkError.
   432  func Symlink(oldname, newname string) error {
   433  	return &LinkError{"symlink", oldname, newname, syscall.EPLAN9}
   434  }
   435  
   436  // Readlink returns the destination of the named symbolic link.
   437  // If there is an error, it will be of type *PathError.
   438  func Readlink(name string) (string, error) {
   439  	return "", &PathError{"readlink", name, syscall.EPLAN9}
   440  }
   441  
   442  // Chown changes the numeric uid and gid of the named file.
   443  // If the file is a symbolic link, it changes the uid and gid of the link's target.
   444  // If there is an error, it will be of type *PathError.
   445  func Chown(name string, uid, gid int) error {
   446  	return &PathError{"chown", name, syscall.EPLAN9}
   447  }
   448  
   449  // Lchown changes the numeric uid and gid of the named file.
   450  // If the file is a symbolic link, it changes the uid and gid of the link itself.
   451  // If there is an error, it will be of type *PathError.
   452  func Lchown(name string, uid, gid int) error {
   453  	return &PathError{"lchown", name, syscall.EPLAN9}
   454  }
   455  
   456  // Chown changes the numeric uid and gid of the named file.
   457  // If there is an error, it will be of type *PathError.
   458  func (f *File) Chown(uid, gid int) error {
   459  	if f == nil {
   460  		return ErrInvalid
   461  	}
   462  	return &PathError{"chown", f.name, syscall.EPLAN9}
   463  }
   464  
   465  // TempDir returns the default directory to use for temporary files.
   466  func TempDir() string {
   467  	return "/tmp"
   468  }