-
Notifications
You must be signed in to change notification settings - Fork 1
/
dirSign.go
198 lines (187 loc) · 4.46 KB
/
dirSign.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
package hq
import (
"io"
"os"
"strconv"
"sync"
"time"
)
// dirSign ...
func (c *Config) dirSign() bool {
id := NewHQ(c)
id.IO.DirName = c.FileName
id.IO.MapClean = isMapClean()
id.IO.TSS = strconv.FormatInt(id.IO.Start.Unix(), 10)
id.IO.Silent = c.Silent
id.IO.FileName = c.FileName
switch {
case id.IO.FileName == ".":
id.IO.FileName = ".hqMAP." + id.IO.TSS + "." + unix2RFC3339(id.IO.TSS) + _compressedFileExt
default:
id.IO.FileName = id.IO.FileName + "/" + ".hqMAP." + id.IO.TSS + "." + unix2RFC3339(id.IO.TSS) + _compressedFileExt
}
// defaults
var (
sepone = []byte("\n")
septwo = []byte("\n\n")
waitWorkerDone sync.WaitGroup
)
// setup channel struct
type obj struct {
filename []byte
hash []byte
chash []byte
code bool
}
// setup channel & wait groups
waitWorkerDone.Add(id.IO.CPU)
chanOut := make(chan obj, 100)
chanFeed := make(chan string, 10000)
chanCount := make(chan uint64, 1)
chanEnd := make(chan time.Time, 1)
// lauch global master control process
go func() {
waitWorkerDone.Wait()
close(chanOut)
}()
// collect chanOut -> data slice & write as compressed map, report, sign
go func() {
var (
data []byte
total uint64
)
for t := range chanOut {
data = append(data, []byte(t.filename)...)
data = append(data, []byte(t.hash)...)
if t.code {
data = append(data, []byte(t.chash)...)
}
total++
}
compressWriteFile(id.IO.FileName, data, _compressedMapLevel, 0o660)
chanCount <- total
close(chanCount)
chanEnd <- time.Now()
close(chanEnd)
}()
// start case specific hash worker group
switch c.CodeReview {
case true:
for i := 0; i < id.IO.CPU; i++ {
go func() {
var chash []byte
var code bool
for t := range chanFeed {
file, _ := os.Open(t)
reader, hash := io.Reader(file), blake3New256()
for {
block := make([]byte, _hashBlockSize)
l, _ := reader.Read(block)
if l < _hashBlockSize {
hash.Write(block)
break
}
hash.Write(block)
}
file.Close()
h := hash.Sum(nil)
code, chash = codeReviewHash(t)
switch code {
case true:
if chash == nil {
continue
}
chanOut <- obj{
filename: append([]byte(t), sepone...),
hash: append(s2hex(h[:]), sepone...),
chash: append(chash, septwo...),
code: true,
}
case false:
chanOut <- obj{
filename: append([]byte(t), sepone...),
hash: append(s2hex(h[:]), septwo...),
code: false,
}
}
}
waitWorkerDone.Done()
}()
}
case false:
for i := 0; i < id.IO.CPU; i++ {
go func() {
for t := range chanFeed {
file, _ := os.Open(t)
reader, hash := io.Reader(file), blake3New256()
for {
block := make([]byte, _hashBlockSize)
l, _ := reader.Read(block)
if l < _hashBlockSize {
hash.Write(block)
break
}
hash.Write(block)
}
file.Close()
h := hash.Sum(nil)
chanOut <- obj{
filename: append([]byte(t), sepone...),
hash: append(s2hex(h[:]), septwo...),
}
}
waitWorkerDone.Done()
}()
}
}
// feeder [fastwalk]
go func() {
path := id.IO.DirName
dirlist, err := os.ReadDir(path)
if err != nil {
errExit("unable to read directory [" + path + "] [" + err.Error() + "]")
}
for _, item := range dirlist {
name := fixPath(path) + item.Name()
inodeType := uint32(item.Type())
switch {
case inodeType&_modeSymlink != 0:
chanFeed <- name // profile symbolic links - but do not [recursive] follow
case inodeType&_modeDir != 0:
walk(name, chanFeed)
case id.IO.MapClean && len(name) > 39 && name[:6] == ".hqMAP":
continue
default:
chanFeed <- name
}
}
if id.IO.MapClean {
cleanMapFiles(id.IO.DirName)
}
close(chanFeed)
}()
// prep sign
if !c.MapOnly {
// now, everything is busy in the background, time to keep the user busy as well
// ask for creds and compute hash cube in parallel (b/c hasher thread can be IO limited)
id.IO.ReportValid = false
id.readPublicKey("me")
id.passEntry("pending hqMAP sign operation [" + id.IO.DirName + "]")
id.unlockHQ()
}
// wait till all threads done, report, sign
id.IO.FilesTotal = <-chanCount
id.IO.ReportValid = false
id.IO.End = <-chanEnd
id.reportDir()
if c.MapOnly {
return true
}
id.IO.MSG = getMSGHash(id.IO.FileName)
id.IO.End = time.Time{}
id.IO.Start = time.Now()
id.genSig()
id.report()
id.writeSig()
return true
}