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