forked from direnv/direnv
/
file_times.go
175 lines (150 loc) · 3.48 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
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
// Handle the path with Stat, which looks at the other
// end of symlinks
stat, err := os.Stat(path)
if os.IsNotExist(err) {
exists = false
} else {
exists = true
if err != nil {
return
}
modtime = stat.ModTime().Unix()
}
// Handle the path with Lstat, which examines the
// symlink itself.
//
// This second case is useful in case the symlink
// changes where it is pointing, and should handle
// the case where the symlink's target doesn't exist.
stat, err = os.Lstat(path)
if os.IsNotExist(err) {
exists = false
} else {
exists = true
if err != nil {
return
}
symlink_modtime := stat.ModTime().Unix()
if symlink_modtime > modtime {
// take the newest of the two
modtime = symlink_modtime
}
}
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 := os.Stat(time.Path)
switch {
case os.IsNotExist(err):
if time.Exists {
log_debug("Check: %s: gone", time.Path)
return checkFailed{fmt.Sprintf("File %q is missing", time.Path)}
}
case err != nil:
log_debug("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", time.Path)
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)
}