github.com/sbinet/go@v0.0.0-20160827155028-54d7de7dd62b/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  	return fs
    35  }
    36  
    37  // arg is an open *File or a path string.
    38  func dirstat(arg interface{}) (*syscall.Dir, error) {
    39  	var name string
    40  	var err error
    41  
    42  	size := syscall.STATFIXLEN + 16*4
    43  
    44  	for i := 0; i < 2; i++ {
    45  		buf := make([]byte, _BIT16SZ+size)
    46  
    47  		var n int
    48  		switch a := arg.(type) {
    49  		case *File:
    50  			name = a.name
    51  			n, err = syscall.Fstat(a.fd, buf)
    52  		case string:
    53  			name = a
    54  			n, err = syscall.Stat(a, buf)
    55  		default:
    56  			panic("phase error in dirstat")
    57  		}
    58  
    59  		if n < _BIT16SZ {
    60  			return nil, &PathError{"stat", name, err}
    61  		}
    62  
    63  		// Pull the real size out of the stat message.
    64  		size = int(uint16(buf[0]) | uint16(buf[1])<<8)
    65  
    66  		// If the stat message is larger than our buffer we will
    67  		// go around the loop and allocate one that is big enough.
    68  		if size <= n {
    69  			d, err := syscall.UnmarshalDir(buf[:n])
    70  			if err != nil {
    71  				return nil, &PathError{"stat", name, err}
    72  			}
    73  			return d, nil
    74  		}
    75  
    76  	}
    77  
    78  	if err == nil {
    79  		err = syscall.ErrBadStat
    80  	}
    81  
    82  	return nil, &PathError{"stat", name, err}
    83  }
    84  
    85  // Stat returns a FileInfo describing the named file.
    86  // If there is an error, it will be of type *PathError.
    87  func Stat(name string) (FileInfo, error) {
    88  	d, err := dirstat(name)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	return fileInfoFromStat(d), nil
    93  }
    94  
    95  // Lstat returns a FileInfo describing the named file.
    96  // If the file is a symbolic link, the returned FileInfo
    97  // describes the symbolic link. Lstat makes no attempt to follow the link.
    98  // If there is an error, it will be of type *PathError.
    99  func Lstat(name string) (FileInfo, error) {
   100  	return Stat(name)
   101  }
   102  
   103  // For testing.
   104  func atime(fi FileInfo) time.Time {
   105  	return time.Unix(int64(fi.Sys().(*syscall.Dir).Atime), 0)
   106  }