forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_times.go
195 lines (167 loc) · 4.44 KB
/
file_times.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/direnv/direnv/gzenv"
)
// FileTime represents a single recorded file status
type FileTime struct {
Path string
Modtime int64
Exists bool
}
// FileTimes represent a record of all the known files and times
type FileTimes struct {
list *[]FileTime
}
// NewFileTimes creates a new empty FileTimes
func NewFileTimes() (times FileTimes) {
list := make([]FileTime, 0)
times.list = &list
return
}
// Update gets the latest stats on the path and updates the record.
func (times *FileTimes) Update(path string) (err error) {
var modtime int64
var exists bool
stat, err := getLatestStat(path)
if os.IsNotExist(err) {
exists = false
} else {
exists = true
if err != nil {
return
}
modtime = stat.ModTime().Unix()
}
err = times.NewTime(path, modtime, exists)
return
}
// NewTime add the file on path, with modtime and exists flag to the list of known
// files.
func (times *FileTimes) NewTime(path string, modtime int64, exists bool) (err error) {
var time *FileTime
path, err = filepath.Abs(path)
if err != nil {
return
}
path = filepath.Clean(path)
for idx := range *(times.list) {
if (*times.list)[idx].Path == path {
time = &(*times.list)[idx]
break
}
}
if time == nil {
newTimes := append(*times.list, FileTime{Path: path})
times.list = &newTimes
time = &((*times.list)[len(*times.list)-1])
}
time.Modtime = modtime
time.Exists = exists
return
}
type checkFailed struct {
message string
}
func (err checkFailed) Error() string {
return err.message
}
// Check validates all the recorded file times
func (times *FileTimes) Check() (err error) {
if len(*times.list) == 0 {
return checkFailed{"Times list is empty"}
}
for idx := range *times.list {
err = (*times.list)[idx].Check()
if err != nil {
return
}
}
return
}
// CheckOne compares notes between the given path and the recorded times
func (times *FileTimes) CheckOne(path string) (err error) {
path, err = filepath.Abs(path)
if err != nil {
return
}
for idx := range *times.list {
if time := (*times.list)[idx]; time.Path == path {
err = time.Check()
return
}
}
return checkFailed{fmt.Sprintf("File %q is unknown", path)}
}
// Check verifies that the file is good and hasn't changed
func (times FileTime) Check() (err error) {
stat, err := getLatestStat(times.Path)
switch {
case os.IsNotExist(err):
if times.Exists {
logDebug("Stat Check: %s: gone", times.Path)
return checkFailed{fmt.Sprintf("File %q is missing (Stat)", times.Path)}
}
case err != nil:
logDebug("Stat Check: %s: ERR: %v", times.Path, err)
return err
case !times.Exists:
logDebug("Check: %s: appeared", times.Path)
return checkFailed{fmt.Sprintf("File %q newly created", times.Path)}
case stat.ModTime().Unix() != times.Modtime:
logDebug("Check: %s: stale (stat: %v, lastcheck: %v)",
times.Path, stat.ModTime().Unix(), times.Modtime)
return checkFailed{fmt.Sprintf("File %q has changed", times.Path)}
}
logDebug("Check: %s: up to date", times.Path)
return nil
}
// Formatted shows the times in a user-friendly format.
func (times *FileTime) Formatted(relDir string) string {
timeBytes, err := time.Unix(times.Modtime, 0).MarshalText()
if err != nil {
timeBytes = []byte("<<???>>")
}
path, err := filepath.Rel(relDir, times.Path)
if err != nil {
path = times.Path
}
return fmt.Sprintf("%q - %s", path, timeBytes)
}
// Marshal dumps the times into gzenv format
func (times *FileTimes) Marshal() string {
return gzenv.Marshal(*times.list)
}
// Unmarshal loads the watches back from gzenv
func (times *FileTimes) Unmarshal(from string) error {
return gzenv.Unmarshal(from, times.list)
}
func getLatestStat(path string) (os.FileInfo, error) {
var lstatModTime int64
var statModTime int64
// Check the examine-a-symlink case first:
lstat, err := os.Lstat(path)
if err != nil {
logDebug("getLatestStat,Lstat: %s: error: %v", path, err)
return nil, err
}
lstatModTime = lstat.ModTime().Unix()
stat, err := os.Stat(path)
if err != nil {
logDebug("getLatestStat,Stat: %s: error: %v (Lstat time: %v)",
path, err, lstatModTime)
return nil, err
}
statModTime = stat.ModTime().Unix()
if lstatModTime > statModTime {
logDebug("getLatestStat: %s: Lstat: %v, Stat: %v -> preferring Lstat",
path, lstatModTime, statModTime)
return lstat, nil
}
logDebug("getLatestStat: %s: Lstat: %v, Stat: %v -> preferring Stat",
path, lstatModTime, statModTime)
return stat, nil
}