diff options
author | Oskar <[email protected]> | 2024-07-21 18:26:14 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-07-21 18:26:14 +0200 |
commit | 2e08693557019529cf8a4daa7931d3414cbeca51 (patch) | |
tree | 9d5744ecf075a0e8537c971ec3dc441509b3fdb3 | |
parent | 730e03e60740905f3c97bed11db637e3dff2801a (diff) |
New testing scripts to generate files and delete. Also fixed a segfault issue happening if the program fails during fill_lfc, it will call free_lfc, but because we haven set the 'next' node to NULL, we segfault.
-rw-r--r-- | Makefile | 9 | ||||
-rwxr-xr-x | test/test_deletefiles.sh | 6 | ||||
-rwxr-xr-x | test/test_generate_delete.sh | 7 | ||||
-rw-r--r-- | trashsys.c | 129 |
4 files changed, 129 insertions, 22 deletions
@@ -3,17 +3,26 @@ CFLAGS_TESTBIN=-O0 -Wfatal-errors -Wall -Werror -Wextra -g -fsanitize=address -W CFLAGS=-O3 -flto -march=native -DNDEBUG -fomit-frame-pointer -s -static -std=gnu99 TARGET=tsr TESTTARGET=tsr-TESTING +SP_TESTTARGET=tsr-SP INSTALL_DIRECTORY=/usr/local/bin MAKEFLAGS += SRCS=trashsys.c +SRCS_SP=trashsys_small_paths.c +P_MAX_SIZE="30" all: release clean: rm -f $(TARGET) rm -f test/$(TESTTARGET) + rm -f test/$(SP_TESTTARGET) + rm -f $(SRCS_SP) tests: + cp $(SRCS) $(SRCS_SP) $(CC) $(CFLAGS_TESTBIN) $(SRCS) -o test/$(TESTTARGET) + sed -i "s#PATH_MAX#$(P_MAX_SIZE)#g" $(SRCS_SP) + $(CC) $(CFLAGS_TESTBIN) $(SRCS_SP) -o test/$(SP_TESTTARGET) + rm -f $(SRCS_SP) install: cp $(TARGET) $(INSTALL_DIRECTORY) diff --git a/test/test_deletefiles.sh b/test/test_deletefiles.sh new file mode 100755 index 0000000..9cf917d --- /dev/null +++ b/test/test_deletefiles.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +files="1000b.img 1000mb.img 1023b.img 1023kib.img 1024b.img 1024kib.img 1.1gib.img 1gib.img 1KB.img 1KiB.img 1mb.img 1mib.img 500kib.img thisfile.txt ts_file1.txt ts_file2.txt ts_file3.txt" +options="-v" + +./tsr-TESTING $options $files diff --git a/test/test_generate_delete.sh b/test/test_generate_delete.sh new file mode 100755 index 0000000..6764682 --- /dev/null +++ b/test/test_generate_delete.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +for i in {1..10} +do + ./gentestfiles.sh + ./test_deletefiles.sh +done @@ -653,6 +653,7 @@ struct list_file_content *fill_lfc (struct initial_path_info *ipi) { } struct list_file_content *lfc = malloc(sizeof(struct list_file_content)); // first node + lfc->next = NULL; struct list_file_content *lfc_head = lfc; bool first = true; while ((ddd = readdir(dir)) != NULL) { @@ -680,8 +681,9 @@ struct list_file_content *fill_lfc (struct initial_path_info *ipi) { if(S_ISREG(d_or_f.st_mode)) { // check if given file is actually a file if(first == false) { - lfc->next = malloc(sizeof(struct list_file_content)); - lfc = lfc->next; + lfc->next = malloc(sizeof(struct list_file_content)); // Create next node + lfc = lfc->next; // Point lfc to the newly created node + lfc->next = NULL; // Set next to NULL so in case there is a failure, free_lfc wont get a segfault } else { first = false; } @@ -746,29 +748,28 @@ struct list_file_content *fill_lfc (struct initial_path_info *ipi) { } -int clear_all_files (char *paths, const int mode) { - - if(choice(mode) == 1) { - return FUNCTION_SUCCESS; - } +int clear_all_files (char *paths) { struct dirent *ddd; DIR *dir = opendir(paths); if (dir == NULL) { return FUNCTION_FAILURE; } + + char all[PATH_MAX] = {0}; + if(concat_str(all, PATH_MAX, paths) == NULL) { + closedir(dir); + return FUNCTION_FAILURE; + } + int paths_len = strlen(paths); while ((ddd = readdir(dir)) != NULL) { if (strncmp(".", ddd->d_name, 2) == 0 || strncmp("..", ddd->d_name, 3) == 0) { continue; } - char all[PATH_MAX]; - all[0] = '\0'; - if(concat_str(all, PATH_MAX, paths) == NULL) { - closedir(dir); - return FUNCTION_FAILURE; - } + + all[paths_len] = '\0'; if(concat_str(all, REM_SZ(PATH_MAX, all), ddd->d_name) == NULL) { closedir(dir); return FUNCTION_FAILURE; @@ -783,7 +784,6 @@ int clear_all_files (char *paths, const int mode) { } closedir(dir); - return FUNCTION_SUCCESS; } @@ -800,13 +800,87 @@ int compare_unixtime (time_t deleted_time, int difference_in_days) { final = current_time - deleted_time; if(final < diff_converted) { - printf("failure\n"); + cvm_fprintf(v_cvm_fprintf, stdout, "final is not older than diff_converted"); return FUNCTION_FAILURE; } - printf("success\n"); + cvm_fprintf(v_cvm_fprintf, stdout, "final is older than diff_converted"); return FUNCTION_SUCCESS; } +int clear_old_files (int file_age_in_days, struct initial_path_info *ipi) { + + struct list_file_content *lfc = fill_lfc(ipi); + struct list_file_content *walk; + int i = 1; + //int lfc_formatted (struct list_file_content *lfc, const bool L_used) + /* + struct list_file_content { + char ID[PATH_MAX]; + char filename[PATH_MAX]; + char trashed_filename[PATH_MAX]; + char filesize[PATH_MAX]; + char time[PATH_MAX]; + char originalpath[PATH_MAX]; + char tmp[PATH_MAX]; + struct list_file_content *next; + }; + + struct initial_path_info { // Initial useful strings to create before we do anything. Super useful when programming. + char ts_path_user_home[PATH_MAX]; + char ts_path_trashsys[PATH_MAX]; + char ts_path_log[PATH_MAX]; + char ts_path_trashed[PATH_MAX]; + char ts_path_user_home_withslash[PATH_MAX]; + char ts_path_trashsys_withslash[PATH_MAX]; + char ts_path_log_withslash[PATH_MAX]; + char ts_path_trashed_withslash[PATH_MAX]; + }; + */ + if(lfc == NULL) { return EXIT_SUCCESS; } + for(walk = lfc ; walk != NULL ; walk = walk->next, i++) { + char *endptr; + time_t deleted_time = (time_t)strtoll(walk->time, &endptr, 10); + if (errno == ERANGE || lfc->time == endptr) { + fprintf(stdout, "strtoll fail\n"); + return FUNCTION_FAILURE; + } + if(compare_unixtime(deleted_time, file_age_in_days) == FUNCTION_FAILURE) { + continue; + } + char cur_log_path[PATH_MAX]; + char cur_trashed_path[PATH_MAX]; + cur_log_path[0] = '\0'; + cur_trashed_path[0] = '\0'; + if(concat_str(cur_log_path, PATH_MAX, ipi->ts_path_log_withslash) == NULL + || concat_str(cur_trashed_path, PATH_MAX, ipi->ts_path_trashed_withslash) == NULL) { + fprintf(stderr, "Paths are too long. Continuing to next file.\n"); + continue; + } + if(concat_str(cur_log_path, REM_SZ(PATH_MAX, cur_log_path), walk->trashed_filename) == NULL + || concat_str(cur_trashed_path, REM_SZ(PATH_MAX, cur_trashed_path), walk->trashed_filename) == NULL) { + fprintf(stderr, "Paths are too long. Continuing to next file.\n"); + continue; + } + if(concat_str(cur_log_path, REM_SZ(PATH_MAX, cur_log_path), ".log") == NULL) { + fprintf(stderr, "Paths are too long. Continuing to next file.\n"); + continue; + } + int rm1 = remove(cur_log_path); + int rm2 = remove(cur_trashed_path); + if(rm1 == -1 || rm2 == -1) { + if(rm1 == -1) {fprintf(stdout, "failed to remove: %s\n", cur_log_path);} + if(rm2 == -1) {fprintf(stdout, "failed to remove: %s\n", cur_trashed_path);} + continue; + } + + cvm_fprintf(v_cvm_fprintf, stdout, "removed %s\n", cur_log_path); + cvm_fprintf(v_cvm_fprintf, stdout, "removed %s\n", cur_trashed_path); + } + + free_lfc(lfc); + return EXIT_SUCCESS; +} + int main (int argc, char *argv[]) { if (argc == 1) { @@ -892,11 +966,16 @@ int main (int argc, char *argv[]) { break; } } - if(optind == argc && (l_used || L_used || C_used) == false) { + if(optind == argc && (l_used || L_used || C_used || c_used) == false) { + USAGE_OUT(stderr); + return EXIT_FAILURE; + } + if(c_used && C_used) { USAGE_OUT(stderr); return EXIT_FAILURE; } - if (v_used == true) { v_cvm_fprintf = true; } // Verbose mode + + if(v_used == true) { v_cvm_fprintf = true; } // Verbose mode cvm_fprintf(v_cvm_fprintf, stdout, "options RCcLltafvny: %d%d%d%d%d%d%d%d%d%d%d\n", R_used, C_used, c_used, L_used, l_used, t_used, a_used, f_used, v_used, n_used, y_used); choice_mode = handle_ynf(y_used, n_used, f_used); @@ -912,11 +991,17 @@ int main (int argc, char *argv[]) { return EXIT_FAILURE; } - + if(c_used == true) { + clear_old_files(30, &ipi_m); + return EXIT_SUCCESS; + } if(C_used == true) { - clear_all_files(ipi_m.ts_path_log_withslash, choice_mode); - clear_all_files(ipi_m.ts_path_trashed_withslash, choice_mode); - return EXIT_FAILURE; + if(choice(choice_mode) == 1) { + return EXIT_SUCCESS; + } + clear_all_files(ipi_m.ts_path_log_withslash); + clear_all_files(ipi_m.ts_path_trashed_withslash); + return EXIT_SUCCESS; } if(l_used == true || L_used == true) { |