cp,mv,install: avoid excess stat calls on non-GNU

* gl/lib/targetdir.c (target_directory_operand): New arg ST.
All callers changed.
* src/cp.c (do_copy):
* src/mv.c (main):
Avoid unnecessary stat call if target_directory_operand already
got the status.
This commit is contained in:
Paul Eggert committed 2022-04-12 23:57:15 -07:00
1 parent 30c932a309
commit 197a570ff0
5 files changed
+22 -16

No files matched your search

+4 -3
View File
@@ -602,7 +602,7 @@ do_copy (int n_files, char **file, char const *target_directory,
}
else if (target_directory)
{
target_dirfd = target_directory_operand (target_directory);
target_dirfd = target_directory_operand (target_directory, &sb);
if (! target_dirfd_valid (target_dirfd))
die (EXIT_FAILURE, errno, _("target directory %s"),
quoteaf (target_directory));
@@ -610,7 +610,7 @@ do_copy (int n_files, char **file, char const *target_directory,
else
{
char const *lastfile = file[n_files - 1];
int fd = target_directory_operand (lastfile);
int fd = target_directory_operand (lastfile, &sb);
if (target_dirfd_valid (fd))
{
target_dirfd = fd;
@@ -634,7 +634,8 @@ do_copy (int n_files, char **file, char const *target_directory,
| O_DIRECTORY) failed with EACCES not ENOTDIR. */
if (2 < n_files
|| (O_PATHSEARCH == O_SEARCH && err == EACCES
&& stat (lastfile, &sb) == 0 && S_ISDIR (sb.st_mode)))
&& (sb.st_mode || stat (lastfile, &sb) == 0)
&& S_ISDIR (sb.st_mode)))
die (EXIT_FAILURE, err, _("target %s"), quoteaf (lastfile));
}
}
+3 -2
View File
@@ -931,6 +931,7 @@ main (int argc, char **argv)
usage (EXIT_FAILURE);
}
struct stat sb;
int target_dirfd = AT_FDCWD;
if (no_target_directory)
{
@@ -946,7 +947,7 @@ main (int argc, char **argv)
}
else if (target_directory)
{
target_dirfd = target_directory_operand (target_directory);
target_dirfd = target_directory_operand (target_directory, &sb);
if (! (target_dirfd_valid (target_dirfd)
|| (mkdir_and_install && errno == ENOENT)))
die (EXIT_FAILURE, errno, _("failed to access %s"),
@@ -955,7 +956,7 @@ main (int argc, char **argv)
else if (!dir_arg)
{
char const *lastfile = file[n_files - 1];
int fd = target_directory_operand (lastfile);
int fd = target_directory_operand (lastfile, &sb);
if (target_dirfd_valid (fd))
{
target_dirfd = fd;
+6 -4
View File
@@ -382,6 +382,8 @@ main (int argc, char **argv)
usage (EXIT_FAILURE);
}
struct stat sb;
sb.st_mode = 0;
int target_dirfd = AT_FDCWD;
if (no_target_directory)
{
@@ -397,7 +399,7 @@ main (int argc, char **argv)
}
else if (target_directory)
{
target_dirfd = target_directory_operand (target_directory);
target_dirfd = target_directory_operand (target_directory, &sb);
if (! target_dirfd_valid (target_dirfd))
die (EXIT_FAILURE, errno, _("target directory %s"),
quoteaf (target_directory));
@@ -411,7 +413,7 @@ main (int argc, char **argv)
? errno : 0);
if (x.rename_errno != 0)
{
int fd = target_directory_operand (lastfile);
int fd = target_directory_operand (lastfile, &sb);
if (target_dirfd_valid (fd))
{
x.rename_errno = -1;
@@ -431,10 +433,10 @@ main (int argc, char **argv)
directory, in case opening a non-directory with (O_SEARCH
| O_DIRECTORY) failed with EACCES not ENOTDIR. */
int err = errno;
struct stat st;
if (2 < n_files
|| (O_PATHSEARCH == O_SEARCH && err == EACCES
&& stat (lastfile, &st) == 0 && S_ISDIR (st.st_mode)))
&& (sb.st_mode != 0 || stat (lastfile, &sb) == 0)
&& S_ISDIR (sb.st_mode)))
die (EXIT_FAILURE, err, _("target %s"), quoteaf (lastfile));
}
}