/
proc.go
433 lines (404 loc) · 10.6 KB
/
proc.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright 2015 Giulio Iotti. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"database/sql"
"encoding/hex"
"fmt"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff"
"hash"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"log"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"sync"
"time"
_ "github.com/go-sql-driver/mysql"
)
const queryInsertFile = `INSERT INTO sys_file (uid, pid, tstamp, last_indexed, missing, storage, type, metadata,
identifier, identifier_hash, folder_hash, extension, mime_type, name, sha1, size, creation_date, modification_date) VALUES
("UID","0","%d","0","0","1","%d","0","%s","%x","%x","%s","%s","%s","%x","%d","%d","%d");
`
const querySelect = `SELECT f.uid, f.tstamp, f.type, f.identifier, f.identifier_hash,
f.folder_hash, f.extension, f.mime_type, f.name, f.sha1, f.size,
m.uid, m.width, m.height
FROM sys_file f JOIN sys_file_metadata m ON f.uid=m.file;
`
const queryInsertMeta = `INSERT INTO sys_file_metadata (tstamp, crdate, file, width, height) VALUES
("%d","%d","UID","%d","%d");
`
var knownMIME = map[string]string{
"xls": "application/vnd.ms-excel",
"doc": "application/msword",
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"pps": "application/vnd.ms-powerpoint",
"ppt": "application/vnd.ms-powerpoint",
"pptm": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"ods": "application/vnd.oasis.opendocument.spreadsheet",
"odt": "application/vnd.oasis.opendocument.text",
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"xlsm": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"docm": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"7z": "application/x-7z-compressed",
"exe": "application/x-dosexec",
"mm": "application/x-freemind",
"mdb": "application/x-msaccess",
"rar": "application/x-rar",
"swf": "application/x-shockwave-flash",
"xml": "application/xml",
"wav": "audio/x-wav",
"tif": "image/tiff",
"bmp": "image/x-ms-bmp",
"rtf": "text/rtf",
"mp4": "video/mp4",
"mpg": "video/mpeg",
"mov": "video/quicktime",
"flv": "video/x-flv",
"wmv": "video/x-ms-asf",
"avi": "video/x-msvideo",
"htaccess": "text/plain",
}
type processor struct {
nproc int
delta delta
writer *writer
wg sync.WaitGroup
in <-chan file
tools chan *tools
}
type tools struct {
hash hash.Hash
buf bytes.Buffer
}
func newProcessor(useMd5 bool, in <-chan file, w *writer, n int, d delta) *processor {
p := &processor{
nproc: n,
in: in,
writer: w,
delta: d,
tools: make(chan *tools, n),
}
for i := 0; i < n; i++ {
t := &tools{buf: bytes.Buffer{}}
if useMd5 {
t.hash = md5.New()
} else {
t.hash = sha1.New()
}
p.tools <- t
}
return p
}
func (p *processor) runOne() {
defer p.wg.Done()
useDelta := len(p.delta) > 0
for f := range p.in {
var done bool
// Get one of the available tool structs
tools := <-p.tools
// Init basic data for this prop
name := f.name()
pr := newProps(tools.hash, f, name)
// If in delta mode, see if there is a cached delta entry
if useDelta {
entry := p.delta[pr.ident]
// If we have an entry and it's modtime is unchanged, use cached entry
if entry != nil && f.ModTime().Unix() == entry.mtime {
p.writer.write(fmt.Sprintf("%s\n%s\n", entry.file, entry.meta))
done = true
}
}
// Do the normal work to create a new prop then write it
if !done {
pr.load(tools.hash, name)
p.writer.write(pr.marshal(&tools.buf))
}
// Free up this tool struct for another worker
p.tools <- tools
}
}
func (p *processor) run() {
p.wg.Add(p.nproc)
for i := 0; i < p.nproc; i++ {
go p.runOne()
}
}
func (p *processor) wait() {
p.wg.Wait()
p.writer.close()
p.writer.wait()
}
type digest [sha1.Size]byte
func strhash(data string, h hash.Hash) []byte {
defer h.Reset()
if _, err := io.WriteString(h, data); err != nil {
log.Print("String write to SHA1: ", err)
return nil
}
return h.Sum(nil)
}
func filehash(name string, h hash.Hash, r io.Reader) []byte {
defer h.Reset()
if _, err := io.Copy(h, r); err != nil {
log.Print(name, ": Copy to SHA1: ", err)
return nil
}
return h.Sum(nil)
}
func guessMIME(ext string) string {
ext = strings.ToLower(ext)
if m, ok := knownMIME[ext]; ok {
return m
}
return mime.TypeByExtension(ext)
}
func sniffMIME(name string, r *os.File) string {
if _, err := r.Seek(0, 0); err != nil {
log.Print(name, ": Seek: ", err)
return ""
}
buf := make([]byte, 255)
n, err := r.Read(buf)
if n == 0 && err != nil {
log.Print(name, ": Read: ", err)
return ""
}
mimetype := http.DetectContentType(buf)
n = strings.Index(mimetype, "; ")
if n >= 0 {
mimetype = mimetype[:n]
}
return mimetype
}
// Metadata to save about a file
type props struct {
// UID or zero for unassigned
uid int
// UID of metadata, or zero for unassigned
metaUid int
// SHA1 hash of file path + name
ident digest
// SHA1 hash of directory
dident digest
// SHA1 hash of file contents
chash digest
// Full filename
fname string
// Basename
bname string
// Extension
ext string
// Directory name
dir string
// MIME type
mime string
// If image, width x height
isize image.Point
// File size in bytes
size int64
// Type of file
ftype int
// Modification time
modtime time.Time
// Creation time (of this structure)
ctime time.Time
}
func mapType(mime string) int {
n := strings.Index(mime, "/")
if n >= 0 {
mime = mime[:n]
}
switch mime {
case "text":
return 1
case "image":
return 2
case "audio":
return 3
case "video":
return 4
case "application":
return 5
}
return 0 // unknown
}
func fileExt(fname string) string {
ext := filepath.Ext(fname)
if ext != "" {
ext = ext[1:]
}
return ext
}
// Fast operations to fill props struct
func newProps(h hash.Hash, f file, name string) *props {
fname := path.Clean(name)
ext := fileExt(fname)
dir := filepath.Dir(fname)
ident := strhash(fname, h)
p := &props{
modtime: f.ModTime(),
ctime: time.Now(),
fname: fname,
ext: ext,
dir: dir,
size: f.Size(),
bname: filepath.Base(fname),
}
copy(p.ident[:], ident)
return p
}
// Slower operations to fill props struct
func (p *props) load(h hash.Hash, name string) {
// Empty files always have this special MIME type
if p.size == 0 {
p.mime = "inode/x-empty"
} else {
p.mime = guessMIME(p.ext)
}
r, err := os.Open(name)
if err != nil {
log.Print(name, ": Props: ", err)
return
}
defer r.Close()
// If the extension is empty, we need to detect
// the MIME type via file contents
if p.mime == "" {
p.mime = sniffMIME(name, r)
}
p.ftype = mapType(p.mime)
if _, err := r.Seek(0, 0); err != nil {
log.Print(name, ": Seek: ", err)
return
}
// TODO: this is quite unreadable
copy(p.chash[:], filehash(name, h, r))
copy(p.dident[:], strhash(p.dir, h))
// Non-images are completely processed at this point
if !strings.HasPrefix(p.mime, "image/") {
return
}
// Image-specific processing
if _, err := r.Seek(0, 0); err != nil {
log.Print(name, ": Seek: ", err)
return
}
imgconf, _, err := image.DecodeConfig(r)
if err != nil {
log.Print(name, ": Image decoder: ", err)
return
}
p.isize = image.Point{imgconf.Width, imgconf.Height}
}
func escape(s string) string {
return strings.Replace(s, `"`, `\"`, -1)
}
func (p *props) marshal(w *bytes.Buffer) string {
defer w.Reset()
switch true {
case *singleMode:
p.writeSingle(w)
case *sqlMode:
p.writeSQL(w)
default:
p.writeNormal(w)
}
return w.String()
}
// Single mode writes a single condensed line. Used for debugging comparison with tester/tester.
func (p *props) writeSingle(w io.Writer) {
fmt.Fprintf(w, `"0","0","1","%d","0","%s",`, p.ftype, escape(p.fname))
fmt.Fprintf(w, `"%x","%x",`, p.ident, p.dident)
fmt.Fprintf(w, `"%s","%s","%s",`, p.ext, p.mime, escape(p.bname))
fmt.Fprintf(w, `"%x",`, p.chash)
fmt.Fprintf(w, "\"%d\",\"%d\",\"%d\"\n", p.size, p.isize.X, p.isize.Y)
}
func (p *props) writeSQL(w io.Writer) {
fmt.Fprintf(w, queryInsertFile, p.ctime.Unix(), p.ftype, escape(p.fname),
p.ident, p.dident, p.ext, p.mime, escape(p.bname), p.chash, p.size,
p.ctime.Unix(), p.modtime.Unix())
fmt.Fprintf(w, queryInsertMeta, p.modtime.Unix(), p.ctime.Unix(), p.isize.X, p.isize.Y)
}
func (p *props) writeNormal(w io.Writer) {
uid := "UID"
if p.uid != 0 {
uid = fmt.Sprintf("%d", p.uid)
}
metaUid := "UID"
if p.metaUid != 0 {
metaUid = fmt.Sprintf("%d", p.metaUid)
}
// Write file entry
fmt.Fprintf(w, `file:"%s","0","%d","0","0","1","%d","0","`, uid, p.ctime.Unix(), p.ftype)
io.WriteString(w, escape(p.fname))
fmt.Fprintf(w, `","%x","%x",`, p.ident, p.dident)
fmt.Fprintf(w, `"%s","%s","`, p.ext, p.mime)
io.WriteString(w, escape(p.bname))
fmt.Fprintf(w, `","%x","%d",`, p.chash, p.size)
fmt.Fprintf(w, "\"%d\",\"%d\"\n", p.ctime.Unix(), p.modtime.Unix())
// Write metadata
fmt.Fprintf(w, `meta:"%s","0","%d","%d","0","0","0","",`, metaUid, p.modtime.Unix(), p.ctime.Unix())
io.WriteString(w, `"0","0","0","","0","0","0","0","0","0",`)
fmt.Fprintf(w, `"%s","","%d","%d",`, uid, p.isize.X, p.isize.Y)
io.WriteString(w, "\"\",\"\",\"0\"\n")
}
func dumpDatabase(dsn string, w io.Writer) error {
db, err := sql.Open("mysql", dsn)
if err != nil {
return fmt.Errorf("cannot open DB: %s", err)
}
rows, err := db.Query(querySelect)
if err != nil {
return fmt.Errorf("cannot execute query: %s", err)
}
defer rows.Close()
p := &props{}
for rows.Next() {
var (
tstamp int64
ident string
dident string
chash string
)
if err := rows.Scan(&p.uid, &tstamp, &p.ftype, &p.fname, &ident,
&dident, &p.ext, &p.mime, &p.bname, &chash, &p.size,
&p.metaUid, &p.isize.X, &p.isize.Y); err != nil {
return fmt.Errorf("reading row failed: %s", err)
}
// Adapt some fields to internal representation. Quite wasteful, but OK for now.
p.ctime = time.Unix(tstamp, 0)
p.modtime = p.ctime
hash, err := hex.DecodeString(ident)
if err != nil {
return fmt.Errorf("parsing %s: %s", ident, err)
}
copy(p.ident[:], hash)
hash, err = hex.DecodeString(dident)
if err != nil {
return fmt.Errorf("parsing %s: %s", dident, err)
}
copy(p.dident[:], hash)
hash, err = hex.DecodeString(chash)
if err != nil {
return fmt.Errorf("parsing %s: %s", chash, err)
}
copy(p.chash[:], hash)
// Write the CSV normal double entry
p.writeNormal(w)
}
return nil
}