basic md5 test implementation

This commit is contained in:
AustrianToast 2024-02-21 17:18:34 +01:00
parent 41d91e078e
commit 64100e34c1
No known key found for this signature in database
GPG Key ID: 8086574D3AAF2453
3 changed files with 42 additions and 17 deletions

View File

@ -1,4 +1,7 @@
#include <exception>
#include <iostream> #include <iostream>
#include <stdexcept>
#include <system_error>
#include <vector> #include <vector>
#include <filesystem> #include <filesystem>
#include <queue> #include <queue>
@ -6,18 +9,18 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
using std::vector, std::queue; using std::vector, std::queue;
int validate(const fs::path path) { bool valid(const fs::path path) {
if(!fs::exists(path)) if(!fs::exists(path))
return 1; return false;
if (false) if (!fs::is_directory(path))
return 1; return false;
return 0; return true;
} }
vector<fs::path> find_all_files(const fs::path path) { vector<fs::path> find_all_files(const fs::path path) {
if (validate(path) == 1) if (valid(path))
return vector<fs::path>(0); return vector<fs::path>(0);
vector<fs::path> files; vector<fs::path> files;
@ -25,11 +28,16 @@ vector<fs::path> find_all_files(const fs::path path) {
folders_to_traverse.push(path); folders_to_traverse.push(path);
while (!folders_to_traverse.empty()) { while (!folders_to_traverse.empty()) {
for (const fs::directory_entry &entry : fs::directory_iterator(folders_to_traverse.front())) try {
for (const fs::directory_entry &entry : fs::directory_iterator(folders_to_traverse.front())) {
if (!entry.is_directory()) if (!entry.is_directory())
files.emplace_back(entry); files.emplace_back(entry);
else else
folders_to_traverse.push(entry); folders_to_traverse.push(entry);
}
} catch (std::exception &system_error) {
system_error.what();
}
folders_to_traverse.pop(); folders_to_traverse.pop();
} }
@ -39,7 +47,7 @@ vector<fs::path> find_all_files(const fs::path path) {
} }
vector<fs::path> find_all_folders(const fs::path path) { vector<fs::path> find_all_folders(const fs::path path) {
if (validate(path) == 1) if (valid(path))
return vector<fs::path>(0); return vector<fs::path>(0);
vector<fs::path> folders; vector<fs::path> folders;
@ -61,7 +69,7 @@ vector<fs::path> find_all_folders(const fs::path path) {
} }
vector<fs::path> find_all_files_and_folders(const fs::path path) { vector<fs::path> find_all_files_and_folders(const fs::path path) {
if (validate(path) == 1) if (valid(path))
return vector<fs::path>(0); return vector<fs::path>(0);
vector<fs::path> files_and_folders; vector<fs::path> files_and_folders;

View File

@ -4,3 +4,4 @@
std::vector<std::filesystem::path> find_all_files(const std::filesystem::path path); std::vector<std::filesystem::path> find_all_files(const std::filesystem::path path);
std::vector<std::filesystem::path> find_all_folders(const std::filesystem::path path); std::vector<std::filesystem::path> find_all_folders(const std::filesystem::path path);
std::vector<std::filesystem::path> find_all_files_and_folders(const std::filesystem::path path); std::vector<std::filesystem::path> find_all_files_and_folders(const std::filesystem::path path);
bool valid(std::filesystem::path path);

View File

@ -2,9 +2,22 @@
#include <vector> #include <vector>
#include <filesystem> #include <filesystem>
#include "fs_reader.hpp" #include "fs_reader.hpp"
#include "openssl/md5.h"
namespace fs = std::filesystem; namespace fs = std::filesystem;
using std::cout, std::vector; using std::cout, std::vector;
void checksum_md5(const fs::path path) {
unsigned char digest[16];
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, reinterpret_cast<unsigned char*>(const_cast<char*>(path.c_str())), path.string().size());
MD5_Final(digest, &ctx);
for (int i = 0; i < 16; i++)
printf("%02x", digest[i]);
putchar ('\n');
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc == 1) { if (argc == 1) {
cout << "chksum: No path specified\n"; cout << "chksum: No path specified\n";
@ -14,14 +27,17 @@ int main(int argc, char *argv[]) {
cout << "chksum: Too many arguments\n"; cout << "chksum: Too many arguments\n";
return 1; return 1;
} }
if (!fs::exists(argv[1])) { if (!valid(argv[1])) {
cout << "chksum: Not a valid path\n"; cout << "chksum: Not a valid path\n";
return 1; return 1;
} }
vector<fs::path> files_and_folders = find_all_files(argv[1]); checksum_md5("hello world");
return 0;
for (const fs::path &path : files_and_folders) vector<fs::path> files = find_all_files(argv[1]);
for (const fs::path &path : files)
cout << path << "\n"; cout << path << "\n";
return 0; return 0;