/bin/pax/buf_subs.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 986 lines · 472 code · 80 blank · 434 comment · 136 complexity · 8b231b095f7a8b31e2bb7220ba2e1a60 MD5 · raw file

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