-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrity.go
95 lines (82 loc) · 2.18 KB
/
integrity.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
package main
type IntegrityChangesAdded struct {
FilePath FilePath
FileHash FileHash
}
type IntegrityChangesModified struct {
FilePath FilePath
NewFileHash FileHash
OldFileHash FileHash
}
type IntegrityChangesRemoved struct {
FilePath FilePath
FileHash FileHash
}
type IntegrityChanges struct {
Added []IntegrityChangesAdded
Modified []IntegrityChangesModified
Removed []IntegrityChangesRemoved
}
func CheckIntegrity(dbPath string, checkPaths []string) (IntegrityChanges, error) {
//// Setup a new database
newDB, err := SetupFileHashDatabase(checkPaths)
if err != nil {
return IntegrityChanges{}, err
}
//// Load old hash database
oldDB, oldDBError := LoadFileHashDatabase(dbPath)
//// Compare the hashes
integrityChanges := IntegrityChanges{
Added: []IntegrityChangesAdded{},
Modified: []IntegrityChangesModified{},
Removed: []IntegrityChangesRemoved{},
}
if oldDBError == nil {
//// Database did exist before
// Check for added and modified files
for filePath, fileHash := range newDB {
oldDBFileHash, ok := oldDB[filePath]
if !ok {
// New file
integrityChanges.Added = append(integrityChanges.Added, IntegrityChangesAdded{
FilePath: filePath,
FileHash: fileHash,
})
} else if fileHash != oldDBFileHash {
// Modified file
integrityChanges.Modified = append(integrityChanges.Modified, IntegrityChangesModified{
FilePath: filePath,
OldFileHash: oldDBFileHash,
NewFileHash: fileHash,
})
}
}
// Check for removed filed
for filePath, fileHash := range oldDB {
_, ok := newDB[filePath]
if !ok {
// Removed file
integrityChanges.Removed = append(integrityChanges.Removed, IntegrityChangesRemoved{
FilePath: filePath,
FileHash: fileHash,
})
}
}
} else {
//// Database did not exist before
// Check for added files
for filePath, fileHash := range newDB {
// New file
integrityChanges.Added = append(integrityChanges.Added, IntegrityChangesAdded{
FilePath: filePath,
FileHash: fileHash,
})
}
}
//// Save new hash database
err = SaveFileHashDatabase(newDB, dbPath)
if err != nil {
return IntegrityChanges{}, err
}
return integrityChanges, nil
}