Source file src/os/stat_plan9.go

     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 bitSize16 = 2
    13  
    14  func fileInfoFromStat(d *syscall.Dir) *fileStat {
    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  	// Consider all files served by #c as character device files.
    39  	if d.Type == 'c' {
    40  		fs.mode |= ModeCharDevice
    41  	}
    42  	return fs
    43  }
    44  
    45  // arg is an open *File or a path string.
    46  func dirstat(arg any) (*syscall.Dir, error) {
    47  	var name string
    48  	var err error
    49  
    50  	size := syscall.STATFIXLEN + 16*4
    51  
    52  	for i := 0; i < 2; i++ {
    53  		buf := make([]byte, bitSize16+size)
    54  
    55  		var n int
    56  		switch a := arg.(type) {
    57  		case *File:
    58  			name = a.name
    59  			if err := a.incref("fstat"); err != nil {
    60  				return nil, err
    61  			}
    62  			n, err = syscall.Fstat(a.fd, buf)
    63  			a.decref()
    64  		case string:
    65  			name = a
    66  			n, err = syscall.Stat(a, buf)
    67  		default:
    68  			panic("phase error in dirstat")
    69  		}
    70  
    71  		if n < bitSize16 {
    72  			return nil, &PathError{Op: "stat", Path: name, Err: err}
    73  		}
    74  
    75  		// Pull the real size out of the stat message.
    76  		size = int(uint16(buf[0]) | uint16(buf[1])<<8)
    77  
    78  		// If the stat message is larger than our buffer we will
    79  		// go around the loop and allocate one that is big enough.
    80  		if size <= n {
    81  			d, err := syscall.UnmarshalDir(buf[:n])
    82  			if err != nil {
    83  				return nil, &PathError{Op: "stat", Path: name, Err: err}
    84  			}
    85  			return d, nil
    86  		}
    87  
    88  	}
    89  
    90  	if err == nil {
    91  		err = syscall.ErrBadStat
    92  	}
    93  
    94  	return nil, &PathError{Op: "stat", Path: name, Err: err}
    95  }
    96  
    97  // statNolog implements Stat for Plan 9.
    98  func statNolog(name string) (FileInfo, error) {
    99  	d, err := dirstat(name)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	return fileInfoFromStat(d), nil
   104  }
   105  
   106  // lstatNolog implements Lstat for Plan 9.
   107  func lstatNolog(name string) (FileInfo, error) {
   108  	return statNolog(name)
   109  }
   110  
   111  // For testing.
   112  func atime(fi FileInfo) time.Time {
   113  	return time.Unix(int64(fi.Sys().(*syscall.Dir).Atime), 0)
   114  }
   115  

View as plain text