| 1 | /* $OpenBSD: ar_subs.c,v 1.48 2016/08/26 05:06:14 guenther Exp $ */ |
| 2 | /* $NetBSD: ar_subs.c,v 1.5 1995/03/21 09:07:06 cgd Exp $ */ |
| 3 | |
| 4 | /*- |
| 5 | * Copyright (c) 2008, 2011, 2012, 2016 |
| 6 | * mirabilos <[email protected]> |
| 7 | * Copyright (c) 1992 Keith Muller. |
| 8 | * Copyright (c) 1992, 1993 |
| 9 | * The Regents of the University of California. All rights reserved. |
| 10 | * |
| 11 | * This code is derived from software contributed to Berkeley by |
| 12 | * Keith Muller of the University of California, San Diego. |
| 13 | * |
| 14 | * Redistribution and use in source and binary forms, with or without |
| 15 | * modification, are permitted provided that the following conditions |
| 16 | * are met: |
| 17 | * 1. Redistributions of source code must retain the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer. |
| 19 | * 2. Redistributions in binary form must reproduce the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer in the |
| 21 | * documentation and/or other materials provided with the distribution. |
| 22 | * 3. Neither the name of the University nor the names of its contributors |
| 23 | * may be used to endorse or promote products derived from this software |
| 24 | * without specific prior written permission. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 36 | * SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | #include <sys/types.h> |
| 40 | #if HAVE_BOTH_TIME_H |
| 41 | #include <sys/time.h> |
| 42 | #include <time.h> |
| 43 | #elif HAVE_SYS_TIME_H |
| 44 | #include <sys/time.h> |
| 45 | #elif HAVE_TIME_H |
| 46 | #include <time.h> |
| 47 | #endif |
| 48 | #include <sys/stat.h> |
| 49 | #include <errno.h> |
| 50 | #include <fcntl.h> |
| 51 | #include <signal.h> |
| 52 | #include <stdio.h> |
| 53 | #include <string.h> |
| 54 | #if HAVE_STRINGS_H |
| 55 | #include <strings.h> |
| 56 | #endif |
| 57 | #include <unistd.h> |
| 58 | |
| 59 | #include "pax.h" |
| 60 | #include "ftimes.h" |
| 61 | #include "extern.h" |
| 62 | |
| 63 | __RCSID("$MirOS: src/bin/pax/ar_subs.c,v 1.20 2018/12/13 07:09:09 tg Exp $"); |
| 64 | |
| 65 | static void wr_archive(ARCHD *, int is_app); |
| 66 | static int get_arc(void); |
| 67 | static int next_head(ARCHD *); |
| 68 | extern sigset_t s_mask; |
| 69 | |
| 70 | /* |
| 71 | * Routines which control the overall operation modes of pax as specified by |
| 72 | * the user: list, append, read ... |
| 73 | */ |
| 74 | |
| 75 | static char hdbuf[BLKMULT]; /* space for archive header on read */ |
| 76 | u_long flcnt; /* number of files processed */ |
| 77 | char ar_do_keepopen = 0; /* see append() below */ |
| 78 | |
| 79 | /* |
| 80 | * list() |
| 81 | * list the contents of an archive which match user supplied pattern(s) |
| 82 | * (no pattern matches all). |
| 83 | */ |
| 84 | |
| 85 | void |
| 86 | list(void) |
| 87 | { |
| 88 | ARCHD *arcn; |
| 89 | int res; |
| 90 | ARCHD archd; |
| 91 | |
| 92 | arcn = &archd; |
| 93 | /* |
| 94 | * figure out archive type; pass any format specific options to the |
| 95 | * archive option processing routine; call the format init routine. We |
| 96 | * also save current time for ls_list() so we do not make a system |
| 97 | * call for each file we need to print. If verbose (vflag) start up |
| 98 | * the name and group caches. |
| 99 | */ |
| 100 | if ((get_arc() < 0) || ((*frmt->options)() < 0) || |
| 101 | ((*frmt->st_rd)() < 0)) |
| 102 | return; |
| 103 | |
| 104 | #if !HAVE_UG_FROM_UGID |
| 105 | if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0))) |
| 106 | return; |
| 107 | #endif |
| 108 | |
| 109 | /* |
| 110 | * step through the archive until the format says it is done |
| 111 | */ |
| 112 | while (next_head(arcn) == 0) { |
| 113 | if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) { |
| 114 | /* |
| 115 | * we need to read, to get the real filename |
| 116 | */ |
| 117 | off_t cnt; |
| 118 | if (!(*frmt->rd_data)(arcn, arcn->type == PAX_GLF |
| 119 | ? -1 : -2, &cnt)) |
| 120 | (void)rd_skip(cnt + arcn->pad); |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * check for pattern, and user specified options match. |
| 126 | * When all patterns are matched we are done. |
| 127 | */ |
| 128 | if ((res = pat_match(arcn)) < 0) |
| 129 | break; |
| 130 | |
| 131 | if ((res == 0) && (sel_chk(arcn) == 0)) { |
| 132 | /* |
| 133 | * pattern resulted in a selected file |
| 134 | */ |
| 135 | if (pat_sel(arcn) < 0) |
| 136 | break; |
| 137 | |
| 138 | /* |
| 139 | * modify the name as requested by the user if name |
| 140 | * survives modification, do a listing of the file |
| 141 | */ |
| 142 | if ((res = mod_name(arcn)) < 0) |
| 143 | break; |
| 144 | if (res == 0) |
| 145 | ls_list(arcn, stdout); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * skip to next archive format header using values calculated |
| 150 | * by the format header read routine |
| 151 | */ |
| 152 | if (rd_skip(arcn->skip + arcn->pad) == 1) |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * all done, let format have a chance to cleanup, and make sure that |
| 158 | * the patterns supplied by the user were all matched |
| 159 | */ |
| 160 | (void)(*frmt->end_rd)(); |
| 161 | (void)sigprocmask(SIG_BLOCK, &s_mask, NULL); |
| 162 | ar_close(0); |
| 163 | pat_chk(); |
| 164 | } |
| 165 | |
| 166 | static int |
| 167 | cmp_file_times(int mtime_flag, int ctime_flag, ARCHD *arcn, struct stat *sbp) |
| 168 | { |
| 169 | struct stat sb; |
| 170 | |
| 171 | if (sbp == NULL) { |
| 172 | if (lstat(arcn->name, &sb) != 0) |
| 173 | return (0); |
| 174 | sbp = &sb; |
| 175 | } |
| 176 | |
| 177 | if (ctime_flag && mtime_flag) |
| 178 | return (st_timecmp(m, &arcn->sb, sbp, <=) && |
| 179 | st_timecmp(c, &arcn->sb, sbp, <=)); |
| 180 | else if (ctime_flag) |
| 181 | return (st_timecmp(c, &arcn->sb, sbp, <=)); |
| 182 | else |
| 183 | return (st_timecmp(m, &arcn->sb, sbp, <=)); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * extract() |
| 188 | * extract the member(s) of an archive as specified by user supplied |
| 189 | * pattern(s) (no patterns extracts all members) |
| 190 | */ |
| 191 | |
| 192 | void |
| 193 | extract(void) |
| 194 | { |
| 195 | ARCHD *arcn; |
| 196 | int res; |
| 197 | off_t cnt; |
| 198 | ARCHD archd; |
| 199 | int fd; |
| 200 | |
| 201 | sltab_start(); |
| 202 | |
| 203 | arcn = &archd; |
| 204 | /* |
| 205 | * figure out archive type; pass any format specific options to the |
| 206 | * archive option processing routine; call the format init routine; |
| 207 | * start up the directory modification time and access mode database |
| 208 | */ |
| 209 | if ((get_arc() < 0) || ((*frmt->options)() < 0) || |
| 210 | ((*frmt->st_rd)() < 0) || (dir_start() < 0)) |
| 211 | return; |
| 212 | |
| 213 | /* |
| 214 | * When we are doing interactive rename, we store the mapping of names |
| 215 | * so we can fix up hard links files later in the archive. |
| 216 | */ |
| 217 | if (iflag && (name_start() < 0)) |
| 218 | return; |
| 219 | |
| 220 | /* |
| 221 | * step through each entry on the archive until the format read routine |
| 222 | * says it is done |
| 223 | */ |
| 224 | while (next_head(arcn) == 0) { |
| 225 | if (arcn->type == PAX_GLL || arcn->type == PAX_GLF) { |
| 226 | /* |
| 227 | * we need to read, to get the real filename |
| 228 | */ |
| 229 | if (!(*frmt->rd_data)(arcn, arcn->type == PAX_GLF |
| 230 | ? -1 : -2, &cnt)) |
| 231 | (void)rd_skip(cnt + arcn->pad); |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * check for pattern, and user specified options match. When |
| 237 | * all the patterns are matched we are done |
| 238 | */ |
| 239 | if ((res = pat_match(arcn)) < 0) |
| 240 | break; |
| 241 | |
| 242 | if ((res > 0) || (sel_chk(arcn) != 0)) { |
| 243 | /* |
| 244 | * file is not selected. skip past any file data and |
| 245 | * padding and go back for the next archive member |
| 246 | */ |
| 247 | (void)rd_skip(arcn->skip + arcn->pad); |
| 248 | continue; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * with -u or -D only extract when the archive member is newer |
| 253 | * than the file with the same name in the filesystem (no |
| 254 | * test of being the same type is required). |
| 255 | * NOTE: this test is done BEFORE name modifications as |
| 256 | * specified by pax. this operation can be confusing to the |
| 257 | * user who might expect the test to be done on an existing |
| 258 | * file AFTER the name mod. In honesty the pax spec is probably |
| 259 | * flawed in this respect. |
| 260 | */ |
| 261 | if ((uflag || Dflag) && |
| 262 | cmp_file_times(uflag, Dflag, arcn, NULL)) { |
| 263 | (void)rd_skip(arcn->skip + arcn->pad); |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * this archive member is now been selected. modify the name. |
| 269 | */ |
| 270 | if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0)) |
| 271 | break; |
| 272 | if (res > 0) { |
| 273 | /* |
| 274 | * a bad name mod, skip and purge name from link table |
| 275 | */ |
| 276 | purg_lnk(arcn); |
| 277 | (void)rd_skip(arcn->skip + arcn->pad); |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Non standard -Y and -Z flag. When the existing file is |
| 283 | * same age or newer skip |
| 284 | */ |
| 285 | if ((Yflag || Zflag) && |
| 286 | cmp_file_times(Yflag, Zflag, arcn, NULL)) { |
| 287 | (void)rd_skip(arcn->skip + arcn->pad); |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | if (vflag) { |
| 292 | if (vflag > 1) |
| 293 | ls_list(arcn, listf); |
| 294 | else { |
| 295 | (void)safe_print(arcn->name, listf); |
| 296 | vfpart = 1; |
| 297 | } |
| 298 | } else if (Vflag) { |
| 299 | (void)putc('.', listf); |
| 300 | (void)fflush(listf); |
| 301 | vfpart = 1; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * if required, chdir around. |
| 306 | */ |
| 307 | if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL)) |
| 308 | if (!to_stdout && chdir(arcn->pat->chdname) != 0) |
| 309 | syswarn(1, errno, "Cannot chdir to %s", |
| 310 | arcn->pat->chdname); |
| 311 | |
| 312 | /* |
| 313 | * all ok, extract this member based on type |
| 314 | */ |
| 315 | if (!PAX_IS_REG(arcn->type)) { |
| 316 | /* |
| 317 | * process archive members that are not regular files. |
| 318 | * throw out padding and any data that might follow the |
| 319 | * header (as determined by the format). |
| 320 | */ |
| 321 | if (!to_stdout) { |
| 322 | if (PAX_IS_HARDLINK(arcn->type)) { |
| 323 | res = lnk_creat(arcn, &fd); |
| 324 | if (fd != -1) |
| 325 | goto extdata; |
| 326 | }else |
| 327 | res = node_creat(arcn); |
| 328 | } |
| 329 | |
| 330 | (void)rd_skip(arcn->skip + arcn->pad); |
| 331 | if (res < 0) |
| 332 | purg_lnk(arcn); |
| 333 | |
| 334 | if (vflag && vfpart) { |
| 335 | (void)putc('\n', listf); |
| 336 | vfpart = 0; |
| 337 | } |
| 338 | goto popd; |
| 339 | } |
| 340 | /* |
| 341 | * we have a file with data here. If we cannot create it, skip |
| 342 | * over the data and purge the name from hard link table |
| 343 | */ |
| 344 | if (to_stdout) |
| 345 | fd = STDOUT_FILENO; |
| 346 | else if ((fd = file_creat(arcn)) < 0) { |
| 347 | (void)rd_skip(arcn->skip + arcn->pad); |
| 348 | purg_lnk(arcn); |
| 349 | goto popd; |
| 350 | } |
| 351 | /* |
| 352 | * extract the file from the archive and skip over padding and |
| 353 | * any unprocessed data |
| 354 | */ |
| 355 | extdata: |
| 356 | res = (*frmt->rd_data)(arcn, fd, &cnt); |
| 357 | if (fd != STDOUT_FILENO) |
| 358 | file_close(arcn, fd); |
| 359 | if (vflag && vfpart) { |
| 360 | (void)putc('\n', listf); |
| 361 | vfpart = 0; |
| 362 | } |
| 363 | if (!res) |
| 364 | (void)rd_skip(cnt + arcn->pad); |
| 365 | |
| 366 | popd: |
| 367 | /* |
| 368 | * if required, chdir around. |
| 369 | */ |
| 370 | if ((arcn->pat != NULL) && (arcn->pat->chdname != NULL)) |
| 371 | if (!to_stdout && fchdir(cwdfd) != 0) |
| 372 | syswarn(1, errno, |
| 373 | "Cannot fchdir to starting directory"); |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * all done, restore directory modes and times as required; make sure |
| 378 | * all patterns supplied by the user were matched; block off signals |
| 379 | * to avoid chance for multiple entry into the cleanup code. |
| 380 | */ |
| 381 | (void)(*frmt->end_rd)(); |
| 382 | (void)sigprocmask(SIG_BLOCK, &s_mask, NULL); |
| 383 | ar_close(0); |
| 384 | sltab_process(0); |
| 385 | proc_dir(0); |
| 386 | pat_chk(); |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * wr_archive() |
| 391 | * Write an archive. used in both creating a new archive and appends on |
| 392 | * previously written archive. |
| 393 | */ |
| 394 | |
| 395 | static void |
| 396 | wr_archive(ARCHD *arcn, int is_app) |
| 397 | { |
| 398 | int res; |
| 399 | int hlk; |
| 400 | int wr_one; |
| 401 | off_t cnt; |
| 402 | int (*wrf)(ARCHD *); |
| 403 | int fd = -1; |
| 404 | |
| 405 | /* |
| 406 | * if this format supports hard link storage, start up the database |
| 407 | * that detects them. |
| 408 | */ |
| 409 | if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0)) |
| 410 | return; |
| 411 | |
| 412 | /* |
| 413 | * if this is not append, and there are no files, we do not write a |
| 414 | * trailer |
| 415 | */ |
| 416 | wr_one = is_app; |
| 417 | |
| 418 | /* |
| 419 | * start up the file traversal code and format specific write |
| 420 | */ |
| 421 | if (ftree_start() < 0) { |
| 422 | if (is_app) |
| 423 | goto trailer; |
| 424 | return; |
| 425 | } else if (((*frmt->st_wr)(is_app) < 0)) |
| 426 | return; |
| 427 | |
| 428 | wrf = frmt->wr; |
| 429 | |
| 430 | /* |
| 431 | * When we are doing interactive rename, we store the mapping of names |
| 432 | * so we can fix up hard links files later in the archive. |
| 433 | */ |
| 434 | if (iflag && (name_start() < 0)) |
| 435 | return; |
| 436 | |
| 437 | /* |
| 438 | * while there are files to archive, process them one at at time |
| 439 | */ |
| 440 | while (next_file(arcn) == 0) { |
| 441 | /* |
| 442 | * check if this file meets user specified options match. |
| 443 | */ |
| 444 | if (sel_chk(arcn) != 0) |
| 445 | continue; |
| 446 | fd = -1; |
| 447 | if (uflag) { |
| 448 | /* |
| 449 | * only archive if this file is newer than a file with |
| 450 | * the same name that is already stored on the archive |
| 451 | */ |
| 452 | if ((res = chk_ftime(arcn)) < 0) |
| 453 | break; |
| 454 | if (res > 0) { |
| 455 | ftree_skipped_newer(); |
| 456 | continue; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * this file is considered selected now. see if this is a hard |
| 462 | * link to a file already stored |
| 463 | */ |
| 464 | ftree_sel(arcn); |
| 465 | if (hlk && (chk_lnk(arcn) < 0)) |
| 466 | break; |
| 467 | |
| 468 | if (PAX_IS_REG(arcn->type) || (arcn->type == PAX_HRG)) { |
| 469 | /* |
| 470 | * we will have to read this file. by opening it now we |
| 471 | * can avoid writing a header to the archive for a file |
| 472 | * we were later unable to read (we also purge it from |
| 473 | * the link table). |
| 474 | */ |
| 475 | if ((fd = binopen3(0, arcn->org_name, O_RDONLY, 0)) < 0) { |
| 476 | syswarn(1, errno, "Unable to open %s to read", |
| 477 | arcn->org_name); |
| 478 | purg_lnk(arcn); |
| 479 | continue; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Now modify the name as requested by the user |
| 485 | */ |
| 486 | if ((res = mod_name(arcn)) < 0) { |
| 487 | /* |
| 488 | * name modification says to skip this file, close the |
| 489 | * file and purge link table entry |
| 490 | */ |
| 491 | rdfile_close(arcn, &fd); |
| 492 | purg_lnk(arcn); |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) { |
| 497 | /* |
| 498 | * unable to obtain the crc we need, close the file, |
| 499 | * purge link table entry |
| 500 | */ |
| 501 | rdfile_close(arcn, &fd); |
| 502 | purg_lnk(arcn); |
| 503 | continue; |
| 504 | } |
| 505 | |
| 506 | if (vflag) { |
| 507 | if (vflag > 1) |
| 508 | ls_list(arcn, listf); |
| 509 | else { |
| 510 | (void)safe_print(arcn->name, listf); |
| 511 | vfpart = 1; |
| 512 | } |
| 513 | } else if (Vflag) { |
| 514 | (void)putc('.', listf); |
| 515 | (void)fflush(listf); |
| 516 | vfpart = 1; |
| 517 | } |
| 518 | ++flcnt; |
| 519 | |
| 520 | /* |
| 521 | * looks safe to store the file, have the format specific |
| 522 | * routine write routine store the file header on the archive |
| 523 | */ |
| 524 | if ((res = (*wrf)(arcn)) < 0) { |
| 525 | rdfile_close(arcn, &fd); |
| 526 | break; |
| 527 | } |
| 528 | wr_one = 1; |
| 529 | if (res > 0) { |
| 530 | /* |
| 531 | * format write says no file data needs to be stored |
| 532 | * so we are done messing with this file |
| 533 | */ |
| 534 | if (vflag && vfpart) { |
| 535 | (void)putc('\n', listf); |
| 536 | vfpart = 0; |
| 537 | } |
| 538 | rdfile_close(arcn, &fd); |
| 539 | continue; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Add file data to the archive, quit on write error. if we |
| 544 | * cannot write the entire file contents to the archive we |
| 545 | * must pad the archive to replace the missing file data |
| 546 | * (otherwise during an extract the file header for the file |
| 547 | * which FOLLOWS this one will not be where we expect it to |
| 548 | * be). |
| 549 | */ |
| 550 | res = (*frmt->wr_data)(arcn, fd, &cnt); |
| 551 | rdfile_close(arcn, &fd); |
| 552 | if (vflag && vfpart) { |
| 553 | (void)putc('\n', listf); |
| 554 | vfpart = 0; |
| 555 | } |
| 556 | if (res < 0) |
| 557 | break; |
| 558 | |
| 559 | /* |
| 560 | * pad as required, cnt is number of bytes not written |
| 561 | */ |
| 562 | if (((cnt > 0) && (wr_skip(cnt) < 0)) || |
| 563 | ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0))) |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | trailer: |
| 568 | /* |
| 569 | * tell format to write trailer; pad to block boundary; reset directory |
| 570 | * mode/access times, and check if all patterns supplied by the user |
| 571 | * were matched. block off signals to avoid chance for multiple entry |
| 572 | * into the cleanup code |
| 573 | */ |
| 574 | if (wr_one) { |
| 575 | (*frmt->end_wr)(); |
| 576 | wr_fin(); |
| 577 | } |
| 578 | (void)sigprocmask(SIG_BLOCK, &s_mask, NULL); |
| 579 | ar_close(0); |
| 580 | if (tflag) |
| 581 | proc_dir(0); |
| 582 | ftree_chk(); |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * append() |
| 587 | * Add file to previously written archive. Archive format specified by the |
| 588 | * user must agree with archive. The archive is read first to collect |
| 589 | * modification times (if -u) and locate the archive trailer. The archive |
| 590 | * is positioned in front of the record with the trailer and wr_archive() |
| 591 | * is called to add the new members. |
| 592 | * PAX IMPLEMENTATION DETAIL NOTE: |
| 593 | * -u is implemented by adding the new members to the end of the archive. |
| 594 | * Care is taken so that these do not end up as links to the older |
| 595 | * version of the same file already stored in the archive. It is expected |
| 596 | * when extraction occurs these newer versions will over-write the older |
| 597 | * ones stored "earlier" in the archive (this may be a bad assumption as |
| 598 | * it depends on the implementation of the program doing the extraction). |
| 599 | * It is really difficult to splice in members without either re-writing |
| 600 | * the entire archive (from the point were the old version was), or having |
| 601 | * assistance of the format specification in terms of a special update |
| 602 | * header that invalidates a previous archive record. The posix spec left |
| 603 | * the method used to implement -u unspecified. This pax is able to |
| 604 | * over write existing files that it creates. |
| 605 | */ |
| 606 | |
| 607 | void |
| 608 | append(void) |
| 609 | { |
| 610 | ARCHD *arcn; |
| 611 | int res; |
| 612 | ARCHD archd; |
| 613 | const FSUB *orgfrmt; |
| 614 | int udev; |
| 615 | off_t tlen; |
| 616 | |
| 617 | arcn = &archd; |
| 618 | orgfrmt = frmt; |
| 619 | |
| 620 | /* |
| 621 | * Do not allow an append operation if the actual archive is of a |
| 622 | * different format than the user specified format. |
| 623 | */ |
| 624 | if (get_arc() < 0) |
| 625 | return; |
| 626 | if ((orgfrmt != NULL) && (orgfrmt != frmt)) { |
| 627 | paxwarn(1, "Cannot mix current archive format %s with %s", |
| 628 | frmt->name, orgfrmt->name); |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * pass the format any options and start up format |
| 634 | */ |
| 635 | if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0)) |
| 636 | return; |
| 637 | |
| 638 | /* hack to allow appending to Unix Archiver libraries */ |
| 639 | if (frmt->is_uar) |
| 640 | ar_do_keepopen = 1; |
| 641 | |
| 642 | /* |
| 643 | * if we only are adding members that are newer, we need to save the |
| 644 | * mod times for all files we see. |
| 645 | */ |
| 646 | if (uflag && (ftime_start() < 0)) |
| 647 | return; |
| 648 | |
| 649 | /* |
| 650 | * some archive formats encode hard links by recording the device and |
| 651 | * file serial number (inode) but copy the file anyway (multiple times) |
| 652 | * to the archive. When we append, we run the risk that newly added |
| 653 | * files may have the same device and inode numbers as those recorded |
| 654 | * on the archive but during a previous run. If this happens, when the |
| 655 | * archive is extracted we get INCORRECT hard links. We avoid this by |
| 656 | * remapping the device numbers so that newly added files will never |
| 657 | * use the same device number as one found on the archive. remapping |
| 658 | * allows new members to safely have links among themselves. remapping |
| 659 | * also avoids problems with file inode (serial number) truncations |
| 660 | * when the inode number is larger than storage space in the archive |
| 661 | * header. See the remap routines for more details. |
| 662 | */ |
| 663 | if ((udev = frmt->udev) && (dev_start() < 0)) |
| 664 | return; |
| 665 | |
| 666 | /* |
| 667 | * reading the archive may take a long time. If verbose tell the user |
| 668 | */ |
| 669 | if (vflag) { |
| 670 | (void)fprintf(listf, |
| 671 | "%s: Reading archive to position at the end...", argv0); |
| 672 | vfpart = 1; |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * step through the archive until the format says it is done |
| 677 | */ |
| 678 | while (next_head(arcn) == 0) { |
| 679 | /* |
| 680 | * check if this file meets user specified options. |
| 681 | */ |
| 682 | if (sel_chk(arcn) != 0) { |
| 683 | if (rd_skip(arcn->skip + arcn->pad) == 1) |
| 684 | break; |
| 685 | continue; |
| 686 | } |
| 687 | |
| 688 | if (uflag) { |
| 689 | /* |
| 690 | * see if this is the newest version of this file has |
| 691 | * already been seen, if so skip. |
| 692 | */ |
| 693 | if ((res = chk_ftime(arcn)) < 0) |
| 694 | break; |
| 695 | if (res > 0) { |
| 696 | if (rd_skip(arcn->skip + arcn->pad) == 1) |
| 697 | break; |
| 698 | continue; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Store this device number. Device numbers seen during the |
| 704 | * read phase of append will cause newly appended files with a |
| 705 | * device number seen in the old part of the archive to be |
| 706 | * remapped to an unused device number. |
| 707 | */ |
| 708 | if ((udev && (add_dev(arcn) < 0)) || |
| 709 | (rd_skip(arcn->skip + arcn->pad) == 1)) |
| 710 | break; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * done, finish up read and get the number of bytes to back up so we |
| 715 | * can add new members. The format might have used the hard link table, |
| 716 | * purge it. |
| 717 | */ |
| 718 | tlen = (*frmt->end_rd)(); |
| 719 | lnk_end(); |
| 720 | |
| 721 | /* |
| 722 | * try to position for write, if this fails quit. if any error occurs, |
| 723 | * we will refuse to write |
| 724 | */ |
| 725 | if (appnd_start(tlen) < 0) |
| 726 | return; |
| 727 | |
| 728 | /* |
| 729 | * tell the user we are done reading. |
| 730 | */ |
| 731 | if (vflag && vfpart) { |
| 732 | (void)fputs("done.\n", listf); |
| 733 | vfpart = 0; |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * go to the writing phase to add the new members |
| 738 | */ |
| 739 | wr_archive(arcn, 1); |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * archive() |
| 744 | * write a new archive |
| 745 | */ |
| 746 | |
| 747 | void |
| 748 | archive(void) |
| 749 | { |
| 750 | ARCHD archd; |
| 751 | |
| 752 | /* |
| 753 | * if we only are adding members that are newer, we need to save the |
| 754 | * mod times for all files; set up for writing; pass the format any |
| 755 | * options write the archive |
| 756 | */ |
| 757 | if ((uflag && (ftime_start() < 0)) || (wr_start() < 0)) |
| 758 | return; |
| 759 | if ((*frmt->options)() < 0) |
| 760 | return; |
| 761 | |
| 762 | wr_archive(&archd, 0); |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * copy() |
| 767 | * copy files from one part of the filesystem to another. this does not |
| 768 | * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an |
| 769 | * archive was written and then extracted in the destination directory |
| 770 | * (except the files are forced to be under the destination directory). |
| 771 | */ |
| 772 | |
| 773 | void |
| 774 | copy(void) |
| 775 | { |
| 776 | ARCHD *arcn; |
| 777 | int res; |
| 778 | int fddest; |
| 779 | char *dest_pt; |
| 780 | size_t dlen; |
| 781 | size_t drem; |
| 782 | int fdsrc = -1; |
| 783 | struct stat sb; |
| 784 | ARCHD archd; |
| 785 | char dirbuf[PAXPATHLEN+1]; |
| 786 | |
| 787 | sltab_start(); |
| 788 | |
| 789 | arcn = &archd; |
| 790 | /* |
| 791 | * set up the destination dir path and make sure it is a directory. We |
| 792 | * make sure we have a trailing / on the destination |
| 793 | */ |
| 794 | dlen = strlcpy(dirbuf, dirptr, sizeof(dirbuf)); |
| 795 | if (dlen >= sizeof(dirbuf) || |
| 796 | (dlen == sizeof(dirbuf) - 1 && dirbuf[dlen - 1] != '/')) { |
| 797 | paxwarn(1, "directory name is too long %s", dirptr); |
| 798 | return; |
| 799 | } |
| 800 | dest_pt = dirbuf + dlen; |
| 801 | if (*(dest_pt-1) != '/') { |
| 802 | *dest_pt++ = '/'; |
| 803 | *dest_pt = '\0'; |
| 804 | ++dlen; |
| 805 | } |
| 806 | drem = PAXPATHLEN - dlen; |
| 807 | |
| 808 | if (stat(dirptr, &sb) < 0) { |
| 809 | syswarn(1, errno, "Cannot access destination directory %s", |
| 810 | dirptr); |
| 811 | return; |
| 812 | } |
| 813 | if (!S_ISDIR(sb.st_mode)) { |
| 814 | paxwarn(1, "Destination is not a directory %s", dirptr); |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | * start up the hard link table; file traversal routines and the |
| 820 | * modification time and access mode database |
| 821 | */ |
| 822 | if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0)) |
| 823 | return; |
| 824 | |
| 825 | /* |
| 826 | * When we are doing interactive rename, we store the mapping of names |
| 827 | * so we can fix up hard links files later in the archive. |
| 828 | */ |
| 829 | if (iflag && (name_start() < 0)) |
| 830 | return; |
| 831 | |
| 832 | /* |
| 833 | * set up to cp file trees |
| 834 | */ |
| 835 | cp_start(); |
| 836 | |
| 837 | /* |
| 838 | * while there are files to archive, process them |
| 839 | */ |
| 840 | while (next_file(arcn) == 0) { |
| 841 | fdsrc = -1; |
| 842 | |
| 843 | /* |
| 844 | * check if this file meets user specified options |
| 845 | */ |
| 846 | if (sel_chk(arcn) != 0) |
| 847 | continue; |
| 848 | |
| 849 | /* |
| 850 | * if there is already a file in the destination directory with |
| 851 | * the same name and it is newer, skip the one stored on the |
| 852 | * archive. |
| 853 | * NOTE: this test is done BEFORE name modifications as |
| 854 | * specified by pax. this can be confusing to the user who |
| 855 | * might expect the test to be done on an existing file AFTER |
| 856 | * the name mod. In honesty the pax spec is probably flawed in |
| 857 | * this respect |
| 858 | */ |
| 859 | if (uflag || Dflag) { |
| 860 | /* |
| 861 | * create the destination name |
| 862 | */ |
| 863 | if (strlcpy(dest_pt, arcn->name + (*arcn->name == '/'), |
| 864 | drem + 1) > drem) { |
| 865 | paxwarn(1, "Destination pathname too long %s", |
| 866 | arcn->name); |
| 867 | continue; |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * if existing file is same age or newer skip |
| 872 | */ |
| 873 | res = lstat(dirbuf, &sb); |
| 874 | *dest_pt = '\0'; |
| 875 | |
| 876 | if (res == 0) { |
| 877 | ftree_skipped_newer(); |
| 878 | if (cmp_file_times(uflag, Dflag, arcn, &sb)) |
| 879 | continue; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * this file is considered selected. See if this is a hard link |
| 885 | * to a previous file; modify the name as requested by the |
| 886 | * user; set the final destination. |
| 887 | */ |
| 888 | ftree_sel(arcn); |
| 889 | if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0)) |
| 890 | break; |
| 891 | if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) { |
| 892 | /* |
| 893 | * skip file, purge from link table |
| 894 | */ |
| 895 | purg_lnk(arcn); |
| 896 | continue; |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * Non standard -Y and -Z flag. When the existing file is |
| 901 | * same age or newer skip |
| 902 | */ |
| 903 | if ((Yflag || Zflag) && |
| 904 | cmp_file_times(Yflag, Zflag, arcn, NULL)) |
| 905 | continue; |
| 906 | |
| 907 | if (vflag) { |
| 908 | (void)safe_print(arcn->name, listf); |
| 909 | vfpart = 1; |
| 910 | } else if (Vflag) { |
| 911 | (void)putc('.', listf); |
| 912 | (void)fflush(listf); |
| 913 | vfpart = 1; |
| 914 | } |
| 915 | ++flcnt; |
| 916 | |
| 917 | /* |
| 918 | * try to create a hard link to the src file if requested |
| 919 | * but make sure we are not trying to overwrite ourselves. |
| 920 | */ |
| 921 | if (lflag) |
| 922 | res = cross_lnk(arcn); |
| 923 | else |
| 924 | res = chk_same(arcn); |
| 925 | if (res <= 0) { |
| 926 | if (vflag && vfpart) { |
| 927 | (void)putc('\n', listf); |
| 928 | vfpart = 0; |
| 929 | } |
| 930 | continue; |
| 931 | } |
| 932 | |
| 933 | /* |
| 934 | * have to create a new file |
| 935 | */ |
| 936 | if (!PAX_IS_REG(arcn->type)) { |
| 937 | /* |
| 938 | * create a link or special file |
| 939 | */ |
| 940 | if (PAX_IS_HARDLINK(arcn->type)) |
| 941 | res = lnk_creat(arcn, NULL); |
| 942 | else |
| 943 | res = node_creat(arcn); |
| 944 | if (res < 0) |
| 945 | purg_lnk(arcn); |
| 946 | if (vflag && vfpart) { |
| 947 | (void)putc('\n', listf); |
| 948 | vfpart = 0; |
| 949 | } |
| 950 | continue; |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * have to copy a regular file to the destination directory. |
| 955 | * first open source file and then create the destination file |
| 956 | */ |
| 957 | if ((fdsrc = binopen3(0, arcn->org_name, O_RDONLY, 0)) < 0) { |
| 958 | syswarn(1, errno, "Unable to open %s to read", |
| 959 | arcn->org_name); |
| 960 | purg_lnk(arcn); |
| 961 | continue; |
| 962 | } |
| 963 | if ((fddest = file_creat(arcn)) < 0) { |
| 964 | rdfile_close(arcn, &fdsrc); |
| 965 | purg_lnk(arcn); |
| 966 | continue; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * copy source file data to the destination file |
| 971 | */ |
| 972 | cp_file(arcn, fdsrc, fddest); |
| 973 | file_close(arcn, fddest); |
| 974 | rdfile_close(arcn, &fdsrc); |
| 975 | |
| 976 | if (vflag && vfpart) { |
| 977 | (void)putc('\n', listf); |
| 978 | vfpart = 0; |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * restore directory modes and times as required; make sure all |
| 984 | * patterns were selected block off signals to avoid chance for |
| 985 | * multiple entry into the cleanup code. |
| 986 | */ |
| 987 | (void)sigprocmask(SIG_BLOCK, &s_mask, NULL); |
| 988 | ar_close(0); |
| 989 | sltab_process(0); |
| 990 | proc_dir(0); |
| 991 | ftree_chk(); |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * next_head() |
| 996 | * try to find a valid header in the archive. Uses format specific |
| 997 | * routines to extract the header and id the trailer. Trailers may be |
| 998 | * located within a valid header or in an invalid header (the location |
| 999 | * is format specific. The inhead field from the option table tells us |
| 1000 | * where to look for the trailer). |
| 1001 | * We keep reading (and resyncing) until we get enough contiguous data |
| 1002 | * to check for a header. If we cannot find one, we shift by a byte |
| 1003 | * add a new byte from the archive to the end of the buffer and try again. |
| 1004 | * If we get a read error, we throw out what we have (as we must have |
| 1005 | * contiguous data) and start over again. |
| 1006 | * ASSUMED: headers fit within a BLKMULT header. |
| 1007 | * Return: |
| 1008 | * 0 if we got a header, -1 if we are unable to ever find another one |
| 1009 | * (we reached the end of input, or we reached the limit on retries. see |
| 1010 | * the specs for rd_wrbuf() for more details) |
| 1011 | */ |
| 1012 | |
| 1013 | static int |
| 1014 | next_head(ARCHD *arcn) |
| 1015 | { |
| 1016 | int ret; |
| 1017 | char *hdend; |
| 1018 | int res; |
| 1019 | int shftsz; |
| 1020 | int hsz; |
| 1021 | int in_resync = 0; /* set when we are in resync mode */ |
| 1022 | int cnt = 0; /* counter for trailer function */ |
| 1023 | int first = 1; /* on 1st read, EOF isn't premature. */ |
| 1024 | |
| 1025 | /* |
| 1026 | * set up initial conditions, we want a whole frmt->hsz block as we |
| 1027 | * have no data yet. |
| 1028 | */ |
| 1029 | res = hsz = frmt->hsz; |
| 1030 | hdend = hdbuf; |
| 1031 | shftsz = hsz - 1; |
| 1032 | for (;;) { |
| 1033 | /* |
| 1034 | * keep looping until we get a contiguous FULL buffer |
| 1035 | * (frmt->hsz is the proper size) |
| 1036 | */ |
| 1037 | for (;;) { |
| 1038 | if ((ret = rd_wrbuf(hdend, res)) == res) |
| 1039 | break; |
| 1040 | |
| 1041 | /* |
| 1042 | * If we read 0 bytes (EOF) from an archive when we |
| 1043 | * expect to find a header, we have stepped upon |
| 1044 | * an archive without the customary block of zeroes |
| 1045 | * end marker. It's just stupid to error out on |
| 1046 | * them, so exit gracefully. |
| 1047 | */ |
| 1048 | if (first && ret == 0) |
| 1049 | return(-1); |
| 1050 | first = 0; |
| 1051 | |
| 1052 | /* |
| 1053 | * some kind of archive read problem, try to resync the |
| 1054 | * storage device, better give the user the bad news. |
| 1055 | */ |
| 1056 | if ((ret == 0) || (rd_sync() < 0) || frmt->is_uar) { |
| 1057 | no_header: |
| 1058 | paxwarn(1,"Premature end of file on archive read"); |
| 1059 | return(-1); |
| 1060 | } |
| 1061 | if (!in_resync) { |
| 1062 | if (act == APPND) { |
| 1063 | paxwarn(1, |
| 1064 | "Archive I/O error, cannot continue"); |
| 1065 | return(-1); |
| 1066 | } |
| 1067 | paxwarn(1,"Archive I/O error. Trying to recover."); |
| 1068 | ++in_resync; |
| 1069 | } |
| 1070 | |
| 1071 | /* |
| 1072 | * oh well, throw it all out and start over |
| 1073 | */ |
| 1074 | res = hsz; |
| 1075 | hdend = hdbuf; |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * ok we have a contiguous buffer of the right size. Call the |
| 1080 | * format read routine. If this was not a valid header and this |
| 1081 | * format stores trailers outside of the header, call the |
| 1082 | * format specific trailer routine to check for a trailer. We |
| 1083 | * have to watch out that we do not mis-identify file data or |
| 1084 | * block padding as a header or trailer. Format specific |
| 1085 | * trailer functions must NOT check for the trailer while we |
| 1086 | * are running in resync mode. Some trailer functions may tell |
| 1087 | * us that this block cannot contain a valid header either, so |
| 1088 | * we then throw out the entire block and start over. |
| 1089 | */ |
| 1090 | if ((*frmt->rd)(arcn, hdbuf) == 0) |
| 1091 | break; |
| 1092 | |
| 1093 | if (frmt->is_uar) |
| 1094 | goto no_header; |
| 1095 | |
| 1096 | if (!frmt->inhead) { |
| 1097 | /* |
| 1098 | * this format has trailers outside of valid headers |
| 1099 | */ |
| 1100 | if ((ret = (*frmt->trail)(arcn,hdbuf,in_resync,&cnt)) == 0){ |
| 1101 | /* |
| 1102 | * valid trailer found, drain input as required |
| 1103 | */ |
| 1104 | ar_drain(); |
| 1105 | return(-1); |
| 1106 | } |
| 1107 | |
| 1108 | if (ret == 1) { |
| 1109 | /* |
| 1110 | * we are in resync and we were told to throw |
| 1111 | * the whole block out because none of the |
| 1112 | * bytes in this block can be used to form a |
| 1113 | * valid header |
| 1114 | */ |
| 1115 | res = hsz; |
| 1116 | hdend = hdbuf; |
| 1117 | continue; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * Brute force section. |
| 1123 | * not a valid header. We may be able to find a header yet. So |
| 1124 | * we shift over by one byte, and set up to read one byte at a |
| 1125 | * time from the archive and place it at the end of the buffer. |
| 1126 | * We will keep moving byte at a time until we find a header or |
| 1127 | * get a read error and have to start over. |
| 1128 | */ |
| 1129 | if (!in_resync) { |
| 1130 | if (act == APPND) { |
| 1131 | paxwarn(1,"Unable to append, archive header flaw"); |
| 1132 | return(-1); |
| 1133 | } |
| 1134 | paxwarn(1,"Invalid header, starting valid header search."); |
| 1135 | ++in_resync; |
| 1136 | } |
| 1137 | memmove(hdbuf, hdbuf+1, shftsz); |
| 1138 | res = 1; |
| 1139 | hdend = hdbuf + shftsz; |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * ok got a valid header, check for trailer if format encodes it in the |
| 1144 | * the header. NOTE: the parameters are different than trailer routines |
| 1145 | * which encode trailers outside of the header! |
| 1146 | */ |
| 1147 | if (frmt->inhead && ((*frmt->trail)(arcn,NULL,0,NULL) == 0)) { |
| 1148 | /* |
| 1149 | * valid trailer found, drain input as required |
| 1150 | */ |
| 1151 | ar_drain(); |
| 1152 | return(-1); |
| 1153 | } |
| 1154 | |
| 1155 | ++flcnt; |
| 1156 | return(0); |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * get_arc() |
| 1161 | * Figure out what format an archive is. Handles archive with flaws by |
| 1162 | * brute force searches for a legal header in any supported format. The |
| 1163 | * format id routines have to be careful to NOT mis-identify a format. |
| 1164 | * ASSUMED: headers fit within a BLKMULT header. |
| 1165 | * Return: |
| 1166 | * 0 if archive found -1 otherwise |
| 1167 | */ |
| 1168 | |
| 1169 | static int |
| 1170 | get_arc(void) |
| 1171 | { |
| 1172 | int i; |
| 1173 | int hdsz = 0; |
| 1174 | int res; |
| 1175 | int minhd = BLKMULT; |
| 1176 | char *hdend; |
| 1177 | int notice = 0; |
| 1178 | |
| 1179 | /* |
| 1180 | * find the smallest header size in all archive formats and then set up |
| 1181 | * to read the archive. |
| 1182 | */ |
| 1183 | for (i = 0; ford[i] != FSUB_MAX; ++i) { |
| 1184 | if (fsub[ford[i]].hsz < minhd) |
| 1185 | minhd = fsub[ford[i]].hsz; |
| 1186 | } |
| 1187 | if (rd_start() < 0) |
| 1188 | return(-1); |
| 1189 | res = BLKMULT; |
| 1190 | hdsz = 0; |
| 1191 | hdend = hdbuf; |
| 1192 | |
| 1193 | #ifndef SMALL |
| 1194 | /* try to verify against ar first */ |
| 1195 | if (buf_fill_internal(8) == 8) { |
| 1196 | i = rd_wrbuf(hdend, 8); |
| 1197 | if (i == 8 && uar_ismagic(hdbuf) == 0) { |
| 1198 | frmt = &(fsub[FSUB_AR]); |
| 1199 | return (0); |
| 1200 | } |
| 1201 | if (i > 0) |
| 1202 | pback(hdend, i); |
| 1203 | } |
| 1204 | #endif |
| 1205 | |
| 1206 | for (;;) { |
| 1207 | for (;;) { |
| 1208 | /* |
| 1209 | * fill the buffer with at least the smallest header |
| 1210 | */ |
| 1211 | i = rd_wrbuf(hdend, res); |
| 1212 | if (i > 0) |
| 1213 | hdsz += i; |
| 1214 | if (hdsz >= minhd) |
| 1215 | break; |
| 1216 | |
| 1217 | /* |
| 1218 | * if we cannot recover from a read error quit |
| 1219 | */ |
| 1220 | if ((i == 0) || (rd_sync() < 0)) |
| 1221 | goto out; |
| 1222 | |
| 1223 | /* |
| 1224 | * when we get an error none of the data we already |
| 1225 | * have can be used to create a legal header (we just |
| 1226 | * got an error in the middle), so we throw it all out |
| 1227 | * and refill the buffer with fresh data. |
| 1228 | */ |
| 1229 | res = BLKMULT; |
| 1230 | hdsz = 0; |
| 1231 | hdend = hdbuf; |
| 1232 | if (!notice) { |
| 1233 | if (act == APPND) |
| 1234 | return(-1); |
| 1235 | paxwarn(1,"Cannot identify format. Searching..."); |
| 1236 | ++notice; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | /* |
| 1241 | * we have at least the size of the smallest header in any |
| 1242 | * archive format. Look to see if we have a match. The array |
| 1243 | * ford[] is used to specify the header id order to reduce the |
| 1244 | * chance of incorrectly id'ing a valid header (some formats |
| 1245 | * may be subsets of each other and the order would then be |
| 1246 | * important). |
| 1247 | */ |
| 1248 | for (i = 0; ford[i] != FSUB_MAX; ++i) { |
| 1249 | if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0) |
| 1250 | continue; |
| 1251 | frmt = &(fsub[ford[i]]); |
| 1252 | /* |
| 1253 | * yuck, to avoid slow special case code in the extract |
| 1254 | * routines, just push this header back as if it was |
| 1255 | * not seen. We have left extra space at start of the |
| 1256 | * buffer for this purpose. This is a bit ugly, but |
| 1257 | * adding all the special case code is far worse. |
| 1258 | */ |
| 1259 | pback(hdbuf, hdsz); |
| 1260 | return(0); |
| 1261 | } |
| 1262 | |
| 1263 | /* |
| 1264 | * We have a flawed archive, no match. we start searching, but |
| 1265 | * we never allow additions to flawed archives |
| 1266 | */ |
| 1267 | if (!notice) { |
| 1268 | if (act == APPND) |
| 1269 | return(-1); |
| 1270 | paxwarn(1, "Cannot identify format. Searching..."); |
| 1271 | ++notice; |
| 1272 | } |
| 1273 | |
| 1274 | /* |
| 1275 | * brute force search for a header that we can id. |
| 1276 | * we shift through byte at a time. this is slow, but we cannot |
| 1277 | * determine the nature of the flaw in the archive in a |
| 1278 | * portable manner |
| 1279 | */ |
| 1280 | if (--hdsz > 0) { |
| 1281 | memmove(hdbuf, hdbuf+1, hdsz); |
| 1282 | res = BLKMULT - hdsz; |
| 1283 | hdend = hdbuf + hdsz; |
| 1284 | } else { |
| 1285 | res = BLKMULT; |
| 1286 | hdend = hdbuf; |
| 1287 | hdsz = 0; |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | out: |
| 1292 | /* |
| 1293 | * we cannot find a header, bow, apologise and quit |
| 1294 | */ |
| 1295 | paxwarn(1, "Sorry, unable to determine archive format."); |
| 1296 | return(-1); |
| 1297 | } |