source: freewrt/tools/paxmirabilis/src/buf_subs.c@ a569125

freewrt_1_0 freewrt_2_0
Last change on this file since a569125 was a569125, checked in by Thorsten Glaser <tg@…>, 14 years ago

even FreeWRT 1.0-stable deserves paxmirabilis-20120216 compiled with LTO ☺

git-svn-id: svn://www.freewrt.org/branches/freewrt_1_0@3981 afb5a338-a214-0410-bd46-81f09a774fd1

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