Skip to content

Commit

Permalink
remove is_modified(), use exists() instead
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Apr 28, 2021
1 parent c3eebb9 commit d359bfb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 42 deletions.
33 changes: 12 additions & 21 deletions src/purging_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,14 @@ rmdir_recursive (const char *dirname, short unsigned level,
struct stat st;
if (lstat (st_dirname_properties.path, &st))
msg_err_lstat (st_dirname_properties.path, __func__, __LINE__);
unsigned long int orig_dev = st.st_dev;
unsigned long int orig_inode = st.st_ino;

if (force >= 2 && ~st.st_mode & S_IWUSR) /* use >= 2 to protect against future changes */
if (force >= 2 && ~st.st_mode & S_IWUSR)
{
if (!chmod (st_dirname_properties.path, 00700))
{
/* Now that the mode has changed, lstat must be run again */
if (lstat (st_dirname_properties.path, &st))
msg_err_lstat (st_dirname_properties.path, __func__, __LINE__);
orig_dev = st.st_dev;
orig_inode = st.st_ino;
}
else
{
Expand All @@ -124,18 +120,18 @@ rmdir_recursive (const char *dirname, short unsigned level,
{
if (!S_ISDIR (st.st_mode))
{
if (!is_modified (st_dirname_properties.path, orig_dev, orig_inode))
remove_result = remove (st_dirname_properties.path);
else
remove_result = -1;

if (remove_result == 0)
if (exists (st_dirname_properties.path))
{
deleted_files_ctr++;
bytes_freed += st.st_size;
if (remove (st_dirname_properties.path) == 0)
{
deleted_files_ctr++;
bytes_freed += st.st_size;
}
else
{
perror ("rmdir_recursive -> remove");
}
}
else
perror ("rmdir_recursive -> remove");
}
else
{
Expand All @@ -159,7 +155,6 @@ rmdir_recursive (const char *dirname, short unsigned level,
}
}
}

else
{
printf ("\nPermission denied while deleting\n");
Expand Down Expand Up @@ -388,9 +383,6 @@ purge (st_config * st_config_data,
}
}

int orig_dev = st.st_dev;
int orig_inode = st.st_ino;

if (S_ISDIR (st.st_mode))
{
if (cli_user_options->want_dry_run == false)
Expand Down Expand Up @@ -449,8 +441,7 @@ purge (st_config * st_config_data,
{
if (cli_user_options->want_dry_run == false)
{
if (!is_modified
(corresponding_file_to_purge, orig_dev, orig_inode))
if (exists (corresponding_file_to_purge))
status = remove (corresponding_file_to_purge);
else
status = -1;
Expand Down
18 changes: 0 additions & 18 deletions src/utils_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,24 +177,6 @@ user_verify (void)
}


/* used to prevent a TOCTOU race condtion */
bool
is_modified (const char* file, dev_t dev, const unsigned long int inode)
{
struct stat st;
if (lstat (file, &st))
msg_err_lstat (file, __func__, __LINE__);

if (dev == st.st_dev && inode == st.st_ino)
return false;

print_msg_warn ();
printf ("%s ", file);
puts ("has been modified since last check, not removing");
return true;
}


/*!
*
* According to RFC2396, we must escape any character that's
Expand Down
3 changes: 0 additions & 3 deletions src/utils_rmw.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ human_readable_size (off_t size);
bool
user_verify (void);

bool
is_modified (const char* file, dev_t dev, const unsigned long int inode);

bool
escape_url (const char *str, char *dest, const int len);

Expand Down

0 comments on commit d359bfb

Please sign in to comment.