12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220 |
- /*
- * This file is part of the UCB release of Plan 9. It is subject to the license
- * terms in the LICENSE file found in the top-level directory of this
- * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
- * part of the UCB release of Plan 9, including this file, may be copied,
- * modified, propagated, or distributed except according to the terms contained
- * in the LICENSE file.
- */
- /*
- * ar - portable (ascii) format version
- */
- #include <u.h>
- #include <libc.h>
- #include <bio.h>
- #include <mach.h>
- #include <ar.h>
- /*
- * The algorithm uses up to 3 temp files. The "pivot member" is the
- * archive member specified by and a, b, or i option. The temp files are
- * astart - contains existing members up to and including the pivot member.
- * amiddle - contains new files moved or inserted behind the pivot.
- * aend - contains the existing members that follow the pivot member.
- * When all members have been processed, function 'install' streams the
- * temp files, in order, back into the archive.
- */
- typedef struct Arsymref
- {
- char *name;
- int type;
- int len;
- int64_t offset;
- struct Arsymref *next;
- } Arsymref;
- typedef struct Armember /* Temp file entry - one per archive member */
- {
- struct Armember *next;
- struct ar_hdr hdr;
- int32_t size;
- int32_t date;
- void *member;
- } Armember;
- typedef struct Arfile /* Temp file control block - one per tempfile */
- {
- int paged; /* set when some data paged to disk */
- char *fname; /* paging file name */
- int fd; /* paging file descriptor */
- int64_t size;
- Armember *head; /* head of member chain */
- Armember *tail; /* tail of member chain */
- Arsymref *sym; /* head of defined symbol chain */
- } Arfile;
- typedef struct Hashchain
- {
- char *name;
- struct Hashchain *next;
- } Hashchain;
- #define NHASH 1024
- /*
- * macro to portably read/write archive header.
- * 'cmd' is read/write/Bread/Bwrite, etc.
- */
- #define HEADER_IO(cmd, f, h) cmd(f, h.name, sizeof(h.name)) != sizeof(h.name)\
- || cmd(f, h.date, sizeof(h.date)) != sizeof(h.date)\
- || cmd(f, h.uid, sizeof(h.uid)) != sizeof(h.uid)\
- || cmd(f, h.gid, sizeof(h.gid)) != sizeof(h.gid)\
- || cmd(f, h.mode, sizeof(h.mode)) != sizeof(h.mode)\
- || cmd(f, h.size, sizeof(h.size)) != sizeof(h.size)\
- || cmd(f, h.fmag, sizeof(h.fmag)) != sizeof(h.fmag)
- /* constants and flags */
- char *man = "mrxtdpq";
- char *opt = "uvnbailo";
- char artemp[] = "/tmp/vXXXXX";
- char movtemp[] = "/tmp/v1XXXXX";
- char tailtemp[] = "/tmp/v2XXXXX";
- char symdef[] = "__.SYMDEF";
- int aflag; /* command line flags */
- int bflag;
- int cflag;
- int oflag;
- int uflag;
- int vflag;
- Arfile *astart, *amiddle, *aend; /* Temp file control block pointers */
- int allobj = 1; /* set when all members are object files of the same type */
- int symdefsize; /* size of symdef file */
- int dupfound; /* flag for duplicate symbol */
- Hashchain *hash[NHASH]; /* hash table of text symbols */
-
- #define ARNAMESIZE sizeof(astart->tail->hdr.name)
- char poname[ARNAMESIZE+1]; /* name of pivot member */
- char *file; /* current file or member being worked on */
- Biobuf bout;
- Biobuf bar;
- void arcopy(Biobuf*, Arfile*, Armember*);
- int arcreate(char*);
- void arfree(Arfile*);
- void arinsert(Arfile*, Armember*);
- char *armalloc(int);
- void armove(Biobuf*, Arfile*, Armember*);
- void arread(Biobuf*, Armember*, int);
- void arstream(int, Arfile*);
- int arwrite(int, Armember*);
- int bamatch(char*, char*);
- int duplicate(char*);
- Armember *getdir(Biobuf*);
- int getspace(void);
- void install(char*, Arfile*, Arfile*, Arfile*, int);
- void int32_tt(Armember*);
- int match(int, char**);
- void mesg(int, char*);
- Arfile *newtempfile(char*);
- Armember *newmember(void);
- void objsym(Sym*, void*);
- int openar(char*, int, int);
- int page(Arfile*);
- void pmode(int32_t);
- void rl(int);
- void scanobj(Biobuf*, Arfile*, int32_t);
- void select(int*, int32_t);
- void setcom(void(*)(char*, int, char**));
- void skip(Biobuf*, int64_t);
- int symcomp(void *c, void*);
- void trim(char*, char*, int);
- void usage(void);
- void wrerr(void);
- void wrsym(Biobuf*, int32_t, Arsymref*);
- void rcmd(char*, int, char**); /* command processing */
- void dcmd(char*, int, char**);
- void xcmd(char*, int, char**);
- void tcmd(char*, int, char**);
- void pcmd(char*, int, char**);
- void mcmd(char*, int, char**);
- void qcmd(char*, int, char**);
- void (*comfun)(char*, int, char**);
- void
- main(int argc, char *argv[])
- {
- char *cp;
- Binit(&bout, 1, OWRITE);
- if(argc < 3)
- usage();
- for (cp = argv[1]; *cp; cp++) {
- switch(*cp) {
- case 'a': aflag = 1; break;
- case 'b': bflag = 1; break;
- case 'c': cflag = 1; break;
- case 'd': setcom(dcmd); break;
- case 'i': bflag = 1; break;
- case 'l':
- strcpy(artemp, "vXXXXX");
- strcpy(movtemp, "v1XXXXX");
- strcpy(tailtemp, "v2XXXXX");
- break;
- case 'm': setcom(mcmd); break;
- case 'o': oflag = 1; break;
- case 'p': setcom(pcmd); break;
- case 'q': setcom(qcmd); break;
- case 'r': setcom(rcmd); break;
- case 't': setcom(tcmd); break;
- case 'u': uflag = 1; break;
- case 'v': vflag = 1; break;
- case 'x': setcom(xcmd); break;
- default:
- fprint(2, "ar: bad option `%c'\n", *cp);
- exits("error");
- }
- }
- if (aflag && bflag) {
- fprint(2, "ar: only one of 'a' and 'b' can be specified\n");
- usage();
- }
- if(aflag || bflag) {
- trim(argv[2], poname, sizeof(poname));
- argv++;
- argc--;
- if(argc < 3)
- usage();
- }
- if(comfun == 0) {
- if(uflag == 0) {
- fprint(2, "ar: one of [%s] must be specified\n", man);
- usage();
- }
- setcom(rcmd);
- }
- cp = argv[2];
- argc -= 3;
- argv += 3;
- (*comfun)(cp, argc, argv); /* do the command */
- cp = 0;
- while (argc--) {
- if (*argv) {
- fprint(2, "ar: %s not found\n", *argv);
- cp = "error";
- }
- argv++;
- }
- exits(cp);
- }
- /*
- * select a command
- */
- void
- setcom(void (*fun)(char *, int, char**))
- {
- if(comfun != 0) {
- fprint(2, "ar: only one of [%s] allowed\n", man);
- usage();
- }
- comfun = fun;
- }
- /*
- * perform the 'r' and 'u' commands
- */
- void
- rcmd(char *arname, int count, char **files)
- {
- int fd;
- int i;
- Arfile *ap;
- Armember *bp;
- Dir *d;
- Biobuf *bfile;
- fd = openar(arname, ORDWR, 1);
- if (fd >= 0) {
- Binit(&bar, fd, OREAD);
- Bseek(&bar,seek(fd,0,1), 1);
- }
- astart = newtempfile(artemp);
- ap = astart;
- aend = 0;
- for(i = 0; fd >= 0; i++) {
- bp = getdir(&bar);
- if (!bp)
- break;
- if (bamatch(file, poname)) { /* check for pivot */
- aend = newtempfile(tailtemp);
- ap = aend;
- }
- /* pitch symdef file */
- if (i == 0 && strcmp(file, symdef) == 0) {
- skip(&bar, bp->size);
- continue;
- }
- if (count && !match(count, files)) {
- scanobj(&bar, ap, bp->size);
- arcopy(&bar, ap, bp);
- continue;
- }
- bfile = Bopen(file, OREAD);
- if (!bfile) {
- if (count != 0)
- fprint(2, "ar: cannot open %s\n", file);
- scanobj(&bar, ap, bp->size);
- arcopy(&bar, ap, bp);
- continue;
- }
- d = dirfstat(Bfildes(bfile));
- if(d == nil)
- fprint(2, "ar: cannot stat %s: %r\n", file);
- if (uflag && (d==nil || d->mtime <= bp->date)) {
- scanobj(&bar, ap, bp->size);
- arcopy(&bar, ap, bp);
- Bterm(bfile);
- free(d);
- continue;
- }
- mesg('r', file);
- skip(&bar, bp->size);
- scanobj(bfile, ap, d->length);
- free(d);
- armove(bfile, ap, bp);
- Bterm(bfile);
- }
- if(fd >= 0)
- close(fd);
- /* copy in remaining files named on command line */
- for (i = 0; i < count; i++) {
- file = files[i];
- if(file == 0)
- continue;
- files[i] = 0;
- bfile = Bopen(file, OREAD);
- if (!bfile)
- fprint(2, "ar: %s cannot open\n", file);
- else {
- mesg('a', file);
- d = dirfstat(Bfildes(bfile));
- if (d == nil)
- fprint(2, "can't stat %s\n", file);
- else {
- scanobj(bfile, astart, d->length);
- armove(bfile, astart, newmember());
- free(d);
- }
- Bterm(bfile);
- }
- }
- if(fd < 0 && !cflag)
- install(arname, astart, 0, aend, 1); /* issue 'creating' msg */
- else
- install(arname, astart, 0, aend, 0);
- }
- void
- dcmd(char *arname, int count, char **files)
- {
- Armember *bp;
- int fd, i;
- if (!count)
- return;
- fd = openar(arname, ORDWR, 0);
- Binit(&bar, fd, OREAD);
- Bseek(&bar,seek(fd,0,1), 1);
- astart = newtempfile(artemp);
- for (i = 0; bp = getdir(&bar); i++) {
- if(match(count, files)) {
- mesg('d', file);
- skip(&bar, bp->size);
- if (strcmp(file, symdef) == 0)
- allobj = 0;
- } else if (i == 0 && strcmp(file, symdef) == 0)
- skip(&bar, bp->size);
- else {
- scanobj(&bar, astart, bp->size);
- arcopy(&bar, astart, bp);
- }
- }
- close(fd);
- install(arname, astart, 0, 0, 0);
- }
- void
- xcmd(char *arname, int count, char **files)
- {
- int fd, f, mode, i;
- Armember *bp;
- Dir dx;
- fd = openar(arname, OREAD, 0);
- Binit(&bar, fd, OREAD);
- Bseek(&bar,seek(fd,0,1), 1);
- i = 0;
- while (bp = getdir(&bar)) {
- if(count == 0 || match(count, files)) {
- mode = strtoul(bp->hdr.mode, 0, 8) & 0777;
- f = create(file, OWRITE, mode);
- if(f < 0) {
- fprint(2, "ar: %s cannot create\n", file);
- skip(&bar, bp->size);
- } else {
- mesg('x', file);
- arcopy(&bar, 0, bp);
- if (write(f, bp->member, bp->size) < 0)
- wrerr();
- if(oflag) {
- nulldir(&dx);
- dx.atime = bp->date;
- dx.mtime = bp->date;
- if(dirwstat(file, &dx) < 0)
- perror(file);
- }
- free(bp->member);
- close(f);
- }
- free(bp);
- if (count && ++i >= count)
- break;
- } else {
- skip(&bar, bp->size);
- free(bp);
- }
- }
- close(fd);
- }
- void
- pcmd(char *arname, int count, char **files)
- {
- int fd;
- Armember *bp;
- fd = openar(arname, OREAD, 0);
- Binit(&bar, fd, OREAD);
- Bseek(&bar,seek(fd,0,1), 1);
- while(bp = getdir(&bar)) {
- if(count == 0 || match(count, files)) {
- if(vflag)
- print("\n<%s>\n\n", file);
- arcopy(&bar, 0, bp);
- if (write(1, bp->member, bp->size) < 0)
- wrerr();
- } else
- skip(&bar, bp->size);
- free(bp);
- }
- close(fd);
- }
- void
- mcmd(char *arname, int count, char **files)
- {
- int fd, i;
- Arfile *ap;
- Armember *bp;
- if (count == 0)
- return;
- fd = openar(arname, ORDWR, 0);
- Binit(&bar, fd, OREAD);
- Bseek(&bar,seek(fd,0,1), 1);
- astart = newtempfile(artemp);
- amiddle = newtempfile(movtemp);
- aend = 0;
- ap = astart;
- for (i = 0; bp = getdir(&bar); i++) {
- if (bamatch(file, poname)) {
- aend = newtempfile(tailtemp);
- ap = aend;
- }
- if(match(count, files)) {
- mesg('m', file);
- scanobj(&bar, amiddle, bp->size);
- arcopy(&bar, amiddle, bp);
- } else
- /*
- * pitch the symdef file if it is at the beginning
- * of the archive and we aren't inserting in front
- * of it (ap == astart).
- */
- if (ap == astart && i == 0 && strcmp(file, symdef) == 0)
- skip(&bar, bp->size);
- else {
- scanobj(&bar, ap, bp->size);
- arcopy(&bar, ap, bp);
- }
- }
- close(fd);
- if (poname[0] && aend == 0)
- fprint(2, "ar: %s not found - files moved to end.\n", poname);
- install(arname, astart, amiddle, aend, 0);
- }
- void
- tcmd(char *arname, int count, char **files)
- {
- int fd;
- Armember *bp;
- char name[ARNAMESIZE+1];
- fd = openar(arname, OREAD, 0);
- Binit(&bar, fd, OREAD);
- Bseek(&bar,seek(fd,0,1), 1);
- while(bp = getdir(&bar)) {
- if(count == 0 || match(count, files)) {
- if(vflag)
- int32_tt(bp);
- trim(file, name, ARNAMESIZE);
- Bprint(&bout, "%s\n", name);
- }
- skip(&bar, bp->size);
- free(bp);
- }
- close(fd);
- }
- void
- qcmd(char *arname, int count, char **files)
- {
- int fd, i;
- Armember *bp;
- Biobuf *bfile;
- if(aflag || bflag) {
- fprint(2, "ar: abi not allowed with q\n");
- exits("error");
- }
- fd = openar(arname, ORDWR, 1);
- if (fd < 0) {
- if(!cflag)
- fprint(2, "ar: creating %s\n", arname);
- fd = arcreate(arname);
- }
- Binit(&bar, fd, OREAD);
- Bseek(&bar,seek(fd,0,1), 1);
- /* leave note group behind when writing archive; i.e. sidestep interrupts */
- rfork(RFNOTEG);
- Bseek(&bar, 0, 2);
- bp = newmember();
- for(i=0; i<count && files[i]; i++) {
- file = files[i];
- files[i] = 0;
- bfile = Bopen(file, OREAD);
- if(!bfile)
- fprint(2, "ar: %s cannot open\n", file);
- else {
- mesg('q', file);
- armove(bfile, 0, bp);
- if (!arwrite(fd, bp))
- wrerr();
- free(bp->member);
- bp->member = 0;
- Bterm(bfile);
- }
- }
- free(bp);
- close(fd);
- }
- /*
- * extract the symbol references from an object file
- */
- void
- scanobj(Biobuf *b, Arfile *ap, int32_t size)
- {
- int obj;
- int64_t offset;
- Dir *d;
- static int lastobj = -1;
- if (!allobj) /* non-object file encountered */
- return;
- offset = Boffset(b);
- obj = objtype(b, 0);
- if (obj < 0) { /* not an object file */
- allobj = 0;
- d = dirfstat(Bfildes(b));
- if (d != nil && d->length == 0)
- fprint(2, "ar: zero length file %s\n", file);
- free(d);
- Bseek(b, offset, 0);
- return;
- }
- if (lastobj >= 0 && obj != lastobj) {
- fprint(2, "ar: inconsistent object file %s\n", file);
- allobj = 0;
- Bseek(b, offset, 0);
- return;
- }
- lastobj = obj;
- if (!readar(b, obj, offset+size, 0)) {
- fprint(2, "ar: invalid symbol reference in file %s\n", file);
- allobj = 0;
- Bseek(b, offset, 0);
- return;
- }
- Bseek(b, offset, 0);
- objtraverse(objsym, ap);
- }
- /*
- * add text and data symbols to the symbol list
- */
- void
- objsym(Sym *s, void *p)
- {
- int n;
- Arsymref *as;
- Arfile *ap;
- if (s->type != 'T' && s->type != 'D')
- return;
- ap = (Arfile*)p;
- as = (Arsymref*)armalloc(sizeof(Arsymref));
- as->offset = ap->size;
- n = strlen(s->name);
- as->name = armalloc(n+1);
- strcpy(as->name, s->name);
- if(s->type == 'T' && duplicate(as->name)) {
- dupfound = 1;
- fprint(2, "duplicate text symbol: %s\n", as->name);
- free(as->name);
- free(as);
- return;
- }
- as->type = s->type;
- symdefsize += 4+(n+1)+1;
- as->len = n;
- as->next = ap->sym;
- ap->sym = as;
- }
- /*
- * Check the symbol table for duplicate text symbols
- */
- int
- duplicate(char *name)
- {
- Hashchain *p;
- char *cp;
- int h;
- h = 0;
- for(cp = name; *cp; h += *cp++)
- h *= 1119;
- if(h < 0)
- h = ~h;
- h %= NHASH;
- for(p = hash[h]; p; p = p->next)
- if(strcmp(p->name, name) == 0)
- return 1;
- p = (Hashchain*) armalloc(sizeof(Hashchain));
- p->next = hash[h];
- p->name = name;
- hash[h] = p;
- return 0;
- }
- /*
- * open an archive and validate its header
- */
- int
- openar(char *arname, int mode, int errok)
- {
- int fd;
- char mbuf[SARMAG];
- fd = open(arname, mode);
- if(fd >= 0){
- if(read(fd, mbuf, SARMAG) != SARMAG || strncmp(mbuf, ARMAG, SARMAG)) {
- fprint(2, "ar: %s not in archive format\n", arname);
- exits("error");
- }
- }else if(!errok){
- fprint(2, "ar: cannot open %s: %r\n", arname);
- exits("error");
- }
- return fd;
- }
- /*
- * create an archive and set its header
- */
- int
- arcreate(char *arname)
- {
- int fd;
- fd = create(arname, OWRITE, 0664);
- if(fd < 0){
- fprint(2, "ar: cannot create %s: %r\n", arname);
- exits("error");
- }
- if(write(fd, ARMAG, SARMAG) != SARMAG)
- wrerr();
- return fd;
- }
- /*
- * error handling
- */
- void
- wrerr(void)
- {
- perror("ar: write error");
- exits("error");
- }
- void
- rderr(void)
- {
- perror("ar: read error");
- exits("error");
- }
- void
- phaseerr(int offset)
- {
- fprint(2, "ar: phase error at offset %d\n", offset);
- exits("error");
- }
- void
- usage(void)
- {
- fprint(2, "usage: ar [%s][%s] archive files ...\n", opt, man);
- exits("error");
- }
- /*
- * read the header for the next archive member
- */
- Armember *
- getdir(Biobuf *b)
- {
- Armember *bp;
- char *cp;
- static char name[ARNAMESIZE+1];
- bp = newmember();
- if(HEADER_IO(Bread, b, bp->hdr)) {
- free(bp);
- return 0;
- }
- if(strncmp(bp->hdr.fmag, ARFMAG, sizeof(bp->hdr.fmag)) != 0)
- phaseerr(Boffset(b));
- strncpy(name, bp->hdr.name, sizeof(bp->hdr.name));
- cp = name+sizeof(name)-1;
- *cp = '\0';
- /* skip trailing spaces and (gnu-produced) slashes */
- while(*--cp == ' ' || *cp == '/')
- ;
- cp[1] = '\0';
- file = name;
- bp->date = strtol(bp->hdr.date, 0, 0);
- bp->size = strtol(bp->hdr.size, 0, 0);
- return bp;
- }
- /*
- * Copy the file referenced by fd to the temp file
- */
- void
- armove(Biobuf *b, Arfile *ap, Armember *bp)
- {
- char *cp;
- Dir *d;
- d = dirfstat(Bfildes(b));
- if (d == nil) {
- fprint(2, "ar: cannot stat %s\n", file);
- return;
- }
- trim(file, bp->hdr.name, sizeof(bp->hdr.name));
- for (cp = strchr(bp->hdr.name, 0); /* blank pad on right */
- cp < bp->hdr.name+sizeof(bp->hdr.name); cp++)
- *cp = ' ';
- sprint(bp->hdr.date, "%-12ld", d->mtime);
- sprint(bp->hdr.uid, "%-6d", 0);
- sprint(bp->hdr.gid, "%-6d", 0);
- sprint(bp->hdr.mode, "%-8lo", d->mode);
- sprint(bp->hdr.size, "%-10lld", d->length);
- strncpy(bp->hdr.fmag, ARFMAG, 2);
- bp->size = d->length;
- arread(b, bp, bp->size);
- if (d->length&0x01)
- d->length++;
- if (ap) {
- arinsert(ap, bp);
- ap->size += d->length+SAR_HDR;
- }
- free(d);
- }
- /*
- * Copy the archive member at the current offset into the temp file.
- */
- void
- arcopy(Biobuf *b, Arfile *ap, Armember *bp)
- {
- int32_t n;
- n = bp->size;
- if (n & 01)
- n++;
- arread(b, bp, n);
- if (ap) {
- arinsert(ap, bp);
- ap->size += n+SAR_HDR;
- }
- }
- /*
- * Skip an archive member
- */
- void
- skip(Biobuf *bp, int64_t len)
- {
- if (len & 01)
- len++;
- Bseek(bp, len, 1);
- }
- /*
- * Stream the three temp files to an archive
- */
- void
- install(char *arname, Arfile *astart, Arfile *amiddle, Arfile *aend,
- int createflag)
- {
- int fd;
- if(allobj && dupfound) {
- fprint(2, "%s not changed\n", arname);
- return;
- }
- /* leave note group behind when copying back; i.e. sidestep interrupts */
- rfork(RFNOTEG);
- if(createflag)
- fprint(2, "ar: creating %s\n", arname);
- fd = arcreate(arname);
- if(allobj)
- rl(fd);
- if (astart) {
- arstream(fd, astart);
- arfree(astart);
- }
- if (amiddle) {
- arstream(fd, amiddle);
- arfree(amiddle);
- }
- if (aend) {
- arstream(fd, aend);
- arfree(aend);
- }
- close(fd);
- }
- void
- rl(int fd)
- {
- Biobuf b;
- char *cp;
- struct ar_hdr a;
- int32_t len;
- Binit(&b, fd, OWRITE);
- Bseek(&b,seek(fd,0,1), 0);
- len = symdefsize;
- if(len&01)
- len++;
- sprint(a.date, "%-12ld", time(0));
- sprint(a.uid, "%-6d", 0);
- sprint(a.gid, "%-6d", 0);
- sprint(a.mode, "%-8lo", 0644L);
- sprint(a.size, "%-10ld", len);
- strncpy(a.fmag, ARFMAG, 2);
- strcpy(a.name, symdef);
- for (cp = strchr(a.name, 0); /* blank pad on right */
- cp < a.name+sizeof(a.name); cp++)
- *cp = ' ';
- if(HEADER_IO(Bwrite, &b, a))
- wrerr();
- len += Boffset(&b);
- if (astart) {
- wrsym(&b, len, astart->sym);
- len += astart->size;
- }
- if(amiddle) {
- wrsym(&b, len, amiddle->sym);
- len += amiddle->size;
- }
- if(aend)
- wrsym(&b, len, aend->sym);
- if(symdefsize&0x01)
- Bputc(&b, 0);
- Bterm(&b);
- }
- /*
- * Write the defined symbols to the symdef file
- */
- void
- wrsym(Biobuf *bp, int32_t offset, Arsymref *as)
- {
- int off;
- while(as) {
- Bputc(bp, as->type);
- off = as->offset+offset;
- Bputc(bp, off);
- Bputc(bp, off>>8);
- Bputc(bp, off>>16);
- Bputc(bp, off>>24);
- if (Bwrite(bp, as->name, as->len+1) != as->len+1)
- wrerr();
- as = as->next;
- }
- }
- /*
- * Check if the archive member matches an entry on the command line.
- */
- int
- match(int count, char **files)
- {
- int i;
- char name[ARNAMESIZE+1];
- for(i=0; i<count; i++) {
- if(files[i] == 0)
- continue;
- trim(files[i], name, ARNAMESIZE);
- if(strncmp(name, file, ARNAMESIZE) == 0) {
- file = files[i];
- files[i] = 0;
- return 1;
- }
- }
- return 0;
- }
- /*
- * compare the current member to the name of the pivot member
- */
- int
- bamatch(char *file, char *pivot)
- {
- static int state = 0;
- switch(state)
- {
- case 0: /* looking for position file */
- if (aflag) {
- if (strncmp(file, pivot, ARNAMESIZE) == 0)
- state = 1;
- } else if (bflag) {
- if (strncmp(file, pivot, ARNAMESIZE) == 0) {
- state = 2; /* found */
- return 1;
- }
- }
- break;
- case 1: /* found - after previous file */
- state = 2;
- return 1;
- case 2: /* already found position file */
- break;
- }
- return 0;
- }
- /*
- * output a message, if 'v' option was specified
- */
- void
- mesg(int c, char *file)
- {
- if(vflag)
- Bprint(&bout, "%c - %s\n", c, file);
- }
- /*
- * isolate file name by stripping leading directories and trailing slashes
- */
- void
- trim(char *s, char *buf, int n)
- {
- char *p;
- for(;;) {
- p = strrchr(s, '/');
- if (!p) { /* no slash in name */
- strncpy(buf, s, n);
- return;
- }
- if (p[1] != 0) { /* p+1 is first char of file name */
- strncpy(buf, p+1, n);
- return;
- }
- *p = 0; /* strip trailing slash */
- }
- }
- /*
- * utilities for printing int32_t form of 't' command
- */
- #define SUID 04000
- #define SGID 02000
- #define ROWN 0400
- #define WOWN 0200
- #define XOWN 0100
- #define RGRP 040
- #define WGRP 020
- #define XGRP 010
- #define ROTH 04
- #define WOTH 02
- #define XOTH 01
- #define STXT 01000
- void
- int32_tt(Armember *bp)
- {
- char *cp;
- pmode(strtoul(bp->hdr.mode, 0, 8));
- Bprint(&bout, "%3ld/%1ld", strtol(bp->hdr.uid, 0, 0), strtol(bp->hdr.gid, 0, 0));
- Bprint(&bout, "%7ld", bp->size);
- cp = ctime(bp->date);
- Bprint(&bout, " %-12.12s %-4.4s ", cp+4, cp+24);
- }
- int m1[] = { 1, ROWN, 'r', '-' };
- int m2[] = { 1, WOWN, 'w', '-' };
- int m3[] = { 2, SUID, 's', XOWN, 'x', '-' };
- int m4[] = { 1, RGRP, 'r', '-' };
- int m5[] = { 1, WGRP, 'w', '-' };
- int m6[] = { 2, SGID, 's', XGRP, 'x', '-' };
- int m7[] = { 1, ROTH, 'r', '-' };
- int m8[] = { 1, WOTH, 'w', '-' };
- int m9[] = { 2, STXT, 't', XOTH, 'x', '-' };
- int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
- void
- pmode(int32_t mode)
- {
- int **mp;
- for(mp = &m[0]; mp < &m[9];)
- select(*mp++, mode);
- }
- void
- select(int *ap, int32_t mode)
- {
- int n;
- n = *ap++;
- while(--n>=0 && (mode&*ap++)==0)
- ap++;
- Bputc(&bout, *ap);
- }
- /*
- * Temp file I/O subsystem. We attempt to cache all three temp files in
- * core. When we run out of memory we spill to disk.
- * The I/O model assumes that temp files:
- * 1) are only written on the end
- * 2) are only read from the beginning
- * 3) are only read after all writing is complete.
- * The architecture uses one control block per temp file. Each control
- * block anchors a chain of buffers, each containing an archive member.
- */
- Arfile *
- newtempfile(char *name) /* allocate a file control block */
- {
- Arfile *ap;
- ap = (Arfile *) armalloc(sizeof(Arfile));
- ap->fname = name;
- return ap;
- }
- Armember *
- newmember(void) /* allocate a member buffer */
- {
- return (Armember *)armalloc(sizeof(Armember));
- }
- void
- arread(Biobuf *b, Armember *bp, int n) /* read an image into a member buffer */
- {
- int i;
- bp->member = armalloc(n);
- i = Bread(b, bp->member, n);
- if (i < 0) {
- free(bp->member);
- bp->member = 0;
- rderr();
- }
- }
- /*
- * insert a member buffer into the member chain
- */
- void
- arinsert(Arfile *ap, Armember *bp)
- {
- bp->next = 0;
- if (!ap->tail)
- ap->head = bp;
- else
- ap->tail->next = bp;
- ap->tail = bp;
- }
- /*
- * stream the members in a temp file to the file referenced by 'fd'.
- */
- void
- arstream(int fd, Arfile *ap)
- {
- Armember *bp;
- int i;
- char buf[8192];
- if (ap->paged) { /* copy from disk */
- seek(ap->fd, 0, 0);
- for (;;) {
- i = read(ap->fd, buf, sizeof(buf));
- if (i < 0)
- rderr();
- if (i == 0)
- break;
- if (write(fd, buf, i) != i)
- wrerr();
- }
- close(ap->fd);
- ap->paged = 0;
- }
- /* dump the in-core buffers */
- for (bp = ap->head; bp; bp = bp->next) {
- if (!arwrite(fd, bp))
- wrerr();
- }
- }
- /*
- * write a member to 'fd'.
- */
- int
- arwrite(int fd, Armember *bp)
- {
- int len;
- if(HEADER_IO(write, fd, bp->hdr))
- return 0;
- len = bp->size;
- if (len & 01)
- len++;
- if (write(fd, bp->member, len) != len)
- return 0;
- return 1;
- }
- /*
- * Spill a member to a disk copy of a temp file
- */
- int
- page(Arfile *ap)
- {
- Armember *bp;
- bp = ap->head;
- if (!ap->paged) { /* not yet paged - create file */
- ap->fname = mktemp(ap->fname);
- ap->fd = create(ap->fname, ORDWR|ORCLOSE, 0600);
- if (ap->fd < 0) {
- fprint(2,"ar: can't create temp file\n");
- return 0;
- }
- ap->paged = 1;
- }
- if (!arwrite(ap->fd, bp)) /* write member and free buffer block */
- return 0;
- ap->head = bp->next;
- if (ap->tail == bp)
- ap->tail = bp->next;
- free(bp->member);
- free(bp);
- return 1;
- }
- /*
- * try to reclaim space by paging. we try to spill the start, middle,
- * and end files, in that order. there is no particular reason for the
- * ordering.
- */
- int
- getspace(void)
- {
- if (astart && astart->head && page(astart))
- return 1;
- if (amiddle && amiddle->head && page(amiddle))
- return 1;
- if (aend && aend->head && page(aend))
- return 1;
- return 0;
- }
- void
- arfree(Arfile *ap) /* free a member buffer */
- {
- Armember *bp, *next;
- for (bp = ap->head; bp; bp = next) {
- next = bp->next;
- if (bp->member)
- free(bp->member);
- free(bp);
- }
- free(ap);
- }
- /*
- * allocate space for a control block or member buffer. if the malloc
- * fails we try to reclaim space by spilling previously allocated
- * member buffers.
- */
- char *
- armalloc(int n)
- {
- char *cp;
- do {
- cp = malloc(n);
- if (cp) {
- memset(cp, 0, n);
- return cp;
- }
- } while (getspace());
- fprint(2, "ar: out of memory\n");
- exits("malloc");
- return 0;
- }
|