| 1 | /* $OpenBSD: buf_subs.c,v 1.30 2016/12/20 21:29:08 kettenis Exp $ */ |
| 2 | /* $NetBSD: buf_subs.c,v 1.5 1995/03/21 09:07:08 cgd Exp $ */ |
| 3 | |
| 4 | /*- |
| 5 | * Copyright (c) 1992 Keith Muller. |
| 6 | * Copyright (c) 1992, 1993 |
| 7 | * The Regents of the University of California. All rights reserved. |
| 8 | * |
| 9 | * This code is derived from software contributed to Berkeley by |
| 10 | * Keith Muller of the University of California, San Diego. |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * 3. Neither the name of the University nor the names of its contributors |
| 21 | * may be used to endorse or promote products derived from this software |
| 22 | * without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | * SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #include <sys/types.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <errno.h> |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #if HAVE_STRINGS_H |
| 44 | #include <strings.h> |
| 45 | #endif |
| 46 | #include <unistd.h> |
| 47 | |
| 48 | #include "pax.h" |
| 49 | #include "extern.h" |
| 50 | |
| 51 | __RCSID("$MirOS: src/bin/pax/buf_subs.c,v 1.15 2024/08/17 23:33:50 tg Exp $"); |
| 52 | |
| 53 | /* |
| 54 | * routines which implement archive and file buffering |
| 55 | */ |
| 56 | |
| 57 | #define MINFBSZ 512 /* default block size for hole detect */ |
| 58 | #define MAXFLT 10 /* default media read error limit */ |
| 59 | |
| 60 | /* |
| 61 | * Need to change bufmem to dynamic allocation when the upper |
| 62 | * limit on blocking size is removed (though that will violate pax spec) |
| 63 | * MAXBLK define and tests will also need to be updated. |
| 64 | */ |
| 65 | static char bufmem[MAXBLK+BLKMULT]; /* i/o buffer + pushback id space */ |
| 66 | static char *buf; /* normal start of i/o buffer */ |
| 67 | static char *bufend; /* end or last char in i/o buffer */ |
| 68 | static char *bufpt; /* read/write point in i/o buffer */ |
| 69 | int blksz = MAXBLK; /* block input/output size in bytes */ |
| 70 | int wrblksz; /* user spec output size in bytes */ |
| 71 | int maxflt = MAXFLT; /* MAX consecutive media errors */ |
| 72 | int rdblksz; /* first read blksize (tapes only) */ |
| 73 | off_t wrlimit; /* # of bytes written per archive vol */ |
| 74 | off_t wrcnt; /* # of bytes written on current vol */ |
| 75 | off_t rdcnt; /* # of bytes read on current vol */ |
| 76 | |
| 77 | /* |
| 78 | * wr_start() |
| 79 | * set up the buffering system to operate in a write mode |
| 80 | * Return: |
| 81 | * 0 if ok, -1 if the user specified write block size violates pax spec |
| 82 | */ |
| 83 | |
| 84 | int |
| 85 | wr_start(void) |
| 86 | { |
| 87 | buf = &(bufmem[BLKMULT]); |
| 88 | /* |
| 89 | * Check to make sure the write block size meets pax specs. If the user |
| 90 | * does not specify a blocksize, we use the format default blocksize. |
| 91 | * We must be picky on writes, so we do not allow the user to create an |
| 92 | * archive that might be hard to read elsewhere. If all ok, we then |
| 93 | * open the first archive volume |
| 94 | */ |
| 95 | if (!wrblksz) |
| 96 | wrblksz = frmt->bsz; |
| 97 | if (wrblksz > MAXBLK) { |
| 98 | paxwarn(1, "Write block size %d too large, maximum is: %d", |
| 99 | wrblksz, MAXBLK); |
| 100 | return(-1); |
| 101 | } |
| 102 | if (wrblksz % BLKMULT) { |
| 103 | paxwarn(1, "Write block size %d is not a %d byte multiple", |
| 104 | wrblksz, BLKMULT); |
| 105 | return(-1); |
| 106 | } |
| 107 | if (wrblksz > MAXBLK_POSIX) { |
| 108 | paxwarn(0, "Write block size %d larger than POSIX max %d, archive may not be portable", |
| 109 | wrblksz, MAXBLK_POSIX); |
| 110 | return(-1); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * we only allow wrblksz to be used with all archive operations |
| 115 | */ |
| 116 | blksz = rdblksz = wrblksz; |
| 117 | if ((ar_open(arcname) < 0) && (ar_next() < 0)) |
| 118 | return(-1); |
| 119 | wrcnt = 0; |
| 120 | bufend = buf + wrblksz; |
| 121 | bufpt = buf; |
| 122 | return(0); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * rd_start() |
| 127 | * set up buffering system to read an archive |
| 128 | * Return: |
| 129 | * 0 if ok, -1 otherwise |
| 130 | */ |
| 131 | |
| 132 | int |
| 133 | rd_start(void) |
| 134 | { |
| 135 | /* |
| 136 | * leave space for the header pushback (see get_arc()). If we are |
| 137 | * going to append and user specified a write block size, check it |
| 138 | * right away |
| 139 | */ |
| 140 | buf = &(bufmem[BLKMULT]); |
| 141 | if ((act == APPND) && wrblksz) { |
| 142 | if (wrblksz > MAXBLK) { |
| 143 | paxwarn(1, "Write block size %d too large, maximum is: %d", |
| 144 | wrblksz, MAXBLK); |
| 145 | return (-1); |
| 146 | } |
| 147 | if (wrblksz % BLKMULT) { |
| 148 | paxwarn(1, "Write block size %d is not a %d byte multiple", |
| 149 | wrblksz, BLKMULT); |
| 150 | return(-1); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * open the archive |
| 156 | */ |
| 157 | if ((ar_open(arcname) < 0) && (ar_next() < 0)) |
| 158 | return(-1); |
| 159 | bufend = buf + rdblksz; |
| 160 | bufpt = bufend; |
| 161 | rdcnt = 0; |
| 162 | return(0); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * cp_start() |
| 167 | * set up buffer system for copying within the filesystem |
| 168 | */ |
| 169 | |
| 170 | void |
| 171 | cp_start(void) |
| 172 | { |
| 173 | buf = &(bufmem[BLKMULT]); |
| 174 | rdblksz = blksz = MAXBLK; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * appnd_start() |
| 179 | * Set up the buffering system to append new members to an archive that |
| 180 | * was just read. The last block(s) of an archive may contain a format |
| 181 | * specific trailer. To append a new member, this trailer has to be |
| 182 | * removed from the archive. The first byte of the trailer is replaced by |
| 183 | * the start of the header of the first file added to the archive. The |
| 184 | * format specific end read function tells us how many bytes to move |
| 185 | * backwards in the archive to be positioned BEFORE the trailer. Two |
| 186 | * different position have to be adjusted, the O.S. file offset (e.g. the |
| 187 | * position of the tape head) and the write point within the data we have |
| 188 | * stored in the read (soon to become write) buffer. We may have to move |
| 189 | * back several records (the number depends on the size of the archive |
| 190 | * record and the size of the format trailer) to read up the record where |
| 191 | * the first byte of the trailer is recorded. Trailers may span (and |
| 192 | * overlap) record boundaries. |
| 193 | * We first calculate which record has the first byte of the trailer. We |
| 194 | * move the OS file offset back to the start of this record and read it |
| 195 | * up. We set the buffer write pointer to be at this byte (the byte where |
| 196 | * the trailer starts). We then move the OS file pointer back to the |
| 197 | * start of this record so a flush of this buffer will replace the record |
| 198 | * in the archive. |
| 199 | * A major problem is rewriting this last record. For archives stored |
| 200 | * on disk files, this is trivial. However, many devices are really picky |
| 201 | * about the conditions under which they will allow a write to occur. |
| 202 | * Often devices restrict the conditions where writes can be made, |
| 203 | * so it may not be feasible to append archives stored on all types of |
| 204 | * devices. |
| 205 | * Return: |
| 206 | * 0 for success, -1 for failure |
| 207 | */ |
| 208 | |
| 209 | int |
| 210 | appnd_start(off_t skcnt) |
| 211 | { |
| 212 | int res; |
| 213 | off_t cnt; |
| 214 | |
| 215 | if (exit_val != 0) { |
| 216 | paxwarn(0, "Cannot append to an archive that may have flaws."); |
| 217 | return(-1); |
| 218 | } |
| 219 | /* |
| 220 | * if the user did not specify a write blocksize, inherit the size used |
| 221 | * in the last archive volume read. (If a is set we still use rdblksz |
| 222 | * until next volume, cannot shift sizes within a single volume). |
| 223 | */ |
| 224 | if (!wrblksz) |
| 225 | wrblksz = blksz = rdblksz; |
| 226 | else |
| 227 | blksz = rdblksz; |
| 228 | |
| 229 | /* |
| 230 | * make sure that this volume allows appends |
| 231 | */ |
| 232 | if (ar_app_ok() < 0) |
| 233 | return(-1); |
| 234 | |
| 235 | /* |
| 236 | * Calculate bytes to move back and move in front of record where we |
| 237 | * need to start writing from. Remember we have to add in any padding |
| 238 | * that might be in the buffer after the trailer in the last block. We |
| 239 | * travel skcnt + padding ROUNDED UP to blksize. |
| 240 | */ |
| 241 | skcnt += bufend - bufpt; |
| 242 | if ((cnt = (skcnt/blksz) * blksz) < skcnt) |
| 243 | cnt += blksz; |
| 244 | if (ar_rev(cnt) < 0) |
| 245 | goto out; |
| 246 | |
| 247 | /* |
| 248 | * We may have gone too far if there is valid data in the block we are |
| 249 | * now in front of, read up the block and position the pointer after |
| 250 | * the valid data. |
| 251 | */ |
| 252 | if ((cnt -= skcnt) > 0) { |
| 253 | /* |
| 254 | * watch out for stupid tape drives. ar_rev() will set rdblksz |
| 255 | * to be real physical blocksize so we must loop until we get |
| 256 | * the old rdblksz (now in blksz). If ar_rev() fouls up the |
| 257 | * determination of the physical block size, we will fail. |
| 258 | */ |
| 259 | bufpt = buf; |
| 260 | bufend = buf + blksz; |
| 261 | while (bufpt < bufend) { |
| 262 | if ((res = ar_read(bufpt, rdblksz)) <= 0) |
| 263 | goto out; |
| 264 | bufpt += res; |
| 265 | } |
| 266 | if (ar_rev(bufpt - buf) < 0) |
| 267 | goto out; |
| 268 | bufpt = buf + cnt; |
| 269 | bufend = buf + blksz; |
| 270 | } else { |
| 271 | /* |
| 272 | * buffer is empty |
| 273 | */ |
| 274 | bufend = buf + blksz; |
| 275 | bufpt = buf; |
| 276 | } |
| 277 | rdblksz = blksz; |
| 278 | rdcnt -= skcnt; |
| 279 | wrcnt = 0; |
| 280 | |
| 281 | /* |
| 282 | * At this point we are ready to write. If the device requires special |
| 283 | * handling to write at a point were previously recorded data resides, |
| 284 | * that is handled in ar_set_wr(). From now on we operate under normal |
| 285 | * ARCHIVE mode (write) conditions |
| 286 | */ |
| 287 | if (ar_set_wr() < 0) |
| 288 | return(-1); |
| 289 | act = ARCHIVE; |
| 290 | return(0); |
| 291 | |
| 292 | out: |
| 293 | paxwarn(1, "Unable to rewrite archive trailer, cannot append."); |
| 294 | return(-1); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * rd_sync() |
| 299 | * A read error occurred on this archive volume. Resync the buffer and |
| 300 | * try to reset the device (if possible) so we can continue to read. Keep |
| 301 | * trying to do this until we get a valid read, or we reach the limit on |
| 302 | * consecutive read faults (at which point we give up). The user can |
| 303 | * adjust the read error limit through a command line option. |
| 304 | * Returns: |
| 305 | * 0 on success, and -1 on failure |
| 306 | */ |
| 307 | |
| 308 | int |
| 309 | rd_sync(void) |
| 310 | { |
| 311 | int errcnt = 0; |
| 312 | int res; |
| 313 | |
| 314 | /* |
| 315 | * if the user says bail out on first fault, we are out of here... |
| 316 | */ |
| 317 | if (maxflt == 0) |
| 318 | return(-1); |
| 319 | if (act == APPND) { |
| 320 | paxwarn(1, "Unable to append when there are archive read errors."); |
| 321 | return(-1); |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * poke at device and try to get past media error |
| 326 | */ |
| 327 | if (ar_rdsync() < 0) { |
| 328 | if (ar_next() < 0) |
| 329 | return(-1); |
| 330 | else |
| 331 | rdcnt = 0; |
| 332 | } |
| 333 | |
| 334 | for (;;) { |
| 335 | if ((res = ar_read(buf, blksz)) > 0) { |
| 336 | /* |
| 337 | * All right! got some data, fill that buffer |
| 338 | */ |
| 339 | bufpt = buf; |
| 340 | bufend = buf + res; |
| 341 | rdcnt += res; |
| 342 | return(0); |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Oh well, yet another failed read... |
| 347 | * if error limit reached, ditch. o.w. poke device to move past |
| 348 | * bad media and try again. if media is badly damaged, we ask |
| 349 | * the poor (and upset user at this point) for the next archive |
| 350 | * volume. remember the goal on reads is to get the most we |
| 351 | * can extract out of the archive. |
| 352 | */ |
| 353 | if ((maxflt > 0) && (++errcnt > maxflt)) |
| 354 | paxwarn(0,"Archive read error limit (%d) reached",maxflt); |
| 355 | else if (ar_rdsync() == 0) |
| 356 | continue; |
| 357 | if (ar_next() < 0) |
| 358 | break; |
| 359 | rdcnt = 0; |
| 360 | errcnt = 0; |
| 361 | } |
| 362 | return(-1); |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * pback() |
| 367 | * push the data used during the archive id phase back into the I/O |
| 368 | * buffer. This is required as we cannot be sure that the header does NOT |
| 369 | * overlap a block boundary (as in the case we are trying to recover a |
| 370 | * flawed archived). This was not designed to be used for any other |
| 371 | * purpose. (What software engineering, HA!) |
| 372 | * WARNING: do not even THINK of pback greater than BLKMULT, unless the |
| 373 | * pback space is increased. |
| 374 | */ |
| 375 | |
| 376 | void |
| 377 | pback(const char *pt, int cnt) |
| 378 | { |
| 379 | bufpt -= cnt; |
| 380 | memcpy(bufpt, pt, cnt); |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * rd_skip() |
| 385 | * skip forward in the archive during a archive read. Used to get quickly |
| 386 | * past file data and padding for files the user did NOT select. |
| 387 | * Return: |
| 388 | * 0 if ok, -1 failure, and 1 when EOF on the archive volume was detected. |
| 389 | */ |
| 390 | |
| 391 | int |
| 392 | rd_skip(off_t skcnt) |
| 393 | { |
| 394 | off_t res; |
| 395 | off_t cnt; |
| 396 | off_t skipped = 0; |
| 397 | |
| 398 | if (skcnt < 0) { |
| 399 | paxwarn(1, "Trying to skip backwards; corrupt archive likely"); |
| 400 | sig_cleanup(0); |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * consume what data we have in the buffer. If we have to move forward |
| 405 | * whole records, we call the low level skip function to see if we can |
| 406 | * move within the archive without doing the expensive reads on data we |
| 407 | * do not want. |
| 408 | */ |
| 409 | if (skcnt == 0) |
| 410 | return(0); |
| 411 | res = MINIMUM((bufend - bufpt), skcnt); |
| 412 | bufpt += res; |
| 413 | skcnt -= res; |
| 414 | |
| 415 | /* |
| 416 | * if skcnt is now 0, then no additional i/o is needed |
| 417 | */ |
| 418 | if (skcnt == 0) |
| 419 | return(0); |
| 420 | |
| 421 | /* |
| 422 | * We have to read more, calculate complete and partial record reads |
| 423 | * based on rdblksz. we skip over "cnt" complete records |
| 424 | */ |
| 425 | res = skcnt%rdblksz; |
| 426 | cnt = (skcnt/rdblksz) * rdblksz; |
| 427 | |
| 428 | /* |
| 429 | * if the skip fails, we will have to resync. ar_fow will tell us |
| 430 | * how much it can skip over. We will have to read the rest. |
| 431 | */ |
| 432 | if (ar_fow(cnt, &skipped) < 0) |
| 433 | return(-1); |
| 434 | res += cnt - skipped; |
| 435 | rdcnt += skipped; |
| 436 | |
| 437 | /* |
| 438 | * what is left we have to read (which may be the whole thing if |
| 439 | * ar_fow() told us the device can only read to skip records); |
| 440 | */ |
| 441 | while (res > 0) { |
| 442 | cnt = bufend - bufpt; |
| 443 | /* |
| 444 | * if the read fails, we will have to resync |
| 445 | */ |
| 446 | if ((cnt <= 0) && ((cnt = buf_fill()) < 0)) |
| 447 | return(-1); |
| 448 | if (cnt == 0) |
| 449 | return(1); |
| 450 | cnt = MINIMUM(cnt, res); |
| 451 | bufpt += cnt; |
| 452 | res -= cnt; |
| 453 | } |
| 454 | return(0); |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * wr_fin() |
| 459 | * flush out any data (and pad if required) the last block. We always pad |
| 460 | * with zero (even though we do not have to). Padding with 0 makes it a |
| 461 | * lot easier to recover if the archive is damaged. zero padding SHOULD |
| 462 | * BE a requirement.... |
| 463 | */ |
| 464 | |
| 465 | void |
| 466 | wr_fin(void) |
| 467 | { |
| 468 | if (frmt->is_uar) { |
| 469 | /*XXX breaks tape/file/stream abstraction */ |
| 470 | extern int arfd; |
| 471 | |
| 472 | char *bufbt = buf; |
| 473 | ssize_t n; |
| 474 | |
| 475 | while (bufpt > bufbt) { |
| 476 | n = write(arfd, bufbt, bufpt - bufbt); |
| 477 | if (n < 0) { |
| 478 | syswarn(1, errno, "Could not finish writing"); |
| 479 | return; |
| 480 | } |
| 481 | bufbt += n; |
| 482 | } |
| 483 | } else if (bufpt > buf) { |
| 484 | memset(bufpt, 0, bufend - bufpt); |
| 485 | bufpt = bufend; |
| 486 | (void)buf_flush(blksz); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * wr_rdbuf() |
| 492 | * fill the write buffer from data passed to it in a buffer (usually used |
| 493 | * by format specific write routines to pass a file header). On failure we |
| 494 | * punt. We do not allow the user to continue to write flawed archives. |
| 495 | * We assume these headers are not very large (the memory copy we use is |
| 496 | * a bit expensive). |
| 497 | * Return: |
| 498 | * 0 if buffer was filled ok, -1 o.w. (buffer flush failure) |
| 499 | */ |
| 500 | |
| 501 | int |
| 502 | wr_rdbuf(const char *out, int outcnt) |
| 503 | { |
| 504 | int cnt; |
| 505 | |
| 506 | /* |
| 507 | * while there is data to copy copy into the write buffer. when the |
| 508 | * write buffer fills, flush it to the archive and continue |
| 509 | */ |
| 510 | while (outcnt > 0) { |
| 511 | cnt = bufend - bufpt; |
| 512 | if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) |
| 513 | return(-1); |
| 514 | /* |
| 515 | * only move what we have space for |
| 516 | */ |
| 517 | cnt = MINIMUM(cnt, outcnt); |
| 518 | memcpy(bufpt, out, cnt); |
| 519 | bufpt += cnt; |
| 520 | out += cnt; |
| 521 | outcnt -= cnt; |
| 522 | } |
| 523 | return(0); |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * rd_wrbuf() |
| 528 | * copy from the read buffer into a supplied buffer a specified number of |
| 529 | * bytes. If the read buffer is empty fill it and continue to copy. |
| 530 | * usually used to obtain a file header for processing by a format |
| 531 | * specific read routine. |
| 532 | * Return |
| 533 | * number of bytes copied to the buffer, 0 indicates EOF on archive volume, |
| 534 | * -1 is a read error |
| 535 | */ |
| 536 | |
| 537 | int |
| 538 | rd_wrbuf(char *in, int cpcnt) |
| 539 | { |
| 540 | int res; |
| 541 | int cnt; |
| 542 | int incnt = cpcnt; |
| 543 | |
| 544 | /* |
| 545 | * loop until we fill the buffer with the requested number of bytes |
| 546 | */ |
| 547 | while (incnt > 0) { |
| 548 | cnt = bufend - bufpt; |
| 549 | if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) { |
| 550 | /* |
| 551 | * read error, return what we got (or the error if |
| 552 | * no data was copied). The caller must know that an |
| 553 | * error occurred and has the best knowledge what to |
| 554 | * do with it |
| 555 | */ |
| 556 | if ((res = cpcnt - incnt) > 0) |
| 557 | return(res); |
| 558 | return(cnt); |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * calculate how much data to copy based on whats left and |
| 563 | * state of buffer |
| 564 | */ |
| 565 | cnt = MINIMUM(cnt, incnt); |
| 566 | memcpy(in, bufpt, cnt); |
| 567 | bufpt += cnt; |
| 568 | incnt -= cnt; |
| 569 | in += cnt; |
| 570 | } |
| 571 | return(cpcnt); |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * wr_skip() |
| 576 | * skip forward during a write. In other words add padding to the file. |
| 577 | * we add zero filled padding as it makes flawed archives much easier to |
| 578 | * recover from. the caller tells us how many bytes of padding to add |
| 579 | * This routine was not designed to add HUGE amount of padding, just small |
| 580 | * amounts (a few 512 byte blocks at most) |
| 581 | * Return: |
| 582 | * 0 if ok, -1 if there was a buf_flush failure |
| 583 | */ |
| 584 | |
| 585 | int |
| 586 | wr_skip(off_t skcnt) |
| 587 | { |
| 588 | int cnt; |
| 589 | |
| 590 | /* |
| 591 | * loop while there is more padding to add |
| 592 | */ |
| 593 | while (skcnt > 0) { |
| 594 | cnt = bufend - bufpt; |
| 595 | if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) |
| 596 | return(-1); |
| 597 | cnt = MINIMUM(cnt, skcnt); |
| 598 | memset(bufpt, 0, cnt); |
| 599 | bufpt += cnt; |
| 600 | skcnt -= cnt; |
| 601 | } |
| 602 | return(0); |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * wr_rdfile() |
| 607 | * fill write buffer with the contents of a file. We are passed an open |
| 608 | * file descriptor to the file and the archive structure that describes |
| 609 | * the file we are storing. The variable "left" is modified to contain the |
| 610 | * number of bytes of the file we were NOT able to write to the archive. |
| 611 | * it is important that we always write EXACTLY the number of bytes that |
| 612 | * the format specific write routine told us to. The file can also get |
| 613 | * bigger, so reading to the end of file would create an improper archive, |
| 614 | * we just detect this case and warn the user. We never create a bad |
| 615 | * archive if we can avoid it. Of course trying to archive files that are |
| 616 | * active is asking for trouble. It we fail, we pass back how much we |
| 617 | * could NOT copy and let the caller deal with it. |
| 618 | * Return: |
| 619 | * 0 ok, -1 if archive write failure. a short read of the file returns a |
| 620 | * 0, but "left" is set to be greater than zero. |
| 621 | */ |
| 622 | |
| 623 | int |
| 624 | wr_rdfile(ARCHD *arcn, int ifd, off_t *left) |
| 625 | { |
| 626 | int cnt; |
| 627 | int res = 0; |
| 628 | off_t size = arcn->sb.st_size; |
| 629 | struct stat sb; |
| 630 | |
| 631 | /* |
| 632 | * while there are more bytes to write |
| 633 | */ |
| 634 | while (size > 0) { |
| 635 | cnt = bufend - bufpt; |
| 636 | if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) { |
| 637 | *left = size; |
| 638 | return(-1); |
| 639 | } |
| 640 | cnt = MINIMUM(cnt, size); |
| 641 | if ((res = read(ifd, bufpt, cnt)) <= 0) |
| 642 | break; |
| 643 | size -= res; |
| 644 | bufpt += res; |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * better check the file did not change during this operation |
| 649 | * or the file read failed. |
| 650 | */ |
| 651 | if (res < 0) |
| 652 | syswarn(1, errno, "Read fault on %s", arcn->org_name); |
| 653 | else if (size != 0) |
| 654 | paxwarn(1, "File changed size during read %s", arcn->org_name); |
| 655 | else if (fstat(ifd, &sb) < 0) |
| 656 | syswarn(1, errno, "Failed stat on %s", arcn->org_name); |
| 657 | else if (st_timecmp(m, &arcn->sb, &sb, !=)) |
| 658 | paxwarn(1, "File %s was modified during copy to %s", |
| 659 | arcn->org_name, "archive"); |
| 660 | *left = size; |
| 661 | return(0); |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * rd_wrfile() |
| 666 | * extract the contents of a file from the archive. If we are unable to |
| 667 | * extract the entire file (due to failure to write the file) we return |
| 668 | * the numbers of bytes we did NOT process. This way the caller knows how |
| 669 | * many bytes to skip past to find the next archive header. If the failure |
| 670 | * was due to an archive read, we will catch that when we try to skip. If |
| 671 | * the format supplies a file data crc value, we calculate the actual crc |
| 672 | * so that it can be compared to the value stored in the header |
| 673 | * NOTE: |
| 674 | * We call a special function to write the file. This function attempts to |
| 675 | * restore file holes (blocks of zeros) into the file. When files are |
| 676 | * sparse this saves space, and is a LOT faster. For non sparse files |
| 677 | * the performance hit is small. As of this writing, no archive supports |
| 678 | * information on where the file holes are. |
| 679 | * Return: |
| 680 | * 0 ok, -1 if archive read failure. if we cannot write the entire file, |
| 681 | * we return a 0 but "left" is set to be the amount unwritten |
| 682 | */ |
| 683 | |
| 684 | int |
| 685 | rd_wrfile(ARCHD *arcn, int ofd, off_t *left) |
| 686 | { |
| 687 | int cnt = 0; |
| 688 | off_t size = arcn->sb.st_size; |
| 689 | int res = 0; |
| 690 | char *fnm = arcn->name; |
| 691 | int isem = 1; |
| 692 | int rem; |
| 693 | int sz = MINFBSZ; |
| 694 | struct stat sb; |
| 695 | uint32_t crc = 0; |
| 696 | |
| 697 | /* |
| 698 | * pass the blocksize of the file being written to the write routine, |
| 699 | * if the size is zero, use the default MINFBSZ |
| 700 | */ |
| 701 | if (ofd < 0) |
| 702 | sz = PAXPATHLEN + 1; /* GNU tar long link/file */ |
| 703 | else if (fstat(ofd, &sb) == 0) { |
| 704 | if (sb.st_blksize > 0) |
| 705 | sz = (int)sb.st_blksize; |
| 706 | } else |
| 707 | syswarn(0,errno,"Unable to obtain block size for file %s",fnm); |
| 708 | rem = sz; |
| 709 | *left = 0; |
| 710 | |
| 711 | /* |
| 712 | * Copy the archive to the file the number of bytes specified. We have |
| 713 | * to assume that we want to recover file holes as none of the archive |
| 714 | * formats can record the location of file holes. |
| 715 | */ |
| 716 | while (size > 0) { |
| 717 | cnt = bufend - bufpt; |
| 718 | /* |
| 719 | * if we get a read error, we do not want to skip, as we may |
| 720 | * miss a header, so we do not set left, but if we get a write |
| 721 | * error, we do want to skip over the unprocessed data. |
| 722 | */ |
| 723 | if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) |
| 724 | break; |
| 725 | cnt = MINIMUM(cnt, size); |
| 726 | if ((res = file_write(ofd,bufpt,cnt,&rem,&isem,sz,fnm)) <= 0) { |
| 727 | *left = size; |
| 728 | break; |
| 729 | } |
| 730 | |
| 731 | if (docrc) { |
| 732 | /* |
| 733 | * update the actual crc value |
| 734 | */ |
| 735 | cnt = res; |
| 736 | while (--cnt >= 0) |
| 737 | crc += *bufpt++ & 0xff; |
| 738 | } else |
| 739 | bufpt += res; |
| 740 | size -= res; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * if the last block has a file hole (all zero), we must make sure this |
| 745 | * gets updated in the file. We force the last block of zeros to be |
| 746 | * written. just closing with the file offset moved forward may not put |
| 747 | * a hole at the end of the file. |
| 748 | */ |
| 749 | if (isem && (arcn->sb.st_size > 0)) |
| 750 | file_flush(ofd, fnm, isem); |
| 751 | |
| 752 | /* |
| 753 | * if we failed from archive read, we do not want to skip |
| 754 | */ |
| 755 | if ((size > 0) && (*left == 0)) |
| 756 | return(-1); |
| 757 | |
| 758 | /* |
| 759 | * some formats record a crc on file data. If so, then we compare the |
| 760 | * calculated crc to the crc stored in the archive |
| 761 | */ |
| 762 | if (docrc && (size == 0) && (arcn->crc != crc)) |
| 763 | paxwarn(1,"Actual crc does not match expected crc %s",arcn->name); |
| 764 | return(0); |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * cp_file() |
| 769 | * copy the contents of one file to another. used during -rw phase of pax |
| 770 | * just as in rd_wrfile() we use a special write function to write the |
| 771 | * destination file so we can properly copy files with holes. |
| 772 | */ |
| 773 | |
| 774 | void |
| 775 | cp_file(ARCHD *arcn, int fd1, int fd2) |
| 776 | { |
| 777 | int cnt; |
| 778 | off_t cpcnt = 0; |
| 779 | int res = 0; |
| 780 | char *fnm = arcn->name; |
| 781 | int no_hole = 0; |
| 782 | int isem = 1; |
| 783 | int rem; |
| 784 | int sz = MINFBSZ; |
| 785 | struct stat sb; |
| 786 | |
| 787 | /* |
| 788 | * check for holes in the source file. If none, we will use regular |
| 789 | * write instead of file write. |
| 790 | */ |
| 791 | if (((off_t)(arcn->sb.st_blocks * BLKMULT)) >= (off_t)arcn->sb.st_size) |
| 792 | ++no_hole; |
| 793 | |
| 794 | /* |
| 795 | * pass the blocksize of the file being written to the write routine, |
| 796 | * if the size is zero, use the default MINFBSZ |
| 797 | */ |
| 798 | if (fstat(fd2, &sb) == 0) { |
| 799 | if (sb.st_blksize > 0) |
| 800 | sz = sb.st_blksize; |
| 801 | } else |
| 802 | syswarn(0,errno,"Unable to obtain block size for file %s",fnm); |
| 803 | rem = sz; |
| 804 | |
| 805 | /* |
| 806 | * read the source file and copy to destination file until EOF |
| 807 | */ |
| 808 | for (;;) { |
| 809 | if ((cnt = read(fd1, buf, blksz)) <= 0) |
| 810 | break; |
| 811 | if (no_hole) |
| 812 | res = write(fd2, buf, cnt); |
| 813 | else |
| 814 | res = file_write(fd2, buf, cnt, &rem, &isem, sz, fnm); |
| 815 | if (res != cnt) |
| 816 | break; |
| 817 | cpcnt += cnt; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * check to make sure the copy is valid. |
| 822 | */ |
| 823 | if (res < 0) |
| 824 | syswarn(1, errno, "Failed write during copy of %s to %s", |
| 825 | arcn->org_name, arcn->name); |
| 826 | else if (cpcnt != (off_t)arcn->sb.st_size) |
| 827 | paxwarn(1, "File %s changed size during copy to %s", |
| 828 | arcn->org_name, arcn->name); |
| 829 | else if (fstat(fd1, &sb) < 0) |
| 830 | syswarn(1, errno, "Failed stat on %s", arcn->org_name); |
| 831 | else if (st_timecmp(m, &arcn->sb, &sb, !=)) |
| 832 | paxwarn(1, "File %s was modified during copy to %s", |
| 833 | arcn->org_name, arcn->name); |
| 834 | |
| 835 | /* |
| 836 | * if the last block has a file hole (all zero), we must make sure this |
| 837 | * gets updated in the file. We force the last block of zeros to be |
| 838 | * written. just closing with the file offset moved forward may not put |
| 839 | * a hole at the end of the file. |
| 840 | */ |
| 841 | if (!no_hole && isem && (arcn->sb.st_size > 0)) |
| 842 | file_flush(fd2, fnm, isem); |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * buf_fill() |
| 847 | * fill the read buffer with the next record (or what we can get) from |
| 848 | * the archive volume. |
| 849 | * Return: |
| 850 | * Number of bytes of data in the read buffer, -1 for read error, and |
| 851 | * 0 when finished (user specified termination in ar_next()). |
| 852 | */ |
| 853 | |
| 854 | int |
| 855 | buf_fill(void) |
| 856 | { |
| 857 | return (buf_fill_internal(blksz)); |
| 858 | } |
| 859 | /*XXX exposure of this breaks block alignment, use only in ar */ |
| 860 | int |
| 861 | buf_fill_internal(int numb) |
| 862 | { |
| 863 | int cnt; |
| 864 | static int fini = 0; |
| 865 | |
| 866 | if (fini) |
| 867 | return(0); |
| 868 | |
| 869 | for (;;) { |
| 870 | /* |
| 871 | * try to fill the buffer. on error the next archive volume is |
| 872 | * opened and we try again. |
| 873 | */ |
| 874 | if ((cnt = ar_read(buf, numb)) > 0) { |
| 875 | bufpt = buf; |
| 876 | bufend = buf + cnt; |
| 877 | rdcnt += cnt; |
| 878 | return(cnt); |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * errors require resync, EOF goes to next archive |
| 883 | * but in case we have not determined yet the format, |
| 884 | * this means that we have a very short file, so we |
| 885 | * are done again. |
| 886 | */ |
| 887 | if (cnt < 0) |
| 888 | break; |
| 889 | if (ar_do_keepopen || frmt == NULL || ar_next() < 0) { |
| 890 | fini = 1; |
| 891 | return(0); |
| 892 | } |
| 893 | rdcnt = 0; |
| 894 | } |
| 895 | exit_val = 1; |
| 896 | return(-1); |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * buf_flush() |
| 901 | * force the write buffer to the archive. We are passed the number of |
| 902 | * bytes in the buffer at the point of the flush. When we change archives |
| 903 | * the record size might change. (either larger or smaller). |
| 904 | * Return: |
| 905 | * 0 if all is ok, -1 when a write error occurs. |
| 906 | */ |
| 907 | |
| 908 | int |
| 909 | buf_flush(int bufcnt) |
| 910 | { |
| 911 | int cnt; |
| 912 | int push = 0; |
| 913 | int totcnt = 0; |
| 914 | |
| 915 | /* |
| 916 | * if we have reached the user specified byte count for each archive |
| 917 | * volume, prompt for the next volume. (The non-standard -R flag). |
| 918 | * NOTE: If the wrlimit is smaller than wrcnt, we will always write |
| 919 | * at least one record. We always round limit UP to next blocksize. |
| 920 | */ |
| 921 | if ((wrlimit > 0) && (wrcnt > wrlimit)) { |
| 922 | paxwarn(0, "User specified archive volume byte limit reached."); |
| 923 | if (ar_next() < 0) { |
| 924 | wrcnt = 0; |
| 925 | exit_val = 1; |
| 926 | return(-1); |
| 927 | } |
| 928 | wrcnt = 0; |
| 929 | |
| 930 | /* |
| 931 | * The new archive volume might have changed the size of the |
| 932 | * write blocksize. if so we figure out if we need to write |
| 933 | * (one or more times), or if there is now free space left in |
| 934 | * the buffer (it is no longer full). bufcnt has the number of |
| 935 | * bytes in the buffer, (the blocksize, at the point we were |
| 936 | * CALLED). Push has the amount of "extra" data in the buffer |
| 937 | * if the block size has shrunk from a volume change. |
| 938 | */ |
| 939 | bufend = buf + blksz; |
| 940 | if (blksz > bufcnt) |
| 941 | return(0); |
| 942 | if (blksz < bufcnt) |
| 943 | push = bufcnt - blksz; |
| 944 | } |
| 945 | |
| 946 | /* |
| 947 | * We have enough data to write at least one archive block |
| 948 | */ |
| 949 | for (;;) { |
| 950 | /* |
| 951 | * write a block and check if it all went out ok |
| 952 | */ |
| 953 | cnt = ar_write(buf, blksz); |
| 954 | if (cnt == blksz) { |
| 955 | /* |
| 956 | * the write went ok |
| 957 | */ |
| 958 | wrcnt += cnt; |
| 959 | totcnt += cnt; |
| 960 | if (push > 0) { |
| 961 | /* we have extra data to push to the front. |
| 962 | * check for more than 1 block of push, and if |
| 963 | * so we loop back to write again |
| 964 | */ |
| 965 | memcpy(buf, bufend, push); |
| 966 | bufpt = buf + push; |
| 967 | if (push >= blksz) { |
| 968 | push -= blksz; |
| 969 | continue; |
| 970 | } |
| 971 | } else |
| 972 | bufpt = buf; |
| 973 | return(totcnt); |
| 974 | } else if (cnt > 0) { |
| 975 | /* |
| 976 | * Oh drat we got a partial write! |
| 977 | * if format does not care about alignment let it go, |
| 978 | * we warned the user in ar_write().... but this means |
| 979 | * the last record on this volume violates pax spec.... |
| 980 | */ |
| 981 | totcnt += cnt; |
| 982 | wrcnt += cnt; |
| 983 | bufpt = buf + cnt; |
| 984 | cnt = bufcnt - cnt; |
| 985 | memcpy(buf, bufpt, cnt); |
| 986 | bufpt = buf + cnt; |
| 987 | if (!frmt->blkalgn || ((cnt % frmt->blkalgn) == 0)) |
| 988 | return(totcnt); |
| 989 | break; |
| 990 | } |
| 991 | |
| 992 | /* |
| 993 | * All done, go to next archive |
| 994 | */ |
| 995 | wrcnt = 0; |
| 996 | if (ar_next() < 0) |
| 997 | break; |
| 998 | |
| 999 | /* |
| 1000 | * The new archive volume might also have changed the block |
| 1001 | * size. if so, figure out if we have too much or too little |
| 1002 | * data for using the new block size |
| 1003 | */ |
| 1004 | bufend = buf + blksz; |
| 1005 | if (blksz > bufcnt) |
| 1006 | return(0); |
| 1007 | if (blksz < bufcnt) |
| 1008 | push = bufcnt - blksz; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * write failed, stop pax. we must not create a bad archive! |
| 1013 | */ |
| 1014 | exit_val = 1; |
| 1015 | return(-1); |
| 1016 | } |
| 1017 | |
| 1018 | #ifndef SMALL |
| 1019 | /* |
| 1020 | * wr_rdfile replacement for the Unix Archiver (padding) |
| 1021 | */ |
| 1022 | int |
| 1023 | uar_wr_data(ARCHD *arcn, int ifd, off_t *left) |
| 1024 | { |
| 1025 | int cnt; |
| 1026 | |
| 1027 | if (wr_rdfile(arcn, ifd, left) < 0) |
| 1028 | return (-1); |
| 1029 | if (!arcn->pad) |
| 1030 | return (0); |
| 1031 | /*XXX arcn->pad == 1 */ |
| 1032 | arcn->pad = 0; |
| 1033 | cnt = bufend - bufpt; |
| 1034 | if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) { |
| 1035 | /* *left == 0 */ |
| 1036 | *left = 1; |
| 1037 | return (-1); |
| 1038 | } |
| 1039 | *bufpt++ = '\n'; |
| 1040 | return (0); |
| 1041 | } |
| 1042 | #endif |