1 /* cp.c -- file copying (main routines)
2 Copyright (C) 1989, 1990, 1991, 1995 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 Written by Torbjorn Granlund, David MacKenzie, and Jim Meyering. */
28 #include "backupfile.h"
31 #ifndef _POSIX_VERSION
35 /* Used by do_copy, make_path_private, and re_protect
36 to keep a list of leading directories whose protections
37 need to be fixed after copying. */
42 struct dir_attr
*next
;
50 enum backup_type
get_version ();
54 static int do_copy
__P ((int argc
, char **argv
));
55 static int copy
__P ((char *src_path
, char *dst_path
, int new_dst
,
56 dev_t device
, struct dir_list
*ancestors
));
57 static int copy_dir
__P ((char *src_path_in
, char *dst_path_in
, int new_dst
,
58 struct stat
*src_sb
, struct dir_list
*ancestors
));
59 static int make_path_private
__P ((char *const_dirpath
, int src_offset
,
60 int mode
, char *verbose_fmt_string
,
61 struct dir_attr
**attr_list
, int *new_dst
));
62 static int copy_reg
__P ((char *src_path
, char *dst_path
));
63 static int re_protect
__P ((char *const_dst_path
, int src_offset
,
64 struct dir_attr
*attr_list
));
66 /* Initial number of entries in each hash table entry's table of inodes. */
67 #define INITIAL_HASH_MODULE 100
69 /* Initial number of entries in the inode hash table. */
70 #define INITIAL_ENTRY_TAB_SIZE 70
72 /* The invocation name of this program. */
75 /* A pointer to either lstat or stat, depending on
76 whether dereferencing of symlinks is done. */
77 static int (*xstat
) ();
79 /* If nonzero, copy all files except (directories and, if not dereferencing
80 them, symbolic links,) as if they were regular files. */
81 static int flag_copy_as_regular
= 1;
83 /* If nonzero, dereference symbolic links (copy the files they point to). */
84 static int flag_dereference
= 1;
86 /* If nonzero, remove existing destination nondirectories. */
87 static int flag_force
= 0;
89 /* If nonzero, create hard links instead of copying files.
90 Create destination directories as usual. */
91 static int flag_hard_link
= 0;
93 /* If nonzero, query before overwriting existing destinations
94 with regular files. */
95 static int flag_interactive
= 0;
97 /* If nonzero, the command "cp x/e_file e_dir" uses "e_dir/x/e_file"
98 as its destination instead of the usual "e_dir/e_file." */
99 static int flag_path
= 0;
101 /* If nonzero, give the copies the original files' permissions,
102 ownership, and timestamps. */
103 static int flag_preserve
= 0;
105 /* If nonzero, copy directories recursively and copy special files
106 as themselves rather than copying their contents. */
107 static int flag_recursive
= 0;
109 /* If nonzero, create symbolic links instead of copying files.
110 Create destination directories as usual. */
111 static int flag_symbolic_link
= 0;
113 /* If nonzero, when copying recursively, skip any subdirectories that are
114 on different filesystems from the one we started on. */
115 static int flag_one_file_system
= 0;
117 /* If nonzero, do not copy a nondirectory that has an existing destination
118 with the same or newer modification time. */
119 static int flag_update
= 0;
121 /* If nonzero, display the names of the files before copying them. */
122 static int flag_verbose
= 0;
124 /* The error code to return to the system. */
125 static int exit_status
= 0;
127 /* The bits to preserve in created files' modes. */
128 static int umask_kill
;
130 /* This process's effective user ID. */
133 /* If nonzero, display usage information and exit. */
134 static int show_help
;
136 /* If nonzero, print the version on standard output and exit. */
137 static int show_version
;
139 static struct option
const long_opts
[] =
141 {"archive", no_argument
, NULL
, 'a'},
142 {"backup", no_argument
, NULL
, 'b'},
143 {"force", no_argument
, NULL
, 'f'},
144 {"interactive", no_argument
, NULL
, 'i'},
145 {"link", no_argument
, NULL
, 'l'},
146 {"no-dereference", no_argument
, &flag_dereference
, 0},
147 {"one-file-system", no_argument
, &flag_one_file_system
, 1},
148 {"parents", no_argument
, &flag_path
, 1},
149 {"path", no_argument
, &flag_path
, 1},
150 {"preserve", no_argument
, &flag_preserve
, 1},
151 {"recursive", no_argument
, NULL
, 'R'},
152 {"suffix", required_argument
, NULL
, 'S'},
153 {"symbolic-link", no_argument
, NULL
, 's'},
154 {"update", no_argument
, &flag_update
, 1},
155 {"verbose", no_argument
, &flag_verbose
, 1},
156 {"version-control", required_argument
, NULL
, 'V'},
157 {"help", no_argument
, &show_help
, 1},
158 {"version", no_argument
, &show_version
, 1},
163 main (int argc
, char **argv
)
166 int make_backups
= 0;
169 program_name
= argv
[0];
172 version
= getenv ("SIMPLE_BACKUP_SUFFIX");
174 simple_backup_suffix
= version
;
175 version
= getenv ("VERSION_CONTROL");
177 /* Find out the current file creation mask, to knock the right bits
178 when using chmod. The creation mask is set to to be liberal, so
179 that created directories can be written, even if it would not
180 have been allowed with the mask this process was started with. */
182 umask_kill
= 0777777 ^ umask (0);
184 while ((c
= getopt_long (argc
, argv
, "abdfilprsuvxPRS:V:", long_opts
,
192 case 'a': /* Like -dpR. */
193 flag_dereference
= 0;
196 flag_copy_as_regular
= 0;
204 flag_dereference
= 0;
209 flag_interactive
= 0;
214 flag_interactive
= 1;
231 flag_copy_as_regular
= 1;
236 flag_copy_as_regular
= 0;
241 flag_symbolic_link
= 1;
243 error (1, 0, _("symbolic links are not supported on this system"));
256 flag_one_file_system
= 1;
260 simple_backup_suffix
= optarg
;
268 usage (2, (char *) 0);
274 printf ("cp - %s\n", version_string
);
281 if (flag_hard_link
&& flag_symbolic_link
)
282 usage (2, _("cannot make both hard and symbolic links"));
285 backup_type
= get_version (version
);
287 if (flag_preserve
== 1)
288 umask_kill
= 0777777;
290 /* The key difference between -d (--no-dereference) and not is the version
291 of `stat' to call. */
293 if (flag_dereference
)
298 /* Allocate space for remembering copied and created files. */
300 hash_init (INITIAL_HASH_MODULE
, INITIAL_ENTRY_TAB_SIZE
);
302 exit_status
|= do_copy (argc
, argv
);
307 /* Scan the arguments, and copy each by calling copy.
308 Return 0 if successful, 1 if any errors occur. */
311 do_copy (int argc
, char **argv
)
319 usage (2, _("missing file arguments"));
320 if (optind
>= argc
- 1)
321 usage (2, _("missing file argument"));
323 dest
= argv
[argc
- 1];
325 if (lstat (dest
, &sb
))
329 error (0, errno
, "%s", dest
);
339 /* If `dest' is not a symlink to a nonexistent file, use
340 the results of stat instead of lstat, so we can copy files
341 into symlinks to directories. */
342 if (stat (dest
, &sbx
) == 0)
346 if (!new_dst
&& S_ISDIR (sb
.st_mode
))
348 /* cp file1...filen edir
349 Copy the files `file1' through `filen'
350 to the existing directory `edir'. */
357 int parent_exists
= 1; /* True if dirname (dst_path) exists. */
358 struct dir_attr
*attr_list
;
362 strip_trailing_slashes (arg
);
366 /* Append all of `arg' to `dest'. */
367 dst_path
= xmalloc (strlen (dest
) + strlen (arg
) + 2);
368 stpcpy (stpcpy (stpcpy (dst_path
, dest
), "/"), arg
);
370 /* For --parents, we have to make sure that the directory
371 dirname (dst_path) exists. We may have to create a few
372 leading directories. */
373 parent_exists
= !make_path_private (dst_path
,
374 strlen (dest
) + 1, 0700,
375 flag_verbose
? "%s -> %s\n" :
377 &attr_list
, &new_dst
);
381 /* Append the last component of `arg' to `dest'. */
384 /* For `cp -R source/.. dest', don't copy into `dest/..'. */
385 if (!strcmp (ap
, ".."))
386 dst_path
= xstrdup (dest
);
389 dst_path
= xmalloc (strlen (dest
) + strlen (ap
) + 2);
390 stpcpy (stpcpy (stpcpy (dst_path
, dest
), "/"), ap
);
396 /* make_path_private failed, so we shouldn't even attempt the copy. */
401 ret
|= copy (arg
, dst_path
, new_dst
, 0, (struct dir_list
*) 0);
406 ret
|= re_protect (dst_path
, strlen (dest
) + 1,
413 if (optind
== argc
- 1)
418 else if (argc
- optind
== 2)
422 struct stat source_stats
;
425 usage (2, _("when preserving paths, last argument must be a directory"));
427 source
= argv
[optind
];
429 /* When the destination is specified with a trailing slash and the
430 source exists but is not a directory, convert the user's command
431 `cp source dest/' to `cp source dest/basename(source)'. */
433 if (dest
[strlen (dest
) - 1] == '/'
434 && lstat (source
, &source_stats
) == 0
435 && !S_ISDIR (source_stats
.st_mode
))
440 tmp_source
= (char *) alloca (strlen (source
) + 1);
441 strcpy (tmp_source
, source
);
442 strip_trailing_slashes (tmp_source
);
443 source_base
= basename (tmp_source
);
445 new_dest
= (char *) alloca (strlen (dest
) + 1 +
446 strlen (source_base
) + 1);
447 stpcpy (stpcpy (stpcpy (new_dest
, dest
), "/"), source_base
);
454 return copy (source
, new_dest
, new_dst
, 0, (struct dir_list
*) 0);
458 _("when copying multiple files, last argument must be a directory"));
461 /* Copy the file SRC_PATH to the file DST_PATH. The files may be of
462 any type. NEW_DST should be nonzero if the file DST_PATH cannot
463 exist because its parent directory was just created; NEW_DST should
464 be zero if DST_PATH might already exist. DEVICE is the device
465 number of the parent directory, or 0 if the parent of this file is
466 not known. ANCESTORS points to a linked, null terminated list of
467 devices and inodes of parent directories of SRC_PATH.
468 Return 0 if successful, 1 if an error occurs. */
471 copy (char *src_path
, char *dst_path
, int new_dst
, dev_t device
,
472 struct dir_list
*ancestors
)
479 char *dst_backup
= NULL
;
482 if ((*xstat
) (src_path
, &src_sb
))
484 error (0, errno
, "%s", src_path
);
488 /* Are we crossing a file system boundary? */
489 if (flag_one_file_system
&& device
!= 0 && device
!= src_sb
.st_dev
)
492 /* We wouldn't insert a node unless nlink > 1, except that we need to
493 find created files so as to not copy infinitely if a directory is
494 copied into itself. */
496 earlier_file
= remember_copied (dst_path
, src_sb
.st_ino
, src_sb
.st_dev
);
498 /* Did we just create this file? */
500 if (earlier_file
== &new_file
)
503 src_mode
= src_sb
.st_mode
;
504 src_type
= src_sb
.st_mode
;
506 if (S_ISDIR (src_type
) && !flag_recursive
)
508 error (0, 0, _("%s: omitting directory"), src_path
);
514 if ((*xstat
) (dst_path
, &dst_sb
))
518 error (0, errno
, "%s", dst_path
);
526 /* The file exists already. */
528 if (src_sb
.st_ino
== dst_sb
.st_ino
&& src_sb
.st_dev
== dst_sb
.st_dev
)
533 error (0, 0, _("`%s' and `%s' are the same file"),
538 if (!S_ISDIR (src_type
))
540 if (S_ISDIR (dst_sb
.st_mode
))
543 _("%s: cannot overwrite directory with non-directory"),
548 if (flag_update
&& src_sb
.st_mtime
<= dst_sb
.st_mtime
)
552 if (S_ISREG (src_type
) && !flag_force
)
554 if (flag_interactive
)
556 if (euidaccess (dst_path
, W_OK
) != 0)
558 _("%s: overwrite `%s', overriding mode %04o? "),
559 program_name
, dst_path
,
560 (unsigned int) (dst_sb
.st_mode
& 07777));
562 fprintf (stderr
, _("%s: overwrite `%s'? "),
563 program_name
, dst_path
);
569 if (backup_type
!= none
&& !S_ISDIR (dst_sb
.st_mode
))
571 char *tmp_backup
= find_backup_file_name (dst_path
);
572 if (tmp_backup
== NULL
)
573 error (1, 0, _("virtual memory exhausted"));
574 dst_backup
= (char *) alloca (strlen (tmp_backup
) + 1);
575 strcpy (dst_backup
, tmp_backup
);
577 if (rename (dst_path
, dst_backup
))
581 error (0, errno
, _("cannot backup `%s'"), dst_path
);
591 if (S_ISDIR (dst_sb
.st_mode
))
593 /* Temporarily change mode to allow overwriting. */
594 if (euidaccess (dst_path
, W_OK
| X_OK
) != 0)
596 if (chmod (dst_path
, 0700))
598 error (0, errno
, "%s", dst_path
);
607 if (unlink (dst_path
) && errno
!= ENOENT
)
609 error (0, errno
, _("cannot remove old link to `%s'"),
619 /* If the source is a directory, we don't always create the destination
620 directory. So --verbose should not announce anything until we're
621 sure we'll create a directory. */
622 if (flag_verbose
&& !S_ISDIR (src_type
))
623 printf ("%s -> %s\n", src_path
, dst_path
);
625 /* Did we copy this inode somewhere else (in this command line argument)
626 and therefore this is a second hard link to the inode? */
628 if (!flag_dereference
&& src_sb
.st_nlink
> 1 && earlier_file
)
630 if (link (earlier_file
, dst_path
))
632 error (0, errno
, "%s", dst_path
);
638 if (S_ISDIR (src_type
))
640 struct dir_list
*dir
;
642 /* If this directory has been copied before during the
643 recursion, there is a symbolic link to an ancestor
644 directory of the symbolic link. It is impossible to
645 continue to copy this, unless we've got an infinite disk. */
647 if (is_ancestor (&src_sb
, ancestors
))
649 error (0, 0, _("%s: cannot copy cyclic symbolic link"), src_path
);
653 /* Insert the current directory in the list of parents. */
655 dir
= (struct dir_list
*) alloca (sizeof (struct dir_list
));
656 dir
->parent
= ancestors
;
657 dir
->ino
= src_sb
.st_ino
;
658 dir
->dev
= src_sb
.st_dev
;
660 if (new_dst
|| !S_ISDIR (dst_sb
.st_mode
))
662 /* Create the new directory writable and searchable, so
663 we can create new entries in it. */
665 if (mkdir (dst_path
, (src_mode
& umask_kill
) | 0700))
667 error (0, errno
, _("cannot create directory `%s'"), dst_path
);
671 /* Insert the created directory's inode and device
672 numbers into the search structure, so that we can
673 avoid copying it again. */
675 if (remember_created (dst_path
))
679 printf ("%s -> %s\n", src_path
, dst_path
);
682 /* Copy the contents of the directory. */
684 if (copy_dir (src_path
, dst_path
, new_dst
, &src_sb
, dir
))
688 else if (flag_symbolic_link
)
691 || (!strncmp (dst_path
, "./", 2) && strchr (dst_path
+ 2, '/') == 0)
692 || strchr (dst_path
, '/') == 0)
694 if (symlink (src_path
, dst_path
))
696 error (0, errno
, "%s", dst_path
);
704 _("%s: can only make relative symbolic links in current directory"), dst_path
);
709 else if (flag_hard_link
)
711 if (link (src_path
, dst_path
))
713 error (0, errno
, _("cannot create link `%s'"), dst_path
);
718 else if (S_ISREG (src_type
)
719 || (flag_copy_as_regular
&& !S_ISDIR (src_type
)
721 && !S_ISLNK (src_type
)
725 if (copy_reg (src_path
, dst_path
))
730 if (S_ISFIFO (src_type
))
732 if (mkfifo (dst_path
, src_mode
& umask_kill
))
734 error (0, errno
, _("cannot create fifo `%s'"), dst_path
);
740 if (S_ISBLK (src_type
) || S_ISCHR (src_type
)
742 || S_ISSOCK (src_type
)
746 if (mknod (dst_path
, src_mode
& umask_kill
, src_sb
.st_rdev
))
748 error (0, errno
, _("cannot create special file `%s'"), dst_path
);
754 if (S_ISLNK (src_type
))
759 link_val
= (char *) alloca (PATH_MAX
+ 2);
760 link_size
= readlink (src_path
, link_val
, PATH_MAX
+ 1);
763 error (0, errno
, _("cannot read symbolic link `%s'"), src_path
);
766 link_val
[link_size
] = '\0';
768 if (symlink (link_val
, dst_path
))
770 error (0, errno
, _("cannot create symbolic link `%s'"), dst_path
);
778 error (0, 0, _("%s: unknown file type"), src_path
);
782 /* Adjust the times (and if possible, ownership) for the copy.
783 chown turns off set[ug]id bits for non-root,
784 so do the chmod last. */
790 utb
.actime
= src_sb
.st_atime
;
791 utb
.modtime
= src_sb
.st_mtime
;
793 if (utime (dst_path
, &utb
))
795 error (0, errno
, "%s", dst_path
);
799 /* If non-root uses -p, it's ok if we can't preserve ownership.
800 But root probably wants to know, e.g. if NFS disallows it. */
802 (myeuid
== 0 ? src_sb
.st_uid
: myeuid
),
804 && (errno
!= EPERM
|| myeuid
== 0))
806 error (0, errno
, "%s", dst_path
);
811 if ((flag_preserve
|| new_dst
)
812 && (flag_copy_as_regular
|| S_ISREG (src_type
) || S_ISDIR (src_type
)))
814 if (chmod (dst_path
, src_mode
& umask_kill
))
816 error (0, errno
, "%s", dst_path
);
822 /* Reset the temporarily changed mode. */
823 if (chmod (dst_path
, dst_sb
.st_mode
))
825 error (0, errno
, "%s", dst_path
);
835 if (rename (dst_backup
, dst_path
))
836 error (0, errno
, _("cannot un-backup `%s'"), dst_path
);
841 /* Ensure that the parent directory of CONST_DIRPATH exists, for
842 the --parents option.
844 SRC_OFFSET is the index in CONST_DIRPATH (which is a destination
845 path) of the beginning of the source directory name.
846 Create any leading directories that don't already exist,
847 giving them permissions MODE.
848 If VERBOSE_FMT_STRING is nonzero, use it as a printf format
849 string for printing a message after successfully making a directory.
850 The format should take two string arguments: the names of the
851 source and destination directories.
852 Creates a linked list of attributes of intermediate directories,
853 *ATTR_LIST, for re_protect to use after calling copy.
854 Sets *NEW_DST to 1 if this function creates parent of CONST_DIRPATH.
856 Return 0 if parent of CONST_DIRPATH exists as a directory with the proper
857 permissions when done, otherwise 1. */
860 make_path_private (char *const_dirpath
, int src_offset
, int mode
,
861 char *verbose_fmt_string
, struct dir_attr
**attr_list
,
865 char *dirpath
; /* A copy of CONST_DIRPATH we can change. */
866 char *src
; /* Source name in `dirpath'. */
867 char *tmp_dst_dirname
; /* Leading path of `dirpath', malloc. */
868 char *dst_dirname
; /* Leading path of `dirpath', alloca. */
870 dirpath
= (char *) alloca (strlen (const_dirpath
) + 1);
871 strcpy (dirpath
, const_dirpath
);
873 src
= dirpath
+ src_offset
;
875 tmp_dst_dirname
= dirname (dirpath
);
876 dst_dirname
= (char *) alloca (strlen (tmp_dst_dirname
) + 1);
877 strcpy (dst_dirname
, tmp_dst_dirname
);
878 free (tmp_dst_dirname
);
882 if ((*xstat
) (dst_dirname
, &stats
))
884 /* Parent of CONST_DIRNAME does not exist.
885 Make all missing intermediate directories. */
889 while (*slash
== '/')
891 while ((slash
= strchr (slash
, '/')))
893 /* Add this directory to the list of directories whose modes need
895 struct dir_attr
*new =
896 (struct dir_attr
*) xmalloc (sizeof (struct dir_attr
));
897 new->slash_offset
= slash
- dirpath
;
898 new->next
= *attr_list
;
902 if ((*xstat
) (dirpath
, &stats
))
904 /* This element of the path does not exist. We must set
905 *new_dst and new->is_new_dir inside this loop because,
906 for example, in the command `cp --parents ../a/../b/c e_dir',
907 make_path_private creates only e_dir/../a if ./b already
911 if (mkdir (dirpath
, mode
))
913 error (0, errno
, _("cannot make directory `%s'"), dirpath
);
918 if (verbose_fmt_string
!= NULL
)
919 printf (verbose_fmt_string
, src
, dirpath
);
922 else if (!S_ISDIR (stats
.st_mode
))
924 error (0, 0, _("`%s' exists but is not a directory"), dirpath
);
934 /* Avoid unnecessary calls to `stat' when given
935 pathnames containing multiple adjacent slashes. */
936 while (*slash
== '/')
941 /* We get here if the parent of `dirpath' already exists. */
943 else if (!S_ISDIR (stats
.st_mode
))
945 error (0, 0, _("`%s' exists but is not a directory"), dst_dirname
);
955 /* Ensure that the parent directories of CONST_DST_PATH have the
956 correct protections, for the --parents option. This is done
957 after all copying has been completed, to allow permissions
958 that don't include user write/execute.
960 SRC_OFFSET is the index in CONST_DST_PATH of the beginning of the
961 source directory name.
963 ATTR_LIST is a null-terminated linked list of structures that
964 indicates the end of the filename of each intermediate directory
965 in CONST_DST_PATH that may need to have its attributes changed.
966 The command `cp --parents --preserve a/b/c d/e_dir' changes the
967 attributes of the directories d/e_dir/a and d/e_dir/a/b to match
968 the corresponding source directories regardless of whether they
969 existed before the `cp' command was given.
971 Return 0 if the parent of CONST_DST_PATH and any intermediate
972 directories specified by ATTR_LIST have the proper permissions
973 when done, otherwise 1. */
976 re_protect (char *const_dst_path
, int src_offset
, struct dir_attr
*attr_list
)
979 char *dst_path
; /* A copy of CONST_DST_PATH we can change. */
980 char *src_path
; /* The source name in `dst_path'. */
982 dst_path
= (char *) alloca (strlen (const_dst_path
) + 1);
983 strcpy (dst_path
, const_dst_path
);
984 src_path
= dst_path
+ src_offset
;
986 for (p
= attr_list
; p
; p
= p
->next
)
990 dst_path
[p
->slash_offset
] = '\0';
992 if ((*xstat
) (src_path
, &src_sb
))
994 error (0, errno
, "%s", src_path
);
998 /* Adjust the times (and if possible, ownership) for the copy.
999 chown turns off set[ug]id bits for non-root,
1000 so do the chmod last. */
1006 utb
.actime
= src_sb
.st_atime
;
1007 utb
.modtime
= src_sb
.st_mtime
;
1009 if (utime (dst_path
, &utb
))
1011 error (0, errno
, "%s", dst_path
);
1015 /* If non-root uses -p, it's ok if we can't preserve ownership.
1016 But root probably wants to know, e.g. if NFS disallows it. */
1017 if (chown (dst_path
, src_sb
.st_uid
, src_sb
.st_gid
)
1018 && (errno
!= EPERM
|| myeuid
== 0))
1020 error (0, errno
, "%s", dst_path
);
1025 if (flag_preserve
|| p
->is_new_dir
)
1027 if (chmod (dst_path
, src_sb
.st_mode
& umask_kill
))
1029 error (0, errno
, "%s", dst_path
);
1034 dst_path
[p
->slash_offset
] = '/';
1039 /* Read the contents of the directory SRC_PATH_IN, and recursively
1040 copy the contents to DST_PATH_IN. NEW_DST is nonzero if
1041 DST_PATH_IN is a directory that was created previously in the
1042 recursion. SRC_SB and ANCESTORS describe SRC_PATH_IN.
1043 Return 0 if successful, -1 if an error occurs. */
1046 copy_dir (char *src_path_in
, char *dst_path_in
, int new_dst
,
1047 struct stat
*src_sb
, struct dir_list
*ancestors
)
1056 name_space
= savedir (src_path_in
, src_sb
->st_size
);
1057 if (name_space
== 0)
1061 error (0, errno
, "%s", src_path_in
);
1065 error (1, 0, _("virtual memory exhausted"));
1069 while (*namep
!= '\0')
1071 int fn_length
= strlen (namep
) + 1;
1073 dst_path
= xmalloc (strlen (dst_path_in
) + fn_length
+ 1);
1074 src_path
= xmalloc (strlen (src_path_in
) + fn_length
+ 1);
1076 stpcpy (stpcpy (stpcpy (src_path
, src_path_in
), "/"), namep
);
1077 stpcpy (stpcpy (stpcpy (dst_path
, dst_path_in
), "/"), namep
);
1079 ret
|= copy (src_path
, dst_path
, new_dst
, src_sb
->st_dev
, ancestors
);
1081 /* Free the memory for `src_path'. The memory for `dst_path'
1082 cannot be deallocated, since it is used to create multiple
1093 /* Copy a regular file from SRC_PATH to DST_PATH.
1094 If the source file contains holes, copies holes and blocks of zeros
1095 in the source file as holes in the destination file.
1096 (Holes are read as zeroes by the `read' system call.)
1097 Return 0 if successful, -1 if an error occurred. */
1100 copy_reg (char *src_path
, char *dst_path
)
1111 long n_read_total
= 0;
1112 int last_write_made_hole
= 0;
1115 source_desc
= open (src_path
, O_RDONLY
);
1116 if (source_desc
< 0)
1118 error (0, errno
, "%s", src_path
);
1122 /* Create the new regular file with small permissions initially,
1123 to not create a security hole. */
1125 dest_desc
= open (dst_path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
1128 error (0, errno
, _("cannot create regular file `%s'"), dst_path
);
1133 /* Find out the optimal buffer size. */
1135 if (fstat (dest_desc
, &sb
))
1137 error (0, errno
, "%s", dst_path
);
1142 buf_size
= ST_BLKSIZE (sb
);
1144 #ifdef HAVE_ST_BLOCKS
1145 if (S_ISREG (sb
.st_mode
))
1147 /* Find out whether the file contains any sparse blocks. */
1149 if (fstat (source_desc
, &sb
))
1151 error (0, errno
, "%s", src_path
);
1156 /* If the file has fewer blocks than would normally
1157 be needed for a file of its size, then
1158 at least one of the blocks in the file is a hole. */
1159 if (S_ISREG (sb
.st_mode
) && sb
.st_size
> sb
.st_blocks
* DEV_BSIZE
)
1164 /* Make a buffer with space for a sentinel at the end. */
1166 buf
= (char *) alloca (buf_size
+ sizeof (int));
1170 n_read
= read (source_desc
, buf
, buf_size
);
1177 error (0, errno
, "%s", src_path
);
1184 n_read_total
+= n_read
;
1189 buf
[n_read
] = 1; /* Sentinel to stop loop. */
1191 /* Find first nonzero *word*, or the word with the sentinel. */
1197 /* Find the first nonzero *byte*, or the sentinel. */
1199 cp
= (char *) (ip
- 1);
1203 /* If we found the sentinel, the whole input block was zero,
1204 and we can make a hole. */
1206 if (cp
> buf
+ n_read
)
1209 if (lseek (dest_desc
, (off_t
) n_read
, SEEK_CUR
) < 0L)
1211 error (0, errno
, "%s", dst_path
);
1215 last_write_made_hole
= 1;
1218 /* Clear to indicate that a normal write is needed. */
1223 if (full_write (dest_desc
, buf
, n_read
) < 0)
1225 error (0, errno
, "%s", dst_path
);
1229 last_write_made_hole
= 0;
1233 /* If the file ends with a `hole', something needs to be written at
1234 the end. Otherwise the kernel would truncate the file at the end
1235 of the last write operation. */
1237 if (last_write_made_hole
)
1239 #ifdef HAVE_FTRUNCATE
1240 /* Write a null character and truncate it again. */
1241 if (full_write (dest_desc
, "", 1) < 0
1242 || ftruncate (dest_desc
, n_read_total
) < 0)
1244 /* Seek backwards one character and write a null. */
1245 if (lseek (dest_desc
, (off_t
) -1, SEEK_CUR
) < 0L
1246 || full_write (dest_desc
, "", 1) < 0)
1249 error (0, errno
, "%s", dst_path
);
1255 if (close (dest_desc
) < 0)
1257 error (0, errno
, "%s", dst_path
);
1261 if (close (source_desc
) < 0)
1263 error (0, errno
, "%s", src_path
);