home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / cpio-2.3-src.lha / src / amiga / cpio-2.3 / copyout.c < prev    next >
C/C++ Source or Header  |  1993-04-28  |  23KB  |  802 lines

  1. /* copyout.c - create a cpio archive
  2.    Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  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. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include "filetypes.h"
  22. #include "system.h"
  23. #include "cpiohdr.h"
  24. #include "dstring.h"
  25. #include "extern.h"
  26. #include "defer.h"
  27. #include "rmt.h"
  28.  
  29. static unsigned long read_for_checksum ();
  30. static void clear_rest_of_block ();
  31. static void pad_output ();
  32. static int last_link ();
  33. static int count_defered_links_to_dev_ino ();
  34. static void add_link_defer ();
  35. static void writeout_other_defers ();
  36. static void writeout_final_defers();
  37. static void writeout_defered_file ();
  38.  
  39. /* Write out header FILE_HDR, including the file name, to file
  40.    descriptor OUT_DES.  */
  41.  
  42. void
  43. write_out_header (file_hdr, out_des)
  44.      struct new_cpio_header *file_hdr;
  45.      int out_des;
  46. {
  47.   if (archive_format == arf_newascii || archive_format == arf_crcascii)
  48.     {
  49.       char ascii_header[112];
  50.       char *magic_string;
  51.  
  52.       if (archive_format == arf_crcascii)
  53.     magic_string = "070702";
  54.       else
  55.     magic_string = "070701";
  56.       sprintf (ascii_header,
  57.            "%6s%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx",
  58.            magic_string,
  59.            file_hdr->c_ino, file_hdr->c_mode, file_hdr->c_uid,
  60.            file_hdr->c_gid, file_hdr->c_nlink, file_hdr->c_mtime,
  61.          file_hdr->c_filesize, file_hdr->c_dev_maj, file_hdr->c_dev_min,
  62.        file_hdr->c_rdev_maj, file_hdr->c_rdev_min, file_hdr->c_namesize,
  63.            file_hdr->c_chksum);
  64.       copy_buf_out (ascii_header, out_des, 110L);
  65.  
  66.       /* Write file name to output.  */
  67.       copy_buf_out (file_hdr->c_name, out_des, (long) file_hdr->c_namesize);
  68.       pad_output (out_des, file_hdr->c_namesize + 110);
  69.     }
  70.   else if (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
  71.     {
  72.       char ascii_header[78];
  73. #ifndef __MSDOS__
  74.       dev_t dev;
  75.       dev_t rdev;
  76.  
  77.       if (archive_format == arf_oldascii)
  78.     {
  79.       dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
  80.       rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
  81.     }
  82.       else
  83.     {
  84.       /* HP/UX cpio creates archives that look just like ordinary archives,
  85.          but for devices it sets major = 0, minor = 1, and puts the
  86.          actual major/minor number in the filesize field.  */
  87.       switch (file_hdr->c_mode & CP_IFMT)
  88.         {
  89.           case CP_IFCHR:
  90.           case CP_IFBLK:
  91. #ifdef CP_IFSOCK
  92.           case CP_IFSOCK:
  93. #endif
  94. #ifdef CP_IFIFO
  95.           case CP_IFIFO:
  96. #endif
  97.         file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj,
  98.                         file_hdr->c_rdev_min);
  99.         rdev = 1;
  100.         break;
  101.           default:
  102.         dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
  103.         rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
  104.         break;
  105.         }
  106.     }
  107. #else
  108.       int dev = 0, rdev = 0;
  109. #endif
  110.  
  111.       if ((file_hdr->c_ino >> 16) != 0)
  112.     error (0, 0, "%s: truncating inode number", file_hdr->c_name);
  113.  
  114.       sprintf (ascii_header,
  115.            "%06lo%06lo%06lo%06lo%06lo%06lo%06lo%06lo%011lo%06lo%011lo",
  116.            file_hdr->c_magic & 0xFFFF, dev & 0xFFFF,
  117.            file_hdr->c_ino & 0xFFFF, file_hdr->c_mode & 0xFFFF,
  118.            file_hdr->c_uid & 0xFFFF, file_hdr->c_gid & 0xFFFF,
  119.            file_hdr->c_nlink & 0xFFFF, rdev & 0xFFFF,
  120.            file_hdr->c_mtime, file_hdr->c_namesize & 0xFFFF,
  121.            file_hdr->c_filesize);
  122.       copy_buf_out (ascii_header, out_des, 76L);
  123.  
  124.       /* Write file name to output.  */
  125.       copy_buf_out (file_hdr->c_name, out_des, (long) file_hdr->c_namesize);
  126.     }
  127.   else if (archive_format == arf_tar || archive_format == arf_ustar)
  128.     {
  129.       write_out_tar_header (file_hdr, out_des);
  130.     }
  131.   else
  132.     {
  133.       struct old_cpio_header short_hdr;
  134.  
  135.       short_hdr.c_magic = 070707;
  136.       short_hdr.c_dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
  137.  
  138.       if ((file_hdr->c_ino >> 16) != 0)
  139.     error (0, 0, "%s: truncating inode number", file_hdr->c_name);
  140.  
  141.       short_hdr.c_ino = file_hdr->c_ino & 0xFFFF;
  142.       short_hdr.c_mode = file_hdr->c_mode & 0xFFFF;
  143.       short_hdr.c_uid = file_hdr->c_uid & 0xFFFF;
  144.       short_hdr.c_gid = file_hdr->c_gid & 0xFFFF;
  145.       short_hdr.c_nlink = file_hdr->c_nlink & 0xFFFF;
  146.       if (archive_format != arf_hpbinary)
  147.     short_hdr.c_rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
  148.       else
  149.     {
  150.       switch (file_hdr->c_mode & CP_IFMT)
  151.         {
  152.           /* HP/UX cpio creates archives that look just like ordinary 
  153.          archives, but for devices it sets major = 0, minor = 1, and 
  154.          puts the actual major/minor number in the filesize field.  */
  155.           case CP_IFCHR:
  156.           case CP_IFBLK:
  157. #ifdef CP_IFSOCK
  158.           case CP_IFSOCK:
  159. #endif
  160. #ifdef CP_IFIFO
  161.           case CP_IFIFO:
  162. #endif
  163.         file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj,
  164.                         file_hdr->c_rdev_min);
  165.         short_hdr.c_rdev = makedev (0, 1);
  166.         break;
  167.           default:
  168.         short_hdr.c_rdev = makedev (file_hdr->c_rdev_maj, 
  169.                         file_hdr->c_rdev_min);
  170.         break;
  171.         }
  172.     }
  173.       short_hdr.c_mtimes[0] = file_hdr->c_mtime >> 16;
  174.       short_hdr.c_mtimes[1] = file_hdr->c_mtime & 0xFFFF;
  175.  
  176.       short_hdr.c_namesize = file_hdr->c_namesize & 0xFFFF;
  177.  
  178.       short_hdr.c_filesizes[0] = file_hdr->c_filesize >> 16;
  179.       short_hdr.c_filesizes[1] = file_hdr->c_filesize & 0xFFFF;
  180.  
  181.       /* Output the file header.  */
  182.       copy_buf_out ((char *) &short_hdr, out_des, 26L);
  183.  
  184.       /* Write file name to output.  */
  185.       copy_buf_out (file_hdr->c_name, out_des, (long) file_hdr->c_namesize);
  186.  
  187.       pad_output (out_des, file_hdr->c_namesize + 26);
  188.     }
  189. }
  190.  
  191. /* Read a list of file names from the standard input
  192.    and write a cpio collection on the standard output.
  193.    The format of the header depends on the compatibility (-c) flag.  */
  194.  
  195. void
  196. process_copy_out ()
  197. {
  198.   int res;            /* Result of functions.  */
  199.   dynamic_string input_name;    /* Name of file read from stdin.  */
  200.   struct utimbuf times;        /* For resetting file times after copy.  */
  201.   struct stat file_stat;    /* Stat record for file.  */
  202.   struct new_cpio_header file_hdr; /* Output header information.  */
  203.   int in_file_des;        /* Source file descriptor.  */
  204.   int out_file_des;        /* Output file descriptor.  */
  205.   char *p;
  206.  
  207.   /* Initialize the copy out.  */
  208.   ds_init (&input_name, 128);
  209.   /* Initialize this in case it has members we don't know to set.  */
  210.   bzero (×, sizeof (struct utimbuf));
  211.   file_hdr.c_magic = 070707;
  212.  
  213. #ifdef __MSDOS__
  214.   setmode (archive_des, O_BINARY);
  215. #endif
  216.   /* Check whether the output file might be a tape.  */
  217.   out_file_des = archive_des;
  218.   if (_isrmt (out_file_des))
  219.     {
  220.       output_is_special = 1;
  221.       output_is_seekable = 0;
  222.     }
  223.   else
  224.     {
  225.       if (fstat (out_file_des, &file_stat))
  226.     error (1, errno, "standard output is closed");
  227.       output_is_special =
  228. #ifdef S_ISBLK
  229.     S_ISBLK (file_stat.st_mode) ||
  230. #endif
  231.     S_ISCHR (file_stat.st_mode);
  232.       output_is_seekable = S_ISREG (file_stat.st_mode);
  233.     }
  234.  
  235.   if (append_flag)
  236.     {
  237.       process_copy_in ();
  238.       prepare_append (out_file_des);
  239.     }
  240.  
  241.   /* Copy files with names read from stdin.  */
  242.   while (ds_fgetstr (stdin, &input_name, name_end) != NULL)
  243.     {
  244.       /* Check for blank line.  */
  245.       if (input_name.ds_string[0] == 0)
  246.     {
  247.       error (0, 0, "blank line ignored");
  248.       continue;
  249.     }
  250.  
  251.       /* Process next file.  */
  252.       if ((*xstat) (input_name.ds_string, &file_stat) < 0)
  253.     error (0, errno, "%s", input_name.ds_string);
  254.       else
  255.     {
  256.       /* Set values in output header.  */
  257.       file_hdr.c_dev_maj = major (file_stat.st_dev);
  258.       file_hdr.c_dev_min = minor (file_stat.st_dev);
  259.       file_hdr.c_ino = file_stat.st_ino;
  260.       /* For POSIX systems that don't define the S_IF macros,
  261.          we can't assume that S_ISfoo means the standard Unix
  262.          S_IFfoo bit(s) are set.  So do it manually, with a
  263.          different name.  Bleah.  */
  264.       file_hdr.c_mode = (file_stat.st_mode & 07777);
  265.       if (S_ISREG (file_stat.st_mode))
  266.         file_hdr.c_mode |= CP_IFREG;
  267.       else if (S_ISDIR (file_stat.st_mode))
  268.         file_hdr.c_mode |= CP_IFDIR;
  269. #ifdef S_ISBLK
  270.       else if (S_ISBLK (file_stat.st_mode))
  271.         file_hdr.c_mode |= CP_IFBLK;
  272. #endif
  273. #ifdef S_ISCHR
  274.       else if (S_ISCHR (file_stat.st_mode))
  275.         file_hdr.c_mode |= CP_IFCHR;
  276. #endif
  277. #ifdef S_ISFIFO
  278.       else if (S_ISFIFO (file_stat.st_mode))
  279.         file_hdr.c_mode |= CP_IFIFO;
  280. #endif
  281. #ifdef S_ISLNK
  282.       else if (S_ISLNK (file_stat.st_mode))
  283.         file_hdr.c_mode |= CP_IFLNK;
  284. #endif
  285. #ifdef S_ISSOCK
  286.       else if (S_ISSOCK (file_stat.st_mode))
  287.         file_hdr.c_mode |= CP_IFSOCK;
  288. #endif
  289. #ifdef S_ISNWK
  290.       else if (S_ISNWK (file_stat.st_mode))
  291.         file_hdr.c_mode |= CP_IFNWK;
  292. #endif
  293.       file_hdr.c_uid = file_stat.st_uid;
  294.       file_hdr.c_gid = file_stat.st_gid;
  295.       file_hdr.c_nlink = file_stat.st_nlink;
  296.       file_hdr.c_rdev_maj = major (file_stat.st_rdev);
  297.       file_hdr.c_rdev_min = minor (file_stat.st_rdev);
  298.       file_hdr.c_mtime = file_stat.st_mtime;
  299.       file_hdr.c_filesize = file_stat.st_size;
  300.       file_hdr.c_chksum = 0;
  301.       file_hdr.c_tar_linkname = NULL;
  302.  
  303.       /* Strip leading `./' from the filename.  */
  304.       p = input_name.ds_string;
  305.       while (*p == '.' && *(p + 1) == '/')
  306.         {
  307.           ++p;
  308.           while (*p == '/')
  309.         ++p;
  310.         }
  311. #ifndef HPUX_CDF
  312.       file_hdr.c_name = p;
  313.       file_hdr.c_namesize = strlen (p) + 1;
  314. #else
  315.       if ( (archive_format != arf_tar) && (archive_format != arf_ustar) )
  316.         {
  317.           /* We mark CDF's in cpio files by adding a 2nd `/' after the
  318.          "hidden" directory name.  We need to do this so we can
  319.          properly recreate the directory as hidden (in case the
  320.          files of a directory go into the archive before the
  321.          directory itself (e.g from "find ... -depth ... | cpio")).  */
  322.           file_hdr.c_name = add_cdf_double_slashes (p);
  323.           file_hdr.c_namesize = strlen (file_hdr.c_name) + 1;
  324.         }
  325.       else
  326.         {
  327.           /* We don't mark CDF's in tar files.  We assume the "hidden"
  328.          directory will always go into the archive before any of
  329.          its files.  */
  330.           file_hdr.c_name = p;
  331.           file_hdr.c_namesize = strlen (p) + 1;
  332.         }
  333. #endif
  334.       if ((archive_format == arf_tar || archive_format == arf_ustar)
  335.           && is_tar_filename_too_long (file_hdr.c_name))
  336.         {
  337.           error (0, 0, "%s: file name too long", file_hdr.c_name);
  338.           continue;
  339.         }
  340.  
  341.       /* Copy the named file to the output.  */
  342.       switch (file_hdr.c_mode & CP_IFMT)
  343.         {
  344.         case CP_IFREG:
  345. #ifndef __MSDOS__
  346.           if (archive_format == arf_tar || archive_format == arf_ustar)
  347.         {
  348.           char *otherfile;
  349.           if ((otherfile = find_inode_file (file_hdr.c_ino,
  350.                             file_hdr.c_dev_maj,
  351.                             file_hdr.c_dev_min)))
  352.             {
  353.               file_hdr.c_tar_linkname = otherfile;
  354.               write_out_header (&file_hdr, out_file_des);
  355.               break;
  356.             }
  357.         }
  358.           if ( (archive_format == arf_newascii || archive_format == arf_crcascii)
  359.           && (file_hdr.c_nlink > 1) )
  360.         {
  361.           if (last_link (&file_hdr) )
  362.             {
  363.               writeout_other_defers (&file_hdr, out_file_des);
  364.             }
  365.           else
  366.             {
  367.               add_link_defer (&file_hdr);
  368.               break;
  369.             }
  370.         }
  371. #endif
  372.           in_file_des = open (input_name.ds_string,
  373.                   O_RDONLY | O_BINARY, 0);
  374.           if (in_file_des < 0)
  375.         {
  376.           error (0, errno, "%s", input_name.ds_string);
  377.           continue;
  378.         }
  379.  
  380.           if (archive_format == arf_crcascii)
  381.         file_hdr.c_chksum = read_for_checksum (in_file_des,
  382.                                file_hdr.c_filesize,
  383.                                input_name.ds_string);
  384.  
  385.           write_out_header (&file_hdr, out_file_des);
  386.           copy_files (in_file_des, out_file_des, file_hdr.c_filesize);
  387.  
  388. #ifndef __MSDOS__
  389.           if (archive_format == arf_tar || archive_format == arf_ustar)
  390.         add_inode (file_hdr.c_ino, file_hdr.c_name, file_hdr.c_dev_maj,
  391.                file_hdr.c_dev_min);
  392. #endif
  393.  
  394.           pad_output (out_file_des, file_hdr.c_filesize);
  395.  
  396.           if (close (in_file_des) < 0)
  397.         error (0, errno, "%s", input_name.ds_string);
  398.           if (reset_time_flag)
  399.         {
  400.           times.actime = file_stat.st_atime;
  401.           times.modtime = file_stat.st_mtime;
  402.           if (utime (file_hdr.c_name, ×) < 0)
  403.             error (0, errno, "%s", file_hdr.c_name);
  404.         }
  405.           break;
  406.  
  407.         case CP_IFDIR:
  408.           file_hdr.c_filesize = 0;
  409.           write_out_header (&file_hdr, out_file_des);
  410.           break;
  411.  
  412. #ifndef __MSDOS__
  413.         case CP_IFCHR:
  414.         case CP_IFBLK:
  415. #ifdef CP_IFSOCK
  416.         case CP_IFSOCK:
  417. #endif
  418. #ifdef CP_IFIFO
  419.         case CP_IFIFO:
  420. #endif
  421.           if (archive_format == arf_tar)
  422.         {
  423.           error (0, 0, "%s not dumped: not a regular file",
  424.              file_hdr.c_name);
  425.           continue;
  426.         }
  427.           else if (archive_format == arf_ustar)
  428.         {
  429.           char *otherfile;
  430.           if ((otherfile = find_inode_file (file_hdr.c_ino,
  431.                             file_hdr.c_dev_maj,
  432.                             file_hdr.c_dev_min)))
  433.             {
  434.               /* This file is linked to another file already in the 
  435.                  archive, so write it out as a hard link. */
  436.               file_hdr.c_mode = (file_stat.st_mode & 07777);
  437.               file_hdr.c_mode |= CP_IFREG;
  438.               file_hdr.c_tar_linkname = otherfile;
  439.               write_out_header (&file_hdr, out_file_des);
  440.               break;
  441.             }
  442.           add_inode (file_hdr.c_ino, file_hdr.c_name, 
  443.                  file_hdr.c_dev_maj, file_hdr.c_dev_min);
  444.         }
  445.           file_hdr.c_filesize = 0;
  446.           write_out_header (&file_hdr, out_file_des);
  447.           break;
  448. #endif
  449.  
  450. #ifdef CP_IFLNK
  451.         case CP_IFLNK:
  452.           {
  453.         char *link_name = (char *) xmalloc (file_stat.st_size + 1);
  454.  
  455.         if (readlink (input_name.ds_string, link_name,
  456.                   file_stat.st_size) < 0)
  457.           {
  458.             error (0, errno, "%s", input_name.ds_string);
  459.             free (link_name);
  460.             continue;
  461.           }
  462.         if (archive_format == arf_tar || archive_format == arf_ustar)
  463.           {
  464.             if (file_stat.st_size + 1 > 100)
  465.               {
  466.             error (0, 0, "%s: symbolic link too long",
  467.                    file_hdr.c_name);
  468.               }
  469.             else
  470.               {
  471.             link_name[file_stat.st_size] = '\0';
  472.             file_hdr.c_tar_linkname = link_name;
  473.             write_out_header (&file_hdr, out_file_des);
  474.               }
  475.           }
  476.         else
  477.           {
  478.             write_out_header (&file_hdr, out_file_des);
  479.             copy_buf_out (link_name, out_file_des, file_stat.st_size);
  480.             pad_output (out_file_des, file_hdr.c_filesize);
  481.           }
  482.         free (link_name);
  483.           }
  484.           break;
  485. #endif
  486.  
  487.         default:
  488.           error (0, 0, "%s: unknown file type", input_name.ds_string);
  489.         }
  490.  
  491.       if (verbose_flag)
  492.         fprintf (stderr, "%s\n", input_name.ds_string);
  493.       if (dot_flag)
  494.         fputc ('.', stderr);
  495.     }
  496.     }
  497.  
  498.   writeout_final_defers(out_file_des);
  499.   /* The collection is complete; append the trailer.  */
  500.   file_hdr.c_ino = 0;
  501.   file_hdr.c_mode = 0;
  502.   file_hdr.c_uid = 0;
  503.   file_hdr.c_gid = 0;
  504.   file_hdr.c_nlink = 1;        /* Must be 1 for crc format.  */
  505.   file_hdr.c_dev_maj = 0;
  506.   file_hdr.c_dev_min = 0;
  507.   file_hdr.c_rdev_maj = 0;
  508.   file_hdr.c_rdev_min = 0;
  509.   file_hdr.c_mtime = 0;
  510.   file_hdr.c_chksum = 0;
  511.  
  512.   file_hdr.c_filesize = 0;
  513.   file_hdr.c_namesize = 11;
  514.   file_hdr.c_name = "TRAILER!!!";
  515.   if (archive_format != arf_tar && archive_format != arf_ustar)
  516.     write_out_header (&file_hdr, out_file_des);
  517.   else
  518.     {
  519.       copy_buf_out (zeros_512, out_file_des, 512);
  520.       copy_buf_out (zeros_512, out_file_des, 512);
  521.     }
  522.  
  523.   /* Fill up the output block.  */
  524.   clear_rest_of_block (out_file_des);
  525.   empty_output_buffer (out_file_des);
  526.   if (dot_flag)
  527.     fputc ('\n', stderr);
  528.   res = (output_bytes + io_block_size - 1) / io_block_size;
  529.   if (res == 1)
  530.     fprintf (stderr, "1 block\n");
  531.   else
  532.     fprintf (stderr, "%d blocks\n", res);
  533. }
  534.  
  535. /* Read FILE_SIZE bytes of FILE_NAME from IN_FILE_DES and
  536.    compute and return a checksum for them.  */
  537.  
  538. static unsigned long
  539. read_for_checksum (in_file_des, file_size, file_name)
  540.      int in_file_des;
  541.      int file_size;
  542.      char *file_name;
  543. {
  544.   unsigned long crc;
  545.   char buf[BUFSIZ];
  546.   int bytes_left;
  547.   int bytes_read;
  548.   int i;
  549.  
  550.   crc = 0;
  551.  
  552.   for (bytes_left = file_size; bytes_left > 0; bytes_left -= bytes_read)
  553.     {
  554.       bytes_read = read (in_file_des, buf, BUFSIZ);
  555.       if (bytes_read < 0)
  556.     error (1, errno, "cannot read checksum for %s", file_name);
  557.       if (bytes_read == 0)
  558.     break;
  559.       for (i = 0; i < bytes_read; ++i)
  560.     crc += buf[i] & 0xff;
  561.     }
  562.   if (lseek (in_file_des, 0L, SEEK_SET))
  563.     error (1, errno, "cannot read checksum for %s", file_name);
  564.  
  565.   return crc;
  566. }
  567.  
  568. /* Write out NULs to fill out the rest of the current block on
  569.    OUT_FILE_DES.  */
  570.  
  571. static void
  572. clear_rest_of_block (out_file_des)
  573.      int out_file_des;
  574. {
  575.   while (output_size < io_block_size)
  576.     {
  577.       if ((io_block_size - output_size) > 512)
  578.     copy_buf_out (zeros_512, out_file_des, 512);
  579.       else
  580.     copy_buf_out (zeros_512, out_file_des, io_block_size - output_size);
  581.     }
  582. }
  583.  
  584. /* Write NULs on OUT_FILE_DES to move from OFFSET (the current location)
  585.    to the end of the header.  */
  586.  
  587. static void
  588. pad_output (out_file_des, offset)
  589.      int out_file_des;
  590.      int offset;
  591. {
  592.   int pad;
  593.  
  594.   if (archive_format == arf_newascii || archive_format == arf_crcascii)
  595.     pad = (4 - (offset % 4)) % 4;
  596.   else if (archive_format == arf_tar || archive_format == arf_ustar)
  597.     pad = (512 - (offset % 512)) % 512;
  598.   else if (archive_format != arf_oldascii && archive_format != arf_hpoldascii)
  599.     pad = (2 - (offset % 2)) % 2;
  600.   else
  601.     pad = 0;
  602.  
  603.   if (pad != 0)
  604.     copy_buf_out (zeros_512, out_file_des, pad);
  605. }
  606.  
  607.  
  608. /* When creating newc and crc archives if a file has multiple (hard)
  609.    links, we don't put any of them into the archive until we have seen
  610.    all of them (or until we get to the end of the list of files that
  611.    are going into the archive and know that we have seen all of the links
  612.    to the file that we will see).  We keep these "defered" files on
  613.    this list.   */
  614.  
  615. struct deferment *deferouts = NULL;
  616.  
  617.  
  618. /* Is this file_hdr the last (hard) link to a file?  I.e., have
  619.    we already seen and defered all of the other links?  */
  620.  
  621. static int
  622. last_link (file_hdr)
  623.   struct new_cpio_header *file_hdr;
  624. {
  625.   int    other_files_sofar;
  626.  
  627.   other_files_sofar = count_defered_links_to_dev_ino (file_hdr);
  628.   if (file_hdr->c_nlink == (other_files_sofar + 1) )
  629.     {
  630.       return 1;
  631.     }
  632.   return 0;
  633. }
  634.  
  635. /* Count the number of other (hard) links to this file that have
  636.    already been defered.  */
  637.  
  638. static int
  639. count_defered_links_to_dev_ino (file_hdr)
  640.   struct new_cpio_header *file_hdr;
  641. {
  642.   struct deferment *d;
  643.   int    ino;
  644.   int     maj;
  645.   int   min;
  646.   int     count;
  647.   ino = file_hdr->c_ino;
  648.   maj = file_hdr->c_dev_maj;
  649.   min = file_hdr->c_dev_min;
  650.   count = 0;
  651.   for (d = deferouts; d != NULL; d = d->next)
  652.     {
  653.       if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
  654.       && (d->header.c_dev_min == min) )
  655.     ++count;
  656.     }
  657.   return count;
  658. }
  659.  
  660. /* Add the file header for a link that is being defered to the deferouts
  661.    list.  */
  662.  
  663. static void
  664. add_link_defer (file_hdr)
  665.   struct new_cpio_header *file_hdr;
  666. {
  667.   struct deferment *d;
  668.   d = create_deferment (file_hdr);
  669.   d->next = deferouts;
  670.   deferouts = d;
  671. }
  672.  
  673. /* We are about to put a file into a newc or crc archive that is
  674.    multiply linked.  We have already seen and defered all of the
  675.    other links to the file but haven't written them into the archive.
  676.    Write the other links into the archive, and remove them from the
  677.    deferouts list.  */
  678.  
  679. static void
  680. writeout_other_defers (file_hdr, out_des)
  681.   struct new_cpio_header *file_hdr;
  682.   int out_des;
  683. {
  684.   struct deferment *d;
  685.   struct deferment *d_prev;
  686.   int    ino;
  687.   int     maj;
  688.   int   min;
  689.   int     count;
  690.   ino = file_hdr->c_ino;
  691.   maj = file_hdr->c_dev_maj;
  692.   min = file_hdr->c_dev_min;
  693.   d_prev = NULL;
  694.   d = deferouts;
  695.   while (d != NULL)
  696.     {
  697.       if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
  698.       && (d->header.c_dev_min == min) )
  699.     {
  700.       struct deferment *d_free;
  701.       d->header.c_filesize = 0;
  702.       write_out_header (&d->header, out_des);
  703.       if (d_prev != NULL)
  704.         d_prev->next = d->next;
  705.       else
  706.         deferouts = d->next;
  707.       d_free = d;
  708.       d = d->next;
  709.       free_deferment (d_free);
  710.     }
  711.       else
  712.     {
  713.       d_prev = d;
  714.       d = d->next;
  715.     }
  716.     }
  717.   return;
  718. }
  719. /* When writing newc and crc format archives we defer multiply linked
  720.    files until we have seen all of the links to the file.  If a file
  721.    has links to it that aren't going into the archive, then we will
  722.    never see the "last" link to the file, so at the end we just write 
  723.    all of the leftover defered files into the archive.  */
  724.  
  725. static void
  726. writeout_final_defers(out_des)
  727.   int    out_des;
  728. {
  729.   struct deferment *d;
  730.   int other_count;
  731.   while (deferouts != NULL)
  732.     {
  733.       d = deferouts;
  734.       other_count = count_defered_links_to_dev_ino (&d->header);
  735.       if (other_count == 1)
  736.     {
  737.       writeout_defered_file (&d->header, out_des);
  738.     }
  739.       else
  740.     {
  741.       struct new_cpio_header file_hdr;
  742.       file_hdr = d->header;
  743.       file_hdr.c_filesize = 0;
  744.       write_out_header (&file_hdr, out_des);
  745.     }
  746.       deferouts = deferouts->next;
  747.     }
  748. }
  749.  
  750. /* Write a file into the archive.  This code is the same as
  751.    the code in process_copy_out(), but we need it here too
  752.    for writeout_final_defers() to call.  */
  753.  
  754. static void
  755. writeout_defered_file (header, out_file_des)
  756.   struct new_cpio_header *header;
  757. {
  758.   int in_file_des;
  759.   struct new_cpio_header file_hdr;
  760.   struct utimbuf times;        /* For setting file times.  */
  761.   /* Initialize this in case it has members we don't know to set.  */
  762.   bzero (×, sizeof (struct utimbuf));
  763.  
  764.   file_hdr = *header;
  765.  
  766.  
  767.   in_file_des = open (header->c_name,
  768.               O_RDONLY | O_BINARY, 0);
  769.   if (in_file_des < 0)
  770.     {
  771.       error (0, errno, "%s", header->c_name);
  772.       return;
  773.     }
  774.  
  775.   if (archive_format == arf_crcascii)
  776.     file_hdr.c_chksum = read_for_checksum (in_file_des,
  777.                        file_hdr.c_filesize,
  778.                        header->c_name);
  779.  
  780.   write_out_header (&file_hdr, out_file_des);
  781.   copy_files (in_file_des, out_file_des, file_hdr.c_filesize);
  782.  
  783. #ifndef __MSDOS__
  784.   if (archive_format == arf_tar || archive_format == arf_ustar)
  785.     add_inode (file_hdr.c_ino, file_hdr.c_name, file_hdr.c_dev_maj,
  786.            file_hdr.c_dev_min);
  787. #endif
  788.  
  789.   pad_output (out_file_des, file_hdr.c_filesize);
  790.  
  791.   if (close (in_file_des) < 0)
  792.     error (0, errno, "%s", header->c_name);
  793.   if (reset_time_flag)
  794.     {
  795.       times.actime = file_hdr.c_mtime;
  796.       times.modtime = file_hdr.c_mtime;
  797.       if (utime (file_hdr.c_name, ×) < 0)
  798.     error (0, errno, "%s", file_hdr.c_name);
  799.     }
  800.   return;
  801. }
  802.