Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (154 loc) · 5.51 KB
/
Copy pathmain.cpp
File metadata and controls
189 lines (154 loc) · 5.51 KB
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
#define _SILENCE_CXX20_U8PATH_DEPRECATION_WARNING
#include <iostream>
#include <filesystem>
#include <string>
#include <chrono>
#include "hash.h"
#include "scan.h"
#include "data.h"
#include "verify.h"
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "usage: hashguard <command> <path>\n";
std::cout << "commands: hash, scan, init, verify, help\n";
return 0;
}
std::string command = argv[1];
if (command == "help" || command == "--help")
{
std::cout << "hashguard commands:\n\n";
std::cout << "hash <file> calculate sha256\n";
std::cout << "scan <directory> scan all files\n";
std::cout << "init <directory> create baseline\n";
std::cout << "verify <directory> verify files\n";
return 0;
}
if (command != "hash" &&
command != "scan" &&
command != "init" &&
command != "verify")
{
std::cout << "invalid command\n";
return 0;
}
if (argc != 3)
{
std::cout << "invalid arguments\n";
std::cout << "usage: hashguard <command> <path>\n";
return 0;
}
try
{
std::filesystem::path filePath =
std::filesystem::u8path(argv[2]);
auto start = std::chrono::steady_clock::now();
if (command == "hash")
{
if (!std::filesystem::is_regular_file(filePath))
{
std::cout << "file not found\n";
return 0;
}
std::string hash = HashGuard::CalculateSHA256(filePath);
std::cout << "hash -> " << hash << "\n";
}
else if (command == "scan")
{
if (!std::filesystem::is_directory(filePath))
{
std::cout << "directory not found\n";
return 0;
}
HashGuard::ScanResult result =
HashGuard::ScanDirectory(filePath);
for (const auto& file : result.files)
{
std::cout << "\nfile -> " << file.path.string() << "\n";
std::cout << "size -> " << file.size << " bytes\n";
std::cout << "hash -> " << file.sha256 << "\n";
}
for (const auto& error : result.errors)
{
std::cout << "\nerror -> " << error.path.string() << "\n";
std::cout << "reason -> " << error.message << "\n";
}
std::cout << "\nscan completed\n";
std::cout << "files -> " << result.files.size() << "\n";
std::cout << "errors -> " << result.errors.size() << "\n";
}
else if (command == "init")
{
if (!std::filesystem::is_directory(filePath))
{
std::cout << "directory not found\n";
return 0;
}
std::filesystem::path outputPath =
std::filesystem::absolute(filePath).parent_path() / "data.json";
if (std::filesystem::exists(outputPath))
{
std::cout << "data.json already exists\n";
return 0;
}
HashGuard::ScanResult result =
HashGuard::ScanDirectory(filePath);
if (HashGuard::SaveBaseline(result, outputPath))
{
std::cout << "baseline created\n";
std::cout << "files -> " << result.files.size() << "\n";
std::cout << "saved -> " << outputPath.string() << "\n";
}
else
{
std::cout << "baseline failed\n";
return 0;
}
}
else if (command == "verify")
{
if (!std::filesystem::is_directory(filePath))
{
std::cout << "directory not found\n";
return 0;
}
std::filesystem::path dataPath =
std::filesystem::absolute(filePath).parent_path() / "data.json";
HashGuard::VerifyResult result =
HashGuard::VerifyDirectory(filePath, dataPath);
for (const auto& file : result.unchanged)
{
std::cout << "unchanged -> " << file << "\n";
}
for (const auto& file : result.modified)
{
std::cout << "modified -> " << file << "\n";
}
for (const auto& file : result.added)
{
std::cout << "added -> " << file << "\n";
}
for (const auto& file : result.deleted)
{
std::cout << "deleted -> " << file << "\n";
}
std::cout << "\nverification completed\n";
std::cout << "unchanged -> " << result.unchanged.size() << "\n";
std::cout << "modified -> " << result.modified.size() << "\n";
std::cout << "added -> " << result.added.size() << "\n";
std::cout << "deleted -> " << result.deleted.size() << "\n";
}
auto end = std::chrono::steady_clock::now();
double elapsed = std::chrono::duration<double, std::milli>(
end - start
).count();
std::cout << "\ntime -> " << elapsed << " ms\n";
}
catch (const std::exception& error)
{
std::cout << "error -> " << error.what() << "\n";
return 0;
}
return 0;
}