1 /* copy.c -- core functions for copying files and directories
2 Copyright (C) 89, 90, 91, 1995-2004 Free Software Foundation.
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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Extracted from cp.c and librarified by Jim Meyering. */
23 #include <sys/types.h>
30 #include "backupfile.h"
35 #include "full-write.h"
38 #include "path-concat.h"
43 #include "xreadlink.h"
45 #define DO_CHOWN(Chown, File, New_uid, New_gid) \
46 (Chown (File, New_uid, New_gid) \
47 /* If non-root uses -p, it's ok if we can't preserve ownership. \
48 But root probably wants to know, e.g. if NFS disallows it, \
49 or if the target system doesn't support file ownership. */ \
50 && ((errno != EPERM && errno != EINVAL) || x->myeuid == 0))
52 #define SAME_OWNER(A, B) ((A).st_uid == (B).st_uid)
53 #define SAME_GROUP(A, B) ((A).st_gid == (B).st_gid)
54 #define SAME_OWNER_AND_GROUP(A, B) (SAME_OWNER (A, B) && SAME_GROUP (A, B))
56 #define UNWRITABLE(File_name, File_mode) \
57 ( /* euidaccess is not meaningful for symlinks */ \
58 ! S_ISLNK (File_mode) \
59 && euidaccess (File_name, W_OK) != 0)
63 struct dir_list
*parent
;
68 /* Describe a just-created or just-renamed destination file. */
76 /* Initial size of the above hash table. */
77 #define DEST_INFO_INITIAL_CAPACITY 61
82 static int copy_internal (const char *src_path
, const char *dst_path
,
83 int new_dst
, dev_t device
,
84 struct dir_list
*ancestors
,
85 const struct cp_options
*x
,
88 int *rename_succeeded
);
90 /* Pointers to the file names: they're used in the diagnostic that is issued
91 when we detect the user is trying to copy a directory into itself. */
92 static char const *top_level_src_path
;
93 static char const *top_level_dst_path
;
95 /* The invocation name of this program. */
96 extern char *program_name
;
98 /* Encapsulate selection of the file mode to be applied to
99 new non-directories. */
102 get_dest_mode (const struct cp_options
*option
, mode_t mode
)
104 /* In some applications (e.g., install), use precisely the
106 if (option
->set_mode
)
109 /* Honor the umask for `cp', but not for `mv' or `cp -p'.
110 In addition, `cp' without -p must clear the set-user-ID and set-group-ID
111 bits. POSIX requires it do that when creating new files. */
112 if (!option
->move_mode
&& !option
->preserve_mode
)
113 mode
&= (option
->umask_kill
& ~(S_ISUID
| S_ISGID
));
118 /* FIXME: describe */
119 /* FIXME: rewrite this to use a hash table so we avoid the quadratic
120 performance hit that's probably noticeable only on trees deeper
121 than a few hundred levels. See use of active_dir_map in remove.c */
124 is_ancestor (const struct stat
*sb
, const struct dir_list
*ancestors
)
126 while (ancestors
!= 0)
128 if (ancestors
->ino
== sb
->st_ino
&& ancestors
->dev
== sb
->st_dev
)
130 ancestors
= ancestors
->parent
;
135 /* Read the contents of the directory SRC_PATH_IN, and recursively
136 copy the contents to DST_PATH_IN. NEW_DST is nonzero if
137 DST_PATH_IN is a directory that was created previously in the
138 recursion. SRC_SB and ANCESTORS describe SRC_PATH_IN.
139 Set *COPY_INTO_SELF to nonzero if SRC_PATH_IN is a parent of
140 (or the same as) DST_PATH_IN; otherwise, set it to zero.
141 Return 0 if successful, -1 if an error occurs. */
144 copy_dir (const char *src_path_in
, const char *dst_path_in
, int new_dst
,
145 const struct stat
*src_sb
, struct dir_list
*ancestors
,
146 const struct cp_options
*x
, int *copy_into_self
)
150 struct cp_options non_command_line_options
= *x
;
153 name_space
= savedir (src_path_in
);
154 if (name_space
== NULL
)
156 /* This diagnostic is a bit vague because savedir can fail in
157 several different ways. */
158 error (0, errno
, _("cannot access %s"), quote (src_path_in
));
162 /* For cp's -H option, dereference command line arguments, but do not
163 dereference symlinks that are found via recursive traversal. */
164 if (x
->dereference
== DEREF_COMMAND_LINE_ARGUMENTS
)
165 non_command_line_options
.xstat
= lstat
;
168 while (*namep
!= '\0')
170 int local_copy_into_self
;
171 char *src_path
= path_concat (src_path_in
, namep
, NULL
);
172 char *dst_path
= path_concat (dst_path_in
, namep
, NULL
);
174 if (dst_path
== NULL
|| src_path
== NULL
)
177 ret
|= copy_internal (src_path
, dst_path
, new_dst
, src_sb
->st_dev
,
178 ancestors
, &non_command_line_options
, 0,
179 &local_copy_into_self
, NULL
);
180 *copy_into_self
|= local_copy_into_self
;
185 namep
+= strlen (namep
) + 1;
191 /* Copy a regular file from SRC_PATH to DST_PATH.
192 If the source file contains holes, copies holes and blocks of zeros
193 in the source file as holes in the destination file.
194 (Holes are read as zeroes by the `read' system call.)
195 Use DST_MODE as the 3rd argument in the call to open.
196 X provides many option settings.
197 Return 0 if successful, -1 if an error occurred.
198 *NEW_DST is as in copy_internal. SRC_SB is the result
199 of calling xstat (aka stat in this case) on SRC_PATH. */
202 copy_reg (const char *src_path
, const char *dst_path
,
203 const struct cp_options
*x
, mode_t dst_mode
, int *new_dst
,
204 struct stat
const *src_sb
)
211 struct stat src_open_sb
;
215 off_t n_read_total
= 0;
216 int last_write_made_hole
= 0;
217 int make_holes
= (x
->sparse_mode
== SPARSE_ALWAYS
);
219 source_desc
= open (src_path
, O_RDONLY
);
222 error (0, errno
, _("cannot open %s for reading"), quote (src_path
));
226 if (fstat (source_desc
, &src_open_sb
))
228 error (0, errno
, _("cannot fstat %s"), quote (src_path
));
233 /* Compare the source dev/ino from the open file to the incoming,
234 saved ones obtained via a previous call to stat. */
235 if (! SAME_INODE (*src_sb
, src_open_sb
))
238 _("skipping file %s, as it was replaced while being copied"),
244 /* These semantics are required for cp.
245 The if-block will be taken in move_mode. */
248 dest_desc
= open (dst_path
, O_WRONLY
| O_CREAT
, dst_mode
);
252 dest_desc
= open (dst_path
, O_WRONLY
| O_TRUNC
, dst_mode
);
254 if (dest_desc
< 0 && x
->unlink_dest_after_failed_open
)
256 if (unlink (dst_path
))
258 error (0, errno
, _("cannot remove %s"), quote (dst_path
));
263 /* Tell caller that the destination file was unlinked. */
266 /* Try the open again, but this time with different flags. */
267 dest_desc
= open (dst_path
, O_WRONLY
| O_CREAT
, dst_mode
);
273 error (0, errno
, _("cannot create regular file %s"), quote (dst_path
));
278 /* Determine the optimal buffer size. */
280 if (fstat (dest_desc
, &sb
))
282 error (0, errno
, _("cannot fstat %s"), quote (dst_path
));
284 goto close_src_and_dst_desc
;
287 buf_size
= ST_BLKSIZE (sb
);
289 #if HAVE_STRUCT_STAT_ST_BLOCKS
290 if (x
->sparse_mode
== SPARSE_AUTO
&& S_ISREG (sb
.st_mode
))
292 /* Use a heuristic to determine whether SRC_PATH contains any
295 if (fstat (source_desc
, &sb
))
297 error (0, errno
, _("cannot fstat %s"), quote (src_path
));
299 goto close_src_and_dst_desc
;
302 /* If the file has fewer blocks than would normally
303 be needed for a file of its size, then
304 at least one of the blocks in the file is a hole. */
305 if (S_ISREG (sb
.st_mode
)
306 && sb
.st_size
/ ST_NBLOCKSIZE
> ST_NBLOCKS (sb
))
311 /* Make a buffer with space for a sentinel at the end. */
313 buf
= alloca (buf_size
+ sizeof (int));
317 ssize_t n_read
= read (source_desc
, buf
, buf_size
);
324 error (0, errno
, _("reading %s"), quote (src_path
));
326 goto close_src_and_dst_desc
;
331 n_read_total
+= n_read
;
336 buf
[n_read
] = 1; /* Sentinel to stop loop. */
338 /* Find first nonzero *word*, or the word with the sentinel. */
344 /* Find the first nonzero *byte*, or the sentinel. */
346 cp
= (char *) (ip
- 1);
350 /* If we found the sentinel, the whole input block was zero,
351 and we can make a hole. */
353 if (cp
> buf
+ n_read
)
356 if (lseek (dest_desc
, (off_t
) n_read
, SEEK_CUR
) < 0L)
358 error (0, errno
, _("cannot lseek %s"), quote (dst_path
));
360 goto close_src_and_dst_desc
;
362 last_write_made_hole
= 1;
365 /* Clear to indicate that a normal write is needed. */
371 if (full_write (dest_desc
, buf
, n
) != n
)
373 error (0, errno
, _("writing %s"), quote (dst_path
));
375 goto close_src_and_dst_desc
;
377 last_write_made_hole
= 0;
381 /* If the file ends with a `hole', something needs to be written at
382 the end. Otherwise the kernel would truncate the file at the end
383 of the last write operation. */
385 if (last_write_made_hole
)
388 /* Write a null character and truncate it again. */
389 if (full_write (dest_desc
, "", 1) != 1
390 || ftruncate (dest_desc
, n_read_total
) < 0)
392 /* Seek backwards one character and write a null. */
393 if (lseek (dest_desc
, (off_t
) -1, SEEK_CUR
) < 0L
394 || full_write (dest_desc
, "", 1) != 1)
397 error (0, errno
, _("writing %s"), quote (dst_path
));
402 close_src_and_dst_desc
:
403 if (close (dest_desc
) < 0)
405 error (0, errno
, _("closing %s"), quote (dst_path
));
409 if (close (source_desc
) < 0)
411 error (0, errno
, _("closing %s"), quote (src_path
));
418 /* Return nonzero if it's ok that the source and destination
419 files are the `same' by some measure. The goal is to avoid
420 making the `copy' operation remove both copies of the file
421 in that case, while still allowing the user to e.g., move or
422 copy a regular file onto a symlink that points to it.
423 Try to minimize the cost of this function in the common case.
424 Set *RETURN_NOW if we've determined that the caller has no more
425 work to do and should return successfully, right away.
427 Set *UNLINK_SRC if we've determined that the caller wants to do
428 `rename (a, b)' where `a' and `b' are distinct hard links to the same
429 file. In that case, the caller should try to unlink `a' and then return
430 successfully. Ideally, we wouldn't have to do that, and we'd be
431 able to rely on rename to remove the source file. However, POSIX
432 mistakenly requires that such a rename call do *nothing* and return
436 same_file_ok (const char *src_path
, const struct stat
*src_sb
,
437 const char *dst_path
, const struct stat
*dst_sb
,
438 const struct cp_options
*x
, int *return_now
, int *unlink_src
)
440 const struct stat
*src_sb_link
;
441 const struct stat
*dst_sb_link
;
442 struct stat tmp_dst_sb
;
443 struct stat tmp_src_sb
;
446 int same
= (SAME_INODE (*src_sb
, *dst_sb
));
451 /* FIXME: this should (at the very least) be moved into the following
452 if-block. More likely, it should be removed, because it inhibits
453 making backups. But removing it will result in a change in behavior
454 that will probably have to be documented -- and tests will have to
456 if (same
&& x
->hard_link
)
462 if (x
->xstat
== lstat
)
466 /* If both the source and destination files are symlinks (and we'll
467 know this here IFF preserving symlinks (aka xstat == lstat),
468 then it's ok -- as long as they are distinct. */
469 if (S_ISLNK (src_sb
->st_mode
) && S_ISLNK (dst_sb
->st_mode
))
470 return ! same_name (src_path
, dst_path
);
472 src_sb_link
= src_sb
;
473 dst_sb_link
= dst_sb
;
480 if (lstat (dst_path
, &tmp_dst_sb
)
481 || lstat (src_path
, &tmp_src_sb
))
484 src_sb_link
= &tmp_src_sb
;
485 dst_sb_link
= &tmp_dst_sb
;
487 same_link
= SAME_INODE (*src_sb_link
, *dst_sb_link
);
489 /* If both are symlinks, then it's ok, but only if the destination
490 will be unlinked before being opened. This is like the test
491 above, but with the addition of the unlink_dest_before_opening
492 conjunct because otherwise, with two symlinks to the same target,
493 we'd end up truncating the source file. */
494 if (S_ISLNK (src_sb_link
->st_mode
) && S_ISLNK (dst_sb_link
->st_mode
)
495 && x
->unlink_dest_before_opening
)
499 /* The backup code ensures there's a copy, so it's usually ok to
500 remove any destination file. One exception is when both
501 source and destination are the same directory entry. In that
502 case, moving the destination file aside (in making the backup)
503 would also rename the source file and result in an error. */
504 if (x
->backup_type
!= none
)
508 /* In copy mode when dereferencing symlinks, if the source is a
509 symlink and the dest is not, then backing up the destination
510 (moving it aside) would make it a dangling symlink, and the
511 subsequent attempt to open it in copy_reg would fail with
512 a misleading diagnostic. Avoid that by returning zero in
513 that case so the caller can make cp (or mv when it has to
514 resort to reading the source file) fail now. */
516 /* FIXME-note: even with the following kludge, we can still provoke
517 the offending diagnostic. It's just a little harder to do :-)
518 $ rm -f a b c; touch c; ln -s c b; ln -s b a; cp -b a b
519 cp: cannot open `a' for reading: No such file or directory
520 That's misleading, since a subsequent `ls' shows that `a'
522 One solution would be to open the source file *before* moving
523 aside the destination, but that'd involve a big rewrite. */
525 && x
->dereference
!= DEREF_NEVER
526 && S_ISLNK (src_sb_link
->st_mode
)
527 && ! S_ISLNK (dst_sb_link
->st_mode
))
533 return ! same_name (src_path
, dst_path
);
537 /* FIXME: use or remove */
539 /* If we're making a backup, we'll detect the problem case in
540 copy_reg because SRC_PATH will no longer exist. Allowing
541 the test to be deferred lets cp do some useful things.
542 But when creating hardlinks and SRC_PATH is a symlink
543 but DST_PATH is not we must test anyway. */
545 || !S_ISLNK (src_sb_link
->st_mode
)
546 || S_ISLNK (dst_sb_link
->st_mode
))
549 if (x
->dereference
!= DEREF_NEVER
)
553 /* They may refer to the same file if we're in move mode and the
554 target is a symlink. That is ok, since we remove any existing
555 destination file before opening it -- via `rename' if they're on
556 the same file system, via `unlink (DST_PATH)' otherwise.
557 It's also ok if they're distinct hard links to the same file. */
558 if (x
->move_mode
|| x
->unlink_dest_before_opening
)
560 if (S_ISLNK (dst_sb_link
->st_mode
))
564 && 1 < dst_sb_link
->st_nlink
565 && ! same_name (src_path
, dst_path
))
576 /* If neither is a symlink, then it's ok as long as they aren't
577 hard links to the same file. */
578 if (!S_ISLNK (src_sb_link
->st_mode
) && !S_ISLNK (dst_sb_link
->st_mode
))
580 if (!SAME_INODE (*src_sb_link
, *dst_sb_link
))
583 /* If they are the same file, it's ok if we're making hard links. */
591 /* It's ok to remove a destination symlink. But that works only when we
592 unlink before opening the destination and when the source and destination
593 files are on the same partition. */
594 if (x
->unlink_dest_before_opening
595 && S_ISLNK (dst_sb_link
->st_mode
))
596 return dst_sb_link
->st_dev
== src_sb_link
->st_dev
;
598 if (x
->xstat
== lstat
)
600 if ( ! S_ISLNK (src_sb_link
->st_mode
))
601 tmp_src_sb
= *src_sb_link
;
602 else if (stat (src_path
, &tmp_src_sb
))
605 if ( ! S_ISLNK (dst_sb_link
->st_mode
))
606 tmp_dst_sb
= *dst_sb_link
;
607 else if (stat (dst_path
, &tmp_dst_sb
))
610 if ( ! SAME_INODE (tmp_src_sb
, tmp_dst_sb
))
613 /* FIXME: shouldn't this be testing whether we're making symlinks? */
625 overwrite_prompt (char const *dst_path
, struct stat
const *dst_sb
)
627 if (euidaccess (dst_path
, W_OK
) != 0)
630 _("%s: overwrite %s, overriding mode %04lo? "),
631 program_name
, quote (dst_path
),
632 (unsigned long) (dst_sb
->st_mode
& CHMOD_MODE_BITS
));
636 fprintf (stderr
, _("%s: overwrite %s? "),
637 program_name
, quote (dst_path
));
641 /* Hash an F_triple. */
643 triple_hash (void const *x
, size_t table_size
)
645 struct F_triple
const *p
= x
;
647 /* Also take the name into account, so that when moving N hard links to the
648 same file (all listed on the command line) all into the same directory,
649 we don't experience any N^2 behavior. */
650 /* FIXME-maybe: is it worth the overhead of doing this
651 just to avoid N^2 in such an unusual case? N would have
652 to be very large to make the N^2 factor noticable, and
653 one would probably encounter a limit on the length of
654 a command line before it became a problem. */
655 size_t tmp
= hash_pjw (p
->name
, table_size
);
657 /* Ignoring the device number here should be fine. */
658 return (tmp
| p
->st_ino
) % table_size
;
661 /* Hash an F_triple. */
663 triple_hash_no_name (void const *x
, size_t table_size
)
665 struct F_triple
const *p
= x
;
667 /* Ignoring the device number here should be fine. */
668 return p
->st_ino
% table_size
;
671 /* Compare two F_triple structs. */
673 triple_compare (void const *x
, void const *y
)
675 struct F_triple
const *a
= x
;
676 struct F_triple
const *b
= y
;
677 return (SAME_INODE (*a
, *b
) && same_name (a
->name
, b
->name
)) ? true : false;
680 /* Free an F_triple. */
682 triple_free (void *x
)
684 struct F_triple
*a
= x
;
689 /* Initialize the hash table implementing a set of F_triple entries
690 corresponding to destination files. */
692 dest_info_init (struct cp_options
*x
)
695 = hash_initialize (DEST_INFO_INITIAL_CAPACITY
,
702 /* Initialize the hash table implementing a set of F_triple entries
703 corresponding to source files listed on the command line. */
705 src_info_init (struct cp_options
*x
)
708 /* Note that we use triple_hash_no_name here.
709 Contrast with the use of triple_hash above.
710 That is necessary because a source file may be specified
711 in many different ways. We want to warn about this
717 = hash_initialize (DEST_INFO_INITIAL_CAPACITY
,
724 /* Return nonzero if there is an entry in hash table, HT,
725 for the file described by FILENAME and STATS.
726 Otherwise, return zero. */
728 seen_file (Hash_table
const *ht
, char const *filename
,
729 struct stat
const *stats
)
731 struct F_triple new_ent
;
736 new_ent
.name
= (char *) filename
;
737 new_ent
.st_ino
= stats
->st_ino
;
738 new_ent
.st_dev
= stats
->st_dev
;
740 return !!hash_lookup (ht
, &new_ent
);
743 /* Record destination filename, FILENAME, and dev/ino from *STATS,
744 in the hash table, HT. If HT is NULL, return immediately.
745 If STATS is NULL, call lstat on FILENAME to get the device
746 and inode numbers. If that lstat fails, simply return.
747 If memory allocation fails, exit immediately. */
749 record_file (Hash_table
*ht
, char const *filename
,
750 struct stat
const *stats
)
752 struct F_triple
*ent
;
757 ent
= xmalloc (sizeof *ent
);
758 ent
->name
= xstrdup (filename
);
761 ent
->st_ino
= stats
->st_ino
;
762 ent
->st_dev
= stats
->st_dev
;
767 if (lstat (filename
, &sb
))
769 ent
->st_ino
= sb
.st_ino
;
770 ent
->st_dev
= sb
.st_dev
;
774 struct F_triple
*ent_from_table
= hash_insert (ht
, ent
);
775 if (ent_from_table
== NULL
)
777 /* Insertion failed due to lack of memory. */
781 if (ent_from_table
!= ent
)
783 /* There was alread a matching entry in the table, so ENT was
784 not inserted. Free it. */
790 /* Copy the file SRC_PATH to the file DST_PATH. The files may be of
791 any type. NEW_DST should be nonzero if the file DST_PATH cannot
792 exist because its parent directory was just created; NEW_DST should
793 be zero if DST_PATH might already exist. DEVICE is the device
794 number of the parent directory, or 0 if the parent of this file is
795 not known. ANCESTORS points to a linked, null terminated list of
796 devices and inodes of parent directories of SRC_PATH. COMMAND_LINE_ARG
797 is nonzero iff SRC_PATH was specified on the command line.
798 Set *COPY_INTO_SELF to nonzero if SRC_PATH is a parent of (or the
799 same as) DST_PATH; otherwise, set it to zero.
800 Return 0 if successful, 1 if an error occurs. */
803 copy_internal (const char *src_path
, const char *dst_path
,
806 struct dir_list
*ancestors
,
807 const struct cp_options
*x
,
808 int command_line_arg
,
810 int *rename_succeeded
)
816 char *earlier_file
= NULL
;
817 char *dst_backup
= NULL
;
818 int backup_succeeded
= 0;
820 int copied_as_regular
= 0;
822 int preserve_metadata
;
824 if (x
->move_mode
&& rename_succeeded
)
825 *rename_succeeded
= 0;
828 if ((*(x
->xstat
)) (src_path
, &src_sb
))
830 error (0, errno
, _("cannot stat %s"), quote (src_path
));
834 src_type
= src_sb
.st_mode
;
836 src_mode
= src_sb
.st_mode
;
838 if (S_ISDIR (src_type
) && !x
->recursive
)
840 error (0, 0, _("omitting directory %s"), quote (src_path
));
844 /* Detect the case in which the same source file appears more than
845 once on the command line and no backup option has been selected.
846 If so, simply warn and don't copy it the second time.
847 This check is enabled only if x->src_info is non-NULL. */
848 if (command_line_arg
)
850 if ( ! S_ISDIR (src_sb
.st_mode
)
851 && x
->backup_type
== none
852 && seen_file (x
->src_info
, src_path
, &src_sb
))
854 error (0, 0, _("warning: source file %s specified more than once"),
859 record_file (x
->src_info
, src_path
, &src_sb
);
864 if ((*(x
->xstat
)) (dst_path
, &dst_sb
))
868 error (0, errno
, _("cannot stat %s"), quote (dst_path
));
880 int ok
= same_file_ok (src_path
, &src_sb
, dst_path
, &dst_sb
,
881 x
, &return_now
, &unlink_src
);
884 if (unlink (src_path
))
886 error (0, errno
, _("cannot remove %s"), quote (src_path
));
889 /* Tell the caller that there's no need to remove src_path. */
890 if (rename_succeeded
)
891 *rename_succeeded
= 1;
899 error (0, 0, _("%s and %s are the same file"),
900 quote_n (0, src_path
), quote_n (1, dst_path
));
904 if (!S_ISDIR (dst_sb
.st_mode
))
906 if (S_ISDIR (src_type
))
909 _("cannot overwrite non-directory %s with directory %s"),
910 quote_n (0, dst_path
), quote_n (1, src_path
));
914 /* Don't let the user destroy their data, even if they try hard:
915 This mv command must fail (likewise for cp):
916 rm -rf a b c; mkdir a b c; touch a/f b/f; mv a/f b/f c
917 Otherwise, the contents of b/f would be lost.
918 In the case of `cp', b/f would be lost if the user simulated
919 a move using cp and rm.
920 Note that it works fine if you use --backup=numbered. */
922 && x
->backup_type
!= numbered
923 && seen_file (x
->dest_info
, dst_path
, &dst_sb
))
926 _("will not overwrite just-created %s with %s"),
927 quote_n (0, dst_path
), quote_n (1, src_path
));
932 if (!S_ISDIR (src_type
))
934 if (S_ISDIR (dst_sb
.st_mode
))
937 _("cannot overwrite directory %s with non-directory"),
942 if (x
->update
&& MTIME_CMP (src_sb
, dst_sb
) <= 0)
944 /* We're using --update and the source file is older
945 than the destination file, so there is no need to
947 /* Pretend the rename succeeded, so the caller (mv)
948 doesn't end up removing the source file. */
949 if (rename_succeeded
)
950 *rename_succeeded
= 1;
955 /* When there is an existing destination file, we may end up
956 returning early, and hence not copying/moving the file.
957 This may be due to an interactive `negative' reply to the
958 prompt about the existing file. It may also be due to the
959 use of the --reply=no option. */
960 if (!S_ISDIR (src_type
))
962 /* cp and mv treat -i and -f differently. */
965 if ((x
->interactive
== I_ALWAYS_NO
966 && UNWRITABLE (dst_path
, dst_sb
.st_mode
))
967 || ((x
->interactive
== I_ASK_USER
968 || (x
->interactive
== I_UNSPECIFIED
970 && UNWRITABLE (dst_path
, dst_sb
.st_mode
)))
971 && (overwrite_prompt (dst_path
, &dst_sb
), 1)
974 /* Pretend the rename succeeded, so the caller (mv)
975 doesn't end up removing the source file. */
976 if (rename_succeeded
)
977 *rename_succeeded
= 1;
983 if (x
->interactive
== I_ALWAYS_NO
984 || (x
->interactive
== I_ASK_USER
985 && (overwrite_prompt (dst_path
, &dst_sb
), 1)
995 /* In move_mode, DEST may not be an existing directory. */
996 if (S_ISDIR (dst_sb
.st_mode
))
998 error (0, 0, _("cannot overwrite directory %s"),
1003 /* Don't allow user to move a directory onto a non-directory. */
1004 if (S_ISDIR (src_sb
.st_mode
) && !S_ISDIR (dst_sb
.st_mode
))
1007 _("cannot move directory onto non-directory: %s -> %s"),
1008 quote_n (0, src_path
), quote_n (0, dst_path
));
1013 if (x
->backup_type
!= none
&& !S_ISDIR (dst_sb
.st_mode
))
1015 char *tmp_backup
= find_backup_file_name (dst_path
,
1017 if (tmp_backup
== NULL
)
1020 /* Detect (and fail) when creating the backup file would
1021 destroy the source file. Before, running the commands
1022 cd /tmp; rm -f a a~; : > a; echo A > a~; cp --b=simple a~ a
1023 would leave two zero-length files: a and a~. */
1024 /* FIXME: but simply change e.g., the final a~ to `./a~'
1025 and the source will still be destroyed. */
1026 if (STREQ (tmp_backup
, src_path
))
1030 ? _("backing up %s would destroy source; %s not moved")
1031 : _("backing up %s would destroy source; %s not copied"));
1033 quote_n (0, dst_path
),
1034 quote_n (1, src_path
));
1039 /* Using alloca for a pathname that may be (in theory) arbitrarily
1040 long is not recommended. In fact, even forming such a name
1041 should be discouraged. Eventually, this code will be rewritten
1042 to use fts, so using alloca here will be less of a problem. */
1043 ASSIGN_STRDUPA (dst_backup
, tmp_backup
);
1045 if (rename (dst_path
, dst_backup
))
1047 if (errno
!= ENOENT
)
1049 error (0, errno
, _("cannot backup %s"), quote (dst_path
));
1059 backup_succeeded
= 1;
1063 else if (! S_ISDIR (dst_sb
.st_mode
)
1064 && (x
->unlink_dest_before_opening
1065 || (x
->xstat
== lstat
1066 && ! S_ISREG (src_sb
.st_mode
))))
1068 if (unlink (dst_path
) && errno
!= ENOENT
)
1070 error (0, errno
, _("cannot remove %s"), quote (dst_path
));
1078 /* If the source is a directory, we don't always create the destination
1079 directory. So --verbose should not announce anything until we're
1080 sure we'll create a directory. */
1081 if (x
->verbose
&& !S_ISDIR (src_type
))
1083 printf ("%s -> %s", quote_n (0, src_path
), quote_n (1, dst_path
));
1084 if (backup_succeeded
)
1085 printf (_(" (backup: %s)"), quote (dst_backup
));
1089 /* Associate the destination path with the source device and inode
1090 so that if we encounter a matching dev/ino pair in the source tree
1091 we can arrange to create a hard link between the corresponding names
1092 in the destination tree.
1094 Sometimes, when preserving links, we have to record dev/ino even
1095 though st_nlink == 1:
1096 - when using -H and processing a command line argument;
1097 that command line argument could be a symlink pointing to another
1098 command line argument. With `cp -H --preserve=link', we hard-link
1099 those two destination files.
1100 - likewise for -L except that it applies to all files, not just
1101 command line arguments.
1103 Also record directory dev/ino when using --recursive. We'll use that
1104 info to detect this problem: cp -R dir dir. FIXME-maybe: ideally,
1105 directory info would be recorded in a separate hash table, since
1106 such entries are useful only while a single command line hierarchy
1107 is being copied -- so that separate table could be cleared between
1108 command line args. Using the same hash table to preserve hard
1109 links means that it may not be cleared. */
1111 if ((x
->preserve_links
1112 && (1 < src_sb
.st_nlink
1113 || (command_line_arg
1114 && x
->dereference
== DEREF_COMMAND_LINE_ARGUMENTS
)
1115 || x
->dereference
== DEREF_ALWAYS
))
1116 || (x
->recursive
&& S_ISDIR (src_type
)))
1118 earlier_file
= remember_copied (dst_path
, src_sb
.st_ino
, src_sb
.st_dev
);
1121 /* Did we copy this inode somewhere else (in this command line argument)
1122 and therefore this is a second hard link to the inode? */
1126 /* Avoid damaging the destination filesystem by refusing to preserve
1127 hard-linked directories (which are found at least in Netapp snapshot
1129 if (S_ISDIR (src_type
))
1131 /* If src_path and earlier_file refer to the same directory entry,
1132 then warn about copying a directory into itself. */
1133 if (same_name (src_path
, earlier_file
))
1135 error (0, 0, _("cannot copy a directory, %s, into itself, %s"),
1136 quote_n (0, top_level_src_path
),
1137 quote_n (1, top_level_dst_path
));
1138 *copy_into_self
= 1;
1142 error (0, 0, _("will not create hard link %s to directory %s"),
1143 quote_n (0, dst_path
), quote_n (1, earlier_file
));
1152 link_failed
= link (earlier_file
, dst_path
);
1154 /* If the link failed because of an existing destination,
1155 remove that file and then call link again. */
1156 if (link_failed
&& errno
== EEXIST
)
1158 if (unlink (dst_path
))
1160 error (0, errno
, _("cannot remove %s"), quote (dst_path
));
1163 link_failed
= link (earlier_file
, dst_path
);
1168 error (0, errno
, _("cannot create hard link %s to %s"),
1169 quote_n (0, dst_path
), quote_n (1, earlier_file
));
1179 if (rename (src_path
, dst_path
) == 0)
1181 if (x
->verbose
&& S_ISDIR (src_type
))
1182 printf ("%s -> %s\n", quote_n (0, src_path
), quote_n (1, dst_path
));
1183 if (rename_succeeded
)
1184 *rename_succeeded
= 1;
1186 if (command_line_arg
)
1188 /* Record destination dev/ino/filename, so that if we are asked
1189 to overwrite that file again, we can detect it and fail. */
1190 /* It's fine to use the _source_ stat buffer (src_sb) to get the
1191 _destination_ dev/ino, since the rename above can't have
1192 changed those, and `mv' always uses lstat.
1193 We could limit it further by operating
1194 only on non-directories. */
1195 record_file (x
->dest_info
, dst_path
, &src_sb
);
1201 /* FIXME: someday, consider what to do when moving a directory into
1202 itself but when source and destination are on different devices. */
1204 /* This happens when attempting to rename a directory to a
1205 subdirectory of itself. */
1208 /* When src_path is on an NFS file system, some types of
1209 clients, e.g., SunOS4.1.4 and IRIX-5.3, set errno to EIO
1210 instead. Testing for this here risks misinterpreting a real
1211 I/O error as an attempt to move a directory into itself, so
1212 FIXME: consider not doing this. */
1215 /* And with SunOS-4.1.4 client and OpenBSD-2.3 server,
1216 we get ENOTEMPTY. */
1217 || errno
== ENOTEMPTY
)
1219 /* FIXME: this is a little fragile in that it relies on rename(2)
1220 failing with a specific errno value. Expect problems on
1221 non-POSIX systems. */
1222 error (0, 0, _("cannot move %s to a subdirectory of itself, %s"),
1223 quote_n (0, top_level_src_path
),
1224 quote_n (1, top_level_dst_path
));
1226 /* Note that there is no need to call forget_created here,
1227 (compare with the other calls in this file) since the
1228 destination directory didn't exist before. */
1230 *copy_into_self
= 1;
1231 /* FIXME-cleanup: Don't return zero here; adjust mv.c accordingly.
1232 The only caller that uses this code (mv.c) ends up setting its
1233 exit status to nonzero when copy_into_self is nonzero. */
1237 /* WARNING: there probably exist systems for which an inter-device
1238 rename fails with a value of errno not handled here.
1239 If/as those are reported, add them to the condition below.
1240 If this happens to you, please do the following and send the output
1241 to the bug-reporting address (e.g., in the output of cp --help):
1242 touch k; perl -e 'rename "k","/tmp/k" or print "$!(",$!+0,")\n"'
1243 where your current directory is on one partion and /tmp is the other.
1244 Also, please try to find the E* errno macro name corresponding to
1245 the diagnostic and parenthesized integer, and include that in your
1246 e-mail. One way to do that is to run a command like this
1247 find /usr/include/. -type f \
1248 | xargs grep 'define.*\<E[A-Z]*\>.*\<18\>' /dev/null
1249 where you'd replace `18' with the integer in parentheses that
1250 was output from the perl one-liner above.
1251 If necessary, of course, change `/tmp' to some other directory. */
1254 /* There are many ways this can happen due to a race condition.
1255 When something happens between the initial xstat and the
1256 subsequent rename, we can get many different types of errors.
1257 For example, if the destination is initially a non-directory
1258 or non-existent, but it is created as a directory, the rename
1259 fails. If two `mv' commands try to rename the same file at
1260 about the same time, one will succeed and the other will fail.
1261 If the permissions on the directory containing the source or
1262 destination file are made too restrictive, the rename will
1265 _("cannot move %s to %s"),
1266 quote_n (0, src_path
), quote_n (1, dst_path
));
1267 forget_created (src_sb
.st_ino
, src_sb
.st_dev
);
1271 /* The rename attempt has failed. Remove any existing destination
1272 file so that a cross-device `mv' acts as if it were really using
1273 the rename syscall. */
1274 if (unlink (dst_path
) && errno
!= ENOENT
)
1277 _("inter-device move failed: %s to %s; unable to remove target"),
1278 quote_n (0, src_path
), quote_n (1, dst_path
));
1279 forget_created (src_sb
.st_ino
, src_sb
.st_dev
);
1288 /* In certain modes (cp's --symbolic-link), and for certain file types
1289 (symlinks and hard links) it doesn't make sense to preserve metadata,
1290 or it's possible to preserve only some of it.
1291 In such cases, set this variable to zero. */
1292 preserve_metadata
= 1;
1294 if (S_ISDIR (src_type
))
1296 struct dir_list
*dir
;
1298 /* If this directory has been copied before during the
1299 recursion, there is a symbolic link to an ancestor
1300 directory of the symbolic link. It is impossible to
1301 continue to copy this, unless we've got an infinite disk. */
1303 if (is_ancestor (&src_sb
, ancestors
))
1305 error (0, 0, _("cannot copy cyclic symbolic link %s"),
1310 /* Insert the current directory in the list of parents. */
1312 dir
= alloca (sizeof *dir
);
1313 dir
->parent
= ancestors
;
1314 dir
->ino
= src_sb
.st_ino
;
1315 dir
->dev
= src_sb
.st_dev
;
1317 if (new_dst
|| !S_ISDIR (dst_sb
.st_mode
))
1319 /* Create the new directory writable and searchable, so
1320 we can create new entries in it. */
1322 if (mkdir (dst_path
, (src_mode
& x
->umask_kill
) | S_IRWXU
))
1324 error (0, errno
, _("cannot create directory %s"),
1329 /* Insert the created directory's inode and device
1330 numbers into the search structure, so that we can
1331 avoid copying it again. */
1333 if (remember_created (dst_path
))
1337 printf ("%s -> %s\n", quote_n (0, src_path
), quote_n (1, dst_path
));
1340 /* Are we crossing a file system boundary? */
1341 if (x
->one_file_system
&& device
!= 0 && device
!= src_sb
.st_dev
)
1344 /* Copy the contents of the directory. */
1346 if (copy_dir (src_path
, dst_path
, new_dst
, &src_sb
, dir
, x
,
1349 /* Don't just return here -- otherwise, the failure to read a
1350 single file in a source directory would cause the containing
1351 destination directory not to have owner/perms set properly. */
1356 else if (x
->symbolic_link
)
1358 preserve_metadata
= 0;
1360 if (*src_path
!= '/')
1362 /* Check that DST_PATH denotes a file in the current directory. */
1364 struct stat dst_parent_sb
;
1368 dst_parent
= dir_name (dst_path
);
1370 in_current_dir
= (STREQ (".", dst_parent
)
1371 /* If either stat call fails, it's ok not to report
1372 the failure and say dst_path is in the current
1373 directory. Other things will fail later. */
1374 || stat (".", &dot_sb
)
1375 || stat (dst_parent
, &dst_parent_sb
)
1376 || SAME_INODE (dot_sb
, dst_parent_sb
));
1379 if (! in_current_dir
)
1382 _("%s: can make relative symbolic links only in current directory"),
1387 if (symlink (src_path
, dst_path
))
1389 error (0, errno
, _("cannot create symbolic link %s to %s"),
1390 quote_n (0, dst_path
), quote_n (1, src_path
));
1395 else if (x
->hard_link
)
1397 preserve_metadata
= 0;
1398 if (link (src_path
, dst_path
))
1400 error (0, errno
, _("cannot create link %s"), quote (dst_path
));
1404 else if (S_ISREG (src_type
)
1405 || (x
->copy_as_regular
&& !S_ISDIR (src_type
)
1406 && !S_ISLNK (src_type
)))
1408 copied_as_regular
= 1;
1409 /* POSIX says the permission bits of the source file must be
1410 used as the 3rd argument in the open call, but that's not consistent
1411 with historical practice. */
1412 if (copy_reg (src_path
, dst_path
, x
,
1413 get_dest_mode (x
, src_mode
), &new_dst
, &src_sb
))
1418 if (S_ISFIFO (src_type
))
1420 if (mkfifo (dst_path
, get_dest_mode (x
, src_mode
)))
1422 error (0, errno
, _("cannot create fifo %s"), quote (dst_path
));
1428 if (S_ISBLK (src_type
) || S_ISCHR (src_type
)
1429 || S_ISSOCK (src_type
))
1431 if (mknod (dst_path
, get_dest_mode (x
, src_mode
), src_sb
.st_rdev
))
1433 error (0, errno
, _("cannot create special file %s"),
1440 if (S_ISLNK (src_type
))
1442 char *src_link_val
= xreadlink (src_path
);
1443 if (src_link_val
== NULL
)
1445 error (0, errno
, _("cannot read symbolic link %s"), quote (src_path
));
1449 if (!symlink (src_link_val
, dst_path
))
1450 free (src_link_val
);
1453 int saved_errno
= errno
;
1455 if (x
->update
&& !new_dst
&& S_ISLNK (dst_sb
.st_mode
))
1457 /* See if the destination is already the desired symlink. */
1458 size_t src_link_len
= strlen (src_link_val
);
1459 char *dest_link_val
= alloca (src_link_len
+ 1);
1460 int dest_link_len
= readlink (dst_path
, dest_link_val
,
1462 if ((size_t) dest_link_len
== src_link_len
1463 && strncmp (dest_link_val
, src_link_val
, src_link_len
) == 0)
1466 free (src_link_val
);
1470 error (0, saved_errno
, _("cannot create symbolic link %s"),
1476 /* There's no need to preserve timestamps or permissions. */
1477 preserve_metadata
= 0;
1479 if (x
->preserve_ownership
)
1481 /* Preserve the owner and group of the just-`copied'
1482 symbolic link, if possible. */
1484 if (DO_CHOWN (lchown
, dst_path
, src_sb
.st_uid
, src_sb
.st_gid
))
1486 error (0, errno
, _("failed to preserve ownership for %s"),
1491 /* Can't preserve ownership of symlinks.
1492 FIXME: maybe give a warning or even error for symlinks
1493 in directories with the sticky bit set -- there, not
1494 preserving owner/group is a potential security problem. */
1501 error (0, 0, _("%s has unknown file type"), quote (src_path
));
1505 if (command_line_arg
)
1506 record_file (x
->dest_info
, dst_path
, NULL
);
1508 if ( ! preserve_metadata
)
1511 /* POSIX says that `cp -p' must restore the following:
1513 - setuid, setgid bits
1515 If it fails to restore any of those, we may give a warning but
1516 the destination must not be removed.
1517 FIXME: implement the above. */
1519 /* Adjust the times (and if possible, ownership) for the copy.
1520 chown turns off set[ug]id bits for non-root,
1521 so do the chmod last. */
1523 if (x
->preserve_timestamps
)
1525 struct timespec timespec
[2];
1527 timespec
[0].tv_sec
= src_sb
.st_atime
;
1528 timespec
[0].tv_nsec
= TIMESPEC_NS (src_sb
.st_atim
);
1529 timespec
[1].tv_sec
= src_sb
.st_mtime
;
1530 timespec
[1].tv_nsec
= TIMESPEC_NS (src_sb
.st_mtim
);
1532 if (utimens (dst_path
, timespec
))
1534 error (0, errno
, _("preserving times for %s"), quote (dst_path
));
1535 if (x
->require_preserve
)
1540 /* Avoid calling chown if we know it's not necessary. */
1541 if (x
->preserve_ownership
1542 && (new_dst
|| !SAME_OWNER_AND_GROUP (src_sb
, dst_sb
)))
1545 if (DO_CHOWN (chown
, dst_path
, src_sb
.st_uid
, src_sb
.st_gid
))
1547 error (0, errno
, _("failed to preserve ownership for %s"),
1549 if (x
->require_preserve
)
1554 #if HAVE_STRUCT_STAT_ST_AUTHOR
1555 /* Preserve the st_author field. */
1557 file_t file
= file_name_lookup (dst_path
, 0, 0);
1558 if (file
== MACH_PORT_NULL
)
1559 error (0, errno
, _("failed to lookup file %s"), quote (dst_path
));
1562 error_t err
= file_chauthor (file
, src_sb
.st_author
);
1564 error (0, err
, _("failed to preserve authorship for %s"),
1566 mach_port_deallocate (mach_task_self (), file
);
1571 /* Permissions of newly-created regular files were set upon `open' in
1572 copy_reg. But don't return early if there were any special bits and
1573 we had to run chown, because the chown must have reset those bits. */
1574 if ((new_dst
&& copied_as_regular
)
1575 && !(ran_chown
&& (src_mode
& ~S_IRWXUGO
)))
1576 return delayed_fail
;
1578 if ((x
->preserve_mode
|| new_dst
)
1579 && (x
->copy_as_regular
|| S_ISREG (src_type
) || S_ISDIR (src_type
)))
1581 if (chmod (dst_path
, get_dest_mode (x
, src_mode
)))
1583 error (0, errno
, _("setting permissions for %s"), quote (dst_path
));
1584 if (x
->set_mode
|| x
->require_preserve
)
1589 return delayed_fail
;
1593 /* We have failed to create the destination file.
1594 If we've just added a dev/ino entry via the remember_copied
1595 call above (i.e., unless we've just failed to create a hard link),
1596 remove the entry associating the source dev/ino with the
1597 destination file name, so we don't try to `preserve' a link
1598 to a file we didn't create. */
1599 if (earlier_file
== NULL
)
1600 forget_created (src_sb
.st_ino
, src_sb
.st_dev
);
1604 if (rename (dst_backup
, dst_path
))
1605 error (0, errno
, _("cannot un-backup %s"), quote (dst_path
));
1609 printf (_("%s -> %s (unbackup)\n"),
1610 quote_n (0, dst_backup
), quote_n (1, dst_path
));
1617 valid_options (const struct cp_options
*co
)
1619 assert (co
!= NULL
);
1621 assert (VALID_BACKUP_TYPE (co
->backup_type
));
1623 /* FIXME: for some reason this assertion always fails,
1624 at least on Solaris 2.5.1. Just disable it for now. */
1625 /* assert (co->xstat == lstat || co->xstat == stat); */
1627 /* Make sure xstat and dereference are consistent. */
1630 assert (VALID_SPARSE_MODE (co
->sparse_mode
));
1635 /* Copy the file SRC_PATH to the file DST_PATH. The files may be of
1636 any type. NONEXISTENT_DST should be nonzero if the file DST_PATH
1637 is known not to exist (e.g., because its parent directory was just
1638 created); NONEXISTENT_DST should be zero if DST_PATH might already
1639 exist. OPTIONS is ... FIXME-describe
1640 Set *COPY_INTO_SELF to nonzero if SRC_PATH is a parent of (or the
1641 same as) DST_PATH; otherwise, set it to zero.
1642 Return 0 if successful, 1 if an error occurs. */
1645 copy (const char *src_path
, const char *dst_path
,
1646 int nonexistent_dst
, const struct cp_options
*options
,
1647 int *copy_into_self
, int *rename_succeeded
)
1649 assert (valid_options (options
));
1651 /* Record the file names: they're used in case of error, when copying
1652 a directory into itself. I don't like to make these tools do *any*
1653 extra work in the common case when that work is solely to handle
1654 exceptional cases, but in this case, I don't see a way to derive the
1655 top level source and destination directory names where they're used.
1656 An alternative is to use COPY_INTO_SELF and print the diagnostic
1657 from every caller -- but I don't want to do that. */
1658 top_level_src_path
= src_path
;
1659 top_level_dst_path
= dst_path
;
1661 return copy_internal (src_path
, dst_path
, nonexistent_dst
, 0, NULL
,
1662 options
, 1, copy_into_self
, rename_succeeded
);