stty: fix untranslated diagnostics
[coreutils.git] / src / copy.c
blobddc9eab302c4aca8b5e3d5d85d8d22c07c17f044
1 /* copy.c -- core functions for copying files and directories
2 Copyright (C) 1989-2023 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Extracted from cp.c and librarified by Jim Meyering. */
19 #include <config.h>
20 #include <stdckdint.h>
21 #include <stdio.h>
22 #include <sys/ioctl.h>
23 #include <sys/types.h>
24 #include <selinux/selinux.h>
26 #if HAVE_HURD_H
27 # include <hurd.h>
28 #endif
29 #if HAVE_PRIV_H
30 # include <priv.h>
31 #endif
33 #include "system.h"
34 #include "acl.h"
35 #include "alignalloc.h"
36 #include "assure.h"
37 #include "backupfile.h"
38 #include "buffer-lcm.h"
39 #include "canonicalize.h"
40 #include "copy.h"
41 #include "cp-hash.h"
42 #include "fadvise.h"
43 #include "fcntl--.h"
44 #include "file-set.h"
45 #include "filemode.h"
46 #include "filenamecat.h"
47 #include "force-link.h"
48 #include "full-write.h"
49 #include "hash.h"
50 #include "hash-triple.h"
51 #include "ignore-value.h"
52 #include "ioblksize.h"
53 #include "quote.h"
54 #include "renameatu.h"
55 #include "root-uid.h"
56 #include "same.h"
57 #include "savedir.h"
58 #include "stat-size.h"
59 #include "stat-time.h"
60 #include "utimecmp.h"
61 #include "utimens.h"
62 #include "write-any-file.h"
63 #include "areadlink.h"
64 #include "yesno.h"
65 #include "selinux.h"
67 #ifndef USE_XATTR
68 # define USE_XATTR false
69 #endif
71 #if USE_XATTR
72 # include <attr/error_context.h>
73 # include <attr/libattr.h>
74 # include <stdarg.h>
75 # include "verror.h"
76 #endif
78 #if HAVE_LINUX_FALLOC_H
79 # include <linux/falloc.h>
80 #endif
82 /* See HAVE_FALLOCATE workaround when including this file. */
83 #ifdef HAVE_LINUX_FS_H
84 # include <linux/fs.h>
85 #endif
87 #if !defined FICLONE && defined __linux__
88 # define FICLONE _IOW (0x94, 9, int)
89 #endif
91 #if HAVE_FCLONEFILEAT && !USE_XATTR
92 # include <sys/clonefile.h>
93 #endif
95 #ifndef HAVE_FCHOWN
96 # define HAVE_FCHOWN false
97 # define fchown(fd, uid, gid) (-1)
98 #endif
100 #ifndef USE_ACL
101 # define USE_ACL 0
102 #endif
104 #define SAME_OWNER(A, B) ((A).st_uid == (B).st_uid)
105 #define SAME_GROUP(A, B) ((A).st_gid == (B).st_gid)
106 #define SAME_OWNER_AND_GROUP(A, B) (SAME_OWNER (A, B) && SAME_GROUP (A, B))
108 /* LINK_FOLLOWS_SYMLINKS is tri-state; if it is -1, we don't know
109 how link() behaves, so assume we can't hardlink symlinks in that case. */
110 #if (defined HAVE_LINKAT && ! LINKAT_SYMLINK_NOTSUP) || ! LINK_FOLLOWS_SYMLINKS
111 # define CAN_HARDLINK_SYMLINKS 1
112 #else
113 # define CAN_HARDLINK_SYMLINKS 0
114 #endif
116 struct dir_list
118 struct dir_list *parent;
119 ino_t ino;
120 dev_t dev;
123 /* Initial size of the cp.dest_info hash table. */
124 #define DEST_INFO_INITIAL_CAPACITY 61
126 static bool copy_internal (char const *src_name, char const *dst_name,
127 int dst_dirfd, char const *dst_relname,
128 int nonexistent_dst, struct stat const *parent,
129 struct dir_list *ancestors,
130 const struct cp_options *x,
131 bool command_line_arg,
132 bool *first_dir_created_per_command_line_arg,
133 bool *copy_into_self,
134 bool *rename_succeeded);
135 static bool owner_failure_ok (struct cp_options const *x);
137 /* Pointers to the file names: they're used in the diagnostic that is issued
138 when we detect the user is trying to copy a directory into itself. */
139 static char const *top_level_src_name;
140 static char const *top_level_dst_name;
142 enum copy_debug_val
144 COPY_DEBUG_UNKNOWN,
145 COPY_DEBUG_NO,
146 COPY_DEBUG_YES,
147 COPY_DEBUG_EXTERNAL,
148 COPY_DEBUG_EXTERNAL_INTERNAL,
149 COPY_DEBUG_AVOIDED,
150 COPY_DEBUG_UNSUPPORTED,
153 /* debug info about the last file copy. */
154 static struct copy_debug
156 enum copy_debug_val offload;
157 enum copy_debug_val reflink;
158 enum copy_debug_val sparse_detection;
159 } copy_debug;
161 static const char*
162 copy_debug_string (enum copy_debug_val debug_val)
164 switch (debug_val)
166 case COPY_DEBUG_NO: return "no";
167 case COPY_DEBUG_YES: return "yes";
168 case COPY_DEBUG_AVOIDED: return "avoided";
169 case COPY_DEBUG_UNSUPPORTED: return "unsupported";
170 default: return "unknown";
174 static const char*
175 copy_debug_sparse_string (enum copy_debug_val debug_val)
177 switch (debug_val)
179 case COPY_DEBUG_NO: return "no";
180 case COPY_DEBUG_YES: return "zeros";
181 case COPY_DEBUG_EXTERNAL: return "SEEK_HOLE";
182 case COPY_DEBUG_EXTERNAL_INTERNAL: return "SEEK_HOLE + zeros";
183 default: return "unknown";
187 /* Print --debug output on standard output. */
188 static void
189 emit_debug (const struct cp_options *x)
191 if (! x->hard_link && ! x->symbolic_link && x->data_copy_required)
192 printf ("copy offload: %s, reflink: %s, sparse detection: %s\n",
193 copy_debug_string (copy_debug.offload),
194 copy_debug_string (copy_debug.reflink),
195 copy_debug_sparse_string (copy_debug.sparse_detection));
198 #ifndef DEV_FD_MIGHT_BE_CHR
199 # define DEV_FD_MIGHT_BE_CHR false
200 #endif
202 /* Act like fstat (DIRFD, FILENAME, ST, FLAGS), except when following
203 symbolic links on Solaris-like systems, treat any character-special
204 device like /dev/fd/0 as if it were the file it is open on. */
205 static int
206 follow_fstatat (int dirfd, char const *filename, struct stat *st, int flags)
208 int result = fstatat (dirfd, filename, st, flags);
210 if (DEV_FD_MIGHT_BE_CHR && result == 0 && !(flags & AT_SYMLINK_NOFOLLOW)
211 && S_ISCHR (st->st_mode))
213 static dev_t stdin_rdev;
214 static signed char stdin_rdev_status;
215 if (stdin_rdev_status == 0)
217 struct stat stdin_st;
218 if (stat ("/dev/stdin", &stdin_st) == 0 && S_ISCHR (stdin_st.st_mode)
219 && minor (stdin_st.st_rdev) == STDIN_FILENO)
221 stdin_rdev = stdin_st.st_rdev;
222 stdin_rdev_status = 1;
224 else
225 stdin_rdev_status = -1;
227 if (0 < stdin_rdev_status && major (stdin_rdev) == major (st->st_rdev))
228 result = fstat (minor (st->st_rdev), st);
231 return result;
234 /* Attempt to punch a hole to avoid any permanent
235 speculative preallocation on file systems such as XFS.
236 Return values as per fallocate(2) except ENOSYS etc. are ignored. */
238 static int
239 punch_hole (int fd, off_t offset, off_t length)
241 int ret = 0;
242 /* +0 is to work around older <linux/fs.h> defining HAVE_FALLOCATE to empty. */
243 #if HAVE_FALLOCATE + 0
244 # if defined FALLOC_FL_PUNCH_HOLE && defined FALLOC_FL_KEEP_SIZE
245 ret = fallocate (fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
246 offset, length);
247 if (ret < 0 && (is_ENOTSUP (errno) || errno == ENOSYS))
248 ret = 0;
249 # endif
250 #endif
251 return ret;
254 /* Create a hole at the end of a file,
255 avoiding preallocation if requested. */
257 static bool
258 create_hole (int fd, char const *name, bool punch_holes, off_t size)
260 off_t file_end = lseek (fd, size, SEEK_CUR);
262 if (file_end < 0)
264 error (0, errno, _("cannot lseek %s"), quoteaf (name));
265 return false;
268 /* Some file systems (like XFS) preallocate when write extending a file.
269 I.e., a previous write() may have preallocated extra space
270 that the seek above will not discard. A subsequent write() could
271 then make this allocation permanent. */
272 if (punch_holes && punch_hole (fd, file_end - size, size) < 0)
274 error (0, errno, _("error deallocating %s"), quoteaf (name));
275 return false;
278 return true;
282 /* Whether an errno value ERR, set by FICLONE or copy_file_range,
283 indicates that the copying operation has terminally failed, even
284 though it was invoked correctly (so that, e.g, EBADF cannot occur)
285 and even though !is_CLONENOTSUP (ERR). */
287 static bool
288 is_terminal_error (int err)
290 return err == EIO || err == ENOMEM || err == ENOSPC || err == EDQUOT;
293 /* Similarly, whether ERR indicates that the copying operation is not
294 supported or allowed for this file or process, even though the
295 operation was invoked correctly. */
297 static bool
298 is_CLONENOTSUP (int err)
300 return err == ENOSYS || err == ENOTTY || is_ENOTSUP (err)
301 || err == EINVAL || err == EBADF
302 || err == EXDEV || err == ETXTBSY
303 || err == EPERM || err == EACCES;
307 /* Copy the regular file open on SRC_FD/SRC_NAME to DST_FD/DST_NAME,
308 honoring the MAKE_HOLES setting and using the BUF_SIZE-byte buffer
309 *ABUF for temporary storage, allocating it lazily if *ABUF is null.
310 Copy no more than MAX_N_READ bytes.
311 Return true upon successful completion;
312 print a diagnostic and return false upon error.
313 Note that for best results, BUF should be "well"-aligned.
314 Set *LAST_WRITE_MADE_HOLE to true if the final operation on
315 DEST_FD introduced a hole. Set *TOTAL_N_READ to the number of
316 bytes read. */
317 static bool
318 sparse_copy (int src_fd, int dest_fd, char **abuf, size_t buf_size,
319 size_t hole_size, bool punch_holes, bool allow_reflink,
320 char const *src_name, char const *dst_name,
321 uintmax_t max_n_read, off_t *total_n_read,
322 bool *last_write_made_hole)
324 *last_write_made_hole = false;
325 *total_n_read = 0;
327 if (copy_debug.sparse_detection == COPY_DEBUG_UNKNOWN)
328 copy_debug.sparse_detection = hole_size ? COPY_DEBUG_YES : COPY_DEBUG_NO;
329 else if (hole_size && copy_debug.sparse_detection == COPY_DEBUG_EXTERNAL)
330 copy_debug.sparse_detection = COPY_DEBUG_EXTERNAL_INTERNAL;
332 /* If not looking for holes, use copy_file_range if functional,
333 but don't use if reflink disallowed as that may be implicit. */
334 if (!hole_size && allow_reflink)
335 while (max_n_read)
337 /* Copy at most COPY_MAX bytes at a time; this is min
338 (SSIZE_MAX, SIZE_MAX) truncated to a value that is
339 surely aligned well. */
340 ssize_t copy_max = MIN (SSIZE_MAX, SIZE_MAX) >> 30 << 30;
341 ssize_t n_copied = copy_file_range (src_fd, nullptr, dest_fd, nullptr,
342 MIN (max_n_read, copy_max), 0);
343 if (n_copied == 0)
345 /* copy_file_range incorrectly returns 0 when reading from
346 the proc file system on the Linux kernel through at
347 least 5.6.19 (2020), so fall back on 'read' if the
348 input file seems empty. */
349 if (*total_n_read == 0)
350 break;
351 copy_debug.offload = COPY_DEBUG_YES;
352 return true;
354 if (n_copied < 0)
356 copy_debug.offload = COPY_DEBUG_UNSUPPORTED;
358 /* Consider operation unsupported only if no data copied.
359 For example, EPERM could occur if copy_file_range not enabled
360 in seccomp filters, so retry with a standard copy. EPERM can
361 also occur for immutable files, but that would only be in the
362 edge case where the file is made immutable after creating,
363 in which case the (more accurate) error is still shown. */
364 if (*total_n_read == 0 && is_CLONENOTSUP (errno))
365 break;
367 /* ENOENT was seen sometimes across CIFS shares, resulting in
368 no data being copied, but subsequent standard copies succeed. */
369 if (*total_n_read == 0 && errno == ENOENT)
370 break;
372 if (errno == EINTR)
373 n_copied = 0;
374 else
376 error (0, errno, _("error copying %s to %s"),
377 quoteaf_n (0, src_name), quoteaf_n (1, dst_name));
378 return false;
381 copy_debug.offload = COPY_DEBUG_YES;
382 max_n_read -= n_copied;
383 *total_n_read += n_copied;
385 else
386 copy_debug.offload = COPY_DEBUG_AVOIDED;
389 bool make_hole = false;
390 off_t psize = 0;
392 while (max_n_read)
394 if (!*abuf)
395 *abuf = xalignalloc (getpagesize (), buf_size);
396 char *buf = *abuf;
397 ssize_t n_read = read (src_fd, buf, MIN (max_n_read, buf_size));
398 if (n_read < 0)
400 if (errno == EINTR)
401 continue;
402 error (0, errno, _("error reading %s"), quoteaf (src_name));
403 return false;
405 if (n_read == 0)
406 break;
407 max_n_read -= n_read;
408 *total_n_read += n_read;
410 /* Loop over the input buffer in chunks of hole_size. */
411 size_t csize = hole_size ? hole_size : buf_size;
412 char *cbuf = buf;
413 char *pbuf = buf;
415 while (n_read)
417 bool prev_hole = make_hole;
418 csize = MIN (csize, n_read);
420 if (hole_size && csize)
421 make_hole = is_nul (cbuf, csize);
423 bool transition = (make_hole != prev_hole) && psize;
424 bool last_chunk = (n_read == csize && ! make_hole) || ! csize;
426 if (transition || last_chunk)
428 if (! transition)
429 psize += csize;
431 if (! prev_hole)
433 if (full_write (dest_fd, pbuf, psize) != psize)
435 error (0, errno, _("error writing %s"),
436 quoteaf (dst_name));
437 return false;
440 else
442 if (! create_hole (dest_fd, dst_name, punch_holes, psize))
443 return false;
446 pbuf = cbuf;
447 psize = csize;
449 if (last_chunk)
451 if (! csize)
452 n_read = 0; /* Finished processing buffer. */
454 if (transition)
455 csize = 0; /* Loop again to deal with last chunk. */
456 else
457 psize = 0; /* Reset for next read loop. */
460 else /* Coalesce writes/seeks. */
462 if (ckd_add (&psize, psize, csize))
464 error (0, 0, _("overflow reading %s"), quoteaf (src_name));
465 return false;
469 n_read -= csize;
470 cbuf += csize;
473 *last_write_made_hole = make_hole;
475 /* It's tempting to break early here upon a short read from
476 a regular file. That would save the final read syscall
477 for each file. Unfortunately that doesn't work for
478 certain files in /proc or /sys with linux kernels. */
481 /* Ensure a trailing hole is created, so that subsequent
482 calls of sparse_copy() start at the correct offset. */
483 if (make_hole && ! create_hole (dest_fd, dst_name, punch_holes, psize))
484 return false;
485 else
486 return true;
489 /* Perform the O(1) btrfs clone operation, if possible.
490 Upon success, return 0. Otherwise, return -1 and set errno. */
491 static inline int
492 clone_file (int dest_fd, int src_fd)
494 #ifdef FICLONE
495 return ioctl (dest_fd, FICLONE, src_fd);
496 #else
497 (void) dest_fd;
498 (void) src_fd;
499 errno = ENOTSUP;
500 return -1;
501 #endif
504 /* Write N_BYTES zero bytes to file descriptor FD. Return true if successful.
505 Upon write failure, set errno and return false. */
506 static bool
507 write_zeros (int fd, off_t n_bytes)
509 static char *zeros;
510 static size_t nz = IO_BUFSIZE;
512 /* Attempt to use a relatively large calloc'd source buffer for
513 efficiency, but if that allocation fails, resort to a smaller
514 statically allocated one. */
515 if (zeros == nullptr)
517 static char fallback[1024];
518 zeros = calloc (nz, 1);
519 if (zeros == nullptr)
521 zeros = fallback;
522 nz = sizeof fallback;
526 while (n_bytes)
528 size_t n = MIN (nz, n_bytes);
529 if ((full_write (fd, zeros, n)) != n)
530 return false;
531 n_bytes -= n;
534 return true;
537 #ifdef SEEK_HOLE
538 /* Perform an efficient extent copy, if possible. This avoids
539 the overhead of detecting holes in hole-introducing/preserving
540 copy, and thus makes copying sparse files much more efficient.
541 Copy from SRC_FD to DEST_FD, using *ABUF (of size BUF_SIZE) for a buffer.
542 Allocate *ABUF lazily if *ABUF is null.
543 Look for holes of size HOLE_SIZE in the input.
544 The input file is of size SRC_TOTAL_SIZE.
545 Use SPARSE_MODE to determine whether to create holes in the output.
546 SRC_NAME and DST_NAME are the input and output file names.
547 Return true if successful, false (with a diagnostic) otherwise. */
549 static bool
550 lseek_copy (int src_fd, int dest_fd, char **abuf, size_t buf_size,
551 size_t hole_size, off_t ext_start, off_t src_total_size,
552 enum Sparse_type sparse_mode,
553 bool allow_reflink,
554 char const *src_name, char const *dst_name)
556 off_t last_ext_start = 0;
557 off_t last_ext_len = 0;
558 off_t dest_pos = 0;
559 bool wrote_hole_at_eof = true;
561 copy_debug.sparse_detection = COPY_DEBUG_EXTERNAL;
563 while (0 <= ext_start)
565 off_t ext_end = lseek (src_fd, ext_start, SEEK_HOLE);
566 if (ext_end < 0)
568 if (errno != ENXIO)
569 goto cannot_lseek;
570 ext_end = src_total_size;
571 if (ext_end <= ext_start)
573 /* The input file grew; get its current size. */
574 src_total_size = lseek (src_fd, 0, SEEK_END);
575 if (src_total_size < 0)
576 goto cannot_lseek;
578 /* If the input file shrank after growing, stop copying. */
579 if (src_total_size <= ext_start)
580 break;
582 ext_end = src_total_size;
585 /* If the input file must have grown, increase its measured size. */
586 if (src_total_size < ext_end)
587 src_total_size = ext_end;
589 if (lseek (src_fd, ext_start, SEEK_SET) < 0)
590 goto cannot_lseek;
592 wrote_hole_at_eof = false;
593 off_t ext_hole_size = ext_start - last_ext_start - last_ext_len;
595 if (ext_hole_size)
597 if (sparse_mode != SPARSE_NEVER)
599 if (! create_hole (dest_fd, dst_name,
600 sparse_mode == SPARSE_ALWAYS,
601 ext_hole_size))
602 return false;
603 wrote_hole_at_eof = true;
605 else
607 /* When not inducing holes and when there is a hole between
608 the end of the previous extent and the beginning of the
609 current one, write zeros to the destination file. */
610 if (! write_zeros (dest_fd, ext_hole_size))
612 error (0, errno, _("%s: write failed"),
613 quotef (dst_name));
614 return false;
619 off_t ext_len = ext_end - ext_start;
620 last_ext_start = ext_start;
621 last_ext_len = ext_len;
623 /* Copy this extent, looking for further opportunities to not
624 bother to write zeros if --sparse=always, since SEEK_HOLE
625 is conservative and may miss some holes. */
626 off_t n_read;
627 bool read_hole;
628 if ( ! sparse_copy (src_fd, dest_fd, abuf, buf_size,
629 sparse_mode != SPARSE_ALWAYS ? 0 : hole_size,
630 true, allow_reflink, src_name, dst_name,
631 ext_len, &n_read, &read_hole))
632 return false;
634 dest_pos = ext_start + n_read;
635 if (n_read)
636 wrote_hole_at_eof = read_hole;
637 if (n_read < ext_len)
639 /* The input file shrank. */
640 src_total_size = dest_pos;
641 break;
644 ext_start = lseek (src_fd, dest_pos, SEEK_DATA);
645 if (ext_start < 0 && errno != ENXIO)
646 goto cannot_lseek;
649 /* When the source file ends with a hole, we have to do a little more work,
650 since the above copied only up to and including the final extent.
651 In order to complete the copy, we may have to insert a hole or write
652 zeros in the destination corresponding to the source file's hole-at-EOF.
654 In addition, if the final extent was a block of zeros at EOF and we've
655 just converted them to a hole in the destination, we must call ftruncate
656 here in order to record the proper length in the destination. */
657 if ((dest_pos < src_total_size || wrote_hole_at_eof)
658 && ! (sparse_mode == SPARSE_NEVER
659 ? write_zeros (dest_fd, src_total_size - dest_pos)
660 : ftruncate (dest_fd, src_total_size) == 0))
662 error (0, errno, _("failed to extend %s"), quoteaf (dst_name));
663 return false;
666 if (sparse_mode == SPARSE_ALWAYS && dest_pos < src_total_size
667 && punch_hole (dest_fd, dest_pos, src_total_size - dest_pos) < 0)
669 error (0, errno, _("error deallocating %s"), quoteaf (dst_name));
670 return false;
673 return true;
675 cannot_lseek:
676 error (0, errno, _("cannot lseek %s"), quoteaf (src_name));
677 return false;
679 #endif
681 /* FIXME: describe */
682 /* FIXME: rewrite this to use a hash table so we avoid the quadratic
683 performance hit that's probably noticeable only on trees deeper
684 than a few hundred levels. See use of active_dir_map in remove.c */
686 ATTRIBUTE_PURE
687 static bool
688 is_ancestor (const struct stat *sb, const struct dir_list *ancestors)
690 while (ancestors != 0)
692 if (ancestors->ino == sb->st_ino && ancestors->dev == sb->st_dev)
693 return true;
694 ancestors = ancestors->parent;
696 return false;
699 static bool
700 errno_unsupported (int err)
702 return err == ENOTSUP || err == ENODATA;
705 #if USE_XATTR
706 ATTRIBUTE_FORMAT ((printf, 2, 3))
707 static void
708 copy_attr_error (MAYBE_UNUSED struct error_context *ctx,
709 char const *fmt, ...)
711 if (!errno_unsupported (errno))
713 int err = errno;
714 va_list ap;
716 /* use verror module to print error message */
717 va_start (ap, fmt);
718 verror (0, err, fmt, ap);
719 va_end (ap);
723 ATTRIBUTE_FORMAT ((printf, 2, 3))
724 static void
725 copy_attr_allerror (MAYBE_UNUSED struct error_context *ctx,
726 char const *fmt, ...)
728 int err = errno;
729 va_list ap;
731 /* use verror module to print error message */
732 va_start (ap, fmt);
733 verror (0, err, fmt, ap);
734 va_end (ap);
737 static char const *
738 copy_attr_quote (MAYBE_UNUSED struct error_context *ctx, char const *str)
740 return quoteaf (str);
743 static void
744 copy_attr_free (MAYBE_UNUSED struct error_context *ctx,
745 MAYBE_UNUSED char const *str)
749 /* Exclude SELinux extended attributes that are otherwise handled,
750 and are problematic to copy again. Also honor attributes
751 configured for exclusion in /etc/xattr.conf.
752 FIXME: Should we handle POSIX ACLs similarly?
753 Return zero to skip. */
754 static int
755 check_selinux_attr (char const *name, struct error_context *ctx)
757 return STRNCMP_LIT (name, "security.selinux")
758 && attr_copy_check_permissions (name, ctx);
761 /* If positive SRC_FD and DST_FD descriptors are passed,
762 then copy by fd, otherwise copy by name. */
764 static bool
765 copy_attr (char const *src_path, int src_fd,
766 char const *dst_path, int dst_fd, struct cp_options const *x)
768 bool all_errors = (!x->data_copy_required || x->require_preserve_xattr);
769 bool some_errors = (!all_errors && !x->reduce_diagnostics);
770 int (*check) (char const *, struct error_context *)
771 = (x->preserve_security_context || x->set_security_context
772 ? check_selinux_attr : nullptr);
774 # if 4 < __GNUC__ + (8 <= __GNUC_MINOR__)
775 /* Pacify gcc -Wsuggest-attribute=format through at least GCC 11.2.1. */
776 # pragma GCC diagnostic push
777 # pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
778 # endif
779 struct error_context *ctx
780 = (all_errors || some_errors
781 ? (&(struct error_context) {
782 .error = all_errors ? copy_attr_allerror : copy_attr_error,
783 .quote = copy_attr_quote,
784 .quote_free = copy_attr_free
786 : nullptr);
787 # if 4 < __GNUC__ + (8 <= __GNUC_MINOR__)
788 # pragma GCC diagnostic pop
789 # endif
791 return ! (0 <= src_fd && 0 <= dst_fd
792 ? attr_copy_fd (src_path, src_fd, dst_path, dst_fd, check, ctx)
793 : attr_copy_file (src_path, dst_path, check, ctx));
795 #else /* USE_XATTR */
797 static bool
798 copy_attr (MAYBE_UNUSED char const *src_path,
799 MAYBE_UNUSED int src_fd,
800 MAYBE_UNUSED char const *dst_path,
801 MAYBE_UNUSED int dst_fd,
802 MAYBE_UNUSED struct cp_options const *x)
804 return true;
806 #endif /* USE_XATTR */
808 /* Read the contents of the directory SRC_NAME_IN, and recursively
809 copy the contents to DST_NAME_IN aka DST_DIRFD+DST_RELNAME_IN.
810 NEW_DST is true if DST_NAME_IN is a directory
811 that was created previously in the recursion.
812 SRC_SB and ANCESTORS describe SRC_NAME_IN.
813 Set *COPY_INTO_SELF if SRC_NAME_IN is a parent of
814 (or the same as) DST_NAME_IN; otherwise, clear it.
815 Propagate *FIRST_DIR_CREATED_PER_COMMAND_LINE_ARG from
816 caller to each invocation of copy_internal. Be careful to
817 pass the address of a temporary, and to update
818 *FIRST_DIR_CREATED_PER_COMMAND_LINE_ARG only upon completion.
819 Return true if successful. */
821 static bool
822 copy_dir (char const *src_name_in, char const *dst_name_in,
823 int dst_dirfd, char const *dst_relname_in, bool new_dst,
824 const struct stat *src_sb, struct dir_list *ancestors,
825 const struct cp_options *x,
826 bool *first_dir_created_per_command_line_arg,
827 bool *copy_into_self)
829 char *name_space;
830 char *namep;
831 struct cp_options non_command_line_options = *x;
832 bool ok = true;
834 name_space = savedir (src_name_in, SAVEDIR_SORT_FASTREAD);
835 if (name_space == nullptr)
837 /* This diagnostic is a bit vague because savedir can fail in
838 several different ways. */
839 error (0, errno, _("cannot access %s"), quoteaf (src_name_in));
840 return false;
843 /* For cp's -H option, dereference command line arguments, but do not
844 dereference symlinks that are found via recursive traversal. */
845 if (x->dereference == DEREF_COMMAND_LINE_ARGUMENTS)
846 non_command_line_options.dereference = DEREF_NEVER;
848 bool new_first_dir_created = false;
849 namep = name_space;
850 while (*namep != '\0')
852 bool local_copy_into_self;
853 char *src_name = file_name_concat (src_name_in, namep, nullptr);
854 char *dst_name = file_name_concat (dst_name_in, namep, nullptr);
855 bool first_dir_created = *first_dir_created_per_command_line_arg;
856 bool rename_succeeded;
858 ok &= copy_internal (src_name, dst_name, dst_dirfd,
859 dst_name + (dst_relname_in - dst_name_in),
860 new_dst, src_sb,
861 ancestors, &non_command_line_options, false,
862 &first_dir_created,
863 &local_copy_into_self, &rename_succeeded);
864 *copy_into_self |= local_copy_into_self;
866 free (dst_name);
867 free (src_name);
869 /* If we're copying into self, there's no point in continuing,
870 and in fact, that would even infloop, now that we record only
871 the first created directory per command line argument. */
872 if (local_copy_into_self)
873 break;
875 new_first_dir_created |= first_dir_created;
876 namep += strlen (namep) + 1;
878 free (name_space);
879 *first_dir_created_per_command_line_arg = new_first_dir_created;
881 return ok;
884 /* Set the owner and owning group of DEST_DESC to the st_uid and
885 st_gid fields of SRC_SB. If DEST_DESC is undefined (-1), set
886 the owner and owning group of DST_NAME aka DST_DIRFD+DST_RELNAME
887 instead; for safety prefer lchownat since no
888 symbolic links should be involved. DEST_DESC must
889 refer to the same file as DST_NAME if defined.
890 Upon failure to set both UID and GID, try to set only the GID.
891 NEW_DST is true if the file was newly created; otherwise,
892 DST_SB is the status of the destination.
893 Return 1 if the initial syscall succeeds, 0 if it fails but it's OK
894 not to preserve ownership, -1 otherwise. */
896 static int
897 set_owner (const struct cp_options *x, char const *dst_name,
898 int dst_dirfd, char const *dst_relname, int dest_desc,
899 struct stat const *src_sb, bool new_dst,
900 struct stat const *dst_sb)
902 uid_t uid = src_sb->st_uid;
903 gid_t gid = src_sb->st_gid;
905 /* Naively changing the ownership of an already-existing file before
906 changing its permissions would create a window of vulnerability if
907 the file's old permissions are too generous for the new owner and
908 group. Avoid the window by first changing to a restrictive
909 temporary mode if necessary. */
911 if (!new_dst && (x->preserve_mode || x->move_mode || x->set_mode))
913 mode_t old_mode = dst_sb->st_mode;
914 mode_t new_mode =
915 (x->preserve_mode || x->move_mode ? src_sb->st_mode : x->mode);
916 mode_t restrictive_temp_mode = old_mode & new_mode & S_IRWXU;
918 if ((USE_ACL
919 || (old_mode & CHMOD_MODE_BITS
920 & (~new_mode | S_ISUID | S_ISGID | S_ISVTX)))
921 && qset_acl (dst_name, dest_desc, restrictive_temp_mode) != 0)
923 if (! owner_failure_ok (x))
924 error (0, errno, _("clearing permissions for %s"),
925 quoteaf (dst_name));
926 return -x->require_preserve;
930 if (HAVE_FCHOWN && dest_desc != -1)
932 if (fchown (dest_desc, uid, gid) == 0)
933 return 1;
934 if (errno == EPERM || errno == EINVAL)
936 /* We've failed to set *both*. Now, try to set just the group
937 ID, but ignore any failure here, and don't change errno. */
938 int saved_errno = errno;
939 ignore_value (fchown (dest_desc, -1, gid));
940 errno = saved_errno;
943 else
945 if (lchownat (dst_dirfd, dst_relname, uid, gid) == 0)
946 return 1;
947 if (errno == EPERM || errno == EINVAL)
949 /* We've failed to set *both*. Now, try to set just the group
950 ID, but ignore any failure here, and don't change errno. */
951 int saved_errno = errno;
952 ignore_value (lchownat (dst_dirfd, dst_relname, -1, gid));
953 errno = saved_errno;
957 if (! chown_failure_ok (x))
959 error (0, errno, _("failed to preserve ownership for %s"),
960 quoteaf (dst_name));
961 if (x->require_preserve)
962 return -1;
965 return 0;
968 /* Set the st_author field of DEST_DESC to the st_author field of
969 SRC_SB. If DEST_DESC is undefined (-1), set the st_author field
970 of DST_NAME instead. DEST_DESC must refer to the same file as
971 DST_NAME if defined. */
973 static void
974 set_author (char const *dst_name, int dest_desc, const struct stat *src_sb)
976 #if HAVE_STRUCT_STAT_ST_AUTHOR
977 /* FIXME: Modify the following code so that it does not
978 follow symbolic links. */
980 /* Preserve the st_author field. */
981 file_t file = (dest_desc < 0
982 ? file_name_lookup (dst_name, 0, 0)
983 : getdport (dest_desc));
984 if (file == MACH_PORT_nullptr)
985 error (0, errno, _("failed to lookup file %s"), quoteaf (dst_name));
986 else
988 error_t err = file_chauthor (file, src_sb->st_author);
989 if (err)
990 error (0, err, _("failed to preserve authorship for %s"),
991 quoteaf (dst_name));
992 mach_port_deallocate (mach_task_self (), file);
994 #else
995 (void) dst_name;
996 (void) dest_desc;
997 (void) src_sb;
998 #endif
1001 /* Set the default security context for the process. New files will
1002 have this security context set. Also existing files can have their
1003 context adjusted based on this process context, by
1004 set_file_security_ctx() called with PROCESS_LOCAL=true.
1005 This should be called before files are created so there is no race
1006 where a file may be present without an appropriate security context.
1007 Based on CP_OPTIONS, diagnose warnings and fail when appropriate.
1008 Return FALSE on failure, TRUE on success. */
1010 bool
1011 set_process_security_ctx (char const *src_name, char const *dst_name,
1012 mode_t mode, bool new_dst, const struct cp_options *x)
1014 if (x->preserve_security_context)
1016 /* Set the default context for the process to match the source. */
1017 bool all_errors = !x->data_copy_required || x->require_preserve_context;
1018 bool some_errors = !all_errors && !x->reduce_diagnostics;
1019 char *con;
1021 if (0 <= lgetfilecon (src_name, &con))
1023 if (setfscreatecon (con) < 0)
1025 if (all_errors || (some_errors && !errno_unsupported (errno)))
1026 error (0, errno,
1027 _("failed to set default file creation context to %s"),
1028 quote (con));
1029 if (x->require_preserve_context)
1031 freecon (con);
1032 return false;
1035 freecon (con);
1037 else
1039 if (all_errors || (some_errors && !errno_unsupported (errno)))
1041 error (0, errno,
1042 _("failed to get security context of %s"),
1043 quoteaf (src_name));
1045 if (x->require_preserve_context)
1046 return false;
1049 else if (x->set_security_context)
1051 /* With -Z, adjust the default context for the process
1052 to have the type component adjusted as per the destination path. */
1053 if (new_dst && defaultcon (x->set_security_context, dst_name, mode) < 0
1054 && ! ignorable_ctx_err (errno))
1056 error (0, errno,
1057 _("failed to set default file creation context for %s"),
1058 quoteaf (dst_name));
1062 return true;
1065 /* Reset the security context of DST_NAME, to that already set
1066 as the process default if !X->set_security_context. Otherwise
1067 adjust the type component of DST_NAME's security context as
1068 per the system default for that path. Issue warnings upon
1069 failure, when allowed by various settings in X.
1070 Return false on failure, true on success. */
1072 bool
1073 set_file_security_ctx (char const *dst_name,
1074 bool recurse, const struct cp_options *x)
1076 bool all_errors = (!x->data_copy_required
1077 || x->require_preserve_context);
1078 bool some_errors = !all_errors && !x->reduce_diagnostics;
1080 if (! restorecon (x->set_security_context, dst_name, recurse))
1082 if (all_errors || (some_errors && !errno_unsupported (errno)))
1083 error (0, errno, _("failed to set the security context of %s"),
1084 quoteaf_n (0, dst_name));
1085 return false;
1088 return true;
1091 /* Change the file mode bits of the file identified by DESC or
1092 DIRFD+NAME to MODE. Use DESC if DESC is valid and fchmod is
1093 available, DIRFD+NAME otherwise. */
1095 static int
1096 fchmod_or_lchmod (int desc, int dirfd, char const *name, mode_t mode)
1098 #if HAVE_FCHMOD
1099 if (0 <= desc)
1100 return fchmod (desc, mode);
1101 #endif
1102 return lchmodat (dirfd, name, mode);
1105 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
1106 # define HAVE_STRUCT_STAT_ST_BLOCKS 0
1107 #endif
1109 /* Type of scan being done on the input when looking for sparseness. */
1110 enum scantype
1112 /* An error was found when determining scantype. */
1113 ERROR_SCANTYPE,
1115 /* No fancy scanning; just read and write. */
1116 PLAIN_SCANTYPE,
1118 /* Read and examine data looking for zero blocks; useful when
1119 attempting to create sparse output. */
1120 ZERO_SCANTYPE,
1122 /* lseek information is available. */
1123 LSEEK_SCANTYPE,
1126 /* Result of infer_scantype. */
1127 union scan_inference
1129 /* Used if infer_scantype returns LSEEK_SCANTYPE. This is the
1130 offset of the first data block, or -1 if the file has no data. */
1131 off_t ext_start;
1134 /* Return how to scan a file with descriptor FD and stat buffer SB.
1135 *SCAN_INFERENCE is set to a valid value if returning LSEEK_SCANTYPE. */
1136 static enum scantype
1137 infer_scantype (int fd, struct stat const *sb,
1138 union scan_inference *scan_inference)
1140 scan_inference->ext_start = -1; /* avoid -Wmaybe-uninitialized */
1142 if (! (HAVE_STRUCT_STAT_ST_BLOCKS
1143 && S_ISREG (sb->st_mode)
1144 && ST_NBLOCKS (*sb) < sb->st_size / ST_NBLOCKSIZE))
1145 return PLAIN_SCANTYPE;
1147 #ifdef SEEK_HOLE
1148 off_t ext_start = lseek (fd, 0, SEEK_DATA);
1149 if (0 <= ext_start || errno == ENXIO)
1151 scan_inference->ext_start = ext_start;
1152 return LSEEK_SCANTYPE;
1154 else if (errno != EINVAL && !is_ENOTSUP (errno))
1155 return ERROR_SCANTYPE;
1156 #endif
1158 return ZERO_SCANTYPE;
1161 #if HAVE_FCLONEFILEAT && !USE_XATTR
1162 # include <sys/acl.h>
1163 /* Return true if FD has a nontrivial ACL. */
1164 static bool
1165 fd_has_acl (int fd)
1167 /* Every platform with fclonefileat (macOS 10.12 or later) also has
1168 acl_get_fd_np. */
1169 bool has_acl = false;
1170 acl_t acl = acl_get_fd_np (fd, ACL_TYPE_EXTENDED);
1171 if (acl)
1173 acl_entry_t ace;
1174 has_acl = 0 <= acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
1175 acl_free (acl);
1177 return has_acl;
1179 #endif
1181 /* Handle failure from FICLONE or fclonefileat.
1182 Return FALSE if it's a terminal failure for this file. */
1184 static bool
1185 handle_clone_fail (int dst_dirfd, char const *dst_relname,
1186 char const *src_name, char const *dst_name,
1187 int dest_desc, bool new_dst, enum Reflink_type reflink_mode)
1189 /* When the clone operation fails, report failure only with errno values
1190 known to mean trouble when the clone is supported and called properly.
1191 Do not report failure merely because !is_CLONENOTSUP (errno),
1192 as systems may yield oddball errno values here with FICLONE,
1193 and is_CLONENOTSUP is not appropriate for fclonefileat. */
1194 bool report_failure = is_terminal_error (errno);
1196 if (reflink_mode == REFLINK_ALWAYS || report_failure)
1197 error (0, errno, _("failed to clone %s from %s"),
1198 quoteaf_n (0, dst_name), quoteaf_n (1, src_name));
1200 /* Remove the destination if cp --reflink=always created it
1201 but cloned no data. */
1202 if (new_dst /* currently not for fclonefileat(). */
1203 && reflink_mode == REFLINK_ALWAYS
1204 && ((! report_failure) || lseek (dest_desc, 0, SEEK_END) == 0)
1205 && unlinkat (dst_dirfd, dst_relname, 0) != 0 && errno != ENOENT)
1206 error (0, errno, _("cannot remove %s"), quoteaf (dst_name));
1208 if (! report_failure)
1209 copy_debug.reflink = COPY_DEBUG_UNSUPPORTED;
1211 if (reflink_mode == REFLINK_ALWAYS || report_failure)
1212 return false;
1214 return true;
1218 /* Copy a regular file from SRC_NAME to DST_NAME aka DST_DIRFD+DST_RELNAME.
1219 If the source file contains holes, copies holes and blocks of zeros
1220 in the source file as holes in the destination file.
1221 (Holes are read as zeroes by the 'read' system call.)
1222 When creating the destination, use DST_MODE & ~OMITTED_PERMISSIONS
1223 as the third argument in the call to open, adding
1224 OMITTED_PERMISSIONS after copying as needed.
1225 X provides many option settings.
1226 Return true if successful.
1227 *NEW_DST is initially as in copy_internal.
1228 If successful, set *NEW_DST to true if the destination file was created and
1229 to false otherwise; if unsuccessful, perhaps set *NEW_DST to some value.
1230 SRC_SB is the result of calling follow_fstatat on SRC_NAME. */
1232 static bool
1233 copy_reg (char const *src_name, char const *dst_name,
1234 int dst_dirfd, char const *dst_relname,
1235 const struct cp_options *x,
1236 mode_t dst_mode, mode_t omitted_permissions, bool *new_dst,
1237 struct stat const *src_sb)
1239 char *buf = nullptr;
1240 int dest_desc;
1241 int dest_errno;
1242 int source_desc;
1243 mode_t src_mode = src_sb->st_mode;
1244 mode_t extra_permissions;
1245 struct stat sb;
1246 struct stat src_open_sb;
1247 union scan_inference scan_inference;
1248 bool return_val = true;
1249 bool data_copy_required = x->data_copy_required;
1250 bool preserve_xattr = USE_XATTR & x->preserve_xattr;
1252 copy_debug.offload = COPY_DEBUG_UNKNOWN;
1253 copy_debug.reflink = x->reflink_mode ? COPY_DEBUG_UNKNOWN : COPY_DEBUG_NO;
1254 copy_debug.sparse_detection = COPY_DEBUG_UNKNOWN;
1256 source_desc = open (src_name,
1257 (O_RDONLY | O_BINARY
1258 | (x->dereference == DEREF_NEVER ? O_NOFOLLOW : 0)));
1259 if (source_desc < 0)
1261 error (0, errno, _("cannot open %s for reading"), quoteaf (src_name));
1262 return false;
1265 if (fstat (source_desc, &src_open_sb) != 0)
1267 error (0, errno, _("cannot fstat %s"), quoteaf (src_name));
1268 return_val = false;
1269 goto close_src_desc;
1272 /* Compare the source dev/ino from the open file to the incoming,
1273 saved ones obtained via a previous call to stat. */
1274 if (! SAME_INODE (*src_sb, src_open_sb))
1276 error (0, 0,
1277 _("skipping file %s, as it was replaced while being copied"),
1278 quoteaf (src_name));
1279 return_val = false;
1280 goto close_src_desc;
1283 /* The semantics of the following open calls are mandated
1284 by the specs for both cp and mv. */
1285 if (! *new_dst)
1287 int open_flags =
1288 O_WRONLY | O_BINARY | (data_copy_required ? O_TRUNC : 0);
1289 dest_desc = openat (dst_dirfd, dst_relname, open_flags);
1290 dest_errno = errno;
1292 /* When using cp --preserve=context to copy to an existing destination,
1293 reset the context as per the default context, which has already been
1294 set according to the src.
1295 When using the mutually exclusive -Z option, then adjust the type of
1296 the existing context according to the system default for the dest.
1297 Note we set the context here, _after_ the file is opened, lest the
1298 new context disallow that. */
1299 if (0 <= dest_desc
1300 && (x->set_security_context || x->preserve_security_context))
1302 if (! set_file_security_ctx (dst_name, false, x))
1304 if (x->require_preserve_context)
1306 return_val = false;
1307 goto close_src_and_dst_desc;
1312 if (dest_desc < 0 && dest_errno != ENOENT
1313 && x->unlink_dest_after_failed_open)
1315 if (unlinkat (dst_dirfd, dst_relname, 0) == 0)
1317 if (x->verbose)
1318 printf (_("removed %s\n"), quoteaf (dst_name));
1320 else if (errno != ENOENT)
1322 error (0, errno, _("cannot remove %s"), quoteaf (dst_name));
1323 return_val = false;
1324 goto close_src_desc;
1327 dest_errno = ENOENT;
1330 if (dest_desc < 0 && dest_errno == ENOENT)
1332 /* Ensure there is no race where a file may be left without
1333 an appropriate security context. */
1334 if (x->set_security_context)
1336 if (! set_process_security_ctx (src_name, dst_name, dst_mode,
1337 true, x))
1339 return_val = false;
1340 goto close_src_desc;
1344 /* Tell caller that the destination file is created. */
1345 *new_dst = true;
1349 if (*new_dst)
1351 #if HAVE_FCLONEFILEAT && !USE_XATTR
1352 # ifndef CLONE_ACL
1353 # define CLONE_ACL 0 /* Added in macOS 12.6. */
1354 # endif
1355 # ifndef CLONE_NOOWNERCOPY
1356 # define CLONE_NOOWNERCOPY 0 /* Added in macOS 10.13. */
1357 # endif
1358 /* Try fclonefileat if copying data in reflink mode.
1359 Use CLONE_NOFOLLOW to avoid security issues that could occur
1360 if writing through dangling symlinks. Although the circa
1361 2023 macOS documentation doesn't say so, CLONE_NOFOLLOW
1362 affects the destination file too. */
1363 if (data_copy_required && x->reflink_mode
1364 && (CLONE_NOOWNERCOPY || x->preserve_ownership))
1366 /* Try fclonefileat so long as it won't create the
1367 destination with unwanted permissions, which could lead
1368 to a security race. */
1369 mode_t cloned_mode_bits = S_ISVTX | S_IRWXUGO;
1370 mode_t cloned_mode = src_mode & cloned_mode_bits;
1371 mode_t desired_mode
1372 = (x->preserve_mode ? src_mode & CHMOD_MODE_BITS
1373 : x->set_mode ? x->mode
1374 : ((x->explicit_no_preserve_mode ? MODE_RW_UGO : dst_mode)
1375 & ~ cached_umask ()));
1376 if (! (cloned_mode & ~desired_mode))
1378 int fc_flags
1379 = (CLONE_NOFOLLOW
1380 | (x->preserve_mode ? CLONE_ACL : 0)
1381 | (x->preserve_ownership ? 0 : CLONE_NOOWNERCOPY));
1382 int s = fclonefileat (source_desc, dst_dirfd, dst_relname,
1383 fc_flags);
1384 if (s != 0 && (fc_flags & CLONE_ACL) && errno == EINVAL)
1386 fc_flags &= ~CLONE_ACL;
1387 s = fclonefileat (source_desc, dst_dirfd, dst_relname,
1388 fc_flags);
1390 if (s == 0)
1392 copy_debug.reflink = COPY_DEBUG_YES;
1394 /* Update the clone's timestamps and permissions
1395 as needed. */
1397 if (!x->preserve_timestamps)
1399 struct timespec timespec[2];
1400 timespec[0].tv_nsec = timespec[1].tv_nsec = UTIME_NOW;
1401 if (utimensat (dst_dirfd, dst_relname, timespec,
1402 AT_SYMLINK_NOFOLLOW)
1403 != 0)
1405 error (0, errno, _("updating times for %s"),
1406 quoteaf (dst_name));
1407 return_val = false;
1408 goto close_src_desc;
1412 extra_permissions = desired_mode & ~cloned_mode;
1413 if (!extra_permissions
1414 && (!x->preserve_mode || (fc_flags & CLONE_ACL)
1415 || !fd_has_acl (source_desc)))
1417 goto close_src_desc;
1420 /* Either some desired permissions were not cloned,
1421 or ACLs were not cloned despite that being requested. */
1422 omitted_permissions = 0;
1423 dest_desc = -1;
1424 goto set_dest_mode;
1426 if (! handle_clone_fail (dst_dirfd, dst_relname, src_name,
1427 dst_name,
1428 -1, false /* We didn't create dst */,
1429 x->reflink_mode))
1431 return_val = false;
1432 goto close_src_desc;
1435 else
1436 copy_debug.reflink = COPY_DEBUG_AVOIDED;
1438 else if (data_copy_required && x->reflink_mode)
1440 if (! CLONE_NOOWNERCOPY)
1441 copy_debug.reflink = COPY_DEBUG_AVOIDED;
1443 #endif
1445 /* To allow copying xattrs on read-only files, create with u+w.
1446 This satisfies an inode permission check done by
1447 xattr_permission in fs/xattr.c of the GNU/Linux kernel. */
1448 mode_t open_mode =
1449 ((dst_mode & ~omitted_permissions)
1450 | (preserve_xattr && !x->owner_privileges ? S_IWUSR : 0));
1451 extra_permissions = open_mode & ~dst_mode; /* either 0 or S_IWUSR */
1453 int open_flags = O_WRONLY | O_CREAT | O_BINARY;
1454 dest_desc = openat (dst_dirfd, dst_relname, open_flags | O_EXCL,
1455 open_mode);
1456 dest_errno = errno;
1458 /* When trying to copy through a dangling destination symlink,
1459 the above open fails with EEXIST. If that happens, and
1460 readlinkat shows that it is a symlink, then we
1461 have a problem: trying to resolve this dangling symlink to
1462 a directory/destination-entry pair is fundamentally racy,
1463 so punt. If x->open_dangling_dest_symlink is set (cp sets
1464 that when POSIXLY_CORRECT is set in the environment), simply
1465 call open again, but without O_EXCL (potentially dangerous).
1466 If not, fail with a diagnostic. These shenanigans are necessary
1467 only when copying, i.e., not in move_mode. */
1468 if (dest_desc < 0 && dest_errno == EEXIST && ! x->move_mode)
1470 char dummy[1];
1471 if (0 <= readlinkat (dst_dirfd, dst_relname, dummy, sizeof dummy))
1473 if (x->open_dangling_dest_symlink)
1475 dest_desc = openat (dst_dirfd, dst_relname,
1476 open_flags, open_mode);
1477 dest_errno = errno;
1479 else
1481 error (0, 0, _("not writing through dangling symlink %s"),
1482 quoteaf (dst_name));
1483 return_val = false;
1484 goto close_src_desc;
1489 /* Improve quality of diagnostic when a nonexistent dst_name
1490 ends in a slash and open fails with errno == EISDIR. */
1491 if (dest_desc < 0 && dest_errno == EISDIR
1492 && *dst_name && dst_name[strlen (dst_name) - 1] == '/')
1493 dest_errno = ENOTDIR;
1495 else
1497 omitted_permissions = extra_permissions = 0;
1500 if (dest_desc < 0)
1502 error (0, dest_errno, _("cannot create regular file %s"),
1503 quoteaf (dst_name));
1504 return_val = false;
1505 goto close_src_desc;
1508 /* --attributes-only overrides --reflink. */
1509 if (data_copy_required && x->reflink_mode)
1511 if (clone_file (dest_desc, source_desc) == 0)
1513 data_copy_required = false;
1514 copy_debug.reflink = COPY_DEBUG_YES;
1516 else
1518 if (! handle_clone_fail (dst_dirfd, dst_relname, src_name, dst_name,
1519 dest_desc, *new_dst, x->reflink_mode))
1521 return_val = false;
1522 goto close_src_and_dst_desc;
1527 if (! (data_copy_required | x->preserve_ownership | extra_permissions))
1528 sb.st_mode = 0;
1529 else if (fstat (dest_desc, &sb) != 0)
1531 error (0, errno, _("cannot fstat %s"), quoteaf (dst_name));
1532 return_val = false;
1533 goto close_src_and_dst_desc;
1536 /* If extra permissions needed for copy_xattr didn't happen (e.g.,
1537 due to umask) chmod to add them temporarily; if that fails give
1538 up with extra permissions, letting copy_attr fail later. */
1539 mode_t temporary_mode = sb.st_mode | extra_permissions;
1540 if (temporary_mode != sb.st_mode
1541 && (fchmod_or_lchmod (dest_desc, dst_dirfd, dst_relname, temporary_mode)
1542 != 0))
1543 extra_permissions = 0;
1545 if (data_copy_required)
1547 /* Choose a suitable buffer size; it may be adjusted later. */
1548 size_t buf_size = io_blksize (sb);
1549 size_t hole_size = ST_BLKSIZE (sb);
1551 /* Deal with sparse files. */
1552 enum scantype scantype = infer_scantype (source_desc, &src_open_sb,
1553 &scan_inference);
1554 if (scantype == ERROR_SCANTYPE)
1556 error (0, errno, _("cannot lseek %s"), quoteaf (src_name));
1557 return_val = false;
1558 goto close_src_and_dst_desc;
1560 bool make_holes
1561 = (S_ISREG (sb.st_mode)
1562 && (x->sparse_mode == SPARSE_ALWAYS
1563 || (x->sparse_mode == SPARSE_AUTO
1564 && scantype != PLAIN_SCANTYPE)));
1566 fdadvise (source_desc, 0, 0, FADVISE_SEQUENTIAL);
1568 /* If not making a sparse file, try to use a more-efficient
1569 buffer size. */
1570 if (! make_holes)
1572 /* Compute the least common multiple of the input and output
1573 buffer sizes, adjusting for outlandish values.
1574 Note we read in multiples of the reported block size
1575 to support (unusual) devices that have this constraint. */
1576 size_t blcm_max = MIN (SIZE_MAX, SSIZE_MAX);
1577 size_t blcm = buffer_lcm (io_blksize (src_open_sb), buf_size,
1578 blcm_max);
1580 /* Do not bother with a buffer larger than the input file, plus one
1581 byte to make sure the file has not grown while reading it. */
1582 if (S_ISREG (src_open_sb.st_mode) && src_open_sb.st_size < buf_size)
1583 buf_size = src_open_sb.st_size + 1;
1585 /* However, stick with a block size that is a positive multiple of
1586 blcm, overriding the above adjustments. Watch out for
1587 overflow. */
1588 buf_size += blcm - 1;
1589 buf_size -= buf_size % blcm;
1590 if (buf_size == 0 || blcm_max < buf_size)
1591 buf_size = blcm;
1594 off_t n_read;
1595 bool wrote_hole_at_eof = false;
1596 if (! (
1597 #ifdef SEEK_HOLE
1598 scantype == LSEEK_SCANTYPE
1599 ? lseek_copy (source_desc, dest_desc, &buf, buf_size, hole_size,
1600 scan_inference.ext_start, src_open_sb.st_size,
1601 make_holes ? x->sparse_mode : SPARSE_NEVER,
1602 x->reflink_mode != REFLINK_NEVER,
1603 src_name, dst_name)
1605 #endif
1606 sparse_copy (source_desc, dest_desc, &buf, buf_size,
1607 make_holes ? hole_size : 0,
1608 x->sparse_mode == SPARSE_ALWAYS,
1609 x->reflink_mode != REFLINK_NEVER,
1610 src_name, dst_name, UINTMAX_MAX, &n_read,
1611 &wrote_hole_at_eof)))
1613 return_val = false;
1614 goto close_src_and_dst_desc;
1616 else if (wrote_hole_at_eof && ftruncate (dest_desc, n_read) < 0)
1618 error (0, errno, _("failed to extend %s"), quoteaf (dst_name));
1619 return_val = false;
1620 goto close_src_and_dst_desc;
1624 if (x->preserve_timestamps)
1626 struct timespec timespec[2];
1627 timespec[0] = get_stat_atime (src_sb);
1628 timespec[1] = get_stat_mtime (src_sb);
1630 if (fdutimensat (dest_desc, dst_dirfd, dst_relname, timespec, 0) != 0)
1632 error (0, errno, _("preserving times for %s"), quoteaf (dst_name));
1633 if (x->require_preserve)
1635 return_val = false;
1636 goto close_src_and_dst_desc;
1641 /* Set ownership before xattrs as changing owners will
1642 clear capabilities. */
1643 if (x->preserve_ownership && ! SAME_OWNER_AND_GROUP (*src_sb, sb))
1645 switch (set_owner (x, dst_name, dst_dirfd, dst_relname, dest_desc,
1646 src_sb, *new_dst, &sb))
1648 case -1:
1649 return_val = false;
1650 goto close_src_and_dst_desc;
1652 case 0:
1653 src_mode &= ~ (S_ISUID | S_ISGID | S_ISVTX);
1654 break;
1658 if (preserve_xattr)
1660 if (!copy_attr (src_name, source_desc, dst_name, dest_desc, x)
1661 && x->require_preserve_xattr)
1662 return_val = false;
1665 set_author (dst_name, dest_desc, src_sb);
1667 #if HAVE_FCLONEFILEAT && !USE_XATTR
1668 set_dest_mode:
1669 #endif
1670 if (x->preserve_mode || x->move_mode)
1672 if (copy_acl (src_name, source_desc, dst_name, dest_desc, src_mode) != 0
1673 && x->require_preserve)
1674 return_val = false;
1676 else if (x->set_mode)
1678 if (set_acl (dst_name, dest_desc, x->mode) != 0)
1679 return_val = false;
1681 else if (x->explicit_no_preserve_mode && *new_dst)
1683 if (set_acl (dst_name, dest_desc, MODE_RW_UGO & ~cached_umask ()) != 0)
1684 return_val = false;
1686 else if (omitted_permissions | extra_permissions)
1688 omitted_permissions &= ~ cached_umask ();
1689 if ((omitted_permissions | extra_permissions)
1690 && (fchmod_or_lchmod (dest_desc, dst_dirfd, dst_relname,
1691 dst_mode & ~ cached_umask ())
1692 != 0))
1694 error (0, errno, _("preserving permissions for %s"),
1695 quoteaf (dst_name));
1696 if (x->require_preserve)
1697 return_val = false;
1701 if (dest_desc < 0)
1702 goto close_src_desc;
1704 close_src_and_dst_desc:
1705 if (close (dest_desc) < 0)
1707 error (0, errno, _("failed to close %s"), quoteaf (dst_name));
1708 return_val = false;
1710 close_src_desc:
1711 if (close (source_desc) < 0)
1713 error (0, errno, _("failed to close %s"), quoteaf (src_name));
1714 return_val = false;
1717 /* Output debug info for data copying operations. */
1718 if (x->debug)
1719 emit_debug (x);
1721 alignfree (buf);
1722 return return_val;
1725 /* Return whether it's OK that two files are the "same" by some measure.
1726 The first file is SRC_NAME and has status SRC_SB.
1727 The second is DST_DIRFD+DST_RELNAME and has status DST_SB.
1728 The copying options are X. The goal is to avoid
1729 making the 'copy' operation remove both copies of the file
1730 in that case, while still allowing the user to e.g., move or
1731 copy a regular file onto a symlink that points to it.
1732 Try to minimize the cost of this function in the common case.
1733 Set *RETURN_NOW if we've determined that the caller has no more
1734 work to do and should return successfully, right away. */
1736 static bool
1737 same_file_ok (char const *src_name, struct stat const *src_sb,
1738 int dst_dirfd, char const *dst_relname, struct stat const *dst_sb,
1739 const struct cp_options *x, bool *return_now)
1741 const struct stat *src_sb_link;
1742 const struct stat *dst_sb_link;
1743 struct stat tmp_dst_sb;
1744 struct stat tmp_src_sb;
1746 bool same_link;
1747 bool same = SAME_INODE (*src_sb, *dst_sb);
1749 *return_now = false;
1751 /* FIXME: this should (at the very least) be moved into the following
1752 if-block. More likely, it should be removed, because it inhibits
1753 making backups. But removing it will result in a change in behavior
1754 that will probably have to be documented -- and tests will have to
1755 be updated. */
1756 if (same && x->hard_link)
1758 *return_now = true;
1759 return true;
1762 if (x->dereference == DEREF_NEVER)
1764 same_link = same;
1766 /* If both the source and destination files are symlinks (and we'll
1767 know this here IFF preserving symlinks), then it's usually ok
1768 when they are distinct. */
1769 if (S_ISLNK (src_sb->st_mode) && S_ISLNK (dst_sb->st_mode))
1771 bool sn = same_nameat (AT_FDCWD, src_name, dst_dirfd, dst_relname);
1772 if ( ! sn)
1774 /* It's fine when we're making any type of backup. */
1775 if (x->backup_type != no_backups)
1776 return true;
1778 /* Here we have two symlinks that are hard-linked together,
1779 and we're not making backups. In this unusual case, simply
1780 returning true would lead to mv calling "rename(A,B)",
1781 which would do nothing and return 0. */
1782 if (same_link)
1784 *return_now = true;
1785 return ! x->move_mode;
1789 return ! sn;
1792 src_sb_link = src_sb;
1793 dst_sb_link = dst_sb;
1795 else
1797 if (!same)
1798 return true;
1800 if (fstatat (dst_dirfd, dst_relname, &tmp_dst_sb,
1801 AT_SYMLINK_NOFOLLOW) != 0
1802 || lstat (src_name, &tmp_src_sb) != 0)
1803 return true;
1805 src_sb_link = &tmp_src_sb;
1806 dst_sb_link = &tmp_dst_sb;
1808 same_link = SAME_INODE (*src_sb_link, *dst_sb_link);
1810 /* If both are symlinks, then it's ok, but only if the destination
1811 will be unlinked before being opened. This is like the test
1812 above, but with the addition of the unlink_dest_before_opening
1813 conjunct because otherwise, with two symlinks to the same target,
1814 we'd end up truncating the source file. */
1815 if (S_ISLNK (src_sb_link->st_mode) && S_ISLNK (dst_sb_link->st_mode)
1816 && x->unlink_dest_before_opening)
1817 return true;
1820 /* The backup code ensures there's a copy, so it's usually ok to
1821 remove any destination file. One exception is when both
1822 source and destination are the same directory entry. In that
1823 case, moving the destination file aside (in making the backup)
1824 would also rename the source file and result in an error. */
1825 if (x->backup_type != no_backups)
1827 if (!same_link)
1829 /* In copy mode when dereferencing symlinks, if the source is a
1830 symlink and the dest is not, then backing up the destination
1831 (moving it aside) would make it a dangling symlink, and the
1832 subsequent attempt to open it in copy_reg would fail with
1833 a misleading diagnostic. Avoid that by returning zero in
1834 that case so the caller can make cp (or mv when it has to
1835 resort to reading the source file) fail now. */
1837 /* FIXME-note: even with the following kludge, we can still provoke
1838 the offending diagnostic. It's just a little harder to do :-)
1839 $ rm -f a b c; touch c; ln -s c b; ln -s b a; cp -b a b
1840 cp: cannot open 'a' for reading: No such file or directory
1841 That's misleading, since a subsequent 'ls' shows that 'a'
1842 is still there.
1843 One solution would be to open the source file *before* moving
1844 aside the destination, but that'd involve a big rewrite. */
1845 if ( ! x->move_mode
1846 && x->dereference != DEREF_NEVER
1847 && S_ISLNK (src_sb_link->st_mode)
1848 && ! S_ISLNK (dst_sb_link->st_mode))
1849 return false;
1851 return true;
1854 /* FIXME: What about case insensitive file systems ? */
1855 return ! same_nameat (AT_FDCWD, src_name, dst_dirfd, dst_relname);
1858 #if 0
1859 /* FIXME: use or remove */
1861 /* If we're making a backup, we'll detect the problem case in
1862 copy_reg because SRC_NAME will no longer exist. Allowing
1863 the test to be deferred lets cp do some useful things.
1864 But when creating hardlinks and SRC_NAME is a symlink
1865 but DST_RELNAME is not we must test anyway. */
1866 if (x->hard_link
1867 || !S_ISLNK (src_sb_link->st_mode)
1868 || S_ISLNK (dst_sb_link->st_mode))
1869 return true;
1871 if (x->dereference != DEREF_NEVER)
1872 return true;
1873 #endif
1875 if (x->move_mode || x->unlink_dest_before_opening)
1877 /* They may refer to the same file if we're in move mode and the
1878 target is a symlink. That is ok, since we remove any existing
1879 destination file before opening it -- via 'rename' if they're on
1880 the same file system, via unlinkat otherwise. */
1881 if (S_ISLNK (dst_sb_link->st_mode))
1882 return true;
1884 /* It's not ok if they're distinct hard links to the same file as
1885 this causes a race condition and we may lose data in this case. */
1886 if (same_link
1887 && 1 < dst_sb_link->st_nlink
1888 && ! same_nameat (AT_FDCWD, src_name, dst_dirfd, dst_relname))
1889 return ! x->move_mode;
1892 /* If neither is a symlink, then it's ok as long as they aren't
1893 hard links to the same file. */
1894 if (!S_ISLNK (src_sb_link->st_mode) && !S_ISLNK (dst_sb_link->st_mode))
1896 if (!SAME_INODE (*src_sb_link, *dst_sb_link))
1897 return true;
1899 /* If they are the same file, it's ok if we're making hard links. */
1900 if (x->hard_link)
1902 *return_now = true;
1903 return true;
1907 /* At this point, it is normally an error (data loss) to move a symlink
1908 onto its referent, but in at least one narrow case, it is not:
1909 In move mode, when
1910 1) src is a symlink,
1911 2) dest has a link count of 2 or more and
1912 3) dest and the referent of src are not the same directory entry,
1913 then it's ok, since while we'll lose one of those hard links,
1914 src will still point to a remaining link.
1915 Note that technically, condition #3 obviates condition #2, but we
1916 retain the 1 < st_nlink condition because that means fewer invocations
1917 of the more expensive #3.
1919 Given this,
1920 $ touch f && ln f l && ln -s f s
1921 $ ls -og f l s
1922 -rw-------. 2 0 Jan 4 22:46 f
1923 -rw-------. 2 0 Jan 4 22:46 l
1924 lrwxrwxrwx. 1 1 Jan 4 22:46 s -> f
1925 this must fail: mv s f
1926 this must succeed: mv s l */
1927 if (x->move_mode
1928 && S_ISLNK (src_sb->st_mode)
1929 && 1 < dst_sb_link->st_nlink)
1931 char *abs_src = canonicalize_file_name (src_name);
1932 if (abs_src)
1934 bool result = ! same_nameat (AT_FDCWD, abs_src,
1935 dst_dirfd, dst_relname);
1936 free (abs_src);
1937 return result;
1941 /* It's ok to recreate a destination symlink. */
1942 if (x->symbolic_link && S_ISLNK (dst_sb_link->st_mode))
1943 return true;
1945 if (x->dereference == DEREF_NEVER)
1947 if ( ! S_ISLNK (src_sb_link->st_mode))
1948 tmp_src_sb = *src_sb_link;
1949 else if (stat (src_name, &tmp_src_sb) != 0)
1950 return true;
1952 if ( ! S_ISLNK (dst_sb_link->st_mode))
1953 tmp_dst_sb = *dst_sb_link;
1954 else if (fstatat (dst_dirfd, dst_relname, &tmp_dst_sb, 0) != 0)
1955 return true;
1957 if ( ! SAME_INODE (tmp_src_sb, tmp_dst_sb))
1958 return true;
1960 if (x->hard_link)
1962 /* It's ok to attempt to hardlink the same file,
1963 and return early if not replacing a symlink.
1964 Note we need to return early to avoid a later
1965 unlink() of DST (when SRC is a symlink). */
1966 *return_now = ! S_ISLNK (dst_sb_link->st_mode);
1967 return true;
1971 return false;
1974 /* Return whether DST_DIRFD+DST_RELNAME, with mode MODE,
1975 is writable in the sense of 'mv'.
1976 Always consider a symbolic link to be writable. */
1977 static bool
1978 writable_destination (int dst_dirfd, char const *dst_relname, mode_t mode)
1980 return (S_ISLNK (mode)
1981 || can_write_any_file ()
1982 || faccessat (dst_dirfd, dst_relname, W_OK, AT_EACCESS) == 0);
1985 static bool
1986 overwrite_ok (struct cp_options const *x, char const *dst_name,
1987 int dst_dirfd, char const *dst_relname,
1988 struct stat const *dst_sb)
1990 if (! writable_destination (dst_dirfd, dst_relname, dst_sb->st_mode))
1992 char perms[12]; /* "-rwxrwxrwx " ls-style modes. */
1993 strmode (dst_sb->st_mode, perms);
1994 perms[10] = '\0';
1995 fprintf (stderr,
1996 (x->move_mode || x->unlink_dest_before_opening
1997 || x->unlink_dest_after_failed_open)
1998 ? _("%s: replace %s, overriding mode %04lo (%s)? ")
1999 : _("%s: unwritable %s (mode %04lo, %s); try anyway? "),
2000 program_name, quoteaf (dst_name),
2001 (unsigned long int) (dst_sb->st_mode & CHMOD_MODE_BITS),
2002 &perms[1]);
2004 else
2006 fprintf (stderr, _("%s: overwrite %s? "),
2007 program_name, quoteaf (dst_name));
2010 return yesno ();
2013 /* Initialize the hash table implementing a set of F_triple entries
2014 corresponding to destination files. */
2015 extern void
2016 dest_info_init (struct cp_options *x)
2018 x->dest_info
2019 = hash_initialize (DEST_INFO_INITIAL_CAPACITY,
2020 nullptr,
2021 triple_hash,
2022 triple_compare,
2023 triple_free);
2024 if (! x->dest_info)
2025 xalloc_die ();
2028 /* Initialize the hash table implementing a set of F_triple entries
2029 corresponding to source files listed on the command line. */
2030 extern void
2031 src_info_init (struct cp_options *x)
2034 /* Note that we use triple_hash_no_name here.
2035 Contrast with the use of triple_hash above.
2036 That is necessary because a source file may be specified
2037 in many different ways. We want to warn about this
2038 cp a a d/
2039 as well as this:
2040 cp a ./a d/
2042 x->src_info
2043 = hash_initialize (DEST_INFO_INITIAL_CAPACITY,
2044 nullptr,
2045 triple_hash_no_name,
2046 triple_compare,
2047 triple_free);
2048 if (! x->src_info)
2049 xalloc_die ();
2052 /* When effecting a move (e.g., for mv(1)), and given the name DST_NAME
2053 aka DST_DIRFD+DST_RELNAME
2054 of the destination and a corresponding stat buffer, DST_SB, return
2055 true if the logical 'move' operation should _not_ proceed.
2056 Otherwise, return false.
2057 Depending on options specified in X, this code may issue an
2058 interactive prompt asking whether it's ok to overwrite DST_NAME. */
2059 static bool
2060 abandon_move (const struct cp_options *x,
2061 char const *dst_name,
2062 int dst_dirfd, char const *dst_relname,
2063 struct stat const *dst_sb)
2065 affirm (x->move_mode);
2066 return (x->interactive == I_ALWAYS_NO
2067 || x->interactive == I_ALWAYS_SKIP
2068 || ((x->interactive == I_ASK_USER
2069 || (x->interactive == I_UNSPECIFIED
2070 && x->stdin_tty
2071 && ! writable_destination (dst_dirfd, dst_relname,
2072 dst_sb->st_mode)))
2073 && ! overwrite_ok (x, dst_name, dst_dirfd, dst_relname, dst_sb)));
2076 /* Print --verbose output on standard output, e.g. 'new' -> 'old'.
2077 If BACKUP_DST_NAME is non-null, then also indicate that it is
2078 the name of a backup file. */
2079 static void
2080 emit_verbose (char const *src, char const *dst, char const *backup_dst_name)
2082 printf ("%s -> %s", quoteaf_n (0, src), quoteaf_n (1, dst));
2083 if (backup_dst_name)
2084 printf (_(" (backup: %s)"), quoteaf (backup_dst_name));
2085 putchar ('\n');
2088 /* A wrapper around "setfscreatecon (nullptr)" that exits upon failure. */
2089 static void
2090 restore_default_fscreatecon_or_die (void)
2092 if (setfscreatecon (nullptr) != 0)
2093 error (EXIT_FAILURE, errno,
2094 _("failed to restore the default file creation context"));
2097 /* Return a newly-allocated string that is like STR
2098 except replace its suffix SUFFIX with NEWSUFFIX. */
2099 static char *
2100 subst_suffix (char const *str, char const *suffix, char const *newsuffix)
2102 idx_t prefixlen = suffix - str;
2103 idx_t newsuffixsize = strlen (newsuffix) + 1;
2104 char *r = ximalloc (prefixlen + newsuffixsize);
2105 memcpy (r + prefixlen, newsuffix, newsuffixsize);
2106 return memcpy (r, str, prefixlen);
2109 /* Create a hard link to SRC_NAME aka SRC_DIRFD+SRC_RELNAME;
2110 the new link is at DST_NAME aka DST_DIRFD+DST_RELNAME.
2111 A null SRC_NAME stands for the file whose name is like DST_NAME
2112 except with DST_RELNAME replaced with SRC_RELNAME.
2113 Honor the REPLACE, VERBOSE and DEREFERENCE settings.
2114 Return true upon success. Otherwise, diagnose the
2115 failure and return false. If SRC_NAME is a symbolic link, then it will not
2116 be followed unless DEREFERENCE is true.
2117 If the system doesn't support hard links to symbolic links, then DST_NAME
2118 will be created as a symbolic link to SRC_NAME. */
2119 static bool
2120 create_hard_link (char const *src_name, int src_dirfd, char const *src_relname,
2121 char const *dst_name, int dst_dirfd, char const *dst_relname,
2122 bool replace, bool verbose, bool dereference)
2124 int err = force_linkat (src_dirfd, src_relname, dst_dirfd, dst_relname,
2125 dereference ? AT_SYMLINK_FOLLOW : 0,
2126 replace, -1);
2127 if (0 < err)
2130 char *a_src_name = nullptr;
2131 if (!src_name)
2132 src_name = a_src_name = subst_suffix (dst_name, dst_relname,
2133 src_relname);
2134 error (0, err, _("cannot create hard link %s to %s"),
2135 quoteaf_n (0, dst_name), quoteaf_n (1, src_name));
2136 free (a_src_name);
2137 return false;
2139 if (err < 0 && verbose)
2140 printf (_("removed %s\n"), quoteaf (dst_name));
2141 return true;
2144 /* Return true if the current file should be (tried to be) dereferenced:
2145 either for DEREF_ALWAYS or for DEREF_COMMAND_LINE_ARGUMENTS in the case
2146 where the current file is a COMMAND_LINE_ARG; otherwise return false. */
2147 ATTRIBUTE_PURE
2148 static inline bool
2149 should_dereference (const struct cp_options *x, bool command_line_arg)
2151 return x->dereference == DEREF_ALWAYS
2152 || (x->dereference == DEREF_COMMAND_LINE_ARGUMENTS
2153 && command_line_arg);
2156 /* Return true if the source file with basename SRCBASE and status SRC_ST
2157 is likely to be the simple backup file for DST_DIRFD+DST_RELNAME. */
2158 static bool
2159 source_is_dst_backup (char const *srcbase, struct stat const *src_st,
2160 int dst_dirfd, char const *dst_relname)
2162 size_t srcbaselen = strlen (srcbase);
2163 char const *dstbase = last_component (dst_relname);
2164 size_t dstbaselen = strlen (dstbase);
2165 size_t suffixlen = strlen (simple_backup_suffix);
2166 if (! (srcbaselen == dstbaselen + suffixlen
2167 && memcmp (srcbase, dstbase, dstbaselen) == 0
2168 && STREQ (srcbase + dstbaselen, simple_backup_suffix)))
2169 return false;
2170 char *dst_back = subst_suffix (dst_relname,
2171 dst_relname + strlen (dst_relname),
2172 simple_backup_suffix);
2173 struct stat dst_back_sb;
2174 int dst_back_status = fstatat (dst_dirfd, dst_back, &dst_back_sb, 0);
2175 free (dst_back);
2176 return dst_back_status == 0 && SAME_INODE (*src_st, dst_back_sb);
2179 /* Copy the file SRC_NAME to the file DST_NAME aka DST_DIRFD+DST_RELNAME.
2180 If NONEXISTENT_DST is positive, DST_NAME does not exist even as a
2181 dangling symlink; if negative, it does not exist except possibly
2182 as a dangling symlink; if zero, its existence status is unknown.
2183 A non-null PARENT describes the parent directory.
2184 ANCESTORS points to a linked, null terminated list of
2185 devices and inodes of parent directories of SRC_NAME.
2186 X summarizes the command-line options.
2187 COMMAND_LINE_ARG means SRC_NAME was specified on the command line.
2188 FIRST_DIR_CREATED_PER_COMMAND_LINE_ARG is both input and output.
2189 Set *COPY_INTO_SELF if SRC_NAME is a parent of (or the
2190 same as) DST_NAME; otherwise, clear it.
2191 If X->move_mode, set *RENAME_SUCCEEDED according to whether
2192 the source was simply renamed to the destination.
2193 Return true if successful. */
2194 static bool
2195 copy_internal (char const *src_name, char const *dst_name,
2196 int dst_dirfd, char const *dst_relname,
2197 int nonexistent_dst,
2198 struct stat const *parent,
2199 struct dir_list *ancestors,
2200 const struct cp_options *x,
2201 bool command_line_arg,
2202 bool *first_dir_created_per_command_line_arg,
2203 bool *copy_into_self,
2204 bool *rename_succeeded)
2206 struct stat src_sb;
2207 struct stat dst_sb;
2208 mode_t src_mode IF_LINT ( = 0);
2209 mode_t dst_mode IF_LINT ( = 0);
2210 mode_t dst_mode_bits;
2211 mode_t omitted_permissions;
2212 bool restore_dst_mode = false;
2213 char *earlier_file = nullptr;
2214 char *dst_backup = nullptr;
2215 char const *drelname = *dst_relname ? dst_relname : ".";
2216 bool delayed_ok;
2217 bool copied_as_regular = false;
2218 bool dest_is_symlink = false;
2219 bool have_dst_lstat = false;
2221 /* Whether the destination is (or was) known to be new, updated as
2222 more info comes in. This may become true if the destination is a
2223 dangling symlink, in contexts where dangling symlinks should be
2224 treated the same as nonexistent files. */
2225 bool new_dst = 0 < nonexistent_dst;
2227 *copy_into_self = false;
2229 int rename_errno = x->rename_errno;
2230 if (x->move_mode)
2232 if (rename_errno < 0)
2233 rename_errno = (renameatu (AT_FDCWD, src_name, dst_dirfd, drelname,
2234 RENAME_NOREPLACE)
2235 ? errno : 0);
2236 nonexistent_dst = *rename_succeeded = new_dst = rename_errno == 0;
2239 if (rename_errno == 0
2240 ? !x->last_file
2241 : rename_errno != EEXIST
2242 || (x->interactive != I_ALWAYS_NO && x->interactive != I_ALWAYS_SKIP))
2244 char const *name = rename_errno == 0 ? dst_name : src_name;
2245 int dirfd = rename_errno == 0 ? dst_dirfd : AT_FDCWD;
2246 char const *relname = rename_errno == 0 ? drelname : src_name;
2247 int fstatat_flags
2248 = x->dereference == DEREF_NEVER ? AT_SYMLINK_NOFOLLOW : 0;
2249 if (follow_fstatat (dirfd, relname, &src_sb, fstatat_flags) != 0)
2251 error (0, errno, _("cannot stat %s"), quoteaf (name));
2252 return false;
2255 src_mode = src_sb.st_mode;
2257 if (S_ISDIR (src_mode) && !x->recursive)
2259 error (0, 0, ! x->install_mode /* cp */
2260 ? _("-r not specified; omitting directory %s")
2261 : _("omitting directory %s"),
2262 quoteaf (src_name));
2263 return false;
2266 else
2268 #if defined lint && (defined __clang__ || defined __COVERITY__)
2269 affirm (x->move_mode);
2270 memset (&src_sb, 0, sizeof src_sb);
2271 #endif
2274 /* Detect the case in which the same source file appears more than
2275 once on the command line and no backup option has been selected.
2276 If so, simply warn and don't copy it the second time.
2277 This check is enabled only if x->src_info is non-null. */
2278 if (command_line_arg && x->src_info)
2280 if ( ! S_ISDIR (src_mode)
2281 && x->backup_type == no_backups
2282 && seen_file (x->src_info, src_name, &src_sb))
2284 error (0, 0, _("warning: source file %s specified more than once"),
2285 quoteaf (src_name));
2286 return true;
2289 record_file (x->src_info, src_name, &src_sb);
2292 bool dereference = should_dereference (x, command_line_arg);
2294 if (nonexistent_dst <= 0)
2296 if (! (rename_errno == EEXIST
2297 && (x->interactive == I_ALWAYS_NO
2298 || x->interactive == I_ALWAYS_SKIP)))
2300 /* Regular files can be created by writing through symbolic
2301 links, but other files cannot. So use stat on the
2302 destination when copying a regular file, and lstat otherwise.
2303 However, if we intend to unlink or remove the destination
2304 first, use lstat, since a copy won't actually be made to the
2305 destination in that case. */
2306 bool use_lstat
2307 = ((! S_ISREG (src_mode)
2308 && (! x->copy_as_regular
2309 || S_ISDIR (src_mode) || S_ISLNK (src_mode)))
2310 || x->move_mode || x->symbolic_link || x->hard_link
2311 || x->backup_type != no_backups
2312 || x->unlink_dest_before_opening);
2313 int fstatat_flags = use_lstat ? AT_SYMLINK_NOFOLLOW : 0;
2314 if (!use_lstat && nonexistent_dst < 0)
2315 new_dst = true;
2316 else if (follow_fstatat (dst_dirfd, drelname, &dst_sb, fstatat_flags)
2317 == 0)
2319 have_dst_lstat = use_lstat;
2320 rename_errno = EEXIST;
2322 else
2324 if (errno == ELOOP && x->unlink_dest_after_failed_open)
2325 /* leave new_dst=false so we unlink later. */;
2326 else if (errno != ENOENT)
2328 error (0, errno, _("cannot stat %s"), quoteaf (dst_name));
2329 return false;
2331 else
2332 new_dst = true;
2336 if (rename_errno == EEXIST)
2338 bool return_now = false;
2339 bool return_val = true;
2340 bool skipped = false;
2342 if ((x->interactive != I_ALWAYS_NO && x->interactive != I_ALWAYS_SKIP)
2343 && ! same_file_ok (src_name, &src_sb, dst_dirfd, drelname,
2344 &dst_sb, x, &return_now))
2346 error (0, 0, _("%s and %s are the same file"),
2347 quoteaf_n (0, src_name), quoteaf_n (1, dst_name));
2348 return false;
2351 if (x->update && !S_ISDIR (src_mode))
2353 /* When preserving timestamps (but not moving within a file
2354 system), don't worry if the destination timestamp is
2355 less than the source merely because of timestamp
2356 truncation. */
2357 int options = ((x->preserve_timestamps
2358 && ! (x->move_mode
2359 && dst_sb.st_dev == src_sb.st_dev))
2360 ? UTIMECMP_TRUNCATE_SOURCE
2361 : 0);
2363 if (0 <= utimecmpat (dst_dirfd, dst_relname, &dst_sb,
2364 &src_sb, options))
2366 /* We're using --update and the destination is not older
2367 than the source, so do not copy or move. Pretend the
2368 rename succeeded, so the caller (if it's mv) doesn't
2369 end up removing the source file. */
2370 if (rename_succeeded)
2371 *rename_succeeded = true;
2373 /* However, we still must record that we've processed
2374 this src/dest pair, in case this source file is
2375 hard-linked to another one. In that case, we'll use
2376 the mapping information to link the corresponding
2377 destination names. */
2378 earlier_file = remember_copied (dst_relname, src_sb.st_ino,
2379 src_sb.st_dev);
2380 if (earlier_file)
2382 /* Note we currently replace DST_NAME unconditionally,
2383 even if it was a newer separate file. */
2384 if (! create_hard_link (nullptr, dst_dirfd, earlier_file,
2385 dst_name, dst_dirfd, dst_relname,
2386 true,
2387 x->verbose, dereference))
2389 goto un_backup;
2393 skipped = true;
2394 goto skip;
2398 /* When there is an existing destination file, we may end up
2399 returning early, and hence not copying/moving the file.
2400 This may be due to an interactive 'negative' reply to the
2401 prompt about the existing file. It may also be due to the
2402 use of the --no-clobber option.
2404 cp and mv treat -i and -f differently. */
2405 if (x->move_mode)
2407 if (abandon_move (x, dst_name, dst_dirfd, drelname, &dst_sb))
2409 /* Pretend the rename succeeded, so the caller (mv)
2410 doesn't end up removing the source file. */
2411 if (rename_succeeded)
2412 *rename_succeeded = true;
2414 skipped = true;
2415 return_val = x->interactive == I_ALWAYS_SKIP;
2418 else
2420 if (! S_ISDIR (src_mode)
2421 && (x->interactive == I_ALWAYS_NO
2422 || x->interactive == I_ALWAYS_SKIP
2423 || (x->interactive == I_ASK_USER
2424 && ! overwrite_ok (x, dst_name, dst_dirfd,
2425 dst_relname, &dst_sb))))
2427 skipped = true;
2428 return_val = x->interactive == I_ALWAYS_SKIP;
2432 skip:
2433 if (skipped)
2435 if (x->interactive == I_ALWAYS_NO)
2436 error (0, 0, _("not replacing %s"), quoteaf (dst_name));
2437 else if (x->debug)
2438 printf (_("skipped %s\n"), quoteaf (dst_name));
2440 return_now = true;
2443 if (return_now)
2444 return return_val;
2446 if (!S_ISDIR (dst_sb.st_mode))
2448 if (S_ISDIR (src_mode))
2450 if (x->move_mode && x->backup_type != no_backups)
2452 /* Moving a directory onto an existing
2453 non-directory is ok only with --backup. */
2455 else
2457 error (0, 0,
2458 _("cannot overwrite non-directory %s with directory %s"),
2459 quoteaf_n (0, dst_name), quoteaf_n (1, src_name));
2460 return false;
2464 /* Don't let the user destroy their data, even if they try hard:
2465 This mv command must fail (likewise for cp):
2466 rm -rf a b c; mkdir a b c; touch a/f b/f; mv a/f b/f c
2467 Otherwise, the contents of b/f would be lost.
2468 In the case of 'cp', b/f would be lost if the user simulated
2469 a move using cp and rm.
2470 Note that it works fine if you use --backup=numbered. */
2471 if (command_line_arg
2472 && x->backup_type != numbered_backups
2473 && seen_file (x->dest_info, dst_relname, &dst_sb))
2475 error (0, 0,
2476 _("will not overwrite just-created %s with %s"),
2477 quoteaf_n (0, dst_name), quoteaf_n (1, src_name));
2478 return false;
2482 if (!S_ISDIR (src_mode))
2484 if (S_ISDIR (dst_sb.st_mode))
2486 if (x->move_mode && x->backup_type != no_backups)
2488 /* Moving a non-directory onto an existing
2489 directory is ok only with --backup. */
2491 else
2493 error (0, 0,
2494 _("cannot overwrite directory %s with non-directory"),
2495 quoteaf (dst_name));
2496 return false;
2501 if (x->move_mode)
2503 /* Don't allow user to move a directory onto a non-directory. */
2504 if (S_ISDIR (src_sb.st_mode) && !S_ISDIR (dst_sb.st_mode)
2505 && x->backup_type == no_backups)
2507 error (0, 0,
2508 _("cannot move directory onto non-directory: %s -> %s"),
2509 quotef_n (0, src_name), quotef_n (0, dst_name));
2510 return false;
2514 char const *srcbase;
2515 if (x->backup_type != no_backups
2516 /* Don't try to back up a destination if the last
2517 component of src_name is "." or "..". */
2518 && ! dot_or_dotdot (srcbase = last_component (src_name))
2519 /* Create a backup of each destination directory in move mode,
2520 but not in copy mode. FIXME: it might make sense to add an
2521 option to suppress backup creation also for move mode.
2522 That would let one use mv to merge new content into an
2523 existing hierarchy. */
2524 && (x->move_mode || ! S_ISDIR (dst_sb.st_mode)))
2526 /* Fail if creating the backup file would likely destroy
2527 the source file. Otherwise, the commands:
2528 cd /tmp; rm -f a a~; : > a; echo A > a~; cp --b=simple a~ a
2529 would leave two zero-length files: a and a~. */
2530 if (x->backup_type != numbered_backups
2531 && source_is_dst_backup (srcbase, &src_sb,
2532 dst_dirfd, dst_relname))
2534 char const *fmt;
2535 fmt = (x->move_mode
2536 ? _("backing up %s might destroy source; %s not moved")
2537 : _("backing up %s might destroy source; %s not copied"));
2538 error (0, 0, fmt,
2539 quoteaf_n (0, dst_name),
2540 quoteaf_n (1, src_name));
2541 return false;
2544 char *tmp_backup = backup_file_rename (dst_dirfd, dst_relname,
2545 x->backup_type);
2547 /* FIXME: use fts:
2548 Using alloca for a file name that may be arbitrarily
2549 long is not recommended. In fact, even forming such a name
2550 should be discouraged. Eventually, this code will be rewritten
2551 to use fts, so using alloca here will be less of a problem. */
2552 if (tmp_backup)
2554 idx_t dirlen = dst_relname - dst_name;
2555 idx_t backupsize = strlen (tmp_backup) + 1;
2556 dst_backup = alloca (dirlen + backupsize);
2557 memcpy (mempcpy (dst_backup, dst_name, dirlen),
2558 tmp_backup, backupsize);
2559 free (tmp_backup);
2561 else if (errno != ENOENT)
2563 error (0, errno, _("cannot backup %s"), quoteaf (dst_name));
2564 return false;
2566 new_dst = true;
2568 else if (! S_ISDIR (dst_sb.st_mode)
2569 /* Never unlink dst_name when in move mode. */
2570 && ! x->move_mode
2571 && (x->unlink_dest_before_opening
2572 || (x->data_copy_required
2573 && ((x->preserve_links && 1 < dst_sb.st_nlink)
2574 || (x->dereference == DEREF_NEVER
2575 && ! S_ISREG (src_sb.st_mode))))
2578 if (unlinkat (dst_dirfd, dst_relname, 0) != 0 && errno != ENOENT)
2580 error (0, errno, _("cannot remove %s"), quoteaf (dst_name));
2581 return false;
2583 new_dst = true;
2584 if (x->verbose)
2585 printf (_("removed %s\n"), quoteaf (dst_name));
2590 /* Ensure we don't try to copy through a symlink that was
2591 created by a prior call to this function. */
2592 if (command_line_arg
2593 && x->dest_info
2594 && ! x->move_mode
2595 && x->backup_type == no_backups)
2597 bool lstat_ok = true;
2598 struct stat tmp_buf;
2599 struct stat *dst_lstat_sb;
2601 /* If we did not follow symlinks above, good: use that data.
2602 Otherwise, use AT_SYMLINK_NOFOLLOW, in case dst_name is a symlink. */
2603 if (have_dst_lstat)
2604 dst_lstat_sb = &dst_sb;
2605 else if (fstatat (dst_dirfd, drelname, &tmp_buf, AT_SYMLINK_NOFOLLOW)
2606 == 0)
2607 dst_lstat_sb = &tmp_buf;
2608 else
2609 lstat_ok = false;
2611 /* Never copy through a symlink we've just created. */
2612 if (lstat_ok
2613 && S_ISLNK (dst_lstat_sb->st_mode)
2614 && seen_file (x->dest_info, dst_relname, dst_lstat_sb))
2616 error (0, 0,
2617 _("will not copy %s through just-created symlink %s"),
2618 quoteaf_n (0, src_name), quoteaf_n (1, dst_name));
2619 return false;
2623 /* If the source is a directory, we don't always create the destination
2624 directory. So --verbose should not announce anything until we're
2625 sure we'll create a directory. Also don't announce yet when moving
2626 so we can distinguish renames versus copies. */
2627 if (x->verbose && !x->move_mode && !S_ISDIR (src_mode))
2628 emit_verbose (src_name, dst_name, dst_backup);
2630 /* Associate the destination file name with the source device and inode
2631 so that if we encounter a matching dev/ino pair in the source tree
2632 we can arrange to create a hard link between the corresponding names
2633 in the destination tree.
2635 When using the --link (-l) option, there is no need to take special
2636 measures, because (barring race conditions) files that are hard-linked
2637 in the source tree will also be hard-linked in the destination tree.
2639 Sometimes, when preserving links, we have to record dev/ino even
2640 though st_nlink == 1:
2641 - when in move_mode, since we may be moving a group of N hard-linked
2642 files (via two or more command line arguments) to a different
2643 partition; the links may be distributed among the command line
2644 arguments (possibly hierarchies) so that the link count of
2645 the final, once-linked source file is reduced to 1 when it is
2646 considered below. But in this case (for mv) we don't need to
2647 incur the expense of recording the dev/ino => name mapping; all we
2648 really need is a lookup, to see if the dev/ino pair has already
2649 been copied.
2650 - when using -H and processing a command line argument;
2651 that command line argument could be a symlink pointing to another
2652 command line argument. With 'cp -H --preserve=link', we hard-link
2653 those two destination files.
2654 - likewise for -L except that it applies to all files, not just
2655 command line arguments.
2657 Also, with --recursive, record dev/ino of each command-line directory.
2658 We'll use that info to detect this problem: cp -R dir dir. */
2660 if (rename_errno == 0)
2661 earlier_file = nullptr;
2662 else if (x->recursive && S_ISDIR (src_mode))
2664 if (command_line_arg)
2665 earlier_file = remember_copied (dst_relname,
2666 src_sb.st_ino, src_sb.st_dev);
2667 else
2668 earlier_file = src_to_dest_lookup (src_sb.st_ino, src_sb.st_dev);
2670 else if (x->move_mode && src_sb.st_nlink == 1)
2672 earlier_file = src_to_dest_lookup (src_sb.st_ino, src_sb.st_dev);
2674 else if (x->preserve_links
2675 && !x->hard_link
2676 && (1 < src_sb.st_nlink
2677 || (command_line_arg
2678 && x->dereference == DEREF_COMMAND_LINE_ARGUMENTS)
2679 || x->dereference == DEREF_ALWAYS))
2681 earlier_file = remember_copied (dst_relname,
2682 src_sb.st_ino, src_sb.st_dev);
2685 /* Did we copy this inode somewhere else (in this command line argument)
2686 and therefore this is a second hard link to the inode? */
2688 if (earlier_file)
2690 /* Avoid damaging the destination file system by refusing to preserve
2691 hard-linked directories (which are found at least in Netapp snapshot
2692 directories). */
2693 if (S_ISDIR (src_mode))
2695 /* If src_name and earlier_file refer to the same directory entry,
2696 then warn about copying a directory into itself. */
2697 if (same_nameat (AT_FDCWD, src_name, dst_dirfd, earlier_file))
2699 error (0, 0, _("cannot copy a directory, %s, into itself, %s"),
2700 quoteaf_n (0, top_level_src_name),
2701 quoteaf_n (1, top_level_dst_name));
2702 *copy_into_self = true;
2703 goto un_backup;
2705 else if (same_nameat (dst_dirfd, dst_relname,
2706 dst_dirfd, earlier_file))
2708 error (0, 0, _("warning: source directory %s "
2709 "specified more than once"),
2710 quoteaf (top_level_src_name));
2711 /* In move mode, if a previous rename succeeded, then
2712 we won't be in this path as the source is missing. If the
2713 rename previously failed, then that has been handled, so
2714 pretend this attempt succeeded so the source isn't removed. */
2715 if (x->move_mode && rename_succeeded)
2716 *rename_succeeded = true;
2717 /* We only do backups in move mode, and for non directories.
2718 So just ignore this repeated entry. */
2719 return true;
2721 else if (x->dereference == DEREF_ALWAYS
2722 || (command_line_arg
2723 && x->dereference == DEREF_COMMAND_LINE_ARGUMENTS))
2725 /* This happens when e.g., encountering a directory for the
2726 second or subsequent time via symlinks when cp is invoked
2727 with -R and -L. E.g.,
2728 rm -rf a b c d; mkdir a b c d; ln -s ../c a; ln -s ../c b;
2729 cp -RL a b d
2732 else
2734 char *earlier = subst_suffix (dst_name, dst_relname,
2735 earlier_file);
2736 error (0, 0, _("will not create hard link %s to directory %s"),
2737 quoteaf_n (0, dst_name), quoteaf_n (1, earlier));
2738 free (earlier);
2739 goto un_backup;
2742 else
2744 if (! create_hard_link (nullptr, dst_dirfd, earlier_file,
2745 dst_name, dst_dirfd, dst_relname,
2746 true, x->verbose, dereference))
2747 goto un_backup;
2749 return true;
2753 if (x->move_mode)
2755 if (rename_errno == EEXIST)
2756 rename_errno = (renameat (AT_FDCWD, src_name, dst_dirfd, drelname) == 0
2757 ? 0 : errno);
2759 if (rename_errno == 0)
2761 if (x->verbose)
2763 printf (_("renamed "));
2764 emit_verbose (src_name, dst_name, dst_backup);
2767 if (x->set_security_context)
2769 /* -Z failures are only warnings currently. */
2770 (void) set_file_security_ctx (dst_name, true, x);
2773 if (rename_succeeded)
2774 *rename_succeeded = true;
2776 if (command_line_arg && !x->last_file)
2778 /* Record destination dev/ino/name, so that if we are asked
2779 to overwrite that file again, we can detect it and fail. */
2780 /* It's fine to use the _source_ stat buffer (src_sb) to get the
2781 _destination_ dev/ino, since the rename above can't have
2782 changed those, and 'mv' always uses lstat.
2783 We could limit it further by operating
2784 only on non-directories. */
2785 record_file (x->dest_info, dst_relname, &src_sb);
2788 return true;
2791 /* FIXME: someday, consider what to do when moving a directory into
2792 itself but when source and destination are on different devices. */
2794 /* This happens when attempting to rename a directory to a
2795 subdirectory of itself. */
2796 if (rename_errno == EINVAL)
2798 /* FIXME: this is a little fragile in that it relies on rename(2)
2799 failing with a specific errno value. Expect problems on
2800 non-POSIX systems. */
2801 error (0, 0, _("cannot move %s to a subdirectory of itself, %s"),
2802 quoteaf_n (0, top_level_src_name),
2803 quoteaf_n (1, top_level_dst_name));
2805 /* Note that there is no need to call forget_created here,
2806 (compare with the other calls in this file) since the
2807 destination directory didn't exist before. */
2809 *copy_into_self = true;
2810 /* FIXME-cleanup: Don't return true here; adjust mv.c accordingly.
2811 The only caller that uses this code (mv.c) ends up setting its
2812 exit status to nonzero when copy_into_self is nonzero. */
2813 return true;
2816 /* WARNING: there probably exist systems for which an inter-device
2817 rename fails with a value of errno not handled here.
2818 If/as those are reported, add them to the condition below.
2819 If this happens to you, please do the following and send the output
2820 to the bug-reporting address (e.g., in the output of cp --help):
2821 touch k; perl -e 'rename "k","/tmp/k" or print "$!(",$!+0,")\n"'
2822 where your current directory is on one partition and /tmp is the other.
2823 Also, please try to find the E* errno macro name corresponding to
2824 the diagnostic and parenthesized integer, and include that in your
2825 e-mail. One way to do that is to run a command like this
2826 find /usr/include/. -type f \
2827 | xargs grep 'define.*\<E[A-Z]*\>.*\<18\>' /dev/null
2828 where you'd replace '18' with the integer in parentheses that
2829 was output from the perl one-liner above.
2830 If necessary, of course, change '/tmp' to some other directory. */
2831 if (rename_errno != EXDEV || x->no_copy)
2833 /* There are many ways this can happen due to a race condition.
2834 When something happens between the initial follow_fstatat and the
2835 subsequent rename, we can get many different types of errors.
2836 For example, if the destination is initially a non-directory
2837 or non-existent, but it is created as a directory, the rename
2838 fails. If two 'mv' commands try to rename the same file at
2839 about the same time, one will succeed and the other will fail.
2840 If the permissions on the directory containing the source or
2841 destination file are made too restrictive, the rename will
2842 fail. Etc. */
2843 error (0, rename_errno,
2844 _("cannot move %s to %s"),
2845 quoteaf_n (0, src_name), quoteaf_n (1, dst_name));
2846 forget_created (src_sb.st_ino, src_sb.st_dev);
2847 return false;
2850 /* The rename attempt has failed. Remove any existing destination
2851 file so that a cross-device 'mv' acts as if it were really using
2852 the rename syscall. Note both src and dst must both be directories
2853 or not, and this is enforced above. Therefore we check the src_mode
2854 and operate on dst_name here as a tighter constraint and also because
2855 src_mode is readily available here. */
2856 if ((unlinkat (dst_dirfd, drelname,
2857 S_ISDIR (src_mode) ? AT_REMOVEDIR : 0)
2858 != 0)
2859 && errno != ENOENT)
2861 error (0, errno,
2862 _("inter-device move failed: %s to %s; unable to remove target"),
2863 quoteaf_n (0, src_name), quoteaf_n (1, dst_name));
2864 forget_created (src_sb.st_ino, src_sb.st_dev);
2865 return false;
2868 if (x->verbose && !S_ISDIR (src_mode))
2870 printf (_("copied "));
2871 emit_verbose (src_name, dst_name, dst_backup);
2873 new_dst = true;
2876 /* If the ownership might change, or if it is a directory (whose
2877 special mode bits may change after the directory is created),
2878 omit some permissions at first, so unauthorized users cannot nip
2879 in before the file is ready. */
2880 dst_mode_bits = (x->set_mode ? x->mode : src_mode) & CHMOD_MODE_BITS;
2881 omitted_permissions =
2882 (dst_mode_bits
2883 & (x->preserve_ownership ? S_IRWXG | S_IRWXO
2884 : S_ISDIR (src_mode) ? S_IWGRP | S_IWOTH
2885 : 0));
2887 delayed_ok = true;
2889 /* If required, set the default security context for new files.
2890 Also for existing files this is used as a reference
2891 when copying the context with --preserve=context.
2892 FIXME: Do we need to consider dst_mode_bits here? */
2893 if (! set_process_security_ctx (src_name, dst_name, src_mode, new_dst, x))
2894 return false;
2896 if (S_ISDIR (src_mode))
2898 struct dir_list *dir;
2900 /* If this directory has been copied before during the
2901 recursion, there is a symbolic link to an ancestor
2902 directory of the symbolic link. It is impossible to
2903 continue to copy this, unless we've got an infinite file system. */
2905 if (is_ancestor (&src_sb, ancestors))
2907 error (0, 0, _("cannot copy cyclic symbolic link %s"),
2908 quoteaf (src_name));
2909 goto un_backup;
2912 /* Insert the current directory in the list of parents. */
2914 dir = alloca (sizeof *dir);
2915 dir->parent = ancestors;
2916 dir->ino = src_sb.st_ino;
2917 dir->dev = src_sb.st_dev;
2919 if (new_dst || !S_ISDIR (dst_sb.st_mode))
2921 /* POSIX says mkdir's behavior is implementation-defined when
2922 (src_mode & ~S_IRWXUGO) != 0. However, common practice is
2923 to ask mkdir to copy all the CHMOD_MODE_BITS, letting mkdir
2924 decide what to do with S_ISUID | S_ISGID | S_ISVTX. */
2925 mode_t mode = dst_mode_bits & ~omitted_permissions;
2926 if (mkdirat (dst_dirfd, drelname, mode) != 0)
2928 error (0, errno, _("cannot create directory %s"),
2929 quoteaf (dst_name));
2930 goto un_backup;
2933 /* We need search and write permissions to the new directory
2934 for writing the directory's contents. Check if these
2935 permissions are there. */
2937 if (fstatat (dst_dirfd, drelname, &dst_sb, AT_SYMLINK_NOFOLLOW) != 0)
2939 error (0, errno, _("cannot stat %s"), quoteaf (dst_name));
2940 goto un_backup;
2942 else if ((dst_sb.st_mode & S_IRWXU) != S_IRWXU)
2944 /* Make the new directory searchable and writable. */
2946 dst_mode = dst_sb.st_mode;
2947 restore_dst_mode = true;
2949 if (lchmodat (dst_dirfd, drelname, dst_mode | S_IRWXU) != 0)
2951 error (0, errno, _("setting permissions for %s"),
2952 quoteaf (dst_name));
2953 goto un_backup;
2957 /* Record the created directory's inode and device numbers into
2958 the search structure, so that we can avoid copying it again.
2959 Do this only for the first directory that is created for each
2960 source command line argument. */
2961 if (!*first_dir_created_per_command_line_arg)
2963 remember_copied (dst_relname, dst_sb.st_ino, dst_sb.st_dev);
2964 *first_dir_created_per_command_line_arg = true;
2967 if (x->verbose)
2969 if (x->move_mode)
2970 printf (_("created directory %s\n"), quoteaf (dst_name));
2971 else
2972 emit_verbose (src_name, dst_name, nullptr);
2975 else
2977 omitted_permissions = 0;
2979 /* For directories, the process global context could be reset for
2980 descendents, so use it to set the context for existing dirs here.
2981 This will also give earlier indication of failure to set ctx. */
2982 if (x->set_security_context || x->preserve_security_context)
2983 if (! set_file_security_ctx (dst_name, false, x))
2985 if (x->require_preserve_context)
2986 goto un_backup;
2990 /* Decide whether to copy the contents of the directory. */
2991 if (x->one_file_system && parent && parent->st_dev != src_sb.st_dev)
2993 /* Here, we are crossing a file system boundary and cp's -x option
2994 is in effect: so don't copy the contents of this directory. */
2996 else
2998 /* Copy the contents of the directory. Don't just return if
2999 this fails -- otherwise, the failure to read a single file
3000 in a source directory would cause the containing destination
3001 directory not to have owner/perms set properly. */
3002 delayed_ok = copy_dir (src_name, dst_name, dst_dirfd, dst_relname,
3003 new_dst, &src_sb, dir, x,
3004 first_dir_created_per_command_line_arg,
3005 copy_into_self);
3008 else if (x->symbolic_link)
3010 dest_is_symlink = true;
3011 if (*src_name != '/')
3013 /* Check that DST_NAME denotes a file in the current directory. */
3014 struct stat dot_sb;
3015 struct stat dst_parent_sb;
3016 char *dst_parent;
3017 bool in_current_dir;
3019 dst_parent = dir_name (dst_relname);
3021 in_current_dir = ((dst_dirfd == AT_FDCWD && STREQ (".", dst_parent))
3022 /* If either stat call fails, it's ok not to report
3023 the failure and say dst_name is in the current
3024 directory. Other things will fail later. */
3025 || stat (".", &dot_sb) != 0
3026 || (fstatat (dst_dirfd, dst_parent, &dst_parent_sb,
3027 0) != 0)
3028 || SAME_INODE (dot_sb, dst_parent_sb));
3029 free (dst_parent);
3031 if (! in_current_dir)
3033 error (0, 0,
3034 _("%s: can make relative symbolic links only in current directory"),
3035 quotef (dst_name));
3036 goto un_backup;
3040 int err = force_symlinkat (src_name, dst_dirfd, dst_relname,
3041 x->unlink_dest_after_failed_open, -1);
3042 if (0 < err)
3044 error (0, err, _("cannot create symbolic link %s to %s"),
3045 quoteaf_n (0, dst_name), quoteaf_n (1, src_name));
3046 goto un_backup;
3050 /* POSIX 2008 states that it is implementation-defined whether
3051 link() on a symlink creates a hard-link to the symlink, or only
3052 to the referent (effectively dereferencing the symlink) (POSIX
3053 2001 required the latter behavior, although many systems provided
3054 the former). Yet cp, invoked with '--link --no-dereference',
3055 should not follow the link. We can approximate the desired
3056 behavior by skipping this hard-link creating block and instead
3057 copying the symlink, via the 'S_ISLNK'- copying code below.
3059 Note gnulib's linkat module, guarantees that the symlink is not
3060 dereferenced. However its emulation currently doesn't maintain
3061 timestamps or ownership so we only call it when we know the
3062 emulation will not be needed. */
3063 else if (x->hard_link
3064 && !(! CAN_HARDLINK_SYMLINKS && S_ISLNK (src_mode)
3065 && x->dereference == DEREF_NEVER))
3067 bool replace = (x->unlink_dest_after_failed_open
3068 || x->interactive == I_ASK_USER);
3069 if (! create_hard_link (src_name, AT_FDCWD, src_name,
3070 dst_name, dst_dirfd, dst_relname,
3071 replace, false, dereference))
3072 goto un_backup;
3074 else if (S_ISREG (src_mode)
3075 || (x->copy_as_regular && !S_ISLNK (src_mode)))
3077 copied_as_regular = true;
3078 /* POSIX says the permission bits of the source file must be
3079 used as the 3rd argument in the open call. Historical
3080 practice passed all the source mode bits to 'open', but the extra
3081 bits were ignored, so it should be the same either way.
3083 This call uses DST_MODE_BITS, not SRC_MODE. These are
3084 normally the same, and the exception (where x->set_mode) is
3085 used only by 'install', which POSIX does not specify and
3086 where DST_MODE_BITS is what's wanted. */
3087 if (! copy_reg (src_name, dst_name, dst_dirfd, dst_relname,
3088 x, dst_mode_bits & S_IRWXUGO,
3089 omitted_permissions, &new_dst, &src_sb))
3090 goto un_backup;
3092 else if (S_ISFIFO (src_mode))
3094 /* Use mknodat, rather than mkfifoat, because the former preserves
3095 the special mode bits of a fifo on Solaris 10, while mkfifoat
3096 does not. But fall back on mkfifoat, because on some BSD systems,
3097 mknodat always fails when asked to create a FIFO. */
3098 mode_t mode = src_mode & ~omitted_permissions;
3099 if (mknodat (dst_dirfd, dst_relname, mode, 0) != 0)
3100 if (mkfifoat (dst_dirfd, dst_relname, mode & ~S_IFIFO) != 0)
3102 error (0, errno, _("cannot create fifo %s"), quoteaf (dst_name));
3103 goto un_backup;
3106 else if (S_ISBLK (src_mode) || S_ISCHR (src_mode) || S_ISSOCK (src_mode))
3108 mode_t mode = src_mode & ~omitted_permissions;
3109 if (mknodat (dst_dirfd, dst_relname, mode, src_sb.st_rdev) != 0)
3111 error (0, errno, _("cannot create special file %s"),
3112 quoteaf (dst_name));
3113 goto un_backup;
3116 else if (S_ISLNK (src_mode))
3118 char *src_link_val = areadlink_with_size (src_name, src_sb.st_size);
3119 dest_is_symlink = true;
3120 if (src_link_val == nullptr)
3122 error (0, errno, _("cannot read symbolic link %s"),
3123 quoteaf (src_name));
3124 goto un_backup;
3127 int symlink_err = force_symlinkat (src_link_val, dst_dirfd, dst_relname,
3128 x->unlink_dest_after_failed_open, -1);
3129 if (0 < symlink_err && x->update && !new_dst && S_ISLNK (dst_sb.st_mode)
3130 && dst_sb.st_size == strlen (src_link_val))
3132 /* See if the destination is already the desired symlink.
3133 FIXME: This behavior isn't documented, and seems wrong
3134 in some cases, e.g., if the destination symlink has the
3135 wrong ownership, permissions, or timestamps. */
3136 char *dest_link_val =
3137 areadlinkat_with_size (dst_dirfd, dst_relname, dst_sb.st_size);
3138 if (dest_link_val)
3140 if (STREQ (dest_link_val, src_link_val))
3141 symlink_err = 0;
3142 free (dest_link_val);
3145 free (src_link_val);
3146 if (0 < symlink_err)
3148 error (0, symlink_err, _("cannot create symbolic link %s"),
3149 quoteaf (dst_name));
3150 goto un_backup;
3153 if (x->preserve_security_context)
3154 restore_default_fscreatecon_or_die ();
3156 if (x->preserve_ownership)
3158 /* Preserve the owner and group of the just-'copied'
3159 symbolic link, if possible. */
3160 if (HAVE_LCHOWN
3161 && (lchownat (dst_dirfd, dst_relname,
3162 src_sb.st_uid, src_sb.st_gid)
3163 != 0)
3164 && ! chown_failure_ok (x))
3166 error (0, errno, _("failed to preserve ownership for %s"),
3167 dst_name);
3168 if (x->require_preserve)
3169 goto un_backup;
3171 else
3173 /* Can't preserve ownership of symlinks.
3174 FIXME: maybe give a warning or even error for symlinks
3175 in directories with the sticky bit set -- there, not
3176 preserving owner/group is a potential security problem. */
3180 else
3182 error (0, 0, _("%s has unknown file type"), quoteaf (src_name));
3183 goto un_backup;
3186 /* With -Z or --preserve=context, set the context for existing files.
3187 Note this is done already for copy_reg() for reasons described therein. */
3188 if (!new_dst && !x->copy_as_regular && !S_ISDIR (src_mode)
3189 && (x->set_security_context || x->preserve_security_context))
3191 if (! set_file_security_ctx (dst_name, false, x))
3193 if (x->require_preserve_context)
3194 goto un_backup;
3198 if (command_line_arg && x->dest_info)
3200 /* Now that the destination file is very likely to exist,
3201 add its info to the set. */
3202 struct stat sb;
3203 if (fstatat (dst_dirfd, drelname, &sb, AT_SYMLINK_NOFOLLOW) == 0)
3204 record_file (x->dest_info, dst_relname, &sb);
3207 /* If we've just created a hard-link due to cp's --link option,
3208 we're done. */
3209 if (x->hard_link && ! S_ISDIR (src_mode)
3210 && !(! CAN_HARDLINK_SYMLINKS && S_ISLNK (src_mode)
3211 && x->dereference == DEREF_NEVER))
3212 return delayed_ok;
3214 if (copied_as_regular)
3215 return delayed_ok;
3217 /* POSIX says that 'cp -p' must restore the following:
3218 - permission bits
3219 - setuid, setgid bits
3220 - owner and group
3221 If it fails to restore any of those, we may give a warning but
3222 the destination must not be removed.
3223 FIXME: implement the above. */
3225 /* Adjust the times (and if possible, ownership) for the copy.
3226 chown turns off set[ug]id bits for non-root,
3227 so do the chmod last. */
3229 if (x->preserve_timestamps)
3231 struct timespec timespec[2];
3232 timespec[0] = get_stat_atime (&src_sb);
3233 timespec[1] = get_stat_mtime (&src_sb);
3235 int utimensat_flags = dest_is_symlink ? AT_SYMLINK_NOFOLLOW : 0;
3236 if (utimensat (dst_dirfd, drelname, timespec, utimensat_flags) != 0)
3238 error (0, errno, _("preserving times for %s"), quoteaf (dst_name));
3239 if (x->require_preserve)
3240 return false;
3244 /* Avoid calling chown if we know it's not necessary. */
3245 if (!dest_is_symlink && x->preserve_ownership
3246 && (new_dst || !SAME_OWNER_AND_GROUP (src_sb, dst_sb)))
3248 switch (set_owner (x, dst_name, dst_dirfd, drelname, -1,
3249 &src_sb, new_dst, &dst_sb))
3251 case -1:
3252 return false;
3254 case 0:
3255 src_mode &= ~ (S_ISUID | S_ISGID | S_ISVTX);
3256 break;
3260 /* Set xattrs after ownership as changing owners will clear capabilities. */
3261 if (x->preserve_xattr && ! copy_attr (src_name, -1, dst_name, -1, x)
3262 && x->require_preserve_xattr)
3263 return false;
3265 /* The operations beyond this point may dereference a symlink. */
3266 if (dest_is_symlink)
3267 return delayed_ok;
3269 set_author (dst_name, -1, &src_sb);
3271 if (x->preserve_mode || x->move_mode)
3273 if (copy_acl (src_name, -1, dst_name, -1, src_mode) != 0
3274 && x->require_preserve)
3275 return false;
3277 else if (x->set_mode)
3279 if (set_acl (dst_name, -1, x->mode) != 0)
3280 return false;
3282 else if (x->explicit_no_preserve_mode && new_dst)
3284 int default_permissions = S_ISDIR (src_mode) || S_ISSOCK (src_mode)
3285 ? S_IRWXUGO : MODE_RW_UGO;
3286 if (set_acl (dst_name, -1, default_permissions & ~cached_umask ()) != 0)
3287 return false;
3289 else
3291 if (omitted_permissions)
3293 omitted_permissions &= ~ cached_umask ();
3295 if (omitted_permissions && !restore_dst_mode)
3297 /* Permissions were deliberately omitted when the file
3298 was created due to security concerns. See whether
3299 they need to be re-added now. It'd be faster to omit
3300 the lstat, but deducing the current destination mode
3301 is tricky in the presence of implementation-defined
3302 rules for special mode bits. */
3303 if (new_dst && (fstatat (dst_dirfd, drelname, &dst_sb,
3304 AT_SYMLINK_NOFOLLOW)
3305 != 0))
3307 error (0, errno, _("cannot stat %s"), quoteaf (dst_name));
3308 return false;
3310 dst_mode = dst_sb.st_mode;
3311 if (omitted_permissions & ~dst_mode)
3312 restore_dst_mode = true;
3316 if (restore_dst_mode)
3318 if (lchmodat (dst_dirfd, drelname, dst_mode | omitted_permissions)
3319 != 0)
3321 error (0, errno, _("preserving permissions for %s"),
3322 quoteaf (dst_name));
3323 if (x->require_preserve)
3324 return false;
3329 return delayed_ok;
3331 un_backup:
3333 if (x->preserve_security_context)
3334 restore_default_fscreatecon_or_die ();
3336 /* We have failed to create the destination file.
3337 If we've just added a dev/ino entry via the remember_copied
3338 call above (i.e., unless we've just failed to create a hard link),
3339 remove the entry associating the source dev/ino with the
3340 destination file name, so we don't try to 'preserve' a link
3341 to a file we didn't create. */
3342 if (earlier_file == nullptr)
3343 forget_created (src_sb.st_ino, src_sb.st_dev);
3345 if (dst_backup)
3347 char const *dst_relbackup = &dst_backup[dst_relname - dst_name];
3348 if (renameat (dst_dirfd, dst_relbackup, dst_dirfd, drelname) != 0)
3349 error (0, errno, _("cannot un-backup %s"), quoteaf (dst_name));
3350 else
3352 if (x->verbose)
3353 printf (_("%s -> %s (unbackup)\n"),
3354 quoteaf_n (0, dst_backup), quoteaf_n (1, dst_name));
3357 return false;
3360 static void
3361 valid_options (const struct cp_options *co)
3363 affirm (VALID_BACKUP_TYPE (co->backup_type));
3364 affirm (VALID_SPARSE_MODE (co->sparse_mode));
3365 affirm (VALID_REFLINK_MODE (co->reflink_mode));
3366 affirm (!(co->hard_link && co->symbolic_link));
3367 affirm (!
3368 (co->reflink_mode == REFLINK_ALWAYS
3369 && co->sparse_mode != SPARSE_AUTO));
3372 /* Copy the file SRC_NAME to the file DST_NAME aka DST_DIRFD+DST_RELNAME.
3373 If NONEXISTENT_DST is positive, DST_NAME does not exist even as a
3374 dangling symlink; if negative, it does not exist except possibly
3375 as a dangling symlink; if zero, its existence status is unknown.
3376 OPTIONS summarizes the command-line options.
3377 Set *COPY_INTO_SELF if SRC_NAME is a parent of (or the
3378 same as) DST_NAME; otherwise, set clear it.
3379 If X->move_mode, set *RENAME_SUCCEEDED according to whether
3380 the source was simply renamed to the destination.
3381 Return true if successful. */
3383 extern bool
3384 copy (char const *src_name, char const *dst_name,
3385 int dst_dirfd, char const *dst_relname,
3386 int nonexistent_dst, const struct cp_options *options,
3387 bool *copy_into_self, bool *rename_succeeded)
3389 valid_options (options);
3391 /* Record the file names: they're used in case of error, when copying
3392 a directory into itself. I don't like to make these tools do *any*
3393 extra work in the common case when that work is solely to handle
3394 exceptional cases, but in this case, I don't see a way to derive the
3395 top level source and destination directory names where they're used.
3396 An alternative is to use COPY_INTO_SELF and print the diagnostic
3397 from every caller -- but I don't want to do that. */
3398 top_level_src_name = src_name;
3399 top_level_dst_name = dst_name;
3401 bool first_dir_created_per_command_line_arg = false;
3402 return copy_internal (src_name, dst_name, dst_dirfd, dst_relname,
3403 nonexistent_dst, nullptr, nullptr,
3404 options, true,
3405 &first_dir_created_per_command_line_arg,
3406 copy_into_self, rename_succeeded);
3409 /* Set *X to the default options for a value of type struct cp_options. */
3411 extern void
3412 cp_options_default (struct cp_options *x)
3414 memset (x, 0, sizeof *x);
3415 #ifdef PRIV_FILE_CHOWN
3417 priv_set_t *pset = priv_allocset ();
3418 if (!pset)
3419 xalloc_die ();
3420 if (getppriv (PRIV_EFFECTIVE, pset) == 0)
3422 x->chown_privileges = priv_ismember (pset, PRIV_FILE_CHOWN);
3423 x->owner_privileges = priv_ismember (pset, PRIV_FILE_OWNER);
3425 priv_freeset (pset);
3427 #else
3428 x->chown_privileges = x->owner_privileges = (geteuid () == ROOT_UID);
3429 #endif
3430 x->rename_errno = -1;
3433 /* Return true if it's OK for chown to fail, where errno is
3434 the error number that chown failed with and X is the copying
3435 option set. */
3437 extern bool
3438 chown_failure_ok (struct cp_options const *x)
3440 /* If non-root uses -p, it's ok if we can't preserve ownership.
3441 But root probably wants to know, e.g. if NFS disallows it,
3442 or if the target system doesn't support file ownership. */
3444 return ((errno == EPERM || errno == EINVAL) && !x->chown_privileges);
3447 /* Similarly, return true if it's OK for chmod and similar operations
3448 to fail, where errno is the error number that chmod failed with and
3449 X is the copying option set. */
3451 static bool
3452 owner_failure_ok (struct cp_options const *x)
3454 return ((errno == EPERM || errno == EINVAL) && !x->owner_privileges);
3457 /* Return the user's umask, caching the result.
3459 FIXME: If the destination's parent directory has has a default ACL,
3460 some operating systems (e.g., GNU/Linux's "POSIX" ACLs) use that
3461 ACL's mask rather than the process umask. Currently, the callers
3462 of cached_umask incorrectly assume that this situation cannot occur. */
3463 extern mode_t
3464 cached_umask (void)
3466 static mode_t mask = (mode_t) -1;
3467 if (mask == (mode_t) -1)
3469 mask = umask (0);
3470 umask (mask);
3472 return mask;