home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / fileutil.lha / fileutils-3.3 / src / cp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-03  |  29.7 KB  |  1,261 lines

  1. /* cp.c  -- file copying (main routines)
  2.    Copyright (C) 1989, 1990, 1991 Free Software Foundation.
  3.  
  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.
  8.  
  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.
  13.  
  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.
  17.  
  18.    Written by Torbjorn Granlund, David MacKenzie, and Jim Meyering. */
  19.  
  20. #ifdef _AIX
  21.  #pragma alloca
  22. #endif
  23. #include <sys/types.h>
  24. #include <stdio.h>
  25. #include <getopt.h>
  26. #include "cp.h"
  27. #include "backupfile.h"
  28.  
  29. #ifndef _POSIX_VERSION
  30. uid_t geteuid ();
  31. #endif
  32.  
  33. /* Used by do_copy, make_path, and re_protect
  34.    to keep a list of leading directories whose protections
  35.    need to be fixed after copying. */
  36. struct dir_attr
  37. {
  38.   int is_new_dir;
  39.   int slash_offset;
  40.   struct dir_attr *next;
  41. };
  42.  
  43. char *dirname ();
  44. enum backup_type get_version ();
  45. int eaccess_stat ();
  46.  
  47. static int make_path ();
  48. static int re_protect ();
  49.  
  50. /* Initial number of entries in each hash table entry's table of inodes.  */
  51. #define INITIAL_HASH_MODULE 100
  52.  
  53. /* Initial number of entries in the inode hash table.  */
  54. #define INITIAL_ENTRY_TAB_SIZE 70
  55.  
  56. /* A pointer to either lstat or stat, depending on
  57.    whether dereferencing of symlinks is done.  */
  58. int (*xstat) ();
  59.  
  60. /* The invocation name of this program.  */
  61. char *program_name;
  62.  
  63. /* If nonzero, copy all files except directories and, if not dereferencing
  64.    them, symbolic links, as if they were regular files. */
  65. int flag_copy_as_regular = 1;
  66.  
  67. /* If nonzero, dereference symbolic links (copy the files they point to). */
  68. int flag_dereference = 1;
  69.  
  70. /* If nonzero, remove existing target nondirectories. */
  71. int flag_force = 0;
  72.  
  73. /* If nonzero, create hard links instead of copying files.
  74.    Create target directories as usual. */
  75. int flag_hard_link = 0;
  76.  
  77. /* If nonzero, query before overwriting existing targets with regular files. */
  78. int flag_interactive = 0;
  79.  
  80. /* If nonzero, the command "cp x/e_file e_dir" uses "e_dir/x/e_file"
  81.    as its target instead of the usual "e_dir/e_file." */
  82. int flag_path = 0;
  83.  
  84. /* If nonzero, give the copies the original files' permissions,
  85.    ownership, and timestamps. */
  86. int flag_preserve = 0;
  87.  
  88. /* If nonzero, copy directories recursively and copy special files
  89.    as themselves rather than copying their contents. */
  90. int flag_recursive = 0;
  91.  
  92. /* If nonzero, create symbolic links instead of copying files.
  93.    Create target directories as usual. */
  94. int flag_symbolic_link = 0;
  95.  
  96. /* If nonzero, when copying recursively, skip any subdirectories that are
  97.    on different filesystems from the one we started on. */
  98. int flag_one_file_system = 0;
  99.  
  100. /* If nonzero, do not copy a nondirectory that has an existing destination
  101.    with the same or newer modification time. */
  102. int flag_update = 0;
  103.  
  104. /* If nonzero, display the names of the files before copying them. */
  105. int flag_verbose = 0;
  106.  
  107. /* The error code to return to the system. */
  108. int exit_status = 0;
  109.  
  110. /* The bits to preserve in created files' modes. */
  111. int umask_kill;
  112.  
  113. /* This process's effective user ID.  */
  114. uid_t myeuid;
  115.  
  116. struct option long_opts[] =
  117. {
  118.   {"archive", 0, NULL, 'a'},
  119.   {"backup", 0, NULL, 'b'},
  120.   {"force", 0, NULL, 'f'},
  121.   {"interactive", 0, NULL, 'i'},
  122.   {"link", 0, NULL, 'l'},
  123.   {"no-dereference", 0, &flag_dereference, 0},
  124.   {"one-file-system", 0, &flag_one_file_system, 1},
  125.   {"path", 0, &flag_path, 1},
  126.   {"preserve", 0, &flag_preserve, 1},
  127.   {"recursive", 0, NULL, 'R'},
  128.   {"suffix", 1, NULL, 'S'},
  129.   {"symbolic-link", 0, NULL, 's'},
  130.   {"update", 0, &flag_update, 1},
  131.   {"verbose", 0, &flag_verbose, 1},
  132.   {"version-control", 1, NULL, 'V'},
  133.   {NULL, 0, NULL, 0}
  134. };
  135.  
  136. void
  137. main (argc, argv)
  138.      int argc;
  139.      char *argv[];
  140. {
  141.   int c;
  142.   int make_backups = 0;
  143.   char *version;
  144.  
  145.   program_name = argv[0];
  146.   myeuid = geteuid ();
  147.  
  148.   version = getenv ("SIMPLE_BACKUP_SUFFIX");
  149.   if (version)
  150.     simple_backup_suffix = version;
  151.   version = getenv ("VERSION_CONTROL");
  152.  
  153.   /* Find out the current file creation mask, to knock the right bits
  154.      when using chmod.  The creation mask is set to to be liberal, so
  155.      that created directories can be written, even if it would not
  156.      have been allowed with the mask this process was started with.  */
  157.  
  158.   umask_kill = 0777777 ^ umask (0);
  159.  
  160.   while ((c = getopt_long (argc, argv, "abdfilprsuvxPRS:V:", long_opts,
  161.                (int *) 0)) != EOF)
  162.     {
  163.       switch (c)
  164.     {
  165.     case 0:
  166.       break;
  167.  
  168.     case 'a':        /* Like -dpR. */
  169.       flag_dereference = 0;
  170.       flag_preserve = 1;
  171.       flag_recursive = 1;
  172.       flag_copy_as_regular = 0;
  173.       break;
  174.  
  175.     case 'b':
  176.       make_backups = 1;
  177.       break;
  178.  
  179.     case 'd':
  180.       flag_dereference = 0;
  181.       break;
  182.  
  183.     case 'f':
  184.       flag_force = 1;
  185.       flag_interactive = 0;
  186.       break;
  187.  
  188.     case 'i':
  189.       flag_force = 0;
  190.       flag_interactive = 1;
  191.       break;
  192.  
  193.     case 'l':
  194.       flag_hard_link = 1;
  195.       break;
  196.  
  197.     case 'p':
  198.       flag_preserve = 1;
  199.       break;
  200.  
  201.     case 'P':
  202.       flag_path = 1;
  203.       break;
  204.  
  205.     case 'r':
  206.       flag_recursive = 1;
  207.       flag_copy_as_regular = 1;
  208.       break;
  209.  
  210.     case 'R':
  211.       flag_recursive = 1;
  212.       flag_copy_as_regular = 0;
  213.       break;
  214.  
  215.     case 's':
  216. #ifdef S_ISLNK
  217.       flag_symbolic_link = 1;
  218. #else
  219.       error (0, 0, "symbolic links not supported; making hard links");
  220.       flag_hard_link = 1;
  221. #endif
  222.       break;
  223.  
  224.     case 'u':
  225.       flag_update = 1;
  226.       break;
  227.  
  228.     case 'v':
  229.       flag_verbose = 1;
  230.       break;
  231.  
  232.     case 'x':
  233.       flag_one_file_system = 1;
  234.       break;
  235.  
  236.     case 'S':
  237.       simple_backup_suffix = optarg;
  238.       break;
  239.  
  240.     case 'V':
  241.       version = optarg;
  242.       break;
  243.  
  244.     default:
  245.       usage ((char *) 0);
  246.     }
  247.     }
  248.  
  249.   if (flag_hard_link && flag_symbolic_link)
  250.     usage ("cannot make both hard and symbolic links");
  251.  
  252.   if (make_backups)
  253.     backup_type = get_version (version);
  254.  
  255.   if (flag_preserve == 1)
  256.     umask_kill = 0777777;
  257.  
  258.   /* The key difference between -d (--no-dereference) and not is the version
  259.      of `stat' to call.  */
  260.  
  261.   if (flag_dereference)
  262.     xstat = stat;
  263.   else
  264.     xstat = lstat;
  265.  
  266.   /* Allocate space for remembering copied and created files.  */
  267.  
  268.   hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
  269.  
  270.   exit_status |= do_copy (argc, argv);
  271.  
  272.   exit (exit_status);
  273. }
  274.  
  275. /* Scan the arguments, and copy each by calling copy.
  276.    Return 0 if successful, 1 if any errors occur. */
  277.  
  278. int
  279. do_copy (argc, argv)
  280.      int argc;
  281.      char *argv[];
  282. {
  283.   char *target;
  284.   struct stat sb;
  285.   int new_dst = 0;
  286.   int ret = 0;
  287.  
  288.   if (optind >= argc)
  289.     usage ("missing file arguments");
  290.   if (optind >= argc - 1)
  291.     usage ("missing file argument");
  292.  
  293.   target = argv[argc - 1];
  294.  
  295.   strip_trailing_slashes (target);
  296.  
  297.   if (lstat (target, &sb))
  298.     {
  299.       if (errno != ENOENT)
  300.     {
  301.       error (0, errno, "%s", target);
  302.       return 1;
  303.     }
  304.       else
  305.     new_dst = 1;
  306.     }
  307.   else
  308.     {
  309.       struct stat sbx;
  310.  
  311.       /* If `target' is not a symlink to a nonexistent file, use
  312.      the results of stat instead of lstat, so we can copy files
  313.      into symlinks to directories. */
  314.       if (stat (target, &sbx) == 0)
  315.     sb = sbx;
  316.     }
  317.  
  318.   if (!new_dst && S_ISDIR (sb.st_mode))
  319.     {
  320.       /* cp file1...filen edir
  321.      Copy the files `file1' through `filen'
  322.      to the existing directory `edir'. */
  323.  
  324.       for (;;)
  325.     {
  326.       char *arg;
  327.       char *ap;
  328.       char *dst_path;
  329.       int parent_exists = 1; /* True if dirname (dst_path) exists. */
  330.       struct dir_attr *attr_list;
  331.  
  332.       arg = argv[optind];
  333.  
  334.       strip_trailing_slashes (arg);
  335.  
  336.       if (flag_path)
  337.         {
  338.           /* Append all of `arg' to `target'.  */
  339.           dst_path = xmalloc (strlen (target) + strlen (arg) + 2);
  340. #ifdef AMIGA
  341.           {
  342.         char *temp = stpcpy(dst_path, target);
  343.  
  344.         if (temp[-1] != ':') temp = stpcpy(temp, "/");
  345.         stpcpy(temp, arg);
  346.           }
  347. #else
  348.           stpcpy (stpcpy (stpcpy (dst_path, target), "/"), arg);
  349. #endif
  350.  
  351.           /* For --path, we have to make sure that the directory
  352.              dirname (dst_path) exists.  We may have to create a few
  353.              leading directories. */
  354.           parent_exists = !make_path (dst_path,
  355.                       strlen (target) + 1, 0700,
  356.                       flag_verbose ? "%s -> %s\n" :
  357.                       (char *) NULL,
  358.                       &attr_list, &new_dst);
  359.         }
  360.         else
  361.           {
  362.           /* Append the last component of `arg' to `target'.  */
  363.  
  364.           ap = basename (arg);
  365.           /* For `cp -R source/.. target', don't copy into `target/..'. */
  366.           if (!strcmp (ap, ".."))
  367.         dst_path = target;
  368.           else
  369.         {
  370.           dst_path = xmalloc (strlen (target) + strlen (ap) + 2);
  371. #ifdef AMIGA
  372.           {
  373.             char *temp = stpcpy(dst_path, target);
  374.  
  375.             if (temp[-1] != ':') temp = stpcpy(temp, "/");
  376.             stpcpy(temp, ap);
  377.           }
  378. #else
  379.           stpcpy (stpcpy (stpcpy (dst_path, target), "/"), ap);
  380. #endif
  381.         }
  382.         }
  383.  
  384.       if (!parent_exists)
  385.         {
  386.           /* make_path failed, so we shouldn't even attempt the copy. */
  387.           ret = 1;
  388.           }
  389.       else
  390.         {
  391.           ret |= copy (arg, dst_path, new_dst, 0, (struct dir_list *) 0);
  392.           forget_all ();
  393.   
  394.           if (flag_path)
  395.         {
  396.           ret |= re_protect (dst_path, strlen (target) + 1,
  397.                     attr_list);
  398.         }
  399.         }
  400.  
  401.       ++optind;
  402.       if (optind == argc - 1)
  403.         break;
  404.     }
  405.       return ret;
  406.     }
  407.   else if (argc - optind == 2)
  408.     {
  409.       if (flag_path)
  410.     usage ("when preserving paths, last argument must be a directory");
  411.       return copy (argv[optind], target, new_dst, 0, (struct dir_list *) 0);
  412.     }
  413.   else
  414.     usage ("when copying multiple files, last argument must be a directory");
  415. }
  416.  
  417. /* Copy the file SRC_PATH to the file DST_PATH.  The files may be of
  418.    any type.  NEW_DST should be non-zero if the file DST_PATH cannot
  419.    exist because its parent directory was just created; NEW_DST should
  420.    be zero if DST_PATH might already exist.  DEVICE is the device
  421.    number of the parent directory, or 0 if the parent of this file is
  422.    not known.  ANCESTORS points to a linked, null terminated list of
  423.    devices and inodes of parent directories of SRC_PATH.
  424.    Return 0 if successful, 1 if an error occurs. */
  425.  
  426. int
  427. copy (src_path, dst_path, new_dst, device, ancestors)
  428.      char *src_path;
  429.      char *dst_path;
  430.      int new_dst;
  431.      dev_t device;
  432.      struct dir_list *ancestors;
  433. {
  434.   struct stat src_sb;
  435.   struct stat dst_sb;
  436.   int src_mode;
  437.   int src_type;
  438.   char *earlier_file;
  439.   char *dst_backup = NULL;
  440.   int dir_mode_changed = 0;
  441.  
  442.   if ((*xstat) (src_path, &src_sb))
  443.     {
  444.       error (0, errno, "%s", src_path);
  445.       return 1;
  446.     }
  447.  
  448.   /* Are we crossing a file system boundary?  */
  449.   if (flag_one_file_system && device != 0 && device != src_sb.st_dev)
  450.     return 0;
  451.  
  452.   /* We wouldn't insert a node unless nlink > 1, except that we need to
  453.      find created files so as to not copy infinitely if a directory is
  454.      copied into itself.  */
  455.  
  456.   earlier_file = remember_copied (dst_path, src_sb.st_ino, src_sb.st_dev);
  457.  
  458.   /* Did we just create this file?  */
  459.  
  460.   if (earlier_file == &new_file)
  461.     return 0;
  462.  
  463.   src_mode = src_sb.st_mode;
  464.   src_type = src_sb.st_mode;
  465.  
  466.   if (S_ISDIR (src_type) && !flag_recursive)
  467.     {
  468.       error (0, 0, "%s: omitting directory", src_path);
  469.       return 1;
  470.     }
  471.  
  472.   if (!new_dst)
  473.     {
  474.       if ((*xstat) (dst_path, &dst_sb))
  475.     {
  476.       if (errno != ENOENT)
  477.         {
  478.           error (0, errno, "%s", dst_path);
  479.           return 1;
  480.         }
  481.       else
  482.         new_dst = 1;
  483.     }
  484.       else
  485.     {
  486.       /* The file exists already.  */
  487.  
  488.       if (src_sb.st_ino == dst_sb.st_ino && src_sb.st_dev == dst_sb.st_dev)
  489.         {
  490.           if (flag_hard_link)
  491.         return 0;
  492.  
  493.           error (0, 0, "`%s' and `%s' are the same file",
  494.              src_path, dst_path);
  495.           return 1;
  496.         }
  497.  
  498.       if (!S_ISDIR (src_type))
  499.         {
  500.           if (S_ISDIR (dst_sb.st_mode))
  501.         {
  502.           error (0, 0,
  503.              "%s: cannot overwrite directory with non-directory",
  504.              dst_path);
  505.           return 1;
  506.         }
  507.  
  508.           if (flag_update && src_sb.st_mtime <= dst_sb.st_mtime)
  509.         return 0;
  510.         }
  511.  
  512.       if (S_ISREG (src_type) && !flag_force)
  513.         {
  514.           if (flag_interactive)
  515.         {
  516.           if (eaccess_stat (&dst_sb, W_OK) != 0)
  517.             fprintf (stderr,
  518.                  "%s: overwrite `%s', overriding mode %04o? ",
  519.                  program_name, dst_path, dst_sb.st_mode & 07777);
  520.           else
  521.             fprintf (stderr, "%s: overwrite `%s'? ",
  522.                  program_name, dst_path);
  523.           if (!yesno ())
  524.             return 0;
  525.         }
  526.         }
  527.  
  528.       if (backup_type != none && !S_ISDIR (dst_sb.st_mode))
  529.         {
  530.           char *tmp_backup = find_backup_file_name (dst_path);
  531.           if (tmp_backup == NULL)
  532.         error (1, 0, "virtual memory exhausted");
  533.           dst_backup = alloca (strlen (tmp_backup) + 1);
  534.           strcpy (dst_backup, tmp_backup);
  535.           free (tmp_backup);
  536.           if (rename (dst_path, dst_backup))
  537.         {
  538.           if (errno != ENOENT)
  539.             {
  540.               error (0, errno, "cannot backup `%s'", dst_path);
  541.               return 1;
  542.             }
  543.           else
  544.             dst_backup = NULL;
  545.         }
  546.           new_dst = 1;
  547.         }
  548.       else if (flag_force)
  549.         {
  550.           if (S_ISDIR (dst_sb.st_mode))
  551.         {
  552.           /* Temporarily change mode to allow overwriting. */
  553.           if (eaccess_stat (&dst_sb, W_OK | X_OK) != 0)
  554.             {
  555.               if (chmod (dst_path, 0700))
  556.             {
  557.               error (0, errno, "%s", dst_path);
  558.               return 1;
  559.             }
  560.               else
  561.             dir_mode_changed = 1;
  562.             }
  563.         }
  564.           else
  565.         {
  566.           if (unlink (dst_path) && errno != ENOENT)
  567.             {
  568.               error (0, errno, "cannot remove old link to `%s'",
  569.                  dst_path);
  570.               return 1;
  571.             }
  572.           new_dst = 1;
  573.         }
  574.         }
  575.     }
  576.     }
  577.  
  578.   /* If the source is a directory, we don't always create the target
  579.      directory.  So --verbose should not announce anything until we're
  580.      sure we'll create a directory. */
  581.   if (flag_verbose && !S_ISDIR (src_type))
  582.     printf ("%s -> %s\n", src_path, dst_path);
  583.  
  584.   /* Did we copy this inode somewhere else (in this command line argument)
  585.      and therefore this is a second hard link to the inode?  */
  586.  
  587.   if (!flag_dereference && src_sb.st_nlink > 1 && earlier_file)
  588.     {
  589.       if (link (earlier_file, dst_path))
  590.     {
  591.       error (0, errno, "%s", dst_path);
  592.       goto un_backup;
  593.     }
  594.       return 0;
  595.     }
  596.  
  597.   if (S_ISDIR (src_type))
  598.     {
  599.       struct dir_list *dir;
  600.  
  601.       /* If this directory has been copied before during the
  602.          recursion, there is a symbolic link to an ancestor
  603.          directory of the symbolic link.  It is impossible to
  604.          continue to copy this, unless we've got an infinite disk.  */
  605.  
  606.       if (is_ancestor (&src_sb, ancestors))
  607.     {
  608.       error (0, 0, "%s: cannot copy cyclic symbolic link", src_path);
  609.       goto un_backup;
  610.     }
  611.  
  612.       /* Insert the current directory in the list of parents.  */
  613.  
  614.       dir = (struct dir_list *) alloca (sizeof (struct dir_list));
  615.       dir->parent = ancestors;
  616.       dir->ino = src_sb.st_ino;
  617.       dir->dev = src_sb.st_dev;
  618.  
  619.       if (new_dst || !S_ISDIR (dst_sb.st_mode))
  620.     {
  621.       /* Create the new directory writable and searchable, so
  622.              we can create new entries in it.  */
  623.  
  624.       if (mkdir (dst_path, (src_mode & umask_kill) | 0700))
  625.         {
  626.           error (0, errno, "cannot create directory `%s'", dst_path);
  627.           goto un_backup;
  628.         }
  629.  
  630.       /* Insert the created directory's inode and device
  631.              numbers into the search structure, so that we can
  632.              avoid copying it again.  */
  633.  
  634.       if (remember_created (dst_path))
  635.         goto un_backup;
  636.  
  637.       if (flag_verbose)
  638.         printf ("%s -> %s\n", src_path, dst_path);
  639.     }
  640.  
  641.       /* Copy the contents of the directory.  */
  642.  
  643.       if (copy_dir (src_path, dst_path, new_dst, &src_sb, dir))
  644.     return 1;
  645.     }
  646. #ifdef S_ISLNK
  647.   else if (flag_symbolic_link)
  648.     {
  649.       if (*src_path == '/'
  650.       || (!strncmp (dst_path, "./", 2) && index (dst_path + 2, '/') == 0)
  651.       || index (dst_path, '/') == 0)
  652.     {
  653.       if (symlink (src_path, dst_path))
  654.         {
  655.           error (0, errno, "%s", dst_path);
  656.           goto un_backup;
  657.         }
  658.       return 0;
  659.     }
  660.       else
  661.     {
  662.       error (0, 0,
  663.          "%s: can only make relative symbolic links in current directory", dst_path);
  664.       goto un_backup;
  665.     }
  666.     }
  667. #endif
  668.   else if (flag_hard_link)
  669.     {
  670.       if (link (src_path, dst_path))
  671.     {
  672.       error (0, errno, "cannot create link `%s'", dst_path);
  673.       goto un_backup;
  674.     }
  675.       return 0;
  676.     }
  677.   else if (S_ISREG (src_type)
  678.        || (flag_copy_as_regular && !S_ISDIR (src_type)
  679. #ifdef S_ISLNK
  680.            && !S_ISLNK (src_type)
  681. #endif
  682.            ))
  683.     {
  684.       if (copy_reg (src_path, dst_path))
  685.     goto un_backup;
  686.     }
  687.   else
  688. #ifdef S_ISFIFO
  689.   if (S_ISFIFO (src_type))
  690.     {
  691.       if (mkfifo (dst_path, src_mode & umask_kill))
  692.     {
  693.       error (0, errno, "cannot create fifo `%s'", dst_path);
  694.       goto un_backup;
  695.     }
  696.     }
  697.   else
  698. #endif
  699.     if (S_ISBLK (src_type) || S_ISCHR (src_type)
  700. #ifdef S_ISSOCK
  701.     || S_ISSOCK (src_type)
  702. #endif
  703.     )
  704.     {
  705.       if (mknod (dst_path, src_mode & umask_kill, src_sb.st_rdev))
  706.     {
  707.       error (0, errno, "cannot create special file `%s'", dst_path);
  708.       goto un_backup;
  709.     }
  710.     }
  711.   else
  712. #ifdef S_ISLNK
  713. #ifdef _AIX
  714. #define LINK_BUF PATH_MAX
  715. #else
  716. #define LINK_BUF src_sb.st_size
  717. #endif
  718.   if (S_ISLNK (src_type))
  719.     {
  720.       char *link_val = (char *) alloca (LINK_BUF + 1);
  721.       int link_size;
  722.  
  723.       link_size = readlink (src_path, link_val, LINK_BUF);
  724.       if (link_size < 0)
  725.     {
  726.       error (0, errno, "cannot read symbolic link `%s'", src_path);
  727.       goto un_backup;
  728.     }
  729.       link_val[link_size] = '\0';
  730.  
  731.       if (symlink (link_val, dst_path))
  732.     {
  733.       error (0, errno, "cannot create symbolic link `%s'", dst_path);
  734.       goto un_backup;
  735.     }
  736.       return 0;
  737.     }
  738.   else
  739. #endif
  740.     {
  741.       error (0, 0, "%s: unknown file type", src_path);
  742.       goto un_backup;
  743.     }
  744.  
  745.   if ((flag_preserve || new_dst)
  746.       && (flag_copy_as_regular || S_ISREG (src_type) || S_ISDIR (src_type)))
  747.     {
  748.       if (chmod (dst_path, src_mode & umask_kill))
  749.     {
  750.       error (0, errno, "%s", dst_path);
  751.       return 1;
  752.     }
  753.     }
  754.   else if (dir_mode_changed)
  755.     {
  756.       /* Reset the temporarily changed mode.  */
  757.       if (chmod (dst_path, dst_sb.st_mode))
  758.     {
  759.       error (0, errno, "%s", dst_path);
  760.       return 1;
  761.     }
  762.     }
  763.  
  764.   /* Adjust the times (and if possible, ownership) for the copy. */
  765.  
  766.   if (flag_preserve)
  767.     {
  768.       struct utimbuf utb;
  769.  
  770.       utb.actime = src_sb.st_atime;
  771.       utb.modtime = src_sb.st_mtime;
  772.  
  773.       if (utime (dst_path, &utb))
  774.     {
  775.       error (0, errno, "%s", dst_path);
  776.       return 1;
  777.     }
  778.  
  779.       /* If non-root uses -p, it's ok if we can't preserve ownership.
  780.      But root probably wants to know, e.g. if NFS disallows it.  */
  781.       if (chown (dst_path, src_sb.st_uid, src_sb.st_gid)
  782.       && (errno != EPERM || myeuid == 0))
  783.     {
  784.       error (0, errno, "%s", dst_path);
  785.       return 1;
  786.     }
  787.     }
  788.  
  789.   return 0;
  790.  
  791. un_backup:
  792.   if (dst_backup)
  793.     {
  794.       if (rename (dst_backup, dst_path))
  795.     error (0, errno, "cannot un-backup `%s'", dst_path);
  796.     }
  797.   return 1;
  798. }
  799.  
  800. /* Ensure that the parent directory of CONST_DIRPATH exists, for
  801.    the --path option.
  802.  
  803.    SRC_OFFSET is the index in CONST_DIRPATH (which is a destination
  804.    path) of the beginning of the source directory name.
  805.    Create any leading directories that don't already exist,
  806.    giving them permissions MODE.
  807.    If VERBOSE_FMT_STRING is nonzero, use it as a printf format
  808.    string for printing a message after successfully making a directory.
  809.    The format should take two string arguments: the names of the
  810.    source and destination directories.
  811.    Creates a linked list of attributes of intermediate directories,
  812.    *ATTR_LIST, for re_protect to use after calling copy.
  813.    Sets *NEW_DST to 1 if this function creates parent of CONST_DIRPATH.
  814.  
  815.    Return 0 if parent of CONST_DIRPATH exists as a directory with the proper
  816.    permissions when done, otherwise 1. */
  817.  
  818. static int
  819. make_path (const_dirpath, src_offset, mode, verbose_fmt_string,
  820.           attr_list, new_dst)
  821.      char *const_dirpath;
  822.      int src_offset;
  823.      int mode;
  824.      char *verbose_fmt_string;
  825.      struct dir_attr **attr_list;
  826.      int *new_dst;
  827. {
  828.   struct stat stats;
  829.   char *dirpath;        /* A copy of CONST_DIRPATH we can change. */
  830.   char *src;            /* Source name in `dirpath'. */
  831.   char *tmp_dst_dirname;    /* Leading path of `dirpath', malloc. */
  832.   char *dst_dirname;        /* Leading path of `dirpath', alloca. */
  833.  
  834.   dirpath = alloca (strlen (const_dirpath));
  835.   strcpy (dirpath, const_dirpath);
  836.  
  837.   src = dirpath + src_offset;
  838.  
  839.   tmp_dst_dirname = dirname (dirpath); 
  840.   dst_dirname = alloca (strlen (tmp_dst_dirname));
  841.   strcpy (dst_dirname, tmp_dst_dirname);
  842.   free (tmp_dst_dirname);
  843.  
  844.   *attr_list = NULL;
  845.  
  846.   if ((*xstat) (dst_dirname, &stats))
  847.     {
  848.       /* Parent of CONST_DIRNAME does not exist.
  849.      Make all missing intermediate directories. */
  850.       char *slash;
  851.  
  852.       slash = src;
  853. #ifndef AMIGA
  854.       while (*slash == '/')
  855.     slash++;
  856. #endif
  857.       while (slash = index (slash, '/'))
  858.     {
  859.       /* Add this directory to the list of directories whose modes need
  860.          fixing later. */
  861.       struct dir_attr *new =
  862.         (struct dir_attr *) xmalloc (sizeof (struct dir_attr));
  863.       new->slash_offset = slash - dirpath;
  864.       new->next = *attr_list;
  865.       *attr_list = new;
  866.  
  867.       *slash = '\0';
  868.       if ((*xstat) (dirpath, &stats))
  869.         {
  870.           /* This element of the path does not exist.  We must set
  871.          *new_dst and new->is_new_dir inside this loop because,
  872.          for example, in the command `cp --path ../a/../b/c e_dir',
  873.          make_path creates only e_dir/../a if ./b already exists. */
  874.           *new_dst = 1;
  875.           new->is_new_dir = 1;
  876.           if (mkdir (dirpath, mode))
  877.         {
  878.           error (0, errno, "cannot make directory `%s'", dirpath);
  879.           return 1;
  880.         }
  881.           else
  882.         {
  883.           if (verbose_fmt_string != NULL)
  884.             printf (verbose_fmt_string, src, dirpath);
  885.         }
  886.         }
  887.       else if (!S_ISDIR (stats.st_mode))
  888.         {
  889.           error (0, 0, "`%s' exists but is not a directory", dirpath);
  890.           return 1;
  891.         }
  892.       else
  893.         {
  894.           new->is_new_dir = 0;
  895.           *new_dst = 0;
  896.         }
  897.       *slash++ = '/';
  898.  
  899. #ifndef AMIGA
  900.       /* Avoid unnecessary calls to `stat' when given
  901.          pathnames containing multiple adjacent slashes.  */
  902.       while (*slash == '/')
  903.         slash++;
  904. #endif
  905.     }
  906.     }
  907.  
  908.   /* We get here if the parent of `dirpath' already exists. */
  909.  
  910.   else if (!S_ISDIR (stats.st_mode))
  911.     {
  912.       error (0, 0, "`%s' exists but is not a directory", dst_dirname);
  913.       return 1;
  914.     }
  915.   else if (chmod (dst_dirname, mode))
  916.     {
  917.       error (0, errno, "%s", dst_dirname);
  918.       return 1;
  919.     }
  920.   else
  921.     {
  922.       *new_dst = 0;
  923.     }
  924.   return 0;
  925. }
  926.  
  927. /* Ensure that the parent directories of CONST_DST_PATH have the
  928.    correct protections, for the --path option.  This is done
  929.    after all copying has been completed, to allow permissions
  930.    that don't include user write/execute.
  931.  
  932.    SRC_OFFSET is the index in CONST_DST_PATH of the beginning of the
  933.    source directory name.
  934.  
  935.    ATTR_LIST is a null-terminated linked list of structures that
  936.    indicates the end of the filename of each intermediate directory
  937.    in CONST_DST_PATH that may need to have its attributes changed.
  938.    The command `cp --path --preserve a/b/c d/e_dir' changes the
  939.    attributes of the directories d/e_dir/a and d/e_dir/a/b to match
  940.    the corresponding source directories regardless of whether they
  941.    existed before the `cp' command was given.
  942.  
  943.    Return 0 if the parent of CONST_DST_PATH and any intermediate
  944.    directories specified by ATTR_LIST have the proper permissions
  945.    when done, otherwise 1. */
  946.  
  947. static int
  948. re_protect (const_dst_path, src_offset, attr_list)
  949.      char *const_dst_path;
  950.      int src_offset;
  951.      struct dir_attr *attr_list;
  952. {
  953.   struct dir_attr *p;
  954.   char *dst_path;        /* A copy of CONST_DST_PATH we can change. */
  955.   char *src_path;        /* The source name in `dst_path'. */
  956.  
  957.   dst_path = alloca (strlen (const_dst_path));
  958.   strcpy (dst_path, const_dst_path);
  959.   src_path = dst_path + src_offset; 
  960.  
  961.   for (p = attr_list; p; p = p->next)
  962.     {
  963.       struct stat src_sb;
  964.  
  965.       dst_path[p->slash_offset] = '\0';
  966.  
  967.       if ((*xstat) (src_path, &src_sb))
  968.     {
  969.       error (0, errno, "%s", src_path);
  970.       return 1;
  971.     }
  972.  
  973.       if (flag_preserve || p->is_new_dir)
  974.     {
  975.       if (chmod (dst_path, src_sb.st_mode & umask_kill))
  976.         {
  977.           error (0, errno, "%s", dst_path);
  978.           return 1;
  979.         }
  980.     }
  981.  
  982.       /* Adjust the times (and if possible, ownership) for the copy. */
  983.  
  984.       if (flag_preserve)
  985.     {
  986.       struct utimbuf utb;
  987.  
  988.       utb.actime = src_sb.st_atime;
  989.       utb.modtime = src_sb.st_mtime;
  990.  
  991.       if (utime (dst_path, &utb))
  992.         {
  993.           error (0, errno, "%s", dst_path);
  994.           return 1;
  995.         }
  996.  
  997.       /* If non-root uses -p, it's ok if we can't preserve ownership.
  998.          But root probably wants to know, e.g. if NFS disallows it.  */
  999.       if (chown (dst_path, src_sb.st_uid, src_sb.st_gid)
  1000.           && (errno != EPERM || myeuid == 0))
  1001.         {
  1002.           error (0, errno, "%s", dst_path);
  1003.           return 1;
  1004.         }
  1005.     }
  1006.  
  1007.       dst_path[p->slash_offset] = '/';
  1008.     }
  1009.   return 0;
  1010. }
  1011.  
  1012. /* Read the contents of the directory SRC_PATH_IN, and recursively
  1013.    copy the contents to DST_PATH_IN.  NEW_DST is non-zero if
  1014.    DST_PATH_IN is a directory that was created previously in the
  1015.    recursion.   SRC_SB and ANCESTORS describe SRC_PATH_IN.
  1016.    Return 0 if successful, -1 if an error occurs. */
  1017.  
  1018. int
  1019. copy_dir (src_path_in, dst_path_in, new_dst, src_sb, ancestors)
  1020.      char *src_path_in;
  1021.      char *dst_path_in;
  1022.      int new_dst;
  1023.      struct stat *src_sb;
  1024.      struct dir_list *ancestors;
  1025. {
  1026.   char *name_space;
  1027.   char *namep;
  1028.   char *src_path;
  1029.   char *dst_path;
  1030.   int ret = 0;
  1031.  
  1032.   errno = 0;
  1033.   name_space = savedir (src_path_in, src_sb->st_size);
  1034.   if (name_space == 0)
  1035.     {
  1036.       if (errno)
  1037.     {
  1038.       error (0, errno, "%s", src_path_in);
  1039.       return -1;
  1040.     }
  1041.       else
  1042.     error (1, 0, "virtual memory exhausted");
  1043.     }
  1044.  
  1045.   namep = name_space;
  1046.   while (*namep != '\0')
  1047.     {
  1048.       int fn_length = strlen (namep) + 1;
  1049.  
  1050.       dst_path = xmalloc (strlen (dst_path_in) + fn_length + 1);
  1051.       src_path = xmalloc (strlen (src_path_in) + fn_length + 1);
  1052.  
  1053. #ifdef AMIGA
  1054.       {
  1055.     char *temp = stpcpy(src_path, src_path_in);
  1056.  
  1057.     if (temp[-1] != ':') temp = stpcpy(temp, "/");
  1058.     stpcpy(temp, namep);
  1059.  
  1060.     temp = stpcpy(dst_path, dst_path_in);
  1061.  
  1062.     if (temp[-1] != ':') temp = stpcpy(temp, "/");
  1063.     stpcpy(temp, namep);
  1064.       }
  1065. #else
  1066.       stpcpy (stpcpy (stpcpy (src_path, src_path_in), "/"), namep);
  1067.       stpcpy (stpcpy (stpcpy (dst_path, dst_path_in), "/"), namep);
  1068. #endif
  1069.  
  1070.       ret |= copy (src_path, dst_path, new_dst, src_sb->st_dev, ancestors);
  1071.  
  1072.       /* Free the memory for `src_path'.  The memory for `dst_path'
  1073.      cannot be deallocated, since it is used to create multiple
  1074.      hard links.  */
  1075.  
  1076.       free (src_path);
  1077.  
  1078.       namep += fn_length;
  1079.     }
  1080.   free (name_space);
  1081.   return -ret;
  1082. }
  1083.  
  1084. /* Copy a regular file from SRC_PATH to DST_PATH.
  1085.    If the source file contains holes, copies holes and blocks of zeros
  1086.    in the source file as holes in the target file.
  1087.    (Holes are read as zeroes by the `read' system call.)
  1088.    Return 0 if successful, -1 if an error occurred. */
  1089.  
  1090. int
  1091. copy_reg (src_path, dst_path)
  1092.      char *src_path;
  1093.      char *dst_path;
  1094. {
  1095.   char *buf;
  1096.   int buf_size;
  1097.   int target_desc;
  1098.   int source_desc;
  1099.   int n_read;
  1100.   int n_written;
  1101.   struct stat sb;
  1102.   char *cp;
  1103.   int *ip;
  1104.   int return_val = 0;
  1105.   long n_read_total = 0;
  1106.   int last_write_made_hole = 0;
  1107.   int make_holes = 0;
  1108.  
  1109.   source_desc = open (src_path, O_RDONLY);
  1110.   if (source_desc < 0)
  1111.     {
  1112.       error (0, errno, "%s", src_path);
  1113.       return -1;
  1114.     }
  1115.  
  1116.   /* Create the new regular file with small permissions initially,
  1117.      to not create a security hole.  */
  1118.  
  1119.   target_desc = open (dst_path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  1120.   if (target_desc < 0)
  1121.     {
  1122.       error (0, errno, "cannot create regular file `%s'", dst_path);
  1123.       return_val = -1;
  1124.       goto ret2;
  1125.     }
  1126.  
  1127.   /* Find out the optimal buffer size.  */
  1128.  
  1129.   if (fstat (target_desc, &sb))
  1130.     {
  1131.       error (0, errno, "%s", dst_path);
  1132.       return_val = -1;
  1133.       goto ret;
  1134.     }
  1135.  
  1136.   buf_size = ST_BLKSIZE (sb);
  1137.  
  1138. #ifdef HAVE_ST_BLOCKS
  1139.   if (S_ISREG (sb.st_mode))
  1140.     {
  1141.       /* Find out whether the file contains any sparse blocks. */
  1142.  
  1143.       if (fstat (source_desc, &sb))
  1144.     {
  1145.       error (0, errno, "%s", src_path);
  1146.       return_val = -1;
  1147.       goto ret;
  1148.     }
  1149.  
  1150.       /* If the file has fewer blocks than would normally
  1151.      be needed for a file of its size, then
  1152.      at least one of the blocks in the file is a hole. */
  1153.       if (S_ISREG (sb.st_mode) &&
  1154.       sb.st_size - (sb.st_blocks * DEV_BSIZE) >= DEV_BSIZE)
  1155.     make_holes = 1;
  1156.     }
  1157. #endif
  1158.  
  1159.   /* Make a buffer with space for a sentinel at the end.  */
  1160.  
  1161.   buf = (char *) alloca (buf_size + sizeof (int));
  1162.  
  1163.   for (;;)
  1164.     {
  1165.       n_read = read (source_desc, buf, buf_size);
  1166.       if (n_read < 0)
  1167.     {
  1168.       error (0, errno, "%s", src_path);
  1169.       return_val = -1;
  1170.       goto ret;
  1171.     }
  1172.       if (n_read == 0)
  1173.     break;
  1174.  
  1175.       n_read_total += n_read;
  1176.  
  1177.       ip = 0;
  1178.       if (make_holes)
  1179.     {
  1180.       buf[n_read] = 1;    /* Sentinel to stop loop.  */
  1181.  
  1182.       /* Find first non-zero *word*, or the word with the sentinel.  */
  1183.  
  1184.       ip = (int *) buf;
  1185.       while (*ip++ == 0)
  1186.         ;
  1187.  
  1188.       /* Find the first non-zero *byte*, or the sentinel.  */
  1189.  
  1190.       cp = (char *) (ip - 1);
  1191.       while (*cp++ == 0)
  1192.         ;
  1193.  
  1194.       /* If we found the sentinel, the whole input block was zero,
  1195.          and we can make a hole.  */
  1196.  
  1197.       if (cp > buf + n_read)
  1198.         {
  1199.           /* Make a hole.  */
  1200.           if (lseek (target_desc, (off_t) n_read, SEEK_CUR) < 0L)
  1201.         {
  1202.           error (0, errno, "%s", dst_path);
  1203.           return_val = -1;
  1204.           goto ret;
  1205.         }
  1206.           last_write_made_hole = 1;
  1207.         }
  1208.       else
  1209.         /* Clear to indicate that a normal write is needed. */
  1210.         ip = 0;
  1211.     }
  1212.       if (ip == 0)
  1213.     {
  1214.       n_written = write (target_desc, buf, n_read);
  1215.       if (n_written < n_read)
  1216.         {
  1217.           error (0, errno, "%s", dst_path);
  1218.           return_val = -1;
  1219.           goto ret;
  1220.         }
  1221.       last_write_made_hole = 0;
  1222.     }
  1223.     }
  1224.  
  1225.   /* If the file ends with a `hole', something needs to be written at
  1226.      the end.  Otherwise the kernel would truncate the file at the end
  1227.      of the last write operation.  */
  1228.  
  1229.   if (last_write_made_hole)
  1230.     {
  1231. #ifdef HAVE_FTRUNCATE
  1232.       /* Write a null character and truncate it again.  */
  1233.       if (write (target_desc, "", 1) != 1
  1234.       || ftruncate (target_desc, n_read_total) < 0)
  1235. #else
  1236.       /* Seek backwards one character and write a null.  */
  1237.       if (lseek (target_desc, (off_t) -1, SEEK_CUR) < 0L
  1238.       || write (target_desc, "", 1) != 1)
  1239. #endif
  1240.     {
  1241.       error (0, errno, "%s", dst_path);
  1242.       return_val = -1;
  1243.     }
  1244.     }
  1245.  
  1246. ret:
  1247.   if (close (target_desc) < 0)
  1248.     {
  1249.       error (0, errno, "%s", dst_path);
  1250.       return_val = -1;
  1251.     }
  1252. ret2:
  1253.   if (close (source_desc) < 0)
  1254.     {
  1255.       error (0, errno, "%s", src_path);
  1256.       return_val = -1;
  1257.     }
  1258.  
  1259.   return return_val;
  1260. }
  1261.