Skip to content

Commit

Permalink
add dry run feature to remove_to_waste() and Restore() (theimpossible…
Browse files Browse the repository at this point in the history
…astronaut#230)

This is still very experimental!
  • Loading branch information
andy5995 committed Jun 5, 2020
1 parent ef845cd commit 2f44b34
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
33 changes: 20 additions & 13 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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);

Expand All @@ -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)
{
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "time_rmw.h"
#include "trashinfo_rmw.h"
#include "parse_cli_options.h"

#define STR_ENABLE_TEST "RMWTEST_HOME"

Expand Down Expand Up @@ -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);
Expand Down
35 changes: 22 additions & 13 deletions src/restore_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -235,7 +242,7 @@ Duplicate filename at destination - appending time string...\n"));
* @bug <a href="https://github.com/theimpossibleastronaut/rmw/issues/205">In some cases, not all files are displayed when using '-s'</a>
*/
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;
Expand Down Expand Up @@ -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));
}
}
}
Expand All @@ -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");
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/restore_rmw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit 2f44b34

Please sign in to comment.