157193Smuller /*- 257193Smuller * Copyright (c) 1992 Keith Muller. 357193Smuller * Copyright (c) 1992 The Regents of the University of California. 457193Smuller * 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*57794Smuller static char sccsid[] = "@(#)ar_io.c 1.5 (Berkeley) 02/02/93"; 1457193Smuller #endif /* not lint */ 1557193Smuller 1657193Smuller #include <sys/types.h> 1757193Smuller #include <sys/time.h> 1857193Smuller #include <sys/stat.h> 1957193Smuller #include <sys/ioctl.h> 2057193Smuller #include <sys/mtio.h> 2157193Smuller #include <sys/param.h> 2257193Smuller #include <signal.h> 2357193Smuller #include <string.h> 2457193Smuller #include <fcntl.h> 2557193Smuller #include <unistd.h> 2657193Smuller #include <stdio.h> 2757193Smuller #include <ctype.h> 2857193Smuller #include <errno.h> 2957193Smuller #include <stdlib.h> 3057193Smuller #include "pax.h" 3157193Smuller #include "extern.h" 3257193Smuller 3357193Smuller /* 3457535Smuller * Routines which deal directly with the archive I/O device/file. 3557193Smuller */ 3657193Smuller 3757193Smuller #define DMOD 0666 /* default mode of created archives */ 3857193Smuller #define EXT_MODE O_RDONLY /* open mode for list/extract */ 3957193Smuller #define AR_MODE (O_WRONLY | O_CREAT | O_TRUNC) /* mode for archive */ 4057193Smuller #define APP_MODE O_RDWR /* mode for append */ 4157193Smuller #define STDO "<STDOUT>" /* psuedo name for stdout */ 4257193Smuller #define STDN "<STDIN>" /* psuedo name for stdin */ 4357193Smuller static int arfd = -1; /* archive file descriptor */ 4457584Smuller static int artyp = ISREG; /* archive type: file/FIFO/tape */ 4557193Smuller static int arvol = 1; /* archive volume number */ 4657193Smuller static int lstrval = -1; /* return value from last i/o */ 4757193Smuller static int io_ok; /* i/o worked on volume after resync */ 4857535Smuller static int did_io; /* did i/o ever occur on volume? */ 4957193Smuller static int done; /* set via tty termination */ 5057193Smuller static struct stat arsb; /* stat of archive device at open */ 5157193Smuller static int invld_rec; /* tape has out of spec record size */ 5257578Smuller static int wr_trail = 1; /* trailer was rewritten in append */ 5357584Smuller static int can_unlnk = 0; /* do we unlink null archives? */ 5457193Smuller char *arcname; /* printable name of archive */ 5557193Smuller 5657193Smuller static int get_phys __P((void)); 5757193Smuller extern sigset_t s_mask; 5857193Smuller 5957193Smuller /* 6057193Smuller * ar_open() 6157193Smuller * Opens the next archive volume. Determines the type of the device and 6257193Smuller * sets up block sizes as required by the archive device and the format. 6357193Smuller * Note: we may be called with name == NULL on the first open only. 6457193Smuller * Return: 6557193Smuller * -1 on failure, 0 otherwise 6657193Smuller */ 6757193Smuller 6857193Smuller #if __STDC__ 6957193Smuller int 7057193Smuller ar_open(char *name) 7157193Smuller #else 7257193Smuller int 7357193Smuller ar_open(name) 7457193Smuller char *name; 7557193Smuller #endif 7657193Smuller { 7757193Smuller struct mtget mb; 7857193Smuller 7957193Smuller if (arfd != -1) 8057193Smuller (void)close(arfd); 8157193Smuller arfd = -1; 82*57794Smuller can_unlnk = did_io = io_ok = invld_rec = 0; 8357584Smuller artyp = ISREG; 8457193Smuller flcnt = 0; 8557193Smuller 8657193Smuller /* 8757193Smuller * open based on overall operation mode 8857193Smuller */ 8957193Smuller switch (act) { 9057193Smuller case LIST: 9157193Smuller case EXTRACT: 9257193Smuller if (name == NULL) { 9357193Smuller arfd = STDIN_FILENO; 9457193Smuller arcname = STDN; 9557193Smuller } else if ((arfd = open(name, EXT_MODE, DMOD)) < 0) 9657535Smuller syswarn(0, errno, "Failed open to read on %s", name); 9757193Smuller break; 9857193Smuller case ARCHIVE: 9957193Smuller if (name == NULL) { 10057193Smuller arfd = STDOUT_FILENO; 10157193Smuller arcname = STDO; 10257193Smuller } else if ((arfd = open(name, AR_MODE, DMOD)) < 0) 10357535Smuller syswarn(0, errno, "Failed open to write on %s", name); 10457584Smuller else 10557584Smuller can_unlnk = 1; 10657193Smuller break; 10757193Smuller case APPND: 10857193Smuller if (name == NULL) { 10957193Smuller arfd = STDOUT_FILENO; 11057193Smuller arcname = STDO; 11157193Smuller } else if ((arfd = open(name, APP_MODE, DMOD)) < 0) 11257535Smuller syswarn(0, errno, "Failed open to read/write on %s", 11357193Smuller name); 11457193Smuller break; 11557193Smuller case COPY: 11657193Smuller /* 11757193Smuller * arfd not used in COPY mode 11857193Smuller */ 11957193Smuller arcname = "<NONE>"; 12057193Smuller lstrval = 1; 12157193Smuller return(0); 12257193Smuller } 12357193Smuller if (arfd < 0) 12457193Smuller return(-1); 12557193Smuller 12657193Smuller /* 12757193Smuller * set up is based on device type 12857193Smuller */ 12957193Smuller if (fstat(arfd, &arsb) < 0) { 13057535Smuller syswarn(0, errno, "Failed stat on %s", arcname); 13157584Smuller (void)close(arfd); 13257584Smuller arfd = -1; 13357584Smuller can_unlnk = 0; 13457193Smuller return(-1); 13557193Smuller } 13657193Smuller if (S_ISDIR(arsb.st_mode)) { 13757535Smuller warn(0, "Cannot write an archive on top of a directory %s", 13857193Smuller arcname); 13957584Smuller (void)close(arfd); 14057584Smuller arfd = -1; 14157584Smuller can_unlnk = 0; 14257193Smuller return(-1); 14357193Smuller } 14457584Smuller 14557193Smuller if (S_ISCHR(arsb.st_mode)) 14657193Smuller artyp = ioctl(arfd, MTIOCGET, &mb) ? ISCHR : ISTAPE; 14757193Smuller else if (S_ISBLK(arsb.st_mode)) 14857193Smuller artyp = ISBLK; 14957193Smuller else if ((lseek(arfd, (off_t)0L, SEEK_CUR) == -1) && (errno == ESPIPE)) 15057193Smuller artyp = ISPIPE; 15157193Smuller else 15257193Smuller artyp = ISREG; 15357193Smuller 15457193Smuller /* 15557584Smuller * make sure we beyond any doubt that we only can unlink regular files 15657584Smuller * we created 15757584Smuller */ 15857584Smuller if (artyp != ISREG) 15957584Smuller can_unlnk = 0; 16057584Smuller /* 16157535Smuller * if we are writing, we are done 16257193Smuller */ 16357193Smuller if (act == ARCHIVE) { 16457193Smuller blksz = rdblksz = wrblksz; 16557193Smuller lstrval = 1; 16657193Smuller return(0); 16757193Smuller } 16857193Smuller 16957193Smuller /* 17057193Smuller * set default blksz on read. APPNDs writes rdblksz on the last volume 17157193Smuller * On all new archive volumes, we shift to wrblksz (if the user 17257193Smuller * specified one, otherwize we will continue to use rdblksz). We 17357193Smuller * must to set blocksize based on what kind of device the archive is 17457193Smuller * stored. 17557193Smuller */ 17657193Smuller switch(artyp) { 17757193Smuller case ISTAPE: 17857193Smuller /* 17957193Smuller * Tape drives come in at least two flavors. Those that support 18057193Smuller * variable sized records and those that have fixed sized 18157193Smuller * records. They must be treated differently. For tape drives 18257193Smuller * that support variable sized records, we must make large 18357193Smuller * reads to make sure we get the entire record, otherwise we 18457193Smuller * will just get the first part of the record (up to size we 18557193Smuller * asked). Tapes with fixed sized records may or may not return 18657193Smuller * multiple records in a single read. We really do not care 18757193Smuller * what the physical record size is UNLESS we are going to 18857193Smuller * append. (We will need the physical block size to rewrite 18957193Smuller * the trailer). Only when we are appending do we go to the 19057535Smuller * effort to figure out the true PHYSICAL record size. 19157193Smuller */ 19257193Smuller blksz = rdblksz = MAXBLK; 19357193Smuller break; 19457193Smuller case ISPIPE: 19557193Smuller case ISBLK: 19657193Smuller case ISCHR: 19757193Smuller /* 19857193Smuller * Blocksize is not a major issue with these devices (but must 19957193Smuller * be kept a multiple of 512). If the user specified a write 20057193Smuller * block size, we use that to read. Under append, we must 20157193Smuller * always keep blksz == rdblksz. Otherwise we go ahead and use 20257193Smuller * the device optimal blocksize as (and if) returned by stat 20357193Smuller * and if it is within pax specs. 20457193Smuller */ 20557193Smuller if ((act == APPND) && wrblksz) { 20657193Smuller blksz = rdblksz = wrblksz; 20757193Smuller break; 20857193Smuller } 20957193Smuller 21057193Smuller if ((arsb.st_blksize > 0) && (arsb.st_blksize < MAXBLK) && 21157193Smuller ((arsb.st_blksize % BLKMULT) == 0)) 21257193Smuller rdblksz = arsb.st_blksize; 21357193Smuller else 21457193Smuller rdblksz = DEVBLK; 21557193Smuller /* 21657193Smuller * For performance go for large reads when we can without harm 21757193Smuller */ 21857193Smuller if ((act == APPND) || (artyp == ISCHR)) 21957193Smuller blksz = rdblksz; 22057193Smuller else 22157193Smuller blksz = MAXBLK; 22257193Smuller break; 22357193Smuller case ISREG: 22457193Smuller /* 22557193Smuller * if the user specified wrblksz works, use it. Under appends 22657193Smuller * we must always keep blksz == rdblksz 22757193Smuller */ 22857193Smuller if ((act == APPND) && wrblksz && ((arsb.st_size%wrblksz)==0)){ 22957193Smuller blksz = rdblksz = wrblksz; 23057193Smuller break; 23157193Smuller } 23257193Smuller /* 23357193Smuller * See if we can find the blocking factor from the file size 23457193Smuller */ 23557193Smuller for (rdblksz = MAXBLK; rdblksz > 0; rdblksz -= BLKMULT) 23657193Smuller if ((arsb.st_size % rdblksz) == 0) 23757193Smuller break; 23857193Smuller /* 23957193Smuller * When we cannont find a match, we may have a flawed archive. 24057193Smuller */ 24157193Smuller if (rdblksz <= 0) 24257193Smuller rdblksz = FILEBLK; 24357193Smuller /* 24457193Smuller * for performance go for large reads when we can 24557193Smuller */ 24657193Smuller if (act == APPND) 24757193Smuller blksz = rdblksz; 24857193Smuller else 24957193Smuller blksz = MAXBLK; 25057193Smuller break; 25157193Smuller default: 25257193Smuller /* 25357193Smuller * should never happen, worse case, slow... 25457193Smuller */ 25557193Smuller blksz = rdblksz = BLKMULT; 25657193Smuller break; 25757193Smuller } 25857193Smuller lstrval = 1; 25957193Smuller return(0); 26057193Smuller } 26157193Smuller 26257193Smuller /* 26357193Smuller * ar_close() 26457193Smuller * closes archive device, increments volume number, and prints i/o summary 26557193Smuller */ 26657193Smuller #if __STDC__ 26757193Smuller void 26857193Smuller ar_close(void) 26957193Smuller #else 27057193Smuller void 27157193Smuller ar_close() 27257193Smuller #endif 27357193Smuller { 27457193Smuller FILE *outf; 27557193Smuller 27657578Smuller if (arfd < 0) { 27757578Smuller did_io = io_ok = flcnt = 0; 27857578Smuller return; 27957578Smuller } 28057578Smuller 28157535Smuller if (act == LIST) 28257535Smuller outf = stdout; 28357535Smuller else 28457535Smuller outf = stderr; 28557535Smuller 28657535Smuller /* 28757535Smuller * Close archive file. This may take a LONG while on tapes (we may be 28857535Smuller * forced to wait for the rewind to complete) so tell the user what is 28957535Smuller * going on (this avoids the user hitting control-c thinking pax is 29057535Smuller * broken). 29157535Smuller */ 29257578Smuller if (vflag && (artyp == ISTAPE)) { 29357578Smuller if (vfpart) 29457535Smuller (void)putc('\n', outf); 29557535Smuller (void)fputs("pax: Waiting for tape drive close to complete...", 29657535Smuller outf); 29757535Smuller (void)fflush(outf); 29857535Smuller } 29957535Smuller 30057584Smuller /* 30157584Smuller * if nothing was written to the archive (and we created it), we remove 30257584Smuller * it 30357584Smuller */ 30457584Smuller if (can_unlnk && (fstat(arfd, &arsb) == 0) && (S_ISREG(arsb.st_mode)) && 30557584Smuller (arsb.st_size == 0)) { 30657584Smuller (void)unlink(arcname); 30757584Smuller can_unlnk = 0; 30857584Smuller } 30957584Smuller 31057193Smuller (void)close(arfd); 31157535Smuller 31257578Smuller if (vflag && (artyp == ISTAPE)) { 31357535Smuller (void)fputs("done.\n", outf); 31457578Smuller vfpart = 0; 31557535Smuller (void)fflush(outf); 31657535Smuller } 31757193Smuller arfd = -1; 31857535Smuller 31957193Smuller if (!io_ok && !did_io) { 32057193Smuller flcnt = 0; 32157193Smuller return; 32257193Smuller } 32357193Smuller did_io = io_ok = 0; 32457193Smuller 32557193Smuller /* 32657193Smuller * The volume number is only increased when the last device has data 32757535Smuller * and we have already determined the archive format. 32857193Smuller */ 32957535Smuller if (frmt != NULL) 33057535Smuller ++arvol; 33157535Smuller 33257193Smuller if (!vflag) { 33357193Smuller flcnt = 0; 33457193Smuller return; 33557193Smuller } 33657193Smuller 33757193Smuller /* 33857193Smuller * Print out a summary of I/O for this archive volume. 33957193Smuller */ 34057193Smuller if (vfpart) { 34157193Smuller (void)putc('\n', outf); 34257193Smuller vfpart = 0; 34357193Smuller } 34457193Smuller 34557535Smuller /* 34657535Smuller * If we have not determined the format yet, we just say how many bytes 34757535Smuller * we have skipped over looking for a header to id. there is no way we 34857535Smuller * could have written anything yet. 34957535Smuller */ 35057535Smuller if (frmt == NULL) { 35157535Smuller # ifdef NET2_STAT 35257535Smuller (void)fprintf(outf, "pax: unknown format, %lu bytes skipped.\n", 35357535Smuller # else 35457535Smuller (void)fprintf(outf, "pax: unknown format, %qu bytes skipped.\n", 35557535Smuller # endif 35657535Smuller rdcnt); 35757535Smuller (void)fflush(outf); 35857535Smuller flcnt = 0; 35957535Smuller return; 36057535Smuller } 36157535Smuller 36257193Smuller (void)fprintf(outf, 36357193Smuller # ifdef NET2_STAT 36457535Smuller "pax: %s vol %d, %lu files, %lu bytes read, %lu bytes written.\n", 36557193Smuller # else 36657535Smuller "pax: %s vol %d, %lu files, %qu bytes read, %qu bytes written.\n", 36757193Smuller # endif 36857193Smuller frmt->name, arvol-1, flcnt, rdcnt, wrcnt); 36957193Smuller (void)fflush(outf); 37057193Smuller flcnt = 0; 37157193Smuller } 37257193Smuller 37357193Smuller /* 37457193Smuller * ar_set_wr() 37557578Smuller * Set up device right before switching from read to write in an append. 37657578Smuller * device dependent code (if required) to do this should be added here. 37757578Smuller * For all archive devices we are already positioned at the place we want 37857578Smuller * to start writing when this routine is called. 37957193Smuller * Return: 38057193Smuller * 0 if all ready to write, -1 otherwise 38157193Smuller */ 38257193Smuller 38357193Smuller #if __STDC__ 38457193Smuller int 38557193Smuller ar_set_wr(void) 38657193Smuller #else 38757193Smuller int 38857193Smuller ar_set_wr() 38957193Smuller #endif 39057193Smuller { 39157193Smuller off_t cpos; 39257193Smuller 39357578Smuller /* 39457578Smuller * we must make sure the trailer is rewritten on append, ar_next() 39557578Smuller * will stop us if the archive containing the trailer was not written 39657578Smuller */ 39757578Smuller wr_trail = 0; 39857578Smuller 39957193Smuller /* 40057193Smuller * Add any device dependent code as required here 40157193Smuller */ 40257193Smuller if (artyp != ISREG) 40357193Smuller return(0); 40457193Smuller /* 40557535Smuller * Ok we have an archive in a regular file. If we were rewriting a 40657535Smuller * file, we must get rid of all the stuff after the current offset 40757535Smuller * (it was not written by pax). 40857193Smuller */ 40957193Smuller if (((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) || 41057578Smuller (ftruncate(arfd, cpos) < 0)) { 41157578Smuller syswarn(1, errno, "Unable to truncate archive file"); 41257193Smuller return(-1); 41357578Smuller } 41457193Smuller return(0); 41557193Smuller } 41657193Smuller 41757193Smuller /* 41857193Smuller * ar_app_ok() 41957193Smuller * check if the last volume in the archive allows appends. We cannot check 42057193Smuller * this until we are ready to write since there is no spec that says all 42157193Smuller * volumes in a single archive have to be of the same type... 42257193Smuller * Return: 42357193Smuller * 0 if we can append, -1 otherwise. 42457193Smuller */ 42557193Smuller 42657193Smuller #if __STDC__ 42757193Smuller int 42857193Smuller ar_app_ok(void) 42957193Smuller #else 43057193Smuller int 43157193Smuller ar_app_ok() 43257193Smuller #endif 43357193Smuller { 43457193Smuller if (artyp == ISPIPE) { 43557193Smuller warn(1, "Cannot append to an archive obtained from a pipe."); 43657193Smuller return(-1); 43757193Smuller } 43857193Smuller 43957193Smuller if (!invld_rec) 44057193Smuller return(0); 44157193Smuller warn(1,"Cannot append, device record size %d does not support pax spec", 44257193Smuller rdblksz); 44357193Smuller return(-1); 44457193Smuller } 44557193Smuller 44657193Smuller /* 44757193Smuller * ar_read() 44857193Smuller * read up to a specified number of bytes from the archive into the 44957193Smuller * supplied buffer. When dealing with tapes we may not always be able to 45057193Smuller * read what we want. 45157193Smuller * Return: 45257193Smuller * Number of bytes in buffer. 0 for end of file, -1 for a read error. 45357193Smuller */ 45457193Smuller 45557193Smuller #if __STDC__ 45657193Smuller int 45757193Smuller ar_read(register char *buf, register int cnt) 45857193Smuller #else 45957193Smuller int 46057193Smuller ar_read(buf, cnt) 46157193Smuller register char *buf; 46257193Smuller register int cnt; 46357193Smuller #endif 46457193Smuller { 46557193Smuller register int res = 0; 46657193Smuller 46757193Smuller /* 46857193Smuller * if last i/o was in error, no more reads until reset or new volume 46957193Smuller */ 47057193Smuller if (lstrval <= 0) 47157193Smuller return(lstrval); 47257193Smuller 47357193Smuller /* 47457193Smuller * how we read must be based on device type 47557193Smuller */ 47657193Smuller switch (artyp) { 47757193Smuller case ISTAPE: 47857193Smuller if ((res = read(arfd, buf, cnt)) > 0) { 47957193Smuller /* 48057193Smuller * CAUTION: tape systems may not always return the same 48157193Smuller * sized records so we leave blksz == MAXBLK. The 48257193Smuller * physical record size that a tape drive supports is 48357193Smuller * very hard to determine in a uniform and portable 48457193Smuller * manner. 48557193Smuller */ 48657193Smuller io_ok = 1; 48757193Smuller if (res != rdblksz) { 48857193Smuller /* 48957193Smuller * Record size changed. If this is happens on 49057535Smuller * any record after the first, we probably have 49157535Smuller * a tape drive which has a fixed record size 49257535Smuller * we are getting multiple records in a single 49357535Smuller * read). Watch out for record blocking that 49457535Smuller * violates pax spec (must be a multiple of 49557535Smuller * BLKMULT). 49657193Smuller */ 49757193Smuller rdblksz = res; 49857193Smuller if (rdblksz % BLKMULT) 49957193Smuller invld_rec = 1; 50057193Smuller } 50157193Smuller return(res); 50257193Smuller } 50357193Smuller break; 50457193Smuller case ISREG: 50557193Smuller case ISBLK: 50657193Smuller case ISCHR: 50757193Smuller case ISPIPE: 50857193Smuller default: 50957193Smuller /* 51057193Smuller * Files are so easy to deal with. These other things cannot 51157193Smuller * be trusted at all. So when we are dealing with character 51257193Smuller * devices and pipes we just take what they have ready for us 51357193Smuller * and return. Trying to do anything else with them runs the 51457193Smuller * risk of failure. 51557193Smuller */ 51657193Smuller if ((res = read(arfd, buf, cnt)) > 0) { 51757193Smuller io_ok = 1; 51857193Smuller return(res); 51957193Smuller } 52057193Smuller break; 52157193Smuller } 52257193Smuller 52357193Smuller /* 52457193Smuller * We are in trouble at this point, something is broken... 52557193Smuller */ 52657193Smuller lstrval = res; 52757193Smuller if (res < 0) 52857193Smuller syswarn(1, errno, "Failed read on archive volume %d", arvol); 52957193Smuller else 53057193Smuller warn(0, "End of archive volume %d reached", arvol); 53157193Smuller return(res); 53257193Smuller } 53357193Smuller 53457193Smuller /* 53557193Smuller * ar_write() 53657193Smuller * Write a specified number of bytes in supplied buffer to the archive 53757193Smuller * device so it appears as a single "block". Deals with errors and tries 53857193Smuller * to recover when faced with short writes. 53957193Smuller * Return: 54057193Smuller * Number of bytes written. 0 indicates end of volume reached and with no 54157193Smuller * flaws (as best that can be detected). A -1 indicates an unrecoverable 54257193Smuller * error in the archive occured. 54357193Smuller */ 54457193Smuller 54557193Smuller #if __STDC__ 54657193Smuller int 54757193Smuller ar_write(register char *buf, register int bsz) 54857193Smuller #else 54957193Smuller int 55057193Smuller ar_write(buf, bsz) 55157193Smuller register char *buf; 55257193Smuller register int bsz; 55357193Smuller #endif 55457193Smuller { 55557193Smuller register int res; 55657193Smuller off_t cpos; 55757193Smuller 55857193Smuller /* 55957193Smuller * do not allow pax to create a "bad" archive. Once a write fails on 56057193Smuller * an archive volume prevent further writes to it. 56157193Smuller */ 56257193Smuller if (lstrval <= 0) 56357193Smuller return(lstrval); 56457193Smuller 56557193Smuller if ((res = write(arfd, buf, bsz)) == bsz) { 56657578Smuller wr_trail = 1; 56757193Smuller io_ok = 1; 56857193Smuller return(bsz); 56957193Smuller } 57057193Smuller /* 57157193Smuller * write broke, see what we can do with it. We try to send any partial 57257535Smuller * writes that may violate pax spec to the next archive volume. 57357193Smuller */ 57457193Smuller if (res < 0) 57557193Smuller lstrval = res; 57657193Smuller else 57757193Smuller lstrval = 0; 57857193Smuller 57957193Smuller switch (artyp) { 58057193Smuller case ISREG: 58157193Smuller if ((res > 0) && (res % BLKMULT)) { 58257193Smuller /* 58357193Smuller * try to fix up partial writes which are not BLKMULT 58457193Smuller * in size by forcing the runt record to next archive 58557193Smuller * volume 58657193Smuller */ 58757193Smuller if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) 58857193Smuller break; 58957193Smuller cpos -= (off_t)res; 59057193Smuller if (ftruncate(arfd, cpos) < 0) 59157193Smuller break; 59257193Smuller res = lstrval = 0; 59357193Smuller break; 59457193Smuller } 59557193Smuller if (res >= 0) 59657193Smuller break; 59757193Smuller /* 59857193Smuller * if file is out of space, handle it like a return of 0 59957193Smuller */ 60057193Smuller if ((errno == ENOSPC) || (errno == EFBIG) || (errno == EDQUOT)) 60157193Smuller res = lstrval = 0; 60257193Smuller break; 60357193Smuller case ISTAPE: 60457193Smuller case ISCHR: 60557193Smuller case ISBLK: 60657193Smuller if (res >= 0) 60757193Smuller break; 60857193Smuller if (errno == EACCES) { 60957193Smuller warn(0, "Write failed, archive is write protected."); 61057193Smuller res = lstrval = 0; 61157193Smuller return(0); 61257193Smuller } 61357193Smuller /* 61457193Smuller * see if we reached the end of media, if so force a change to 61557193Smuller * the next volume 61657193Smuller */ 61757193Smuller if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO)) 61857193Smuller res = lstrval = 0; 61957193Smuller break; 62057193Smuller case ISPIPE: 62157193Smuller default: 62257193Smuller /* 62357193Smuller * we cannot fix errors to these devices 62457193Smuller */ 62557193Smuller break; 62657193Smuller } 62757193Smuller 62857193Smuller /* 62957193Smuller * Better tell the user the bad news... 63057535Smuller * if this is a block aligned archive format, we may have a bad archive 63157535Smuller * if the format wants the header to start at a BLKMULT boundry. While 63257193Smuller * we can deal with the mis-aligned data, it violates spec and other 63357193Smuller * archive readers will likely fail. if the format is not block 63457535Smuller * aligned, the user may be lucky (and the archive is ok). 63557193Smuller */ 63657578Smuller if (res >= 0) { 63757578Smuller if (res > 0) 63857578Smuller wr_trail = 1; 63957193Smuller io_ok = 1; 64057578Smuller } 64157578Smuller 64257578Smuller /* 64357578Smuller * If we were trying to rewrite the trailer and it didn't work, we 64457578Smuller * must quit right away. 64557578Smuller */ 64657578Smuller if (!wr_trail && (res <= 0)) { 64757578Smuller warn(1,"Unable to append, trailer re-write failed. Quitting."); 64857578Smuller return(res); 64957578Smuller } 65057578Smuller 65157578Smuller if (res == 0) 65257193Smuller warn(0, "End of archive volume %d reached", arvol); 65357193Smuller else if (res < 0) 65457193Smuller syswarn(1, errno, "Failed write to archive volume: %d", arvol); 65557193Smuller else if (!frmt->blkalgn || ((res % frmt->blkalgn) == 0)) 65657193Smuller warn(0,"WARNING: partial archive write. Archive MAY BE FLAWED"); 65757193Smuller else 65857193Smuller warn(1,"WARNING: partial archive write. Archive IS FLAWED"); 65957193Smuller return(res); 66057193Smuller } 66157193Smuller 66257193Smuller /* 66357193Smuller * ar_rdsync() 66457193Smuller * Try to move past a bad spot on a flawed archive as needed to continue 66557193Smuller * I/O. Clears error flags to allow I/O to continue. 66657193Smuller * Return: 66757193Smuller * 0 when ok to try i/o again, -1 otherwise. 66857193Smuller */ 66957193Smuller 67057193Smuller #if __STDC__ 67157193Smuller int 67257193Smuller ar_rdsync(void) 67357193Smuller #else 67457193Smuller int 67557193Smuller ar_rdsync() 67657193Smuller #endif 67757193Smuller { 67857193Smuller long fsbz; 67957193Smuller off_t cpos; 68057193Smuller off_t mpos; 68157193Smuller struct mtop mb; 68257193Smuller 68357193Smuller /* 68457193Smuller * Fail resync attempts at user request (done) or this is going to be 68557193Smuller * an update/append to a existing archive. if last i/o hit media end, 68657193Smuller * we need to go to the next volume not try a resync 68757193Smuller */ 68857193Smuller if ((done > 0) || (lstrval == 0)) 68957193Smuller return(-1); 69057193Smuller 69157193Smuller if ((act == APPND) || (act == ARCHIVE)) { 69257193Smuller warn(1, "Cannot allow updates to an archive with flaws."); 69357193Smuller return(-1); 69457193Smuller } 69557193Smuller if (io_ok) 69657193Smuller did_io = 1; 69757193Smuller 69857193Smuller switch(artyp) { 69957193Smuller case ISTAPE: 70057193Smuller /* 70157193Smuller * if the last i/o was a successful data transfer, we assume 70257193Smuller * the fault is just a bad record on the tape that we are now 70357193Smuller * past. If we did not get any data since the last resync try 70457193Smuller * to move the tape foward one PHYSICAL record past any 70557193Smuller * damaged tape section. Some tape drives are stubborn and need 70657193Smuller * to be pushed. 70757193Smuller */ 70857193Smuller if (io_ok) { 70957193Smuller io_ok = 0; 71057193Smuller lstrval = 1; 71157193Smuller break; 71257193Smuller } 71357193Smuller mb.mt_op = MTFSR; 71457193Smuller mb.mt_count = 1; 71557193Smuller if (ioctl(arfd, MTIOCTOP, &mb) < 0) 71657193Smuller break; 71757193Smuller lstrval = 1; 71857193Smuller break; 71957193Smuller case ISREG: 72057193Smuller case ISCHR: 72157193Smuller case ISBLK: 72257193Smuller /* 72357193Smuller * try to step over the bad part of the device. 72457193Smuller */ 72557193Smuller io_ok = 0; 72657193Smuller if (((fsbz = arsb.st_blksize) <= 0) || (artyp != ISREG)) 72757193Smuller fsbz = BLKMULT; 72857193Smuller if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) 72957193Smuller break; 73057193Smuller mpos = fsbz - (cpos % (off_t)fsbz); 73157193Smuller if (lseek(arfd, mpos, SEEK_CUR) < 0) 73257193Smuller break; 73357193Smuller lstrval = 1; 73457193Smuller break; 73557193Smuller case ISPIPE: 73657193Smuller default: 73757193Smuller /* 73857193Smuller * cannot recover on these archive device types 73957193Smuller */ 74057193Smuller io_ok = 0; 74157193Smuller break; 74257193Smuller } 74357193Smuller if (lstrval <= 0) { 74457535Smuller warn(1, "Unable to recover from an archive read failure."); 74557193Smuller return(-1); 74657193Smuller } 74757193Smuller warn(0, "Attempting to recover from an archive read failure."); 74857193Smuller return(0); 74957193Smuller } 75057193Smuller 75157193Smuller /* 75257193Smuller * ar_fow() 75357193Smuller * Move the I/O position within the archive foward the specified number of 75457193Smuller * bytes as supported by the device. If we cannot move the requested 75557193Smuller * number of bytes, return the actual number of bytes moved in skipped. 75657193Smuller * Return: 75757193Smuller * 0 if moved the requested distance, -1 on complete failure, 1 on 75857193Smuller * partial move (the amount moved is in skipped) 75957193Smuller */ 76057193Smuller 76157193Smuller #if __STDC__ 76257193Smuller int 76357193Smuller ar_fow(off_t sksz, off_t *skipped) 76457193Smuller #else 76557193Smuller int 76657193Smuller ar_fow(sksz, skipped) 76757193Smuller off_t sksz; 76857193Smuller off_t *skipped; 76957193Smuller #endif 77057193Smuller { 77157193Smuller off_t cpos; 77257193Smuller off_t mpos; 77357193Smuller 77457193Smuller *skipped = 0; 77557193Smuller if (sksz <= 0) 77657193Smuller return(0); 77757193Smuller 77857193Smuller /* 77957193Smuller * we cannot move foward at EOF or error 78057193Smuller */ 78157193Smuller if (lstrval <= 0) 78257193Smuller return(lstrval); 78357193Smuller 78457193Smuller /* 78557193Smuller * Safer to read forward on devices where it is hard to find the end of 78657193Smuller * the media without reading to it. With tapes we cannot be sure of the 78757193Smuller * number of physical blocks to skip (we do not know physical block 78857193Smuller * size at this point), so we must only read foward on tapes! 78957193Smuller */ 79057193Smuller if (artyp != ISREG) 79157193Smuller return(0); 79257193Smuller 79357193Smuller /* 79457193Smuller * figure out where we are in the archive 79557193Smuller */ 79657193Smuller if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) >= 0) { 79757193Smuller /* 79857193Smuller * we can be asked to move farther than there are bytes in this 79957193Smuller * volume, if so, just go to file end and let normal buf_fill() 80057193Smuller * deal with the end of file (it will go to next volume by 80157193Smuller * itself) 80257193Smuller */ 80357193Smuller if ((mpos = cpos + sksz) > arsb.st_size) { 80457193Smuller *skipped = arsb.st_size - cpos; 80557193Smuller mpos = arsb.st_size; 80657193Smuller } else 80757193Smuller *skipped = sksz; 80857193Smuller if (lseek(arfd, mpos, SEEK_SET) >= 0) 80957193Smuller return(0); 81057193Smuller } 81157193Smuller syswarn(1, errno, "Foward positioning operation on archive failed"); 81257193Smuller lstrval = -1; 81357193Smuller return(-1); 81457193Smuller } 81557193Smuller 81657193Smuller /* 81757193Smuller * ar_rev() 81857193Smuller * move the i/o position within the archive backwards the specified byte 81957193Smuller * count as supported by the device. With tapes drives we RESET rdblksz to 82057193Smuller * the PHYSICAL blocksize. 82157193Smuller * NOTE: We should only be called to move backwards so we can rewrite the 82257193Smuller * last records (the trailer) of an archive (APPEND). 82357193Smuller * Return: 82457193Smuller * 0 if moved the requested distance, -1 on complete failure 82557193Smuller */ 82657193Smuller 82757193Smuller #if __STDC__ 82857193Smuller int 82957193Smuller ar_rev(off_t sksz) 83057193Smuller #else 83157193Smuller int 83257193Smuller ar_rev(sksz) 83357193Smuller off_t sksz; 83457193Smuller #endif 83557193Smuller { 83657193Smuller off_t cpos; 83757193Smuller struct mtop mb; 838*57794Smuller register int phyblk; 83957193Smuller 84057193Smuller /* 84157578Smuller * make sure we do not have try to reverse on a flawed archive 84257193Smuller */ 84357193Smuller if (lstrval < 0) 84457193Smuller return(lstrval); 84557193Smuller 84657193Smuller switch(artyp) { 84757193Smuller case ISPIPE: 84857578Smuller if (sksz <= 0) 84957578Smuller break; 85057193Smuller /* 85157193Smuller * cannot go backwards on these critters 85257193Smuller */ 85357578Smuller warn(1, "Reverse positioning on pipes is not supported."); 85457578Smuller lstrval = -1; 85557578Smuller return(-1); 85657193Smuller case ISREG: 85757193Smuller case ISBLK: 85857193Smuller case ISCHR: 85957193Smuller default: 86057578Smuller if (sksz <= 0) 86157578Smuller break; 86257578Smuller 86357193Smuller /* 86457193Smuller * For things other than files, backwards movement has a very 86557193Smuller * high probability of failure as we really do not know the 86657193Smuller * true attributes of the device we are talking to (the device 86757193Smuller * may not even have the ability to lseek() in any direction). 86857578Smuller * First we figure out where we are in the archive. 86957193Smuller */ 87057578Smuller if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) { 87157578Smuller syswarn(1, errno, 87257578Smuller "Unable to obtain current archive byte offset"); 87357578Smuller lstrval = -1; 87457578Smuller return(-1); 87557578Smuller } 87657193Smuller 87757193Smuller /* 87857193Smuller * we may try to go backwards past the start when the archive 87957193Smuller * is only a single record. If this hapens and we are on a 88057193Smuller * multi volume archive, we need to go to the end of the 88157193Smuller * previous volume and continue our movement backwards from 88257578Smuller * there. 88357193Smuller */ 88457193Smuller if ((cpos -= sksz) < (off_t)0L) { 88557193Smuller if (arvol > 1) { 88657578Smuller /* 88757578Smuller * this should never happen 88857578Smuller */ 88957578Smuller warn(1,"Reverse position on previous volume."); 89057193Smuller lstrval = -1; 89157193Smuller return(-1); 89257193Smuller } 89357193Smuller cpos = (off_t)0L; 89457193Smuller } 89557578Smuller if (lseek(arfd, cpos, SEEK_SET) < 0) { 89657578Smuller syswarn(1, errno, "Unable to seek archive backwards"); 89757578Smuller lstrval = -1; 89857578Smuller return(-1); 89957578Smuller } 90057578Smuller break; 90157193Smuller case ISTAPE: 90257193Smuller /* 90357193Smuller * Calculate and move the proper number of PHYSICAL tape 90457578Smuller * blocks. If the sksz is not an even multiple of the physical 90557193Smuller * tape size, we cannot do the move (this should never happen). 90657193Smuller * (We also cannot handler trailers spread over two vols). 90757578Smuller * get_phys() also makes sure we are in front of the filemark. 90857193Smuller */ 909*57794Smuller if ((phyblk = get_phys()) <= 0) { 91057578Smuller lstrval = -1; 91157578Smuller return(-1); 91257193Smuller } 91357193Smuller 91457578Smuller /* 91557578Smuller * make sure future tape reads only go by physical tape block 91657578Smuller * size (set rdblksz to the real size). 91757578Smuller */ 91857578Smuller rdblksz = phyblk; 91957578Smuller 92057578Smuller /* 92157578Smuller * if no movement is required, just return (we must be after 92257578Smuller * get_phys() so the physical blocksize is properly set) 92357578Smuller */ 92457578Smuller if (sksz <= 0) 92557578Smuller break; 92657578Smuller 92757578Smuller /* 92857578Smuller * ok we have to move. Make sure the tape drive can do it. 92957578Smuller */ 93057193Smuller if (sksz % phyblk) { 93157578Smuller warn(1, 93257578Smuller "Tape drive unable to backspace requested amount"); 93357193Smuller lstrval = -1; 93457193Smuller return(-1); 93557193Smuller } 93657193Smuller 93757578Smuller /* 93857578Smuller * move backwards the requested number of bytes 93957578Smuller */ 94057193Smuller mb.mt_op = MTBSR; 94157193Smuller mb.mt_count = sksz/phyblk; 94257578Smuller if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 94357578Smuller syswarn(1,errno, "Unable to backspace tape %d blocks.", 94457578Smuller mb.mt_count); 94557578Smuller lstrval = -1; 94657578Smuller return(-1); 94757578Smuller } 94857578Smuller break; 94957193Smuller } 95057578Smuller lstrval = 1; 95157578Smuller return(0); 95257193Smuller } 95357193Smuller 95457193Smuller /* 95557193Smuller * get_phys() 95657578Smuller * Determine the physical block size on a tape drive. We need the physical 95757578Smuller * block size so we know how many bytes we skip over when we move with 95857578Smuller * mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when 95957578Smuller * return. 96057578Smuller * This is one really SLOW routine... 96157193Smuller * Return: 962*57794Smuller * physical block size if ok (ok > 0), -1 otherwise 96357193Smuller */ 96457193Smuller 96557193Smuller #if __STDC__ 96657193Smuller static int 96757193Smuller get_phys(void) 96857193Smuller #else 96957193Smuller static int 97057193Smuller get_phys() 97157193Smuller #endif 97257193Smuller { 97357578Smuller register int padsz = 0; 97457535Smuller register int res; 975*57794Smuller register int phyblk; 97657535Smuller struct mtop mb; 97757578Smuller char scbuf[MAXBLK]; 97857193Smuller 97957193Smuller /* 98057578Smuller * move to the file mark, and then back up one record and read it. 98157578Smuller * this should tell us the physical record size the tape is using. 98257578Smuller */ 98357578Smuller if (lstrval == 1) { 98457578Smuller /* 98557578Smuller * we know we are at file mark when we get back a 0 from 98657578Smuller * read() 98757578Smuller */ 98857578Smuller while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0) 98957578Smuller padsz += res; 99057578Smuller if (res < 0) { 99157578Smuller syswarn(1, errno, "Unable to locate tape filemark."); 99257578Smuller return(-1); 99357578Smuller } 99457578Smuller } 99557193Smuller 99657578Smuller /* 99757578Smuller * move backwards over the file mark so we are at the end of the 99857578Smuller * last record. 99957578Smuller */ 100057578Smuller mb.mt_op = MTBSF; 100157193Smuller mb.mt_count = 1; 100257578Smuller if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 100357578Smuller syswarn(1, errno, "Unable to backspace over tape filemark."); 100457193Smuller return(-1); 100557535Smuller } 100657193Smuller 100757193Smuller /* 100857578Smuller * move backwards so we are in front of the last record and read it to 100957578Smuller * get physical tape blocksize. 101057193Smuller */ 101157578Smuller mb.mt_op = MTBSR; 101257578Smuller mb.mt_count = 1; 101357578Smuller if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 101457578Smuller syswarn(1, errno, "Unable to backspace over last tape block."); 101557193Smuller return(-1); 101657578Smuller } 101757578Smuller if ((phyblk = read(arfd, scbuf, sizeof(scbuf))) <= 0) { 101857578Smuller syswarn(1, errno, "Cannot determine archive tape blocksize."); 101957578Smuller return(-1); 102057578Smuller } 102157535Smuller 102257535Smuller /* 102357578Smuller * read foward to the file mark, then back up in front of the filemark 102457578Smuller * (this is a bit paranoid, but should be safe to do). 102557535Smuller */ 102657578Smuller while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0) 102757578Smuller ; 102857578Smuller if (res < 0) { 102957578Smuller syswarn(1, errno, "Unable to locate tape filemark."); 103057535Smuller return(-1); 103157578Smuller } 103257578Smuller mb.mt_op = MTBSF; 103357578Smuller mb.mt_count = 1; 103457578Smuller if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 103557578Smuller syswarn(1, errno, "Unable to backspace over tape filemark."); 103657578Smuller return(-1); 103757578Smuller } 103857535Smuller 103957535Smuller /* 104057578Smuller * set lstrval so we know that the filemark has not been seen 104157535Smuller */ 104257578Smuller lstrval = 1; 104357578Smuller 104457578Smuller /* 104557578Smuller * return if there was no padding 104657578Smuller */ 104757578Smuller if (padsz == 0) 1048*57794Smuller return(phyblk); 104957535Smuller 105057535Smuller /* 105157578Smuller * make sure we can move backwards over the padding. (this should 105257578Smuller * never fail). 105357535Smuller */ 105457578Smuller if (padsz % phyblk) { 105557578Smuller warn(1, "Tape drive unable to backspace requested amount"); 105657535Smuller return(-1); 105757578Smuller } 105857535Smuller 105957535Smuller /* 106057578Smuller * move backwards over the padding so the head is where it was when 106157578Smuller * we were first called (if required). 106257535Smuller */ 106357578Smuller mb.mt_op = MTBSR; 106457578Smuller mb.mt_count = padsz/phyblk; 106557578Smuller if (ioctl(arfd, MTIOCTOP, &mb) < 0) { 106657578Smuller syswarn(1,errno,"Unable to backspace tape over %d pad blocks", 106757578Smuller mb.mt_count); 106857535Smuller return(-1); 106957578Smuller } 1070*57794Smuller return(phyblk); 107157193Smuller } 107257193Smuller 107357193Smuller /* 107457193Smuller * ar_next() 107557535Smuller * prompts the user for the next volume in this archive. For some devices 107657535Smuller * we may allow the media to be changed. Otherwise a new archive is 107757535Smuller * prompted for. By pax spec, if there is no controlling tty or an eof is 107857535Smuller * read on tty input, we must quit pax. 107957193Smuller * Return: 108057193Smuller * 0 when ready to continue, -1 when all done 108157193Smuller */ 108257193Smuller 108357193Smuller #if __STDC__ 108457193Smuller int 108557193Smuller ar_next(void) 108657193Smuller #else 108757193Smuller int 108857193Smuller ar_next() 108957193Smuller #endif 109057193Smuller { 109157193Smuller char buf[PAXPATHLEN+2]; 109257193Smuller static int freeit = 0; 109357193Smuller sigset_t o_mask; 109457193Smuller 109557193Smuller /* 109657535Smuller * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so 109757193Smuller * things like writing EOF etc will be done) (Watch out ar_close() can 109857535Smuller * also be called via a signal handler, so we must prevent a race. 109957193Smuller */ 110057193Smuller if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0) 110157535Smuller syswarn(0, errno, "Unable to set signal mask"); 110257193Smuller ar_close(); 110357193Smuller if (sigprocmask(SIG_SETMASK, &o_mask, (sigset_t *)NULL) < 0) 110457535Smuller syswarn(0, errno, "Unable to restore signal mask"); 110557193Smuller 110657578Smuller if (done || !wr_trail) 110757193Smuller return(-1); 110857193Smuller 110957193Smuller tty_prnt("\nATTENTION! Pax archive volume change required.\n"); 111057193Smuller 111157193Smuller /* 111257193Smuller * if i/o is on stdin or stdout, we cannot reopen it (we do not know 111357535Smuller * the name), the user will be forced to type it in. 111457193Smuller */ 111557193Smuller if (strcmp(arcname, STDO) && strcmp(arcname, STDN) && (artyp != ISREG) 111657193Smuller && (artyp != ISPIPE)) { 111757193Smuller if (artyp == ISTAPE) { 111857193Smuller tty_prnt("%s ready for archive tape volume: %d\n", 111957193Smuller arcname, arvol); 112057193Smuller tty_prnt("Load the NEXT TAPE on the tape drive"); 112157193Smuller } else { 112257193Smuller tty_prnt("%s ready for archive volume: %d\n", 112357193Smuller arcname, arvol); 112457193Smuller tty_prnt("Load the NEXT STORAGE MEDIA (if required)"); 112557193Smuller } 112657193Smuller 112757193Smuller if ((act == ARCHIVE) || (act == APPND)) 112857193Smuller tty_prnt(" and make sure it is WRITE ENABLED.\n"); 112957193Smuller else 113057193Smuller tty_prnt("\n"); 113157193Smuller 113257193Smuller for(;;) { 113357193Smuller tty_prnt("Type \"y\" to continue, \".\" to quit pax,"); 113457193Smuller tty_prnt(" or \"s\" to switch to new device.\nIf you"); 113557193Smuller tty_prnt(" cannot change storage media, type \"s\"\n"); 113657193Smuller tty_prnt("Is the device ready and online? > "); 113757193Smuller 113857193Smuller if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){ 113957193Smuller done = 1; 114057193Smuller lstrval = -1; 114157193Smuller tty_prnt("Quitting pax!\n"); 114257193Smuller vfpart = 0; 114357193Smuller return(-1); 114457193Smuller } 114557193Smuller 114657193Smuller if ((buf[0] == '\0') || (buf[1] != '\0')) { 114757193Smuller tty_prnt("%s unknown command, try again\n",buf); 114857193Smuller continue; 114957193Smuller } 115057193Smuller 115157193Smuller switch (buf[0]) { 115257193Smuller case 'y': 115357193Smuller case 'Y': 115457193Smuller /* 115557193Smuller * we are to continue with the same device 115657193Smuller */ 115757193Smuller if (ar_open(arcname) >= 0) 115857193Smuller return(0); 115957193Smuller tty_prnt("Cannot re-open %s, try again\n", 116057193Smuller arcname); 116157193Smuller continue; 116257193Smuller case 's': 116357193Smuller case 'S': 116457193Smuller /* 116557193Smuller * user wants to open a different device 116657193Smuller */ 116757193Smuller tty_prnt("Switching to a different archive\n"); 116857193Smuller break; 116957193Smuller default: 117057193Smuller tty_prnt("%s unknown command, try again\n",buf); 117157193Smuller continue; 117257193Smuller } 117357193Smuller break; 117457193Smuller } 117557193Smuller } else 117657193Smuller tty_prnt("Ready for archive volume: %d\n", arvol); 117757193Smuller 117857193Smuller /* 117957193Smuller * have to go to a different archive 118057193Smuller */ 118157193Smuller for (;;) { 118257193Smuller tty_prnt("Input archive name or \".\" to quit pax.\n"); 118357193Smuller tty_prnt("Archive name > "); 118457193Smuller 118557193Smuller if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) { 118657193Smuller done = 1; 118757193Smuller lstrval = -1; 118857193Smuller tty_prnt("Quitting pax!\n"); 118957193Smuller vfpart = 0; 119057193Smuller return(-1); 119157193Smuller } 119257193Smuller if (buf[0] == '\0') { 119357193Smuller tty_prnt("Empty file name, try again\n"); 119457193Smuller continue; 119557193Smuller } 119657193Smuller if (!strcmp(buf, "..")) { 119757193Smuller tty_prnt("Illegal file name: .. try again\n"); 119857193Smuller continue; 119957193Smuller } 120057193Smuller if (strlen(buf) > PAXPATHLEN) { 120157193Smuller tty_prnt("File name too long, try again\n"); 120257193Smuller continue; 120357193Smuller } 120457193Smuller 120557193Smuller /* 120657193Smuller * try to open new archive 120757193Smuller */ 120857193Smuller if (ar_open(buf) >= 0) { 120957193Smuller if (freeit) { 121057193Smuller (void)free(arcname); 121157193Smuller freeit = 0; 121257193Smuller } 121357193Smuller if ((arcname = strdup(buf)) == NULL) { 121457193Smuller done = 1; 121557193Smuller lstrval = -1; 121657535Smuller warn(0, "Cannot save archive name."); 121757193Smuller return(-1); 121857193Smuller } 121957193Smuller freeit = 1; 122057193Smuller break; 122157193Smuller } 122257193Smuller tty_prnt("Cannot open %s, try again\n", buf); 122357193Smuller continue; 122457193Smuller } 122557193Smuller return(0); 122657193Smuller } 1227