1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os
import (
"syscall"
"time"
)
func sameFile(sys1, sys2 interface{}) bool {
a := sys1.(*dir)
b := sys2.(*dir)
return a.Qid.Path == b.Qid.Path && a.Type == b.Type && a.Dev == b.Dev
}
func fileInfoFromStat(d *dir) FileInfo {
fs := &fileStat{
name: d.Name,
size: int64(d.Length),
modTime: time.Unix(int64(d.Mtime), 0),
sys: d,
}
fs.mode = FileMode(d.Mode & 0777)
if d.Mode&syscall.DMDIR != 0 {
fs.mode |= ModeDir
}
if d.Mode&syscall.DMAPPEND != 0 {
fs.mode |= ModeAppend
}
if d.Mode&syscall.DMEXCL != 0 {
fs.mode |= ModeExclusive
}
if d.Mode&syscall.DMTMP != 0 {
fs.mode |= ModeTemporary
}
return fs
}
// arg is an open *File or a path string.
func dirstat(arg interface{}) (d *dir, err error) {
var name string
// This is big enough for most stat messages
// and rounded to a multiple of 128 bytes.
size := (syscall.STATFIXLEN + 16*4 + 128) &^ 128
for i := 0; i < 2; i++ {
buf := make([]byte, size)
var n int
switch a := arg.(type) {
case *File:
name = a.name
n, err = syscall.Fstat(a.fd, buf)
case string:
name = a
n, err = syscall.Stat(name, buf)
}
if err != nil {
return nil, &PathError{"stat", name, err}
}
if n < syscall.STATFIXLEN {
return nil, &PathError{"stat", name, errShortStat}
}
// Pull the real size out of the stat message.
s, _ := gbit16(buf)
size = int(s)
// If the stat message is larger than our buffer we will
// go around the loop and allocate one that is big enough.
if size <= n {
d, err = unmarshalDir(buf[:n])
if err != nil {
return nil, &PathError{"stat", name, err}
}
return
}
}
return nil, &PathError{"stat", name, errBadStat}
}
// Stat returns a FileInfo describing the named file.
// If there is an error, it will be of type *PathError.
func Stat(name string) (fi FileInfo, err error) {
d, err := dirstat(name)
if err != nil {
return nil, err
}
return fileInfoFromStat(d), nil
}
// Lstat returns a FileInfo describing the named file.
// If the file is a symbolic link, the returned FileInfo
// describes the symbolic link. Lstat makes no attempt to follow the link.
// If there is an error, it will be of type *PathError.
func Lstat(name string) (fi FileInfo, err error) {
return Stat(name)
}
// For testing.
func atime(fi FileInfo) time.Time {
return time.Unix(int64(fi.Sys().(*dir).Atime), 0)
}
|