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