home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / src / create.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  34KB  |  1,381 lines

  1. /* Create a tar archive.
  2.    Copyright (C) 1985, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GNU Tar.
  5.  
  6.    GNU Tar is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GNU Tar is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GNU Tar; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Create a tar archive.
  21.    Written 25 Aug 1985 by John Gilmore.  */
  22.  
  23. #include "system.h"
  24.  
  25. #ifndef    __MSDOS__
  26. #include <pwd.h>
  27. #include <grp.h>
  28. #endif
  29.  
  30. #ifdef HAVE_UTIME_H
  31. #include <utime.h>
  32. #else
  33. struct utimbuf
  34.   {
  35.     long actime;
  36.     long modtime;
  37.   };
  38. #endif
  39.  
  40. #include "tar.h"
  41.  
  42. extern struct stat hstat;    /* stat struct corresponding */
  43.  
  44. #ifndef __MSDOS__
  45. extern dev_t ar_dev;
  46. extern ino_t ar_ino;
  47. #endif
  48.  
  49. /* JF */
  50. extern struct name *gnu_list_name;
  51.  
  52. /* If there are no symbolic links, there is no lstat().  Use stat().  */
  53. #ifndef S_ISLNK
  54. #define lstat stat
  55. #endif
  56.  
  57. /* This module is the only one that cares about `struct link's.  */
  58.  
  59. struct link
  60.   {
  61.     struct link *next;
  62.     dev_t dev;
  63.     ino_t ino;
  64.     short linkcount;
  65.     char name[1];
  66.   };
  67.  
  68. struct link *linklist;        /* points to first link in list */
  69.  
  70.  
  71. /*-------------------------------------------------------------------------.
  72. | Quick and dirty octal conversion.  Converts long "value" into a       |
  73. | "digs"-digit field at "where", including a trailing space and room for a |
  74. | null.  "digs"==3 means 1 digit, a space, and room for a null.           |
  75. |                                        |
  76. | We assume the trailing null is already there and don't fill it in.  This |
  77. | fact is used by start_header and finish_header, so don't change it!       |
  78. `-------------------------------------------------------------------------*/
  79.  
  80. /* This should be equivalent to:
  81.     sprintf (where, "%*lo ", digs-2, value);
  82.    except that sprintf fills in the trailing null and we don't.  */
  83.  
  84. void
  85. to_oct (register long value, register int digs, register char *where)
  86. {
  87.   --digs;            /* Trailing null slot is left alone */
  88.   where[--digs] = ' ';        /* put in the space, though */
  89.  
  90.   /* Produce the digits -- at least one.  */
  91.  
  92.   do
  93.     {
  94.       where[--digs] = '0' + (char) (value & 7);    /* one octal digit */
  95.       value >>= 3;
  96.     }
  97.   while (digs > 0 && value != 0);
  98.  
  99.   /* Leading spaces, if necessary.  */
  100.   while (digs > 0)
  101.     where[--digs] = ' ';
  102. }
  103.  
  104. /* Writing routines.  */
  105.  
  106. /*-----------------------------------------------------------------------.
  107. | Just zeroes out the buffer so we don't confuse ourselves with leftover |
  108. | data.                                     |
  109. `-----------------------------------------------------------------------*/
  110.  
  111. static void
  112. clear_buffer (char *buf)
  113. {
  114.   register int i;
  115.  
  116.   for (i = 0; i < RECORDSIZE; i++)
  117.     buf[i] = '\0';
  118. }
  119.  
  120. /*------------------------------------------------------------------------.
  121. | Write the EOT record(s).  We actually zero at least one record, through |
  122. | the end of the block.  Old tar writes garbage after two zeroed records  |
  123. | -- and PDtar used to.                              |
  124. `------------------------------------------------------------------------*/
  125.  
  126. void
  127. write_eot (void)
  128. {
  129.   union record *p;
  130.   int bufsize;
  131.  
  132.   p = findrec ();
  133.   if (p)
  134.     {
  135.       bufsize = endofrecs ()->charptr - p->charptr;
  136.       memset (p->charptr, 0, (size_t) bufsize);
  137.       userec (p);
  138.     }
  139. }
  140.  
  141. /*--------------------------------------------.
  142. | Write a LF_LONGLINK or LF_LONGNAME record.  |
  143. `--------------------------------------------*/
  144.  
  145. /* FIXME: Cross recursion between start_header and write_long!  */
  146.  
  147. static union record *start_header __P ((const char *, register struct stat *));
  148. #if 0
  149. static void write_long (const char *, char);
  150. #endif
  151.  
  152. static void
  153. write_long (const char *p, char type)
  154. {
  155.   int size = strlen (p) + 1;
  156.   int bufsize;
  157.   union record *header;
  158.   struct stat foo;
  159.  
  160.   memset (&foo, 0, sizeof foo);
  161.   foo.st_size = size;
  162.  
  163.   header = start_header ("././@LongLink", &foo);
  164.   header->header.linkflag = type;
  165.   finish_header (header);
  166.  
  167.   header = findrec ();
  168.  
  169.   bufsize = endofrecs ()->charptr - header->charptr;
  170.  
  171.   while (bufsize < size)
  172.     {
  173.       memcpy (header->charptr, p, (size_t) bufsize);
  174.       p += bufsize;
  175.       size -= bufsize;
  176.       userec (header + (bufsize - 1) / RECORDSIZE);
  177.       header = findrec ();
  178.       bufsize = endofrecs ()->charptr - header->charptr;
  179.     }
  180.   memcpy (header->charptr, p, (size_t) size);
  181.   memset (header->charptr + size, 0, (size_t) (bufsize - size));
  182.   userec (header + (size - 1) / RECORDSIZE);
  183. }
  184.  
  185. /* Header handling.  */
  186.  
  187. /*---------------------------------------------------------------------.
  188. | Make a header block for the file name whose stat info is st.  Return |
  189. | header pointer for success, NULL if the name is too long.           |
  190. `---------------------------------------------------------------------*/
  191.  
  192. static union record *
  193. start_header (const char *name, register struct stat *st)
  194. {
  195.   register union record *header;
  196.  
  197.   if (strlen (name) >= (size_t) NAMSIZ)
  198.     write_long (name, LF_LONGNAME);
  199.  
  200.   header = (union record *) findrec ();
  201.   memset (header->charptr, 0, sizeof (*header));    /* FIXME: speed up */
  202.  
  203.   /* Check the file name and put it in the record.  */
  204.  
  205.   if (!flag_absolute_names)
  206.     {
  207.       static int warned_once = 0;
  208.  
  209. #ifdef __MSDOS__
  210.       if (name[1] == ':')
  211.     {
  212.       name += 2;
  213.       if (!warned_once++)
  214.         WARN ((0, 0, _("Removing drive spec from names in the archive")));
  215.     }
  216. #endif
  217.  
  218.       while (*name == '/')
  219.     {
  220.       name++;        /* force relative path */
  221.       if (!warned_once++)
  222.         WARN ((0, 0, _("\
  223. Removing leading / from absolute path names in the archive")));
  224.     }
  225.     }
  226.  
  227.   assign_string (¤t_file_name, name);
  228.  
  229.   strncpy (header->header.arch_name, name, NAMSIZ);
  230.   header->header.arch_name[NAMSIZ - 1] = '\0';
  231.  
  232.   /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
  233.      for a few tars and came up with the following interoperability
  234.      matrix:
  235.  
  236.           WRITER
  237.     1 2 3 4 5 6 7 8 9   READER
  238.     . . . . . . . . .   1 = SunOS 4.2 tar
  239.     # . . # # . . # #   2 = NEC SVR4.0.2 tar
  240.     . . . # # . . # .   3 = Solaris 2.1 tar
  241.     . . . . . . . . .   4 = GNU tar 1.11.1
  242.     . . . . . . . . .   5 = HP-UX 8.07 tar
  243.     . . . . . . . . .   6 = Ultrix 4.1
  244.     . . . . . . . . .   7 = AIX 3.2
  245.     . . . . . . . . .   8 = Hitachi HI-UX 1.03
  246.     . . . . . . . . .   9 = Omron UNIOS-B 4.3BSD 1.60Beta
  247.  
  248.          . = works
  249.          # = ``impossible file type''
  250.  
  251.      The following mask for old archive removes the `#'s in column 4
  252.      above, thus making GNU tar both a universal donor and a universal
  253.      acceptor for Paul's test.  */
  254.  
  255.   to_oct ((long) (flag_oldarch ? (st->st_mode & 07777) : st->st_mode),
  256.       8, header->header.mode);
  257.  
  258.   to_oct ((long) st->st_uid, 8, header->header.uid);
  259.   to_oct ((long) st->st_gid, 8, header->header.gid);
  260.   to_oct ((long) st->st_size, 1 + 12, header->header.size);
  261.   to_oct ((long) st->st_mtime, 1 + 12, header->header.mtime);
  262.  
  263.   /* header->header.linkflag is left as null.  */
  264.  
  265.   if (flag_gnudump)
  266.     {
  267.       to_oct ((long) st->st_atime, 1 + 12, header->header.atime);
  268.       to_oct ((long) st->st_ctime, 1 + 12, header->header.ctime);
  269.     }
  270.  
  271. #ifndef NONAMES
  272.   /* Fill in new Unix Standard fields if desired.  */
  273.  
  274.   if (flag_standard)
  275.     {
  276.       header->header.linkflag = LF_NORMAL;    /* new default */
  277.       strcpy (header->header.magic, TMAGIC);    /* mark as Unix Std */
  278.       finduname (header->header.uname, st->st_uid);
  279.       findgname (header->header.gname, st->st_gid);
  280.     }
  281. #endif
  282.  
  283.   return header;
  284. }
  285.  
  286. /*-------------------------------------------------------------------------.
  287. | Finish off a filled-in header block and write it out.  We also print the |
  288. | file name and/or full info if verbose is on.                   |
  289. `-------------------------------------------------------------------------*/
  290.  
  291. void
  292. finish_header (register union record *header)
  293. {
  294.   register int i, sum;
  295.   register char *p;
  296.  
  297.   memcpy (header->header.chksum, CHKBLANKS, sizeof (header->header.chksum));
  298.  
  299.   sum = 0;
  300.   p = header->charptr;
  301.   for (i = sizeof (*header); --i >= 0; )
  302.     /* We can't use unsigned char here because of old compilers, e.g. V7.  */
  303.     sum += 0xFF & *p++;
  304.  
  305.   /* Fill in the checksum field.  It's formatted differently from the
  306.      other fields: it has [6] digits, a null, then a space -- rather than
  307.      digits, a space, then a null.  We use to_oct then write the null in
  308.      over to_oct's space.  The final space is already there, from
  309.      checksumming, and to_oct doesn't modify it.
  310.  
  311.      This is a fast way to do:
  312.  
  313.      sprintf(header->header.chksum, "%6o", sum);  */
  314.  
  315.   to_oct ((long) sum, 8, header->header.chksum);
  316.   header->header.chksum[6] = '\0';    /* zap the space */
  317.  
  318.   userec (header);
  319.  
  320.   if (flag_verbose)
  321.     {
  322.  
  323.       /* These globals are parameters to print_header, sigh.  */
  324.  
  325.       head = header;
  326.       /* hstat is already set up.  */
  327.       head_standard = flag_standard;
  328.       print_header ();
  329.     }
  330.  
  331.   return;
  332. }
  333.  
  334. /* Sparse file processing.  */
  335.  
  336. /*---------------------------------------------------------------------.
  337. | Takes a recordful of data and basically cruises through it to see if |
  338. | it's made *entirely* of zeros, returning a 0 the instant it finds    |
  339. | something that is a non-zero, i.e., useful data.               |
  340. `---------------------------------------------------------------------*/
  341.  
  342. static int
  343. zero_record (char *buffer)
  344. {
  345.   register int i;
  346.  
  347.   for (i = 0; i < RECORDSIZE; i++)
  348.     if (buffer[i] != '\0')
  349.       return 0;
  350.   return 1;
  351. }
  352.  
  353. /*---.
  354. | ?  |
  355. `---*/
  356.  
  357. static void
  358. init_sparsearray (void)
  359. {
  360.   register int i;
  361.  
  362.   sp_array_size = 10;
  363.  
  364.   /* Make room for our scratch space -- initially is 10 elts long.  */
  365.  
  366.   sparsearray = (struct sp_array *)
  367.     xmalloc (sp_array_size * sizeof (struct sp_array));
  368.   for (i = 0; i < sp_array_size; i++)
  369.     {
  370.       sparsearray[i].offset = 0;
  371.       sparsearray[i].numbytes = 0;
  372.     }
  373. }
  374.  
  375. /*---.
  376. | ?  |
  377. `---*/
  378.  
  379. static void
  380. find_new_file_size (int *filesize, int highest_index)
  381. {
  382.   register int i;
  383.  
  384.   *filesize = 0;
  385.   for (i = 0; sparsearray[i].numbytes && i <= highest_index; i++)
  386.     *filesize += sparsearray[i].numbytes;
  387. }
  388.  
  389. /*-------------------------------------------------------------------------.
  390. | Okay, we've got a sparse file on our hands -- now, what we need to do is |
  391. | make a pass through the file and carefully note where any data is, i.e., |
  392. | we want to find how far into the file each instance of data is, and how  |
  393. | many bytes are there.  We store this information in the sparsearray,       |
  394. | which will later be translated into header information.  For now, we use |
  395. | the sparsearray as convenient storage.                   |
  396. `-------------------------------------------------------------------------*/
  397.  
  398. /* As a side note, this routine is a mess.  If I could have found a
  399.    cleaner way to do it, I would have.  If anyone wants to find a nicer
  400.    way to do this, feel free.
  401.  
  402.    There is little point in trimming small amounts of null data at the
  403.    head and tail of blocks -- it's ok if we only avoid dumping blocks of
  404.    complete null data.  */
  405.  
  406. static int
  407. deal_with_sparse (char *name, union record *header)
  408. {
  409.   long numbytes = 0;
  410.   long offset = 0;
  411. #if 0
  412.   long save_offset;
  413. #endif
  414.   int fd;
  415. #if 0
  416.   int current_size = hstat.st_size;
  417. #endif
  418.   int sparse_ind = 0, cc;
  419.   char buf[RECORDSIZE];
  420. #if 0
  421.   int read_last_data = 0;    /* did we just read the last record? */
  422. #endif
  423.   int amidst_data = 0;
  424.  
  425.   header->header.isextended = 0;
  426.  
  427.   /* Cannot open the file -- this problem will be caught later on, so just
  428.      return.  */
  429.  
  430.   if (fd = open (name, O_RDONLY), fd < 0)
  431.     return 0;
  432.  
  433.   init_sparsearray ();
  434.   clear_buffer (buf);
  435.  
  436.   while (cc = read (fd, buf, sizeof buf), cc != 0)
  437.     {
  438.       if (sparse_ind > sp_array_size - 1)
  439.     {
  440.  
  441.       /* Realloc the scratch area, since we've run out of room.  */
  442.  
  443.       sparsearray = (struct sp_array *)
  444.         xrealloc (sparsearray,
  445.               2 * sp_array_size * sizeof (struct sp_array));
  446.       sp_array_size *= 2;
  447.     }
  448.       if (cc == sizeof buf)
  449.     {
  450.       if (zero_record (buf))
  451.         {
  452.           if (amidst_data)
  453.         {
  454.           sparsearray[sparse_ind++].numbytes = numbytes;
  455.           amidst_data = 0;
  456.         }
  457.         }
  458.       else
  459.         if (amidst_data)
  460.           numbytes += cc;
  461.         else
  462.           {
  463.         amidst_data = 1;
  464.         numbytes = cc;
  465.         sparsearray[sparse_ind].offset = offset;
  466.           }
  467.     }
  468.       else if (cc < sizeof buf)
  469.     {
  470.  
  471.       /* This has to be the last bit of the file, so this is somewhat
  472.          shorter than the above.  */
  473.  
  474.       if (!zero_record (buf))
  475.         {
  476.           if (amidst_data)
  477.         numbytes += cc;
  478.           else
  479.         {
  480.           amidst_data = 1;
  481.           numbytes = cc;
  482.           sparsearray[sparse_ind].offset = offset;
  483.         }
  484.         }
  485.     }
  486.       offset += cc;
  487.       clear_buffer (buf);
  488.     }
  489.   if (amidst_data)
  490.     sparsearray[sparse_ind++].numbytes = numbytes;
  491.   else
  492.     {
  493.       sparsearray[sparse_ind].offset = offset - 1;
  494.       sparsearray[sparse_ind++].numbytes = 1;
  495.     }
  496.   close (fd);
  497.  
  498.   return sparse_ind - 1;
  499. }
  500.  
  501. /*---.
  502. | ?  |
  503. `---*/
  504.  
  505. static int
  506. finish_sparse_file (int fd, long *sizeleft, long fullsize, char *name)
  507. {
  508.   union record *start;
  509.   char tempbuf[RECORDSIZE];
  510.   int bufsize, sparse_ind = 0, count;
  511.   long pos;
  512.   long nwritten = 0;
  513.  
  514.   while (*sizeleft > 0)
  515.     {
  516.       start = findrec ();
  517.       memset (start->charptr, 0, RECORDSIZE);
  518.       bufsize = sparsearray[sparse_ind].numbytes;
  519.       if (!bufsize)
  520.     {
  521.  
  522.       /* We blew it, maybe.  */
  523.  
  524.       ERROR ((0, 0, _("Wrote %ld of %ld bytes to file %s"),
  525.           fullsize - *sizeleft, fullsize, name));
  526.       break;
  527.     }
  528.       pos = lseek (fd, sparsearray[sparse_ind++].offset, 0);
  529.  
  530.       /* If the number of bytes to be written here exceeds the size of
  531.      the temporary buffer, do it in steps.  */
  532.  
  533.       while (bufsize > RECORDSIZE)
  534.     {
  535. #if 0
  536.       if (amt_read)
  537.         {
  538.           count = read (fd, start->charptr+amt_read, RECORDSIZE-amt_read);
  539.           bufsize -= RECORDSIZE - amt_read;
  540.           amt_read = 0;
  541.           userec (start);
  542.           start = findrec ();
  543.           memset (start->charptr, 0, RECORDSIZE);
  544.         }
  545. #endif
  546.       /* Store the data.  */
  547.  
  548.       count = read (fd, start->charptr, RECORDSIZE);
  549.       if (count < 0)
  550.         {
  551.           ERROR ((0, errno, _("\
  552. Read error at byte %ld, reading %d bytes, in file %s"),
  553.              fullsize - *sizeleft, bufsize, name));
  554.           return 1;
  555.         }
  556.       bufsize -= count;
  557.       *sizeleft -= count;
  558.       userec (start);
  559.       nwritten += RECORDSIZE;    /* FIXME */
  560.       start = findrec ();
  561.       memset (start->charptr, 0, RECORDSIZE);
  562.     }
  563.  
  564.       clear_buffer (tempbuf);
  565.       count = read (fd, tempbuf, (size_t) bufsize);
  566.       memcpy (start->charptr, tempbuf, RECORDSIZE);
  567.       if (count < 0)
  568.     {
  569.       ERROR ((0, errno,
  570.           _("Read error at byte %ld, reading %d bytes, in file %s"),
  571.           fullsize - *sizeleft, bufsize, name));
  572.       return 1;
  573.     }
  574. #if 0
  575.       if (amt_read >= RECORDSIZE)
  576.     {
  577.       amt_read = 0;
  578.       userec (start + (count - 1) / RECORDSIZE);
  579.       if (count != bufsize)
  580.         {
  581.           ERROR ((0, 0,
  582.               _("File %s shrunk by %d bytes, padding with zeros"),
  583.               name, sizeleft));
  584.           return 1;
  585.         }
  586.       start = findrec ();
  587.     }
  588.       else
  589.     amt_read += bufsize;
  590. #endif
  591.       nwritten += count;    /* FIXME */
  592.       *sizeleft -= count;
  593.       userec (start);
  594.  
  595.     }
  596.   free (sparsearray);
  597. #if 0
  598.   printf (_("Amount actually written is (I hope) %d.\n"), nwritten);
  599.   userec (start + (count - 1) / RECORDSIZE);
  600. #endif
  601.   return 0;
  602. }
  603.  
  604. /* Main functions of this module.  */
  605.  
  606. /*---.
  607. | ?  |
  608. `---*/
  609.  
  610. void
  611. create_archive (void)
  612. {
  613.   register char *p;
  614.  
  615.   open_archive (0);        /* open for writing */
  616.  
  617.   if (flag_gnudump)
  618.     {
  619.       char *buf = xmalloc (PATH_MAX);
  620.       char *q, *bufp;
  621.  
  622.       collect_and_sort_names ();
  623.  
  624.       while (p = name_from_list (), p)
  625.     dump_file (p, -1, 1);
  626. #if 0
  627.       if (!flag_dironly)
  628.     {
  629. #endif
  630.       blank_name_list ();
  631.       while (p = name_from_list (), p)
  632.         {
  633.           strcpy (buf, p);
  634.           if (p[strlen (p) - 1] != '/')
  635.         strcat (buf, "/");
  636.           bufp = buf + strlen (buf);
  637.           for (q = gnu_list_name->dir_contents;
  638.            q && *q;
  639.            q += strlen (q) + 1)
  640.         {
  641.           if (*q == 'Y')
  642.             {
  643.               strcpy (bufp, q + 1);
  644.               dump_file (buf, -1, 1);
  645.             }
  646.         }
  647.         }
  648. #if 0
  649.     }
  650. #endif
  651.       free (buf);
  652.     }
  653.   else
  654.     {
  655.       while (p = name_next (1), p)
  656.     dump_file (p, -1, 1);
  657.     }
  658.  
  659.   write_eot ();
  660.   close_archive ();
  661.   if (flag_gnudump && gnu_dumpfile)
  662.     write_dir_file ();
  663.   name_close ();
  664. }
  665.  
  666. /*-------------------------------------------------------------------------.
  667. | Dump a single file.  If it's a directory, recurse.  Result is 1 for       |
  668. | success, 0 for failure.  Sets global "hstat" to stat() output for this   |
  669. | file.  P is file name to dump.  CURDEV is device our parent dir was on.  |
  670. | TOPLEVEL tells wether we are a toplevel call.                   |
  671. `-------------------------------------------------------------------------*/
  672.  
  673. void
  674. dump_file (char *p, int curdev, int toplevel)
  675. {
  676.   union record *header;
  677.   char type;
  678.   union record *exhdr;
  679.   char save_linkflag;
  680.   int critical_error = 0;
  681.   struct utimbuf restore_times;
  682. #if 0
  683.   int sparse_ind = 0;
  684. #endif
  685.  
  686.   /* FIXME: `header' might be used uninitialized in this function.
  687.      Reported by Bruno Haible.  */
  688.  
  689.   /* FIXME: `upperbound' might be used uninitialized in this function.
  690.      Reported by Bruno Haible.  */
  691.  
  692.   if (flag_confirm && !confirm ("add", p))
  693.     return;
  694.  
  695.   /* Use stat if following (rather than dumping) 4.2BSD's symbolic links.
  696.      Otherwise, use lstat (which, on non-4.2 systems, is #define'd to
  697.      stat anyway.  */
  698.  
  699. #ifdef STX_HIDDEN        /* AIX */
  700.   if (flag_follow_links != 0 ?
  701.       statx (p, &hstat, STATSIZE, STX_HIDDEN) :
  702.       statx (p, &hstat, STATSIZE, STX_HIDDEN | STX_LINK))
  703. #else
  704.   if (flag_follow_links != 0 ? stat (p, &hstat) : lstat (p, &hstat))
  705. #endif
  706.     {
  707.     badperror:
  708.       WARN ((0, errno, _("Cannot add file %s"), p));
  709.       /* Fall through.  */
  710.  
  711.     badfile:
  712.       if (!flag_ignore_failed_read || critical_error)
  713.     exit_status = TAREXIT_FAILURE;
  714.  
  715.       return;
  716.     }
  717.  
  718.   restore_times.actime = hstat.st_atime;
  719.   restore_times.modtime = hstat.st_mtime;
  720.  
  721. #ifdef S_ISHIDDEN
  722.   if (S_ISHIDDEN (hstat.st_mode))
  723.     {
  724.       char *new = (char *) alloca (strlen (p) + 2);
  725.       if (new)
  726.     {
  727.       strcpy (new, p);
  728.       strcat (new, "@");
  729.       p = new;
  730.     }
  731.     }
  732. #endif
  733.  
  734.   /* See if we only want new files, and check if this one is too old to
  735.      put in the archive.  */
  736.  
  737.   if (flag_new_files && !flag_gnudump && new_time > hstat.st_mtime
  738.       && !S_ISDIR (hstat.st_mode)
  739.       && (flag_new_files > 1 || new_time > hstat.st_ctime))
  740.     {
  741.       if (curdev == -1)
  742.     WARN ((0, 0, _("%s: is unchanged; not dumped"), p));
  743.       return;
  744.     }
  745.  
  746. #ifndef __MSDOS__
  747.   /* See if we are trying to dump the archive.  */
  748.  
  749.   if (ar_dev && hstat.st_dev == ar_dev && hstat.st_ino == ar_ino)
  750.     {
  751.       WARN ((0, 0, _("%s is the archive; not dumped"), p));
  752.       return;
  753.     }
  754. #endif
  755.  
  756.   /* Check for multiple links.
  757.  
  758.      We maintain a list of all such files that we've written so far.  Any
  759.      time we see another, we check the list and avoid dumping the data
  760.      again if we've done it once already.  */
  761.  
  762.   if (hstat.st_nlink > 1
  763.       && (S_ISREG (hstat.st_mode)
  764. #ifdef S_ISCTG
  765.       || S_ISCTG (hstat.st_mode)
  766. #endif
  767. #ifdef S_ISCHR
  768.       || S_ISCHR (hstat.st_mode)
  769. #endif
  770. #ifdef S_ISBLK
  771.       || S_ISBLK (hstat.st_mode)
  772. #endif
  773. #ifdef S_ISFIFO
  774.       || S_ISFIFO (hstat.st_mode)
  775. #endif
  776.       ))
  777.     {
  778.       register struct link *lp;
  779.  
  780.       /* First quick and dirty.  Hashing, etc later.  FIXME.  */
  781.  
  782.       for (lp = linklist; lp; lp = lp->next)
  783.     if (lp->ino == hstat.st_ino && lp->dev == hstat.st_dev)
  784.       {
  785.         char *link_name = lp->name;
  786.  
  787.         /* We found a link.  */
  788.  
  789.         while (!flag_absolute_names && *link_name == '/')
  790.           {
  791.         static int link_warn = 0;
  792.  
  793.         if (!link_warn)
  794.           {
  795.             WARN ((0, 0, _("Removing leading / from absolute links")));
  796.             link_warn++;
  797.           }
  798.         link_name++;
  799.           }
  800.         if (link_name - lp->name >= NAMSIZ)
  801.           write_long (link_name, LF_LONGLINK);
  802.         assign_string (¤t_link_name, link_name);
  803.  
  804.         hstat.st_size = 0;
  805.         header = start_header (p, &hstat);
  806.         if (header == NULL)
  807.           {
  808.         critical_error = 1;
  809.         goto badfile;
  810.           }
  811.         strncpy (header->header.arch_linkname,
  812.              link_name, NAMSIZ);
  813.  
  814.         /* Force null truncated.  */
  815.  
  816.         header->header.arch_linkname[NAMSIZ - 1] = 0;
  817.  
  818.         header->header.linkflag = LF_LINK;
  819.         finish_header (header);
  820.  
  821.         /* FIXME: Maybe remove from list after all links found?  */
  822.  
  823.         if (flag_remove_files)
  824.           {
  825.         if (unlink (p) == -1)
  826.           ERROR ((0, errno, _("Cannot remove %s"), p));
  827.           }
  828.         return;        /* we dumped it */
  829.       }
  830.  
  831.       /* Not found.  Add it to the list of possible links.  */
  832.  
  833.       lp = (struct link *)
  834.     xmalloc ((size_t) (sizeof (struct link) + strlen (p)));
  835.       lp->ino = hstat.st_ino;
  836.       lp->dev = hstat.st_dev;
  837.       strcpy (lp->name, p);
  838.       lp->next = linklist;
  839.       linklist = lp;
  840.     }
  841.  
  842.   /* This is not a link to a previously dumped file, so dump it.  */
  843.  
  844.   if (S_ISREG (hstat.st_mode)
  845. #ifdef S_ISCTG
  846.       || S_ISCTG (hstat.st_mode)
  847. #endif
  848.       )
  849.     {
  850.       int f;            /* file descriptor */
  851.       long bufsize, count;
  852.       long sizeleft;
  853.       register union record *start;
  854.       int header_moved;
  855.       char isextended = 0;
  856.       int upperbound;
  857. #if 0
  858.       int end_nulls = 0;
  859. #endif
  860. #if 0
  861.       static int cried_once = 0;
  862. #endif
  863.  
  864.       header_moved = 0;
  865.  
  866.       if (flag_sparse_files)
  867.     {
  868.  
  869.       /* Check the size of the file against the number of blocks
  870.          allocated for it, counting both data and indirect blocks.
  871.          If there is a smaller number of blocks that would be
  872.          necessary to accommodate a file of this size, this is safe
  873.          to say that we have a sparse file: at least one of those
  874.          records in the file is just a useless hole.  For sparse
  875.          files not having more hole blocks than indirect blocks, the
  876.          sparseness will go undetected.  */
  877.  
  878.       /* tar.h defines ST_NBLOCKS in term of 512 byte sectors, even
  879.          for HP-UX's which count in 1024 byte units and AIX's which
  880.          count in 4096 byte units.  So this should work...  */
  881.  
  882.       /* Bruno Haible sent me these statistics for Linux.  It seems that
  883.          some filesystems count indirect blocks in st_blocks, while
  884.          others do not seem to:
  885.  
  886.          minix-fs   tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
  887.          extfs      tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
  888.          ext2fs     tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
  889.          msdos-fs   tar: size=7205, st_blocks=16 and ST_NBLOCKS=16 */
  890.  
  891.       if (hstat.st_size > ST_NBLOCKS (hstat) * RECORDSIZE)
  892.         {
  893.           int filesize = hstat.st_size;
  894.           register int i;
  895.  
  896.           header = start_header (p, &hstat);
  897.           if (header == NULL)
  898.         {
  899.           critical_error = 1;
  900.           goto badfile;
  901.         }
  902.           header->header.linkflag = LF_SPARSE;
  903.           header_moved++;
  904.  
  905.           /* Call the routine that figures out the layout of the
  906.          sparse file in question.  UPPERBOUND is the index of the
  907.          last element of the "sparsearray," i.e., the number of
  908.          elements it needed to describe the file.  */
  909.  
  910.           upperbound = deal_with_sparse (p, header);
  911.  
  912.           /* See if we'll need an extended header later.  */
  913.  
  914.           if (upperbound > SPARSE_IN_HDR - 1)
  915.         header->header.isextended++;
  916.  
  917.           /* We store the "real" file size so we can show that in
  918.          case someone wants to list the archive, i.e., tar tvf
  919.          <file>.  It might be kind of disconcerting if the
  920.          shrunken file size was the one that showed up.  */
  921.  
  922.           to_oct ((long) hstat.st_size, 1 + 12, header->header.realsize);
  923.  
  924.           /* This will be the new "size" of the file, i.e., the size
  925.          of the file minus the records of holes that we're
  926.          skipping over.  */
  927.  
  928.           find_new_file_size (&filesize, upperbound);
  929.           hstat.st_size = filesize;
  930.           to_oct ((long) filesize, 1 + 12, header->header.size);
  931. #if 0
  932.           to_oct ((long) end_nulls, 1 + 12, header->header.ending_blanks);
  933. #endif
  934.  
  935.           for (i = 0; i < SPARSE_IN_HDR; i++)
  936.         {
  937.           if (!sparsearray[i].numbytes)
  938.             break;
  939.           to_oct (sparsearray[i].offset, 1 + 12,
  940.               header->header.sp[i].offset);
  941.           to_oct (sparsearray[i].numbytes, 1 + 12,
  942.               header->header.sp[i].numbytes);
  943.         }
  944.  
  945.         }
  946.     }
  947.       else
  948.     upperbound = SPARSE_IN_HDR - 1;
  949.  
  950.       sizeleft = hstat.st_size;
  951.  
  952.       /* Don't bother opening empty, world readable files.  */
  953.  
  954.       if (sizeleft > 0 || 0444 != (0444 & hstat.st_mode))
  955.     {
  956.       f = open (p, O_RDONLY | O_BINARY);
  957.       if (f < 0)
  958.         goto badperror;
  959.     }
  960.       else
  961.     f = -1;
  962.  
  963.       /* If the file is sparse, we've already taken care of this.  */
  964.  
  965.       if (!header_moved)
  966.     {
  967.       header = start_header (p, &hstat);
  968.       if (header == NULL)
  969.         {
  970.           if (f >= 0)
  971.         close (f);
  972.           critical_error = 1;
  973.           goto badfile;
  974.         }
  975.     }
  976. #ifdef S_ISCTG
  977.       /* Mark contiguous files, if we support them.  */
  978.  
  979.       if (flag_standard && S_ISCTG (hstat.st_mode))
  980.     header->header.linkflag = LF_CONTIG;
  981. #endif
  982.       isextended = header->header.isextended;
  983.       save_linkflag = header->header.linkflag;
  984.       finish_header (header);
  985.       if (isextended)
  986.     {
  987. #if 0
  988.       int sum = 0;
  989. #endif
  990.       register int i;
  991. #if 0
  992.       register union record *exhdr;
  993.       int arraybound = SPARSE_EXT_HDR;
  994. #endif
  995.       /* static */ int index_offset = SPARSE_IN_HDR;
  996.  
  997.     extend:
  998.       exhdr = findrec ();
  999.  
  1000.       if (exhdr == NULL)
  1001.         {
  1002.           critical_error = 1;
  1003.           goto badfile;
  1004.         }
  1005.       memset (exhdr->charptr, 0, RECORDSIZE);
  1006.       for (i = 0; i < SPARSE_EXT_HDR; i++)
  1007.         {
  1008.           if (i + index_offset > upperbound)
  1009.         break;
  1010.           to_oct ((long) sparsearray[i + index_offset].numbytes,
  1011.               1 + 12,
  1012.               exhdr->ext_hdr.sp[i].numbytes);
  1013.           to_oct ((long) sparsearray[i + index_offset].offset,
  1014.               1 + 12,
  1015.               exhdr->ext_hdr.sp[i].offset);
  1016.         }
  1017.       userec (exhdr);
  1018. #if 0
  1019.       sum += i;
  1020.       if (sum < upperbound)
  1021.         goto extend;
  1022. #endif
  1023.       if (index_offset + i <= upperbound)
  1024.         {
  1025.           index_offset += i;
  1026.           exhdr->ext_hdr.isextended++;
  1027.           goto extend;
  1028.         }
  1029.  
  1030.     }
  1031.       if (save_linkflag == LF_SPARSE)
  1032.     {
  1033.       if (finish_sparse_file (f, &sizeleft, hstat.st_size, p))
  1034.         goto padit;
  1035.     }
  1036.       else
  1037.     while (sizeleft > 0)
  1038.       {
  1039.         if (flag_multivol)
  1040.           {
  1041.         assign_string (&save_name, p);
  1042.         save_sizeleft = sizeleft;
  1043.         save_totsize = hstat.st_size;
  1044.           }
  1045.         start = findrec ();
  1046.  
  1047.         bufsize = endofrecs ()->charptr - start->charptr;
  1048.  
  1049.         if (sizeleft < bufsize)
  1050.           {
  1051.  
  1052.         /* Last read -- zero out area beyond.  */
  1053.  
  1054.         bufsize = (int) sizeleft;
  1055.         count = bufsize % RECORDSIZE;
  1056.         if (count)
  1057.           memset (start->charptr + sizeleft, 0,
  1058.               (size_t) (RECORDSIZE - count));
  1059.           }
  1060.         count = read (f, start->charptr, (size_t) bufsize);
  1061.         if (count < 0)
  1062.           {
  1063.         ERROR ((0, errno, _("\
  1064. Read error at byte %ld, reading %d bytes, in file %s"),
  1065.                hstat.st_size - sizeleft, bufsize, p));
  1066.         goto padit;
  1067.           }
  1068.         sizeleft -= count;
  1069.  
  1070.         /* This is nonportable (the type of userec's arg).  */
  1071.  
  1072.         userec (start + (count - 1) / RECORDSIZE);
  1073.  
  1074.         if (count == bufsize)
  1075.           continue;
  1076.         ERROR ((0, 0, _("File %s shrunk by %d bytes, padding with zeros"),
  1077.             p, sizeleft));
  1078.         goto padit;        /* short read */
  1079.       }
  1080.  
  1081.       if (flag_multivol)
  1082.     assign_string (&save_name, NULL);
  1083.  
  1084.       if (f >= 0)
  1085.     close (f);
  1086.  
  1087.       if (flag_remove_files)
  1088.     {
  1089.       if (unlink (p) == -1)
  1090.         ERROR ((0, errno, _("Cannot remove %s"), p));
  1091.     }
  1092.       if (flag_atime_preserve)
  1093.     utime (p, &restore_times);
  1094.       return;
  1095.  
  1096.       /* File shrunk or gave error, pad out tape to match the size we
  1097.      specified in the header.  */
  1098.  
  1099.     padit:
  1100.       while (sizeleft > 0)
  1101.     {
  1102.       save_sizeleft = sizeleft;
  1103.       start = findrec ();
  1104.       memset (start->charptr, 0, RECORDSIZE);
  1105.       userec (start);
  1106.       sizeleft -= RECORDSIZE;
  1107.     }
  1108.       if (flag_multivol)
  1109.     assign_string (&save_name, NULL);
  1110.       if (f >= 0)
  1111.     close (f);
  1112.       if (flag_atime_preserve)
  1113.     utime (p, &restore_times);
  1114.       return;
  1115.     }
  1116.  
  1117. #ifdef S_ISLNK
  1118.   else if (S_ISLNK (hstat.st_mode))
  1119.     {
  1120.       int size;
  1121.       char *buf = (char *) alloca (PATH_MAX + 1);
  1122.  
  1123.       size = readlink (p, buf, PATH_MAX + 1);
  1124.       if (size < 0)
  1125.     goto badperror;
  1126.       buf[size] = '\0';
  1127.       if (size >= NAMSIZ)
  1128.     write_long (buf, LF_LONGLINK);
  1129.       assign_string (¤t_link_name, buf);
  1130.  
  1131.       hstat.st_size = 0;    /* force 0 size on symlink */
  1132.       header = start_header (p, &hstat);
  1133.       if (header == NULL)
  1134.     {
  1135.       critical_error = 1;
  1136.       goto badfile;
  1137.     }
  1138.       strncpy (header->header.arch_linkname, buf, NAMSIZ);
  1139.       header->header.arch_linkname[NAMSIZ - 1] = '\0';
  1140.       header->header.linkflag = LF_SYMLINK;
  1141.       finish_header (header);    /* nothing more to do to it */
  1142.       if (flag_remove_files)
  1143.     {
  1144.       if (unlink (p) == -1)
  1145.         ERROR ((0, errno, _("Cannot remove %s"), p));
  1146.     }
  1147.       return;
  1148.     }
  1149. #endif /* S_ISLNK */
  1150.  
  1151.   else if (S_ISDIR (hstat.st_mode))
  1152.     {
  1153.       register DIR *dirp;
  1154.       register struct dirent *d;
  1155.       char *namebuf;
  1156.       int buflen;
  1157.       register int len;
  1158.       int our_device = hstat.st_dev;
  1159.  
  1160.       /* Build new prototype name.  */
  1161.  
  1162.       len = strlen (p);
  1163.       buflen = len + NAMSIZ;
  1164.       namebuf = xmalloc ((size_t) (buflen + 1));
  1165.       strncpy (namebuf, p, (size_t) buflen);
  1166.       while (len >= 1 && namebuf[len - 1] == '/')
  1167.     len--;            /* delete trailing slashes */
  1168.       namebuf[len++] = '/';    /* now add exactly one back */
  1169.       namebuf[len] = '\0';    /* make sure null-terminated */
  1170.  
  1171.       /* Output directory header record with permissions FIXME, do this
  1172.      AFTER files, to avoid R/O dir problems?  If old archive format,
  1173.      don't write record at all.  */
  1174.  
  1175.       if (!flag_oldarch)
  1176.     {
  1177.       hstat.st_size = 0;    /* force 0 size on dir */
  1178.  
  1179.       /* If people could really read standard archives, this should
  1180.          be: (FIXME)
  1181.  
  1182.          header = start_header (flag_standard ? p : namebuf, &hstat);
  1183.  
  1184.          but since they'd interpret LF_DIR records as regular files,
  1185.          we'd better put the / on the name.  */
  1186.  
  1187.       header = start_header (namebuf, &hstat);
  1188.       if (header == NULL)
  1189.         {
  1190.           critical_error = 1;
  1191.           goto badfile;    /* eg name too long */
  1192.         }
  1193.  
  1194.       if (flag_gnudump)
  1195.         header->header.linkflag = LF_DUMPDIR;
  1196.       else if (flag_standard)
  1197.         header->header.linkflag = LF_DIR;
  1198.  
  1199.       /* If we're gnudumping, we aren't done yet so don't close it.  */
  1200.  
  1201.       if (!flag_gnudump)
  1202.         finish_header (header);    /* done with directory header */
  1203.     }
  1204.  
  1205.       if (flag_gnudump && gnu_list_name->dir_contents)
  1206.     {
  1207.       int sizeleft;
  1208.       int totsize;
  1209.       int bufsize;
  1210.       union record *start;
  1211.       int count;
  1212.       char *buf, *p_buf;
  1213.  
  1214.       buf = gnu_list_name->dir_contents; /* FOO */
  1215.       totsize = 0;
  1216.       for (p_buf = buf; p_buf && *p_buf;)
  1217.         {
  1218.           int tmp;
  1219.  
  1220.           tmp = strlen (p_buf) + 1;
  1221.           totsize += tmp;
  1222.           p_buf += tmp;
  1223.         }
  1224.       totsize++;
  1225.       to_oct ((long) totsize, 1 + 12, header->header.size);
  1226.       finish_header (header);
  1227.       p_buf = buf;
  1228.       sizeleft = totsize;
  1229.       while (sizeleft > 0)
  1230.         {
  1231.           if (flag_multivol)
  1232.         {
  1233.           assign_string (&save_name, p);
  1234.           save_sizeleft = sizeleft;
  1235.           save_totsize = totsize;
  1236.         }
  1237.           start = findrec ();
  1238.           bufsize = endofrecs ()->charptr - start->charptr;
  1239.           if (sizeleft < bufsize)
  1240.         {
  1241.           bufsize = sizeleft;
  1242.           count = bufsize % RECORDSIZE;
  1243.           if (count)
  1244.             memset (start->charptr + sizeleft, 0,
  1245.                (size_t) (RECORDSIZE - count));
  1246.         }
  1247.           memcpy (start->charptr, p_buf, (size_t) bufsize);
  1248.           sizeleft -= bufsize;
  1249.           p_buf += bufsize;
  1250.           userec (start + (bufsize - 1) / RECORDSIZE);
  1251.         }
  1252.       if (flag_multivol)
  1253.         assign_string (&save_name, NULL);
  1254.       if (flag_atime_preserve)
  1255.         utime (p, &restore_times);
  1256.       return;
  1257.     }
  1258.  
  1259.       /* Now output all the files in the directory.  */
  1260.  
  1261. #if 0
  1262.       if (flag_dironly)
  1263.     return;            /* unless the cmdline said not to */
  1264. #endif
  1265.  
  1266.       /* See if we are crossing from one file system to another, and
  1267.      avoid doing so if the user only wants to dump one file system.  */
  1268.  
  1269.       if (flag_local_filesys && !toplevel && curdev != hstat.st_dev)
  1270.     {
  1271.       if (flag_verbose)
  1272.         WARN ((0, 0, _("%s: On a different filesystem; not dumped"), p));
  1273.       return;
  1274.     }
  1275.  
  1276.       errno = 0;
  1277.       dirp = opendir (p);
  1278.       if (!dirp)
  1279.     {
  1280.       ERROR ((0, errno, _("Cannot open directory %s"), p));
  1281.       return;
  1282.     }
  1283.  
  1284.       /* Hack to remove "./" from the front of all the file names.  */
  1285.  
  1286.       if (len == 2 && namebuf[0] == '.' && namebuf[1] == '/')
  1287.     len = 0;
  1288.  
  1289.       /* Should speed this up by cd-ing into the dir, FIXME.  */
  1290.  
  1291.       while (d = readdir (dirp), d)
  1292.     {
  1293.  
  1294.       /* Skip `.' and `..'.  */
  1295.  
  1296.       if (is_dot_or_dotdot (d->d_name))
  1297.         continue;
  1298.  
  1299.       if ((int) NAMLEN (d) + len >= buflen)
  1300.         {
  1301.           buflen = len + NAMLEN (d);
  1302.           namebuf = (char *) xrealloc (namebuf, (size_t) (buflen + 1));
  1303. #if 0
  1304.           namebuf[len] = '\0';
  1305.           ERROR ((0, 0, _("File name %s%s too long"), namebuf, d->d_name));
  1306.           continue;
  1307. #endif
  1308.         }
  1309.       strcpy (namebuf + len, d->d_name);
  1310.       if (flag_exclude && check_exclude (namebuf))
  1311.         continue;
  1312.       dump_file (namebuf, our_device, 0);
  1313.     }
  1314.  
  1315.       closedir (dirp);
  1316.       free (namebuf);
  1317.       if (flag_atime_preserve)
  1318.     utime (p, &restore_times);
  1319.       return;
  1320.     }
  1321.  
  1322. #ifdef S_ISCHR
  1323.   else if (S_ISCHR (hstat.st_mode))
  1324.     type = LF_CHR;
  1325. #endif
  1326.  
  1327. #ifdef S_ISBLK
  1328.   else if (S_ISBLK (hstat.st_mode))
  1329.     type = LF_BLK;
  1330. #endif
  1331.  
  1332.   /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK.  */
  1333.  
  1334. #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
  1335.   else if (S_ISFIFO (hstat.st_mode))
  1336.     type = LF_FIFO;
  1337. #endif
  1338.  
  1339. #ifdef S_ISSOCK
  1340.   else if (S_ISSOCK (hstat.st_mode))
  1341.     type = LF_FIFO;
  1342. #endif
  1343.  
  1344.   else
  1345.     goto unknown;
  1346.  
  1347.   if (!flag_standard)
  1348.     goto unknown;
  1349.  
  1350.   hstat.st_size = 0;        /* force 0 size */
  1351.   header = start_header (p, &hstat);
  1352.   if (header == NULL)
  1353.     {
  1354.       critical_error = 1;
  1355.       goto badfile;        /* eg name too long */
  1356.     }
  1357.  
  1358.   header->header.linkflag = type;
  1359.  
  1360. #if defined(S_IFBLK) || defined(S_IFCHR)
  1361.   if (type != LF_FIFO)
  1362.     {
  1363.       to_oct ((long) major (hstat.st_rdev), 8,
  1364.           header->header.devmajor);
  1365.       to_oct ((long) minor (hstat.st_rdev), 8,
  1366.           header->header.devminor);
  1367.     }
  1368. #endif
  1369.  
  1370.   finish_header (header);
  1371.   if (flag_remove_files)
  1372.     {
  1373.       if (unlink (p) == -1)
  1374.     ERROR ((0, errno, _("Cannot remove %s"), p));
  1375.     }
  1376.   return;
  1377.  
  1378. unknown:
  1379.   ERROR ((0, 0, _("%s: Unknown file type; file ignored"), p));
  1380. }
  1381.