github.com/FenixAra/go@v0.0.0-20170127160404-96ea0918e670/src/os/stat_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  	"syscall"
     9  	"time"
    10  )
    11  
    12  const _BIT16SZ = 2
    13  
    14  func fileInfoFromStat(d *syscall.Dir) FileInfo {
    15  	fs := &fileStat{
    16  		name:    d.Name,
    17  		size:    d.Length,
    18  		modTime: time.Unix(int64(d.Mtime), 0),
    19  		sys:     d,
    20  	}
    21  	fs.mode = FileMode(d.Mode & 0777)
    22  	if d.Mode&syscall.DMDIR != 0 {
    23  		fs.mode |= ModeDir
    24  	}
    25  	if d.Mode&syscall.DMAPPEND != 0 {
    26  		fs.mode |= ModeAppend
    27  	}
    28  	if d.Mode&syscall.DMEXCL != 0 {
    29  		fs.mode |= ModeExclusive
    30  	}
    31  	if d.Mode&syscall.DMTMP != 0 {
    32  		fs.mode |= ModeTemporary
    33  	}
    34  	// Consider all files not served by #M as device files.
    35  	if d.Type != 'M' {
    36  		fs.mode |= ModeDevice
    37  	}
    38  	return fs
    39  }
    40  
    41  // arg is an open *File or a path string.
    42  func dirstat(arg interface{}) (*syscall.Dir, error) {
    43  	var name string
    44  	var err error
    45  
    46  	size := syscall.STATFIXLEN + 16*4
    47  
    48  	for i := 0; i < 2; i++ {
    49  		buf := make([]byte, _BIT16SZ+size)
    50  
    51  		var n int
    52  		switch a := arg.(type) {
    53  		case *File:
    54  			name = a.name
    55  			n, err = syscall.Fstat(a.fd, buf)
    56  		case string:
    57  			name = a
    58  			n, err = syscall.Stat(a, buf)
    59  		default:
    60  			panic("phase error in dirstat")
    61  		}
    62  
    63  		if n < _BIT16SZ {
    64  			return nil, &PathError{"stat", name, err}
    65  		}
    66  
    67  		// Pull the real size out of the stat message.
    68  		size = int(uint16(buf[0]) | uint16(buf[1])<<8)
    69  
    70  		// If the stat message is larger than our buffer we will
    71  		// go around the loop and allocate one that is big enough.
    72  		if size <= n {
    73  			d, err := syscall.UnmarshalDir(buf[:n])
    74  			if err != nil {
    75  				return nil, &PathError{"stat", name, err}
    76  			}
    77  			return d, nil
    78  		}
    79  
    80  	}
    81  
    82  	if err == nil {
    83  		err = syscall.ErrBadStat
    84  	}
    85  
    86  	return nil, &PathError{"stat", name, err}
    87  }
    88  
    89  // Stat returns a FileInfo describing the named file.
    90  // If there is an error, it will be of type *PathError.
    91  func Stat(name string) (FileInfo, error) {
    92  	d, err := dirstat(name)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	return fileInfoFromStat(d), nil
    97  }
    98  
    99  // Lstat returns a FileInfo describing the named file.
   100  // If the file is a symbolic link, the returned FileInfo
   101  // describes the symbolic link. Lstat makes no attempt to follow the link.
   102  // If there is an error, it will be of type *PathError.
   103  func Lstat(name string) (FileInfo, error) {
   104  	return Stat(name)
   105  }
   106  
   107  // For testing.
   108  func atime(fi FileInfo) time.Time {
   109  	return time.Unix(int64(fi.Sys().(*syscall.Dir).Atime), 0)
   110  }