diff options
-rwxr-xr-x | test/test_generate_delete.sh | 2 | ||||
-rwxr-xr-x | test/test_replace_timestamp.sh | 10 | ||||
-rw-r--r-- | trashsystem2.cpp | 46 | ||||
-rw-r--r-- | trashsystem2.hpp | 6 |
4 files changed, 50 insertions, 14 deletions
diff --git a/test/test_generate_delete.sh b/test/test_generate_delete.sh index 6764682..810eb96 100755 --- a/test/test_generate_delete.sh +++ b/test/test_generate_delete.sh @@ -1,6 +1,6 @@ #!/bin/bash -for i in {1..10} +for i in {1..5} do ./gentestfiles.sh ./test_deletefiles.sh diff --git a/test/test_replace_timestamp.sh b/test/test_replace_timestamp.sh index ffad66e..a8f44b9 100755 --- a/test/test_replace_timestamp.sh +++ b/test/test_replace_timestamp.sh @@ -3,7 +3,7 @@ LOG_DIR="/home/oskar/.trashsys/log" if [ -z "$1" ]; then - echo "Usage: $0 <replacement_string>" + echo "Usage: $0 <replacement_timestamp>" exit 1 fi @@ -16,8 +16,12 @@ fi for file in "$LOG_DIR"/*; do if [ -f "$file" ]; then - echo "replacing trashed time in: $file" - awk -v replacement="$REPLACEMENT" 'NR % 5 == 0 {print replacement} NR % 5 != 0 {print $0}' "$file" > "$file.tmp" && mv "$file.tmp" "$file" + echo "Replacing timestamp in: $file" + awk -v replacement="$REPLACEMENT" '{ + # Replace the fifth field (timestamp) with the replacement string + $5 = replacement + print $0 + }' "$file" > "$file.tmp" && mv "$file.tmp" "$file" fi done diff --git a/trashsystem2.cpp b/trashsystem2.cpp index 44a8dcf..0ece851 100644 --- a/trashsystem2.cpp +++ b/trashsystem2.cpp @@ -88,11 +88,11 @@ TS_FUNCTION_RESULT compare_unixtime (const time_t deleted_time, int const differ final = current_time - deleted_time; if(final < diff_converted) { - DEBUG_STREAM(<< "final is not older than diff_converted\n" << std::endl); + DEBUG_STREAM(<< "final is not older than diff_converted" << std::endl); return FUNCTION_FAILURE; } - DEBUG_STREAM(<< "final is older than diff_converted\n" << std::endl); + DEBUG_STREAM(<< "final is older than diff_converted" << std::endl); return FUNCTION_SUCCESS; } @@ -233,7 +233,8 @@ TS_FUNCTION_RESULT get_file_info(const std::filesystem::path &path, auto trashtime = std::time(nullptr); // maybe check if time fails? auto file_name = path.filename(); - trashsys_log_info _tli(id+1,filesize,trashtime,file_name,canon_path,isdir); + auto file_name_and_id = std::to_string(id)+":"+std::string(file_name); + trashsys_log_info _tli(id+1,filesize,trashtime,file_name,canon_path,isdir, file_name_and_id); tli = _tli; return FUNCTION_SUCCESS; } @@ -289,9 +290,9 @@ TS_FUNCTION_RESULT get_file_info_from_log(const initial_path_info &ipi, std::string line; while(std::getline(file_ifs, line)) { std::istringstream to_tli(line); - if(to_tli >> id >> log_filename >> log_id_and_filename/* <--- Not used!!! */ >> + if(to_tli >> id >> log_filename >> log_id_and_filename >> filesize >> trashtime >> log_originalpath >> is_dir) { - trashsys_log_info tli(id, filesize, trashtime, log_filename, log_originalpath, is_dir); + trashsys_log_info tli(id, filesize, trashtime, log_filename, log_originalpath, is_dir, log_id_and_filename); vtli.push_back(tli); DEBUG_STREAM( << "get_file_info_from_log: push_back successful: '" << std::string(a.rget_path()) << "' date pushed back to vtli.\n"); } else { @@ -489,14 +490,37 @@ TS_FUNCTION_RESULT restore_file(const directory_entry &de, const initial_path_in return FUNCTION_FAILURE; } -TS_FUNCTION_RESULT clear_old_files(initial_path_info &ipi) { +TS_FUNCTION_RESULT clear_old_trashed(const initial_path_info &ipi, const int days) { + std::vector<trashsys_log_info> vtli; + if(get_file_info_from_log(ipi, vtli) == FUNCTION_FAILURE) { + return FUNCTION_FAILURE; + } + + for(auto &a : vtli) { + if(compare_unixtime(a.rget_logtt(), days) == FUNCTION_FAILURE) { + continue; + } + std::filesystem::path makepath = std::string(ipi.rget_trd_ws())+std::string(a.rget_logfnid()); + std::filesystem::path makepathlog = std::string(ipi.rget_log_ws())+std::string(a.rget_logfnid())+".log"; + std::cout << makepath << "\n" << makepathlog << std::endl; + std::filesystem::remove_all(makepath); + std::filesystem::remove_all(makepathlog); + } return FUNCTION_SUCCESS; } -TS_FUNCTION_RESULT clear_all_trashed(initial_path_info &ipi) { +TS_FUNCTION_RESULT clear_all_trashed(const initial_path_info &ipi) { + auto count_trd = std::filesystem::remove_all(ipi.rget_trd()); + auto count_log = std::filesystem::remove_all(ipi.rget_log()); + if(count_trd != count_log) { + std::cerr << g_argv << ": Error: \n" + << count_trd << " trashed files were removed\n" + << count_log << "log files were removed" << std::endl; + } + return FUNCTION_SUCCESS; } @@ -675,12 +699,18 @@ int main (int argc, char **argv) { if(c_used == true) { DEBUG_STREAM( << "-c" << std::endl); + clear_old_trashed(ipi, 30); return EXIT_SUCCESS; } if(C_used == true) { DEBUG_STREAM( << "-C" << std::endl); - choice(choice_mode); + auto yn = choice(choice_mode); + if(yn == 1) { + return EXIT_SUCCESS; + } + + clear_all_trashed(ipi); return EXIT_SUCCESS; } diff --git a/trashsystem2.hpp b/trashsystem2.hpp index 5b1f116..388f5a2 100644 --- a/trashsystem2.hpp +++ b/trashsystem2.hpp @@ -80,13 +80,14 @@ class trashsys_log_info { public: trashsys_log_info() = default; trashsys_log_info(int64_t log_id, size_t log_filesize, time_t log_trashtime, - std::filesystem::path log_filename, std::filesystem::path log_originalpath, bool is_dir): + std::filesystem::path log_filename, std::filesystem::path log_originalpath, bool is_dir, std::filesystem::path fnid): ts_log_id(log_id), ts_log_filesize(log_filesize), ts_log_trashtime(log_trashtime), - ts_log_filename(log_filename), ts_log_originalpath(log_originalpath), ts_is_dir(is_dir) { } + ts_log_filename(log_filename), ts_log_originalpath(log_originalpath), ts_is_dir(is_dir), ts_log_filename_and_id(fnid) { } const int64_t &rget_logid() const { return ts_log_id; } const std::uintmax_t &rget_logfsz() const { return ts_log_filesize; } const time_t &rget_logtt() const { return ts_log_trashtime; } const std::filesystem::path &rget_logfn() const { return ts_log_filename; } + const std::filesystem::path &rget_logfnid() const { return ts_log_filename_and_id; } const std::filesystem::path &rget_logop() const { return ts_log_originalpath; } const bool &rget_isdir() const { return ts_is_dir; } private: @@ -96,6 +97,7 @@ private: std::filesystem::path ts_log_filename; std::filesystem::path ts_log_originalpath; bool ts_is_dir; + std::filesystem::path ts_log_filename_and_id; }; class initial_path_info { // Initial useful strings to create before we do anything. Super useful when programming. |