/
file_times.go
185 lines (156 loc) · 3.81 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
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/direnv/direnv/gzenv"
)
type FileTime struct {
Path string
Modtime int64
Exists bool
}
type FileTimes struct {
list *[]FileTime
}
func NewFileTimes() (times FileTimes) {
list := make([]FileTime, 0)
times.list = &list
return
}
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
}
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
}
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
}
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)}
}
func (time FileTime) Check() (err error) {
stat, err := getLatestStat(time.Path)
switch {
case os.IsNotExist(err):
if time.Exists {
log_debug("Stat Check: %s: gone", time.Path)
return checkFailed{fmt.Sprintf("File %q is missing (Stat)", time.Path)}
}
case err != nil:
log_debug("Stat Check: %s: ERR: %v", time.Path, err)
return err
case !time.Exists:
log_debug("Check: %s: appeared", time.Path)
return checkFailed{fmt.Sprintf("File %q newly created", time.Path)}
case stat.ModTime().Unix() != time.Modtime:
log_debug("Check: %s: stale (stat: %v, lastcheck: %v)",
time.Path, stat.ModTime().Unix(), time.Modtime)
return checkFailed{fmt.Sprintf("File %q has changed", time.Path)}
}
log_debug("Check: %s: up to date", time.Path)
return nil
}
func (self *FileTime) Formatted(relDir string) string {
timeBytes, err := time.Unix(self.Modtime, 0).MarshalText()
if err != nil {
timeBytes = []byte("<<???>>")
}
path, err := filepath.Rel(relDir, self.Path)
if err != nil {
path = self.Path
}
return fmt.Sprintf("%q - %s", path, timeBytes)
}
func (times *FileTimes) Marshal() string {
return gzenv.Marshal(*times.list)
}
func (times *FileTimes) Unmarshal(from string) error {
return gzenv.Unmarshal(from, times.list)
}
func getLatestStat(path string) (os.FileInfo, error) {
var lstat_modtime int64
var stat_modtime int64
// Check the examine-a-symlink case first:
lstat, err := os.Lstat(path)
if err != nil {
log_debug("getLatestStat,Lstat: %s: error: %v", path, err)
return nil, err
}
lstat_modtime = lstat.ModTime().Unix()
stat, err := os.Stat(path)
if err != nil {
log_debug("getLatestStat,Stat: %s: error: %v (Lstat time: %v)",
path, err, lstat_modtime)
return nil, err
}
stat_modtime = stat.ModTime().Unix()
if lstat_modtime > stat_modtime {
log_debug("getLatestStat: %s: Lstat: %v, Stat: %v -> preferring Lstat",
path, lstat_modtime, stat_modtime)
return lstat, nil
} else {
log_debug("getLatestStat: %s: Lstat: %v, Stat: %v -> preferring Stat",
path, lstat_modtime, stat_modtime)
return stat, nil
}
}