2 * ar - portable (ascii) format version
11 * The algorithm uses up to 3 temp files. The "pivot member" is the
12 * archive member specified by and a, b, or i option. The temp files are
13 * astart - contains existing members up to and including the pivot member.
14 * amiddle - contains new files moved or inserted behind the pivot.
15 * aend - contains the existing members that follow the pivot member.
16 * When all members have been processed, function 'install' streams the
17 * temp files, in order, back into the archive.
20 typedef struct Arsymref
26 struct Arsymref *next;
29 typedef struct Armember /* Temp file entry - one per archive member */
31 struct Armember *next;
38 typedef struct Arfile /* Temp file control block - one per tempfile */
40 int paged; /* set when some data paged to disk */
41 char *fname; /* paging file name */
42 int fd; /* paging file descriptor */
44 Armember *head; /* head of member chain */
45 Armember *tail; /* tail of member chain */
46 Arsymref *sym; /* head of defined symbol chain */
49 typedef struct Hashchain
52 struct Hashchain *next;
58 * macro to portably read/write archive header.
59 * 'cmd' is read/write/Bread/Bwrite, etc.
61 #define HEADER_IO(cmd, f, h) cmd(f, h.name, sizeof(h.name)) != sizeof(h.name)\
62 || cmd(f, h.date, sizeof(h.date)) != sizeof(h.date)\
63 || cmd(f, h.uid, sizeof(h.uid)) != sizeof(h.uid)\
64 || cmd(f, h.gid, sizeof(h.gid)) != sizeof(h.gid)\
65 || cmd(f, h.mode, sizeof(h.mode)) != sizeof(h.mode)\
66 || cmd(f, h.size, sizeof(h.size)) != sizeof(h.size)\
67 || cmd(f, h.fmag, sizeof(h.fmag)) != sizeof(h.fmag)
69 /* constants and flags */
70 char *man = "mrxtdpq";
71 char *opt = "uvnbailo";
72 char artemp[] = "/tmp/vXXXXX";
73 char movtemp[] = "/tmp/v1XXXXX";
74 char tailtemp[] = "/tmp/v2XXXXX";
75 char symdef[] = "__.SYMDEF";
77 int aflag; /* command line flags */
84 Arfile *astart, *amiddle, *aend; /* Temp file control block pointers */
85 int allobj = 1; /* set when all members are object files of the same type */
86 int symdefsize; /* size of symdef file */
87 int dupfound; /* flag for duplicate symbol */
88 Hashchain *hash[NHASH]; /* hash table of text symbols */
90 #define ARNAMESIZE sizeof(astart->tail->hdr.name)
92 char poname[ARNAMESIZE+1]; /* name of pivot member */
93 char *file; /* current file or member being worked on */
97 void arcopy(Biobuf*, Arfile*, Armember*);
100 void arinsert(Arfile*, Armember*);
102 void armove(Biobuf*, Arfile*, Armember*);
103 void arread(Biobuf*, Armember*, int);
104 void arstream(int, Arfile*);
105 int arwrite(int, Armember*);
106 int bamatch(char*, char*);
107 int duplicate(char*);
108 Armember *getdir(Biobuf*);
110 void install(char*, Arfile*, Arfile*, Arfile*, int);
111 void longt(Armember*);
112 int match(int, char**);
113 void mesg(int, char*);
114 Arfile *newtempfile(char*);
115 Armember *newmember(void);
116 void objsym(Sym*, void*);
117 int openar(char*, int, int);
121 void scanobj(Biobuf*, Arfile*, long);
122 void select(int*, long);
123 void setcom(void(*)(char*, int, char**));
124 void skip(Biobuf*, vlong);
125 int symcomp(void*, void*);
126 void trim(char*, char*, int);
129 void wrsym(Biobuf*, long, Arsymref*);
131 void rcmd(char*, int, char**); /* command processing */
132 void dcmd(char*, int, char**);
133 void xcmd(char*, int, char**);
134 void tcmd(char*, int, char**);
135 void pcmd(char*, int, char**);
136 void mcmd(char*, int, char**);
137 void qcmd(char*, int, char**);
138 void (*comfun)(char*, int, char**);
141 main(int argc, char *argv[])
145 Binit(&bout, 1, OWRITE);
148 for (cp = argv[1]; *cp; cp++) {
150 case 'a': aflag = 1; break;
151 case 'b': bflag = 1; break;
152 case 'c': cflag = 1; break;
153 case 'd': setcom(dcmd); break;
154 case 'i': bflag = 1; break;
156 strcpy(artemp, "vXXXXX");
157 strcpy(movtemp, "v1XXXXX");
158 strcpy(tailtemp, "v2XXXXX");
160 case 'm': setcom(mcmd); break;
161 case 'o': oflag = 1; break;
162 case 'p': setcom(pcmd); break;
163 case 'q': setcom(qcmd); break;
164 case 'r': setcom(rcmd); break;
165 case 't': setcom(tcmd); break;
166 case 'u': uflag = 1; break;
167 case 'v': vflag = 1; break;
168 case 'x': setcom(xcmd); break;
170 fprint(2, "ar: bad option `%c'\n", *cp);
174 if (aflag && bflag) {
175 fprint(2, "ar: only one of 'a' and 'b' can be specified\n");
179 trim(argv[2], poname, sizeof(poname));
187 fprint(2, "ar: one of [%s] must be specified\n", man);
195 (*comfun)(cp, argc, argv); /* do the command */
199 fprint(2, "ar: %s not found\n", *argv);
210 setcom(void (*fun)(char *, int, char**))
214 fprint(2, "ar: only one of [%s] allowed\n", man);
220 * perform the 'r' and 'u' commands
223 rcmd(char *arname, int count, char **files)
232 fd = openar(arname, ORDWR, 1);
234 Binit(&bar, fd, OREAD);
235 Bseek(&bar,seek(fd,0,1), 1);
237 astart = newtempfile(artemp);
240 for(i = 0; fd >= 0; i++) {
244 if (bamatch(file, poname)) { /* check for pivot */
245 aend = newtempfile(tailtemp);
248 /* pitch symdef file */
249 if (i == 0 && strcmp(file, symdef) == 0) {
250 skip(&bar, bp->size);
253 if (count && !match(count, files)) {
254 scanobj(&bar, ap, bp->size);
255 arcopy(&bar, ap, bp);
258 bfile = Bopen(file, OREAD);
261 fprint(2, "ar: cannot open %s\n", file);
262 scanobj(&bar, ap, bp->size);
263 arcopy(&bar, ap, bp);
266 d = dirfstat(Bfildes(bfile));
268 fprint(2, "ar: cannot stat %s: %r\n", file);
269 if (uflag && (d==nil || d->mtime <= bp->date)) {
270 scanobj(&bar, ap, bp->size);
271 arcopy(&bar, ap, bp);
277 skip(&bar, bp->size);
278 scanobj(bfile, ap, d->length);
280 armove(bfile, ap, bp);
285 /* copy in remaining files named on command line */
286 for (i = 0; i < count; i++) {
291 bfile = Bopen(file, OREAD);
293 fprint(2, "ar: %s cannot open\n", file);
296 d = dirfstat(Bfildes(bfile));
298 fprint(2, "can't stat %s\n", file);
300 scanobj(bfile, astart, d->length);
301 armove(bfile, astart, newmember());
308 install(arname, astart, 0, aend, 1); /* issue 'creating' msg */
310 install(arname, astart, 0, aend, 0);
314 dcmd(char *arname, int count, char **files)
321 fd = openar(arname, ORDWR, 0);
322 Binit(&bar, fd, OREAD);
323 Bseek(&bar,seek(fd,0,1), 1);
324 astart = newtempfile(artemp);
325 for (i = 0; bp = getdir(&bar); i++) {
326 if(match(count, files)) {
328 skip(&bar, bp->size);
329 if (strcmp(file, symdef) == 0)
331 } else if (i == 0 && strcmp(file, symdef) == 0)
332 skip(&bar, bp->size);
334 scanobj(&bar, astart, bp->size);
335 arcopy(&bar, astart, bp);
339 install(arname, astart, 0, 0, 0);
343 xcmd(char *arname, int count, char **files)
349 fd = openar(arname, OREAD, 0);
350 Binit(&bar, fd, OREAD);
351 Bseek(&bar,seek(fd,0,1), 1);
353 while (bp = getdir(&bar)) {
354 if(count == 0 || match(count, files)) {
355 mode = strtoul(bp->hdr.mode, 0, 8) & 0777;
356 f = create(file, OWRITE, mode);
358 fprint(2, "ar: %s cannot create\n", file);
359 skip(&bar, bp->size);
363 if (write(f, bp->member, bp->size) < 0)
369 if(dirwstat(file, &dx) < 0)
376 if (count && ++i >= count)
379 skip(&bar, bp->size);
386 pcmd(char *arname, int count, char **files)
391 fd = openar(arname, OREAD, 0);
392 Binit(&bar, fd, OREAD);
393 Bseek(&bar,seek(fd,0,1), 1);
394 while(bp = getdir(&bar)) {
395 if(count == 0 || match(count, files)) {
397 print("\n<%s>\n\n", file);
399 if (write(1, bp->member, bp->size) < 0)
402 skip(&bar, bp->size);
408 mcmd(char *arname, int count, char **files)
416 fd = openar(arname, ORDWR, 0);
417 Binit(&bar, fd, OREAD);
418 Bseek(&bar,seek(fd,0,1), 1);
419 astart = newtempfile(artemp);
420 amiddle = newtempfile(movtemp);
423 for (i = 0; bp = getdir(&bar); i++) {
424 if (bamatch(file, poname)) {
425 aend = newtempfile(tailtemp);
428 if(match(count, files)) {
430 scanobj(&bar, amiddle, bp->size);
431 arcopy(&bar, amiddle, bp);
434 * pitch the symdef file if it is at the beginning
435 * of the archive and we aren't inserting in front
436 * of it (ap == astart).
438 if (ap == astart && i == 0 && strcmp(file, symdef) == 0)
439 skip(&bar, bp->size);
441 scanobj(&bar, ap, bp->size);
442 arcopy(&bar, ap, bp);
446 if (poname[0] && aend == 0)
447 fprint(2, "ar: %s not found - files moved to end.\n", poname);
448 install(arname, astart, amiddle, aend, 0);
451 tcmd(char *arname, int count, char **files)
455 char name[ARNAMESIZE+1];
457 fd = openar(arname, OREAD, 0);
458 Binit(&bar, fd, OREAD);
459 Bseek(&bar,seek(fd,0,1), 1);
460 while(bp = getdir(&bar)) {
461 if(count == 0 || match(count, files)) {
464 trim(file, name, ARNAMESIZE);
465 Bprint(&bout, "%s\n", name);
467 skip(&bar, bp->size);
473 qcmd(char *arname, int count, char **files)
480 fprint(2, "ar: abi not allowed with q\n");
483 fd = openar(arname, ORDWR, 1);
486 fprint(2, "ar: creating %s\n", arname);
487 fd = arcreate(arname);
489 Binit(&bar, fd, OREAD);
490 Bseek(&bar,seek(fd,0,1), 1);
491 /* leave note group behind when writing archive; i.e. sidestep interrupts */
495 for(i=0; i<count && files[i]; i++) {
498 bfile = Bopen(file, OREAD);
500 fprint(2, "ar: %s cannot open\n", file);
503 armove(bfile, 0, bp);
504 if (!arwrite(fd, bp))
516 * extract the symbol references from an object file
519 scanobj(Biobuf *b, Arfile *ap, long size)
524 static int lastobj = -1;
526 if (!allobj) /* non-object file encountered */
530 if (obj < 0) { /* not an object file */
532 d = dirfstat(Bfildes(b));
533 if (d != nil && d->length == 0)
534 fprint(2, "ar: zero length file %s\n", file);
539 if (lastobj >= 0 && obj != lastobj) {
540 fprint(2, "ar: inconsistent object file %s\n", file);
546 if (!readar(b, obj, offset+size, 0)) {
547 fprint(2, "ar: invalid symbol reference in file %s\n", file);
553 objtraverse(objsym, ap);
557 * add text and data symbols to the symbol list
560 objsym(Sym *s, void *p)
566 if (s->type != 'T' && s->type != 'D')
569 as = (Arsymref*)armalloc(sizeof(Arsymref));
570 as->offset = ap->size;
572 as->name = armalloc(n+1);
573 strcpy(as->name, s->name);
574 if(s->type == 'T' && duplicate(as->name)) {
576 fprint(2, "duplicate text symbol: %s\n", as->name);
582 symdefsize += 4+(n+1)+1;
589 * Check the symbol table for duplicate text symbols
592 duplicate(char *name)
599 for(cp = name; *cp; h += *cp++)
605 for(p = hash[h]; p; p = p->next)
606 if(strcmp(p->name, name) == 0)
608 p = (Hashchain*) armalloc(sizeof(Hashchain));
616 * open an archive and validate its header
619 openar(char *arname, int mode, int errok)
624 fd = open(arname, mode);
626 if(read(fd, mbuf, SARMAG) != SARMAG || strncmp(mbuf, ARMAG, SARMAG)) {
627 fprint(2, "ar: %s not in archive format\n", arname);
631 fprint(2, "ar: cannot open %s: %r\n", arname);
638 * create an archive and set its header
641 arcreate(char *arname)
645 fd = create(arname, OWRITE, 0664);
647 fprint(2, "ar: cannot create %s: %r\n", arname);
650 if(write(fd, ARMAG, SARMAG) != SARMAG)
661 perror("ar: write error");
668 perror("ar: read error");
675 fprint(2, "ar: phase error at offset %d\n", offset);
682 fprint(2, "usage: ar [%s][%s] archive files ...\n", opt, man);
687 * read the header for the next archive member
694 static char name[ARNAMESIZE+1];
697 if(HEADER_IO(Bread, b, bp->hdr)) {
701 if(strncmp(bp->hdr.fmag, ARFMAG, sizeof(bp->hdr.fmag)) != 0)
702 phaseerr(Boffset(b));
703 strncpy(name, bp->hdr.name, sizeof(bp->hdr.name));
704 cp = name+sizeof(name)-1;
706 /* skip trailing spaces and (gnu-produced) slashes */
707 while(*--cp == ' ' || *cp == '/')
711 bp->date = strtol(bp->hdr.date, 0, 0);
712 bp->size = strtol(bp->hdr.size, 0, 0);
717 * Copy the file referenced by fd to the temp file
720 armove(Biobuf *b, Arfile *ap, Armember *bp)
725 d = dirfstat(Bfildes(b));
727 fprint(2, "ar: cannot stat %s\n", file);
730 trim(file, bp->hdr.name, sizeof(bp->hdr.name));
731 for (cp = strchr(bp->hdr.name, 0); /* blank pad on right */
732 cp < bp->hdr.name+sizeof(bp->hdr.name); cp++)
734 sprint(bp->hdr.date, "%-12ld", d->mtime);
735 sprint(bp->hdr.uid, "%-6d", 0);
736 sprint(bp->hdr.gid, "%-6d", 0);
737 sprint(bp->hdr.mode, "%-8lo", d->mode);
738 sprint(bp->hdr.size, "%-10lld", d->length);
739 strncpy(bp->hdr.fmag, ARFMAG, 2);
740 bp->size = d->length;
741 arread(b, bp, bp->size);
746 ap->size += d->length+SAR_HDR;
752 * Copy the archive member at the current offset into the temp file.
755 arcopy(Biobuf *b, Arfile *ap, Armember *bp)
765 ap->size += n+SAR_HDR;
770 * Skip an archive member
773 skip(Biobuf *bp, vlong len)
781 * Stream the three temp files to an archive
784 install(char *arname, Arfile *astart, Arfile *amiddle, Arfile *aend, int createflag)
788 if(allobj && dupfound) {
789 fprint(2, "%s not changed\n", arname);
792 /* leave note group behind when copying back; i.e. sidestep interrupts */
796 fprint(2, "ar: creating %s\n", arname);
797 fd = arcreate(arname);
803 arstream(fd, astart);
807 arstream(fd, amiddle);
826 Binit(&b, fd, OWRITE);
827 Bseek(&b,seek(fd,0,1), 0);
832 sprint(a.date, "%-12ld", time(0));
833 sprint(a.uid, "%-6d", 0);
834 sprint(a.gid, "%-6d", 0);
835 sprint(a.mode, "%-8lo", 0644L);
836 sprint(a.size, "%-10ld", len);
837 strncpy(a.fmag, ARFMAG, 2);
838 strcpy(a.name, symdef);
839 for (cp = strchr(a.name, 0); /* blank pad on right */
840 cp < a.name+sizeof(a.name); cp++)
842 if(HEADER_IO(Bwrite, &b, a))
847 wrsym(&b, len, astart->sym);
851 wrsym(&b, len, amiddle->sym);
852 len += amiddle->size;
855 wrsym(&b, len, aend->sym);
863 * Write the defined symbols to the symdef file
866 wrsym(Biobuf *bp, long offset, Arsymref *as)
872 off = as->offset+offset;
877 if (Bwrite(bp, as->name, as->len+1) != as->len+1)
884 * Check if the archive member matches an entry on the command line.
887 match(int count, char **files)
890 char name[ARNAMESIZE+1];
892 for(i=0; i<count; i++) {
895 trim(files[i], name, ARNAMESIZE);
896 if(strncmp(name, file, ARNAMESIZE) == 0) {
906 * compare the current member to the name of the pivot member
909 bamatch(char *file, char *pivot)
911 static int state = 0;
915 case 0: /* looking for position file */
917 if (strncmp(file, pivot, ARNAMESIZE) == 0)
920 if (strncmp(file, pivot, ARNAMESIZE) == 0) {
921 state = 2; /* found */
926 case 1: /* found - after previous file */
929 case 2: /* already found position file */
936 * output a message, if 'v' option was specified
939 mesg(int c, char *file)
943 Bprint(&bout, "%c - %s\n", c, file);
947 * isolate file name by stripping leading directories and trailing slashes
950 trim(char *s, char *buf, int n)
956 if (!p) { /* no slash in name */
960 if (p[1] != 0) { /* p+1 is first char of file name */
961 strncpy(buf, p+1, n);
964 *p = 0; /* strip trailing slash */
969 * utilities for printing long form of 't' command
989 pmode(strtoul(bp->hdr.mode, 0, 8));
990 Bprint(&bout, "%3ld/%1ld", strtol(bp->hdr.uid, 0, 0), strtol(bp->hdr.gid, 0, 0));
991 Bprint(&bout, "%7ld", bp->size);
992 cp = ctime(bp->date);
993 Bprint(&bout, " %-12.12s %-4.4s ", cp+4, cp+24);
996 int m1[] = { 1, ROWN, 'r', '-' };
997 int m2[] = { 1, WOWN, 'w', '-' };
998 int m3[] = { 2, SUID, 's', XOWN, 'x', '-' };
999 int m4[] = { 1, RGRP, 'r', '-' };
1000 int m5[] = { 1, WGRP, 'w', '-' };
1001 int m6[] = { 2, SGID, 's', XGRP, 'x', '-' };
1002 int m7[] = { 1, ROTH, 'r', '-' };
1003 int m8[] = { 1, WOTH, 'w', '-' };
1004 int m9[] = { 2, STXT, 't', XOTH, 'x', '-' };
1006 int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
1013 for(mp = &m[0]; mp < &m[9];)
1014 select(*mp++, mode);
1018 select(int *ap, long mode)
1023 while(--n>=0 && (mode&*ap++)==0)
1029 * Temp file I/O subsystem. We attempt to cache all three temp files in
1030 * core. When we run out of memory we spill to disk.
1031 * The I/O model assumes that temp files:
1032 * 1) are only written on the end
1033 * 2) are only read from the beginning
1034 * 3) are only read after all writing is complete.
1035 * The architecture uses one control block per temp file. Each control
1036 * block anchors a chain of buffers, each containing an archive member.
1039 newtempfile(char *name) /* allocate a file control block */
1043 ap = (Arfile *) armalloc(sizeof(Arfile));
1049 newmember(void) /* allocate a member buffer */
1051 return (Armember *)armalloc(sizeof(Armember));
1055 arread(Biobuf *b, Armember *bp, int n) /* read an image into a member buffer */
1059 bp->member = armalloc(n);
1060 i = Bread(b, bp->member, n);
1069 * insert a member buffer into the member chain
1072 arinsert(Arfile *ap, Armember *bp)
1078 ap->tail->next = bp;
1083 * stream the members in a temp file to the file referenced by 'fd'.
1086 arstream(int fd, Arfile *ap)
1092 if (ap->paged) { /* copy from disk */
1095 i = read(ap->fd, buf, sizeof(buf));
1100 if (write(fd, buf, i) != i)
1106 /* dump the in-core buffers */
1107 for (bp = ap->head; bp; bp = bp->next) {
1108 if (!arwrite(fd, bp))
1114 * write a member to 'fd'.
1117 arwrite(int fd, Armember *bp)
1121 if(HEADER_IO(write, fd, bp->hdr))
1126 if (write(fd, bp->member, len) != len)
1132 * Spill a member to a disk copy of a temp file
1140 if (!ap->paged) { /* not yet paged - create file */
1141 ap->fname = mktemp(ap->fname);
1142 ap->fd = create(ap->fname, ORDWR|ORCLOSE, 0600);
1144 fprint(2,"ar: can't create temp file\n");
1149 if (!arwrite(ap->fd, bp)) /* write member and free buffer block */
1151 ap->head = bp->next;
1153 ap->tail = bp->next;
1160 * try to reclaim space by paging. we try to spill the start, middle,
1161 * and end files, in that order. there is no particular reason for the
1167 if (astart && astart->head && page(astart))
1169 if (amiddle && amiddle->head && page(amiddle))
1171 if (aend && aend->head && page(aend))
1177 arfree(Arfile *ap) /* free a member buffer */
1179 Armember *bp, *next;
1181 for (bp = ap->head; bp; bp = next) {
1191 * allocate space for a control block or member buffer. if the malloc
1192 * fails we try to reclaim space by spilling previously allocated
1206 } while (getspace());
1207 fprint(2, "ar: out of memory\n");