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;
47 enum backup_type get_version ();
51 static int do_copy ();
53 static int copy_dir ();
54 static int make_path_private ();
55 static int copy_reg ();
56 static int re_protect ();
58 /* Initial number of entries in each hash table entry's table of inodes. */
59 #define INITIAL_HASH_MODULE 100
61 /* Initial number of entries in the inode hash table. */
62 #define INITIAL_ENTRY_TAB_SIZE 70
64 /* The invocation name of this program. */
67 /* A pointer to either lstat or stat, depending on
68 whether dereferencing of symlinks is done. */
69 static int (*xstat) ();
71 /* If nonzero, copy all files except (directories and, if not dereferencing
72 them, symbolic links,) as if they were regular files. */
73 static int flag_copy_as_regular = 1;
75 /* If nonzero, dereference symbolic links (copy the files they point to). */
76 static int flag_dereference = 1;
78 /* If nonzero, remove existing destination nondirectories. */
79 static int flag_force = 0;
81 /* If nonzero, create hard links instead of copying files.
82 Create destination directories as usual. */
83 static int flag_hard_link = 0;
85 /* If nonzero, query before overwriting existing destinations
86 with regular files. */
87 static int flag_interactive = 0;
89 /* If nonzero, the command "cp x/e_file e_dir" uses "e_dir/x/e_file"
90 as its destination instead of the usual "e_dir/e_file." */
91 static int flag_path = 0;
93 /* If nonzero, give the copies the original files' permissions,
94 ownership, and timestamps. */
95 static int flag_preserve = 0;
97 /* If nonzero, copy directories recursively and copy special files
98 as themselves rather than copying their contents. */
99 static int flag_recursive = 0;
101 /* If nonzero, create symbolic links instead of copying files.
102 Create destination directories as usual. */
103 static int flag_symbolic_link = 0;
105 /* If nonzero, when copying recursively, skip any subdirectories that are
106 on different filesystems from the one we started on. */
107 static int flag_one_file_system = 0;
109 /* If nonzero, do not copy a nondirectory that has an existing destination
110 with the same or newer modification time. */
111 static int flag_update = 0;
113 /* If nonzero, display the names of the files before copying them. */
114 static int flag_verbose = 0;
116 /* The error code to return to the system. */
117 static int exit_status = 0;
119 /* The bits to preserve in created files' modes. */
120 static int umask_kill;
122 /* This process's effective user ID. */
125 /* If non-zero, display usage information and exit. */
126 static int show_help;
128 /* If non-zero, print the version on standard output and exit. */
129 static int show_version;
131 static struct option const long_opts[] =
133 {"archive", no_argument, NULL, 'a'},
134 {"backup", no_argument, NULL, 'b'},
135 {"force", no_argument, NULL, 'f'},
136 {"interactive", no_argument, NULL, 'i'},
137 {"link", no_argument, NULL, 'l'},
138 {"no-dereference", no_argument, &flag_dereference, 0},
139 {"one-file-system", no_argument, &flag_one_file_system, 1},
140 {"parents", no_argument, &flag_path, 1},
141 {"path", no_argument, &flag_path, 1},
142 {"preserve", no_argument, &flag_preserve, 1},
143 {"recursive", no_argument, NULL, 'R'},
144 {"suffix", required_argument, NULL, 'S'},
145 {"symbolic-link", no_argument, NULL, 's'},
146 {"update", no_argument, &flag_update, 1},
147 {"verbose", no_argument, &flag_verbose, 1},
148 {"version-control", required_argument, NULL, 'V'},
149 {"help", no_argument, &show_help, 1},
150 {"version", no_argument, &show_version, 1},
160 int make_backups = 0;
163 program_name = argv[0];
166 version = getenv ("SIMPLE_BACKUP_SUFFIX");
168 simple_backup_suffix = version;
169 version = getenv ("VERSION_CONTROL");
171 /* Find out the current file creation mask, to knock the right bits
172 when using chmod. The creation mask is set to to be liberal, so
173 that created directories can be written, even if it would not
174 have been allowed with the mask this process was started with. */
176 umask_kill = 0777777 ^ umask (0);
178 while ((c = getopt_long (argc, argv, "abdfilprsuvxPRS:V:", long_opts,
186 case 'a': /* Like -dpR. */
187 flag_dereference = 0;
190 flag_copy_as_regular = 0;
198 flag_dereference = 0;
203 flag_interactive = 0;
208 flag_interactive = 1;
225 flag_copy_as_regular = 1;
230 flag_copy_as_regular = 0;
235 flag_symbolic_link = 1;
237 error (1, 0, "symbolic links are not supported on this system");
250 flag_one_file_system = 1;
254 simple_backup_suffix = optarg;
262 usage (2, (char *) 0);
268 printf ("%s\n", version_string);
275 if (flag_hard_link && flag_symbolic_link)
276 usage (2, "cannot make both hard and symbolic links");
279 backup_type = get_version (version);
281 if (flag_preserve == 1)
282 umask_kill = 0777777;
284 /* The key difference between -d (--no-dereference) and not is the version
285 of `stat' to call. */
287 if (flag_dereference)
292 /* Allocate space for remembering copied and created files. */
294 hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
296 exit_status |= do_copy (argc, argv);
301 /* Scan the arguments, and copy each by calling copy.
302 Return 0 if successful, 1 if any errors occur. */
315 usage (2, "missing file arguments");
316 if (optind >= argc - 1)
317 usage (2, "missing file argument");
319 dest = argv[argc - 1];
321 if (lstat (dest, &sb))
325 error (0, errno, "%s", dest);
335 /* If `dest' is not a symlink to a nonexistent file, use
336 the results of stat instead of lstat, so we can copy files
337 into symlinks to directories. */
338 if (stat (dest, &sbx) == 0)
342 if (!new_dst && S_ISDIR (sb.st_mode))
344 /* cp file1...filen edir
345 Copy the files `file1' through `filen'
346 to the existing directory `edir'. */
353 int parent_exists = 1; /* True if dirname (dst_path) exists. */
354 struct dir_attr *attr_list;
358 strip_trailing_slashes (arg);
362 /* Append all of `arg' to `dest'. */
363 dst_path = xmalloc (strlen (dest) + strlen (arg) + 2);
364 stpcpy (stpcpy (stpcpy (dst_path, dest), "/"), arg);
366 /* For --parents, we have to make sure that the directory
367 dirname (dst_path) exists. We may have to create a few
368 leading directories. */
369 parent_exists = !make_path_private (dst_path,
370 strlen (dest) + 1, 0700,
371 flag_verbose ? "%s -> %s\n" :
373 &attr_list, &new_dst);
377 /* Append the last component of `arg' to `dest'. */
380 /* For `cp -R source/.. dest', don't copy into `dest/..'. */
381 if (!strcmp (ap, ".."))
382 dst_path = xstrdup (dest);
385 dst_path = xmalloc (strlen (dest) + strlen (ap) + 2);
386 stpcpy (stpcpy (stpcpy (dst_path, dest), "/"), ap);
392 /* make_path_private failed, so we shouldn't even attempt the copy. */
397 ret |= copy (arg, dst_path, new_dst, 0, (struct dir_list *) 0);
402 ret |= re_protect (dst_path, strlen (dest) + 1,
409 if (optind == argc - 1)
414 else if (argc - optind == 2)
418 struct stat source_stats;
421 usage (2, "when preserving paths, last argument must be a directory");
423 source = argv[optind];
425 /* When the destination is specified with a trailing slash and the
426 source exists but is not a directory, convert the user's command
427 `cp source dest/' to `cp source dest/basename(source)'. */
429 if (dest[strlen (dest) - 1] == '/'
430 && lstat (source, &source_stats) == 0
431 && !S_ISDIR (source_stats.st_mode))
436 tmp_source = (char *) alloca (strlen (source) + 1);
437 strcpy (tmp_source, source);
438 strip_trailing_slashes (tmp_source);
439 source_base = basename (tmp_source);
441 new_dest = (char *) alloca (strlen (dest) + 1 +
442 strlen (source_base) + 1);
443 stpcpy (stpcpy (stpcpy (new_dest, dest), "/"), source_base);
450 return copy (source, new_dest, new_dst, 0, (struct dir_list *) 0);
454 "when copying multiple files, last argument must be a directory");
457 /* Copy the file SRC_PATH to the file DST_PATH. The files may be of
458 any type. NEW_DST should be non-zero if the file DST_PATH cannot
459 exist because its parent directory was just created; NEW_DST should
460 be zero if DST_PATH might already exist. DEVICE is the device
461 number of the parent directory, or 0 if the parent of this file is
462 not known. ANCESTORS points to a linked, null terminated list of
463 devices and inodes of parent directories of SRC_PATH.
464 Return 0 if successful, 1 if an error occurs. */
467 copy (src_path, dst_path, new_dst, device, ancestors)
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 (eaccess_stat (&dst_sb, W_OK, dst_path) != 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 (eaccess_stat (&dst_sb, 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 (const_dirpath, src_offset, mode, verbose_fmt_string,
865 char *verbose_fmt_string;
866 struct dir_attr **attr_list;
870 char *dirpath; /* A copy of CONST_DIRPATH we can change. */
871 char *src; /* Source name in `dirpath'. */
872 char *tmp_dst_dirname; /* Leading path of `dirpath', malloc. */
873 char *dst_dirname; /* Leading path of `dirpath', alloca. */
875 dirpath = (char *) alloca (strlen (const_dirpath) + 1);
876 strcpy (dirpath, const_dirpath);
878 src = dirpath + src_offset;
880 tmp_dst_dirname = dirname (dirpath);
881 dst_dirname = (char *) alloca (strlen (tmp_dst_dirname) + 1);
882 strcpy (dst_dirname, tmp_dst_dirname);
883 free (tmp_dst_dirname);
887 if ((*xstat) (dst_dirname, &stats))
889 /* Parent of CONST_DIRNAME does not exist.
890 Make all missing intermediate directories. */
894 while (*slash == '/')
896 while ((slash = strchr (slash, '/')))
898 /* Add this directory to the list of directories whose modes need
900 struct dir_attr *new =
901 (struct dir_attr *) xmalloc (sizeof (struct dir_attr));
902 new->slash_offset = slash - dirpath;
903 new->next = *attr_list;
907 if ((*xstat) (dirpath, &stats))
909 /* This element of the path does not exist. We must set
910 *new_dst and new->is_new_dir inside this loop because,
911 for example, in the command `cp --parents ../a/../b/c e_dir',
912 make_path_private creates only e_dir/../a if ./b already
916 if (mkdir (dirpath, mode))
918 error (0, errno, "cannot make directory `%s'", dirpath);
923 if (verbose_fmt_string != NULL)
924 printf (verbose_fmt_string, src, dirpath);
927 else if (!S_ISDIR (stats.st_mode))
929 error (0, 0, "`%s' exists but is not a directory", dirpath);
939 /* Avoid unnecessary calls to `stat' when given
940 pathnames containing multiple adjacent slashes. */
941 while (*slash == '/')
946 /* We get here if the parent of `dirpath' already exists. */
948 else if (!S_ISDIR (stats.st_mode))
950 error (0, 0, "`%s' exists but is not a directory", dst_dirname);
960 /* Ensure that the parent directories of CONST_DST_PATH have the
961 correct protections, for the --parents option. This is done
962 after all copying has been completed, to allow permissions
963 that don't include user write/execute.
965 SRC_OFFSET is the index in CONST_DST_PATH of the beginning of the
966 source directory name.
968 ATTR_LIST is a null-terminated linked list of structures that
969 indicates the end of the filename of each intermediate directory
970 in CONST_DST_PATH that may need to have its attributes changed.
971 The command `cp --parents --preserve a/b/c d/e_dir' changes the
972 attributes of the directories d/e_dir/a and d/e_dir/a/b to match
973 the corresponding source directories regardless of whether they
974 existed before the `cp' command was given.
976 Return 0 if the parent of CONST_DST_PATH and any intermediate
977 directories specified by ATTR_LIST have the proper permissions
978 when done, otherwise 1. */
981 re_protect (const_dst_path, src_offset, attr_list)
982 char *const_dst_path;
984 struct dir_attr *attr_list;
987 char *dst_path; /* A copy of CONST_DST_PATH we can change. */
988 char *src_path; /* The source name in `dst_path'. */
990 dst_path = (char *) alloca (strlen (const_dst_path) + 1);
991 strcpy (dst_path, const_dst_path);
992 src_path = dst_path + src_offset;
994 for (p = attr_list; p; p = p->next)
998 dst_path[p->slash_offset] = '\0';
1000 if ((*xstat) (src_path, &src_sb))
1002 error (0, errno, "%s", src_path);
1006 /* Adjust the times (and if possible, ownership) for the copy.
1007 chown turns off set[ug]id bits for non-root,
1008 so do the chmod last. */
1014 utb.actime = src_sb.st_atime;
1015 utb.modtime = src_sb.st_mtime;
1017 if (utime (dst_path, &utb))
1019 error (0, errno, "%s", dst_path);
1023 /* If non-root uses -p, it's ok if we can't preserve ownership.
1024 But root probably wants to know, e.g. if NFS disallows it. */
1025 if (chown (dst_path, src_sb.st_uid, src_sb.st_gid)
1026 && (errno != EPERM || myeuid == 0))
1028 error (0, errno, "%s", dst_path);
1033 if (flag_preserve || p->is_new_dir)
1035 if (chmod (dst_path, src_sb.st_mode & umask_kill))
1037 error (0, errno, "%s", dst_path);
1042 dst_path[p->slash_offset] = '/';
1047 /* Read the contents of the directory SRC_PATH_IN, and recursively
1048 copy the contents to DST_PATH_IN. NEW_DST is non-zero if
1049 DST_PATH_IN is a directory that was created previously in the
1050 recursion. SRC_SB and ANCESTORS describe SRC_PATH_IN.
1051 Return 0 if successful, -1 if an error occurs. */
1054 copy_dir (src_path_in, dst_path_in, new_dst, src_sb, ancestors)
1058 struct stat *src_sb;
1059 struct dir_list *ancestors;
1068 name_space = savedir (src_path_in, src_sb->st_size);
1069 if (name_space == 0)
1073 error (0, errno, "%s", src_path_in);
1077 error (1, 0, "virtual memory exhausted");
1081 while (*namep != '\0')
1083 int fn_length = strlen (namep) + 1;
1085 dst_path = xmalloc (strlen (dst_path_in) + fn_length + 1);
1086 src_path = xmalloc (strlen (src_path_in) + fn_length + 1);
1088 stpcpy (stpcpy (stpcpy (src_path, src_path_in), "/"), namep);
1089 stpcpy (stpcpy (stpcpy (dst_path, dst_path_in), "/"), namep);
1091 ret |= copy (src_path, dst_path, new_dst, src_sb->st_dev, ancestors);
1093 /* Free the memory for `src_path'. The memory for `dst_path'
1094 cannot be deallocated, since it is used to create multiple
1105 /* Copy a regular file from SRC_PATH to DST_PATH.
1106 If the source file contains holes, copies holes and blocks of zeros
1107 in the source file as holes in the destination file.
1108 (Holes are read as zeroes by the `read' system call.)
1109 Return 0 if successful, -1 if an error occurred. */
1112 copy_reg (src_path, dst_path)
1125 long n_read_total = 0;
1126 int last_write_made_hole = 0;
1129 source_desc = open (src_path, O_RDONLY);
1130 if (source_desc < 0)
1132 error (0, errno, "%s", src_path);
1136 /* Create the new regular file with small permissions initially,
1137 to not create a security hole. */
1139 dest_desc = open (dst_path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1142 error (0, errno, "cannot create regular file `%s'", dst_path);
1147 /* Find out the optimal buffer size. */
1149 if (fstat (dest_desc, &sb))
1151 error (0, errno, "%s", dst_path);
1156 buf_size = ST_BLKSIZE (sb);
1158 #ifdef HAVE_ST_BLOCKS
1159 if (S_ISREG (sb.st_mode))
1161 /* Find out whether the file contains any sparse blocks. */
1163 if (fstat (source_desc, &sb))
1165 error (0, errno, "%s", src_path);
1170 /* If the file has fewer blocks than would normally
1171 be needed for a file of its size, then
1172 at least one of the blocks in the file is a hole. */
1173 if (S_ISREG (sb.st_mode) && sb.st_size > sb.st_blocks * DEV_BSIZE)
1178 /* Make a buffer with space for a sentinel at the end. */
1180 buf = (char *) alloca (buf_size + sizeof (int));
1184 n_read = read (source_desc, buf, buf_size);
1191 error (0, errno, "%s", src_path);
1198 n_read_total += n_read;
1203 buf[n_read] = 1; /* Sentinel to stop loop. */
1205 /* Find first non-zero *word*, or the word with the sentinel. */
1211 /* Find the first non-zero *byte*, or the sentinel. */
1213 cp = (char *) (ip - 1);
1217 /* If we found the sentinel, the whole input block was zero,
1218 and we can make a hole. */
1220 if (cp > buf + n_read)
1223 if (lseek (dest_desc, (off_t) n_read, SEEK_CUR) < 0L)
1225 error (0, errno, "%s", dst_path);
1229 last_write_made_hole = 1;
1232 /* Clear to indicate that a normal write is needed. */
1237 if (full_write (dest_desc, buf, n_read) < 0)
1239 error (0, errno, "%s", dst_path);
1243 last_write_made_hole = 0;
1247 /* If the file ends with a `hole', something needs to be written at
1248 the end. Otherwise the kernel would truncate the file at the end
1249 of the last write operation. */
1251 if (last_write_made_hole)
1253 #ifdef HAVE_FTRUNCATE
1254 /* Write a null character and truncate it again. */
1255 if (full_write (dest_desc, "", 1) < 0
1256 || ftruncate (dest_desc, n_read_total) < 0)
1258 /* Seek backwards one character and write a null. */
1259 if (lseek (dest_desc, (off_t) -1, SEEK_CUR) < 0L
1260 || full_write (dest_desc, "", 1) < 0)
1263 error (0, errno, "%s", dst_path);
1269 if (close (dest_desc) < 0)
1271 error (0, errno, "%s", dst_path);
1275 if (close (source_desc) < 0)
1277 error (0, errno, "%s", src_path);