-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstat.go
More file actions
89 lines (78 loc) · 2.08 KB
/
stat.go
File metadata and controls
89 lines (78 loc) · 2.08 KB
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
package os
import "solod.dev/so/time"
// A FileInfo describes a file and is returned by [Stat] and [Lstat].
type FileInfo struct {
name string
size int64
mode FileMode
modTime time.Time
dev uint64
ino uint64
}
// Name returns the base name of the file.
func (fi *FileInfo) Name() string { return fi.name }
// Size returns the length in bytes for regular files; system-dependent for others.
func (fi *FileInfo) Size() int64 { return fi.size }
// Mode returns the file mode bits.
func (fi *FileInfo) Mode() FileMode { return fi.mode }
// ModTime returns the modification time.
func (fi *FileInfo) ModTime() time.Time { return fi.modTime }
// IsDir reports whether the file is a directory.
func (fi *FileInfo) IsDir() bool { return fi.mode.IsDir() }
// baseName returns the last element of the path.
func baseName(path string) string {
i := len(path) - 1
// Strip trailing slashes.
for i > 0 && path[i] == '/' {
i--
}
end := i + 1
// Find the last slash.
for i >= 0 && path[i] != '/' {
i--
}
if end == 0 {
return "."
}
return path[i+1 : end]
}
// FileInfoResult is a helper struct for returning
// a FileInfo and an error from a function.
type FileInfoResult struct {
val FileInfo
err error
}
// Stat returns a [FileInfo] describing the named file.
func Stat(name string) (FileInfo, error) {
r := os_stat(name)
if !r.ok {
return FileInfo{}, mapError()
}
fmode := mode_t(r.mode).toFileMode()
return FileInfo{
name: baseName(name),
size: r.size,
mode: fmode,
modTime: time.Unix(r.modSec, r.modNsec),
dev: r.dev,
ino: r.ino,
}, 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.
func Lstat(name string) (FileInfo, error) {
r := os_lstat(name)
if !r.ok {
return FileInfo{}, mapError()
}
fmode := mode_t(r.mode).toFileMode()
return FileInfo{
name: baseName(name),
size: r.size,
mode: fmode,
modTime: time.Unix(r.modSec, r.modNsec),
dev: r.dev,
ino: r.ino,
}, nil
}