/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1992 Keith Muller.4* Copyright (c) 1992, 19935* The Regents of the University of California. All rights reserved.6*7* This code is derived from software contributed to Berkeley by8* Keith Muller of the University of California, San Diego.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. Neither the name of the University nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include <sys/types.h>36#include <sys/stat.h>37#include <errno.h>38#include <unistd.h>39#include <stdio.h>40#include <string.h>41#include "pax.h"42#include "extern.h"4344/*45* routines which implement archive and file buffering46*/4748#define MINFBSZ 512 /* default block size for hole detect */49#define MAXFLT 10 /* default media read error limit */5051/*52* Need to change bufmem to dynamic allocation when the upper53* limit on blocking size is removed (though that will violate pax spec)54* MAXBLK define and tests will also need to be updated.55*/56static char bufmem[MAXBLK+BLKMULT]; /* i/o buffer + pushback id space */57static char *buf; /* normal start of i/o buffer */58static char *bufend; /* end or last char in i/o buffer */59static char *bufpt; /* read/write point in i/o buffer */60int blksz = MAXBLK; /* block input/output size in bytes */61int wrblksz; /* user spec output size in bytes */62int maxflt = MAXFLT; /* MAX consecutive media errors */63int rdblksz; /* first read blksize (tapes only) */64off_t wrlimit; /* # of bytes written per archive vol */65off_t wrcnt; /* # of bytes written on current vol */66off_t rdcnt; /* # of bytes read on current vol */6768/*69* wr_start()70* set up the buffering system to operate in a write mode71* Return:72* 0 if ok, -1 if the user specified write block size violates pax spec73*/7475int76wr_start(void)77{78buf = &(bufmem[BLKMULT]);79/*80* Check to make sure the write block size meets pax specs. If the user81* does not specify a blocksize, we use the format default blocksize.82* We must be picky on writes, so we do not allow the user to create an83* archive that might be hard to read elsewhere. If all ok, we then84* open the first archive volume85*/86if (!wrblksz)87wrblksz = frmt->bsz;88if (wrblksz > MAXBLK) {89paxwarn(1, "Write block size of %d too large, maximum is: %d",90wrblksz, MAXBLK);91return(-1);92}93if (wrblksz % BLKMULT) {94paxwarn(1, "Write block size of %d is not a %d byte multiple",95wrblksz, BLKMULT);96return(-1);97}98if (wrblksz > MAXBLK_POSIX) {99paxwarn(0, "Write block size of %d larger than POSIX max %d, archive may not be portable",100wrblksz, MAXBLK_POSIX);101return(-1);102}103104/*105* we only allow wrblksz to be used with all archive operations106*/107blksz = rdblksz = wrblksz;108if ((ar_open(arcname) < 0) && (ar_next() < 0))109return(-1);110wrcnt = 0;111bufend = buf + wrblksz;112bufpt = buf;113return(0);114}115116/*117* rd_start()118* set up buffering system to read an archive119* Return:120* 0 if ok, -1 otherwise121*/122123int124rd_start(void)125{126/*127* leave space for the header pushback (see get_arc()). If we are128* going to append and user specified a write block size, check it129* right away130*/131buf = &(bufmem[BLKMULT]);132if ((act == APPND) && wrblksz) {133if (wrblksz > MAXBLK) {134paxwarn(1,"Write block size %d too large, maximum is: %d",135wrblksz, MAXBLK);136return(-1);137}138if (wrblksz % BLKMULT) {139paxwarn(1, "Write block size %d is not a %d byte multiple",140wrblksz, BLKMULT);141return(-1);142}143}144145/*146* open the archive147*/148if ((ar_open(arcname) < 0) && (ar_next() < 0))149return(-1);150bufend = buf + rdblksz;151bufpt = bufend;152rdcnt = 0;153return(0);154}155156/*157* cp_start()158* set up buffer system for copying within the file system159*/160161void162cp_start(void)163{164buf = &(bufmem[BLKMULT]);165rdblksz = blksz = MAXBLK;166}167168/*169* appnd_start()170* Set up the buffering system to append new members to an archive that171* was just read. The last block(s) of an archive may contain a format172* specific trailer. To append a new member, this trailer has to be173* removed from the archive. The first byte of the trailer is replaced by174* the start of the header of the first file added to the archive. The175* format specific end read function tells us how many bytes to move176* backwards in the archive to be positioned BEFORE the trailer. Two177* different positions have to be adjusted, the O.S. file offset (e.g. the178* position of the tape head) and the write point within the data we have179* stored in the read (soon to become write) buffer. We may have to move180* back several records (the number depends on the size of the archive181* record and the size of the format trailer) to read up the record where182* the first byte of the trailer is recorded. Trailers may span (and183* overlap) record boundaries.184* We first calculate which record has the first byte of the trailer. We185* move the OS file offset back to the start of this record and read it186* up. We set the buffer write pointer to be at this byte (the byte where187* the trailer starts). We then move the OS file pointer back to the188* start of this record so a flush of this buffer will replace the record189* in the archive.190* A major problem is rewriting this last record. For archives stored191* on disk files, this is trivial. However, many devices are really picky192* about the conditions under which they will allow a write to occur.193* Often devices restrict the conditions where writes can be made,194* so it may not be feasible to append archives stored on all types of195* devices.196* Return:197* 0 for success, -1 for failure198*/199200int201appnd_start(off_t skcnt)202{203int res;204off_t cnt;205206if (exit_val != 0) {207paxwarn(0, "Cannot append to an archive that may have flaws.");208return(-1);209}210/*211* if the user did not specify a write blocksize, inherit the size used212* in the last archive volume read. (If a is set we still use rdblksz213* until next volume, cannot shift sizes within a single volume).214*/215if (!wrblksz)216wrblksz = blksz = rdblksz;217else218blksz = rdblksz;219220/*221* make sure that this volume allows appends222*/223if (ar_app_ok() < 0)224return(-1);225226/*227* Calculate bytes to move back and move in front of record where we228* need to start writing from. Remember we have to add in any padding229* that might be in the buffer after the trailer in the last block. We230* travel skcnt + padding ROUNDED UP to blksize.231*/232skcnt += bufend - bufpt;233if ((cnt = (skcnt/blksz) * blksz) < skcnt)234cnt += blksz;235if (ar_rev((off_t)cnt) < 0)236goto out;237238/*239* We may have gone too far if there is valid data in the block we are240* now in front of, read up the block and position the pointer after241* the valid data.242*/243if ((cnt -= skcnt) > 0) {244/*245* watch out for stupid tape drives. ar_rev() will set rdblksz246* to be real physical blocksize so we must loop until we get247* the old rdblksz (now in blksz). If ar_rev() fouls up the248* determination of the physical block size, we will fail.249*/250bufpt = buf;251bufend = buf + blksz;252while (bufpt < bufend) {253if ((res = ar_read(bufpt, rdblksz)) <= 0)254goto out;255bufpt += res;256}257if (ar_rev((off_t)(bufpt - buf)) < 0)258goto out;259bufpt = buf + cnt;260bufend = buf + blksz;261} else {262/*263* buffer is empty264*/265bufend = buf + blksz;266bufpt = buf;267}268rdblksz = blksz;269rdcnt -= skcnt;270wrcnt = 0;271272/*273* At this point we are ready to write. If the device requires special274* handling to write at a point were previously recorded data resides,275* that is handled in ar_set_wr(). From now on we operate under normal276* ARCHIVE mode (write) conditions277*/278if (ar_set_wr() < 0)279return(-1);280act = ARCHIVE;281return(0);282283out:284paxwarn(1, "Unable to rewrite archive trailer, cannot append.");285return(-1);286}287288/*289* rd_sync()290* A read error occurred on this archive volume. Resync the buffer and291* try to reset the device (if possible) so we can continue to read. Keep292* trying to do this until we get a valid read, or we reach the limit on293* consecutive read faults (at which point we give up). The user can294* adjust the read error limit through a command line option.295* Returns:296* 0 on success, and -1 on failure297*/298299int300rd_sync(void)301{302int errcnt = 0;303int res;304305/*306* if the user says bail out on first fault, we are out of here...307*/308if (maxflt == 0)309return(-1);310if (act == APPND) {311paxwarn(1, "Unable to append when there are archive read errors.");312return(-1);313}314315/*316* poke at device and try to get past media error317*/318if (ar_rdsync() < 0) {319if (ar_next() < 0)320return(-1);321else322rdcnt = 0;323}324325for (;;) {326if ((res = ar_read(buf, blksz)) > 0) {327/*328* All right! got some data, fill that buffer329*/330bufpt = buf;331bufend = buf + res;332rdcnt += res;333return(0);334}335336/*337* Oh well, yet another failed read...338* if error limit reached, ditch. o.w. poke device to move past339* bad media and try again. if media is badly damaged, we ask340* the poor (and upset user at this point) for the next archive341* volume. remember the goal on reads is to get the most we342* can extract out of the archive.343*/344if ((maxflt > 0) && (++errcnt > maxflt))345paxwarn(0,"Archive read error limit (%d) reached",maxflt);346else if (ar_rdsync() == 0)347continue;348if (ar_next() < 0)349break;350rdcnt = 0;351errcnt = 0;352}353return(-1);354}355356/*357* pback()358* push the data used during the archive id phase back into the I/O359* buffer. This is required as we cannot be sure that the header does NOT360* overlap a block boundary (as in the case we are trying to recover a361* flawed archived). This was not designed to be used for any other362* purpose. (What software engineering, HA!)363* WARNING: do not even THINK of pback greater than BLKMULT, unless the364* pback space is increased.365*/366367void368pback(char *pt, int cnt)369{370bufpt -= cnt;371memcpy(bufpt, pt, cnt);372return;373}374375/*376* rd_skip()377* skip forward in the archive during an archive read. Used to get quickly378* past file data and padding for files the user did NOT select.379* Return:380* 0 if ok, -1 failure, and 1 when EOF on the archive volume was detected.381*/382383int384rd_skip(off_t skcnt)385{386off_t res;387off_t cnt;388off_t skipped = 0;389390/*391* consume what data we have in the buffer. If we have to move forward392* whole records, we call the low level skip function to see if we can393* move within the archive without doing the expensive reads on data we394* do not want.395*/396if (skcnt == 0)397return(0);398res = MIN((bufend - bufpt), skcnt);399bufpt += res;400skcnt -= res;401402/*403* if skcnt is now 0, then no additional i/o is needed404*/405if (skcnt == 0)406return(0);407408/*409* We have to read more, calculate complete and partial record reads410* based on rdblksz. we skip over "cnt" complete records411*/412res = skcnt%rdblksz;413cnt = (skcnt/rdblksz) * rdblksz;414415/*416* if the skip fails, we will have to resync. ar_fow will tell us417* how much it can skip over. We will have to read the rest.418*/419if (ar_fow(cnt, &skipped) < 0)420return(-1);421res += cnt - skipped;422rdcnt += skipped;423424/*425* what is left we have to read (which may be the whole thing if426* ar_fow() told us the device can only read to skip records);427*/428while (res > 0L) {429cnt = bufend - bufpt;430/*431* if the read fails, we will have to resync432*/433if ((cnt <= 0) && ((cnt = buf_fill()) < 0))434return(-1);435if (cnt == 0)436return(1);437cnt = MIN(cnt, res);438bufpt += cnt;439res -= cnt;440}441return(0);442}443444/*445* wr_fin()446* flush out any data (and pad if required) the last block. We always pad447* with zero (even though we do not have to). Padding with 0 makes it a448* lot easier to recover if the archive is damaged. zero padding SHOULD449* BE a requirement....450*/451452void453wr_fin(void)454{455if (bufpt > buf) {456memset(bufpt, 0, bufend - bufpt);457bufpt = bufend;458(void)buf_flush(blksz);459}460}461462/*463* wr_rdbuf()464* fill the write buffer from data passed to it in a buffer (usually used465* by format specific write routines to pass a file header). On failure we466* punt. We do not allow the user to continue to write flawed archives.467* We assume these headers are not very large (the memory copy we use is468* a bit expensive).469* Return:470* 0 if buffer was filled ok, -1 o.w. (buffer flush failure)471*/472473int474wr_rdbuf(char *out, int outcnt)475{476int cnt;477478/*479* while there is data to copy into the write buffer. when the480* write buffer fills, flush it to the archive and continue481*/482while (outcnt > 0) {483cnt = bufend - bufpt;484if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0))485return(-1);486/*487* only move what we have space for488*/489cnt = MIN(cnt, outcnt);490memcpy(bufpt, out, cnt);491bufpt += cnt;492out += cnt;493outcnt -= cnt;494}495return(0);496}497498/*499* rd_wrbuf()500* copy from the read buffer into a supplied buffer a specified number of501* bytes. If the read buffer is empty fill it and continue to copy.502* usually used to obtain a file header for processing by a format503* specific read routine.504* Return505* number of bytes copied to the buffer, 0 indicates EOF on archive volume,506* -1 is a read error507*/508509int510rd_wrbuf(char *in, int cpcnt)511{512int res;513int cnt;514int incnt = cpcnt;515516/*517* loop until we fill the buffer with the requested number of bytes518*/519while (incnt > 0) {520cnt = bufend - bufpt;521if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) {522/*523* read error, return what we got (or the error if524* no data was copied). The caller must know that an525* error occurred and has the best knowledge what to526* do with it527*/528if ((res = cpcnt - incnt) > 0)529return(res);530return(cnt);531}532533/*534* calculate how much data to copy based on what's left and535* state of buffer536*/537cnt = MIN(cnt, incnt);538memcpy(in, bufpt, cnt);539bufpt += cnt;540incnt -= cnt;541in += cnt;542}543return(cpcnt);544}545546/*547* wr_skip()548* skip forward during a write. In other words add padding to the file.549* we add zero filled padding as it makes flawed archives much easier to550* recover from. the caller tells us how many bytes of padding to add551* This routine was not designed to add HUGE amount of padding, just small552* amounts (a few 512 byte blocks at most)553* Return:554* 0 if ok, -1 if there was a buf_flush failure555*/556557int558wr_skip(off_t skcnt)559{560int cnt;561562/*563* loop while there is more padding to add564*/565while (skcnt > 0L) {566cnt = bufend - bufpt;567if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0))568return(-1);569cnt = MIN(cnt, skcnt);570memset(bufpt, 0, cnt);571bufpt += cnt;572skcnt -= cnt;573}574return(0);575}576577/*578* wr_rdfile()579* fill write buffer with the contents of a file. We are passed an open580* file descriptor to the file and the archive structure that describes the581* file we are storing. The variable "left" is modified to contain the582* number of bytes of the file we were NOT able to write to the archive.583* it is important that we always write EXACTLY the number of bytes that584* the format specific write routine told us to. The file can also get585* bigger, so reading to the end of file would create an improper archive,586* we just detect this case and warn the user. We never create a bad587* archive if we can avoid it. Of course trying to archive files that are588* active is asking for trouble. It we fail, we pass back how much we589* could NOT copy and let the caller deal with it.590* Return:591* 0 ok, -1 if archive write failure. a short read of the file returns a592* 0, but "left" is set to be greater than zero.593*/594595int596wr_rdfile(ARCHD *arcn, int ifd, off_t *left)597{598int cnt;599int res = 0;600off_t size = arcn->sb.st_size;601struct stat sb;602603/*604* while there are more bytes to write605*/606while (size > 0L) {607cnt = bufend - bufpt;608if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) {609*left = size;610return(-1);611}612cnt = MIN(cnt, size);613if ((res = read(ifd, bufpt, cnt)) <= 0)614break;615size -= res;616bufpt += res;617}618619/*620* better check the file did not change during this operation621* or the file read failed.622*/623if (res < 0)624syswarn(1, errno, "Read fault on %s", arcn->org_name);625else if (size != 0L)626paxwarn(1, "File changed size during read %s", arcn->org_name);627else if (fstat(ifd, &sb) < 0)628syswarn(1, errno, "Failed stat on %s", arcn->org_name);629else if (arcn->sb.st_mtime != sb.st_mtime)630paxwarn(1, "File %s was modified during copy to archive",631arcn->org_name);632*left = size;633return(0);634}635636/*637* rd_wrfile()638* extract the contents of a file from the archive. If we are unable to639* extract the entire file (due to failure to write the file) we return640* the numbers of bytes we did NOT process. This way the caller knows how641* many bytes to skip past to find the next archive header. If the failure642* was due to an archive read, we will catch that when we try to skip. If643* the format supplies a file data crc value, we calculate the actual crc644* so that it can be compared to the value stored in the header645* NOTE:646* We call a special function to write the file. This function attempts to647* restore file holes (blocks of zeros) into the file. When files are648* sparse this saves space, and is a LOT faster. For non sparse files649* the performance hit is small. As of this writing, no archive supports650* information on where the file holes are.651* Return:652* 0 ok, -1 if archive read failure. if we cannot write the entire file,653* we return a 0 but "left" is set to be the amount unwritten654*/655656int657rd_wrfile(ARCHD *arcn, int ofd, off_t *left)658{659int cnt = 0;660off_t size = arcn->sb.st_size;661int res = 0;662char *fnm = arcn->name;663int isem = 1;664int rem;665int sz = MINFBSZ;666struct stat sb;667u_long crc = 0L;668669/*670* pass the blocksize of the file being written to the write routine,671* if the size is zero, use the default MINFBSZ672*/673if (fstat(ofd, &sb) == 0) {674if (sb.st_blksize > 0)675sz = (int)sb.st_blksize;676} else677syswarn(0,errno,"Unable to obtain block size for file %s",fnm);678rem = sz;679*left = 0L;680681/*682* Copy the archive to the file the number of bytes specified. We have683* to assume that we want to recover file holes as none of the archive684* formats can record the location of file holes.685*/686while (size > 0L) {687cnt = bufend - bufpt;688/*689* if we get a read error, we do not want to skip, as we may690* miss a header, so we do not set left, but if we get a write691* error, we do want to skip over the unprocessed data.692*/693if ((cnt <= 0) && ((cnt = buf_fill()) <= 0))694break;695cnt = MIN(cnt, size);696if ((res = file_write(ofd,bufpt,cnt,&rem,&isem,sz,fnm)) <= 0) {697*left = size;698break;699}700701if (docrc) {702/*703* update the actual crc value704*/705cnt = res;706while (--cnt >= 0)707crc += *bufpt++ & 0xff;708} else709bufpt += res;710size -= res;711}712713/*714* if the last block has a file hole (all zero), we must make sure this715* gets updated in the file. We force the last block of zeros to be716* written. just closing with the file offset moved forward may not put717* a hole at the end of the file.718*/719if (isem && (arcn->sb.st_size > 0L))720file_flush(ofd, fnm, isem);721722/*723* if we failed from archive read, we do not want to skip724*/725if ((size > 0L) && (*left == 0L))726return(-1);727728/*729* some formats record a crc on file data. If so, then we compare the730* calculated crc to the crc stored in the archive731*/732if (docrc && (size == 0L) && (arcn->crc != crc))733paxwarn(1,"Actual crc does not match expected crc %s",arcn->name);734return(0);735}736737/*738* cp_file()739* copy the contents of one file to another. used during -rw phase of pax740* just as in rd_wrfile() we use a special write function to write the741* destination file so we can properly copy files with holes.742*/743744void745cp_file(ARCHD *arcn, int fd1, int fd2)746{747int cnt;748off_t cpcnt = 0L;749int res = 0;750char *fnm = arcn->name;751int no_hole = 0;752int isem = 1;753int rem;754int sz = MINFBSZ;755struct stat sb;756757/*758* check for holes in the source file. If none, we will use regular759* write instead of file write.760*/761if (((off_t)(arcn->sb.st_blocks * BLKMULT)) >= arcn->sb.st_size)762++no_hole;763764/*765* pass the blocksize of the file being written to the write routine,766* if the size is zero, use the default MINFBSZ767*/768if (fstat(fd2, &sb) == 0) {769if (sb.st_blksize > 0)770sz = sb.st_blksize;771} else772syswarn(0,errno,"Unable to obtain block size for file %s",fnm);773rem = sz;774775/*776* read the source file and copy to destination file until EOF777*/778for(;;) {779if ((cnt = read(fd1, buf, blksz)) <= 0)780break;781if (no_hole)782res = write(fd2, buf, cnt);783else784res = file_write(fd2, buf, cnt, &rem, &isem, sz, fnm);785if (res != cnt)786break;787cpcnt += cnt;788}789790/*791* check to make sure the copy is valid.792*/793if (res < 0)794syswarn(1, errno, "Failed write during copy of %s to %s",795arcn->org_name, arcn->name);796else if (cpcnt != arcn->sb.st_size)797paxwarn(1, "File %s changed size during copy to %s",798arcn->org_name, arcn->name);799else if (fstat(fd1, &sb) < 0)800syswarn(1, errno, "Failed stat of %s", arcn->org_name);801else if (arcn->sb.st_mtime != sb.st_mtime)802paxwarn(1, "File %s was modified during copy to %s",803arcn->org_name, arcn->name);804805/*806* if the last block has a file hole (all zero), we must make sure this807* gets updated in the file. We force the last block of zeros to be808* written. just closing with the file offset moved forward may not put809* a hole at the end of the file.810*/811if (!no_hole && isem && (arcn->sb.st_size > 0L))812file_flush(fd2, fnm, isem);813return;814}815816/*817* buf_fill()818* fill the read buffer with the next record (or what we can get) from819* the archive volume.820* Return:821* Number of bytes of data in the read buffer, -1 for read error, and822* 0 when finished (user specified termination in ar_next()).823*/824825int826buf_fill(void)827{828int cnt;829static int fini = 0;830831if (fini)832return(0);833834for(;;) {835/*836* try to fill the buffer. on error the next archive volume is837* opened and we try again.838*/839if ((cnt = ar_read(buf, blksz)) > 0) {840bufpt = buf;841bufend = buf + cnt;842rdcnt += cnt;843return(cnt);844}845846/*847* errors require resync, EOF goes to next archive848* but in case we have not determined yet the format,849* this means that we have a very short file, so we850* are done again.851*/852if (cnt < 0)853break;854if (frmt == NULL || ar_next() < 0) {855fini = 1;856return(0);857}858rdcnt = 0;859}860exit_val = 1;861return(-1);862}863864/*865* buf_flush()866* force the write buffer to the archive. We are passed the number of867* bytes in the buffer at the point of the flush. When we change archives868* the record size might change. (either larger or smaller).869* Return:870* 0 if all is ok, -1 when a write error occurs.871*/872873int874buf_flush(int bufcnt)875{876int cnt;877int push = 0;878int totcnt = 0;879880/*881* if we have reached the user specified byte count for each archive882* volume, prompt for the next volume. (The non-standard -R flag).883* NOTE: If the wrlimit is smaller than wrcnt, we will always write884* at least one record. We always round limit UP to next blocksize.885*/886if ((wrlimit > 0) && (wrcnt > wrlimit)) {887paxwarn(0, "User specified archive volume byte limit reached.");888if (ar_next() < 0) {889wrcnt = 0;890exit_val = 1;891return(-1);892}893wrcnt = 0;894895/*896* The new archive volume might have changed the size of the897* write blocksize. if so we figure out if we need to write898* (one or more times), or if there is now free space left in899* the buffer (it is no longer full). bufcnt has the number of900* bytes in the buffer, (the blocksize, at the point we were901* CALLED). Push has the amount of "extra" data in the buffer902* if the block size has shrunk from a volume change.903*/904bufend = buf + blksz;905if (blksz > bufcnt)906return(0);907if (blksz < bufcnt)908push = bufcnt - blksz;909}910911/*912* We have enough data to write at least one archive block913*/914for (;;) {915/*916* write a block and check if it all went out ok917*/918cnt = ar_write(buf, blksz);919if (cnt == blksz) {920/*921* the write went ok922*/923wrcnt += cnt;924totcnt += cnt;925if (push > 0) {926/* we have extra data to push to the front.927* check for more than 1 block of push, and if928* so we loop back to write again929*/930memcpy(buf, bufend, push);931bufpt = buf + push;932if (push >= blksz) {933push -= blksz;934continue;935}936} else937bufpt = buf;938return(totcnt);939} else if (cnt > 0) {940/*941* Oh drat we got a partial write!942* if format doesn't care about alignment let it go,943* we warned the user in ar_write().... but this means944* the last record on this volume violates pax spec....945*/946totcnt += cnt;947wrcnt += cnt;948bufpt = buf + cnt;949cnt = bufcnt - cnt;950memcpy(buf, bufpt, cnt);951bufpt = buf + cnt;952if (!frmt->blkalgn || ((cnt % frmt->blkalgn) == 0))953return(totcnt);954break;955}956957/*958* All done, go to next archive959*/960wrcnt = 0;961if (ar_next() < 0)962break;963964/*965* The new archive volume might also have changed the block966* size. if so, figure out if we have too much or too little967* data for using the new block size968*/969bufend = buf + blksz;970if (blksz > bufcnt)971return(0);972if (blksz < bufcnt)973push = bufcnt - blksz;974}975976/*977* write failed, stop pax. we must not create a bad archive!978*/979exit_val = 1;980return(-1);981}982983984