Mon, 23 Sep 2019 22:00:30 -0400
Rename app icon using reverse domain.
This is the name that flatpak looks for, and matches the desktop file name.
| 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 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 */ | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
156 | fp = g_fopen(name, CONVERT ? "w" : "wb"); |
| 5005 | 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'; | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
172 | (void)g_mkdir(name, 0777); |
| 5005 | 173 | name[i] = '/'; |
| 174 | } | |
| 175 | } | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
176 | fp = g_fopen(name, CONVERT ? "w" : "wb"); |
| 5005 | 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 */ | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
195 | fpsrc = g_fopen(src, "rb"); |
| 5005 | 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); | |
|
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
|
206 | if (!fpdst) { |
| 5005 | 207 | /* 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
|
208 | fclose(fpsrc); |
| 5005 | 209 | 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
|
210 | } |
| 5005 | 211 | |
| 212 | #ifdef _POSIX_SOURCE | |
| 213 | # ifndef _WEAK_POSIX | |
| 214 | /* first try to link it over, instead of copying */ | |
| 215 | fclose(fpdst); | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
216 | g_unlink(dst); |
| 5005 | 217 | if (sym) |
| 218 | { | |
| 219 | if (symlink(src, dst)) | |
| 220 | { | |
| 221 | perror(dst); | |
| 222 | } | |
| 223 | fclose(fpsrc); | |
| 224 | return; | |
| 225 | } | |
| 226 | if (!link(src, dst)) | |
| 227 | { | |
| 228 | /* This story had a happy ending */ | |
| 229 | fclose(fpsrc); | |
| 230 | return; | |
| 231 | } | |
| 232 | ||
| 233 | /* Dang. Reopen the destination again */ | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
234 | fpdst = g_fopen(dst, "wb"); |
| 5005 | 235 | /* This *can't* fail */ |
| 236 | ||
| 237 | # endif /* _WEAK_POSIX */ | |
| 238 | #endif /* _POSIX_SOURCE */ | |
| 239 | ||
| 240 | /* Copy characters */ | |
| 241 | while ((c = getc(fpsrc)) != EOF) | |
| 242 | putc(c, fpdst); | |
| 243 | ||
| 244 | /* Close the files */ | |
| 245 | fclose(fpsrc); | |
| 246 | fclose(fpdst); | |
| 247 | ||
| 248 | /* Give a warning */ | |
| 249 | untar_warning("%s: copy instead of link\n", dst); | |
| 250 | } | |
| 251 | ||
| 252 | /* This calls fwrite(), possibly after converting CR-LF to LF */ | |
| 253 | static void cvtwrite(blk, size, fp) | |
| 254 | Uchar_t *blk; /* the block to be written */ | |
| 255 | Ulong_t size; /* number of characters to be written */ | |
| 256 | FILE *fp; /* file to write to */ | |
| 257 | { | |
|
35858
ec92c3bfaf27
cross-win32: fix remaining warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
31105
diff
changeset
|
258 | Ulong_t i, j; |
| 5005 | 259 | static Uchar_t mod[TSIZE]; |
| 260 | ||
| 261 | if (CONVERT) | |
| 262 | { | |
| 263 | for (i = j = 0; i < size; i++) | |
| 264 | { | |
| 265 | /* convert LF to local newline convention */ | |
| 266 | if (blk[i] == LF) | |
| 267 | mod[j++] = '\n'; | |
| 268 | /* If CR-LF pair, then delete the CR */ | |
| 269 | else if (blk[i] == CR && (i+1 >= size || blk[i+1] == LF)) | |
| 270 | ; | |
| 271 | /* other characters copied literally */ | |
| 272 | else | |
| 273 | mod[j++] = blk[i]; | |
| 274 | } | |
| 275 | size = j; | |
| 276 | blk = mod; | |
| 277 | } | |
| 278 | ||
| 279 | fwrite(blk, (size_t)size, sizeof(Uchar_t), fp); | |
| 280 | } | |
| 281 | ||
| 282 | ||
| 283 | /* Compute the checksum of a tar header block, and return it as a long int. | |
| 284 | * The checksum can be computed using either POSIX rules (unsigned bytes) | |
| 285 | * or Sun rules (signed bytes). | |
| 286 | */ | |
| 287 | static long checksum(tblk, sunny) | |
| 288 | tar_t *tblk; /* buffer containing the tar header block */ | |
| 289 | int sunny; /* Boolean: Sun-style checksums? (else POSIX) */ | |
| 290 | { | |
| 291 | long sum; | |
| 292 | char *scan; | |
| 293 | ||
| 294 | /* compute the sum of the first 148 bytes -- everything up to but not | |
| 295 | * including the checksum field itself. | |
| 296 | */ | |
| 297 | sum = 0L; | |
| 298 | for (scan = (char *)tblk; scan < tblk->checksum; scan++) | |
| 299 | { | |
| 300 | sum += (*scan) & 0xff; | |
| 301 | if (sunny && (*scan & 0x80) != 0) | |
| 302 | sum -= 256; | |
| 303 | } | |
| 304 | ||
| 305 | /* for the 8 bytes of the checksum field, add blanks to the sum */ | |
| 306 | sum += ' ' * sizeof tblk->checksum; | |
| 307 | scan += sizeof tblk->checksum; | |
| 308 | ||
| 309 | /* finish counting the sum of the rest of the block */ | |
| 310 | for (; scan < (char *)tblk + sizeof *tblk; scan++) | |
| 311 | { | |
| 312 | sum += (*scan) & 0xff; | |
| 313 | if (sunny && (*scan & 0x80) != 0) | |
| 314 | sum -= 256; | |
| 315 | } | |
| 316 | ||
| 317 | return sum; | |
| 318 | } | |
| 319 | ||
| 320 | ||
| 321 | ||
| 322 | /* list files in an archive, and optionally extract them as well */ | |
| 323 | 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
|
324 | static char nbuf[4096];/* storage space for prefix+name, combined */ |
| 5005 | 325 | static char *name,*n2;/* prefix and name, combined */ |
| 326 | static int first = 1;/* Boolean: first block of archive? */ | |
| 327 | long sum; /* checksum for this block */ | |
|
35858
ec92c3bfaf27
cross-win32: fix remaining warnings
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
31105
diff
changeset
|
328 | guint i; |
| 5005 | 329 | tar_t tblk[1]; |
| 330 | ||
| 331 | #ifdef _POSIX_SOURCE | |
| 332 | static mode_t mode; /* file permissions */ | |
| 333 | static struct utimbuf timestamp; /* file timestamp */ | |
| 334 | #endif | |
| 335 | ||
| 336 | /* make a local copy of the block, and treat it as a tar header */ | |
| 337 | tblk[0] = *(tar_t *)blk; | |
| 338 | ||
| 339 | /* process each type of tape block differently */ | |
| 340 | if (outsize > TSIZE) | |
| 341 | { | |
| 342 | /* data block, but not the last one */ | |
| 343 | if (outfp) | |
| 344 | cvtwrite(blk, (Ulong_t)TSIZE, outfp); | |
| 345 | outsize -= TSIZE; | |
| 346 | } | |
| 347 | else if (outsize > 0) | |
| 348 | { | |
| 349 | /* last data block of current file */ | |
| 350 | if (outfp) | |
| 351 | { | |
| 352 | cvtwrite(blk, outsize, outfp); | |
| 353 | fclose(outfp); | |
| 354 | outfp = NULL; | |
| 355 | #ifdef _POSIX_SOURCE | |
| 356 | utime(nbuf, ×tamp); | |
| 357 | chmod(nbuf, mode); | |
| 358 | #endif | |
| 359 | } | |
| 360 | outsize = 0; | |
| 361 | } | |
| 362 | else if ((tblk)->filename[0] == '\0') | |
| 363 | { | |
| 364 | /* end-of-archive marker */ | |
| 365 | if (didabs) | |
| 366 | untar_warning("Removed leading slashes because \"ABSPATH option\" wasn't given.\n"); | |
| 367 | return 1; | |
| 368 | } | |
| 369 | else | |
| 370 | { | |
| 371 | /* file header */ | |
|
22687
3cb9f701d421
printf format warning fixes.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
372 | |
| 5005 | 373 | /* half-assed verification -- does it look like header? */ |
| 374 | if ((tblk)->filename[99] != '\0' | |
| 375 | || ((tblk)->size[0] < '0' | |
| 376 | && (tblk)->size[0] != ' ') | |
| 377 | || (tblk)->size[0] > '9') | |
| 378 | { | |
| 379 | if (first) | |
| 380 | { | |
| 381 | untar_error("%s: not a valid tar file\n", inname); | |
| 382 | return 0; | |
| 383 | } | |
| 384 | else | |
| 385 | { | |
| 386 | untar_error("Garbage detected; preceding file may be damaged\n"); | |
| 387 | return 0; | |
| 388 | } | |
| 389 | } | |
| 390 | ||
| 391 | /* combine prefix and filename */ | |
| 392 | memset(nbuf, 0, sizeof nbuf); | |
| 393 | if ((tblk)->prefix[0]) | |
| 394 | { | |
| 36174 | 395 | snprintf(nbuf, sizeof(nbuf), "%s/%s", |
|
36172
8f870b9011c9
Fix incorrect uses of strncpy().
Mark Doliner <mark@kingant.net>
parents:
36171
diff
changeset
|
396 | (tblk)->prefix, (tblk)->filename); |
| 5005 | 397 | } |
| 398 | else | |
| 399 | { | |
| 36174 | 400 | g_strlcpy(nbuf, (tblk)->filename, |
| 401 | sizeof (nbuf)); | |
| 5005 | 402 | } |
| 403 | ||
|
36177
68b8eb10977f
Fix a bug in the untar code that we use on Windows where we
Mark Doliner <mark@kingant.net>
parents:
36176
diff
changeset
|
404 | /* Possibly strip the drive from the path */ |
|
68b8eb10977f
Fix a bug in the untar code that we use on Windows where we
Mark Doliner <mark@kingant.net>
parents:
36176
diff
changeset
|
405 | if (!ABSPATH) { |
|
68b8eb10977f
Fix a bug in the untar code that we use on Windows where we
Mark Doliner <mark@kingant.net>
parents:
36176
diff
changeset
|
406 | /* If the path contains a colon, assume everything before the |
|
68b8eb10977f
Fix a bug in the untar code that we use on Windows where we
Mark Doliner <mark@kingant.net>
parents:
36176
diff
changeset
|
407 | * colon is intended to be a drive name and ignore it. This |
|
68b8eb10977f
Fix a bug in the untar code that we use on Windows where we
Mark Doliner <mark@kingant.net>
parents:
36176
diff
changeset
|
408 | * should be just a single drive letter, but it should be safe |
|
68b8eb10977f
Fix a bug in the untar code that we use on Windows where we
Mark Doliner <mark@kingant.net>
parents:
36176
diff
changeset
|
409 | * to drop it even if it's longer. */ |
|
68b8eb10977f
Fix a bug in the untar code that we use on Windows where we
Mark Doliner <mark@kingant.net>
parents:
36176
diff
changeset
|
410 | const char *lastcolon = strrchr(nbuf, ':'); |
|
68b8eb10977f
Fix a bug in the untar code that we use on Windows where we
Mark Doliner <mark@kingant.net>
parents:
36176
diff
changeset
|
411 | if (lastcolon) { |
|
68b8eb10977f
Fix a bug in the untar code that we use on Windows where we
Mark Doliner <mark@kingant.net>
parents:
36176
diff
changeset
|
412 | memmove(nbuf, lastcolon, strlen(lastcolon) + 1); |
|
68b8eb10977f
Fix a bug in the untar code that we use on Windows where we
Mark Doliner <mark@kingant.net>
parents:
36176
diff
changeset
|
413 | didabs = 1; /* Path was changed from absolute to relative */ |
|
68b8eb10977f
Fix a bug in the untar code that we use on Windows where we
Mark Doliner <mark@kingant.net>
parents:
36176
diff
changeset
|
414 | } |
| 5005 | 415 | } |
| 416 | ||
| 417 | /* Convert any backslashes to forward slashes, and guard | |
| 418 | * against doubled-up slashes. (Some DOS versions of "tar" | |
| 419 | * get this wrong.) Also strip off leading slashes. | |
| 420 | */ | |
| 36174 | 421 | name = nbuf; |
| 5005 | 422 | if (!ABSPATH && (*name == '/' || *name == '\\')) |
| 423 | didabs = 1; | |
| 424 | for (n2 = nbuf; *name; name++) | |
| 425 | { | |
| 426 | if (*name == '\\') | |
| 427 | *name = '/'; | |
| 428 | if (*name != '/' | |
| 429 | || (ABSPATH && n2 == nbuf) | |
| 430 | || (n2 != nbuf && n2[-1] != '/')) | |
| 431 | *n2++ = *name; | |
| 432 | } | |
| 433 | if (n2 == nbuf) | |
| 434 | *n2++ = '/'; | |
| 435 | *n2 = '\0'; | |
| 436 | ||
| 437 | /* verify the checksum */ | |
| 438 | for (sum = 0L, i = 0; i < sizeof((tblk)->checksum); i++) | |
| 439 | { | |
| 440 | if ((tblk)->checksum[i] >= '0' | |
| 441 | && (tblk)->checksum[i] <= '7') | |
| 442 | sum = sum * 8 + (tblk)->checksum[i] - '0'; | |
| 443 | } | |
| 444 | if (sum != checksum(tblk, 0) && sum != checksum(tblk, 1)) | |
| 445 | { | |
| 446 | if (!first) | |
| 447 | untar_error("Garbage detected; preceding file may be damaged\n"); | |
| 448 | untar_error("%s: header has bad checksum for %s\n", inname, nbuf); | |
| 449 | return 0; | |
| 450 | } | |
| 451 | ||
| 452 | /* From this point on, we don't care whether this is the first | |
| 453 | * block or not. Might as well reset the "first" flag now. | |
| 454 | */ | |
| 455 | first = 0; | |
| 456 | ||
| 457 | /* if last character of name is '/' then assume directory */ | |
| 458 | if (*nbuf && nbuf[strlen(nbuf) - 1] == '/') | |
| 459 | (tblk)->type = '5'; | |
| 460 | ||
| 461 | /* convert file size */ | |
| 462 | for (outsize = 0L, i = 0; i < sizeof((tblk)->size); i++) | |
| 463 | { | |
| 464 | if ((tblk)->size[i] >= '0' && (tblk)->size[i] <= '7') | |
| 465 | outsize = outsize * 8 + (tblk)->size[i] - '0'; | |
| 466 | } | |
| 467 | ||
| 468 | #ifdef _POSIX_SOURCE | |
| 469 | /* convert file timestamp */ | |
| 470 | for (timestamp.modtime=0L, i=0; i < sizeof((tblk)->mtime); i++) | |
| 471 | { | |
| 472 | if ((tblk)->mtime[i] >= '0' && (tblk)->mtime[i] <= '7') | |
| 473 | timestamp.modtime = timestamp.modtime * 8 | |
| 474 | + (tblk)->mtime[i] - '0'; | |
| 475 | } | |
| 476 | timestamp.actime = timestamp.modtime; | |
| 477 | ||
| 478 | /* convert file permissions */ | |
| 479 | for (mode = i = 0; i < sizeof((tblk)->mode); i++) | |
| 480 | { | |
| 481 | if ((tblk)->mode[i] >= '0' && (tblk)->mode[i] <= '7') | |
| 482 | mode = mode * 8 + (tblk)->mode[i] - '0'; | |
| 483 | } | |
| 484 | #endif | |
| 485 | ||
| 486 | /* list the file */ | |
| 487 | if (VERBOSE) | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
488 | untar_verbose("%c %s", |
| 5005 | 489 | ISREGULAR(*tblk) ? '-' : ("hlcbdp"[(tblk)->type - '1']), |
| 490 | nbuf); | |
| 491 | else if (!QUIET) | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
492 | untar_verbose("%s\n", nbuf); |
| 5005 | 493 | |
| 494 | /* if link, then do the link-or-copy thing */ | |
| 495 | if (tblk->type == '1' || tblk->type == '2') | |
| 496 | { | |
| 497 | if (VERBOSE) | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
498 | untar_verbose(" -> %s\n", tblk->linkto); |
| 5005 | 499 | if (!LISTING) |
| 500 | linkorcopy(tblk->linkto, nbuf, tblk->type == '2'); | |
| 501 | outsize = 0L; | |
| 502 | return 1; | |
| 503 | } | |
| 504 | ||
| 505 | /* If directory, then make a weak attempt to create it. | |
| 506 | * Ideally we would do the "create path" thing, but that | |
| 507 | * seems like more trouble than it's worth since traditional | |
| 508 | * tar archives don't contain directories anyway. | |
| 509 | */ | |
| 510 | if (tblk->type == '5') | |
| 511 | { | |
|
36176
f38a5005e8a8
Don't reuse the n2 variable.
Mark Doliner <mark@kingant.net>
parents:
36175
diff
changeset
|
512 | char *tmp; |
| 5005 | 513 | if (LISTING) |
|
36176
f38a5005e8a8
Don't reuse the n2 variable.
Mark Doliner <mark@kingant.net>
parents:
36175
diff
changeset
|
514 | tmp = " directory"; |
| 5005 | 515 | #ifdef _POSIX_SOURCE |
| 516 | else if (mkdir(nbuf, mode) == 0) | |
| 517 | #else | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
518 | else if (g_mkdir(nbuf, 0755) == 0) |
| 5005 | 519 | #endif |
|
36176
f38a5005e8a8
Don't reuse the n2 variable.
Mark Doliner <mark@kingant.net>
parents:
36175
diff
changeset
|
520 | tmp = " created"; |
| 5005 | 521 | else |
|
36176
f38a5005e8a8
Don't reuse the n2 variable.
Mark Doliner <mark@kingant.net>
parents:
36175
diff
changeset
|
522 | tmp = " ignored"; |
| 5005 | 523 | if (VERBOSE) |
|
36176
f38a5005e8a8
Don't reuse the n2 variable.
Mark Doliner <mark@kingant.net>
parents:
36175
diff
changeset
|
524 | untar_verbose("%s\n", tmp); |
| 5005 | 525 | return 1; |
| 526 | } | |
| 527 | ||
| 528 | /* if not a regular file, then skip it */ | |
| 529 | if (!ISREGULAR(*tblk)) | |
| 530 | { | |
| 531 | if (VERBOSE) | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
532 | untar_verbose(" ignored\n"); |
| 5005 | 533 | outsize = 0L; |
| 534 | return 1; | |
| 535 | } | |
| 536 | ||
| 537 | /* print file statistics */ | |
| 538 | if (VERBOSE) | |
| 539 | { | |
|
6425
3e86c949c98a
[gaim-migrate @ 6933]
Herman Bloggs <herman@bluedigits.com>
parents:
5005
diff
changeset
|
540 | untar_verbose(" (%ld byte%s, %ld tape block%s)\n", |
| 5005 | 541 | outsize, |
| 542 | outsize == 1 ? "" : "s", | |
| 543 | (outsize + TSIZE - 1) / TSIZE, | |
| 544 | (outsize > 0 && outsize <= TSIZE) ? "" : "s"); | |
| 545 | } | |
| 546 | ||
| 547 | /* if extracting, then try to create the file */ | |
| 548 | if (!LISTING) | |
| 549 | outfp = createpath(nbuf); | |
| 550 | else | |
| 551 | outfp = NULL; | |
| 552 | ||
| 553 | /* if file is 0 bytes long, then we're done already! */ | |
| 554 | if (outsize == 0 && outfp) | |
| 555 | { | |
| 556 | fclose(outfp); | |
| 557 | #ifdef _POSIX_SOURCE | |
| 558 | utime(nbuf, ×tamp); | |
| 559 | chmod(nbuf, mode); | |
| 560 | #endif | |
| 561 | } | |
| 562 | } | |
| 563 | return 1; | |
| 564 | } | |
| 565 | ||
| 566 | /* Process an archive file. This involves reading the blocks one at a time | |
| 567 | * and passing them to a untar() function. | |
| 568 | */ | |
| 569 | int untar(const char *filename, const char* destdir, untar_opt options) { | |
| 570 | int ret=1; | |
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
571 | 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
|
572 | wchar_t *w_destdir; |
| 5005 | 573 | untarops = options; |
| 574 | /* open the archive */ | |
| 575 | inname = filename; | |
|
10589
4e10236e06d4
[gaim-migrate @ 11994]
Daniel Atallah <datallah@pidgin.im>
parents:
7242
diff
changeset
|
576 | infp = g_fopen(filename, "rb"); |
| 5005 | 577 | if (!infp) |
| 578 | { | |
| 579 | untar_error("Error opening: %s\n", filename); | |
| 580 | return 0; | |
| 581 | } | |
|
22687
3cb9f701d421
printf format warning fixes.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
582 | |
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
583 | 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
|
584 | |
| 5005 | 585 | /* Set current directory */ |
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
586 | if(!GetCurrentDirectoryW(_MAX_PATH, curdir)) { |
|
22687
3cb9f701d421
printf format warning fixes.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
587 | untar_error("Could not get current directory (error %lu).\n", GetLastError()); |
| 5005 | 588 | fclose(infp); |
| 589 | return 0; | |
| 590 | } | |
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
591 | if(!SetCurrentDirectoryW(w_destdir)) { |
|
22687
3cb9f701d421
printf format warning fixes.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
592 | untar_error("Could not set current directory to (error %lu): %s\n", GetLastError(), destdir); |
| 5005 | 593 | fclose(infp); |
| 594 | return 0; | |
| 595 | } else { | |
| 596 | /* UNCOMPRESSED */ | |
| 597 | /* send each block to the untar_block() function */ | |
| 598 | while (fread(slide, 1, TSIZE, infp) == TSIZE) { | |
| 599 | if(!untar_block(slide)) { | |
| 600 | untar_error("untar failure: %s\n", filename); | |
| 601 | fclose(infp); | |
| 602 | ret=0; | |
| 603 | } | |
| 604 | } | |
| 605 | if (outsize > 0 && ret) { | |
| 606 | untar_warning("Last file might be truncated!\n"); | |
| 607 | fclose(outfp); | |
| 608 | outfp = NULL; | |
| 609 | } | |
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
610 | if(!SetCurrentDirectoryW(curdir)) { |
|
22687
3cb9f701d421
printf format warning fixes.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
611 | untar_error("Could not set current dir back to original (error %lu).\n", GetLastError()); |
| 5005 | 612 | ret=0; |
| 613 | } | |
| 614 | } | |
| 615 | ||
|
31105
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
616 | g_free(w_destdir); |
|
b5d95fcb40e1
Make untarring work for non-ASCII destination dirs on Windows.
Daniel Atallah <datallah@pidgin.im>
parents:
29492
diff
changeset
|
617 | |
| 5005 | 618 | /* close the archive file. */ |
| 619 | fclose(infp); | |
| 620 | ||
| 621 | return ret; | |
| 622 | } | |
| 623 |