*** empty log message ***
[coreutils.git] / src / copy.c
blob99f02868afb7bae09af55f3d7db67b0667ecb709
1 /* copy.c -- core functions for copying files and directories
2 Copyright (C) 89, 90, 91, 1995-2002 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)
7 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, 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. */
20 #ifdef _AIX
21 #pragma alloca
22 #endif
24 #include <config.h>
25 #include <stdio.h>
26 #include <assert.h>
27 #include <sys/types.h>
29 #if HAVE_HURD_H
30 # include <hurd.h>
31 #endif
33 #include "system.h"
34 #include "error.h"
35 #include "backupfile.h"
36 #include "savedir.h"
37 #include "copy.h"
38 #include "cp-hash.h"
39 #include "hash.h"
40 #include "hash-pjw.h"
41 #include "same.h"
42 #include "dirname.h"
43 #include "full-write.h"
44 #include "path-concat.h"
45 #include "quote.h"
46 #include "same.h"
47 #include "xreadlink.h"
49 #define DO_CHOWN(Chown, File, New_uid, New_gid) \
50 (Chown (File, New_uid, New_gid) \
51 /* If non-root uses -p, it's ok if we can't preserve ownership. \
52 But root probably wants to know, e.g. if NFS disallows it, \
53 or if the target system doesn't support file ownership. */ \
54 && ((errno != EPERM && errno != EINVAL) || x->myeuid == 0))
56 #define SAME_OWNER(A, B) ((A).st_uid == (B).st_uid)
57 #define SAME_GROUP(A, B) ((A).st_gid == (B).st_gid)
58 #define SAME_OWNER_AND_GROUP(A, B) (SAME_OWNER (A, B) && SAME_GROUP (A, B))
60 #define UNWRITABLE(File_name, File_mode) \
61 ( /* euidaccess is not meaningful for symlinks */ \
62 ! S_ISLNK (File_mode) \
63 && euidaccess (File_name, W_OK) != 0)
65 struct dir_list
67 struct dir_list *parent;
68 ino_t ino;
69 dev_t dev;
72 /* Describe a just-created or just-renamed destination file. */
73 struct F_triple
75 char const* name;
76 ino_t st_ino;
77 dev_t st_dev;
80 /* Initial size of the above hash table. */
81 #define DEST_INFO_INITIAL_CAPACITY 61
83 int euidaccess ();
84 int yesno ();
86 static int copy_internal PARAMS ((const char *src_path, const char *dst_path,
87 int new_dst, dev_t device,
88 struct dir_list *ancestors,
89 const struct cp_options *x,
90 int move_mode,
91 int *copy_into_self,
92 int *rename_succeeded));
94 /* Pointers to the file names: they're used in the diagnostic that is issued
95 when we detect the user is trying to copy a directory into itself. */
96 static char const *top_level_src_path;
97 static char const *top_level_dst_path;
99 /* The invocation name of this program. */
100 extern char *program_name;
102 /* Encapsulate selection of the file mode to be applied to
103 new non-directories. */
105 static mode_t
106 get_dest_mode (const struct cp_options *option, mode_t mode)
108 /* In some applications (e.g., install), use precisely the
109 specified mode. */
110 if (option->set_mode)
111 return option->mode;
113 /* Honor the umask for `cp', but not for `mv' or `cp -p'.
114 In addition, `cp' without -p must clear the set-user-ID and set-group-ID
115 bits. POSIX requires it do that when creating new files. */
116 if (!option->move_mode && !option->preserve_mode)
117 mode &= (option->umask_kill & ~(S_ISUID | S_ISGID));
119 return mode;
122 /* FIXME: describe */
123 /* FIXME: rewrite this to use a hash table so we avoid the quadratic
124 performance hit that's probably noticeable only on trees deeper
125 than a few hundred levels. See use of active_dir_map in remove.c */
127 static int
128 is_ancestor (const struct stat *sb, const struct dir_list *ancestors)
130 while (ancestors != 0)
132 if (ancestors->ino == sb->st_ino && ancestors->dev == sb->st_dev)
133 return 1;
134 ancestors = ancestors->parent;
136 return 0;
139 /* Read the contents of the directory SRC_PATH_IN, and recursively
140 copy the contents to DST_PATH_IN. NEW_DST is nonzero if
141 DST_PATH_IN is a directory that was created previously in the
142 recursion. SRC_SB and ANCESTORS describe SRC_PATH_IN.
143 Set *COPY_INTO_SELF to nonzero if SRC_PATH_IN is a parent of
144 (or the same as) DST_PATH_IN; otherwise, set it to zero.
145 Return 0 if successful, -1 if an error occurs. */
147 static int
148 copy_dir (const char *src_path_in, const char *dst_path_in, int new_dst,
149 const struct stat *src_sb, struct dir_list *ancestors,
150 const struct cp_options *x, int *copy_into_self)
152 char *name_space;
153 char *namep;
154 struct cp_options non_command_line_options = *x;
155 int ret = 0;
157 name_space = savedir (src_path_in);
158 if (name_space == NULL)
160 /* This diagnostic is a bit vague because savedir can fail in
161 several different ways. */
162 error (0, errno, _("cannot access %s"), quote (src_path_in));
163 return -1;
166 /* For cp's -H option, dereference command line arguments, but do not
167 dereference symlinks that are found via recursive traversal. */
168 if (x->dereference == DEREF_COMMAND_LINE_ARGUMENTS)
169 non_command_line_options.xstat = lstat;
171 namep = name_space;
172 while (*namep != '\0')
174 int local_copy_into_self;
175 char *src_path = path_concat (src_path_in, namep, NULL);
176 char *dst_path = path_concat (dst_path_in, namep, NULL);
178 if (dst_path == NULL || src_path == NULL)
179 xalloc_die ();
181 ret |= copy_internal (src_path, dst_path, new_dst, src_sb->st_dev,
182 ancestors, &non_command_line_options, 0,
183 &local_copy_into_self, NULL);
184 *copy_into_self |= local_copy_into_self;
186 free (dst_path);
187 free (src_path);
189 namep += strlen (namep) + 1;
191 free (name_space);
192 return -ret;
195 /* Copy a regular file from SRC_PATH to DST_PATH.
196 If the source file contains holes, copies holes and blocks of zeros
197 in the source file as holes in the destination file.
198 (Holes are read as zeroes by the `read' system call.)
199 Use DST_MODE as the 3rd argument in the call to open.
200 X provides many option settings.
201 Return 0 if successful, -1 if an error occurred.
202 *NEW_DST is as in copy_internal. SRC_SB is the result
203 of calling xstat (aka stat in this case) on SRC_PATH. */
205 static int
206 copy_reg (const char *src_path, const char *dst_path,
207 const struct cp_options *x, mode_t dst_mode, int *new_dst,
208 struct stat const *src_sb)
210 char *buf;
211 int buf_size;
212 int dest_desc;
213 int source_desc;
214 struct stat sb;
215 struct stat src_open_sb;
216 char *cp;
217 int *ip;
218 int return_val = 0;
219 off_t n_read_total = 0;
220 int last_write_made_hole = 0;
221 int make_holes = (x->sparse_mode == SPARSE_ALWAYS);
223 source_desc = open (src_path, O_RDONLY);
224 if (source_desc < 0)
226 error (0, errno, _("cannot open %s for reading"), quote (src_path));
227 return -1;
230 if (fstat (source_desc, &src_open_sb))
232 error (0, errno, _("cannot fstat %s"), quote (src_path));
233 return_val = -1;
234 goto close_src_desc;
237 /* Compare the source dev/ino from the open file to the incoming,
238 saved ones obtained via a previous call to stat. */
239 if (! SAME_INODE (*src_sb, src_open_sb))
241 error (0, 0,
242 _("skipping file %s, as it was replaced while being copied"),
243 quote (src_path));
244 return_val = -1;
245 goto close_src_desc;
248 /* These semantics are required for cp.
249 The if-block will be taken in move_mode. */
250 if (*new_dst)
252 dest_desc = open (dst_path, O_WRONLY | O_CREAT, dst_mode);
254 else
256 dest_desc = open (dst_path, O_WRONLY | O_TRUNC, dst_mode);
258 if (dest_desc < 0 && x->unlink_dest_after_failed_open)
260 if (unlink (dst_path))
262 error (0, errno, _("cannot remove %s"), quote (dst_path));
263 return_val = -1;
264 goto close_src_desc;
267 /* Tell caller that the destination file was unlinked. */
268 *new_dst = 1;
270 /* Try the open again, but this time with different flags. */
271 dest_desc = open (dst_path, O_WRONLY | O_CREAT, dst_mode);
275 if (dest_desc < 0)
277 error (0, errno, _("cannot create regular file %s"), quote (dst_path));
278 return_val = -1;
279 goto close_src_desc;
282 /* Determine the optimal buffer size. */
284 if (fstat (dest_desc, &sb))
286 error (0, errno, _("cannot fstat %s"), quote (dst_path));
287 return_val = -1;
288 goto close_src_and_dst_desc;
291 buf_size = ST_BLKSIZE (sb);
293 #if HAVE_STRUCT_STAT_ST_BLOCKS
294 if (x->sparse_mode == SPARSE_AUTO && S_ISREG (sb.st_mode))
296 /* Use a heuristic to determine whether SRC_PATH contains any
297 sparse blocks. */
299 if (fstat (source_desc, &sb))
301 error (0, errno, _("cannot fstat %s"), quote (src_path));
302 return_val = -1;
303 goto close_src_and_dst_desc;
306 /* If the file has fewer blocks than would normally
307 be needed for a file of its size, then
308 at least one of the blocks in the file is a hole. */
309 if (S_ISREG (sb.st_mode)
310 && sb.st_size / ST_NBLOCKSIZE > ST_NBLOCKS (sb))
311 make_holes = 1;
313 #endif
315 /* Make a buffer with space for a sentinel at the end. */
317 buf = (char *) alloca (buf_size + sizeof (int));
319 for (;;)
321 ssize_t n_read = read (source_desc, buf, buf_size);
322 if (n_read < 0)
324 #ifdef EINTR
325 if (errno == EINTR)
326 continue;
327 #endif
328 error (0, errno, _("reading %s"), quote (src_path));
329 return_val = -1;
330 goto close_src_and_dst_desc;
332 if (n_read == 0)
333 break;
335 n_read_total += n_read;
337 ip = 0;
338 if (make_holes)
340 buf[n_read] = 1; /* Sentinel to stop loop. */
342 /* Find first nonzero *word*, or the word with the sentinel. */
344 ip = (int *) buf;
345 while (*ip++ == 0)
348 /* Find the first nonzero *byte*, or the sentinel. */
350 cp = (char *) (ip - 1);
351 while (*cp++ == 0)
354 /* If we found the sentinel, the whole input block was zero,
355 and we can make a hole. */
357 if (cp > buf + n_read)
359 /* Make a hole. */
360 if (lseek (dest_desc, (off_t) n_read, SEEK_CUR) < 0L)
362 error (0, errno, _("cannot lseek %s"), quote (dst_path));
363 return_val = -1;
364 goto close_src_and_dst_desc;
366 last_write_made_hole = 1;
368 else
369 /* Clear to indicate that a normal write is needed. */
370 ip = 0;
372 if (ip == 0)
374 size_t n = n_read;
375 if (full_write (dest_desc, buf, n) != n)
377 error (0, errno, _("writing %s"), quote (dst_path));
378 return_val = -1;
379 goto close_src_and_dst_desc;
381 last_write_made_hole = 0;
385 /* If the file ends with a `hole', something needs to be written at
386 the end. Otherwise the kernel would truncate the file at the end
387 of the last write operation. */
389 if (last_write_made_hole)
391 #if HAVE_FTRUNCATE
392 /* Write a null character and truncate it again. */
393 if (full_write (dest_desc, "", 1) != 1
394 || ftruncate (dest_desc, n_read_total) < 0)
395 #else
396 /* Seek backwards one character and write a null. */
397 if (lseek (dest_desc, (off_t) -1, SEEK_CUR) < 0L
398 || full_write (dest_desc, "", 1) != 1)
399 #endif
401 error (0, errno, _("writing %s"), quote (dst_path));
402 return_val = -1;
406 close_src_and_dst_desc:
407 if (close (dest_desc) < 0)
409 error (0, errno, _("closing %s"), quote (dst_path));
410 return_val = -1;
412 close_src_desc:
413 if (close (source_desc) < 0)
415 error (0, errno, _("closing %s"), quote (src_path));
416 return_val = -1;
419 return return_val;
422 /* Return nonzero if it's ok that the source and destination
423 files are the `same' by some measure. The goal is to avoid
424 making the `copy' operation remove both copies of the file
425 in that case, while still allowing the user to e.g., move or
426 copy a regular file onto a symlink that points to it.
427 Try to minimize the cost of this function in the common case. */
429 static int
430 same_file_ok (const char *src_path, const struct stat *src_sb,
431 const char *dst_path, const struct stat *dst_sb,
432 const struct cp_options *x, int *return_now)
434 const struct stat *src_sb_link;
435 const struct stat *dst_sb_link;
436 struct stat tmp_dst_sb;
437 struct stat tmp_src_sb;
439 int same_link;
440 int same = (SAME_INODE (*src_sb, *dst_sb));
442 *return_now = 0;
444 /* FIXME: this should (at the very least) be moved into the following
445 if-block. More likely, it should be removed, because it inhibits
446 making backups. But removing it will result in a change in behavior
447 that will probably have to be documented -- and tests will have to
448 be updated. */
449 if (same && x->hard_link)
451 *return_now = 1;
452 return 1;
455 if (x->xstat == lstat)
457 same_link = same;
459 /* If both the source and destination files are symlinks (and we'll
460 know this here IFF preserving symlinks (aka xstat == lstat),
461 then it's ok -- as long as they are distinct. */
462 if (S_ISLNK (src_sb->st_mode) && S_ISLNK (dst_sb->st_mode))
463 return ! same_name (src_path, dst_path);
465 src_sb_link = src_sb;
466 dst_sb_link = dst_sb;
468 else
470 if (!same)
471 return 1;
473 if (lstat (dst_path, &tmp_dst_sb)
474 || lstat (src_path, &tmp_src_sb))
475 return 1;
477 src_sb_link = &tmp_src_sb;
478 dst_sb_link = &tmp_dst_sb;
480 same_link = SAME_INODE (*src_sb_link, *dst_sb_link);
482 /* If both are symlinks, then it's ok, but only if the destination
483 will be unlinked before being opened. This is like the test
484 above, but with the addition of the unlink_dest_before_opening
485 conjunct because otherwise, with two symlinks to the same target,
486 we'd end up truncating the source file. */
487 if (S_ISLNK (src_sb_link->st_mode) && S_ISLNK (dst_sb_link->st_mode)
488 && x->unlink_dest_before_opening)
489 return 1;
492 /* The backup code ensures there's a copy, so it's usually ok to
493 remove any destination file. One exception is when both
494 source and destination are the same directory entry. In that
495 case, moving the destination file aside (in making the backup)
496 would also rename the source file and result in an error. */
497 if (x->backup_type != none)
499 if (!same_link)
501 /* In copy mode when dereferencing symlinks, if the source is a
502 symlink and the dest is not, then backing up the destination
503 (moving it aside) would make it a dangling symlink, and the
504 subsequent attempt to open it in copy_reg would fail with
505 a misleading diagnostic. Avoid that by returning zero in
506 that case so the caller can make cp (or mv when it has to
507 resort to reading the source file) fail now. */
509 /* FIXME-note: even with the following kludge, we can still provoke
510 the offending diagnostic. It's just a little harder to do :-)
511 $ rm -f a b c; touch c; ln -s c b; ln -s b a; cp -b a b
512 cp: cannot open `a' for reading: No such file or directory
513 That's misleading, since a subsequent `ls' shows that `a'
514 is still there.
515 One solution would be to open the source file *before* moving
516 aside the destination, but that'd involve a big rewrite. */
517 if ( ! x->move_mode
518 && x->dereference != DEREF_NEVER
519 && S_ISLNK (src_sb_link->st_mode)
520 && ! S_ISLNK (dst_sb_link->st_mode))
521 return 0;
523 return 1;
526 return ! same_name (src_path, dst_path);
529 #if 0
530 /* FIXME: use or remove */
532 /* If we're making a backup, we'll detect the problem case in
533 copy_reg because SRC_PATH will no longer exist. Allowing
534 the test to be deferred lets cp do some useful things.
535 But when creating hardlinks and SRC_PATH is a symlink
536 but DST_PATH is not we must test anyway. */
537 if (x->hard_link
538 || !S_ISLNK (src_sb_link->st_mode)
539 || S_ISLNK (dst_sb_link->st_mode))
540 return 1;
542 if (x->dereference != DEREF_NEVER)
543 return 1;
544 #endif
546 /* They may refer to the same file if we're in move mode and the
547 target is a symlink. That is ok, since we remove any existing
548 destination file before opening it -- via `rename' if they're on
549 the same file system, via `unlink (DST_PATH)' otherwise.
550 It's also ok if they're distinct hard links to the same file. */
551 if ((x->move_mode || x->unlink_dest_before_opening)
552 && (S_ISLNK (dst_sb_link->st_mode)
553 || (same_link && !same_name (src_path, dst_path))))
554 return 1;
556 /* If neither is a symlink, then it's ok as long as they aren't
557 hard links to the same file. */
558 if (!S_ISLNK (src_sb_link->st_mode) && !S_ISLNK (dst_sb_link->st_mode))
560 if (!SAME_INODE (*src_sb_link, *dst_sb_link))
561 return 1;
563 /* If they are the same file, it's ok if we're making hard links. */
564 if (x->hard_link)
566 *return_now = 1;
567 return 1;
571 /* It's ok to remove a destination symlink. But that works only when we
572 unlink before opening the destination and when the source and destination
573 files are on the same partition. */
574 if (x->unlink_dest_before_opening
575 && S_ISLNK (dst_sb_link->st_mode))
576 return dst_sb_link->st_dev == src_sb_link->st_dev;
578 if (x->xstat == lstat)
580 if ( ! S_ISLNK (src_sb_link->st_mode))
581 tmp_src_sb = *src_sb_link;
582 else if (stat (src_path, &tmp_src_sb))
583 return 1;
585 if ( ! S_ISLNK (dst_sb_link->st_mode))
586 tmp_dst_sb = *dst_sb_link;
587 else if (stat (dst_path, &tmp_dst_sb))
588 return 1;
590 if ( ! SAME_INODE (tmp_src_sb, tmp_dst_sb))
591 return 1;
593 /* FIXME: shouldn't this be testing whether we're making symlinks? */
594 if (x->hard_link)
596 *return_now = 1;
597 return 1;
601 return 0;
604 static void
605 overwrite_prompt (char const *dst_path, struct stat const *dst_sb)
607 if (euidaccess (dst_path, W_OK) != 0)
609 fprintf (stderr,
610 _("%s: overwrite %s, overriding mode %04lo? "),
611 program_name, quote (dst_path),
612 (unsigned long) (dst_sb->st_mode & CHMOD_MODE_BITS));
614 else
616 fprintf (stderr, _("%s: overwrite %s? "),
617 program_name, quote (dst_path));
621 /* Hash an F_triple. */
622 static unsigned int
623 triple_hash (void const *x, unsigned int table_size)
625 struct F_triple const *p = x;
627 /* Also take the name into account, so that when moving N hard links to the
628 same file (all listed on the command line) all into the same directory,
629 we don't experience any N^2 behavior. */
630 /* FIXME-maybe: is it worth the overhead of doing this
631 just to avoid N^2 in such an unusual case? N would have
632 to be very large to make the N^2 factor noticable, and
633 one would probably encounter a limit on the length of
634 a command line before it became a problem. */
635 unsigned int tmp = hash_pjw (p->name, table_size);
637 /* Ignoring the device number here should be fine. */
638 return (tmp | p->st_ino) % table_size;
641 /* Hash an F_triple. */
642 static unsigned int
643 triple_hash_no_name (void const *x, unsigned int table_size)
645 struct F_triple const *p = x;
647 /* Ignoring the device number here should be fine. */
648 return p->st_ino % table_size;
651 /* Compare two F_triple structs. */
652 static bool
653 triple_compare (void const *x, void const *y)
655 struct F_triple const *a = x;
656 struct F_triple const *b = y;
657 return (SAME_INODE (*a, *b) && same_name (a->name, b->name)) ? true : false;
660 /* Free an F_triple. */
661 static void
662 triple_free (void *x)
664 struct F_triple *a = x;
665 free ((char *) (a->name));
666 free (a);
669 /* Initialize the hash table implementing a set of F_triple entries
670 corresponding to destination files. */
671 void
672 dest_info_init (struct cp_options *x)
674 x->dest_info
675 = hash_initialize (DEST_INFO_INITIAL_CAPACITY,
676 NULL,
677 triple_hash,
678 triple_compare,
679 triple_free);
682 /* Initialize the hash table implementing a set of F_triple entries
683 corresponding to source files listed on the command line. */
684 void
685 src_info_init (struct cp_options *x)
688 /* Note that we use triple_hash_no_name here.
689 Contrast with the use of triple_hash above.
690 That is necessary because a source file may be specified
691 in many different ways. We want to warn about this
692 cp a a d/
693 as well as this:
694 cp a ./a d/
696 x->src_info
697 = hash_initialize (DEST_INFO_INITIAL_CAPACITY,
698 NULL,
699 triple_hash_no_name,
700 triple_compare,
701 triple_free);
704 /* Return nonzero if there is an entry in hash table, HT,
705 for the file described by FILENAME and STATS.
706 Otherwise, return zero. */
707 static int
708 seen_file (Hash_table const *ht, char const *filename,
709 struct stat const *stats)
711 struct F_triple new_ent;
713 if (ht == NULL)
714 return 0;
716 new_ent.name = filename;
717 new_ent.st_ino = stats->st_ino;
718 new_ent.st_dev = stats->st_dev;
720 return !!hash_lookup (ht, &new_ent);
723 /* Record destination filename, FILENAME, and dev/ino from *STATS,
724 in the hash table, HT. If HT is NULL, return immediately.
725 If STATS is NULL, call lstat on FILENAME to get the device
726 and inode numbers. If that lstat fails, simply return.
727 If memory allocation fails, exit immediately. */
728 static void
729 record_file (Hash_table *ht, char const *filename,
730 struct stat const *stats)
732 struct F_triple *ent;
734 if (ht == NULL)
735 return;
737 ent = (struct F_triple *) xmalloc (sizeof *ent);
738 ent->name = xstrdup (filename);
739 if (stats)
741 ent->st_ino = stats->st_ino;
742 ent->st_dev = stats->st_dev;
744 else
746 struct stat sb;
747 if (lstat (filename, &sb))
748 return;
749 ent->st_ino = sb.st_ino;
750 ent->st_dev = sb.st_dev;
754 struct F_triple *ent_from_table = hash_insert (ht, ent);
755 if (ent_from_table == NULL)
757 /* Insertion failed due to lack of memory. */
758 xalloc_die ();
761 if (ent_from_table != ent)
763 /* There was alread a matching entry in the table, so ENT was
764 not inserted. Free it. */
765 triple_free (ent);
770 /* Copy the file SRC_PATH to the file DST_PATH. The files may be of
771 any type. NEW_DST should be nonzero if the file DST_PATH cannot
772 exist because its parent directory was just created; NEW_DST should
773 be zero if DST_PATH might already exist. DEVICE is the device
774 number of the parent directory, or 0 if the parent of this file is
775 not known. ANCESTORS points to a linked, null terminated list of
776 devices and inodes of parent directories of SRC_PATH. COMMAND_LINE_ARG
777 is nonzero iff SRC_PATH was specified on the command line.
778 Set *COPY_INTO_SELF to nonzero if SRC_PATH is a parent of (or the
779 same as) DST_PATH; otherwise, set it to zero.
780 Return 0 if successful, 1 if an error occurs. */
782 static int
783 copy_internal (const char *src_path, const char *dst_path,
784 int new_dst,
785 dev_t device,
786 struct dir_list *ancestors,
787 const struct cp_options *x,
788 int command_line_arg,
789 int *copy_into_self,
790 int *rename_succeeded)
792 struct stat src_sb;
793 struct stat dst_sb;
794 mode_t src_mode;
795 mode_t src_type;
796 char *earlier_file = NULL;
797 char *dst_backup = NULL;
798 int backup_succeeded = 0;
799 int delayed_fail;
800 int copied_as_regular = 0;
801 int ran_chown = 0;
802 int preserve_metadata;
804 /* move_mode is set to the value from the `options' parameter for the
805 first copy_internal call. For any subsequent recursive call, it must
806 be zero. This is because if we're moving (via mv) a hierarchy and
807 end up having to recurse, it means the initial rename failed and so we
808 are in the process of *copy*ing all of the parts, not renaming them. */
809 int move_mode = (command_line_arg ? x->move_mode : 0);
811 if (move_mode && rename_succeeded)
812 *rename_succeeded = 0;
814 *copy_into_self = 0;
815 if ((*(x->xstat)) (src_path, &src_sb))
817 error (0, errno, _("cannot stat %s"), quote (src_path));
818 return 1;
821 src_type = src_sb.st_mode;
823 src_mode = src_sb.st_mode;
825 if (S_ISDIR (src_type) && !x->recursive)
827 error (0, 0, _("omitting directory %s"), quote (src_path));
828 return 1;
831 /* Detect the case in which the same source file appears more than
832 once on the command line and no backup option has been selected.
833 If so, simply warn and don't copy it the second time.
834 This check is enabled only if x->src_info is non-NULL. */
835 if (command_line_arg)
837 if ( ! S_ISDIR (src_sb.st_mode)
838 && x->backup_type == none
839 && seen_file (x->src_info, src_path, &src_sb))
841 error (0, 0, _("warning: source file %s specified more than once"),
842 quote (src_path));
843 return 0;
846 record_file (x->src_info, src_path, &src_sb);
849 if (!new_dst)
851 if ((*(x->xstat)) (dst_path, &dst_sb))
853 if (errno != ENOENT)
855 error (0, errno, _("cannot stat %s"), quote (dst_path));
856 return 1;
858 else
860 new_dst = 1;
863 else
865 int return_now;
866 int ok = same_file_ok (src_path, &src_sb, dst_path, &dst_sb,
867 x, &return_now);
868 if (return_now)
869 return 0;
871 if (! ok)
873 error (0, 0, _("%s and %s are the same file"),
874 quote_n (0, src_path), quote_n (1, dst_path));
875 return 1;
878 if (!S_ISDIR (dst_sb.st_mode))
880 if (S_ISDIR (src_type))
882 error (0, 0,
883 _("cannot overwrite non-directory %s with directory %s"),
884 quote_n (0, dst_path), quote_n (1, src_path));
885 return 1;
888 /* Don't let the user destroy their data, even if they try hard:
889 This mv command must fail (likewise for cp):
890 rm -rf a b c; mkdir a b c; touch a/f b/f; mv a/f b/f c
891 Otherwise, the contents of b/f would be lost.
892 In the case of `cp', b/f would be lost if the user simulated
893 a move using cp and rm.
894 Note that it works fine if you use --backup=numbered. */
895 if (command_line_arg
896 && x->backup_type != numbered
897 && seen_file (x->dest_info, dst_path, &dst_sb))
899 error (0, 0,
900 _("will not overwrite just-created %s with %s"),
901 quote_n (0, dst_path), quote_n (1, src_path));
902 return 1;
906 if (!S_ISDIR (src_type))
908 if (S_ISDIR (dst_sb.st_mode))
910 error (0, 0,
911 _("cannot overwrite directory %s with non-directory"),
912 quote (dst_path));
913 return 1;
916 if (x->update && MTIME_CMP (src_sb, dst_sb) <= 0)
918 /* We're using --update and the source file is older
919 than the destination file, so there is no need to
920 copy or move. */
921 /* Pretend the rename succeeded, so the caller (mv)
922 doesn't end up removing the source file. */
923 if (rename_succeeded)
924 *rename_succeeded = 1;
925 return 0;
929 /* When there is an existing destination file, we may end up
930 returning early, and hence not copying/moving the file.
931 This may be due to an interactive `negative' reply to the
932 prompt about the existing file. It may also be due to the
933 use of the --reply=no option. */
934 if (!S_ISDIR (src_type))
936 /* cp and mv treat -i and -f differently. */
937 if (x->move_mode)
939 if ((x->interactive == I_ALWAYS_NO
940 && UNWRITABLE (dst_path, dst_sb.st_mode))
941 || ((x->interactive == I_ASK_USER
942 || (x->interactive == I_UNSPECIFIED
943 && x->stdin_tty
944 && UNWRITABLE (dst_path, dst_sb.st_mode)))
945 && (overwrite_prompt (dst_path, &dst_sb), 1)
946 && ! yesno ()))
948 /* Pretend the rename succeeded, so the caller (mv)
949 doesn't end up removing the source file. */
950 if (rename_succeeded)
951 *rename_succeeded = 1;
952 return 0;
955 else
957 if (x->interactive == I_ALWAYS_NO
958 || (x->interactive == I_ASK_USER
959 && (overwrite_prompt (dst_path, &dst_sb), 1)
960 && ! yesno ()))
962 return 0;
967 if (move_mode)
969 /* In move_mode, DEST may not be an existing directory. */
970 if (S_ISDIR (dst_sb.st_mode))
972 error (0, 0, _("cannot overwrite directory %s"),
973 quote (dst_path));
974 return 1;
977 /* Don't allow user to move a directory onto a non-directory. */
978 if (S_ISDIR (src_sb.st_mode) && !S_ISDIR (dst_sb.st_mode))
980 error (0, 0,
981 _("cannot move directory onto non-directory: %s -> %s"),
982 quote_n (0, src_path), quote_n (0, dst_path));
983 return 1;
987 if (x->backup_type != none && !S_ISDIR (dst_sb.st_mode))
989 char *tmp_backup = find_backup_file_name (dst_path,
990 x->backup_type);
991 if (tmp_backup == NULL)
992 xalloc_die ();
994 /* Detect (and fail) when creating the backup file would
995 destroy the source file. Before, running the commands
996 cd /tmp; rm -f a a~; : > a; echo A > a~; cp --b=simple a~ a
997 would leave two zero-length files: a and a~. */
998 /* FIXME: but simply change e.g., the final a~ to `./a~'
999 and the source will still be destroyed. */
1000 if (STREQ (tmp_backup, src_path))
1002 const char *fmt;
1003 fmt = (x->move_mode
1004 ? _("backing up %s would destroy source; %s not moved")
1005 : _("backing up %s would destroy source; %s not copied"));
1006 error (0, 0, fmt,
1007 quote_n (0, dst_path),
1008 quote_n (1, src_path));
1009 free (tmp_backup);
1010 return 1;
1013 dst_backup = (char *) alloca (strlen (tmp_backup) + 1);
1014 strcpy (dst_backup, tmp_backup);
1015 free (tmp_backup);
1016 if (rename (dst_path, dst_backup))
1018 if (errno != ENOENT)
1020 error (0, errno, _("cannot backup %s"), quote (dst_path));
1021 return 1;
1023 else
1025 dst_backup = NULL;
1028 else
1030 backup_succeeded = 1;
1032 new_dst = 1;
1034 else if (! S_ISDIR (dst_sb.st_mode)
1035 && (x->unlink_dest_before_opening
1036 || (x->xstat == lstat
1037 && ! S_ISREG (src_sb.st_mode))))
1039 if (unlink (dst_path) && errno != ENOENT)
1041 error (0, errno, _("cannot remove %s"), quote (dst_path));
1042 return 1;
1044 new_dst = 1;
1049 /* If the source is a directory, we don't always create the destination
1050 directory. So --verbose should not announce anything until we're
1051 sure we'll create a directory. */
1052 if (x->verbose && !S_ISDIR (src_type))
1054 printf ("%s -> %s", quote_n (0, src_path), quote_n (1, dst_path));
1055 if (backup_succeeded)
1056 printf (_(" (backup: %s)"), quote (dst_backup));
1057 putchar ('\n');
1060 /* Associate the destination path with the source device and inode
1061 so that if we encounter a matching dev/ino pair in the source tree
1062 we can arrange to create a hard link between the corresponding names
1063 in the destination tree.
1065 Sometimes, when preserving links, we have to record dev/ino even
1066 though st_nlink == 1:
1067 - when using -H and processing a command line argument;
1068 that command line argument could be a symlink pointing to another
1069 command line argument. With `cp -H --preserve=link', we hard-link
1070 those two destination files.
1071 - likewise for -L except that it applies to all files, not just
1072 command line arguments.
1074 Also record directory dev/ino when using --recursive. We'll use that
1075 info to detect this problem: cp -R dir dir. FIXME-maybe: ideally,
1076 directory info would be recorded in a separate hash table, since
1077 such entries are useful only while a single command line hierarchy
1078 is being copied -- so that separate table could be cleared between
1079 command line args. Using the same hash table to preserve hard
1080 links means that it may not be cleared. */
1082 if ((x->preserve_links
1083 && (1 < src_sb.st_nlink
1084 || (command_line_arg
1085 && x->dereference == DEREF_COMMAND_LINE_ARGUMENTS)
1086 || x->dereference == DEREF_ALWAYS))
1087 || (x->recursive && S_ISDIR (src_type)))
1089 earlier_file = remember_copied (dst_path, src_sb.st_ino, src_sb.st_dev);
1092 /* Did we copy this inode somewhere else (in this command line argument)
1093 and therefore this is a second hard link to the inode? */
1095 if (earlier_file)
1097 /* Avoid damaging the destination filesystem by refusing to preserve
1098 hard-linked directories (which are found at least in Netapp snapshot
1099 directories). */
1100 if (S_ISDIR (src_type))
1102 /* If src_path and earlier_file refer to the same directory entry,
1103 then warn about copying a directory into itself. */
1104 if (same_name (src_path, earlier_file))
1106 error (0, 0, _("cannot copy a directory, %s, into itself, %s"),
1107 quote_n (0, top_level_src_path),
1108 quote_n (1, top_level_dst_path));
1109 *copy_into_self = 1;
1111 else
1113 error (0, 0, _("will not create hard link %s to directory %s"),
1114 quote_n (0, dst_path), quote_n (1, earlier_file));
1117 goto un_backup;
1120 if (link (earlier_file, dst_path))
1122 error (0, errno, _("cannot create hard link %s to %s"),
1123 quote_n (0, dst_path), quote_n (1, earlier_file));
1124 goto un_backup;
1127 return 0;
1130 /* Note that this is testing the local variable move_mode, not
1131 the x->move_mode member. */
1132 if (move_mode)
1134 if (rename (src_path, dst_path) == 0)
1136 if (x->verbose && S_ISDIR (src_type))
1137 printf ("%s -> %s\n", quote_n (0, src_path), quote_n (1, dst_path));
1138 if (rename_succeeded)
1139 *rename_succeeded = 1;
1141 if (command_line_arg)
1143 /* Record destination dev/ino/filename, so that if we are asked
1144 to overwrite that file again, we can detect it and fail. */
1145 /* It's fine to use the _source_ stat buffer (src_sb) to get the
1146 _destination_ dev/ino, since the rename above can't have
1147 changed those, and `mv' always uses lstat.
1148 We could limit it further by operating
1149 only on non-directories. */
1150 record_file (x->dest_info, dst_path, &src_sb);
1153 return 0;
1156 /* FIXME: someday, consider what to do when moving a directory into
1157 itself but when source and destination are on different devices. */
1159 /* This happens when attempting to rename a directory to a
1160 subdirectory of itself. */
1161 if (errno == EINVAL
1163 /* When src_path is on an NFS file system, some types of
1164 clients, e.g., SunOS4.1.4 and IRIX-5.3, set errno to EIO
1165 instead. Testing for this here risks misinterpreting a real
1166 I/O error as an attempt to move a directory into itself, so
1167 FIXME: consider not doing this. */
1168 || errno == EIO
1170 /* And with SunOS-4.1.4 client and OpenBSD-2.3 server,
1171 we get ENOTEMPTY. */
1172 || errno == ENOTEMPTY)
1174 /* FIXME: this is a little fragile in that it relies on rename(2)
1175 failing with a specific errno value. Expect problems on
1176 non-POSIX systems. */
1177 error (0, 0, _("cannot move %s to a subdirectory of itself, %s"),
1178 quote_n (0, top_level_src_path),
1179 quote_n (1, top_level_dst_path));
1181 /* Note that there is no need to call forget_created here,
1182 (compare with the other calls in this file) since the
1183 destination directory didn't exist before. */
1185 *copy_into_self = 1;
1186 /* FIXME-cleanup: Don't return zero here; adjust mv.c accordingly.
1187 The only caller that uses this code (mv.c) ends up setting its
1188 exit status to nonzero when copy_into_self is nonzero. */
1189 return 0;
1192 /* WARNING: there probably exist systems for which an inter-device
1193 rename fails with a value of errno not handled here.
1194 If/as those are reported, add them to the condition below.
1195 If this happens to you, please do the following and send the output
1196 to the bug-reporting address (e.g., in the output of cp --help):
1197 touch k; perl -e 'rename "k","/tmp/k" or print "$!(",$!+0,")\n"'
1198 where your current directory is on one partion and /tmp is the other.
1199 Also, please try to find the E* errno macro name corresponding to
1200 the diagnostic and parenthesized integer, and include that in your
1201 e-mail. One way to do that is to run a command like this
1202 find /usr/include/. -type f \
1203 | xargs grep 'define.*\<E[A-Z]*\>.*\<18\>' /dev/null
1204 where you'd replace `18' with the integer in parentheses that
1205 was output from the perl one-liner above.
1206 If necessary, of course, change `/tmp' to some other directory. */
1207 if (errno != EXDEV)
1209 /* There are many ways this can happen due to a race condition.
1210 When something happens between the initial xstat and the
1211 subsequent rename, we can get many different types of errors.
1212 For example, if the destination is initially a non-directory
1213 or non-existent, but it is created as a directory, the rename
1214 fails. If two `mv' commands try to rename the same file at
1215 about the same time, one will succeed and the other will fail.
1216 If the permissions on the directory containing the source or
1217 destination file are made too restrictive, the rename will
1218 fail. Etc. */
1219 error (0, errno,
1220 _("cannot move %s to %s"),
1221 quote_n (0, src_path), quote_n (1, dst_path));
1222 forget_created (src_sb.st_ino, src_sb.st_dev);
1223 return 1;
1226 /* The rename attempt has failed. Remove any existing destination
1227 file so that a cross-device `mv' acts as if it were really using
1228 the rename syscall. */
1229 if (unlink (dst_path) && errno != ENOENT)
1231 error (0, errno,
1232 _("inter-device move failed: %s to %s; unable to remove target"),
1233 quote_n (0, src_path), quote_n (1, dst_path));
1234 forget_created (src_sb.st_ino, src_sb.st_dev);
1235 return 1;
1238 new_dst = 1;
1241 delayed_fail = 0;
1243 /* In certain modes (cp's --symbolic-link), and for certain file types
1244 (symlinks and hard links) it doesn't make sense to preserve metadata,
1245 or it's possible to preserve only some of it.
1246 In such cases, set this variable to zero. */
1247 preserve_metadata = 1;
1249 if (S_ISDIR (src_type))
1251 struct dir_list *dir;
1253 /* If this directory has been copied before during the
1254 recursion, there is a symbolic link to an ancestor
1255 directory of the symbolic link. It is impossible to
1256 continue to copy this, unless we've got an infinite disk. */
1258 if (is_ancestor (&src_sb, ancestors))
1260 error (0, 0, _("cannot copy cyclic symbolic link %s"),
1261 quote (src_path));
1262 goto un_backup;
1265 /* Insert the current directory in the list of parents. */
1267 dir = (struct dir_list *) alloca (sizeof (struct dir_list));
1268 dir->parent = ancestors;
1269 dir->ino = src_sb.st_ino;
1270 dir->dev = src_sb.st_dev;
1272 if (new_dst || !S_ISDIR (dst_sb.st_mode))
1274 /* Create the new directory writable and searchable, so
1275 we can create new entries in it. */
1277 if (mkdir (dst_path, (src_mode & x->umask_kill) | S_IRWXU))
1279 error (0, errno, _("cannot create directory %s"),
1280 quote (dst_path));
1281 goto un_backup;
1284 /* Insert the created directory's inode and device
1285 numbers into the search structure, so that we can
1286 avoid copying it again. */
1288 if (remember_created (dst_path))
1289 goto un_backup;
1291 if (x->verbose)
1292 printf ("%s -> %s\n", quote_n (0, src_path), quote_n (1, dst_path));
1295 /* Are we crossing a file system boundary? */
1296 if (x->one_file_system && device != 0 && device != src_sb.st_dev)
1297 return 0;
1299 /* Copy the contents of the directory. */
1301 if (copy_dir (src_path, dst_path, new_dst, &src_sb, dir, x,
1302 copy_into_self))
1304 /* Don't just return here -- otherwise, the failure to read a
1305 single file in a source directory would cause the containing
1306 destination directory not to have owner/perms set properly. */
1307 delayed_fail = 1;
1310 #ifdef S_ISLNK
1311 else if (x->symbolic_link)
1313 preserve_metadata = 0;
1315 if (*src_path != '/')
1317 /* Check that DST_PATH denotes a file in the current directory. */
1318 struct stat dot_sb;
1319 struct stat dst_parent_sb;
1320 char *dst_parent;
1321 int in_current_dir;
1323 dst_parent = dir_name (dst_path);
1325 in_current_dir = (STREQ (".", dst_parent)
1326 /* If either stat call fails, it's ok not to report
1327 the failure and say dst_path is in the current
1328 directory. Other things will fail later. */
1329 || stat (".", &dot_sb)
1330 || stat (dst_parent, &dst_parent_sb)
1331 || SAME_INODE (dot_sb, dst_parent_sb));
1332 free (dst_parent);
1334 if (! in_current_dir)
1336 error (0, 0,
1337 _("%s: can make relative symbolic links only in current directory"),
1338 quote (dst_path));
1339 goto un_backup;
1342 if (symlink (src_path, dst_path))
1344 error (0, errno, _("cannot create symbolic link %s to %s"),
1345 quote_n (0, dst_path), quote_n (1, src_path));
1346 goto un_backup;
1349 #endif
1350 else if (x->hard_link)
1352 preserve_metadata = 0;
1353 if (link (src_path, dst_path))
1355 error (0, errno, _("cannot create link %s"), quote (dst_path));
1356 goto un_backup;
1359 else if (S_ISREG (src_type)
1360 || (x->copy_as_regular && !S_ISDIR (src_type)
1361 #ifdef S_ISLNK
1362 && !S_ISLNK (src_type)
1363 #endif
1366 copied_as_regular = 1;
1367 /* POSIX says the permission bits of the source file must be
1368 used as the 3rd argument in the open call, but that's not consistent
1369 with historical practice. */
1370 if (copy_reg (src_path, dst_path, x,
1371 get_dest_mode (x, src_mode), &new_dst, &src_sb))
1372 goto un_backup;
1374 else
1375 #ifdef S_ISFIFO
1376 if (S_ISFIFO (src_type))
1378 if (mkfifo (dst_path, get_dest_mode (x, src_mode)))
1380 error (0, errno, _("cannot create fifo %s"), quote (dst_path));
1381 goto un_backup;
1384 else
1385 #endif
1386 if (S_ISBLK (src_type) || S_ISCHR (src_type)
1387 #ifdef S_ISSOCK
1388 || S_ISSOCK (src_type)
1389 #endif
1392 if (mknod (dst_path, get_dest_mode (x, src_mode), src_sb.st_rdev))
1394 error (0, errno, _("cannot create special file %s"),
1395 quote (dst_path));
1396 goto un_backup;
1399 else
1400 #ifdef S_ISLNK
1401 if (S_ISLNK (src_type))
1403 char *src_link_val = xreadlink (src_path);
1404 if (src_link_val == NULL)
1406 error (0, errno, _("cannot read symbolic link %s"), quote (src_path));
1407 goto un_backup;
1410 if (!symlink (src_link_val, dst_path))
1411 free (src_link_val);
1412 else
1414 int saved_errno = errno;
1415 int same_link = 0;
1416 if (x->update && !new_dst && S_ISLNK (dst_sb.st_mode))
1418 /* See if the destination is already the desired symlink. */
1419 size_t src_link_len = strlen (src_link_val);
1420 char *dest_link_val = (char *) alloca (src_link_len + 1);
1421 int dest_link_len = readlink (dst_path, dest_link_val,
1422 src_link_len + 1);
1423 if ((size_t) dest_link_len == src_link_len
1424 && strncmp (dest_link_val, src_link_val, src_link_len) == 0)
1425 same_link = 1;
1427 free (src_link_val);
1429 if (! same_link)
1431 error (0, saved_errno, _("cannot create symbolic link %s"),
1432 quote (dst_path));
1433 goto un_backup;
1437 /* There's no need to preserve timestamps or permissions. */
1438 preserve_metadata = 0;
1440 if (x->preserve_ownership)
1442 /* Preserve the owner and group of the just-`copied'
1443 symbolic link, if possible. */
1444 # if HAVE_LCHOWN
1445 if (DO_CHOWN (lchown, dst_path, src_sb.st_uid, src_sb.st_gid))
1447 error (0, errno, _("failed to preserve ownership for %s"),
1448 dst_path);
1449 goto un_backup;
1451 # else
1452 /* Can't preserve ownership of symlinks.
1453 FIXME: maybe give a warning or even error for symlinks
1454 in directories with the sticky bit set -- there, not
1455 preserving owner/group is a potential security problem. */
1456 # endif
1459 else
1460 #endif
1462 error (0, 0, _("%s has unknown file type"), quote (src_path));
1463 goto un_backup;
1466 if (command_line_arg)
1467 record_file (x->dest_info, dst_path, NULL);
1469 if ( ! preserve_metadata)
1470 return 0;
1472 /* POSIX says that `cp -p' must restore the following:
1473 - permission bits
1474 - setuid, setgid bits
1475 - owner and group
1476 If it fails to restore any of those, we may give a warning but
1477 the destination must not be removed.
1478 FIXME: implement the above. */
1480 /* Adjust the times (and if possible, ownership) for the copy.
1481 chown turns off set[ug]id bits for non-root,
1482 so do the chmod last. */
1484 if (x->preserve_timestamps)
1486 struct utimbuf utb;
1488 /* There's currently no interface to set file timestamps with
1489 better than 1-second resolution, so discard any fractional
1490 part of the source timestamp. */
1492 utb.actime = src_sb.st_atime;
1493 utb.modtime = src_sb.st_mtime;
1495 if (utime (dst_path, &utb))
1497 error (0, errno, _("preserving times for %s"), quote (dst_path));
1498 if (x->require_preserve)
1499 return 1;
1503 /* Avoid calling chown if we know it's not necessary. */
1504 if (x->preserve_ownership
1505 && (new_dst || !SAME_OWNER_AND_GROUP (src_sb, dst_sb)))
1507 ran_chown = 1;
1508 if (DO_CHOWN (chown, dst_path, src_sb.st_uid, src_sb.st_gid))
1510 error (0, errno, _("failed to preserve ownership for %s"),
1511 quote (dst_path));
1512 if (x->require_preserve)
1513 return 1;
1517 #if HAVE_STRUCT_STAT_ST_AUTHOR
1518 /* Preserve the st_author field. */
1520 file_t file = file_name_lookup (dst_path, 0, 0);
1521 if (file_chauthor (file, src_sb.st_author))
1522 error (0, errno, _("failed to preserve authorship for %s"),
1523 quote (dst_path));
1524 mach_port_deallocate (mach_task_self (), file);
1526 #endif
1528 /* Permissions of newly-created regular files were set upon `open' in
1529 copy_reg. But don't return early if there were any special bits and
1530 we had to run chown, because the chown must have reset those bits. */
1531 if ((new_dst && copied_as_regular)
1532 && !(ran_chown && (src_mode & ~S_IRWXUGO)))
1533 return delayed_fail;
1535 if ((x->preserve_mode || new_dst)
1536 && (x->copy_as_regular || S_ISREG (src_type) || S_ISDIR (src_type)))
1538 if (chmod (dst_path, get_dest_mode (x, src_mode)))
1540 error (0, errno, _("setting permissions for %s"), quote (dst_path));
1541 if (x->set_mode || x->require_preserve)
1542 return 1;
1546 return delayed_fail;
1548 un_backup:
1550 /* We didn't create the destination.
1551 Remove the entry associating the source dev/ino with the
1552 destination file name, so we don't try to `preserve' a link
1553 to a file we didn't create. */
1554 forget_created (src_sb.st_ino, src_sb.st_dev);
1556 if (dst_backup)
1558 if (rename (dst_backup, dst_path))
1559 error (0, errno, _("cannot un-backup %s"), quote (dst_path));
1560 else
1562 if (x->verbose)
1563 printf (_("%s -> %s (unbackup)\n"),
1564 quote_n (0, dst_backup), quote_n (1, dst_path));
1567 return 1;
1570 static int
1571 valid_options (const struct cp_options *co)
1573 assert (co != NULL);
1575 assert (VALID_BACKUP_TYPE (co->backup_type));
1577 /* FIXME: for some reason this assertion always fails,
1578 at least on Solaris2.5.1. Just disable it for now. */
1579 /* assert (co->xstat == lstat || co->xstat == stat); */
1581 /* Make sure xstat and dereference are consistent. */
1582 /* FIXME */
1584 assert (VALID_SPARSE_MODE (co->sparse_mode));
1586 return 1;
1589 /* Copy the file SRC_PATH to the file DST_PATH. The files may be of
1590 any type. NONEXISTENT_DST should be nonzero if the file DST_PATH
1591 is known not to exist (e.g., because its parent directory was just
1592 created); NONEXISTENT_DST should be zero if DST_PATH might already
1593 exist. OPTIONS is ... FIXME-describe
1594 Set *COPY_INTO_SELF to nonzero if SRC_PATH is a parent of (or the
1595 same as) DST_PATH; otherwise, set it to zero.
1596 Return 0 if successful, 1 if an error occurs. */
1599 copy (const char *src_path, const char *dst_path,
1600 int nonexistent_dst, const struct cp_options *options,
1601 int *copy_into_self, int *rename_succeeded)
1603 assert (valid_options (options));
1605 /* Record the file names: they're used in case of error, when copying
1606 a directory into itself. I don't like to make these tools do *any*
1607 extra work in the common case when that work is solely to handle
1608 exceptional cases, but in this case, I don't see a way to derive the
1609 top level source and destination directory names where they're used.
1610 An alternative is to use COPY_INTO_SELF and print the diagnostic
1611 from every caller -- but I don't want to do that. */
1612 top_level_src_path = src_path;
1613 top_level_dst_path = dst_path;
1615 return copy_internal (src_path, dst_path, nonexistent_dst, 0, NULL,
1616 options, 1, copy_into_self, rename_succeeded);