diff --git a/src/fs_reader.cpp b/src/fs_reader.cpp index 6bf40cc..8e12456 100644 --- a/src/fs_reader.cpp +++ b/src/fs_reader.cpp @@ -1,4 +1,7 @@ +#include #include +#include +#include #include #include #include @@ -6,18 +9,18 @@ namespace fs = std::filesystem; using std::vector, std::queue; -int validate(const fs::path path) { +bool valid(const fs::path path) { if(!fs::exists(path)) - return 1; + return false; - if (false) - return 1; + if (!fs::is_directory(path)) + return false; - return 0; + return true; } vector find_all_files(const fs::path path) { - if (validate(path) == 1) + if (valid(path)) return vector(0); vector files; @@ -25,11 +28,16 @@ vector find_all_files(const fs::path path) { folders_to_traverse.push(path); while (!folders_to_traverse.empty()) { - for (const fs::directory_entry &entry : fs::directory_iterator(folders_to_traverse.front())) - if (!entry.is_directory()) - files.emplace_back(entry); - else - folders_to_traverse.push(entry); + try { + for (const fs::directory_entry &entry : fs::directory_iterator(folders_to_traverse.front())) { + if (!entry.is_directory()) + files.emplace_back(entry); + else + folders_to_traverse.push(entry); + } + } catch (std::exception &system_error) { + system_error.what(); + } folders_to_traverse.pop(); } @@ -39,7 +47,7 @@ vector find_all_files(const fs::path path) { } vector find_all_folders(const fs::path path) { - if (validate(path) == 1) + if (valid(path)) return vector(0); vector folders; @@ -61,7 +69,7 @@ vector find_all_folders(const fs::path path) { } vector find_all_files_and_folders(const fs::path path) { - if (validate(path) == 1) + if (valid(path)) return vector(0); vector files_and_folders; diff --git a/src/fs_reader.hpp b/src/fs_reader.hpp index 446c70f..dd55b65 100644 --- a/src/fs_reader.hpp +++ b/src/fs_reader.hpp @@ -3,4 +3,5 @@ std::vector find_all_files(const std::filesystem::path path); std::vector find_all_folders(const std::filesystem::path path); -std::vector find_all_files_and_folders(const std::filesystem::path path); \ No newline at end of file +std::vector find_all_files_and_folders(const std::filesystem::path path); +bool valid(std::filesystem::path path); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 63ed357..0a92b68 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,9 +2,22 @@ #include #include #include "fs_reader.hpp" +#include "openssl/md5.h" namespace fs = std::filesystem; 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(const_cast(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[]) { if (argc == 1) { cout << "chksum: No path specified\n"; @@ -14,14 +27,17 @@ int main(int argc, char *argv[]) { cout << "chksum: Too many arguments\n"; return 1; } - if (!fs::exists(argv[1])) { + if (!valid(argv[1])) { cout << "chksum: Not a valid path\n"; return 1; } - vector files_and_folders = find_all_files(argv[1]); + checksum_md5("hello world"); + return 0; - for (const fs::path &path : files_and_folders) + vector files = find_all_files(argv[1]); + + for (const fs::path &path : files) cout << path << "\n"; return 0;