-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.go
147 lines (122 loc) · 3.06 KB
/
process.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
package filedriller
import (
"encoding/hex"
"github.com/djherbis/times"
"io/fs"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/cheggaaa/pb/v3"
"github.com/gomodule/redigo/redis"
"github.com/richardlehane/siegfried"
)
// CreateFileList creates a list of file paths and a directory listing
func CreateFileList(rootDir string) ([]string, []string) {
var fileList []string
var dirList []string
err := filepath.WalkDir(rootDir, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
fileList = append(fileList, path)
} else if info.IsDir() {
dirList = append(dirList, path)
}
return nil
})
if err != nil {
ErrorLogger.Println(err)
}
return fileList, dirList
}
// IdentifyDirs reads metadata from the filesystem
func IdentifyFSInfo(entryList []string) {
for _, entry := range entryList {
fdinfo, err := os.Stat(entry)
if err != nil {
log.Println(err)
}
// debug
println(fdinfo.Mode().Perm())
//println(fdinfo.ModTime())
// end debug
}
}
// IdentifyFiles creates metadata with siegfried and hashsum
func IdentifyFiles(fileList []string, hashDigest string, nsrlEnabled bool, conn redis.Conn, entroEnabled bool) []string {
var resultList []string
s, err := siegfried.Load("pronom.sig")
if err != nil {
e(err)
}
var calcNSRL bool
if hashDigest != "sha1" {
calcNSRL = true
}
var entroFile float64
showBar := true
if len(fileList) < 2 {
showBar = false
}
bar := pb.New(len(fileList))
if showBar {
bar.Start()
}
for _, filePath := range fileList {
successful, oneFileResult := siegfriedIdent(s, filePath)
if !successful {
ErrorLogger.Println(oneFileResult)
continue
}
onefilehash := hex.EncodeToString(Hashit(filePath, hashDigest))
oneFile := oneFileResult + ",\"" + onefilehash + "\",\"" + CreateUUID() + "\","
// get atime,mtime and ctime from files
t, err := times.Stat(filePath)
if err != nil {
ErrorLogger.Println(oneFileResult)
}
oneFile = oneFile + t.AccessTime().String() + ","
oneFile = oneFile + t.ModTime().String() + ","
if t.HasChangeTime() {
oneFile = oneFile + t.ChangeTime().String() + ","
} else {
oneFile = oneFile + ","
}
if t.HasBirthTime() {
oneFile = oneFile + t.BirthTime().String() + ","
} else {
oneFile = oneFile + ","
}
// we need a sha1 for redis. if sha1 is not used in this run we
// need to calculate sha1 for redis if nsrl is enabled
if nsrlEnabled {
var nsrlHash string
if calcNSRL {
nsrlHash = hex.EncodeToString(Hashit(filePath, "sha1"))
} else {
nsrlHash = onefilehash
}
inNSRL := RedisGet(conn, strings.ToUpper(nsrlHash))
oneFile = oneFile + inNSRL
}
if entroEnabled {
entroFile, err = entropy(filePath)
if err == nil {
oneFile = oneFile + ",\"" + strconv.FormatFloat(entroFile, 'E', -1, 32) + "\""
} else {
oneFile = oneFile + ",\"" + "ERROR calculating entropy" + "\""
}
} else {
oneFile = oneFile + ","
}
resultList = append(resultList, oneFile)
if showBar {
bar.Increment()
}
}
bar.Finish()
return resultList
}