pnpinfo(8): Oops, add missing semicolons.
[dragonfly.git] / contrib / cpio / tar.c
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   tape_buffered_write ((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   tape_buffered_read ((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       tape_buffered_read ((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           tape_buffered_read (((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 |= CP_IFDIR;
293           break;
294 #ifndef __MSDOS__
295         case CHRTYPE:
296           file_hdr->c_mode |= CP_IFCHR;
297           /* If a POSIX tar header has a valid linkname it's always supposed
298              to set typeflag to be LNKTYPE.  System V.4 tar seems to
299              be broken, and for device files with multiple links it
300              puts the name of the link into linkname, but leaves typeflag 
301              as CHRTYPE, BLKTYPE, FIFOTYPE, etc.  */
302           file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
303
304           /* Does POSIX say that the filesize must be 0 for devices?  We
305              assume so, but HPUX's POSIX tar sets it to be 1 which causes
306              us problems (when reading an archive we assume we can always
307              skip to the next file by skipping filesize bytes).  For 
308              now at least, it's easier to clear filesize for devices,
309              rather than check everywhere we skip in copyin.c.  */
310           file_hdr->c_filesize = 0;
311           break;
312         case BLKTYPE:
313           file_hdr->c_mode |= CP_IFBLK;
314           file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
315           file_hdr->c_filesize = 0;
316           break;
317 #ifdef CP_IFIFO
318         case FIFOTYPE:
319           file_hdr->c_mode |= CP_IFIFO;
320           file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
321           file_hdr->c_filesize = 0;
322           break;
323 #endif
324         case SYMTYPE:
325 #ifdef CP_IFLNK
326           file_hdr->c_mode |= CP_IFLNK;
327           file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
328           file_hdr->c_filesize = 0;
329           break;
330           /* Else fall through.  */
331 #endif
332         case LNKTYPE:
333           file_hdr->c_mode |= CP_IFREG;
334           file_hdr->c_tar_linkname = stash_tar_linkname (tar_hdr->linkname);
335           file_hdr->c_filesize = 0;
336           break;
337 #endif /* !__MSDOS__ */
338         case AREGTYPE:
339           /* Old tar format; if the last char in filename is '/' then it is
340              a directory, otherwise it's a regular file.  */
341           if (file_hdr->c_name[strlen (file_hdr->c_name) - 1] == '/')
342             file_hdr->c_mode |= CP_IFDIR;
343           else
344             file_hdr->c_mode |= CP_IFREG;
345           break;
346         }
347       break;
348     }
349   if (bytes_skipped > 0)
350     error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
351 }
352
353 /* Stash the tar linkname in static storage.  */
354
355 static char *
356 stash_tar_linkname (linkname)
357      char *linkname;
358 {
359   static char hold_tar_linkname[TARLINKNAMESIZE + 1];
360
361   strncpy (hold_tar_linkname, linkname, TARLINKNAMESIZE);
362   hold_tar_linkname[TARLINKNAMESIZE] = '\0';
363   return hold_tar_linkname;
364 }
365
366 /* Stash the tar filename and optional prefix in static storage.  */
367
368 static char *
369 stash_tar_filename (prefix, filename)
370      char *prefix;
371      char *filename;
372 {
373   static char hold_tar_filename[TARNAMESIZE + TARPREFIXSIZE + 2];
374   if (prefix == NULL || *prefix == '\0')
375     {
376       strncpy (hold_tar_filename, filename, TARNAMESIZE);
377       hold_tar_filename[TARNAMESIZE] = '\0';
378     }
379   else
380     {
381       strncpy (hold_tar_filename, prefix, TARPREFIXSIZE);
382       hold_tar_filename[TARPREFIXSIZE] = '\0';
383       strcat (hold_tar_filename, "/");
384       strncat (hold_tar_filename, filename, TARNAMESIZE);
385       hold_tar_filename[TARPREFIXSIZE + TARNAMESIZE] = '\0';
386     }
387   return hold_tar_filename;
388 }
389
390 /* Convert the string of octal digits S into a number and store
391    it in *N.  Return nonzero if the whole string was converted,
392    zero if there was something after the number.
393    Skip leading and trailing spaces.  */
394
395 int
396 otoa (s, n)
397      char *s;
398      unsigned long *n;
399 {
400   unsigned long val = 0;
401
402   while (*s == ' ')
403     ++s;
404   while (*s >= '0' && *s <= '7')
405     val = 8 * val + *s++ - '0';
406   while (*s == ' ')
407     ++s;
408   *n = val;
409   return *s == '\0';
410 }
411
412 /* Convert a number into a string of octal digits.
413    Convert long VALUE into a DIGITS-digit field at WHERE,
414    including a trailing space and room for a NUL.  DIGITS==3 means
415    1 digit, a space, and room for a NUL.
416
417    We assume the trailing NUL is already there and don't fill it in.
418    This fact is used by start_header and finish_header, so don't change it!
419
420    This is be equivalent to:
421    sprintf (where, "%*lo ", digits - 2, value);
422    except that sprintf fills in the trailing NUL and we don't.  */
423
424 static void
425 to_oct (value, digits, where)
426      register long value;
427      register int digits;
428      register char *where;
429 {
430   --digits;                     /* Leave the trailing NUL slot alone.  */
431   where[--digits] = ' ';        /* Put in the space, though.  */
432
433   /* Produce the digits -- at least one.  */
434   do
435     {
436       where[--digits] = '0' + (char) (value & 7); /* One octal digit.  */
437       value >>= 3;
438     }
439   while (digits > 0 && value != 0);
440
441   /* Add leading spaces, if necessary.  */
442   while (digits > 0)
443     where[--digits] = ' ';
444 }
445
446 /* Return
447    2 if BUF is a valid POSIX tar header (the checksum is correct
448    and it has the "ustar" magic string),
449    1 if BUF is a valid old tar header (the checksum is correct),
450    0 otherwise.  */
451
452 int
453 is_tar_header (buf)
454      char *buf;
455 {
456   struct tar_header *tar_hdr = (struct tar_header *) buf;
457   unsigned long chksum;
458
459   otoa (tar_hdr->chksum, &chksum);
460
461   if (chksum != tar_checksum (tar_hdr))
462     return 0;
463
464   /* GNU tar 1.10 and previous set the magic field to be "ustar " instead
465      of "ustar\0".  Only look at the first 5 characters of the magic
466      field so we can recognize old GNU tar ustar archives.  */
467   if (!strncmp (tar_hdr->magic, TMAGIC, TMAGLEN - 1))
468       return 2;
469   return 1;
470 }
471
472 /* Return TRUE if the filename is too long to fit in a tar header.
473    For old tar headers, if the filename's length is less than or equal
474    to 100 then it will fit, otherwise it will not.  For POSIX tar headers,
475    if the filename's length is less than or equal to 100 then it
476    will definitely fit, and if it is greater than 256 then it
477    will definitely not fit.  If the length is between 100 and 256,
478    then the filename will fit only if it is possible to break it
479    into a 155 character "prefix" and 100 character "name".  There
480    must be a slash between the "prefix" and the "name", although
481    the slash is not stored or counted in either the "prefix" or
482    the "name", and there must be at least one character in both
483    the "prefix" and the "name".  If it is not possible to break down
484    the filename like this then it will not fit.  */
485
486 int
487 is_tar_filename_too_long (name)
488      char *name;
489 {
490   int whole_name_len;
491   int prefix_name_len;
492   char *p;
493
494   whole_name_len = strlen (name);
495   if (whole_name_len <= TARNAMESIZE)
496     return FALSE;
497
498   if (archive_format != arf_ustar)
499     return TRUE;
500
501   if (whole_name_len > TARNAMESIZE + TARPREFIXSIZE + 1)
502     return TRUE;
503
504   /* See whether we can split up the name into acceptably-sized
505      `prefix' and `name' (`p') pieces.  Start out by making `name'
506      as big as possible, then shrink it by looking for the first '/'.  */
507   p = name + whole_name_len - TARNAMESIZE;
508   while (*p != '/' && *p != '\0')
509     ++p;
510   if (*p == '\0')
511     /* The last component of the path is longer than TARNAMESIZE.  */
512     return TRUE;
513
514   prefix_name_len = p - name - 1;
515   /* Interestingly, a name consisting of a slash followed by
516      TARNAMESIZE characters can't be stored, because the prefix
517      would be empty, and thus ignored.  */
518   if (prefix_name_len > TARPREFIXSIZE || prefix_name_len == 0)
519     return TRUE;
520
521   return FALSE;
522 }