Skip to content

Commit

Permalink
implement reverse orphan check (fixes #289)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Aug 27, 2020
1 parent c9b6b5e commit 3e8075a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,18 @@ Please check your configuration file and permissions\
st_time st_time_var;
init_time_vars (&st_time_var);

int orphan_ctr = 0;
if (cli_user_options.want_purge || is_time_to_purge(&st_time_var, data_dir))
{
if (!st_config_data.force_required || cli_user_options.force)
purge (&st_config_data, &cli_user_options, &st_time_var);
purge (&st_config_data, &cli_user_options, &st_time_var, &orphan_ctr);
else
printf (_("purge has been skipped: use -f or --force\n"));
}

if (cli_user_options.want_orphan_chk)
{
orphan_maint(st_config_data.st_waste_folder_props_head, &st_time_var);
orphan_maint(st_config_data.st_waste_folder_props_head, &st_time_var, &orphan_ctr);
return 0;
}

Expand Down
34 changes: 28 additions & 6 deletions src/purging_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ int
purge (
st_config *st_config_data,
const rmw_options * cli_user_options,
st_time *st_time_var)
st_time *st_time_var,
int *orphan_ctr)
{
if (!st_config_data->purge_after)
{
Expand Down Expand Up @@ -321,8 +322,30 @@ purge (
truncate_str (temp, strlen (TRASHINFO_EXT)); /* acquire the (basename - trashinfo extension) */

strcat (corresponding_file_to_purge, temp); /* path to file in <WASTE>/files */

// If the corresponding file wasn't found, either display an error and exit, or remove the
// (probably) orphaned trashinfo file.
if (lstat (corresponding_file_to_purge, &st))
msg_err_lstat (corresponding_file_to_purge, __func__, __LINE__);
{
if (cli_user_options->want_orphan_chk && cli_user_options->force >= 2)
{
if (cli_user_options->want_dry_run == false)
{
if (remove(trashinfo_entry_realpath) != 0)
msg_err_remove (trashinfo_entry_realpath, __func__);
}
printf ("removed '%s'\n", trashinfo_entry_realpath);
(*orphan_ctr)++;
continue;
}
else
{
printf ("While processing %s:\n", trashinfo_entry_realpath);
puts ("You can remove the trashinfo file with '-offg'");
// Will exit after error
msg_err_lstat (corresponding_file_to_purge, __func__, __LINE__);
}
}

int orig_dev = st.st_dev;
int orig_inode = st.st_ino;
Expand Down Expand Up @@ -454,7 +477,7 @@ purge (
#ifndef TEST_LIB

short
orphan_maint (st_waste * waste_head, st_time *st_time_var)
orphan_maint (st_waste * waste_head, st_time *st_time_var, int *orphan_ctr)
{
rmw_target st_file_properties;

Expand All @@ -465,7 +488,6 @@ orphan_maint (st_waste * waste_head, st_time *st_time_var)
st_file_properties.is_duplicate = 0;

char path_to_trashinfo[LEN_MAX_PATH];
int orphan_ctr = 0;
st_waste *waste_curr = waste_head;
while (waste_curr != NULL)
{
Expand Down Expand Up @@ -502,7 +524,7 @@ orphan_maint (st_waste * waste_head, st_time *st_time_var)
{
/* TRANSLATORS: "created" refers to a file */
printf (_("Created %s\n"), path_to_trashinfo);
orphan_ctr++;
(*orphan_ctr)++;
}
else
{
Expand All @@ -517,7 +539,7 @@ orphan_maint (st_waste * waste_head, st_time *st_time_var)
waste_curr = waste_curr->next_node;
}

printf ("%d %s found\n", orphan_ctr, orphan_ctr == 1 ? "orphan" : "orphans");
printf ("%d %s found\n", *orphan_ctr, *orphan_ctr == 1 ? "orphan" : "orphans");

return 0;
}
Expand Down
5 changes: 3 additions & 2 deletions src/purging_rmw.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ int
purge (
st_config *st_config_data,
const rmw_options * cli_user_options,
st_time *st_time_var);
st_time *st_time_var,
int *orphan_ctr);

#ifndef TEST_LIB
short orphan_maint (st_waste * waste_head, st_time *st_time_var);
short orphan_maint (st_waste * waste_head, st_time *st_time_var, int *orphan_ctr);
#endif

0 comments on commit 3e8075a

Please sign in to comment.