This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_scanner.go
93 lines (79 loc) · 2.24 KB
/
file_scanner.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
// Copyright 2018 IBM Corporation
// Licensed under the Apache License, Version 2.0. See LICENSE file.
package scanner
import (
"io"
"os"
pb "gopkg.in/cheggaaa/pb.v1"
"github.com/IBM/binprint/record"
)
// IsScannablePath returns true if the name matches an existing path that looks like something we can scan.
func IsScannablePath(name string, stat os.FileInfo) bool {
var err error
if stat == nil {
stat, err = os.Stat(name)
}
return err == nil && (stat.IsDir() || stat.Mode().IsRegular())
}
// var myFingerprint *binprint.Fingerprint
// func init() {
// fileName, err := os.Executable()
// if err != nil {
// return
// }
// // IdentifyFile scans the specified file and returns a binprint.File record of it. The result may be cached, based on the inode, mtime, and size of the file.
// stat, err := os.Stat(fileName)
// if err != nil {
// panic(err)
// }
// myFingerprint = new(binprint.Fingerprint)
// f, err := os.Open(fileName)
// if err != nil {
// panic(err)
// }
// defer f.Close()
// calcMissingSums(myFingerprint, f, stat.Size())
// }
// IdentifyFileWithStat returns digest results for the provided file, either from
// cache or by scanning the file
func IdentifyFileWithStat(fileName string, stat os.FileInfo, bar *pb.ProgressBar) *record.File {
cfp := cache.GetStatFingerprint(stat)
if cfp != nil {
// Cache hit!
if bar != nil {
bar.Add64(stat.Size())
}
return cache.PutFile(&record.File{
Path: fileName,
Fingerprint: cache.PutFingerprint(cfp),
})
}
var result record.Fingerprint
f, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer f.Close()
var fReader io.Reader
if bar != nil {
fReader = bar.NewProxyReader(f)
} else {
fReader = f
}
result.CalculateSums(fReader, stat.Size())
fp := cache.PutFingerprint(&result)
cache.PutStatFingerprint(stat, fp)
fileRes := cache.PutFile(&record.File{
Path: fileName,
Fingerprint: fp,
})
return fileRes
}
// IdentifyFile scans the specified file and returns a binprint.File record of it. The result may be cached, based on the inode, mtime, and size of the file.
func IdentifyFile(fileName string) *record.File {
stat, err := os.Stat(fileName)
if err != nil {
panic(err)
}
return IdentifyFileWithStat(fileName, stat, nil)
}