/
mdr_dirNode.go
304 lines (282 loc) · 6.75 KB
/
mdr_dirNode.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// DirNode.go (c) 2014 David Rook - all rights reserved
package mdr
import (
// standard lib go 1.2 pkgs
"crypto/sha256"
"fmt"
"hash"
"log"
"os"
"sort"
)
const (
unused_constant = false
G_datetimeFmt = "2006-01-02:15_04_05"
)
type FileRec struct {
R Rec256
// fname string // needed to sort on
Touched bool
}
func (fr FileRec) Dump() {
fr.R.Dump()
fmt.Printf("touched = %v\n", fr.Touched)
}
type DirNode struct {
Pathname string
IsSortedByName bool
Files []FileRec
Size int64
SHA256 string
}
type ByName []FileRec
func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool { return a[i].R.Name < a[j].R.Name }
// Finalize computes the total size and SHA256 of the leaf
func (d *DirNode) Finalize() (string, int64) {
d.Size = 0
d.SortFilesByName()
var h hash.Hash = sha256.New()
for i := 0; i < len(d.Files); i++ {
d.Size += d.Files[i].R.Size
h.Write([]byte(d.Files[i].R.SHA))
}
digest := h.Sum(nil)
rv := ""
for _, hex := range digest {
rv = rv + fmt.Sprintf("%02x", hex)
}
d.SHA256 = rv
return rv, d.Size
}
func (d *DirNode) UnTouchAll() {
for i := 0; i < len(d.Files); i++ {
d.Files[i].Touched = false
}
}
func (d *DirNode) AddFile(f FileRec) {
//Verbose.Printf("Adding file %s to %d others\n", f.r.Name, len(d.files))
d.Files = append(d.Files, f)
flen := len(d.Files)
if flen == 1 {
d.IsSortedByName = true
return
}
// remains sorted if addition is > last element
if d.IsSortedByName && (d.Files[flen-2].R.Name < d.Files[flen-1].R.Name) {
d.IsSortedByName = true
} else {
d.IsSortedByName = false
}
}
func (d *DirNode) SortFilesByName() {
//Verbose.Printf("SortFilesByName in %s\n", d.pathname)
if len(d.Files) <= 1 {
d.IsSortedByName = true
return
}
// magic happens here
//fmt.Printf("Before sort\n")
//d.Dump()
sort.Sort(ByName(d.Files))
//if sort.IsSorted(ByName(d.files)) == false {
// log.Fatalf("houston we have a problem\n")
//}
//fmt.Printf("After sort\n")
d.IsSortedByName = true
//d.Dump()
}
func (d *DirNode) DeleteFile(fname string) {
//Verbose.Printf("Deleting %s\n", fname)
log.Fatalf("here")
// it is an error to delete a file that's not in list
ndx := d.IndexOf(fname)
if ndx < 0 {
d.Dump()
log.Fatalf("!Err---> cant find file %s to delete it\n", fname)
}
// otherwise we found it somewhere
if len(d.Files) == 1 { // special case if it's the only file
d.Files = []FileRec{}
d.IsSortedByName = false // calling zero length unsorted
return
}
if ndx == 0 { // first one is a special case, doesn't change sorted status
d.Files = d.Files[1:]
return
}
// if its the last one thats a special case, doesn't change sorted status
lastNdx := len(d.Files) - 1
if ndx == lastNdx {
d.Files = d.Files[:lastNdx-1] // truncate the list
return
}
// if possible concat slices and preserve sorted status (expensive but cheaper than a resort)
// otherwise swap current and last item and truncate the list
if d.IsSortedByName {
d.Files = append(d.Files[:ndx], d.Files[ndx+1:]...)
} else {
d.Files[ndx] = d.Files[lastNdx]
d.Files = d.Files[:lastNdx-1]
}
}
// brute force works
func (d *DirNode) IndexOfBrute(fname string) int {
for ndx, f := range d.Files {
//if ndx > 0 {
// if d.files[ndx].r.Name < d.files[ndx-1].r.Name {
// fmt.Printf("+++ WARNING +++ file not sorted\n")
// }
//}
if f.R.Name == fname {
return ndx
}
}
return -1
}
func (d *DirNode) IsSortedProperly() bool {
flen := len(d.Files)
if flen <= 1 {
return true
}
for i := 1; i < len(d.Files)-1; i++ {
if d.Files[i].R.Name < d.Files[i-1].R.Name {
fmt.Printf("out of order %s %s\n",
d.Files[i].R.Name,
d.Files[i-1].R.Name)
log.Panicf("crashed here")
}
}
return true
}
// special case speedup if we check last returned value +1 first?
// better handles case where we're stepping through list ???
// Returns -1 if not found
func (d *DirNode) IndexOf(fname string) int {
//return d.IndexOfBrute(fname)
if d.IsSortedByName == false {
d.SortFilesByName()
}
// use binary search here
length := len(d.Files)
if length == 0 {
return -1
}
if length == 1 {
if fname == d.Files[0].R.Name {
return 0
} else {
return -1
}
}
lo := 0
hi := length - 1
var loopCt int
foundAt := -1
for {
/*
if paranoid {
if d.files[lo].r.Name > d.files[hi].r.Name {
d.Dump()
fmt.Printf("g_dirMap has %d items\n", len(g_dirMap))
fmt.Printf("refreshed[%d] errs[%d] ok[%d] added[%d]\n",
g_refreshCount, g_errCount, g_okCount, g_addCount)
fmt.Printf("sort called %d times\n", g_sortCt)
fmt.Printf("lo[%d]%s > hi[%d]%s\n",
lo, d.files[lo].r.Name, hi, d.files[hi].r.Name)
fmt.Printf("lo value > hi value so\n")
log.Panicf("something is broken in DirNode's binary search")
}
}
*/
if fname < d.Files[lo].R.Name || fname > d.Files[hi].R.Name {
// not found
return -1
}
mid := (hi + lo) / 2
//Verbose.Printf("lo[%d] hi[%d] mid = %d loopct=%d\n", lo, hi, mid, loopCt)
if fname == d.Files[lo].R.Name {
foundAt = lo
break
}
if fname == d.Files[hi].R.Name {
foundAt = hi
break
}
if fname == d.Files[mid].R.Name {
foundAt = mid
break
}
if fname < d.Files[lo].R.Name { // not found
return -1
}
if fname > d.Files[hi].R.Name { // not found
return -1
}
if (hi - lo) < 2 {
break
}
if true {
loopCt++
if loopCt > 64 { // implies > 2^64 elements in list
fmt.Printf("lo[%d] hi[%d]\n", lo, hi)
log.Panicf("something is broken in DirNode's binary search")
}
}
if fname < d.Files[mid].R.Name {
hi = mid
continue
}
if fname > d.Files[mid].R.Name {
lo = mid
continue
}
}
return foundAt
}
// calls AddFile if file not currently in DirNode
// name should be just the base part of path
func (d *DirNode) Update(fname string) error {
// BUG(mdr): ? be paranoid and check if we have any slashes in fname?
var fr FileRec
fullPath := d.Pathname + fname
info, err := os.Stat(fullPath)
if err != nil {
fmt.Printf("!Err---> %v\n", err)
return err
}
fr.R.Size = info.Size()
fr.R.Date = info.ModTime().Format(G_datetimeFmt)
tmpsha, err := FileSHA256(fullPath)
if err != nil {
fmt.Printf("!Err---> %v\n", err)
return err
}
fr.R.SHA = tmpsha
fr.R.Name = fname
ndx := d.IndexOf(fname)
if ndx >= 0 {
d.Files[ndx].R = fr.R
} else {
d.AddFile(fr)
}
//Verbose.Printf("Updated %s\n", fullPath)
return nil
}
func (d *DirNode) Dump() {
var sortStatus string
if d.IsSortedByName {
sortStatus = "is"
} else {
sortStatus = "is NOT"
}
fmt.Printf("Dir path = %s %s sorted\n", d.Pathname, sortStatus)
// if d.isSortedByName == false {
// d.SortFilesByName()
// }
for i := 0; i < len(d.Files)-1; i++ {
fmt.Printf("%d %s\n", i, d.Files[i].R.Name)
d.Files[i].Dump()
}
}