-
Notifications
You must be signed in to change notification settings - Fork 1
/
path.go
executable file
·227 lines (179 loc) · 5.36 KB
/
path.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package pfinternal
import (
"os"
"io"
"fmt"
"path"
"hash"
"io/ioutil"
)
const (
PathListBatchSize = 3
)
type Path struct {
hashAlgorithm *string
reportingChannel chan<- *ChangeEvent
}
func NewPath(hashAlgorithm *string, reportingChannel chan<- *ChangeEvent) *Path {
p := Path {
hashAlgorithm: hashAlgorithm,
reportingChannel: reportingChannel,
}
return &p
}
func (self *Path) getHashObject() (h hash.Hash, err error) {
l := NewLogger("path")
defer func() {
if r := recover(); r != nil {
h = nil
err = r.(error)
l.Error("Could not get hash object", "err", err)
}
}()
h, err = getHashObject(self.hashAlgorithm)
if err != nil {
panic(err)
}
return h, nil
}
// Generate a hash for a path.
func (self *Path) GeneratePathHash(scanPath *string, relPath *string, existingCatalog *Catalog) (hash string, err error) {
l := NewLogger("path")
defer func() {
if r := recover(); r != nil {
hash = ""
err = r.(error)
l.Error("Could not generate path hash", "err", err)
}
}()
l.Debug("Generating hash for PATH.", "relPath", *relPath)
h, err := self.getHashObject()
if err != nil {
panic(err)
}
// We need this list to be sorted (read: complete) in order to produce
// deterministic results.
entries, err := ioutil.ReadDir(*scanPath)
if err != nil {
panic(err)
}
h, err = self.getHashObject()
if err != nil {
panic(err)
}
for _, entry := range entries {
var childHash string = ""
filename := entry.Name()
childPath := path.Join(*scanPath, filename)
relChildPath := path.Join(*relPath, filename)
mode := entry.Mode()
if mode.IsDir() == true {
l.Debug("Hashing directory.", "relChildPath", relChildPath)
bc, err := existingCatalog.BranchCatalog(&filename)
if err != nil {
panic(err)
}
childHash, err = self.GeneratePathHash(&childPath, &relChildPath, bc)
if err != nil {
panic(err)
}
} else if mode.IsRegular() == true {
l.Debug("Hashing regular file.", "relChildPath", relChildPath)
s, err := os.Stat(childPath)
if err != nil {
panic(err)
}
mtime := s.ModTime().Unix()
flr, err := existingCatalog.lookupFile(&filename)
if err != nil {
panic(err)
} else if flr.wasFound == false || flr.entry.mtime != mtime {
childHash, err = self.GenerateFileHash(&childPath)
if err != nil {
panic(err)
}
if flr.wasFound == false || childHash != flr.entry.hash {
// TODO(dustin): !! How do we or should we emit update events for paths?
err = existingCatalog.setFile(flr, mtime, &childHash)
if err != nil {
panic(err)
}
}
} else {
childHash = flr.entry.hash
}
} else if mode & os.ModeSymlink > 0 {
l.Debug("Hashing symlink.", "relChildPath", relChildPath)
targetFilepath, err := os.Readlink(childPath)
if err != nil {
panic(err)
}
childHash, err = getHash(h, &targetFilepath)
if err != nil {
panic(err)
}
} else {
l.Warn("Skipping file of unacceptable type.",
"relChildPath", relChildPath,
"mode", mode)
continue
}
io.WriteString(h, relChildPath)
io.WriteString(h, "\000")
io.WriteString(h, childHash)
io.WriteString(h, "\000")
}
hash = fmt.Sprintf("%x", h.Sum(nil))
l.Debug("Calculated PATH hash.",
"relPath", *relPath,
"hash", hash)
// TODO(dustin): !! How do we or should we emit update events for paths?
lhp := existingCatalog.getLastHash()
if lhp == nil || *lhp != hash {
err = existingCatalog.updatePath(&hash)
if err != nil {
panic(err)
}
}
return hash, nil
}
func (self *Path) GenerateFileHash(filepath *string) (hash string, err error) {
l := NewLogger("path")
defer func() {
if r := recover(); r != nil {
hash = ""
err = r.(error)
l.Error("Could not generate hash", "err", err)
}
}()
l.Debug("Generating hash for FILEPATH.", "filepath", *filepath)
f, err := os.Open(*filepath)
if err != nil {
panic(err)
}
defer func() {
l.Debug("Closing file (generated hash).", "filepath", *filepath)
f.Close()
}()
h, err := self.getHashObject()
if err != nil {
panic(err)
}
part := make([]byte, h.BlockSize() * 2)
for {
_, err := f.Read(part)
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
if _, err := h.Write(part); err != nil {
panic(err)
}
}
hash = fmt.Sprintf("%x", h.Sum(nil))
l.Debug("Calculated FILE hash.",
"hash", hash,
"filepath", *filepath)
return hash, nil
}