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.
14 func sameFile(fs1
, fs2
*fileStat
) bool {
15 a
:= fs1
.sys
.(*syscall
.Dir
)
16 b
:= fs2
.sys
.(*syscall
.Dir
)
17 return a
.Qid
.Path
== b
.Qid
.Path
&& a
.Type
== b
.Type
&& a
.Dev
== b
.Dev
20 func fileInfoFromStat(d
*syscall
.Dir
) FileInfo
{
23 size
: int64(d
.Length
),
24 modTime
: time
.Unix(int64(d
.Mtime
), 0),
27 fs
.mode
= FileMode(d
.Mode
& 0777)
28 if d
.Mode
&syscall
.DMDIR
!= 0 {
31 if d
.Mode
&syscall
.DMAPPEND
!= 0 {
34 if d
.Mode
&syscall
.DMEXCL
!= 0 {
35 fs
.mode |
= ModeExclusive
37 if d
.Mode
&syscall
.DMTMP
!= 0 {
38 fs
.mode |
= ModeTemporary
43 // arg is an open *File or a path string.
44 func dirstat(arg
interface{}) (*syscall
.Dir
, error
) {
48 size
:= syscall
.STATFIXLEN
+ 16*4
50 for i
:= 0; i
< 2; i
++ {
51 buf
:= make([]byte, _BIT16SZ
+size
)
54 switch a
:= arg
.(type) {
57 n
, err
= syscall
.Fstat(a
.fd
, buf
)
60 n
, err
= syscall
.Stat(a
, buf
)
62 panic("phase error in dirstat")
66 return nil, &PathError
{"stat", name
, err
}
69 // Pull the real size out of the stat message.
70 size
= int(uint16(buf
[0]) |
uint16(buf
[1])<<8)
72 // If the stat message is larger than our buffer we will
73 // go around the loop and allocate one that is big enough.
75 d
, err
:= syscall
.UnmarshalDir(buf
[:n
])
77 return nil, &PathError
{"stat", name
, err
}
85 err
= syscall
.ErrBadStat
88 return nil, &PathError
{"stat", name
, err
}
91 // Stat returns a FileInfo describing the named file.
92 // If there is an error, it will be of type *PathError.
93 func Stat(name
string) (FileInfo
, error
) {
94 d
, err
:= dirstat(name
)
98 return fileInfoFromStat(d
), nil
101 // Lstat returns a FileInfo describing the named file.
102 // If the file is a symbolic link, the returned FileInfo
103 // describes the symbolic link. Lstat makes no attempt to follow the link.
104 // If there is an error, it will be of type *PathError.
105 func Lstat(name
string) (FileInfo
, error
) {
110 func atime(fi FileInfo
) time
.Time
{
111 return time
.Unix(int64(fi
.Sys().(*syscall
.Dir
).Atime
), 0)