* 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.
/* Return a file descriptor open to FILE, for use in openat.
As an optimization, return AT_FDCWD if FILE must be the working directory.
+ As a side effect, possibly set *ST to the file's status.
Fail and set errno if FILE is not a directory.
On failure return -2 if AT_FDCWD is -1, -1 otherwise. */
int
-target_directory_operand (char const *file)
+target_directory_operand (char const *file, struct stat *st)
{
if (must_be_working_directory (file))
return AT_FDCWD;
int fd = -1;
int maybe_dir = -1;
- struct stat st;
/* On old systems without O_DIRECTORY, like Solaris 10,
check with stat first lest we try to open a fifo for example and hang.
where open() was seen to return EACCES for non executable non dirs.
*/
if ((!O_DIRECTORY || (O_PATHSEARCH == O_SEARCH))
- && stat (file, &st) == 0)
+ && stat (file, st) == 0)
{
- maybe_dir = S_ISDIR (st.st_mode);
+ maybe_dir = S_ISDIR (st->st_mode);
if (! maybe_dir)
errno = ENOTDIR;
}
/* On old systems like Solaris 10 double check type,
to ensure we've opened a directory. */
int err;
- if (fstat (fd, &st) != 0 ? (err = errno, true)
- : !S_ISDIR (st.st_mode) && (err = ENOTDIR, true))
+ if (fstat (fd, st) != 0 ? (err = errno, true)
+ : !S_ISDIR (st->st_mode) && (err = ENOTDIR, true))
{
close (fd);
errno = err;
#include <fcntl.h>
#include <stdbool.h>
+#include <sys/stat.h>
#ifndef _GL_INLINE_HEADER_BEGIN
#error "Please include config.h first."
/* Return a file descriptor open to FILE, for use in openat.
As an optimization, return AT_FDCWD if FILE must be the working directory.
+ As a side effect, possibly set *ST to the file's status.
Fail and set errno if FILE is not a directory.
On failure return -2 if AT_FDCWD is -1, -1 otherwise. */
-extern int target_directory_operand (char const *file);
+extern int target_directory_operand (char const *file, struct stat *st);
/* Return true if FD represents success for target_directory_operand. */
TARGETDIR_INLINE _GL_ATTRIBUTE_PURE bool
}
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));
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;
| 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));
}
}
usage (EXIT_FAILURE);
}
+ struct stat sb;
int target_dirfd = AT_FDCWD;
if (no_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)
|| (mkdir_and_install && errno == ENOENT)))
die (EXIT_FAILURE, errno, _("failed to access %s"),
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;
usage (EXIT_FAILURE);
}
+ struct stat sb;
+ sb.st_mode = 0;
int target_dirfd = AT_FDCWD;
if (no_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));
? 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;
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));
}
}