diff options
author | Oskar <[email protected]> | 2024-10-24 22:28:05 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-10-24 22:28:05 +0200 |
commit | 52430face266fe677577e95a2f00a69270d984e9 (patch) | |
tree | 1cc2dfea99a7408b2035412fe459736faa264de2 | |
parent | 985c87d2b06cdebb005a37422c39de702d28915e (diff) |
split up restore file with another function find_by_id and slight modification to directory_entry class
-rw-r--r-- | trashsystem2.cpp | 47 | ||||
-rw-r--r-- | trashsystem2.hpp | 7 |
2 files changed, 48 insertions, 6 deletions
diff --git a/trashsystem2.cpp b/trashsystem2.cpp index c2d6c20..a8e8303 100644 --- a/trashsystem2.cpp +++ b/trashsystem2.cpp @@ -428,9 +428,38 @@ TS_FUNCTION_RESULT r_argument_validation(std::string argument) { return FUNCTION_FAILURE; } -TS_FUNCTION_RESULT restore_file(const initial_path_info &ipi, TS_FUNCTION_RESULT id) { +directory_entry find_by_id(const initial_path_info &ipi, TS_FUNCTION_RESULT id) { - if(id || ipi.is_fail()) {} + directory_entry foundfile(""); + auto vdirentry = get_files_in_directory(ipi.rget_log()); + for(auto &a : vdirentry) { + if(a.is_dir() || a.is_other()) { + DEBUG_STREAM( << "restore_file: Non-regular file found in log. Skipping." << std::endl); + continue; + } + + int64_t number = 0; + std::ifstream ifs(a.rget_path()); + ifs >> number; + if(ifs.fail()) { + DEBUG_STREAM( << "restore_file: Incorrect file format found in a log file. Skipping." << std::endl); + continue; + } + + std::cout << number << std::endl; + if(id == number) { + return foundfile; + } + } + + DEBUG_STREAM( << "restore_file: ID not found" << std::endl); + foundfile.set_fail(); + return foundfile; +} + +TS_FUNCTION_RESULT restore_file(directory_entry de) { + + if(de.is_dir()) {} return FUNCTION_SUCCESS; } @@ -583,11 +612,21 @@ int main (int argc, char **argv) { if(R_used == true) { auto argnumber = r_argument_validation(r_arg); if(argnumber == FUNCTION_FAILURE) { - std::cerr << "Invalid argument. Please provide a valid ID." << std::endl; + std::cerr << g_argv << ": Error: Invalid argument. Please provide a valid ID." << std::endl; + return EXIT_FAILURE; + } + + auto de = find_by_id(ipi, argnumber); + if(de.get_function_result() == FUNCTION_FAILURE) { + std::cerr << g_argv << ": Error: File with ID '" << argnumber << "' was not found." << std::endl; + return EXIT_FAILURE; + } + + if(restore_file(de) == FUNCTION_FAILURE) { + std::cerr << g_argv << ": Error: " << std::endl; return EXIT_FAILURE; } - restore_file(ipi, argnumber); DEBUG_STREAM( << "-R" << std::endl); return EXIT_SUCCESS; } diff --git a/trashsystem2.hpp b/trashsystem2.hpp index 83c1ead..5b1f116 100644 --- a/trashsystem2.hpp +++ b/trashsystem2.hpp @@ -60,17 +60,20 @@ private: class directory_entry { public: - directory_entry(std::filesystem::path dep, bool isd = false, bool isr = false, bool iso = false): - _de_path(dep), _is_dir(isd), _is_reg(isr), _is_other(iso) {} + directory_entry(std::filesystem::path dep, bool isd = false, bool isr = false, bool iso = false, TS_FUNCTION_RESULT isf = FUNCTION_SUCCESS): + _de_path(dep), _is_dir(isd), _is_reg(isr), _is_other(iso), _function_result(isf) {} bool is_reg() const { return _is_reg; } bool is_dir() const { return _is_dir; } bool is_other() const { return _is_other; } + TS_FUNCTION_RESULT get_function_result () const { return _function_result; } const std::filesystem::path &rget_path() const { return _de_path; } + void set_fail() { _function_result = FUNCTION_FAILURE; } private: std::filesystem::path _de_path; bool _is_dir = false; bool _is_reg = false; bool _is_other = false; + TS_FUNCTION_RESULT _function_result = FUNCTION_SUCCESS; }; class trashsys_log_info { |