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
.dereference
= DEREF_NEVER
;
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;
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 /* Even with --sparse=always, try to create holes only
290 if the destination is a regular file. */
291 if (x
->sparse_mode
== SPARSE_ALWAYS
&& S_ISREG (sb
.st_mode
))
294 #if HAVE_STRUCT_STAT_ST_BLOCKS
295 if (x
->sparse_mode
== SPARSE_AUTO
&& S_ISREG (sb
.st_mode
))
297 /* Use a heuristic to determine whether SRC_PATH contains any
300 if (fstat (source_desc
, &sb
))
302 error (0, errno
, _("cannot fstat %s"), quote (src_path
));
304 goto close_src_and_dst_desc
;
307 /* If the file has fewer blocks than would normally
308 be needed for a file of its size, then
309 at least one of the blocks in the file is a hole. */
310 if (S_ISREG (sb
.st_mode
)
311 && sb
.st_size
/ ST_NBLOCKSIZE
> ST_NBLOCKS (sb
))
316 /* Make a buffer with space for a sentinel at the end. */
318 buf
= alloca (buf_size
+ sizeof (int));
322 ssize_t n_read
= read (source_desc
, buf
, buf_size
);
329 error (0, errno
, _("reading %s"), quote (src_path
));
331 goto close_src_and_dst_desc
;
336 n_read_total
+= n_read
;
341 buf
[n_read
] = 1; /* Sentinel to stop loop. */
343 /* Find first nonzero *word*, or the word with the sentinel. */
349 /* Find the first nonzero *byte*, or the sentinel. */
351 cp
= (char *) (ip
- 1);
355 /* If we found the sentinel, the whole input block was zero,
356 and we can make a hole. */
358 if (cp
> buf
+ n_read
)
361 if (lseek (dest_desc
, (off_t
) n_read
, SEEK_CUR
) < 0L)
363 error (0, errno
, _("cannot lseek %s"), quote (dst_path
));
365 goto close_src_and_dst_desc
;
367 last_write_made_hole
= 1;
370 /* Clear to indicate that a normal write is needed. */
376 if (full_write (dest_desc
, buf
, n
) != n
)
378 error (0, errno
, _("writing %s"), quote (dst_path
));
380 goto close_src_and_dst_desc
;
382 last_write_made_hole
= 0;
386 /* If the file ends with a `hole', something needs to be written at
387 the end. Otherwise the kernel would truncate the file at the end
388 of the last write operation. */
390 if (last_write_made_hole
)
393 /* Write a null character and truncate it again. */
394 if (full_write (dest_desc
, "", 1) != 1
395 || ftruncate (dest_desc
, n_read_total
) < 0)
397 /* Seek backwards one character and write a null. */
398 if (lseek (dest_desc
, (off_t
) -1, SEEK_CUR
) < 0L
399 || full_write (dest_desc
, "", 1) != 1)
402 error (0, errno
, _("writing %s"), quote (dst_path
));
407 close_src_and_dst_desc
:
408 if (close (dest_desc
) < 0)
410 error (0, errno
, _("closing %s"), quote (dst_path
));
414 if (close (source_desc
) < 0)
416 error (0, errno
, _("closing %s"), quote (src_path
));
423 /* Return nonzero if it's ok that the source and destination
424 files are the `same' by some measure. The goal is to avoid
425 making the `copy' operation remove both copies of the file
426 in that case, while still allowing the user to e.g., move or
427 copy a regular file onto a symlink that points to it.
428 Try to minimize the cost of this function in the common case.
429 Set *RETURN_NOW if we've determined that the caller has no more
430 work to do and should return successfully, right away.
432 Set *UNLINK_SRC if we've determined that the caller wants to do
433 `rename (a, b)' where `a' and `b' are distinct hard links to the same
434 file. In that case, the caller should try to unlink `a' and then return
435 successfully. Ideally, we wouldn't have to do that, and we'd be
436 able to rely on rename to remove the source file. However, POSIX
437 mistakenly requires that such a rename call do *nothing* and return
441 same_file_ok (const char *src_path
, const struct stat
*src_sb
,
442 const char *dst_path
, const struct stat
*dst_sb
,
443 const struct cp_options
*x
, int *return_now
, int *unlink_src
)
445 const struct stat
*src_sb_link
;
446 const struct stat
*dst_sb_link
;
447 struct stat tmp_dst_sb
;
448 struct stat tmp_src_sb
;
451 int same
= (SAME_INODE (*src_sb
, *dst_sb
));
456 /* FIXME: this should (at the very least) be moved into the following
457 if-block. More likely, it should be removed, because it inhibits
458 making backups. But removing it will result in a change in behavior
459 that will probably have to be documented -- and tests will have to
461 if (same
&& x
->hard_link
)
467 if (x
->dereference
== DEREF_NEVER
)
471 /* If both the source and destination files are symlinks (and we'll
472 know this here IFF preserving symlinks (aka xstat == lstat),
473 then it's ok -- as long as they are distinct. */
474 if (S_ISLNK (src_sb
->st_mode
) && S_ISLNK (dst_sb
->st_mode
))
475 return ! same_name (src_path
, dst_path
);
477 src_sb_link
= src_sb
;
478 dst_sb_link
= dst_sb
;
485 if (lstat (dst_path
, &tmp_dst_sb
)
486 || lstat (src_path
, &tmp_src_sb
))
489 src_sb_link
= &tmp_src_sb
;
490 dst_sb_link
= &tmp_dst_sb
;
492 same_link
= SAME_INODE (*src_sb_link
, *dst_sb_link
);
494 /* If both are symlinks, then it's ok, but only if the destination
495 will be unlinked before being opened. This is like the test
496 above, but with the addition of the unlink_dest_before_opening
497 conjunct because otherwise, with two symlinks to the same target,
498 we'd end up truncating the source file. */
499 if (S_ISLNK (src_sb_link
->st_mode
) && S_ISLNK (dst_sb_link
->st_mode
)
500 && x
->unlink_dest_before_opening
)
504 /* The backup code ensures there's a copy, so it's usually ok to
505 remove any destination file. One exception is when both
506 source and destination are the same directory entry. In that
507 case, moving the destination file aside (in making the backup)
508 would also rename the source file and result in an error. */
509 if (x
->backup_type
!= none
)
513 /* In copy mode when dereferencing symlinks, if the source is a
514 symlink and the dest is not, then backing up the destination
515 (moving it aside) would make it a dangling symlink, and the
516 subsequent attempt to open it in copy_reg would fail with
517 a misleading diagnostic. Avoid that by returning zero in
518 that case so the caller can make cp (or mv when it has to
519 resort to reading the source file) fail now. */
521 /* FIXME-note: even with the following kludge, we can still provoke
522 the offending diagnostic. It's just a little harder to do :-)
523 $ rm -f a b c; touch c; ln -s c b; ln -s b a; cp -b a b
524 cp: cannot open `a' for reading: No such file or directory
525 That's misleading, since a subsequent `ls' shows that `a'
527 One solution would be to open the source file *before* moving
528 aside the destination, but that'd involve a big rewrite. */
530 && x
->dereference
!= DEREF_NEVER
531 && S_ISLNK (src_sb_link
->st_mode
)
532 && ! S_ISLNK (dst_sb_link
->st_mode
))
538 return ! same_name (src_path
, dst_path
);
542 /* FIXME: use or remove */
544 /* If we're making a backup, we'll detect the problem case in
545 copy_reg because SRC_PATH will no longer exist. Allowing
546 the test to be deferred lets cp do some useful things.
547 But when creating hardlinks and SRC_PATH is a symlink
548 but DST_PATH is not we must test anyway. */
550 || !S_ISLNK (src_sb_link
->st_mode
)
551 || S_ISLNK (dst_sb_link
->st_mode
))
554 if (x
->dereference
!= DEREF_NEVER
)
558 /* They may refer to the same file if we're in move mode and the
559 target is a symlink. That is ok, since we remove any existing
560 destination file before opening it -- via `rename' if they're on
561 the same file system, via `unlink (DST_PATH)' otherwise.
562 It's also ok if they're distinct hard links to the same file. */
563 if (x
->move_mode
|| x
->unlink_dest_before_opening
)
565 if (S_ISLNK (dst_sb_link
->st_mode
))
569 && 1 < dst_sb_link
->st_nlink
570 && ! same_name (src_path
, dst_path
))
581 /* If neither is a symlink, then it's ok as long as they aren't
582 hard links to the same file. */
583 if (!S_ISLNK (src_sb_link
->st_mode
) && !S_ISLNK (dst_sb_link
->st_mode
))
585 if (!SAME_INODE (*src_sb_link
, *dst_sb_link
))
588 /* If they are the same file, it's ok if we're making hard links. */
596 /* It's ok to remove a destination symlink. But that works only when we
597 unlink before opening the destination and when the source and destination
598 files are on the same partition. */
599 if (x
->unlink_dest_before_opening
600 && S_ISLNK (dst_sb_link
->st_mode
))
601 return dst_sb_link
->st_dev
== src_sb_link
->st_dev
;
603 if (x
->dereference
== DEREF_NEVER
)
605 if ( ! S_ISLNK (src_sb_link
->st_mode
))
606 tmp_src_sb
= *src_sb_link
;
607 else if (stat (src_path
, &tmp_src_sb
))
610 if ( ! S_ISLNK (dst_sb_link
->st_mode
))
611 tmp_dst_sb
= *dst_sb_link
;
612 else if (stat (dst_path
, &tmp_dst_sb
))
615 if ( ! SAME_INODE (tmp_src_sb
, tmp_dst_sb
))
618 /* FIXME: shouldn't this be testing whether we're making symlinks? */
630 overwrite_prompt (char const *dst_path
, struct stat
const *dst_sb
)
632 if (euidaccess (dst_path
, W_OK
) != 0)
635 _("%s: overwrite %s, overriding mode %04lo? "),
636 program_name
, quote (dst_path
),
637 (unsigned long) (dst_sb
->st_mode
& CHMOD_MODE_BITS
));
641 fprintf (stderr
, _("%s: overwrite %s? "),
642 program_name
, quote (dst_path
));
646 /* Hash an F_triple. */
648 triple_hash (void const *x
, size_t table_size
)
650 struct F_triple
const *p
= x
;
652 /* Also take the name into account, so that when moving N hard links to the
653 same file (all listed on the command line) all into the same directory,
654 we don't experience any N^2 behavior. */
655 /* FIXME-maybe: is it worth the overhead of doing this
656 just to avoid N^2 in such an unusual case? N would have
657 to be very large to make the N^2 factor noticable, and
658 one would probably encounter a limit on the length of
659 a command line before it became a problem. */
660 size_t tmp
= hash_pjw (p
->name
, table_size
);
662 /* Ignoring the device number here should be fine. */
663 return (tmp
| p
->st_ino
) % table_size
;
666 /* Hash an F_triple. */
668 triple_hash_no_name (void const *x
, size_t table_size
)
670 struct F_triple
const *p
= x
;
672 /* Ignoring the device number here should be fine. */
673 return p
->st_ino
% table_size
;
676 /* Compare two F_triple structs. */
678 triple_compare (void const *x
, void const *y
)
680 struct F_triple
const *a
= x
;
681 struct F_triple
const *b
= y
;
682 return (SAME_INODE (*a
, *b
) && same_name (a
->name
, b
->name
)) ? true : false;
685 /* Free an F_triple. */
687 triple_free (void *x
)
689 struct F_triple
*a
= x
;
694 /* Initialize the hash table implementing a set of F_triple entries
695 corresponding to destination files. */
697 dest_info_init (struct cp_options
*x
)
700 = hash_initialize (DEST_INFO_INITIAL_CAPACITY
,
707 /* Initialize the hash table implementing a set of F_triple entries
708 corresponding to source files listed on the command line. */
710 src_info_init (struct cp_options
*x
)
713 /* Note that we use triple_hash_no_name here.
714 Contrast with the use of triple_hash above.
715 That is necessary because a source file may be specified
716 in many different ways. We want to warn about this
722 = hash_initialize (DEST_INFO_INITIAL_CAPACITY
,
729 /* Return nonzero if there is an entry in hash table, HT,
730 for the file described by FILENAME and STATS.
731 Otherwise, return zero. */
733 seen_file (Hash_table
const *ht
, char const *filename
,
734 struct stat
const *stats
)
736 struct F_triple new_ent
;
741 new_ent
.name
= (char *) filename
;
742 new_ent
.st_ino
= stats
->st_ino
;
743 new_ent
.st_dev
= stats
->st_dev
;
745 return !!hash_lookup (ht
, &new_ent
);
748 /* Record destination filename, FILENAME, and dev/ino from *STATS,
749 in the hash table, HT. If HT is NULL, return immediately.
750 If STATS is NULL, call lstat on FILENAME to get the device
751 and inode numbers. If that lstat fails, simply return.
752 If memory allocation fails, exit immediately. */
754 record_file (Hash_table
*ht
, char const *filename
,
755 struct stat
const *stats
)
757 struct F_triple
*ent
;
762 ent
= xmalloc (sizeof *ent
);
763 ent
->name
= xstrdup (filename
);
766 ent
->st_ino
= stats
->st_ino
;
767 ent
->st_dev
= stats
->st_dev
;
772 if (lstat (filename
, &sb
))
774 ent
->st_ino
= sb
.st_ino
;
775 ent
->st_dev
= sb
.st_dev
;
779 struct F_triple
*ent_from_table
= hash_insert (ht
, ent
);
780 if (ent_from_table
== NULL
)
782 /* Insertion failed due to lack of memory. */
786 if (ent_from_table
!= ent
)
788 /* There was alread a matching entry in the table, so ENT was
789 not inserted. Free it. */
795 /* Copy the file SRC_PATH to the file DST_PATH. The files may be of
796 any type. NEW_DST should be nonzero if the file DST_PATH cannot
797 exist because its parent directory was just created; NEW_DST should
798 be zero if DST_PATH might already exist. DEVICE is the device
799 number of the parent directory, or 0 if the parent of this file is
800 not known. ANCESTORS points to a linked, null terminated list of
801 devices and inodes of parent directories of SRC_PATH. COMMAND_LINE_ARG
802 is nonzero iff SRC_PATH was specified on the command line.
803 Set *COPY_INTO_SELF to nonzero if SRC_PATH is a parent of (or the
804 same as) DST_PATH; otherwise, set it to zero.
805 Return 0 if successful, 1 if an error occurs. */
808 copy_internal (const char *src_path
, const char *dst_path
,
811 struct dir_list
*ancestors
,
812 const struct cp_options
*x
,
813 int command_line_arg
,
815 int *rename_succeeded
)
821 char *earlier_file
= NULL
;
822 char *dst_backup
= NULL
;
823 int backup_succeeded
= 0;
825 int copied_as_regular
= 0;
827 int preserve_metadata
;
829 if (x
->move_mode
&& rename_succeeded
)
830 *rename_succeeded
= 0;
834 if (XSTAT (x
, src_path
, &src_sb
))
836 error (0, errno
, _("cannot stat %s"), quote (src_path
));
840 src_type
= src_sb
.st_mode
;
842 src_mode
= src_sb
.st_mode
;
844 if (S_ISDIR (src_type
) && !x
->recursive
)
846 error (0, 0, _("omitting directory %s"), quote (src_path
));
850 /* Detect the case in which the same source file appears more than
851 once on the command line and no backup option has been selected.
852 If so, simply warn and don't copy it the second time.
853 This check is enabled only if x->src_info is non-NULL. */
854 if (command_line_arg
)
856 if ( ! S_ISDIR (src_sb
.st_mode
)
857 && x
->backup_type
== none
858 && seen_file (x
->src_info
, src_path
, &src_sb
))
860 error (0, 0, _("warning: source file %s specified more than once"),
865 record_file (x
->src_info
, src_path
, &src_sb
);
870 if (XSTAT (x
, dst_path
, &dst_sb
))
874 error (0, errno
, _("cannot stat %s"), quote (dst_path
));
886 int ok
= same_file_ok (src_path
, &src_sb
, dst_path
, &dst_sb
,
887 x
, &return_now
, &unlink_src
);
890 if (unlink (src_path
))
892 error (0, errno
, _("cannot remove %s"), quote (src_path
));
895 /* Tell the caller that there's no need to remove src_path. */
896 if (rename_succeeded
)
897 *rename_succeeded
= 1;
905 error (0, 0, _("%s and %s are the same file"),
906 quote_n (0, src_path
), quote_n (1, dst_path
));
910 if (!S_ISDIR (dst_sb
.st_mode
))
912 if (S_ISDIR (src_type
))
915 _("cannot overwrite non-directory %s with directory %s"),
916 quote_n (0, dst_path
), quote_n (1, src_path
));
920 /* Don't let the user destroy their data, even if they try hard:
921 This mv command must fail (likewise for cp):
922 rm -rf a b c; mkdir a b c; touch a/f b/f; mv a/f b/f c
923 Otherwise, the contents of b/f would be lost.
924 In the case of `cp', b/f would be lost if the user simulated
925 a move using cp and rm.
926 Note that it works fine if you use --backup=numbered. */
928 && x
->backup_type
!= numbered
929 && seen_file (x
->dest_info
, dst_path
, &dst_sb
))
932 _("will not overwrite just-created %s with %s"),
933 quote_n (0, dst_path
), quote_n (1, src_path
));
938 if (!S_ISDIR (src_type
))
940 if (S_ISDIR (dst_sb
.st_mode
))
943 _("cannot overwrite directory %s with non-directory"),
948 if (x
->update
&& MTIME_CMP (src_sb
, dst_sb
) <= 0)
950 /* We're using --update and the source file is older
951 than the destination file, so there is no need to
953 /* Pretend the rename succeeded, so the caller (mv)
954 doesn't end up removing the source file. */
955 if (rename_succeeded
)
956 *rename_succeeded
= 1;
961 /* When there is an existing destination file, we may end up
962 returning early, and hence not copying/moving the file.
963 This may be due to an interactive `negative' reply to the
964 prompt about the existing file. It may also be due to the
965 use of the --reply=no option. */
966 if (!S_ISDIR (src_type
))
968 /* cp and mv treat -i and -f differently. */
971 if ((x
->interactive
== I_ALWAYS_NO
972 && UNWRITABLE (dst_path
, dst_sb
.st_mode
))
973 || ((x
->interactive
== I_ASK_USER
974 || (x
->interactive
== I_UNSPECIFIED
976 && UNWRITABLE (dst_path
, dst_sb
.st_mode
)))
977 && (overwrite_prompt (dst_path
, &dst_sb
), 1)
980 /* Pretend the rename succeeded, so the caller (mv)
981 doesn't end up removing the source file. */
982 if (rename_succeeded
)
983 *rename_succeeded
= 1;
989 if (x
->interactive
== I_ALWAYS_NO
990 || (x
->interactive
== I_ASK_USER
991 && (overwrite_prompt (dst_path
, &dst_sb
), 1)
1001 /* In move_mode, DEST may not be an existing directory. */
1002 if (S_ISDIR (dst_sb
.st_mode
))
1004 error (0, 0, _("cannot overwrite directory %s"),
1009 /* Don't allow user to move a directory onto a non-directory. */
1010 if (S_ISDIR (src_sb
.st_mode
) && !S_ISDIR (dst_sb
.st_mode
))
1013 _("cannot move directory onto non-directory: %s -> %s"),
1014 quote_n (0, src_path
), quote_n (0, dst_path
));
1019 if (x
->backup_type
!= none
&& !S_ISDIR (dst_sb
.st_mode
))
1021 char *tmp_backup
= find_backup_file_name (dst_path
,
1023 if (tmp_backup
== NULL
)
1026 /* Detect (and fail) when creating the backup file would
1027 destroy the source file. Before, running the commands
1028 cd /tmp; rm -f a a~; : > a; echo A > a~; cp --b=simple a~ a
1029 would leave two zero-length files: a and a~. */
1030 /* FIXME: but simply change e.g., the final a~ to `./a~'
1031 and the source will still be destroyed. */
1032 if (STREQ (tmp_backup
, src_path
))
1036 ? _("backing up %s would destroy source; %s not moved")
1037 : _("backing up %s would destroy source; %s not copied"));
1039 quote_n (0, dst_path
),
1040 quote_n (1, src_path
));
1045 /* Using alloca for a pathname that may be (in theory) arbitrarily
1046 long is not recommended. In fact, even forming such a name
1047 should be discouraged. Eventually, this code will be rewritten
1048 to use fts, so using alloca here will be less of a problem. */
1049 ASSIGN_STRDUPA (dst_backup
, tmp_backup
);
1051 if (rename (dst_path
, dst_backup
))
1053 if (errno
!= ENOENT
)
1055 error (0, errno
, _("cannot backup %s"), quote (dst_path
));
1065 backup_succeeded
= 1;
1069 else if (! S_ISDIR (dst_sb
.st_mode
)
1070 && (x
->unlink_dest_before_opening
1071 || (x
->dereference
== DEREF_NEVER
1072 && ! S_ISREG (src_sb
.st_mode
))))
1074 if (unlink (dst_path
) && errno
!= ENOENT
)
1076 error (0, errno
, _("cannot remove %s"), quote (dst_path
));
1084 /* If the source is a directory, we don't always create the destination
1085 directory. So --verbose should not announce anything until we're
1086 sure we'll create a directory. */
1087 if (x
->verbose
&& !S_ISDIR (src_type
))
1089 printf ("%s -> %s", quote_n (0, src_path
), quote_n (1, dst_path
));
1090 if (backup_succeeded
)
1091 printf (_(" (backup: %s)"), quote (dst_backup
));
1095 /* Associate the destination path with the source device and inode
1096 so that if we encounter a matching dev/ino pair in the source tree
1097 we can arrange to create a hard link between the corresponding names
1098 in the destination tree.
1100 Sometimes, when preserving links, we have to record dev/ino even
1101 though st_nlink == 1:
1102 - when in move_mode, since we may be moving a group of N hard-linked
1103 files (via two or more command line arguments) to a different
1104 partition; the links may be distributed among the command line
1105 arguments (possibly hierarchies) so that the link count of
1106 the final, once-linked source file is reduced to 1 when it is
1107 considered below. But in this case (for mv) we don't need to
1108 incur the expense of recording the dev/ino => name mapping; all we
1109 really need is a lookup, to see if the dev/ino pair has already
1111 - when using -H and processing a command line argument;
1112 that command line argument could be a symlink pointing to another
1113 command line argument. With `cp -H --preserve=link', we hard-link
1114 those two destination files.
1115 - likewise for -L except that it applies to all files, not just
1116 command line arguments.
1118 Also record directory dev/ino when using --recursive. We'll use that
1119 info to detect this problem: cp -R dir dir. FIXME-maybe: ideally,
1120 directory info would be recorded in a separate hash table, since
1121 such entries are useful only while a single command line hierarchy
1122 is being copied -- so that separate table could be cleared between
1123 command line args. Using the same hash table to preserve hard
1124 links means that it may not be cleared. */
1126 if (x
->move_mode
&& src_sb
.st_nlink
== 1)
1128 earlier_file
= src_to_dest_lookup (src_sb
.st_ino
, src_sb
.st_dev
);
1130 else if ((x
->preserve_links
1131 && (1 < src_sb
.st_nlink
1132 || (command_line_arg
1133 && x
->dereference
== DEREF_COMMAND_LINE_ARGUMENTS
)
1134 || x
->dereference
== DEREF_ALWAYS
))
1135 || (x
->recursive
&& S_ISDIR (src_type
)))
1137 earlier_file
= remember_copied (dst_path
, src_sb
.st_ino
, src_sb
.st_dev
);
1140 /* Did we copy this inode somewhere else (in this command line argument)
1141 and therefore this is a second hard link to the inode? */
1145 /* Avoid damaging the destination filesystem by refusing to preserve
1146 hard-linked directories (which are found at least in Netapp snapshot
1148 if (S_ISDIR (src_type
))
1150 /* If src_path and earlier_file refer to the same directory entry,
1151 then warn about copying a directory into itself. */
1152 if (same_name (src_path
, earlier_file
))
1154 error (0, 0, _("cannot copy a directory, %s, into itself, %s"),
1155 quote_n (0, top_level_src_path
),
1156 quote_n (1, top_level_dst_path
));
1157 *copy_into_self
= 1;
1161 error (0, 0, _("will not create hard link %s to directory %s"),
1162 quote_n (0, dst_path
), quote_n (1, earlier_file
));
1171 link_failed
= link (earlier_file
, dst_path
);
1173 /* If the link failed because of an existing destination,
1174 remove that file and then call link again. */
1175 if (link_failed
&& errno
== EEXIST
)
1177 if (unlink (dst_path
))
1179 error (0, errno
, _("cannot remove %s"), quote (dst_path
));
1182 link_failed
= link (earlier_file
, dst_path
);
1187 error (0, errno
, _("cannot create hard link %s to %s"),
1188 quote_n (0, dst_path
), quote_n (1, earlier_file
));
1198 if (rename (src_path
, dst_path
) == 0)
1200 if (x
->verbose
&& S_ISDIR (src_type
))
1201 printf ("%s -> %s\n", quote_n (0, src_path
), quote_n (1, dst_path
));
1202 if (rename_succeeded
)
1203 *rename_succeeded
= 1;
1205 if (command_line_arg
)
1207 /* Record destination dev/ino/filename, so that if we are asked
1208 to overwrite that file again, we can detect it and fail. */
1209 /* It's fine to use the _source_ stat buffer (src_sb) to get the
1210 _destination_ dev/ino, since the rename above can't have
1211 changed those, and `mv' always uses lstat.
1212 We could limit it further by operating
1213 only on non-directories. */
1214 record_file (x
->dest_info
, dst_path
, &src_sb
);
1220 /* FIXME: someday, consider what to do when moving a directory into
1221 itself but when source and destination are on different devices. */
1223 /* This happens when attempting to rename a directory to a
1224 subdirectory of itself. */
1227 /* When src_path is on an NFS file system, some types of
1228 clients, e.g., SunOS4.1.4 and IRIX-5.3, set errno to EIO
1229 instead. Testing for this here risks misinterpreting a real
1230 I/O error as an attempt to move a directory into itself, so
1231 FIXME: consider not doing this. */
1234 /* And with SunOS-4.1.4 client and OpenBSD-2.3 server,
1235 we get ENOTEMPTY. */
1236 || errno
== ENOTEMPTY
)
1238 /* FIXME: this is a little fragile in that it relies on rename(2)
1239 failing with a specific errno value. Expect problems on
1240 non-POSIX systems. */
1241 error (0, 0, _("cannot move %s to a subdirectory of itself, %s"),
1242 quote_n (0, top_level_src_path
),
1243 quote_n (1, top_level_dst_path
));
1245 /* Note that there is no need to call forget_created here,
1246 (compare with the other calls in this file) since the
1247 destination directory didn't exist before. */
1249 *copy_into_self
= 1;
1250 /* FIXME-cleanup: Don't return zero here; adjust mv.c accordingly.
1251 The only caller that uses this code (mv.c) ends up setting its
1252 exit status to nonzero when copy_into_self is nonzero. */
1256 /* WARNING: there probably exist systems for which an inter-device
1257 rename fails with a value of errno not handled here.
1258 If/as those are reported, add them to the condition below.
1259 If this happens to you, please do the following and send the output
1260 to the bug-reporting address (e.g., in the output of cp --help):
1261 touch k; perl -e 'rename "k","/tmp/k" or print "$!(",$!+0,")\n"'
1262 where your current directory is on one partion and /tmp is the other.
1263 Also, please try to find the E* errno macro name corresponding to
1264 the diagnostic and parenthesized integer, and include that in your
1265 e-mail. One way to do that is to run a command like this
1266 find /usr/include/. -type f \
1267 | xargs grep 'define.*\<E[A-Z]*\>.*\<18\>' /dev/null
1268 where you'd replace `18' with the integer in parentheses that
1269 was output from the perl one-liner above.
1270 If necessary, of course, change `/tmp' to some other directory. */
1273 /* There are many ways this can happen due to a race condition.
1274 When something happens between the initial xstat and the
1275 subsequent rename, we can get many different types of errors.
1276 For example, if the destination is initially a non-directory
1277 or non-existent, but it is created as a directory, the rename
1278 fails. If two `mv' commands try to rename the same file at
1279 about the same time, one will succeed and the other will fail.
1280 If the permissions on the directory containing the source or
1281 destination file are made too restrictive, the rename will
1284 _("cannot move %s to %s"),
1285 quote_n (0, src_path
), quote_n (1, dst_path
));
1286 forget_created (src_sb
.st_ino
, src_sb
.st_dev
);
1290 /* The rename attempt has failed. Remove any existing destination
1291 file so that a cross-device `mv' acts as if it were really using
1292 the rename syscall. */
1293 if (unlink (dst_path
) && errno
!= ENOENT
)
1296 _("inter-device move failed: %s to %s; unable to remove target"),
1297 quote_n (0, src_path
), quote_n (1, dst_path
));
1298 forget_created (src_sb
.st_ino
, src_sb
.st_dev
);
1307 /* In certain modes (cp's --symbolic-link), and for certain file types
1308 (symlinks and hard links) it doesn't make sense to preserve metadata,
1309 or it's possible to preserve only some of it.
1310 In such cases, set this variable to zero. */
1311 preserve_metadata
= 1;
1313 if (S_ISDIR (src_type
))
1315 struct dir_list
*dir
;
1317 /* If this directory has been copied before during the
1318 recursion, there is a symbolic link to an ancestor
1319 directory of the symbolic link. It is impossible to
1320 continue to copy this, unless we've got an infinite disk. */
1322 if (is_ancestor (&src_sb
, ancestors
))
1324 error (0, 0, _("cannot copy cyclic symbolic link %s"),
1329 /* Insert the current directory in the list of parents. */
1331 dir
= alloca (sizeof *dir
);
1332 dir
->parent
= ancestors
;
1333 dir
->ino
= src_sb
.st_ino
;
1334 dir
->dev
= src_sb
.st_dev
;
1336 if (new_dst
|| !S_ISDIR (dst_sb
.st_mode
))
1338 /* Create the new directory writable and searchable, so
1339 we can create new entries in it. */
1341 if (mkdir (dst_path
, (src_mode
& x
->umask_kill
) | S_IRWXU
))
1343 error (0, errno
, _("cannot create directory %s"),
1348 /* Insert the created directory's inode and device
1349 numbers into the search structure, so that we can
1350 avoid copying it again. */
1352 if (remember_created (dst_path
))
1356 printf ("%s -> %s\n", quote_n (0, src_path
), quote_n (1, dst_path
));
1359 /* Are we crossing a file system boundary? */
1360 if (x
->one_file_system
&& device
!= 0 && device
!= src_sb
.st_dev
)
1363 /* Copy the contents of the directory. */
1365 if (copy_dir (src_path
, dst_path
, new_dst
, &src_sb
, dir
, x
,
1368 /* Don't just return here -- otherwise, the failure to read a
1369 single file in a source directory would cause the containing
1370 destination directory not to have owner/perms set properly. */
1375 else if (x
->symbolic_link
)
1377 preserve_metadata
= 0;
1379 if (*src_path
!= '/')
1381 /* Check that DST_PATH denotes a file in the current directory. */
1383 struct stat dst_parent_sb
;
1387 dst_parent
= dir_name (dst_path
);
1389 in_current_dir
= (STREQ (".", dst_parent
)
1390 /* If either stat call fails, it's ok not to report
1391 the failure and say dst_path is in the current
1392 directory. Other things will fail later. */
1393 || stat (".", &dot_sb
)
1394 || stat (dst_parent
, &dst_parent_sb
)
1395 || SAME_INODE (dot_sb
, dst_parent_sb
));
1398 if (! in_current_dir
)
1401 _("%s: can make relative symbolic links only in current directory"),
1406 if (symlink (src_path
, dst_path
))
1408 error (0, errno
, _("cannot create symbolic link %s to %s"),
1409 quote_n (0, dst_path
), quote_n (1, src_path
));
1414 else if (x
->hard_link
)
1416 preserve_metadata
= 0;
1417 if (link (src_path
, dst_path
))
1419 error (0, errno
, _("cannot create link %s"), quote (dst_path
));
1423 else if (S_ISREG (src_type
)
1424 || (x
->copy_as_regular
&& !S_ISDIR (src_type
)
1425 && !S_ISLNK (src_type
)))
1427 copied_as_regular
= 1;
1428 /* POSIX says the permission bits of the source file must be
1429 used as the 3rd argument in the open call, but that's not consistent
1430 with historical practice. */
1431 if (copy_reg (src_path
, dst_path
, x
,
1432 get_dest_mode (x
, src_mode
), &new_dst
, &src_sb
))
1437 if (S_ISFIFO (src_type
))
1439 if (mkfifo (dst_path
, get_dest_mode (x
, src_mode
)))
1441 error (0, errno
, _("cannot create fifo %s"), quote (dst_path
));
1447 if (S_ISBLK (src_type
) || S_ISCHR (src_type
)
1448 || S_ISSOCK (src_type
))
1450 if (mknod (dst_path
, get_dest_mode (x
, src_mode
), src_sb
.st_rdev
))
1452 error (0, errno
, _("cannot create special file %s"),
1459 if (S_ISLNK (src_type
))
1461 char *src_link_val
= xreadlink (src_path
);
1462 if (src_link_val
== NULL
)
1464 error (0, errno
, _("cannot read symbolic link %s"), quote (src_path
));
1468 if (!symlink (src_link_val
, dst_path
))
1469 free (src_link_val
);
1472 int saved_errno
= errno
;
1474 if (x
->update
&& !new_dst
&& S_ISLNK (dst_sb
.st_mode
))
1476 /* See if the destination is already the desired symlink. */
1477 size_t src_link_len
= strlen (src_link_val
);
1478 char *dest_link_val
= alloca (src_link_len
+ 1);
1479 int dest_link_len
= readlink (dst_path
, dest_link_val
,
1481 if ((size_t) dest_link_len
== src_link_len
1482 && strncmp (dest_link_val
, src_link_val
, src_link_len
) == 0)
1485 free (src_link_val
);
1489 error (0, saved_errno
, _("cannot create symbolic link %s"),
1495 /* There's no need to preserve timestamps or permissions. */
1496 preserve_metadata
= 0;
1498 if (x
->preserve_ownership
)
1500 /* Preserve the owner and group of the just-`copied'
1501 symbolic link, if possible. */
1503 if (DO_CHOWN (lchown
, dst_path
, src_sb
.st_uid
, src_sb
.st_gid
))
1505 error (0, errno
, _("failed to preserve ownership for %s"),
1510 /* Can't preserve ownership of symlinks.
1511 FIXME: maybe give a warning or even error for symlinks
1512 in directories with the sticky bit set -- there, not
1513 preserving owner/group is a potential security problem. */
1520 error (0, 0, _("%s has unknown file type"), quote (src_path
));
1524 if (command_line_arg
)
1525 record_file (x
->dest_info
, dst_path
, NULL
);
1527 if ( ! preserve_metadata
)
1530 /* POSIX says that `cp -p' must restore the following:
1532 - setuid, setgid bits
1534 If it fails to restore any of those, we may give a warning but
1535 the destination must not be removed.
1536 FIXME: implement the above. */
1538 /* Adjust the times (and if possible, ownership) for the copy.
1539 chown turns off set[ug]id bits for non-root,
1540 so do the chmod last. */
1542 if (x
->preserve_timestamps
)
1544 struct timespec timespec
[2];
1546 timespec
[0].tv_sec
= src_sb
.st_atime
;
1547 timespec
[0].tv_nsec
= TIMESPEC_NS (src_sb
.st_atim
);
1548 timespec
[1].tv_sec
= src_sb
.st_mtime
;
1549 timespec
[1].tv_nsec
= TIMESPEC_NS (src_sb
.st_mtim
);
1551 if (utimens (dst_path
, timespec
))
1553 error (0, errno
, _("preserving times for %s"), quote (dst_path
));
1554 if (x
->require_preserve
)
1559 /* Avoid calling chown if we know it's not necessary. */
1560 if (x
->preserve_ownership
1561 && (new_dst
|| !SAME_OWNER_AND_GROUP (src_sb
, dst_sb
)))
1564 if (DO_CHOWN (chown
, dst_path
, src_sb
.st_uid
, src_sb
.st_gid
))
1566 error (0, errno
, _("failed to preserve ownership for %s"),
1568 if (x
->require_preserve
)
1573 #if HAVE_STRUCT_STAT_ST_AUTHOR
1574 /* Preserve the st_author field. */
1576 file_t file
= file_name_lookup (dst_path
, 0, 0);
1577 if (file
== MACH_PORT_NULL
)
1578 error (0, errno
, _("failed to lookup file %s"), quote (dst_path
));
1581 error_t err
= file_chauthor (file
, src_sb
.st_author
);
1583 error (0, err
, _("failed to preserve authorship for %s"),
1585 mach_port_deallocate (mach_task_self (), file
);
1590 /* Permissions of newly-created regular files were set upon `open' in
1591 copy_reg. But don't return early if there were any special bits and
1592 we had to run chown, because the chown must have reset those bits. */
1593 if ((new_dst
&& copied_as_regular
)
1594 && !(ran_chown
&& (src_mode
& ~S_IRWXUGO
)))
1595 return delayed_fail
;
1597 if ((x
->preserve_mode
|| new_dst
)
1598 && (x
->copy_as_regular
|| S_ISREG (src_type
) || S_ISDIR (src_type
)))
1600 if (chmod (dst_path
, get_dest_mode (x
, src_mode
)))
1602 error (0, errno
, _("setting permissions for %s"), quote (dst_path
));
1603 if (x
->set_mode
|| x
->require_preserve
)
1608 return delayed_fail
;
1612 /* We have failed to create the destination file.
1613 If we've just added a dev/ino entry via the remember_copied
1614 call above (i.e., unless we've just failed to create a hard link),
1615 remove the entry associating the source dev/ino with the
1616 destination file name, so we don't try to `preserve' a link
1617 to a file we didn't create. */
1618 if (earlier_file
== NULL
)
1619 forget_created (src_sb
.st_ino
, src_sb
.st_dev
);
1623 if (rename (dst_backup
, dst_path
))
1624 error (0, errno
, _("cannot un-backup %s"), quote (dst_path
));
1628 printf (_("%s -> %s (unbackup)\n"),
1629 quote_n (0, dst_backup
), quote_n (1, dst_path
));
1636 valid_options (const struct cp_options
*co
)
1638 assert (co
!= NULL
);
1639 assert (VALID_BACKUP_TYPE (co
->backup_type
));
1640 assert (VALID_SPARSE_MODE (co
->sparse_mode
));
1644 /* Copy the file SRC_PATH to the file DST_PATH. The files may be of
1645 any type. NONEXISTENT_DST should be nonzero if the file DST_PATH
1646 is known not to exist (e.g., because its parent directory was just
1647 created); NONEXISTENT_DST should be zero if DST_PATH might already
1648 exist. OPTIONS is ... FIXME-describe
1649 Set *COPY_INTO_SELF to nonzero if SRC_PATH is a parent of (or the
1650 same as) DST_PATH; otherwise, set it to zero.
1651 Return 0 if successful, 1 if an error occurs. */
1654 copy (const char *src_path
, const char *dst_path
,
1655 int nonexistent_dst
, const struct cp_options
*options
,
1656 int *copy_into_self
, int *rename_succeeded
)
1658 assert (valid_options (options
));
1660 /* Record the file names: they're used in case of error, when copying
1661 a directory into itself. I don't like to make these tools do *any*
1662 extra work in the common case when that work is solely to handle
1663 exceptional cases, but in this case, I don't see a way to derive the
1664 top level source and destination directory names where they're used.
1665 An alternative is to use COPY_INTO_SELF and print the diagnostic
1666 from every caller -- but I don't want to do that. */
1667 top_level_src_path
= src_path
;
1668 top_level_dst_path
= dst_path
;
1670 return copy_internal (src_path
, dst_path
, nonexistent_dst
, 0, NULL
,
1671 options
, 1, copy_into_self
, rename_succeeded
);