Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
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 "syscall" | |
8 | |
9 func fileInfoFromStat(fi *FileInfo, d *Dir) *FileInfo { | |
10 fi.Dev = uint64(d.Qid.Vers) | uint64(d.Qid.Type<<32) | |
11 fi.Ino = d.Qid.Path | |
12 | |
13 fi.Mode = uint32(d.Mode) & 0777 | |
14 if (d.Mode & syscall.DMDIR) == syscall.DMDIR { | |
15 fi.Mode |= syscall.S_IFDIR | |
16 } else { | |
17 fi.Mode |= syscall.S_IFREG | |
18 } | |
19 | |
20 fi.Size = int64(d.Length) | |
21 fi.Atime_ns = 1e9 * int64(d.Atime) | |
22 fi.Mtime_ns = 1e9 * int64(d.Mtime) | |
23 fi.Name = d.Name | |
24 fi.FollowedSymlink = false | |
25 return fi | |
26 } | |
27 | |
28 // arg is an open *File or a path string.· | |
29 func dirstat(arg interface{}) (fi *FileInfo, err Error) { | |
30 var name string | |
31 nd := syscall.STATFIXLEN + 16*4 | |
32 | |
33 for i := 0; i < 2; i++ { /* should work by the second try */ | |
34 buf := make([]byte, nd) | |
35 | |
36 var n int | |
37 var e syscall.Error | |
38 | |
39 switch syscall_arg := arg.(type) { | |
40 case *File: | |
41 name = syscall_arg.name | |
42 n, e = syscall.Fstat(syscall_arg.fd, buf) | |
43 case string: | |
44 name = syscall_arg | |
45 n, e = syscall.Stat(name, buf) | |
46 } | |
47 | |
48 if e != nil { | |
49 return nil, &PathError{"stat", name, e} | |
50 } | |
51 | |
52 if n < syscall.STATFIXLEN { | |
53 return nil, &PathError{"stat", name, Eshortstat} | |
54 } | |
55 | |
56 ntmp, _ := gbit16(buf) | |
57 nd = int(ntmp) | |
58 | |
59 if nd <= n { | |
60 d, e := UnmarshalDir(buf[:n]) | |
61 | |
62 if e != nil { | |
63 return nil, &PathError{"stat", name, e} | |
64 } | |
65 | |
66 return fileInfoFromStat(new(FileInfo), d), nil | |
67 } | |
68 } | |
69 | |
70 return nil, &PathError{"stat", name, Ebadstat} | |
71 } | |
72 | |
73 | |
74 // Stat returns a FileInfo structure describing the named file and an error, if any. | |
75 func Stat(name string) (fi *FileInfo, err Error) { | |
76 return dirstat(name) | |
77 } | |
78 | |
79 func Lstat(name string) (fi *FileInfo, err Error) { | |
r
2011/03/31 22:57:46
copy the comment for Lstat from file.go
paulzhol
2011/04/01 20:16:44
Done.
| |
80 return dirstat(name) | |
81 } | |
OLD | NEW |