Skip to content

Commit

Permalink
try using faccessat() instead of lstat()
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Apr 26, 2021
1 parent 80714e2 commit e48cf49
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
45 changes: 35 additions & 10 deletions src/restore_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,35 @@ char *get_waste_parent (const char *src)
int
restore (const char *src, st_time *st_time_var, const rmw_options * cli_user_options, st_waste *waste_head)
{
if (exists (src))
char *src_with_path = src;
if (*src != '.' && *src != '/')
{
bufchk (src, LEN_MAX_PATH);
char *waste_parent = get_waste_parent (src);
src_with_path = malloc (LEN_MAX_PATH);
chk_malloc (src_with_path, __func__, __LINE__);
src_with_path = getcwd (src_with_path, PATH_MAX);
if (src_with_path == NULL || *src_with_path != '/')
{
print_msg_error ();
fprintf (stderr, "while getting the current working directory\n");
return 1;
}
int req_len = multi_strlen (src_with_path, "/", src, NULL) + 1;
bufchk_len (req_len, LEN_MAX_PATH, __func__, __LINE__);
char tmp[LEN_MAX_PATH];
snprintf (tmp, req_len, "%s/%s", src_with_path, src);
strcpy (src_with_path, tmp);
}

char src_real[strlen (src_with_path) + 1];
strcpy (src_real, src_with_path);

if (src_with_path != src)
free (src_with_path);

if (exists (src_real))
{
bufchk (src_real, LEN_MAX_PATH);
char *waste_parent = get_waste_parent (src_real);

st_waste *waste_curr = waste_head;
bool waste_match = false;
Expand All @@ -85,12 +110,12 @@ restore (const char *src, st_time *st_time_var, const rmw_options * cli_user_opt
if (!waste_match)
{
print_msg_error ();
fprintf (stderr, "'%s' is not in a Waste directory.\n", src);
fprintf (stderr, "'%s' is not in a Waste directory.\n", src_real);
return 1;
}

char src_copy[strlen (src) + 1];
strcpy (src_copy, src);
char src_copy[strlen (src_real) + 1];
strcpy (src_copy, src_real);
char *src_basename = basename (src_copy);

char src_tinfo[LEN_MAX_PATH];
Expand Down Expand Up @@ -145,11 +170,11 @@ Duplicate filename at destination - appending time string...\n"));

int rename_res = 0;
if (cli_user_options->want_dry_run == false)
rename_res = rename (src, dest);
rename_res = rename (src_real, dest);

if (!rename_res)
{
printf ("+'%s' -> '%s'\n", src, dest);
printf ("+'%s' -> '%s'\n", src_real, dest);

int result = 0;
if (cli_user_options->want_dry_run == false)
Expand All @@ -166,7 +191,7 @@ Duplicate filename at destination - appending time string...\n"));
}
else
{
msg_err_rename (src, dest, __func__, __LINE__);
msg_err_rename (src_real, dest, __func__, __LINE__);
return rename_res;
}
}
Expand All @@ -176,7 +201,7 @@ Duplicate filename at destination - appending time string...\n"));
* string below it unchanged */
printf (" :");
/* TRANSLATORS: "%s" refers to a file or directory */
printf (_("File not found: '%s'\n"), src);
printf (_("File not found: '%s'\n"), src_real);
msg_return_code (ENOENT);
return ENOENT;
}
Expand Down
13 changes: 12 additions & 1 deletion src/utils_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "globals.h"
#endif

#include <unistd.h>
#include <fcntl.h>
#include "utils_rmw.h"
#include "strings_rmw.h"
#include "messages_rmw.h"
Expand Down Expand Up @@ -100,7 +102,16 @@ bool exists (const char *filename)

static struct stat st;
static int res;
res = (lstat (filename, &st));
// res = (lstat (filename, &st));
char filename_copy[LEN_MAX_PATH];
strcpy (filename_copy, filename);
if (filename[0] != '.' && *filename != '/')
{
char tmp[LEN_MAX_PATH];
snprintf (tmp, LEN_MAX_PATH, "./%s", filename);
strcpy (filename_copy, tmp);
}
res = faccessat(0, filename_copy, F_OK, AT_SYMLINK_NOFOLLOW);
return res == 0 ? true : false;
}

Expand Down
2 changes: 1 addition & 1 deletion test/test_restore.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ set -x
$RMW_TEST_CMD_STRING $RMWTEST_HOME/somefiles/topdir -v || exit $?
cd $PRIMARY_WASTE_DIR/files
output=$($RMW_TEST_CMD_STRING -z topd*) || exit $?
test "$output" = "+'topdir' -> '$RMWTEST_HOME/somefiles/topdir'" || exit $?
test "$output" = "+'$RMWTEST_HOME/.Waste/files/topdir' -> '$RMWTEST_HOME/somefiles/topdir'" || exit $?

echo $SEPARATOR
echo "Try restoring a file that doesn't exist"
Expand Down

0 comments on commit e48cf49

Please sign in to comment.