diff --git a/src/main.c b/src/main.c index 063c9514..3211a353 100644 --- a/src/main.c +++ b/src/main.c @@ -28,7 +28,6 @@ #include "main.h" #include "utils_rmw.h" #include "restore_rmw.h" -#include "parse_cli_options.h" #include "config_rmw.h" #include "purging_rmw.h" #include "strings_rmw.h" @@ -128,7 +127,7 @@ Please check your configuration file and permissions\ if (cli_user_options.want_selection_menu) { - int result = restore_select (st_config_data.st_waste_folder_props_head, &st_time_var); + int result = restore_select (st_config_data.st_waste_folder_props_head, &st_time_var, &cli_user_options); dispose_waste (st_config_data.st_waste_folder_props_head); return result; } @@ -137,7 +136,7 @@ Please check your configuration file and permissions\ if (cli_user_options.want_undo) { - undo_last_rmw (st_config_data.st_waste_folder_props_head, &st_time_var, mrl_file); + undo_last_rmw (st_config_data.st_waste_folder_props_head, &st_time_var, mrl_file, &cli_user_options); dispose_waste (st_config_data.st_waste_folder_props_head); return 0; } @@ -153,7 +152,8 @@ Please check your configuration file and permissions\ msg_warn_restore( restore_errors += Restore (argv[file_arg], st_config_data.st_waste_folder_props_head, - &st_time_var)); + &st_time_var, + &cli_user_options)); dispose_waste (st_config_data.st_waste_folder_props_head); @@ -167,7 +167,8 @@ Please check your configuration file and permissions\ argv, st_config_data.st_waste_folder_props_head, &st_time_var, - mrl_file); + mrl_file, + &cli_user_options); if (result > 1) { @@ -273,7 +274,8 @@ remove_to_waste ( char* const argv[], st_waste *waste_head, st_time *st_time_var, - const char *mrl_file) + const char *mrl_file, + const rmw_options * cli_user_options) { rmw_target st_file_properties; @@ -342,19 +344,24 @@ remove_to_waste ( strcat (st_file_properties.waste_dest_name, st_time_var->suffix_added_dup_exists); } - if (rename (st_file_properties.main_argv, st_file_properties.waste_dest_name) == 0) + int r_result = 0; + if (cli_user_options->want_dry_run == false) + r_result = rename (st_file_properties.main_argv, st_file_properties.waste_dest_name); + + if (r_result == 0) { if (verbose) printf ("'%s' -> '%s'\n", st_file_properties.main_argv, st_file_properties.waste_dest_name); removed_files_ctr++; - if (!create_trashinfo (&st_file_properties, waste_curr, st_time_var)) - { - confirmed_removals_list = add_removal (confirmed_removals_list, st_file_properties.waste_dest_name); - if (confirmed_removals_list_head == NULL) - confirmed_removals_list_head = confirmed_removals_list; - } + if (cli_user_options->want_dry_run == false) + if (!create_trashinfo (&st_file_properties, waste_curr, st_time_var)) + { + confirmed_removals_list = add_removal (confirmed_removals_list, st_file_properties.waste_dest_name); + if (confirmed_removals_list_head == NULL) + confirmed_removals_list_head = confirmed_removals_list; + } } else msg_err_rename (st_file_properties.main_argv, diff --git a/src/main.h b/src/main.h index 4d099ac0..2c4ae67b 100644 --- a/src/main.h +++ b/src/main.h @@ -31,6 +31,7 @@ #include "time_rmw.h" #include "trashinfo_rmw.h" +#include "parse_cli_options.h" #define STR_ENABLE_TEST "RMWTEST_HOME" @@ -66,7 +67,8 @@ remove_to_waste ( char* const argv[], st_waste *waste_head, st_time *st_time_var, - const char *mrl_file); + const char *mrl_file, + const rmw_options * cli_user_options); void list_waste_folders (st_waste *waste_head); diff --git a/src/restore_rmw.c b/src/restore_rmw.c index f835ffae..c9074868 100644 --- a/src/restore_rmw.c +++ b/src/restore_rmw.c @@ -45,7 +45,7 @@ * will it really be argv. */ int -Restore (const char *argv, st_waste *waste_head, st_time *st_time_var) +Restore (const char *argv, st_waste *waste_head, st_time *st_time_var, const rmw_options * cli_user_options) { static struct restore { @@ -77,7 +77,7 @@ Restore (const char *argv, st_waste *waste_head, st_time *st_time_var) strcat (possibly_in_path, file_arg); - msg_warn_restore (Restore (possibly_in_path, waste_curr, st_time_var)); + msg_warn_restore (Restore (possibly_in_path, waste_curr, st_time_var, cli_user_options)); waste_curr = waste_curr->next_node; } @@ -172,16 +172,23 @@ Duplicate filename at destination - appending time string...\n")); truncate_str (parent_dir, strlen (basename (file.dest))); - /* FIXME: needs error checking */ - if (! exists (parent_dir)) - make_dir (parent_dir); + if (cli_user_options->want_dry_run == 0) + if (! exists (parent_dir)) + make_dir (parent_dir); + + int rename_res = 0; + if (cli_user_options->want_dry_run == 0) + rename_res = rename (file_arg, file.dest); - int rename_res = rename (file_arg, file.dest); if (!rename_res) { printf ("+'%s' -> '%s'\n", file_arg, file.dest); - if (remove (file.info) != 0) + int result = 0; + if (cli_user_options->want_dry_run == 0) + result = remove (file.info); + + if (result != 0) { print_msg_error (); printf (_("while removing .trashinfo file: '%s'\n"), @@ -235,7 +242,7 @@ Duplicate filename at destination - appending time string...\n")); * @bug In some cases, not all files are displayed when using '-s' */ int -restore_select (st_waste *waste_head, st_time *st_time_var) +restore_select (st_waste *waste_head, st_time *st_time_var, const rmw_options * cli_user_options) { st_waste *waste_curr = waste_head; int c = 0; @@ -381,7 +388,7 @@ restore_select (st_waste *waste_head, st_time *st_time_var) static char recover_file[PATH_MAX + 1]; sprintf (recover_file, "%s%s", waste_curr->files, item_name (items[i])); /* waste_curr, not waste_head should always be passed here */ - msg_warn_restore(Restore (recover_file, waste_curr, st_time_var)); + msg_warn_restore(Restore (recover_file, waste_curr, st_time_var, cli_user_options)); } } } @@ -402,7 +409,7 @@ restore_select (st_waste *waste_head, st_time *st_time_var) * Reads the `lastrmw` file and restores the files listed inside */ void -undo_last_rmw (st_waste *waste_head, st_time *st_time_var, const char *mrl_file) +undo_last_rmw (st_waste *waste_head, st_time *st_time_var, const char *mrl_file, const rmw_options * cli_user_options) { FILE *fd; fd = fopen (mrl_file, "r"); @@ -413,9 +420,8 @@ undo_last_rmw (st_waste *waste_head, st_time *st_time_var, const char *mrl_file) int err_ctr = 0; while (fgets (line, sizeof line, fd) != NULL) { - int result = 0; trim_white_space (line); - result = Restore (line, waste_head, st_time_var); + int result = Restore (line, waste_head, st_time_var, cli_user_options); msg_warn_restore (result); err_ctr += result; } @@ -424,7 +430,10 @@ undo_last_rmw (st_waste *waste_head, st_time *st_time_var, const char *mrl_file) if (err_ctr == 0) { - if (remove (mrl_file)) + int result = 0; + if (cli_user_options->want_dry_run == 0) + result = remove (mrl_file); + if (result) { print_msg_error (); printf (_("failed to remove %s\n"), mrl_file); diff --git a/src/restore_rmw.h b/src/restore_rmw.h index 4dd93f43..f765b1d1 100644 --- a/src/restore_rmw.h +++ b/src/restore_rmw.h @@ -56,12 +56,12 @@ #ifndef TEST_LIB int -Restore (const char *argv, st_waste *waste_head, st_time *st_time_var); +Restore (const char *argv, st_waste *waste_head, st_time *st_time_var, const rmw_options * cli_user_options); int -restore_select (st_waste *waste_head, st_time *st_time_var); +restore_select (st_waste *waste_head, st_time *st_time_var, const rmw_options * cli_user_options); void -undo_last_rmw (st_waste *waste_head, st_time *st_time_var, const char *mrl_file); +undo_last_rmw (st_waste *waste_head, st_time *st_time_var, const char *mrl_file, const rmw_options * cli_user_options); #endif /* TEST_LIB */