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