OSDN Git Service

libgo: Update to Go 1.0.3.
[pf3gnuchains/gcc-fork.git] / libgo / go / 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 func sameFile(sys1, sys2 interface{}) bool {
13         a := sys1.(*Dir)
14         b := sys2.(*Dir)
15         return a.Qid.Path == b.Qid.Path && a.Type == b.Type && a.Dev == b.Dev
16 }
17
18 func fileInfoFromStat(d *Dir) FileInfo {
19         fs := &fileStat{
20                 name:    d.Name,
21                 size:    int64(d.Length),
22                 modTime: time.Unix(int64(d.Mtime), 0),
23                 sys:     d,
24         }
25         fs.mode = FileMode(d.Mode & 0777)
26         if d.Mode&syscall.DMDIR != 0 {
27                 fs.mode |= ModeDir
28         }
29         if d.Mode&syscall.DMAPPEND != 0 {
30                 fs.mode |= ModeAppend
31         }
32         if d.Mode&syscall.DMEXCL != 0 {
33                 fs.mode |= ModeExclusive
34         }
35         if d.Mode&syscall.DMTMP != 0 {
36                 fs.mode |= ModeTemporary
37         }
38         return fs
39 }
40
41 // arg is an open *File or a path string. 
42 func dirstat(arg interface{}) (d *Dir, err error) {
43         var name string
44
45         // This is big enough for most stat messages
46         // and rounded to a multiple of 128 bytes.
47         size := (syscall.STATFIXLEN + 16*4 + 128) &^ 128
48
49         for i := 0; i < 2; i++ {
50                 buf := make([]byte, size)
51
52                 var n int
53                 switch a := arg.(type) {
54                 case *File:
55                         name = a.name
56                         n, err = syscall.Fstat(a.fd, buf)
57                 case string:
58                         name = a
59                         n, err = syscall.Stat(name, buf)
60                 }
61                 if err != nil {
62                         return nil, &PathError{"stat", name, err}
63                 }
64                 if n < syscall.STATFIXLEN {
65                         return nil, &PathError{"stat", name, errShortStat}
66                 }
67
68                 // Pull the real size out of the stat message.
69                 s, _ := gbit16(buf)
70                 size = int(s)
71
72                 // If the stat message is larger than our buffer we will
73                 // go around the loop and allocate one that is big enough.
74                 if size <= n {
75                         d, err = UnmarshalDir(buf[:n])
76                         if err != nil {
77                                 return nil, &PathError{"stat", name, err}
78                         }
79                         return
80                 }
81         }
82         return nil, &PathError{"stat", name, errBadStat}
83 }
84
85 // Stat returns a FileInfo structure 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 the FileInfo structure describing the named file.
96 // If the file is a symbolic link (though Plan 9 does not have symbolic links), 
97 // the returned FileInfo 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().(*Dir).Atime), 0)
106 }