home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / cpio-2.3-src.lha / GNU / src / amiga / cpio-2.3 / tar.c < prev    next >
C/C++ Source or Header  |  1993-04-29  |  15KB  |  523 lines

  1. /* tar.c - read in write tar headers for cpio
  2.    Copyright (C) 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 "rmt.h"
  27. #include "tarhdr.h"
  28.  
  29. static void to_oct ();
  30. static char *stash_tar_linkname ();
  31. static char *stash_tar_filename ();
  32.  
  33. /* Compute and return a checksum for TAR_HDR,
  34.    counting the checksum bytes as if they were spaces.  */
  35.  
  36. unsigned long
  37. tar_checksum (tar_hdr)
  38.      struct tar_header *tar_hdr;
  39. {
  40.   unsigned long sum = 0;
  41.   char *p = (char *) tar_hdr;
  42.   char *q = p + TARRECORDSIZE;
  43.   int i;
  44.  
  45.   while (p < tar_hdr->chksum)
  46.     sum += *p++ & 0xff;
  47.   for (i = 0; i < 8; ++i)
  48.     {
  49.       sum += ' ';
  50.       ++p;
  51.     }
  52.   while (p < q)
  53.     sum += *p++ & 0xff;
  54.   return sum;
  55. }
  56.  
  57. /* Write out header FILE_HDR, including the file name, to file
  58.    descriptor OUT_DES.  */
  59.  
  60. void
  61. write_out_tar_header (file_hdr, out_des)
  62.      struct new_cpio_header *file_hdr;
  63.      int out_des;
  64. {
  65.   int name_len;
  66.   union tar_record tar_rec;
  67.   struct tar_header *tar_hdr = (struct tar_header *) &tar_rec;
  68.  
  69.   bzero ((char *) &tar_rec, TARRECORDSIZE);
  70.  
  71.   /* process_copy_out must ensure that file_hdr->c_name is short enough,
  72.      or we will lose here.  */
  73.  
  74.   name_len = strlen (file_hdr->c_name);
  75.   if (name_len <= TARNAMESIZE)
  76.     {
  77.       strncpy (tar_hdr->name, file_hdr->c_name, name_len);
  78.     }
  79.   else
  80.     {
  81.       /* Fit as much as we can into `name', the rest into `prefix'.  */
  82.       char *suffix = file_hdr->c_name + name_len - TARNAMESIZE;
  83.  
  84.       /* We have to put the boundary at a slash.  */
  85.       name_len = TARNAMESIZE;
  86.       while (*suffix != '/')
  87.     {
  88.       --name_len;
  89.       ++suffix;
  90.     }
  91.       strncpy (tar_hdr->name, suffix + 1, name_len);
  92.       strncpy (tar_hdr->prefix, file_hdr->c_name, suffix - file_hdr->c_name);
  93.     }
  94.  
  95.   /* SVR4 seems to want the whole mode, not just protection modes.
  96.      Nobody else seems to care, so we might as well put it all in.  */
  97.   to_oct (file_hdr->c_mode, 8, tar_hdr->mode);
  98.   to_oct (file_hdr->c_uid, 8, tar_hdr->uid);
  99.   to_oct (file_hdr->c_gid, 8, tar_hdr->gid);
  100.   to_oct (file_hdr->c_filesize, 12, tar_hdr->size);
  101.   to_oct (file_hdr->c_mtime, 12, tar_hdr->mtime);
  102.  
  103.   switch (file_hdr->c_mode & CP_IFMT)
  104.     {
  105.     case CP_IFREG:
  106.       if (file_hdr->c_tar_linkname)
  107.     {
  108.       /* process_copy_out makes sure that c_tar_linkname is shorter
  109.          than TARLINKNAMESIZE.  */
  110.       strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
  111.            TARLINKNAMESIZE);
  112.       tar_hdr->typeflag = LNKTYPE;
  113.       to_oct (0, 12, tar_hdr->size);
  114.     }
  115.       else
  116.     tar_hdr->typeflag = REGTYPE;
  117.       break;
  118.     case CP_IFDIR:
  119.       tar_hdr->typeflag = DIRTYPE;
  120.       break;
  121. #ifndef __MSDOS__
  122.     case CP_IFCHR:
  123.       tar_hdr->typeflag = CHRTYPE;
  124.       break;
  125.     case CP_IFBLK:
  126.       tar_hdr->typeflag = BLKTYPE;
  127.       break;
  128. #ifdef CP_IFIFO
  129.     case CP_IFIFO:
  130.       tar_hdr->typeflag = FIFOTYPE;
  131.       break;
  132. #endif /* CP_IFIFO */
  133. #ifdef CP_IFLNK
  134.     case CP_IFLNK:
  135.       tar_hdr->typeflag = SYMTYPE;
  136.       /* process_copy_out makes sure that c_tar_linkname is shorter
  137.      than TARLINKNAMESIZE.  */
  138.       strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
  139.            TARLINKNAMESIZE);
  140.       to_oct (0, 12, tar_hdr->size);
  141.       break;
  142. #endif /* CP_IFLNK */
  143. #endif /* !__MSDOS__ */
  144.     }
  145.  
  146.   if (archive_format == arf_ustar)
  147.     {
  148.       char *name;
  149.  
  150.       strncpy (tar_hdr->magic, TMAGIC, TMAGLEN);
  151.       strncpy (tar_hdr->magic + TMAGLEN, TVERSION, TVERSLEN);
  152.  
  153. #ifndef __MSDOS__
  154.       name = getuser (file_hdr->c_uid);
  155.       if (name)
  156.     strcpy (tar_hdr->uname, name);
  157.       name = getgroup (file_hdr->c_gid);
  158.       if (name)
  159.     strcpy (tar_hdr->gname, name);
  160. #endif
  161.  
  162.       to_oct (file_hdr->c_rdev_maj, 8, tar_hdr->devmajor);
  163.       to_oct (file_hdr->c_rdev_min, 8, tar_hdr->devminor);
  164.     }
  165.  
  166.   to_oct (tar_checksum (tar_hdr), 8, tar_hdr->chksum);
  167.  
  168.   copy_buf_out ((char *) &tar_rec, out_des, TARRECORDSIZE);
  169. }
  170.  
  171. /* Return nonzero iff all the bytes in BLOCK are NUL.
  172.    SIZE is the number of bytes to check in BLOCK; it must be a
  173.    multiple of sizeof (long).  */
  174.  
  175. int
  176. null_block (block, size)
  177.      long *block;
  178.      int size;
  179. {
  180.   register long *p = block;
  181.   register int i = size / sizeof (long);
  182.  
  183.   while (i--)
  184.     if (*p++)
  185.       return 0;
  186.   return 1;
  187. }
  188.  
  189. /* Read a tar header, including the file name, from file descriptor IN_DES
  190.    into FILE_HDR.  */
  191.  
  192. void
  193. read_in_tar_header (file_hdr, in_des)
  194.      struct new_cpio_header *file_hdr;
  195.      int in_des;
  196. {
  197.   long bytes_skipped = 0;
  198.   int warned = FALSE;
  199.   union tar_record tar_rec;
  200.   struct tar_header *tar_hdr = (struct tar_header *) &tar_rec;
  201. #ifndef __MSDOS__
  202.   uid_t *uidp;
  203.   gid_t *gidp;
  204. #endif
  205.  
  206.   copy_in_buf ((char *) &tar_rec, in_des, TARRECORDSIZE);
  207.  
  208.   /* Check for a block of 0's.  */
  209.   if (null_block ((long *) &tar_rec, TARRECORDSIZE))
  210.     {
  211. #if 0
  212.       /* Found one block of 512 0's.  If the next block is also all 0's
  213.      then this is the end of the archive.  If not, assume the
  214.      previous block was all corruption and continue reading
  215.      the archive.  */
  216.       /* Commented out because GNU tar sometimes creates archives with
  217.      only one block of 0's at the end.  This happened for the
  218.      cpio 2.0 distribution!  */
  219.       copy_in_buf ((char *) &tar_rec, in_des, TARRECORDSIZE);
  220.       if (null_block ((long *) &tar_rec, TARRECORDSIZE))
  221. #endif
  222.     {
  223.       file_hdr->c_name = "TRAILER!!!";
  224.       return;
  225.     }
  226. #if 0
  227.       bytes_skipped = TARRECORDSIZE;
  228. #endif
  229.     }
  230.  
  231.   while (1)
  232.     {
  233.       otoa (tar_hdr->chksum, &file_hdr->c_chksum);
  234.  
  235.       if (file_hdr->c_chksum != tar_checksum (tar_hdr))
  236.     {
  237.       /* If the checksum is bad, skip 1 byte and try again.  When
  238.          we try again we do not look for an EOF record (all zeros),
  239.          because when we start skipping bytes in a corrupted archive
  240.          the chances are pretty good that we might stumble across
  241.          2 blocks of 512 zeros (that probably is not really the last
  242.          record) and it is better to miss the EOF and give the user
  243.          a "premature EOF" error than to give up too soon on a corrupted
  244.          archive.  */
  245.       if (!warned)
  246.         {
  247.           error (0, 0, "invalid header: checksum error");
  248.           warned = TRUE;
  249.         }
  250.       bcopy (((char *) &tar_rec) + 1, (char *) &tar_rec,
  251.          TARRECORDSIZE - 1);
  252.       copy_in_buf (((char *) &tar_rec) + (TARRECORDSIZE - 1), in_des, 1);
  253.       ++bytes_skipped;
  254.       continue;
  255.     }
  256.  
  257.       if (archive_format != arf_ustar)
  258.     file_hdr->c_name = stash_tar_filename (NULL, tar_hdr->name);
  259.       else
  260.     file_hdr->c_name = stash_tar_filename (tar_hdr->prefix, tar_hdr->name);
  261.       file_hdr->c_nlink = 1;
  262.       otoa (tar_hdr->mode, &file_hdr->c_mode);
  263.       file_hdr->c_mode = file_hdr->c_mode & 07777;
  264. #ifndef __MSDOS__
  265.       if (archive_format == arf_ustar
  266.       && (uidp = getuidbyname (tar_hdr->uname)))
  267.     file_hdr->c_uid = *uidp;
  268.       else
  269. #endif
  270.     otoa (tar_hdr->uid, &file_hdr->c_uid);
  271. #ifndef __MSDOS__
  272.       if (archive_format == arf_ustar
  273.       && (gidp = getgidbyname (tar_hdr->gname)))
  274.     file_hdr->c_gid = *gidp;
  275.       else
  276. #endif
  277.     otoa (tar_hdr->gid, &file_hdr->c_gid);
  278.       otoa (tar_hdr->size, &file_hdr->c_filesize);
  279.       otoa (tar_hdr->mtime, &file_hdr->c_mtime);
  280.       otoa (tar_hdr->devmajor, (unsigned long *) &file_hdr->c_rdev_maj);
  281.       otoa (tar_hdr->devminor, (unsigned long *) &file_hdr->c_rdev_min);
  282.       file_hdr->c_tar_linkname = NULL;
  283.  
  284.       switch (tar_hdr->typeflag)
  285.     {
  286.     case REGTYPE:
  287.     case CONTTYPE:        /* For now, punt.  */
  288.     default:
  289.       file_hdr->c_mode |= CP_IFREG;
  290.       break;
  291.     case DIRTYPE:
  292.       file_hdr->c_mode