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