157193Smuller /*-
257193Smuller * Copyright (c) 1992 Keith Muller.
360676Sbostic * Copyright (c) 1992, 1993
460676Sbostic * The Regents of the University of California. All rights reserved.
557193Smuller *
657193Smuller * This code is derived from software contributed to Berkeley by
757193Smuller * Keith Muller of the University of California, San Diego.
857193Smuller *
957193Smuller * %sccs.include.redist.c%
1057193Smuller */
1157193Smuller
1257193Smuller #ifndef lint
13*66890Sbostic static char sccsid[] = "@(#)buf_subs.c 8.2 (Berkeley) 04/18/94";
1457193Smuller #endif /* not lint */
1557193Smuller
1657193Smuller #include <sys/types.h>
1757193Smuller #include <sys/time.h>
1857193Smuller #include <sys/stat.h>
1957193Smuller #include <sys/param.h>
2057193Smuller #include <stdio.h>
2157193Smuller #include <ctype.h>
2257193Smuller #include <errno.h>
2357193Smuller #include <unistd.h>
2457193Smuller #include <stdlib.h>
2557193Smuller #include <string.h>
2657193Smuller #include "pax.h"
2757193Smuller #include "extern.h"
2857193Smuller
2957193Smuller /*
3057193Smuller * routines which implement archive and file buffering
3157193Smuller */
3257193Smuller
33*66890Sbostic #define MINFBSZ 512 /* default block size for hole detect */
3457193Smuller #define MAXFLT 10 /* default media read error limit */
3557193Smuller
36*66890Sbostic /*
37*66890Sbostic * Need to change bufmem to dynamic allocation when the upper
38*66890Sbostic * limit on blocking size is removed (though that will violate pax spec)
39*66890Sbostic * MAXBLK define and tests will also need to be updated.
40*66890Sbostic */
4157193Smuller static char bufmem[MAXBLK+BLKMULT]; /* i/o buffer + pushback id space */
4257193Smuller static char *buf; /* normal start of i/o buffer */
4357193Smuller static char *bufend; /* end or last char in i/o buffer */
4457193Smuller static char *bufpt; /* read/write point in i/o buffer */
4557193Smuller int blksz = MAXBLK; /* block input/output size in bytes */
4657193Smuller int wrblksz; /* user spec output size in bytes */
4757193Smuller int maxflt = MAXFLT; /* MAX consecutive media errors */
4857193Smuller int rdblksz; /* first read blksize (tapes only) */
4957193Smuller off_t wrlimit; /* # of bytes written per archive vol */
5057193Smuller off_t wrcnt; /* # of bytes written on current vol */
5157193Smuller off_t rdcnt; /* # of bytes read on current vol */
5257193Smuller
5357193Smuller /*
5457193Smuller * wr_start()
5557193Smuller * set up the buffering system to operate in a write mode
5657193Smuller * Return:
5757193Smuller * 0 if ok, -1 if the user specified write block size violates pax spec
5857193Smuller */
5957193Smuller
6057193Smuller #if __STDC__
6157193Smuller int
wr_start(void)6257193Smuller wr_start(void)
6357193Smuller #else
6457193Smuller int
6557193Smuller wr_start()
6657193Smuller #endif
6757193Smuller {
6857193Smuller buf = &(bufmem[BLKMULT]);
6957193Smuller /*
7057193Smuller * Check to make sure the write block size meets pax specs. If the user
7157193Smuller * does not specify a blocksize, we use the format default blocksize.
7257193Smuller * We must be picky on writes, so we do not allow the user to create an
7357193Smuller * archive that might be hard to read elsewhere. If all ok, we then
7457193Smuller * open the first archive volume
7557193Smuller */
7657193Smuller if (!wrblksz)
7757193Smuller wrblksz = frmt->bsz;
7857193Smuller if (wrblksz > MAXBLK) {
7957193Smuller warn(1, "Write block size of %d too large, maximium is: %d",
8057193Smuller wrblksz, MAXBLK);
8157193Smuller return(-1);
8257193Smuller }
8357193Smuller if (wrblksz % BLKMULT) {
8457193Smuller warn(1, "Write block size of %d is not a %d byte multiple",
8557193Smuller wrblksz, BLKMULT);
8657193Smuller return(-1);
8757193Smuller }
8857193Smuller
8957193Smuller /*
9057193Smuller * we only allow wrblksz to be used with all archive operations
9157193Smuller */
9257193Smuller blksz = rdblksz = wrblksz;
9357193Smuller if ((ar_open(arcname) < 0) && (ar_next() < 0))
9457193Smuller return(-1);
9557193Smuller wrcnt = 0;
9657193Smuller bufend = buf + wrblksz;
9757193Smuller bufpt = buf;
9857193Smuller return(0);
9957193Smuller }
10057193Smuller
10157193Smuller /*
10257193Smuller * rd_start()
10357193Smuller * set up buffering system to read an archive
10457193Smuller * Return:
10557193Smuller * 0 if ok, -1 otherwise
10657193Smuller */
10757193Smuller
10857193Smuller #if __STDC__
10957193Smuller int
rd_start(void)11057193Smuller rd_start(void)
11157193Smuller #else
11257193Smuller int
11357193Smuller rd_start()
11457193Smuller #endif
11557193Smuller {
11657193Smuller /*
11757193Smuller * leave space for the header pushback (see get_arc()). If we are
11857193Smuller * going to append and user specified a write block size, check it
11957193Smuller * right away
12057193Smuller */
12157193Smuller buf = &(bufmem[BLKMULT]);
12257193Smuller if ((act == APPND) && wrblksz) {
12357193Smuller if (wrblksz > MAXBLK) {
12457193Smuller warn(1,"Write block size %d too large, maximium is: %d",
12557193Smuller wrblksz, MAXBLK);
12657193Smuller return(-1);
12757193Smuller }
12857193Smuller if (wrblksz % BLKMULT) {
12957193Smuller warn(1, "Write block size %d is not a %d byte multiple",
13057193Smuller wrblksz, BLKMULT);
13157193Smuller return(-1);
13257193Smuller }
13357193Smuller }
13457193Smuller
13557193Smuller /*
13657193Smuller * open the archive
13757193Smuller */
13857193Smuller if ((ar_open(arcname) < 0) && (ar_next() < 0))
13957193Smuller return(-1);
14057193Smuller bufend = buf + rdblksz;
14157193Smuller bufpt = bufend;
14257193Smuller rdcnt = 0;
14357193Smuller return(0);
14457193Smuller }
14557193Smuller
14657193Smuller /*
14757193Smuller * cp_start()
14857193Smuller * set up buffer system for copying within the file system
14957193Smuller */
15057193Smuller
15157193Smuller #if __STDC__
15257193Smuller void
cp_start(void)15357193Smuller cp_start(void)
15457193Smuller #else
15557193Smuller void
15657193Smuller cp_start()
15757193Smuller #endif
15857193Smuller {
15957193Smuller buf = &(bufmem[BLKMULT]);
16057193Smuller rdblksz = blksz = MAXBLK;
16157193Smuller }
16257193Smuller
16357193Smuller /*
16457193Smuller * appnd_start()
16557193Smuller * Set up the buffering system to append new members to an archive that
16657193Smuller * was just read. The last block(s) of an archive may contain a format
16757193Smuller * specific trailer. To append a new member, this trailer has to be
16857193Smuller * removed from the archive. The first byte of the trailer is replaced by
16957193Smuller * the start of the header of the first file added to the archive. The
17057193Smuller * format specific end read function tells us how many bytes to move
17157193Smuller * backwards in the archive to be positioned BEFORE the trailer. Two
17257193Smuller * different postions have to be adjusted, the O.S. file offset (e.g. the
17357193Smuller * position of the tape head) and the write point within the data we have
17457193Smuller * stored in the read (soon to become write) buffer. We may have to move
17557193Smuller * back several records (the number depends on the size of the archive
17657193Smuller * record and the size of the format trailer) to read up the record where
17757193Smuller * the first byte of the trailer is recorded. Trailers may span (and
17857193Smuller * overlap) record boundries.
17957193Smuller * We first calculate which record has the first byte of the trailer. We
18057193Smuller * move the OS file offset back to the start of this record and read it
18157193Smuller * up. We set the buffer write pointer to be at this byte (the byte where
18257193Smuller * the trailer starts). We then move the OS file pointer back to the
18357193Smuller * start of this record so a flush of this buffer will replace the record
18457193Smuller * in the archive.
18557193Smuller * A major problem is rewriting this last record. For archives stored
18657193Smuller * on disk files, this is trival. However, many devices are really picky
18757193Smuller * about the conditions under which they will allow a write to occur.
18857193Smuller * Often devices restrict the conditions where writes can be made writes,
18957193Smuller * so it may not be feasable to append archives stored on all types of
19057193Smuller * devices.
19157193Smuller * Return:
19257193Smuller * 0 for success, -1 for failure
19357193Smuller */
19457193Smuller
19557193Smuller #if __STDC__
19657193Smuller int
appnd_start(off_t skcnt)19757193Smuller appnd_start(off_t skcnt)
19857193Smuller #else
19957193Smuller int
20057193Smuller appnd_start(skcnt)
20157193Smuller off_t skcnt;
20257193Smuller #endif
20357193Smuller {
20457193Smuller register int res;
20557193Smuller off_t cnt;
20657193Smuller
20757537Smuller if (exit_val != 0) {
20857537Smuller warn(0, "Cannot append to an archive that may have flaws.");
20957193Smuller return(-1);
21057537Smuller }
21157193Smuller /*
21257193Smuller * if the user did not specify a write blocksize, inherit the size used
21357193Smuller * in the last archive volume read. (If a is set we still use rdblksz
21457193Smuller * until next volume, cannot shift sizes within a single volume).
21557193Smuller */
21657193Smuller if (!wrblksz)
21757193Smuller wrblksz = blksz = rdblksz;
21857193Smuller else
21957193Smuller blksz = rdblksz;
22057193Smuller
22157193Smuller /*
22257193Smuller * make sure that this volume allows appends
22357193Smuller */
22457193Smuller if (ar_app_ok() < 0)
22557193Smuller return(-1);
22657193Smuller
22757193Smuller /*
22857193Smuller * Calculate bytes to move back and move in front of record where we
22957193Smuller * need to start writing from. Remember we have to add in any padding
23057193Smuller * that might be in the buffer after the trailer in the last block. We
23157193Smuller * travel skcnt + padding ROUNDED UP to blksize.
23257193Smuller */
23357193Smuller skcnt += bufend - bufpt;
23457193Smuller if ((cnt = (skcnt/blksz) * blksz) < skcnt)
23557193Smuller cnt += blksz;
23657193Smuller if (ar_rev((off_t)cnt) < 0)
23757193Smuller goto out;
23857193Smuller
23957193Smuller /*
24057193Smuller * We may have gone too far if there is valid data in the block we are
24157193Smuller * now in front of, read up the block and position the pointer after
24257193Smuller * the valid data.
24357193Smuller */
24457193Smuller if ((cnt -= skcnt) > 0) {
24557193Smuller /*
24657193Smuller * watch out for stupid tape drives. ar_rev() will set rdblksz
24757193Smuller * to be real physical blocksize so we must loop until we get
24857193Smuller * the old rdblksz (now in blksz). If ar_rev() fouls up the
24957193Smuller * determination of the physical block size, we will fail.
25057193Smuller */
25157193Smuller bufpt = buf;
25257193Smuller bufend = buf + blksz;
25357193Smuller while (bufpt < bufend) {
25457193Smuller if ((res = ar_read(bufpt, rdblksz)) <= 0)
25557193Smuller goto out;
25657193Smuller bufpt += res;
25757193Smuller }
25857193Smuller if (ar_rev((off_t)(bufpt - buf)) < 0)
25957193Smuller goto out;
26057193Smuller bufpt = buf + cnt;
26157193Smuller bufend = buf + blksz;
26257193Smuller } else {
26357193Smuller /*
26457193Smuller * buffer is empty
26557193Smuller */
26657193Smuller bufend = buf + blksz;
26757193Smuller bufpt = buf;
26857193Smuller }
26957193Smuller rdblksz = blksz;
27057193Smuller rdcnt -= skcnt;
27157193Smuller wrcnt = 0;
27257193Smuller
27357193Smuller /*
27457193Smuller * At this point we are ready to write. If the device requires special
27557193Smuller * handling to write at a point were previously recorded data resides,
27657193Smuller * that is handled in ar_set_wr(). From now on we operate under normal
27757193Smuller * ARCHIVE mode (write) conditions
27857193Smuller */
27957193Smuller if (ar_set_wr() < 0)
28057193Smuller return(-1);
28157193Smuller act = ARCHIVE;
28257193Smuller return(0);
28357193Smuller
28457193Smuller out:
28557537Smuller warn(1, "Unable to rewrite archive trailer, cannot append.");
28657193Smuller return(-1);
28757193Smuller }
28857193Smuller
28957193Smuller /*
29057193Smuller * rd_sync()
29157193Smuller * A read error occurred on this archive volume. Resync the buffer and
29257193Smuller * try to reset the device (if possible) so we can continue to read. Keep
29357193Smuller * trying to do this until we get a valid read, or we reach the limit on
29457193Smuller * consecutive read faults (at which point we give up). The user can
29557193Smuller * adjust the read error limit through a command line option.
29657193Smuller * Returns:
29757193Smuller * 0 on success, and -1 on failure
29857193Smuller */
29957193Smuller
30057193Smuller #if __STDC__
30157193Smuller int
rd_sync(void)30257193Smuller rd_sync(void)
30357193Smuller #else
30457193Smuller int
30557193Smuller rd_sync()
30657193Smuller #endif
30757193Smuller {
30857193Smuller register int errcnt = 0;
30957193Smuller register int res;
31057193Smuller
31157193Smuller /*
31257193Smuller * if the user says bail out on first fault, we are out of here...
31357193Smuller */
31457193Smuller if (maxflt == 0)
31557193Smuller return(-1);
31657193Smuller if (act == APPND) {
31757193Smuller warn(1, "Unable to append when there are archive read errors.");
31857193Smuller return(-1);
31957193Smuller }
32057193Smuller
32157193Smuller /*
32257193Smuller * poke at device and try to get past media error
32357193Smuller */
32457193Smuller if (ar_rdsync() < 0) {
32557193Smuller if (ar_next() < 0)
32657193Smuller return(-1);
32757193Smuller else
32857193Smuller rdcnt = 0;
32957193Smuller }
33057193Smuller
33157193Smuller for (;;) {
33257193Smuller if ((res = ar_read(buf, blksz)) > 0) {
33357193Smuller /*
33457193Smuller * All right! got some data, fill that buffer
33557193Smuller */
33657193Smuller bufpt = buf;
33757193Smuller bufend = buf + res;
33857193Smuller rdcnt += res;
33957193Smuller return(0);
34057193Smuller }
34157193Smuller
34257193Smuller /*
34357193Smuller * Oh well, yet another failed read...
34457193Smuller * if error limit reached, ditch. o.w. poke device to move past
34557193Smuller * bad media and try again. if media is badly damaged, we ask
34657193Smuller * the poor (and upset user at this point) for the next archive
34757193Smuller * volume. remember the goal on reads is to get the most we
34857193Smuller * can extract out of the archive.
34957193Smuller */
35057193Smuller if ((maxflt > 0) && (++errcnt > maxflt))
35157537Smuller warn(0,"Archive read error limit (%d) reached",maxflt);
35257193Smuller else if (ar_rdsync() == 0)
35357193Smuller continue;
35457193Smuller if (ar_next() < 0)
35557193Smuller break;
35657193Smuller rdcnt = 0;
35757193Smuller errcnt = 0;
35857193Smuller }
35957193Smuller return(-1);
36057193Smuller }
36157193Smuller
36257193Smuller /*
36357193Smuller * pback()
36457193Smuller * push the data used during the archive id phase back into the I/O
36557193Smuller * buffer. This is required as we cannot be sure that the header does NOT
36657193Smuller * overlap a block boundry (as in the case we are trying to recover a
36757193Smuller * flawed archived). This was not designed to be used for any other
36857193Smuller * purpose. (What software engineering, HA!)
36957193Smuller * WARNING: do not even THINK of pback greater than BLKMULT, unless the
37057193Smuller * pback space is increased.
37157193Smuller */
37257193Smuller
37357193Smuller #if __STDC__
37457193Smuller void
pback(char * pt,int cnt)37557193Smuller pback(char *pt, int cnt)
37657193Smuller #else
37757193Smuller void
37857193Smuller pback(pt, cnt)
37957193Smuller char *pt;
38057193Smuller int cnt;
38157193Smuller #endif
38257193Smuller {
38357193Smuller bufpt -= cnt;
38457193Smuller bcopy(pt, bufpt, cnt);
38557193Smuller return;
38657193Smuller }
38757193Smuller
38857193Smuller /*
38957193Smuller * rd_skip()
39057193Smuller * skip foward in the archive during a archive read. Used to get quickly
39157193Smuller * past file data and padding for files the user did NOT select.
39257193Smuller * Return:
39357193Smuller * 0 if ok, -1 failure, and 1 when EOF on the archive volume was detected.
39457193Smuller */
39557193Smuller
39657193Smuller #if __STDC__
39757193Smuller int
rd_skip(off_t skcnt)39857193Smuller rd_skip(off_t skcnt)
39957193Smuller #else
40057193Smuller int
40157193Smuller rd_skip(skcnt)
40257193Smuller off_t skcnt;
40357193Smuller #endif
40457193Smuller {
40557193Smuller off_t res;
40657193Smuller off_t cnt;
40757193Smuller off_t skipped = 0;
40857193Smuller
40957193Smuller /*
41057193Smuller * consume what data we have in the buffer. If we have to move foward
41157193Smuller * whole records, we call the low level skip function to see if we can
41257193Smuller * move within the archive without doing the expensive reads on data we
41357193Smuller * do not want.
41457193Smuller */
41557193Smuller if (skcnt == 0)
41657193Smuller return(0);
41757193Smuller res = MIN((bufend - bufpt), skcnt);
41857193Smuller bufpt += res;
41957193Smuller skcnt -= res;
42057193Smuller
42157193Smuller /*
42257193Smuller * if skcnt is now 0, then no additional i/o is needed
42357193Smuller */
42457193Smuller if (skcnt == 0)
42557193Smuller return(0);
42657193Smuller
42757193Smuller /*
42857193Smuller * We have to read more, calculate complete and partial record reads
42957193Smuller * based on rdblksz. we skip over "cnt" complete records
43057193Smuller */
43157193Smuller res = skcnt%rdblksz;
43257193Smuller cnt = (skcnt/rdblksz) * rdblksz;
43357193Smuller
43457193Smuller /*
43557193Smuller * if the skip fails, we will have to resync. ar_fow will tell us
43657193Smuller * how much it can skip over. We will have to read the rest.
43757193Smuller */
43857193Smuller if (ar_fow(cnt, &skipped) < 0)
43957193Smuller return(-1);
44057193Smuller res += cnt - skipped;
44157193Smuller rdcnt += skipped;
44257193Smuller
44357193Smuller /*
44457193Smuller * what is left we have to read (which may be the whole thing if
44557193Smuller * ar_fow() told us the device can only read to skip records);
44657193Smuller */
44757193Smuller while (res > 0L) {
44857193Smuller cnt = bufend - bufpt;
44957193Smuller /*
45057193Smuller * if the read fails, we will have to resync
45157193Smuller */
45257193Smuller if ((cnt <= 0) && ((cnt = buf_fill()) < 0))
45357193Smuller return(-1);
45457193Smuller if (cnt == 0)
45557193Smuller return(1);
45657193Smuller cnt = MIN(cnt, res);
45757193Smuller bufpt += cnt;
45857193Smuller res -= cnt;
45957193Smuller }
46057193Smuller return(0);
46157193Smuller }
46257193Smuller
46357193Smuller /*
46457193Smuller * wr_fin()
46557193Smuller * flush out any data (and pad if required) the last block. We always pad
46657193Smuller * with zero (even though we do not have to). Padding with 0 makes it a
46757193Smuller * lot easier to recover if the archive is damaged. zero paddding SHOULD
46857193Smuller * BE a requirement....
46957193Smuller */
47057193Smuller
47157193Smuller #if __STDC__
47257193Smuller void
wr_fin(void)47357193Smuller wr_fin(void)
47457193Smuller #else
47557193Smuller void
47657193Smuller wr_fin()
47757193Smuller #endif
47857193Smuller {
47957193Smuller if (bufpt > buf) {
48057193Smuller bzero(bufpt, bufend - bufpt);
48157193Smuller bufpt = bufend;
48257193Smuller (void)buf_flush(blksz);
48357193Smuller }
48457193Smuller }
48557193Smuller
48657193Smuller /*
48757193Smuller * wr_rdbuf()
48857193Smuller * fill the write buffer from data passed to it in a buffer (usually used
48957193Smuller * by format specific write routines to pass a file header). On failure we
49057193Smuller * punt. We do not allow the user to continue to write flawed archives.
49157193Smuller * We assume these headers are not very large (the memory copy we use is
49257193Smuller * a bit expensive).
49357193Smuller * Return:
49457193Smuller * 0 if buffer was filled ok, -1 o.w. (buffer flush failure)
49557193Smuller */
49657193Smuller
49757193Smuller #if __STDC__
49857193Smuller int
wr_rdbuf(register char * out,register int outcnt)49957193Smuller wr_rdbuf(register char *out, register int outcnt)
50057193Smuller #else
50157193Smuller int
50257193Smuller wr_rdbuf(out, outcnt)
50357193Smuller register char *out;
50457193Smuller register int outcnt;
50557193Smuller #endif
50657193Smuller {
50757193Smuller register int cnt;
50857193Smuller
50957193Smuller /*
51057193Smuller * while there is data to copy copy into the write buffer. when the
51157193Smuller * write buffer fills, flush it to the archive and continue
51257193Smuller */
51357193Smuller while (outcnt > 0) {
51457193Smuller cnt = bufend - bufpt;
51557193Smuller if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0))
51657193Smuller return(-1);
51757193Smuller /*
51857193Smuller * only move what we have space for
51957193Smuller */
52057193Smuller cnt = MIN(cnt, outcnt);
52157193Smuller bcopy(out, bufpt, cnt);
52257193Smuller bufpt += cnt;
52357193Smuller out += cnt;
52457193Smuller outcnt -= cnt;
52557193Smuller }
52657193Smuller return(0);
52757193Smuller }
52857193Smuller
52957193Smuller /*
53057193Smuller * rd_wrbuf()
53157193Smuller * copy from the read buffer into a supplied buffer a specified number of
53257193Smuller * bytes. If the read buffer is empty fill it and continue to copy.
53357193Smuller * usually used to obtain a file header for processing by a format
53457193Smuller * specific read routine.
53557193Smuller * Return
53657193Smuller * number of bytes copied to the buffer, 0 indicates EOF on archive volume,
53757193Smuller * -1 is a read error
53857193Smuller */
53957193Smuller
54057193Smuller #if __STDC__
54157193Smuller int
rd_wrbuf(register char * in,register int cpcnt)54257193Smuller rd_wrbuf(register char *in, register int cpcnt)
54357193Smuller #else
54457193Smuller int
54557193Smuller rd_wrbuf(in, cpcnt)
54657193Smuller register char *in;
54757193Smuller register int cpcnt;
54857193Smuller #endif
54957193Smuller {
55057193Smuller register int res;
55157193Smuller register int cnt;
55257193Smuller register int incnt = cpcnt;
55357193Smuller
55457193Smuller /*
55557193Smuller * loop until we fill the buffer with the requested number of bytes
55657193Smuller */
55757193Smuller while (incnt > 0) {
55857193Smuller cnt = bufend - bufpt;
55957193Smuller if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) {
56057193Smuller /*
56157193Smuller * read error, return what we got (or the error if
56257193Smuller * no data was copied). The caller must know that an
56357193Smuller * error occured and has the best knowledge what to
56457193Smuller * do with it
56557193Smuller */
56657193Smuller if ((res = cpcnt - incnt) > 0)
56757193Smuller return(res);
56857193Smuller return(cnt);
56957193Smuller }
57057193Smuller
57157193Smuller /*
57257193Smuller * calculate how much data to copy based on whats left and
57357193Smuller * state of buffer
57457193Smuller */
57557193Smuller cnt = MIN(cnt, incnt);
57657193Smuller bcopy(bufpt, in, cnt);
57757193Smuller bufpt += cnt;
57857193Smuller incnt -= cnt;
57957193Smuller in += cnt;
58057193Smuller }
58157193Smuller return(cpcnt);
58257193Smuller }
58357193Smuller
58457193Smuller /*
58557193Smuller * wr_skip()
58657193Smuller * skip foward during a write. In other words add padding to the file.
58757193Smuller * we add zero filled padding as it makes flawed archives much easier to
58857193Smuller * recover from. the caller tells us how many bytes of padding to add
58957193Smuller * This routine was not designed to add HUGE amount of padding, just small
59057193Smuller * amounts (a few 512 byte blocks at most)
59157193Smuller * Return:
59257193Smuller * 0 if ok, -1 if there was a buf_flush failure
59357193Smuller */
59457193Smuller
59557193Smuller #if __STDC__
59657193Smuller int
wr_skip(off_t skcnt)59757193Smuller wr_skip(off_t skcnt)
59857193Smuller #else
59957193Smuller int
60057193Smuller wr_skip(skcnt)
60157193Smuller off_t skcnt;
60257193Smuller #endif
60357193Smuller {
60457193Smuller register int cnt;
60557193Smuller
60657193Smuller /*
60757193Smuller * loop while there is more padding to add
60857193Smuller */
60957193Smuller while (skcnt > 0L) {
61057193Smuller cnt = bufend - bufpt;
61157193Smuller if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0))
61257193Smuller return(-1);
61357193Smuller cnt = MIN(cnt, skcnt);
61457193Smuller bzero(bufpt, cnt);
61557193Smuller bufpt += cnt;
61657193Smuller skcnt -= cnt;
61757193Smuller }
61857193Smuller return(0);
61957193Smuller }
62057193Smuller
62157193Smuller /*
62257193Smuller * wr_rdfile()
62357193Smuller * fill write buffer with the contents of a file. We are passed an open
62457193Smuller * file descriptor to the file an the archive structure that describes the
62557193Smuller * file we are storing. The variable "left" is modified to contain the
62657193Smuller * number of bytes of the file we were NOT able to write to the archive.
62757193Smuller * it is important that we always write EXACTLY the number of bytes that
62857193Smuller * the format specific write routine told us to. The file can also get
62957193Smuller * bigger, so reading to the end of file would create an improper archive,
63057193Smuller * we just detect this case and warn the user. We never create a bad
63157193Smuller * archive if we can avoid it. Of course trying to archive files that are
63257193Smuller * active is asking for trouble. It we fail, we pass back how much we
63357193Smuller * could NOT copy and let the caller deal with it.
63457193Smuller * Return:
63557193Smuller * 0 ok, -1 if archive write failure. a short read of the file returns a
63657193Smuller * 0, but "left" is set to be greater than zero.
63757193Smuller */
63857193Smuller
63957193Smuller #if __STDC__
64057193Smuller int
wr_rdfile(ARCHD * arcn,int ifd,off_t * left)64157193Smuller wr_rdfile(ARCHD *arcn, int ifd, off_t *left)
64257193Smuller #else
64357193Smuller int
64457193Smuller wr_rdfile(arcn, ifd, left)
64557193Smuller ARCHD *arcn;
64657193Smuller int ifd;
64757193Smuller off_t *left;
64857193Smuller #endif
64957193Smuller {
65057193Smuller register int cnt;
65157193Smuller register int res = 0;
65257193Smuller register off_t size = arcn->sb.st_size;
65357193Smuller struct stat sb;
65457193Smuller
65557193Smuller /*
65657193Smuller * while there are more bytes to write
65757193Smuller */
65857193Smuller while (size > 0L) {
65957193Smuller cnt = bufend - bufpt;
66057193Smuller if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) {
66157193Smuller *left = size;
66257193Smuller return(-1);
66357193Smuller }
66457193Smuller cnt = MIN(cnt, size);
66557193Smuller if ((res = read(ifd, bufpt, cnt)) <= 0)
66657193Smuller break;
66757193Smuller size -= res;
66857193Smuller bufpt += res;
66957193Smuller }
67057193Smuller
67157193Smuller /*
67257193Smuller * better check the file did not change during this operation
67357193Smuller * or the file read failed.
67457193Smuller */
67557193Smuller if (res < 0)
67657193Smuller syswarn(1, errno, "Read fault on %s", arcn->org_name);
67757193Smuller else if (size != 0L)
67857193Smuller warn(1, "File changed size during read %s", arcn->org_name);
67957193Smuller else if (fstat(ifd, &sb) < 0)
68057193Smuller syswarn(1, errno, "Failed stat on %s", arcn->org_name);
68157193Smuller else if (arcn->sb.st_mtime != sb.st_mtime)
68257193Smuller warn(1, "File %s was modified during copy to archive",
68357193Smuller arcn->org_name);
68457193Smuller *left = size;
68557193Smuller return(0);
68657193Smuller }
68757193Smuller
68857193Smuller /*
68957193Smuller * rd_wrfile()
69057193Smuller * extract the contents of a file from the archive. If we are unable to
69157193Smuller * extract the entire file (due to failure to write the file) we return
69257193Smuller * the numbers of bytes we did NOT process. This way the caller knows how
69357193Smuller * many bytes to skip past to find the next archive header. If the failure
69457193Smuller * was due to an archive read, we will catch that when we try to skip. If
69557193Smuller * the format supplies a file data crc value, we calculate the actual crc
69657193Smuller * so that it can be compared to the value stored in the header
69757193Smuller * NOTE:
69857193Smuller * We call a special function to write the file. This function attempts to
69957193Smuller * restore file holes (blocks of zeros) into the file. When files are
70057193Smuller * sparse this saves space, and is a LOT faster. For non sparse files
70157193Smuller * the performance hit is small. As of this writing, no archive supports
70257193Smuller * information on where the file holes are.
70357193Smuller * Return:
70457193Smuller * 0 ok, -1 if archive read failure. if we cannot write the entire file,
70557193Smuller * we return a 0 but "left" is set to be the amount unwritten
70657193Smuller */
70757193Smuller
70857193Smuller #if __STDC__
70957193Smuller int
rd_wrfile(ARCHD * arcn,int ofd,off_t * left)71057193Smuller rd_wrfile(ARCHD *arcn, int ofd, off_t *left)
71157193Smuller #else
71257193Smuller int
71357193Smuller rd_wrfile(arcn, ofd, left)
71457193Smuller ARCHD *arcn;
71557193Smuller int ofd;
71657193Smuller off_t *left;
71757193Smuller #endif
71857193Smuller {
71957193Smuller register int cnt = 0;
72057193Smuller register off_t size = arcn->sb.st_size;
72157193Smuller register int res = 0;
72257193Smuller register char *fnm = arcn->name;
72357193Smuller int isem = 1;
72457193Smuller int rem;
72557193Smuller int sz = MINFBSZ;
72657193Smuller struct stat sb;
72757193Smuller u_long crc = 0L;
72857193Smuller
72957193Smuller /*
73057193Smuller * pass the blocksize of the file being written to the write routine,
731*66890Sbostic * if the size is zero, use the default MINFBSZ
73257193Smuller */
73357193Smuller if (fstat(ofd, &sb) == 0) {
734*66890Sbostic if (sb.st_blksize > 0)
73557193Smuller sz = (int)sb.st_blksize;
73657193Smuller } else
73757193Smuller syswarn(0,errno,"Unable to obtain block size for file %s",fnm);
73857193Smuller rem = sz;
73957193Smuller *left = 0L;
74057193Smuller
74157193Smuller /*
74257193Smuller * Copy the archive to the file the number of bytes specified. We have
74357193Smuller * to assume that we want to recover file holes as none of the archive
74457193Smuller * formats can record the location of file holes.
74557193Smuller */
74657193Smuller while (size > 0L) {
74757193Smuller cnt = bufend - bufpt;
74857193Smuller /*
74957193Smuller * if we get a read error, we do not want to skip, as we may
75057193Smuller * miss a header, so we do not set left, but if we get a write
75157193Smuller * error, we do want to skip over the unprocessed data.
75257193Smuller */
75357193Smuller if ((cnt <= 0) && ((cnt = buf_fill()) <= 0))
75457193Smuller break;
75557193Smuller cnt = MIN(cnt, size);
75657193Smuller if ((res = file_write(ofd,bufpt,cnt,&rem,&isem,sz,fnm)) <= 0) {
75757193Smuller *left = size;
75857193Smuller break;
75957193Smuller }
76057193Smuller
76157193Smuller if (docrc) {
76257193Smuller /*
76357193Smuller * update the actual crc value
76457193Smuller */
76557193Smuller cnt = res;
76657193Smuller while (--cnt >= 0)
76757193Smuller crc += *bufpt++ & 0xff;
76857193Smuller } else
76957193Smuller bufpt += res;
77057193Smuller size -= res;
77157193Smuller }
77257193Smuller
77357193Smuller /*
77457193Smuller * if the last block has a file hole (all zero), we must make sure this
77557193Smuller * gets updated in the file. We force the last block of zeros to be
77657193Smuller * written. just closing with the file offset moved foward may not put
77757193Smuller * a hole at the end of the file.
77857193Smuller */
77957193Smuller if (isem && (arcn->sb.st_size > 0L))
78057193Smuller file_flush(ofd, fnm, isem);
78157193Smuller
78257193Smuller /*
78357193Smuller * if we failed from archive read, we do not want to skip
78457193Smuller */
78557193Smuller if ((size > 0L) && (*left == 0L))
78657193Smuller return(-1);
78757193Smuller
78857193Smuller /*
78957193Smuller * some formats record a crc on file data. If so, then we compare the
79057193Smuller * calculated crc to the crc stored in the archive
79157193Smuller */
79257193Smuller if (docrc && (size == 0L) && (arcn->crc != crc))
79357193Smuller warn(1,"Actual crc does not match expected crc %s",arcn->name);
79457193Smuller return(0);
79557193Smuller }
79657193Smuller
79757193Smuller /*
79857193Smuller * cp_file()
79957193Smuller * copy the contents of one file to another. used during -rw phase of pax
80057193Smuller * just as in rd_wrfile() we use a special write function to write the
80157193Smuller * destination file so we can properly copy files with holes.
80257193Smuller */
80357193Smuller
80457193Smuller #if __STDC__
80557193Smuller void
cp_file(ARCHD * arcn,int fd1,int fd2)80657193Smuller cp_file(ARCHD *arcn, int fd1, int fd2)
80757193Smuller #else
80857193Smuller void
80957193Smuller cp_file(arcn, fd1, fd2)
81057193Smuller ARCHD *arcn;
81157193Smuller int fd1;
81257193Smuller int fd2;
81357193Smuller #endif
81457193Smuller {
81557193Smuller register int cnt;
81657193Smuller register off_t cpcnt = 0L;
81757193Smuller register int res = 0;
81857193Smuller register char *fnm = arcn->name;
81957193Smuller register int no_hole = 0;
82057193Smuller int isem = 1;
82157193Smuller int rem;
82257193Smuller int sz = MINFBSZ;
82357193Smuller struct stat sb;
82457193Smuller
82557193Smuller /*
82657193Smuller * check for holes in the source file. If none, we will use regular
82757193Smuller * write instead of file write.
82857193Smuller */
82957193Smuller if (((off_t)(arcn->sb.st_blocks * BLKMULT)) >= arcn->sb.st_size)
83057193Smuller ++no_hole;
83157193Smuller
83257193Smuller /*
83357193Smuller * pass the blocksize of the file being written to the write routine,
834*66890Sbostic * if the size is zero, use the default MINFBSZ
83557193Smuller */
83657193Smuller if (fstat(fd2, &sb) == 0) {
837*66890Sbostic if (sb.st_blksize > 0)
83857193Smuller sz = sb.st_blksize;
83957193Smuller } else
84057193Smuller syswarn(0,errno,"Unable to obtain block size for file %s",fnm);
84157193Smuller rem = sz;
84257193Smuller
84357193Smuller /*
84457193Smuller * read the source file and copy to destination file until EOF
84557193Smuller */
84657193Smuller for(;;) {
84757193Smuller if ((cnt = read(fd1, buf, blksz)) <= 0)
84857193Smuller break;
84957193Smuller if (no_hole)
85057193Smuller res = write(fd2, buf, cnt);
85157193Smuller else
85257193Smuller res = file_write(fd2, buf, cnt, &rem, &isem, sz, fnm);
85357193Smuller if (res != cnt)
85457193Smuller break;
85557193Smuller cpcnt += cnt;
85657193Smuller }
85757193Smuller
85857193Smuller /*
85957193Smuller * check to make sure the copy is valid.
86057193Smuller */
86157193Smuller if (res < 0)
86257193Smuller syswarn(1, errno, "Failed write during copy of %s to %s",
86357193Smuller arcn->org_name, arcn->name);
86457193Smuller else if (cpcnt != arcn->sb.st_size)
86557193Smuller warn(1, "File %s changed size during copy to %s",
86657193Smuller arcn->org_name, arcn->name);
86757193Smuller else if (fstat(fd1, &sb) < 0)
86857193Smuller syswarn(1, errno, "Failed stat of %s", arcn->org_name);
86957193Smuller else if (arcn->sb.st_mtime != sb.st_mtime)
87057193Smuller warn(1, "File %s was modified during copy to %s",
87157193Smuller arcn->org_name, arcn->name);
87257193Smuller
87357193Smuller /*
87457193Smuller * if the last block has a file hole (all zero), we must make sure this
87557193Smuller * gets updated in the file. We force the last block of zeros to be
87657193Smuller * written. just closing with the file offset moved foward may not put
87757193Smuller * a hole at the end of the file.
87857193Smuller */
87957193Smuller if (!no_hole && isem && (arcn->sb.st_size > 0L))
88057193Smuller file_flush(fd2, fnm, isem);
88157193Smuller return;
88257193Smuller }
88357193Smuller
88457193Smuller /*
88557193Smuller * buf_fill()
88657193Smuller * fill the read buffer with the next record (or what we can get) from
88757193Smuller * the archive volume.
88857193Smuller * Return:
88957193Smuller * Number of bytes of data in the read buffer, -1 for read error, and
89057193Smuller * 0 when finished (user specified termination in ar_next()).
89157193Smuller */
89257193Smuller
89357193Smuller #if __STDC__
89457193Smuller int
buf_fill(void)89557193Smuller buf_fill(void)
89657193Smuller #else
89757193Smuller int
89857193Smuller buf_fill()
89957193Smuller #endif
90057193Smuller {
90157193Smuller register int cnt;
90257193Smuller static int fini = 0;
90357193Smuller
90457193Smuller if (fini)
90557193Smuller return(0);
90657193Smuller
90757193Smuller for(;;) {
90857193Smuller /*
90957193Smuller * try to fill the buffer. on error the next archive volume is
91057193Smuller * opened and we try again.
91157193Smuller */
91257193Smuller if ((cnt = ar_read(buf, blksz)) > 0) {
91357193Smuller bufpt = buf;
91457193Smuller bufend = buf + cnt;
91557193Smuller rdcnt += cnt;
91657193Smuller return(cnt);
91757193Smuller }
91857193Smuller
91957193Smuller /*
92057193Smuller * errors require resync, EOF goes to next archive
92157193Smuller */
92257193Smuller if (cnt < 0)
92357193Smuller break;
92457193Smuller if (ar_next() < 0) {
92557193Smuller fini = 1;
92657193Smuller return(0);
92757193Smuller }
92857193Smuller rdcnt = 0;
92957193Smuller }
93057193Smuller exit_val = 1;
93157193Smuller return(-1);
93257193Smuller }
93357193Smuller
93457193Smuller /*
93557193Smuller * buf_flush()
93657193Smuller * force the write buffer to the archive. We are passed the number of
93757193Smuller * bytes in the buffer at the point of the flush. When we change archives
93857193Smuller * the record size might change. (either larger or smaller).
93957193Smuller * Return:
94057193Smuller * 0 if all is ok, -1 when a write error occurs.
94157193Smuller */
94257193Smuller
94357193Smuller #if __STDC__
94457193Smuller int
buf_flush(register int bufcnt)94557193Smuller buf_flush(register int bufcnt)
94657193Smuller #else
94757193Smuller int
94857193Smuller buf_flush(bufcnt)
94957193Smuller register int bufcnt;
95057193Smuller #endif
95157193Smuller {
95257193Smuller register int cnt;
95357193Smuller register int push = 0;
95457193Smuller register int totcnt = 0;
95557193Smuller
95657193Smuller /*
95757193Smuller * if we have reached the user specified byte count for each archive
95857193Smuller * volume, prompt for the next volume. (The non-standrad -R flag).
95957193Smuller * NOTE: If the wrlimit is smaller than wrcnt, we will always write
96057193Smuller * at least one record. We always round limit UP to next blocksize.
96157193Smuller */
96257193Smuller if ((wrlimit > 0) && (wrcnt > wrlimit)) {
96357193Smuller warn(0, "User specified archive volume byte limit reached.");
96457193Smuller if (ar_next() < 0) {
96557193Smuller wrcnt = 0;
96657193Smuller exit_val = 1;
96757193Smuller return(-1);
96857193Smuller }
96957193Smuller wrcnt = 0;
97057193Smuller
97157193Smuller /*
97257193Smuller * The new archive volume might have changed the size of the
97357193Smuller * write blocksize. if so we figure out if we need to write
97457193Smuller * (one or more times), or if there is now free space left in
97557193Smuller * the buffer (it is no longer full). bufcnt has the number of
97657193Smuller * bytes in the buffer, (the blocksize, at the point we were
97757193Smuller * CALLED). Push has the amount of "extra" data in the buffer
97857193Smuller * if the block size has shrunk from a volume change.
97957193Smuller */
98057193Smuller bufend = buf + blksz;
98157193Smuller if (blksz > bufcnt)
98257193Smuller return(0);
98357193Smuller if (blksz < bufcnt)
98457193Smuller push = bufcnt - blksz;
98557193Smuller }
98657193Smuller
98757193Smuller /*
98857193Smuller * We have enough data to write at least one archive block
98957193Smuller */
99057193Smuller for (;;) {
99157193Smuller /*
99257193Smuller * write a block and check if it all went out ok
99357193Smuller */
99457193Smuller cnt = ar_write(buf, blksz);
99557193Smuller if (cnt == blksz) {
99657193Smuller /*
99757193Smuller * the write went ok
99857193Smuller */
99957193Smuller wrcnt += cnt;
100057193Smuller totcnt += cnt;
100157193Smuller if (push > 0) {
100257193Smuller /* we have extra data to push to the front.
100357193Smuller * check for more than 1 block of push, and if
100457193Smuller * so we loop back to write again
100557193Smuller */
100657193Smuller bcopy(bufend, buf, push);
100757193Smuller bufpt = buf + push;
100857193Smuller if (push >= blksz) {
100957193Smuller push -= blksz;
101057193Smuller continue;
101157193Smuller }
101257193Smuller } else
101357193Smuller bufpt = buf;
101457193Smuller return(totcnt);
101557193Smuller } else if (cnt > 0) {
101657193Smuller /*
101757193Smuller * Oh drat we got a partial write!
101857193Smuller * if format doesnt care about alignment let it go,
101957193Smuller * we warned the user in ar_write().... but this means
102057193Smuller * the last record on this volume violates pax spec....
102157193Smuller */
102257193Smuller totcnt += cnt;
102357193Smuller wrcnt += cnt;
102457193Smuller bufpt = buf + cnt;
102557193Smuller cnt = bufcnt - cnt;
102657193Smuller bcopy(bufpt, buf, cnt);
102757193Smuller bufpt = buf + cnt;
102857193Smuller if (!frmt->blkalgn || ((cnt % frmt->blkalgn) == 0))
102957193Smuller return(totcnt);
103057193Smuller break;
103157193Smuller }
103257193Smuller
103357193Smuller /*
103457193Smuller * All done, go to next archive
103557193Smuller */
103657193Smuller wrcnt = 0;
103757193Smuller if (ar_next() < 0)
103857193Smuller break;
103957193Smuller
104057193Smuller /*
104157193Smuller * The new archive volume might also have changed the block
104257193Smuller * size. if so, figure out if we have too much or too little
104357193Smuller * data for using the new block size
104457193Smuller */
104557193Smuller bufend = buf + blksz;
104657193Smuller if (blksz > bufcnt)
104757193Smuller return(0);
104857193Smuller if (blksz < bufcnt)
104957193Smuller push = bufcnt - blksz;
105057193Smuller }
105157193Smuller
105257193Smuller /*
105357193Smuller * write failed, stop pax. we must not create a bad archive!
105457193Smuller */
105557193Smuller exit_val = 1;
105657193Smuller return(-1);
105757193Smuller }
1058