Daily bump.
[official-gcc.git] / libgo / go / os / stat_plan9.go
blob274d0d86f34c593b28ee39bd611502ff1ca8af70
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.
5 package os
7 import (
8 "syscall"
9 "time"
12 const _BIT16SZ = 2
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,
21 fs.mode = FileMode(d.Mode & 0777)
22 if d.Mode&syscall.DMDIR != 0 {
23 fs.mode |= ModeDir
25 if d.Mode&syscall.DMAPPEND != 0 {
26 fs.mode |= ModeAppend
28 if d.Mode&syscall.DMEXCL != 0 {
29 fs.mode |= ModeExclusive
31 if d.Mode&syscall.DMTMP != 0 {
32 fs.mode |= ModeTemporary
34 // Consider all files not served by #M as device files.
35 if d.Type != 'M' {
36 fs.mode |= ModeDevice
38 return fs
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
46 size := syscall.STATFIXLEN + 16*4
48 for i := 0; i < 2; i++ {
49 buf := make([]byte, _BIT16SZ+size)
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")
63 if n < _BIT16SZ {
64 return nil, &PathError{"stat", name, err}
67 // Pull the real size out of the stat message.
68 size = int(uint16(buf[0]) | uint16(buf[1])<<8)
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}
77 return d, nil
82 if err == nil {
83 err = syscall.ErrBadStat
86 return nil, &PathError{"stat", name, err}
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
96 return fileInfoFromStat(d), nil
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)
107 // For testing.
108 func atime(fi FileInfo) time.Time {
109 return time.Unix(int64(fi.Sys().(*syscall.Dir).Atime), 0)