Sat, 01 Mar 2014 17:04:55 -0800
Fix sizeof calls.
Sorry, forgot that sizeof(pointer to stack buffer) doesn't do what
I want.
| 5005 | 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 | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
79 | #include "debug.h" |
| 5005 | 80 | #include "untar.h" |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
81 | #include <glib.h> |
| 5005 | 82 | |
|
29492
25c9a945380f
Kill unneeded GLIB_CHECK_VERSION uses in Pidgin. Refs #10024.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
26573
diff
changeset
|
83 | #include <glib/gstdio.h> |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
84 | |
| 15884 | 85 | #define untar_error( error, args... ) purple_debug(PURPLE_DEBUG_ERROR, "untar", error, ## args ) |
| 86 | #define untar_warning( warning, args... ) purple_debug(PURPLE_DEBUG_WARNING, "untar", warning, ## args ) | |
| 87 | #define untar_verbose( args... ) purple_debug(PURPLE_DEBUG_INFO, "untar", ## args ) | |
|
22687
3cb9f701d421
printf format warning fixes.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
88 | |
| 5005 | 89 | #define WSIZE 32768 /* size of decompression buffer */ |
| 90 | #define TSIZE 512 /* size of a "tape" block */ | |
| 91 | #define CR 13 /* carriage-return character */ | |
| 92 | #define LF 10 /* line-feed character */ | |
| 93 | ||
| 94 | typedef unsigned char Uchar_t; | |
| 95 | typedef unsigned short Ushort_t; | |
| 96 | typedef unsigned long Ulong_t; | |
| 97 | ||
| 98 | typedef struct | |
| 99 | { | |
| 100 | char filename[100]; /* 0 name of next file */ | |
| 101 | char mode[8]; /* 100 Permissions and type (octal digits) */ | |
| 102 | char owner[8]; /* 108 Owner ID (ignored) */ | |
| 103 | char group[8]; /* 116 Group ID (ignored) */ | |
| 104 | char size[12]; /* 124 Bytes in file (octal digits) */ | |
| 105 | char mtime[12]; /* 136 Modification time stamp (octal digits)*/ | |
| 106 | char checksum[8]; /* 148 Header checksum (ignored) */ | |
| 107 | char type; /* 156 File type (see below) */ | |
| 108 | char linkto[100]; /* 157 Linked-to name */ | |
| 109 | char brand[8]; /* 257 Identifies tar version (ignored) */ | |
| 110 | char ownername[32]; /* 265 Name of owner (ignored) */ | |
| 111 | char groupname[32]; /* 297 Name of group (ignored) */ | |
| 112 | char devmajor[8]; /* 329 Device major number (ignored) */ | |
| 113 | char defminor[8]; /* 337 Device minor number (ignored) */ | |
| 114 | char prefix[155]; /* 345 Prefix of name (optional) */ | |
| 115 | char RESERVED[12]; /* 500 Pad header size to 512 bytes */ | |
| 116 | } tar_t; | |
| 117 | #define ISREGULAR(hdr) ((hdr).type < '1' || (hdr).type > '6') | |
| 118 | ||
| 119 | Uchar_t slide[WSIZE]; | |
| 120 | ||
| 121 | static const char *inname = NULL; /* name of input archive */ | |
| 122 | static FILE *infp = NULL; /* input byte stream */ | |
| 123 | static FILE *outfp = NULL; /* output stream, for file currently being extracted */ | |
| 124 | static Ulong_t outsize = 0; /* number of bytes remainin in file currently being extracted */ | |
| 125 | static char **only = NULL; /* array of filenames to extract/list */ | |
| 126 | static int nonlys = 0; /* number of filenames in "only" array; 0=extract all */ | |
| 127 | static int didabs = 0; /* were any filenames affected by the absence of -p? */ | |
| 128 | ||
| 129 | static untar_opt untarops = 0; /* Untar options */ | |
| 130 | ||
| 131 | /* Options checked during untar process */ | |
| 132 | #define LISTING (untarops & UNTAR_LISTING) /* 1 if listing, 0 if extracting */ | |
| 133 | #define QUIET (untarops & UNTAR_QUIET) /* 1 to write nothing to stdout, 0 for normal chatter */ | |
| 134 | #define VERBOSE (untarops & UNTAR_VERBOSE) /* 1 to write extra information to stdout */ | |
| 135 | #define FORCE (untarops & UNTAR_FORCE) /* 1 to overwrite existing files, 0 to skip them */ | |
| 136 | #define ABSPATH (untarops & UNTAR_ABSPATH) /* 1 to allow leading '/', 0 to strip leading '/' */ | |
| 137 | #define CONVERT (untarops & UNTAR_CONVERT) /* 1 to convert newlines, 0 to leave unchanged */ | |
| 138 | ||
| 139 | /*----------------------------------------------------------------------------*/ | |
| 140 | ||
| 141 | /* create a file for writing. If necessary, create the directories leading up | |
| 142 | * to that file as well. | |
| 143 | */ | |
| 144 | static FILE *createpath(name) | |
| 145 | char *name; /* pathname of file to create */ | |
| 146 | { | |
| 147 | FILE *fp; | |
| 148 | int i; | |
| 149 | ||
| 150 | /* if we aren't allowed to overwrite and this file exists, return NULL */ | |
| 151 | if (!FORCE && access(name, 0) == 0) | |
| 152 | { | |
| 153 | untar_warning("%s: exists, will not overwrite without \"FORCE option\"\n", name); | |
| 154 | return NULL; | |
| 155 | } | |
| 156 | ||
| 157 | /* first try creating it the easy way */ | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
158 | fp = g_fopen(name, CONVERT ? "w" : "wb"); |
| 5005 | 159 | if (fp) |
| 160 | return fp; | |
| 161 | ||
| 162 | /* Else try making all of its directories, and then try creating | |
| 163 | * the file again. | |
| 164 | */ | |
| 165 | for (i = 0; name[i]; i++) | |
| 166 | { | |
| 167 | /* If this is a slash, then temporarily replace the '/' | |
| 168 | * with a '\0' and do a mkdir() on the resulting string. | |
| 169 | * Ignore errors for now. | |
| 170 | */ | |
| 171 | if (name[i] == '/') | |
| 172 | { | |
| 173 | name[i] = '\0'; | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
174 | (void)g_mkdir(name, 0777); |
| 5005 | 175 | name[i] = '/'; |
| 176 | } | |
| 177 | } | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
178 | fp = g_fopen(name, CONVERT ? "w" : "wb"); |
| 5005 | 179 | if (!fp) |
| 180 | untar_error("Error opening: %s\n", name); | |
| 181 | return fp; | |
| 182 | } | |
| 183 | ||
| 184 | /* Create a link, or copy a file. If the file is copied (not linked) then | |
| 185 | * give a warning. | |
| 186 | */ | |
| 187 | static void linkorcopy(src, dst, sym) | |
| 188 | char *src; /* name of existing source file */ | |
| 189 | char *dst; /* name of new destination file */ | |
| 190 | int sym; /* use symlink instead of link */ | |
| 191 | { | |
| 192 | FILE *fpsrc; | |
| 193 | FILE *fpdst; | |
| 194 | int c; | |
| 195 | ||
| 196 | /* Open the source file. We do this first to make sure it exists */ | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
197 | fpsrc = g_fopen(src, "rb"); |
| 5005 | 198 | if (!fpsrc) |
| 199 | { | |
| 200 | untar_error("Error opening: %s\n", src); | |
| 201 | return; | |
| 202 | } | |
| 203 | ||
| 204 | /* Create the destination file. On POSIX systems, this is just to | |
| 205 | * make sure the directory path exists. | |
| 206 | */ | |
| 207 | fpdst = createpath(dst); | |
|
26573
24c7d3ba4e9b
Don't leak an fd if we can't open the destination file when untarring
Mark Doliner <markdoliner@pidgin.im>
parents:
22687
diff
changeset
|
208 | if (!fpdst) { |
| 5005 | 209 | /* error message already given */ |
|
26573
24c7d3ba4e9b
Don't leak an fd if we can't open the destination file when untarring
Mark Doliner <markdoliner@pidgin.im>
parents:
22687
diff
changeset
|
210 | fclose(fpsrc); |
| 5005 | 211 | return; |
|
26573
24c7d3ba4e9b
Don't leak an fd if we can't open the destination file when untarring
Mark Doliner <markdoliner@pidgin.im>
parents:
22687
diff
changeset
|
212 | } |
| 5005 | 213 | |
| 214 | #ifdef _POSIX_SOURCE | |
| 215 | # ifndef _WEAK_POSIX | |
| 216 | /* first try to link it over, instead of copying */ | |
| 217 | fclose(fpdst); | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
218 | g_unlink(dst); |
| 5005 | 219 | if (sym) |
| 220 | { | |
| 221 | if (symlink(src, dst)) | |
| 222 | { | |
| 223 | perror(dst); | |
| 224 | } | |
| 225 | fclose(fpsrc); | |
| 226 | return; | |
| 227 | } | |
| 228 | if (!link(src, dst)) | |
| 229 | { | |
| 230 | /* This story had a happy ending */ | |
| 231 | fclose(fpsrc); | |
| 232 | return; | |
| 233 | } | |
| 234 | ||
| 235 | /* Dang. Reopen the destination again */ | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
236 | fpdst = g_fopen(dst, "wb"); |
| 5005 | 237 | /* This *can't* fail */ |
| 238 | ||
| 239 | # endif /* _WEAK_POSIX */ | |
| 240 | #endif /* _POSIX_SOURCE */ | |
| 241 | ||
| 242 | /* Copy characters */ | |
| 243 | while ((c = getc(fpsrc)) != EOF) | |
| 244 | putc(c, fpdst); | |
| 245 | ||
| 246 | /* Close the files */ | |
| 247 | fclose(fpsrc); | |
| 248 | fclose(fpdst); | |
| 249 | ||
| 250 | /* Give a warning */ | |
| 251 | untar_warning("%s: copy instead of link\n", dst); | |
| 252 | } | |
| 253 | ||
| 254 | /* This calls fwrite(), possibly after converting CR-LF to LF */ | |
| 255 | static void cvtwrite(blk, size, fp) | |
| 256 | Uchar_t *blk; /* the block to be written */ | |
| 257 | Ulong_t size; /* number of characters to be written */ | |
| 258 | FILE *fp; /* file to write to */ | |
| 259 | { | |
| 260 | int i, j; | |
| 261 | static Uchar_t mod[TSIZE]; | |
| 262 | ||
| 263 | if (CONVERT) | |
| 264 | { | |
| 265 | for (i = j = 0; i < size; i++) | |
| 266 | { | |
| 267 | /* convert LF to local newline convention */ | |
| 268 | if (blk[i] == LF) | |
| 269 | mod[j++] = '\n'; | |
| 270 | /* If CR-LF pair, then delete the CR */ | |
| 271 | else if (blk[i] == CR && (i+1 >= size || blk[i+1] == LF)) | |
| 272 | ; | |
| 273 | /* other characters copied literally */ | |
| 274 | else | |
| 275 | mod[j++] = blk[i]; | |
| 276 | } | |
| 277 | size = j; | |
| 278 | blk = mod; | |
| 279 | } | |
| 280 | ||
| 281 | fwrite(blk, (size_t)size, sizeof(Uchar_t), fp); | |
| 282 | } | |
| 283 | ||
| 284 | ||
| 285 | /* Compute the checksum of a tar header block, and return it as a long int. | |
| 286 | * The checksum can be computed using either POSIX rules (unsigned bytes) | |
| 287 | * or Sun rules (signed bytes). | |
| 288 | */ | |
| 289 | static long checksum(tblk, sunny) | |
| 290 | tar_t *tblk; /* buffer containing the tar header block */ | |
| 291 | int sunny; /* Boolean: Sun-style checksums? (else POSIX) */ | |
| 292 | { | |
| 293 | long sum; | |
| 294 | char *scan; | |
| 295 | ||
| 296 | /* compute the sum of the first 148 bytes -- everything up to but not | |
| 297 | * including the checksum field itself. | |
| 298 | */ | |
| 299 | sum = 0L; | |
| 300 | for (scan = (char *)tblk; scan < tblk->checksum; scan++) | |
| 301 | { | |
| 302 | sum += (*scan) & 0xff; | |
| 303 | if (sunny && (*scan & 0x80) != 0) | |
| 304 | sum -= 256; | |
| 305 | } | |
| 306 | ||
| 307 | /* for the 8 bytes of the checksum field, add blanks to the sum */ | |
| 308 | sum += ' ' * sizeof tblk->checksum; | |
| 309 | scan += sizeof tblk->checksum; | |
| 310 | ||
| 311 | /* finish counting the sum of the rest of the block */ | |
| 312 | for (; scan < (char *)tblk + sizeof *tblk; scan++) | |
| 313 | { | |
| 314 | sum += (*scan) & 0xff; | |
| 315 | if (sunny && (*scan & 0x80) != 0) | |
| 316 | sum -= 256; | |
| 317 | } | |
| 318 | ||
| 319 | return sum; | |
| 320 | } | |
| 321 | ||
| 322 | ||
| 323 | ||
| 324 | /* list files in an archive, and optionally extract them as well */ | |
| 325 | static int untar_block(Uchar_t *blk) { | |
|
36171
89c89c449595
Use a larger buffer size for untarred filenames.
Mark Doliner <mark@kingant.net>
parents:
31105
diff
changeset
|
326 | static char nbuf[4096];/* storage space for prefix+name, combined */ |
| 5005 | 327 | static char *name,*n2;/* prefix and name, combined */ |
| 328 | static int first = 1;/* Boolean: first block of archive? */ | |
| 329 | long sum; /* checksum for this block */ | |
| 330 | int i; | |
| 331 | tar_t tblk[1]; | |
| 332 | ||
| 333 | #ifdef _POSIX_SOURCE | |
| 334 | static mode_t mode; /* file permissions */ | |
| 335 | static struct utimbuf timestamp; /* file timestamp */ | |
| 336 | #endif | |
| 337 | ||
| 338 | /* make a local copy of the block, and treat it as a tar header */ | |
| 339 | tblk[0] = *(tar_t *)blk; | |
| 340 | ||
| 341 | /* process each type of tape block differently */ | |
| 342 | if (outsize > TSIZE) | |
| 343 | { | |
| 344 | /* data block, but not the last one */ | |
| 345 | if (outfp) | |
| 346 | cvtwrite(blk, (Ulong_t)TSIZE, outfp); | |
| 347 | outsize -= TSIZE; | |
| 348 | } | |
| 349 | else if (outsize > 0) | |
| 350 | { | |
| 351 | /* last data block of current file */ | |
| 352 | if (outfp) | |
| 353 | { | |
| 354 | cvtwrite(blk, outsize, outfp); | |
| 355 | fclose(outfp); | |
| 356 | outfp = NULL; | |
| 357 | #ifdef _POSIX_SOURCE | |
| 358 | utime(nbuf, ×tamp); | |
| 359 | chmod(nbuf, mode); | |
| 360 | #endif | |
| 361 | } | |
| 362 | outsize = 0; | |
| 363 | } | |
| 364 | else if ((tblk)->filename[0] == '\0') | |
| 365 | { | |
| 366 | /* end-of-archive marker */ | |
| 367 | if (didabs) | |
| 368 | untar_warning("Removed leading slashes because \"ABSPATH option\" wasn't given.\n"); | |
| 369 | return 1; | |
| 370 | } | |
| 371 | else | |
| 372 | { | |
| 373 | /* file header */ | |
|
22687
3cb9f701d421
printf format warning fixes.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
374 | |
| 5005 | 375 | /* half-assed verification -- does it look like header? */ |
| 376 | if ((tblk)->filename[99] != '\0' | |
| 377 | || ((tblk)->size[0] < '0' | |
| 378 | && (tblk)->size[0] != ' ') | |
| 379 | || (tblk)->size[0] > '9') | |
| 380 | { | |
| 381 | if (first) | |
| 382 | { | |
| 383 | untar_error("%s: not a valid tar file\n", inname); | |
| 384 | return 0; | |
| 385 | } | |
| 386 | else | |
| 387 | { | |
| 388 | untar_error("Garbage detected; preceding file may be damaged\n"); | |
| 389 | return 0; | |
| 390 | } | |
| 391 | } | |
| 392 | ||
| 393 | /* combine prefix and filename */ | |
| 394 | memset(nbuf, 0, sizeof nbuf); | |
| 395 | if ((tblk)->prefix[0]) | |
| 396 | { | |
| 36174 | 397 | snprintf(nbuf, sizeof(nbuf), "%s/%s", |
|
36172
8f870b9011c9
Fix incorrect uses of strncpy().
Mark Doliner <mark@kingant.net>
parents:
36171
diff
changeset
|
398 | (tblk)->prefix, (tblk)->filename); |
| 5005 | 399 | } |
| 400 | else | |
| 401 | { | |
| 36174 | 402 | g_strlcpy(nbuf, (tblk)->filename, |
| 403 | sizeof (nbuf)); | |
| 5005 | 404 | } |
| 405 | ||
| 406 | /* Convert any backslashes to forward slashes, and guard | |
| 407 | * against doubled-up slashes. (Some DOS versions of "tar" | |
| 408 | * get this wrong.) Also strip off leading slashes. | |
| 409 | */ | |
| 36174 | 410 | name = nbuf; |
| 5005 | 411 | if (!ABSPATH && (*name == '/' || *name == '\\')) |
| 412 | didabs = 1; | |
| 413 | for (n2 = nbuf; *name; name++) | |
| 414 | { | |
| 415 | if (*name == '\\') | |
| 416 | *name = '/'; | |
| 417 | if (*name != '/' | |
| 418 | || (ABSPATH && n2 == nbuf) | |
| 419 | || (n2 != nbuf && n2[-1] != '/')) | |
| 420 | *n2++ = *name; | |
| 421 | } | |
| 422 | if (n2 == nbuf) | |
| 423 | *n2++ = '/'; | |
| 424 | *n2 = '\0'; | |
| 425 | ||
| 426 | /* verify the checksum */ | |
| 427 | for (sum = 0L, i = 0; i < sizeof((tblk)->checksum); i++) | |
| 428 | { | |
| 429 | if ((tblk)->checksum[i] >= '0' | |
| 430 | && (tblk)->checksum[i] <= '7') | |
| 431 | sum = sum * 8 + (tblk)->checksum[i] - '0'; | |
| 432 | } | |
| 433 | if (sum != checksum(tblk, 0) && sum != checksum(tblk, 1)) | |
| 434 | { | |
| 435 | if (!first) | |
| 436 | untar_error("Garbage detected; preceding file may be damaged\n"); | |
| 437 | untar_error("%s: header has bad checksum for %s\n", inname, nbuf); | |
| 438 | return 0; | |
| 439 | } | |
| 440 | ||
| 441 | /* From this point on, we don't care whether this is the first | |
| 442 | * block or not. Might as well reset the "first" flag now. | |
| 443 | */ | |
| 444 | first = 0; | |
| 445 | ||
| 446 | /* if last character of name is '/' then assume directory */ | |
| 447 | if (*nbuf && nbuf[strlen(nbuf) - 1] == '/') | |
| 448 | (tblk)->type = '5'; | |
| 449 | ||
| 450 | /* convert file size */ | |
| 451 | for (outsize = 0L, i = 0; i < sizeof((tblk)->size); i++) | |
| 452 | { | |
| 453 | if ((tblk)->size[i] >= '0' && (tblk)->size[i] <= '7') | |
| 454 | outsize = outsize * 8 + (tblk)->size[i] - '0'; | |
| 455 | } | |
| 456 | ||
| 457 | #ifdef _POSIX_SOURCE | |
| 458 | /* convert file timestamp */ | |
| 459 | for (timestamp.modtime=0L, i=0; i < sizeof((tblk)->mtime); i++) | |
| 460 | { | |
| 461 | if ((tblk)->mtime[i] >= '0' && (tblk)->mtime[i] <= '7') | |
| 462 | timestamp.modtime = timestamp.modtime * 8 | |
| 463 | + (tblk)->mtime[i] - '0'; | |
| 464 | } | |
| 465 | timestamp.actime = timestamp.modtime; | |
| 466 | ||
| 467 | /* convert file permissions */ | |
| 468 | for (mode = i = 0; i < sizeof((tblk)->mode); i++) | |
| 469 | { | |
| 470 | if ((tblk)->mode[i] >= '0' && (tblk)->mode[i] <= '7') | |
| 471 | mode = mode * 8 + (tblk)->mode[i] - '0'; | |
| 472 | } | |
| 473 | #endif | |
| 474 | ||
| 475 | /* If we have an "only" list, and this file isn't in it, | |
| 476 | * then skip it. | |
| 477 | */ | |
| 478 | if (nonlys > 0) | |
| 479 | { | |
| 480 | for (i = 0; | |
| 481 | i < nonlys | |
| 482 | && strcmp(only[i], nbuf) | |
| 483 | && (strncmp(only[i], nbuf, strlen(only[i])) | |
| 484 | || nbuf[strlen(only[i])] != '/'); | |
| 485 | i++) | |
| 486 | { | |
| 487 | } | |
| 488 | if (i >= nonlys) | |
| 489 | { | |
| 490 | outfp = NULL; | |
| 491 | return 1; | |
| 492 | } | |
| 493 | } | |
| 494 | ||
| 495 | /* list the file */ | |
| 496 | if (VERBOSE) | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
497 | untar_verbose("%c %s", |
| 5005 | 498 | ISREGULAR(*tblk) ? '-' : ("hlcbdp"[(tblk)->type - '1']), |
| 499 | nbuf); | |
| 500 | else if (!QUIET) | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
501 | untar_verbose("%s\n", nbuf); |
| 5005 | 502 | |
| 503 | /* if link, then do the link-or-copy thing */ | |
| 504 | if (tblk->type == '1' || tblk->type == '2') | |
| 505 | { | |
| 506 | if (VERBOSE) | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
507 | untar_verbose(" -> %s\n", tblk->linkto); |
| 5005 | 508 | if (!LISTING) |
| 509 | linkorcopy(tblk->linkto, nbuf, tblk->type == '2'); | |
| 510 | outsize = 0L; | |
| 511 | return 1; | |
| 512 | } | |
| 513 | ||
| 514 | /* If directory, then make a weak attempt to create it. | |
| 515 | * Ideally we would do the "create path" thing, but that | |
| 516 | * seems like more trouble than it's worth since traditional | |
| 517 | * tar archives don't contain directories anyway. | |
| 518 | */ | |
| 519 | if (tblk->type == '5') | |
| 520 | { | |
| 521 | if (LISTING) | |
| 522 | n2 = " directory"; | |
| 523 | #ifdef _POSIX_SOURCE | |
| 524 | else if (mkdir(nbuf, mode) == 0) | |
| 525 | #else | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
526 | else if (g_mkdir(nbuf, 0755) == 0) |
| 5005 | 527 | #endif |
| 528 | n2 = " created"; | |
| 529 | else | |
| 530 | n2 = " ignored"; | |
| 531 | if (VERBOSE) | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
532 | untar_verbose("%s\n", n2); |
| 5005 | 533 | return 1; |
| 534 | } | |
| 535 | ||
| 536 | /* if not a regular file, then skip it */ | |
| 537 | if (!ISREGULAR(*tblk)) | |
| 538 | { | |
| 539 | if (VERBOSE) | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
540 | untar_verbose(" ignored\n"); |
| 5005 | 541 | outsize = 0L; |
| 542 | return 1; | |
| 543 | } | |
| 544 | ||
| 545 | /* print file statistics */ | |
| 546 | if (VERBOSE) | |
| 547 | { | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
548 | untar_verbose(" (%ld byte%s, %ld tape block%s)\n", |
| 5005 | 549 | outsize, |
| 550 | outsize == 1 ? "" : "s", | |
| 551 | (outsize + TSIZE - 1) / TSIZE, | |
| 552 | (outsize > 0 && outsize <= TSIZE) ? "" : "s"); | |
| 553 | } | |
| 554 | ||
| 555 | /* if extracting, then try to create the file */ | |
| 556 | if (!LISTING) | |
| 557 | outfp = createpath(nbuf); | |
| 558 | else | |
| 559 | outfp = NULL; | |
| 560 | ||
| 561 | /* if file is 0 bytes long, then we're done already! */ | |
| 562 | if (outsize == 0 && outfp) | |
| 563 | { | |
| 564 | fclose(outfp); | |
| 565 | #ifdef _POSIX_SOURCE | |
| 566 | utime(nbuf, ×tamp); | |
| 567 | chmod(nbuf, mode); | |
| 568 | #endif | |
| 569 | } | |
| 570 | } | |
| 571 | return 1; | |
| 572 | } | |
| 573 | ||
| 574 | /* Process an archive file. This involves reading the blocks one at a time | |
| 575 | * and passing them to a untar() function. | |
| 576 | */ | |
| 577 | int untar(const char *filename, const char* destdir, untar_opt options) { | |
| 578 | int ret=1; | |
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
579 | wchar_t curdir[_MAX_PATH]; |
|
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
580 | wchar_t *w_destdir; |
| 5005 | 581 | untarops = options; |
| 582 | /* open the archive */ | |
| 583 | inname = filename; | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
584 | infp = g_fopen(filename, "rb"); |
| 5005 | 585 | if (!infp) |
| 586 | { | |
| 587 | untar_error("Error opening: %s\n", filename); | |
| 588 | return 0; | |
| 589 | } | |
|
22687
3cb9f701d421
printf format warning fixes.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
590 | |
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
591 | w_destdir = g_utf8_to_utf16(destdir, -1, NULL, NULL, NULL); |
|
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
592 | |
| 5005 | 593 | /* Set current directory */ |
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
594 | if(!GetCurrentDirectoryW(_MAX_PATH, curdir)) { |
|
22687
3cb9f701d421
printf format warning fixes.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
595 | untar_error("Could not get current directory (error %lu).\n", GetLastError()); |
| 5005 | 596 | fclose(infp); |
| 597 | return 0; | |
| 598 | } | |
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
599 | if(!SetCurrentDirectoryW(w_destdir)) { |
|
22687
3cb9f701d421
printf format warning fixes.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
600 | untar_error("Could not set current directory to (error %lu): %s\n", GetLastError(), destdir); |
| 5005 | 601 | fclose(infp); |
| 602 | return 0; | |
| 603 | } else { | |
| 604 | /* UNCOMPRESSED */ | |
| 605 | /* send each block to the untar_block() function */ | |
| 606 | while (fread(slide, 1, TSIZE, infp) == TSIZE) { | |
| 607 | if(!untar_block(slide)) { | |
| 608 | untar_error("untar failure: %s\n", filename); | |
| 609 | fclose(infp); | |
| 610 | ret=0; | |
| 611 | } | |
| 612 | } | |
| 613 | if (outsize > 0 && ret) { | |
| 614 | untar_warning("Last file might be truncated!\n"); | |
| 615 | fclose(outfp); | |
| 616 | outfp = NULL; | |
| 617 | } | |
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
618 | if(!SetCurrentDirectoryW(curdir)) { |
|
22687
3cb9f701d421
printf format warning fixes.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
619 | untar_error("Could not set current dir back to original (error %lu).\n", GetLastError()); |
| 5005 | 620 | ret=0; |
| 621 | } | |
| 622 | } | |
| 623 | ||
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
624 | g_free(w_destdir); |
|
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
625 | |
| 5005 | 626 | /* close the archive file. */ |
| 627 | fclose(infp); | |
| 628 | ||
| 629 | return ret; | |
| 630 | } | |
| 631 |