-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (179 loc) · 4.42 KB
/
main.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
package main
import (
"crypto/md5"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
_ "modernc.org/sqlite"
"net/smtp"
"os"
"path/filepath"
"sort"
"strings"
"sync"
)
type HashResult struct {
FilePath string
Hash string
}
func main() {
if len(os.Args) < 3 {
programName := os.Args[0]
fmt.Printf("Usage: %s database_path root_directory email", programName)
return
}
databasePath := os.Args[1]
rootDirectory := os.Args[2]
files, err := os.ReadDir(rootDirectory)
SortFileSizeDescend(files)
if err != nil {
log.Fatalf("Error reading the specified directory: %v", err)
}
db, err := sql.Open("sqlite", databasePath)
if err != nil {
log.Fatalf("Error opening database: %v", err)
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Fatalf("Error closing the database: %v", err)
}
}(db)
// Create the "file_hashes" table if it doesn't exist.
createTableStmt := `
CREATE TABLE IF NOT EXISTS file_hashes (
filename TEXT PRIMARY KEY,
hash TEXT
);
`
_, err = db.Exec(createTableStmt)
if err != nil {
log.Fatalf("Error creating table: %v", err)
}
fileCh := make(chan string)
hashCh := make(chan HashResult)
var wg sync.WaitGroup
numWorkers := 8
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for filePath := range fileCh {
// Compute the MD5 hash of the file.
hash, err := computeFileMD5Hash(filePath)
if err != nil {
log.Printf("Error computing MD5 hash for %s: %v", filePath, err)
continue
}
hashCh <- HashResult{FilePath: filePath, Hash: hash}
}
}()
}
go func() {
for _, file := range files {
if !file.IsDir() {
fileCh <- filepath.Join(rootDirectory, file.Name())
}
}
close(fileCh)
wg.Wait()
close(hashCh)
}()
var hashError = false
var hashNew = false
var hashLogs = ""
var hashSuccess = 0
for result := range hashCh {
var dbHash string
err = db.QueryRow("SELECT hash FROM file_hashes WHERE filename = ?", result.FilePath).Scan(&dbHash)
if errors.Is(sql.ErrNoRows, err) {
// File is not in the database; insert it.
_, err = db.Exec("INSERT INTO file_hashes (filename, hash) VALUES (?, ?)", result.FilePath, result.Hash)
if err != nil {
hashLogs += fmt.Sprintf("Error inserting MD5 hash for %s: %v\n", result.FilePath, err)
hashError = true
} else {
hashLogs += fmt.Sprintf("Inserted MD5 hash for %s: %s\n", result.FilePath, result.Hash)
hashNew = true
}
} else if err != nil {
hashLogs += fmt.Sprintf("Error querying MD5 hash for %s: %v\n", result.FilePath, err)
hashError = true
} else if result.Hash != dbHash {
hashLogs += fmt.Sprintf("MD5 hash mismatch for %s: stored=%s, computed=%s\n", result.FilePath, dbHash, result.Hash)
hashError = true
} else {
hashSuccess++
fmt.Printf("MD5 hash match for %s: computed=%s\n", result.FilePath, dbHash)
}
}
hashLogs += fmt.Sprintf("%d files have passed the integrity tests\n", hashSuccess)
fmt.Print(hashLogs)
if len(os.Args) > 3 {
dest := os.Args[3]
subject := ""
if hashError {
subject = "Error detected while verifying integrity"
} else if hashNew {
subject = "New files found in the database"
} else {
subject = "Integrity check successful"
}
sendEmail(dest, subject, hashLogs)
}
}
func SortFileSizeDescend(files []os.DirEntry) {
sort.Slice(files, func(i, j int) bool {
info1, err := files[i].Info()
if err != nil {
log.Fatal(err)
}
info2, err := files[j].Info()
if err != nil {
log.Fatal(err)
}
return info1.Size() > info2.Size()
})
}
func computeFileMD5Hash(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatalf("Error closing the file: %v", err)
}
}(file)
hash := md5.New()
_, err = io.Copy(hash, file)
if err != nil {
return "", err
}
hashBytes := hash.Sum(nil)
hashStr := hex.EncodeToString(hashBytes)
return strings.ToLower(hashStr), nil
}
func sendEmail(dest string, subject string, body string) {
from := From
password := Password
to := []string{
dest,
}
smtpHost := "smtp.gmail.com"
smtpPort := "587"
message := []byte("From: " + from + "\n" +
"To: " + dest + "\n" +
"Subject: " + subject + "\n\n" +
body + "\n")
auth := smtp.PlainAuth("", from, password, smtpHost)
err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message)
if err != nil {
fmt.Println(err)
return
}
}