comparison src/win32/untar.c @ 5005:a1f9725f4816

[gaim-migrate @ 5340] Initial import committer: Tailor Script <tailor@pidgin.im>
author Herman Bloggs <hermanator12002@yahoo.com>
date Sat, 05 Apr 2003 01:10:13 +0000
parents
children 26b739bc9f1a
comparison
equal deleted inserted replaced
5004:e5a78df87bf8 5005:a1f9725f4816
1 /* untar.c */
2
3 /*#define VERSION "1.4"*/
4
5 /* DESCRIPTION:
6 * Untar extracts files from an uncompressed tar archive, or one which
7 * has been compressed with gzip. Usually such archives will have file
8 * names that end with ".tar" or ".tgz" respectively, although untar
9 * doesn't depend on any naming conventions. For a summary of the
10 * command-line options, run untar with no arguments.
11 *
12 * HOW TO COMPILE:
13 * Untar doesn't require any special libraries or compile-time flags.
14 * A simple "cc untar.c -o untar" (or the local equivalent) is
15 * sufficient. Even "make untar" works, without needing a Makefile.
16 * For Microsoft Visual C++, the command is "cl /D_WEAK_POSIX untar.c"
17 * (for 32 bit compilers) or "cl /F 1400 untar.c" (for 16-bit).
18 *
19 * IF YOU SEE COMPILER WARNINGS, THAT'S NORMAL; you can ignore them.
20 * Most of the warnings could be eliminated by adding #include <string.h>
21 * but that isn't portable -- some systems require <strings.h> and
22 * <malloc.h>, for example. Because <string.h> isn't quite portable,
23 * and isn't really necessary in the context of this program, it isn't
24 * included.
25 *
26 * PORTABILITY:
27 * Untar only requires the <stdio.h> header. It uses old-style function
28 * definitions. It opens all files in binary mode. Taken together,
29 * this means that untar should compile & run on just about anything.
30 *
31 * If your system supports the POSIX chmod(2), utime(2), link(2), and
32 * symlink(2) calls, then you may wish to compile with -D_POSIX_SOURCE,
33 * which will enable untar to use those system calls to restore the
34 * timestamp and permissions of the extracted files, and restore links.
35 * (For Linux, _POSIX_SOURCE is always defined.)
36 *
37 * For systems which support some POSIX features but not enough to support
38 * -D_POSIX_SOURCE, you might be able to use -D_WEAK_POSIX. This allows
39 * untar to restore time stamps and file permissions, but not links.
40 * This should work for Microsoft systems, and hopefully others as well.
41 *
42 * AUTHOR & COPYRIGHT INFO:
43 * Written by Steve Kirkendall, kirkenda@cs.pdx.edu
44 * Placed in public domain, 6 October 1995
45 *
46 * Portions derived from inflate.c -- Not copyrighted 1992 by Mark Adler
47 * version c10p1, 10 January 1993
48 *
49 * Altered by Herman Bloggs <hermanator12002@yahoo.com>
50 * April 4, 2003
51 * Changes: Stripped out gz compression code, added better interface for
52 * untar.
53 */
54 #include <windows.h>
55 #include <stdio.h>
56 #include <io.h>
57 #include <string.h>
58 #include <stdlib.h>
59 #ifndef SEEK_SET
60 # define SEEK_SET 0
61 #endif
62
63 #ifdef _WEAK_POSIX
64 # ifndef _POSIX_SOURCE
65 # define _POSIX_SOURCE
66 # endif
67 #endif
68
69 #ifdef _POSIX_SOURCE
70 # include <sys/types.h>
71 # include <sys/stat.h>
72 # include <sys/utime.h>
73 # ifdef _WEAK_POSIX
74 # define mode_t int
75 # else
76 # include <unistd.h>
77 # endif
78 #endif
79 #include "untar.h"
80
81 extern void debug_printf(char * fmt, ...);
82
83 #define mkdir(a,b) _mkdir((a))
84 #define untar_error( error, args... ) debug_printf( "UNTAR ERROR: " ## error ## , ## args )
85 #define untar_warning( warning, args... ) debug_printf( "UNTAR WARNING: " ## warning ## , ## args )
86
87 #define WSIZE 32768 /* size of decompression buffer */
88 #define TSIZE 512 /* size of a "tape" block */
89 #define CR 13 /* carriage-return character */
90 #define LF 10 /* line-feed character */
91
92 typedef unsigned char Uchar_t;
93 typedef unsigned short Ushort_t;
94 typedef unsigned long Ulong_t;
95
96 typedef struct
97 {
98 char filename[100]; /* 0 name of next file */
99 char mode[8]; /* 100 Permissions and type (octal digits) */
100 char owner[8]; /* 108 Owner ID (ignored) */
101 char group[8]; /* 116 Group ID (ignored) */
102 char size[12]; /* 124 Bytes in file (octal digits) */
103 char mtime[12]; /* 136 Modification time stamp (octal digits)*/
104 char checksum[8]; /* 148 Header checksum (ignored) */
105 char type; /* 156 File type (see below) */
106 char linkto[100]; /* 157 Linked-to name */
107 char brand[8]; /* 257 Identifies tar version (ignored) */
108 char ownername[32]; /* 265 Name of owner (ignored) */
109 char groupname[32]; /* 297 Name of group (ignored) */
110 char devmajor[8]; /* 329 Device major number (ignored) */
111 char defminor[8]; /* 337 Device minor number (ignored) */
112 char prefix[155]; /* 345 Prefix of name (optional) */
113 char RESERVED[12]; /* 500 Pad header size to 512 bytes */
114 } tar_t;
115 #define ISREGULAR(hdr) ((hdr).type < '1' || (hdr).type > '6')
116
117 Uchar_t slide[WSIZE];
118
119 static const char *inname = NULL; /* name of input archive */
120 static FILE *infp = NULL; /* input byte stream */
121 static FILE *outfp = NULL; /* output stream, for file currently being extracted */
122 static Ulong_t outsize = 0; /* number of bytes remainin in file currently being extracted */
123 static char **only = NULL; /* array of filenames to extract/list */
124 static int nonlys = 0; /* number of filenames in "only" array; 0=extract all */
125 static int didabs = 0; /* were any filenames affected by the absence of -p? */
126
127 static untar_opt untarops = 0; /* Untar options */
128
129 /* Options checked during untar process */
130 #define LISTING (untarops & UNTAR_LISTING) /* 1 if listing, 0 if extracting */
131 #define QUIET (untarops & UNTAR_QUIET) /* 1 to write nothing to stdout, 0 for normal chatter */
132 #define VERBOSE (untarops & UNTAR_VERBOSE) /* 1 to write extra information to stdout */
133 #define FORCE (untarops & UNTAR_FORCE) /* 1 to overwrite existing files, 0 to skip them */
134 #define ABSPATH (untarops & UNTAR_ABSPATH) /* 1 to allow leading '/', 0 to strip leading '/' */
135 #define CONVERT (untarops & UNTAR_CONVERT) /* 1 to convert newlines, 0 to leave unchanged */
136
137 /*----------------------------------------------------------------------------*/
138
139 /* create a file for writing. If necessary, create the directories leading up
140 * to that file as well.
141 */
142 static FILE *createpath(name)
143 char *name; /* pathname of file to create */
144 {
145 FILE *fp;
146 int i;
147
148 /* if we aren't allowed to overwrite and this file exists, return NULL */
149 if (!FORCE && access(name, 0) == 0)
150 {
151 untar_warning("%s: exists, will not overwrite without \"FORCE option\"\n", name);
152 return NULL;
153 }
154
155 /* first try creating it the easy way */
156 fp = fopen(name, CONVERT ? "w" : "wb");
157 if (fp)
158 return fp;
159
160 /* Else try making all of its directories, and then try creating
161 * the file again.
162 */
163 for (i = 0; name[i]; i++)
164 {
165 /* If this is a slash, then temporarily replace the '/'
166 * with a '\0' and do a mkdir() on the resulting string.
167 * Ignore errors for now.
168 */
169 if (name[i] == '/')
170 {
171 name[i] = '\0';
172 (void)mkdir(name, 0777);
173 name[i] = '/';
174 }
175 }
176 fp = fopen(name, CONVERT ? "w" : "wb");
177 if (!fp)
178 untar_error("Error opening: %s\n", name);
179 return fp;
180 }
181
182 /* Create a link, or copy a file. If the file is copied (not linked) then
183 * give a warning.
184 */
185 static void linkorcopy(src, dst, sym)
186 char *src; /* name of existing source file */
187 char *dst; /* name of new destination file */
188 int sym; /* use symlink instead of link */
189 {
190 FILE *fpsrc;
191 FILE *fpdst;
192 int c;
193
194 /* Open the source file. We do this first to make sure it exists */
195 fpsrc = fopen(src, "rb");
196 if (!fpsrc)
197 {
198 untar_error("Error opening: %s\n", src);
199 return;
200 }
201
202 /* Create the destination file. On POSIX systems, this is just to
203 * make sure the directory path exists.
204 */
205 fpdst = createpath(dst);
206 if (!fpdst)
207 /* error message already given */
208 return;
209
210 #ifdef _POSIX_SOURCE
211 # ifndef _WEAK_POSIX
212 /* first try to link it over, instead of copying */
213 fclose(fpdst);
214 unlink(dst);
215 if (sym)
216 {
217 if (symlink(src, dst))
218 {
219 perror(dst);
220 }
221 fclose(fpsrc);
222 return;
223 }
224 if (!link(src, dst))
225 {
226 /* This story had a happy ending */
227 fclose(fpsrc);
228 return;
229 }
230
231 /* Dang. Reopen the destination again */
232 fpdst = fopen(dst, "wb");
233 /* This *can't* fail */
234
235 # endif /* _WEAK_POSIX */
236 #endif /* _POSIX_SOURCE */
237
238 /* Copy characters */
239 while ((c = getc(fpsrc)) != EOF)
240 putc(c, fpdst);
241
242 /* Close the files */
243 fclose(fpsrc);
244 fclose(fpdst);
245
246 /* Give a warning */
247 untar_warning("%s: copy instead of link\n", dst);
248 }
249
250 /* This calls fwrite(), possibly after converting CR-LF to LF */
251 static void cvtwrite(blk, size, fp)
252 Uchar_t *blk; /* the block to be written */
253 Ulong_t size; /* number of characters to be written */
254 FILE *fp; /* file to write to */
255 {
256 int i, j;
257 static Uchar_t mod[TSIZE];
258
259 if (CONVERT)
260 {
261 for (i = j = 0; i < size; i++)
262 {
263 /* convert LF to local newline convention */
264 if (blk[i] == LF)
265 mod[j++] = '\n';
266 /* If CR-LF pair, then delete the CR */
267 else if (blk[i] == CR && (i+1 >= size || blk[i+1] == LF))
268 ;
269 /* other characters copied literally */
270 else
271 mod[j++] = blk[i];
272 }
273 size = j;
274 blk = mod;
275 }
276
277 fwrite(blk, (size_t)size, sizeof(Uchar_t), fp);
278 }
279
280
281 /* Compute the checksum of a tar header block, and return it as a long int.
282 * The checksum can be computed using either POSIX rules (unsigned bytes)
283 * or Sun rules (signed bytes).
284 */
285 static long checksum(tblk, sunny)
286 tar_t *tblk; /* buffer containing the tar header block */
287 int sunny; /* Boolean: Sun-style checksums? (else POSIX) */
288 {
289 long sum;
290 char *scan;
291
292 /* compute the sum of the first 148 bytes -- everything up to but not
293 * including the checksum field itself.
294 */
295 sum = 0L;
296 for (scan = (char *)tblk; scan < tblk->checksum; scan++)
297 {
298 sum += (*scan) & 0xff;
299 if (sunny && (*scan & 0x80) != 0)
300 sum -= 256;
301 }
302
303 /* for the 8 bytes of the checksum field, add blanks to the sum */
304 sum += ' ' * sizeof tblk->checksum;
305 scan += sizeof tblk->checksum;
306
307 /* finish counting the sum of the rest of the block */
308 for (; scan < (char *)tblk + sizeof *tblk; scan++)
309 {
310 sum += (*scan) & 0xff;
311 if (sunny && (*scan & 0x80) != 0)
312 sum -= 256;
313 }
314
315 return sum;
316 }
317
318
319
320 /* list files in an archive, and optionally extract them as well */
321 static int untar_block(Uchar_t *blk) {
322 static char nbuf[256];/* storage space for prefix+name, combined */
323 static char *name,*n2;/* prefix and name, combined */
324 static int first = 1;/* Boolean: first block of archive? */
325 long sum; /* checksum for this block */
326 int i;
327 tar_t tblk[1];
328
329 #ifdef _POSIX_SOURCE
330 static mode_t mode; /* file permissions */
331 static struct utimbuf timestamp; /* file timestamp */
332 #endif
333
334 /* make a local copy of the block, and treat it as a tar header */
335 tblk[0] = *(tar_t *)blk;
336
337 /* process each type of tape block differently */
338 if (outsize > TSIZE)
339 {
340 /* data block, but not the last one */
341 if (outfp)
342 cvtwrite(blk, (Ulong_t)TSIZE, outfp);
343 outsize -= TSIZE;
344 }
345 else if (outsize > 0)
346 {
347 /* last data block of current file */
348 if (outfp)
349 {
350 cvtwrite(blk, outsize, outfp);
351 fclose(outfp);
352 outfp = NULL;
353 #ifdef _POSIX_SOURCE
354 utime(nbuf, &timestamp);
355 chmod(nbuf, mode);
356 #endif
357 }
358 outsize = 0;
359 }
360 else if ((tblk)->filename[0] == '\0')
361 {
362 /* end-of-archive marker */
363 if (didabs)
364 untar_warning("Removed leading slashes because \"ABSPATH option\" wasn't given.\n");
365 return 1;
366 }
367 else
368 {
369 /* file header */
370
371 /* half-assed verification -- does it look like header? */
372 if ((tblk)->filename[99] != '\0'
373 || ((tblk)->size[0] < '0'
374 && (tblk)->size[0] != ' ')
375 || (tblk)->size[0] > '9')
376 {
377 if (first)
378 {
379 untar_error("%s: not a valid tar file\n", inname);
380 return 0;
381 }
382 else
383 {
384 untar_error("Garbage detected; preceding file may be damaged\n");
385 return 0;
386 }
387 }
388
389 /* combine prefix and filename */
390 memset(nbuf, 0, sizeof nbuf);
391 name = nbuf;
392 if ((tblk)->prefix[0])
393 {
394 strncpy(name, (tblk)->prefix, sizeof (tblk)->prefix);
395 strcat(name, "/");
396 strncat(name + strlen(name), (tblk)->filename,
397 sizeof (tblk)->filename);
398 }
399 else
400 {
401 strncpy(name, (tblk)->filename,
402 sizeof (tblk)->filename);
403 }
404
405 /* Convert any backslashes to forward slashes, and guard
406 * against doubled-up slashes. (Some DOS versions of "tar"
407 * get this wrong.) Also strip off leading slashes.
408 */
409 if (!ABSPATH && (*name == '/' || *name == '\\'))
410 didabs = 1;
411 for (n2 = nbuf; *name; name++)
412 {
413 if (*name == '\\')
414 *name = '/';
415 if (*name != '/'
416 || (ABSPATH && n2 == nbuf)
417 || (n2 != nbuf && n2[-1] != '/'))
418 *n2++ = *name;
419 }
420 if (n2 == nbuf)
421 *n2++ = '/';
422 *n2 = '\0';
423
424 /* verify the checksum */
425 for (sum = 0L, i = 0; i < sizeof((tblk)->checksum); i++)
426 {
427 if ((tblk)->checksum[i] >= '0'
428 && (tblk)->checksum[i] <= '7')
429 sum = sum * 8 + (tblk)->checksum[i] - '0';
430 }
431 if (sum != checksum(tblk, 0) && sum != checksum(tblk, 1))
432 {
433 if (!first)
434 untar_error("Garbage detected; preceding file may be damaged\n");
435 untar_error("%s: header has bad checksum for %s\n", inname, nbuf);
436 return 0;
437 }
438
439 /* From this point on, we don't care whether this is the first
440 * block or not. Might as well reset the "first" flag now.
441 */
442 first = 0;
443
444 /* if last character of name is '/' then assume directory */
445 if (*nbuf && nbuf[strlen(nbuf) - 1] == '/')
446 (tblk)->type = '5';
447
448 /* convert file size */
449 for (outsize = 0L, i = 0; i < sizeof((tblk)->size); i++)
450 {
451 if ((tblk)->size[i] >= '0' && (tblk)->size[i] <= '7')
452 outsize = outsize * 8 + (tblk)->size[i] - '0';
453 }
454
455 #ifdef _POSIX_SOURCE
456 /* convert file timestamp */
457 for (timestamp.modtime=0L, i=0; i < sizeof((tblk)->mtime); i++)
458 {
459 if ((tblk)->mtime[i] >= '0' && (tblk)->mtime[i] <= '7')
460 timestamp.modtime = timestamp.modtime * 8
461 + (tblk)->mtime[i] - '0';
462 }
463 timestamp.actime = timestamp.modtime;
464
465 /* convert file permissions */
466 for (mode = i = 0; i < sizeof((tblk)->mode); i++)
467 {
468 if ((tblk)->mode[i] >= '0' && (tblk)->mode[i] <= '7')
469 mode = mode * 8 + (tblk)->mode[i] - '0';
470 }
471 #endif
472
473 /* If we have an "only" list, and this file isn't in it,
474 * then skip it.
475 */
476 if (nonlys > 0)
477 {
478 for (i = 0;
479 i < nonlys
480 && strcmp(only[i], nbuf)
481 && (strncmp(only[i], nbuf, strlen(only[i]))
482 || nbuf[strlen(only[i])] != '/');
483 i++)
484 {
485 }
486 if (i >= nonlys)
487 {
488 outfp = NULL;
489 return 1;
490 }
491 }
492
493 /* list the file */
494 if (VERBOSE)
495 debug_printf("%c %s",
496 ISREGULAR(*tblk) ? '-' : ("hlcbdp"[(tblk)->type - '1']),
497 nbuf);
498 else if (!QUIET)
499 debug_printf("%s\n", nbuf);
500
501 /* if link, then do the link-or-copy thing */
502 if (tblk->type == '1' || tblk->type == '2')
503 {
504 if (VERBOSE)
505 debug_printf(" -> %s\n", tblk->linkto);
506 if (!LISTING)
507 linkorcopy(tblk->linkto, nbuf, tblk->type == '2');
508 outsize = 0L;
509 return 1;
510 }
511
512 /* If directory, then make a weak attempt to create it.
513 * Ideally we would do the "create path" thing, but that
514 * seems like more trouble than it's worth since traditional
515 * tar archives don't contain directories anyway.
516 */
517 if (tblk->type == '5')
518 {
519 if (LISTING)
520 n2 = " directory";
521 #ifdef _POSIX_SOURCE
522 else if (mkdir(nbuf, mode) == 0)
523 #else
524 else if (mkdir(nbuf, 0755) == 0)
525 #endif
526 n2 = " created";
527 else
528 n2 = " ignored";
529 if (VERBOSE)
530 debug_printf("%s\n", n2);
531 return 1;
532 }
533
534 /* if not a regular file, then skip it */
535 if (!ISREGULAR(*tblk))
536 {
537 if (VERBOSE)
538 debug_printf(" ignored\n");
539 outsize = 0L;
540 return 1;
541 }
542
543 /* print file statistics */
544 if (VERBOSE)
545 {
546 debug_printf(" (%ld byte%s, %ld tape block%s)\n",
547 outsize,
548 outsize == 1 ? "" : "s",
549 (outsize + TSIZE - 1) / TSIZE,
550 (outsize > 0 && outsize <= TSIZE) ? "" : "s");
551 }
552
553 /* if extracting, then try to create the file */
554 if (!LISTING)
555 outfp = createpath(nbuf);
556 else
557 outfp = NULL;
558
559 /* if file is 0 bytes long, then we're done already! */
560 if (outsize == 0 && outfp)
561 {
562 fclose(outfp);
563 #ifdef _POSIX_SOURCE
564 utime(nbuf, &timestamp);
565 chmod(nbuf, mode);
566 #endif
567 }
568 }
569 return 1;
570 }
571
572 /* Process an archive file. This involves reading the blocks one at a time
573 * and passing them to a untar() function.
574 */
575 int untar(const char *filename, const char* destdir, untar_opt options) {
576 int ret=1;
577 char curdir[_MAX_PATH];
578 untarops = options;
579 /* open the archive */
580 inname = filename;
581 infp = fopen(filename, "rb");
582 if (!infp)
583 {
584 untar_error("Error opening: %s\n", filename);
585 return 0;
586 }
587
588 /* Set current directory */
589 if(!GetCurrentDirectory(_MAX_PATH, curdir)) {
590 untar_error("Could not get current directory (error %d).\n", GetLastError());
591 fclose(infp);
592 return 0;
593 }
594 if(!SetCurrentDirectory(destdir)) {
595 untar_error("Could not set current directory to (error %d): %s\n", GetLastError(), destdir);
596 fclose(infp);
597 return 0;
598 } else {
599 /* UNCOMPRESSED */
600 /* send each block to the untar_block() function */
601 while (fread(slide, 1, TSIZE, infp) == TSIZE) {
602 if(!untar_block(slide)) {
603 untar_error("untar failure: %s\n", filename);
604 fclose(infp);
605 ret=0;
606 }
607 }
608 if (outsize > 0 && ret) {
609 untar_warning("Last file might be truncated!\n");
610 fclose(outfp);
611 outfp = NULL;
612 }
613 if(!SetCurrentDirectory(curdir)) {
614 untar_error("Could not set current dir back to original (error %d).\n", GetLastError());
615 ret=0;
616 }
617 }
618
619 /* close the archive file. */
620 fclose(infp);
621
622 return ret;
623 }
624