From 66954c83bfc7549897eed4610ef4fca899d343fc Mon Sep 17 00:00:00 2001 From: andy5995 Date: Mon, 26 Apr 2021 00:26:09 -0500 Subject: [PATCH] try using faccessat() instead of lstat() (#283) --- src/restore_rmw.c | 45 ++++++++++++++++++++++++++++++++--------- src/utils_rmw.c | 13 +++++++++++- test/test_restore.sh.in | 2 +- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/restore_rmw.c b/src/restore_rmw.c index 8cf6942c..dee733e9 100644 --- a/src/restore_rmw.c +++ b/src/restore_rmw.c @@ -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; @@ -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]; @@ -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) @@ -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; } } @@ -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; } diff --git a/src/utils_rmw.c b/src/utils_rmw.c index ca3ae51d..823d1e1e 100644 --- a/src/utils_rmw.c +++ b/src/utils_rmw.c @@ -28,6 +28,8 @@ #include "globals.h" #endif +#include +#include #include "utils_rmw.h" #include "strings_rmw.h" #include "messages_rmw.h" @@ -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; } diff --git a/test/test_restore.sh.in b/test/test_restore.sh.in index e803c363..cf6fd908 100644 --- a/test/test_restore.sh.in +++ b/test/test_restore.sh.in @@ -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"