ar.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * ar - portable (ascii) format version
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. #include <bio.h>
  15. #include <mach.h>
  16. #include <ar.h>
  17. /*
  18. * The algorithm uses up to 3 temp files. The "pivot member" is the
  19. * archive member specified by and a, b, or i option. The temp files are
  20. * astart - contains existing members up to and including the pivot member.
  21. * amiddle - contains new files moved or inserted behind the pivot.
  22. * aend - contains the existing members that follow the pivot member.
  23. * When all members have been processed, function 'install' streams the
  24. * temp files, in order, back into the archive.
  25. */
  26. typedef struct Arsymref
  27. {
  28. char *name;
  29. int type;
  30. int len;
  31. int64_t offset;
  32. struct Arsymref *next;
  33. } Arsymref;
  34. typedef struct Armember /* Temp file entry - one per archive member */
  35. {
  36. struct Armember *next;
  37. struct ar_hdr hdr;
  38. int32_t size;
  39. int32_t date;
  40. void *member;
  41. } Armember;
  42. typedef struct Arfile /* Temp file control block - one per tempfile */
  43. {
  44. int paged; /* set when some data paged to disk */
  45. char *fname; /* paging file name */
  46. int fd; /* paging file descriptor */
  47. int64_t size;
  48. Armember *head; /* head of member chain */
  49. Armember *tail; /* tail of member chain */
  50. Arsymref *sym; /* head of defined symbol chain */
  51. } Arfile;
  52. typedef struct Hashchain
  53. {
  54. char *name;
  55. struct Hashchain *next;
  56. } Hashchain;
  57. #define NHASH 1024
  58. /*
  59. * macro to portably read/write archive header.
  60. * 'cmd' is read/write/Bread/Bwrite, etc.
  61. */
  62. #define HEADER_IO(cmd, f, h) cmd(f, h.name, sizeof(h.name)) != sizeof(h.name)\
  63. || cmd(f, h.date, sizeof(h.date)) != sizeof(h.date)\
  64. || cmd(f, h.uid, sizeof(h.uid)) != sizeof(h.uid)\
  65. || cmd(f, h.gid, sizeof(h.gid)) != sizeof(h.gid)\
  66. || cmd(f, h.mode, sizeof(h.mode)) != sizeof(h.mode)\
  67. || cmd(f, h.size, sizeof(h.size)) != sizeof(h.size)\
  68. || 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";
  76. int aflag; /* command line flags */
  77. int bflag;
  78. int cflag;
  79. int oflag;
  80. int uflag;
  81. int vflag;
  82. Arfile *astart, *amiddle, *aend; /* Temp file control block pointers */
  83. int allobj = 1; /* set when all members are object files of the same type */
  84. int symdefsize; /* size of symdef file */
  85. int dupfound; /* flag for duplicate symbol */
  86. Hashchain *hash[NHASH]; /* hash table of text symbols */
  87. #define ARNAMESIZE sizeof(astart->tail->hdr.name)
  88. char poname[ARNAMESIZE+1]; /* name of pivot member */
  89. char *file; /* current file or member being worked on */
  90. Biobuf bout;
  91. Biobuf bar;
  92. void arcopy(Biobuf*, Arfile*, Armember*);
  93. int arcreate(char*);
  94. void arfree(Arfile*);
  95. void arinsert(Arfile*, Armember*);
  96. char *armalloc(int);
  97. void armove(Biobuf*, Arfile*, Armember*);
  98. void arread(Biobuf*, Armember*, int);
  99. void arstream(int, Arfile*);
  100. int arwrite(int, Armember*);
  101. int bamatch(char*, char*);
  102. int duplicate(char*);
  103. Armember *getdir(Biobuf*);
  104. int getspace(void);
  105. void install(char*, Arfile*, Arfile*, Arfile*, int);
  106. void int32_tt(Armember*);
  107. int match(int, char**);
  108. void mesg(int, char*);
  109. Arfile *newtempfile(char*);
  110. Armember *newmember(void);
  111. void objsym(Sym*, void*);
  112. int openar(char*, int, int);
  113. int page(Arfile*);
  114. void pmode(int32_t);
  115. void rl(int);
  116. void scanobj(Biobuf*, Arfile*, int32_t);
  117. void select(int*, int32_t);
  118. void setcom(void(*)(char*, int, char**));
  119. void skip(Biobuf*, int64_t);
  120. int symcomp(void *c, void*);
  121. void trim(char*, char*, int);
  122. void usage(void);
  123. void wrerr(void);
  124. void wrsym(Biobuf*, int32_t, Arsymref*);
  125. void rcmd(char*, int, char**); /* command processing */
  126. void dcmd(char*, int, char**);
  127. void xcmd(char*, int, char**);
  128. void tcmd(char*, int, char**);
  129. void pcmd(char*, int, char**);
  130. void mcmd(char*, int, char**);
  131. void qcmd(char*, int, char**);
  132. void (*comfun)(char*, int, char**);
  133. void
  134. main(int argc, char *argv[])
  135. {
  136. char *cp;
  137. Binit(&bout, 1, OWRITE);
  138. if(argc < 3)
  139. usage();
  140. for (cp = argv[1]; *cp; cp++) {
  141. switch(*cp) {
  142. case 'a': aflag = 1; break;
  143. case 'b': bflag = 1; break;
  144. case 'c': cflag = 1; break;
  145. case 'd': setcom(dcmd); break;
  146. case 'i': bflag = 1; break;
  147. case 'l':
  148. strcpy(artemp, "vXXXXX");
  149. strcpy(movtemp, "v1XXXXX");
  150. strcpy(tailtemp, "v2XXXXX");
  151. break;
  152. case 'm': setcom(mcmd); break;
  153. case 'o': oflag = 1; break;
  154. case 'p': setcom(pcmd); break;
  155. case 'q': setcom(qcmd); break;
  156. case 'r': setcom(rcmd); break;
  157. case 't': setcom(tcmd); break;
  158. case 'u': uflag = 1; break;
  159. case 'v': vflag = 1; break;
  160. case 'x': setcom(xcmd); break;
  161. default:
  162. fprint(2, "ar: bad option `%c'\n", *cp);
  163. exits("error");
  164. }
  165. }
  166. if (aflag && bflag) {
  167. fprint(2, "ar: only one of 'a' and 'b' can be specified\n");
  168. usage();
  169. }
  170. if(aflag || bflag) {
  171. trim(argv[2], poname, sizeof(poname));
  172. argv++;
  173. argc--;
  174. if(argc < 3)
  175. usage();
  176. }
  177. if(comfun == 0) {
  178. if(uflag == 0) {
  179. fprint(2, "ar: one of [%s] must be specified\n", man);
  180. usage();
  181. }
  182. setcom(rcmd);
  183. }
  184. cp = argv[2];
  185. argc -= 3;
  186. argv += 3;
  187. (*comfun)(cp, argc, argv); /* do the command */
  188. cp = 0;
  189. while (argc--) {
  190. if (*argv) {
  191. fprint(2, "ar: %s not found\n", *argv);
  192. cp = "error";
  193. }
  194. argv++;
  195. }
  196. exits(cp);
  197. }
  198. /*
  199. * select a command
  200. */
  201. void
  202. setcom(void (*fun)(char *, int, char**))
  203. {
  204. if(comfun != 0) {
  205. fprint(2, "ar: only one of [%s] allowed\n", man);
  206. usage();
  207. }
  208. comfun = fun;
  209. }
  210. /*
  211. * perform the 'r' and 'u' commands
  212. */
  213. void
  214. rcmd(char *arname, int count, char **files)
  215. {
  216. int fd;
  217. int i;
  218. Arfile *ap;
  219. Armember *bp;
  220. Dir *d;
  221. Biobuf *bfile;
  222. fd = openar(arname, ORDWR, 1);
  223. if (fd >= 0) {
  224. Binit(&bar, fd, OREAD);
  225. Bseek(&bar,seek(fd,0,1), 1);
  226. }
  227. astart = newtempfile(artemp);
  228. ap = astart;
  229. aend = 0;
  230. for(i = 0; fd >= 0; i++) {
  231. bp = getdir(&bar);
  232. if (!bp)
  233. break;
  234. if (bamatch(file, poname)) { /* check for pivot */
  235. aend = newtempfile(tailtemp);
  236. ap = aend;
  237. }
  238. /* pitch symdef file */
  239. if (i == 0 && strcmp(file, symdef) == 0) {
  240. skip(&bar, bp->size);
  241. continue;
  242. }
  243. if (count && !match(count, files)) {
  244. scanobj(&bar, ap, bp->size);
  245. arcopy(&bar, ap, bp);
  246. continue;
  247. }
  248. bfile = Bopen(file, OREAD);
  249. if (!bfile) {
  250. if (count != 0)
  251. fprint(2, "ar: cannot open %s\n", file);
  252. scanobj(&bar, ap, bp->size);
  253. arcopy(&bar, ap, bp);
  254. continue;
  255. }
  256. d = dirfstat(Bfildes(bfile));
  257. if(d == nil)
  258. fprint(2, "ar: cannot stat %s: %r\n", file);
  259. if (uflag && (d==nil || d->mtime <= bp->date)) {
  260. scanobj(&bar, ap, bp->size);
  261. arcopy(&bar, ap, bp);
  262. Bterm(bfile);
  263. free(d);
  264. continue;
  265. }
  266. mesg('r', file);
  267. skip(&bar, bp->size);
  268. scanobj(bfile, ap, d->length);
  269. free(d);
  270. armove(bfile, ap, bp);
  271. Bterm(bfile);
  272. }
  273. if(fd >= 0)
  274. close(fd);
  275. /* copy in remaining files named on command line */
  276. for (i = 0; i < count; i++) {
  277. file = files[i];
  278. if(file == 0)
  279. continue;
  280. files[i] = 0;
  281. bfile = Bopen(file, OREAD);
  282. if (!bfile)
  283. fprint(2, "ar: %s cannot open\n", file);
  284. else {
  285. mesg('a', file);
  286. d = dirfstat(Bfildes(bfile));
  287. if (d == nil)
  288. fprint(2, "can't stat %s\n", file);
  289. else {
  290. scanobj(bfile, astart, d->length);
  291. armove(bfile, astart, newmember());
  292. free(d);
  293. }
  294. Bterm(bfile);
  295. }
  296. }
  297. if(fd < 0 && !cflag)
  298. install(arname, astart, 0, aend, 1); /* issue 'creating' msg */
  299. else
  300. install(arname, astart, 0, aend, 0);
  301. }
  302. void
  303. dcmd(char *arname, int count, char **files)
  304. {
  305. Armember *bp;
  306. int fd, i;
  307. if (!count)
  308. return;
  309. fd = openar(arname, ORDWR, 0);
  310. Binit(&bar, fd, OREAD);
  311. Bseek(&bar,seek(fd,0,1), 1);
  312. astart = newtempfile(artemp);
  313. for (i = 0; (bp = getdir(&bar)) != nil; i++) {
  314. if(match(count, files)) {
  315. mesg('d', file);
  316. skip(&bar, bp->size);
  317. if (strcmp(file, symdef) == 0)
  318. allobj = 0;
  319. } else if (i == 0 && strcmp(file, symdef) == 0)
  320. skip(&bar, bp->size);
  321. else {
  322. scanobj(&bar, astart, bp->size);
  323. arcopy(&bar, astart, bp);
  324. }
  325. }
  326. close(fd);
  327. install(arname, astart, 0, 0, 0);
  328. }
  329. void
  330. xcmd(char *arname, int count, char **files)
  331. {
  332. int fd, f, mode, i;
  333. Armember *bp;
  334. Dir dx;
  335. fd = openar(arname, OREAD, 0);
  336. Binit(&bar, fd, OREAD);
  337. Bseek(&bar,seek(fd,0,1), 1);
  338. i = 0;
  339. while ((bp = getdir(&bar)) != nil) {
  340. if(count == 0 || match(count, files)) {
  341. mode = strtoul(bp->hdr.mode, 0, 8) & 0777;
  342. f = create(file, OWRITE, mode);
  343. if(f < 0) {
  344. fprint(2, "ar: %s cannot create\n", file);
  345. skip(&bar, bp->size);
  346. } else {
  347. mesg('x', file);
  348. arcopy(&bar, 0, bp);
  349. if (write(f, bp->member, bp->size) < 0)
  350. wrerr();
  351. if(oflag) {
  352. nulldir(&dx);
  353. dx.atime = bp->date;
  354. dx.mtime = bp->date;
  355. if(dirwstat(file, &dx) < 0)
  356. perror(file);
  357. }
  358. free(bp->member);
  359. close(f);
  360. }
  361. free(bp);
  362. if (count && ++i >= count)
  363. break;
  364. } else {
  365. skip(&bar, bp->size);
  366. free(bp);
  367. }
  368. }
  369. close(fd);
  370. }
  371. void
  372. pcmd(char *arname, int count, char **files)
  373. {
  374. int fd;
  375. Armember *bp;
  376. fd = openar(arname, OREAD, 0);
  377. Binit(&bar, fd, OREAD);
  378. Bseek(&bar,seek(fd,0,1), 1);
  379. while((bp = getdir(&bar)) != nil){
  380. if(count == 0 || match(count, files)) {
  381. if(vflag)
  382. print("\n<%s>\n\n", file);
  383. arcopy(&bar, 0, bp);
  384. if (write(1, bp->member, bp->size) < 0)
  385. wrerr();
  386. } else
  387. skip(&bar, bp->size);
  388. free(bp);
  389. }
  390. close(fd);
  391. }
  392. void
  393. mcmd(char *arname, int count, char **files)
  394. {
  395. int fd, i;
  396. Arfile *ap;
  397. Armember *bp;
  398. if (count == 0)
  399. return;
  400. fd = openar(arname, ORDWR, 0);
  401. Binit(&bar, fd, OREAD);
  402. Bseek(&bar,seek(fd,0,1), 1);
  403. astart = newtempfile(artemp);
  404. amiddle = newtempfile(movtemp);
  405. aend = 0;
  406. ap = astart;
  407. for (i = 0; (bp = getdir(&bar)) != nil; i++) {
  408. if (bamatch(file, poname)) {
  409. aend = newtempfile(tailtemp);
  410. ap = aend;
  411. }
  412. if(match(count, files)) {
  413. mesg('m', file);
  414. scanobj(&bar, amiddle, bp->size);
  415. arcopy(&bar, amiddle, bp);
  416. } else
  417. /*
  418. * pitch the symdef file if it is at the beginning
  419. * of the archive and we aren't inserting in front
  420. * of it (ap == astart).
  421. */
  422. if (ap == astart && i == 0 && strcmp(file, symdef) == 0)
  423. skip(&bar, bp->size);
  424. else {
  425. scanobj(&bar, ap, bp->size);
  426. arcopy(&bar, ap, bp);
  427. }
  428. }
  429. close(fd);
  430. if (poname[0] && aend == 0)
  431. fprint(2, "ar: %s not found - files moved to end.\n", poname);
  432. install(arname, astart, amiddle, aend, 0);
  433. }
  434. void
  435. tcmd(char *arname, int count, char **files)
  436. {
  437. int fd;
  438. Armember *bp;
  439. char name[ARNAMESIZE+1];
  440. fd = openar(arname, OREAD, 0);
  441. Binit(&bar, fd, OREAD);
  442. Bseek(&bar,seek(fd,0,1), 1);
  443. while((bp = getdir(&bar)) != nil){
  444. if(count == 0 || match(count, files)) {
  445. if(vflag)
  446. int32_tt(bp);
  447. trim(file, name, ARNAMESIZE);
  448. Bprint(&bout, "%s\n", name);
  449. }
  450. skip(&bar, bp->size);
  451. free(bp);
  452. }
  453. close(fd);
  454. }
  455. void
  456. qcmd(char *arname, int count, char **files)
  457. {
  458. int fd, i;
  459. Armember *bp;
  460. Biobuf *bfile;
  461. if(aflag || bflag) {
  462. fprint(2, "ar: abi not allowed with q\n");
  463. exits("error");
  464. }
  465. fd = openar(arname, ORDWR, 1);
  466. if (fd < 0) {
  467. if(!cflag)
  468. fprint(2, "ar: creating %s\n", arname);
  469. fd = arcreate(arname);
  470. }
  471. Binit(&bar, fd, OREAD);
  472. Bseek(&bar,seek(fd,0,1), 1);
  473. /* leave note group behind when writing archive; i.e. sidestep interrupts */
  474. rfork(RFNOTEG);
  475. Bseek(&bar, 0, 2);
  476. bp = newmember();
  477. for(i=0; i<count && files[i]; i++) {
  478. file = files[i];
  479. files[i] = 0;
  480. bfile = Bopen(file, OREAD);
  481. if(!bfile)
  482. fprint(2, "ar: %s cannot open\n", file);
  483. else {
  484. mesg('q', file);
  485. armove(bfile, 0, bp);
  486. if (!arwrite(fd, bp))
  487. wrerr();
  488. free(bp->member);
  489. bp->member = 0;
  490. Bterm(bfile);
  491. }
  492. }
  493. free(bp);
  494. close(fd);
  495. }
  496. /*
  497. * extract the symbol references from an object file
  498. */
  499. void
  500. scanobj(Biobuf *b, Arfile *ap, int32_t size)
  501. {
  502. int obj;
  503. int64_t offset;
  504. Dir *d;
  505. static int lastobj = -1;
  506. if (!allobj) /* non-object file encountered */
  507. return;
  508. offset = Boffset(b);
  509. obj = objtype(b, 0);
  510. if (obj < 0) { /* not an object file */
  511. allobj = 0;
  512. d = dirfstat(Bfildes(b));
  513. if (d != nil && d->length == 0)
  514. fprint(2, "ar: zero length file %s\n", file);
  515. free(d);
  516. Bseek(b, offset, 0);
  517. return;
  518. }
  519. if (lastobj >= 0 && obj != lastobj) {
  520. fprint(2, "ar: inconsistent object file %s\n", file);
  521. allobj = 0;
  522. Bseek(b, offset, 0);
  523. return;
  524. }
  525. lastobj = obj;
  526. if (!readar(b, obj, offset+size, 0)) {
  527. fprint(2, "ar: invalid symbol reference in file %s\n", file);
  528. allobj = 0;
  529. Bseek(b, offset, 0);
  530. return;
  531. }
  532. Bseek(b, offset, 0);
  533. objtraverse(objsym, ap);
  534. }
  535. /*
  536. * add text and data symbols to the symbol list
  537. */
  538. void
  539. objsym(Sym *s, void *p)
  540. {
  541. int n;
  542. Arsymref *as;
  543. Arfile *ap;
  544. if (s->type != 'T' && s->type != 'D')
  545. return;
  546. ap = (Arfile*)p;
  547. as = (Arsymref*)armalloc(sizeof(Arsymref));
  548. as->offset = ap->size;
  549. n = strlen(s->name);
  550. as->name = armalloc(n+1);
  551. strcpy(as->name, s->name);
  552. if(s->type == 'T' && duplicate(as->name)) {
  553. dupfound = 1;
  554. fprint(2, "duplicate text symbol: %s\n", as->name);
  555. free(as->name);
  556. free(as);
  557. return;
  558. }
  559. as->type = s->type;
  560. symdefsize += 4+(n+1)+1;
  561. as->len = n;
  562. as->next = ap->sym;
  563. ap->sym = as;
  564. }
  565. /*
  566. * Check the symbol table for duplicate text symbols
  567. */
  568. int
  569. duplicate(char *name)
  570. {
  571. Hashchain *p;
  572. char *cp;
  573. int h;
  574. h = 0;
  575. for(cp = name; *cp; h += *cp++)
  576. h *= 1119;
  577. if(h < 0)
  578. h = ~h;
  579. h %= NHASH;
  580. for(p = hash[h]; p; p = p->next)
  581. if(strcmp(p->name, name) == 0)
  582. return 1;
  583. p = (Hashchain*) armalloc(sizeof(Hashchain));
  584. p->next = hash[h];
  585. p->name = name;
  586. hash[h] = p;
  587. return 0;
  588. }
  589. /*
  590. * open an archive and validate its header
  591. */
  592. int
  593. openar(char *arname, int mode, int errok)
  594. {
  595. int fd;
  596. char mbuf[SARMAG];
  597. fd = open(arname, mode);
  598. if(fd >= 0){
  599. if(read(fd, mbuf, SARMAG) != SARMAG || strncmp(mbuf, ARMAG, SARMAG)) {
  600. fprint(2, "ar: %s not in archive format\n", arname);
  601. exits("error");
  602. }
  603. }else if(!errok){
  604. fprint(2, "ar: cannot open %s: %r\n", arname);
  605. exits("error");
  606. }
  607. return fd;
  608. }
  609. /*
  610. * create an archive and set its header
  611. */
  612. int
  613. arcreate(char *arname)
  614. {
  615. int fd;
  616. fd = create(arname, OWRITE, 0664);
  617. if(fd < 0){
  618. fprint(2, "ar: cannot create %s: %r\n", arname);
  619. exits("error");
  620. }
  621. if(write(fd, ARMAG, SARMAG) != SARMAG)
  622. wrerr();
  623. return fd;
  624. }
  625. /*
  626. * error handling
  627. */
  628. void
  629. wrerr(void)
  630. {
  631. perror("ar: write error");
  632. exits("error");
  633. }
  634. void
  635. rderr(void)
  636. {
  637. perror("ar: read error");
  638. exits("error");
  639. }
  640. void
  641. phaseerr(int offset)
  642. {
  643. fprint(2, "ar: phase error at offset %d\n", offset);
  644. exits("error");
  645. }
  646. void
  647. usage(void)
  648. {
  649. fprint(2, "usage: ar [%s][%s] archive files ...\n", opt, man);
  650. exits("error");
  651. }
  652. /*
  653. * read the header for the next archive member
  654. */
  655. Armember *
  656. getdir(Biobuf *b)
  657. {
  658. Armember *bp;
  659. char *cp;
  660. static char name[ARNAMESIZE+1];
  661. bp = newmember();
  662. if(HEADER_IO(Bread, b, bp->hdr)) {
  663. free(bp);
  664. return 0;
  665. }
  666. if(strncmp(bp->hdr.fmag, ARFMAG, sizeof(bp->hdr.fmag)) != 0)
  667. phaseerr(Boffset(b));
  668. strncpy(name, bp->hdr.name, sizeof(bp->hdr.name));
  669. cp = name+sizeof(name)-1;
  670. *cp = '\0';
  671. /* skip trailing spaces and (gnu-produced) slashes */
  672. while(*--cp == ' ' || *cp == '/')
  673. ;
  674. cp[1] = '\0';
  675. file = name;
  676. bp->date = strtol(bp->hdr.date, 0, 0);
  677. bp->size = strtol(bp->hdr.size, 0, 0);
  678. return bp;
  679. }
  680. /*
  681. * Copy the file referenced by fd to the temp file
  682. */
  683. void
  684. armove(Biobuf *b, Arfile *ap, Armember *bp)
  685. {
  686. char *cp;
  687. Dir *d;
  688. d = dirfstat(Bfildes(b));
  689. if (d == nil) {
  690. fprint(2, "ar: cannot stat %s\n", file);
  691. return;
  692. }
  693. trim(file, bp->hdr.name, sizeof(bp->hdr.name));
  694. for (cp = strchr(bp->hdr.name, 0); /* blank pad on right */
  695. cp < bp->hdr.name+sizeof(bp->hdr.name); cp++)
  696. *cp = ' ';
  697. sprint(bp->hdr.date, "%-12ld", d->mtime);
  698. sprint(bp->hdr.uid, "%-6d", 0);
  699. sprint(bp->hdr.gid, "%-6d", 0);
  700. sprint(bp->hdr.mode, "%-8lo", d->mode);
  701. sprint(bp->hdr.size, "%-10lld", d->length);
  702. strncpy(bp->hdr.fmag, ARFMAG, 2);
  703. bp->size = d->length;
  704. arread(b, bp, bp->size);
  705. if (d->length&0x01)
  706. d->length++;
  707. if (ap) {
  708. arinsert(ap, bp);
  709. ap->size += d->length+SAR_HDR;
  710. }
  711. free(d);
  712. }
  713. /*
  714. * Copy the archive member at the current offset into the temp file.
  715. */
  716. void
  717. arcopy(Biobuf *b, Arfile *ap, Armember *bp)
  718. {
  719. int32_t n;
  720. n = bp->size;
  721. if (n & 01)
  722. n++;
  723. arread(b, bp, n);
  724. if (ap) {
  725. arinsert(ap, bp);
  726. ap->size += n+SAR_HDR;
  727. }
  728. }
  729. /*
  730. * Skip an archive member
  731. */
  732. void
  733. skip(Biobuf *bp, int64_t len)
  734. {
  735. if (len & 01)
  736. len++;
  737. Bseek(bp, len, 1);
  738. }
  739. /*
  740. * Stream the three temp files to an archive
  741. */
  742. void
  743. install(char *arname, Arfile *astart, Arfile *amiddle, Arfile *aend,
  744. int createflag)
  745. {
  746. int fd;
  747. if(allobj && dupfound) {
  748. fprint(2, "%s not changed\n", arname);
  749. return;
  750. }
  751. /* leave note group behind when copying back; i.e. sidestep interrupts */
  752. rfork(RFNOTEG);
  753. if(createflag)
  754. fprint(2, "ar: creating %s\n", arname);
  755. fd = arcreate(arname);
  756. if(allobj)
  757. rl(fd);
  758. if (astart) {
  759. arstream(fd, astart);
  760. arfree(astart);
  761. }
  762. if (amiddle) {
  763. arstream(fd, amiddle);
  764. arfree(amiddle);
  765. }
  766. if (aend) {
  767. arstream(fd, aend);
  768. arfree(aend);
  769. }
  770. close(fd);
  771. }
  772. void
  773. rl(int fd)
  774. {
  775. Biobuf b;
  776. char *cp;
  777. struct ar_hdr a;
  778. int32_t len;
  779. Binit(&b, fd, OWRITE);
  780. Bseek(&b,seek(fd,0,1), 0);
  781. len = symdefsize;
  782. if(len&01)
  783. len++;
  784. sprint(a.date, "%-12ld", time(0));
  785. sprint(a.uid, "%-6d", 0);
  786. sprint(a.gid, "%-6d", 0);
  787. sprint(a.mode, "%-8lo", 0644L);
  788. sprint(a.size, "%-10ld", len);
  789. strncpy(a.fmag, ARFMAG, 2);
  790. strcpy(a.name, symdef);
  791. for (cp = strchr(a.name, 0); /* blank pad on right */
  792. cp < a.name+sizeof(a.name); cp++)
  793. *cp = ' ';
  794. if(HEADER_IO(Bwrite, &b, a))
  795. wrerr();
  796. len += Boffset(&b);
  797. if (astart) {
  798. wrsym(&b, len, astart->sym);
  799. len += astart->size;
  800. }
  801. if(amiddle) {
  802. wrsym(&b, len, amiddle->sym);
  803. len += amiddle->size;
  804. }
  805. if(aend)
  806. wrsym(&b, len, aend->sym);
  807. if(symdefsize&0x01)
  808. Bputc(&b, 0);
  809. Bterm(&b);
  810. }
  811. /*
  812. * Write the defined symbols to the symdef file
  813. */
  814. void
  815. wrsym(Biobuf *bp, int32_t offset, Arsymref *as)
  816. {
  817. int off;
  818. while(as) {
  819. Bputc(bp, as->type);
  820. off = as->offset+offset;
  821. Bputc(bp, off);
  822. Bputc(bp, off>>8);
  823. Bputc(bp, off>>16);
  824. Bputc(bp, off>>24);
  825. if (Bwrite(bp, as->name, as->len+1) != as->len+1)
  826. wrerr();
  827. as = as->next;
  828. }
  829. }
  830. /*
  831. * Check if the archive member matches an entry on the command line.
  832. */
  833. int
  834. match(int count, char **files)
  835. {
  836. int i;
  837. char name[ARNAMESIZE+1];
  838. for(i=0; i<count; i++) {
  839. if(files[i] == 0)
  840. continue;
  841. trim(files[i], name, ARNAMESIZE);
  842. if(strncmp(name, file, ARNAMESIZE) == 0) {
  843. file = files[i];
  844. files[i] = 0;
  845. return 1;
  846. }
  847. }
  848. return 0;
  849. }
  850. /*
  851. * compare the current member to the name of the pivot member
  852. */
  853. int
  854. bamatch(char *file, char *pivot)
  855. {
  856. static int state = 0;
  857. switch(state)
  858. {
  859. case 0: /* looking for position file */
  860. if (aflag) {
  861. if (strncmp(file, pivot, ARNAMESIZE) == 0)
  862. state = 1;
  863. } else if (bflag) {
  864. if (strncmp(file, pivot, ARNAMESIZE) == 0) {
  865. state = 2; /* found */
  866. return 1;
  867. }
  868. }
  869. break;
  870. case 1: /* found - after previous file */
  871. state = 2;
  872. return 1;
  873. case 2: /* already found position file */
  874. break;
  875. }
  876. return 0;
  877. }
  878. /*
  879. * output a message, if 'v' option was specified
  880. */
  881. void
  882. mesg(int c, char *file)
  883. {
  884. if(vflag)
  885. Bprint(&bout, "%c - %s\n", c, file);
  886. }
  887. /*
  888. * isolate file name by stripping leading directories and trailing slashes
  889. */
  890. void
  891. trim(char *s, char *buf, int n)
  892. {
  893. char *p;
  894. for(;;) {
  895. p = strrchr(s, '/');
  896. if (!p) { /* no slash in name */
  897. strncpy(buf, s, n);
  898. return;
  899. }
  900. if (p[1] != 0) { /* p+1 is first char of file name */
  901. strncpy(buf, p+1, n);
  902. return;
  903. }
  904. *p = 0; /* strip trailing slash */
  905. }
  906. }
  907. /*
  908. * utilities for printing int32_t form of 't' command
  909. */
  910. #define SUID 04000
  911. #define SGID 02000
  912. #define ROWN 0400
  913. #define WOWN 0200
  914. #define XOWN 0100
  915. #define RGRP 040
  916. #define WGRP 020
  917. #define XGRP 010
  918. #define ROTH 04
  919. #define WOTH 02
  920. #define XOTH 01
  921. #define STXT 01000
  922. void
  923. int32_tt(Armember *bp)
  924. {
  925. char *cp;
  926. pmode(strtoul(bp->hdr.mode, 0, 8));
  927. Bprint(&bout, "%3ld/%1ld", strtol(bp->hdr.uid, 0, 0), strtol(bp->hdr.gid, 0, 0));
  928. Bprint(&bout, "%7ld", bp->size);
  929. cp = ctime(bp->date);
  930. Bprint(&bout, " %-12.12s %-4.4s ", cp+4, cp+24);
  931. }
  932. int m1[] = { 1, ROWN, 'r', '-' };
  933. int m2[] = { 1, WOWN, 'w', '-' };
  934. int m3[] = { 2, SUID, 's', XOWN, 'x', '-' };
  935. int m4[] = { 1, RGRP, 'r', '-' };
  936. int m5[] = { 1, WGRP, 'w', '-' };
  937. int m6[] = { 2, SGID, 's', XGRP, 'x', '-' };
  938. int m7[] = { 1, ROTH, 'r', '-' };
  939. int m8[] = { 1, WOTH, 'w', '-' };
  940. int m9[] = { 2, STXT, 't', XOTH, 'x', '-' };
  941. int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
  942. void
  943. pmode(int32_t mode)
  944. {
  945. int **mp;
  946. for(mp = &m[0]; mp < &m[9];)
  947. select(*mp++, mode);
  948. }
  949. void
  950. select(int *ap, int32_t mode)
  951. {
  952. int n;
  953. n = *ap++;
  954. while(--n>=0 && (mode&*ap++)==0)
  955. ap++;
  956. Bputc(&bout, *ap);
  957. }
  958. /*
  959. * Temp file I/O subsystem. We attempt to cache all three temp files in
  960. * core. When we run out of memory we spill to disk.
  961. * The I/O model assumes that temp files:
  962. * 1) are only written on the end
  963. * 2) are only read from the beginning
  964. * 3) are only read after all writing is complete.
  965. * The architecture uses one control block per temp file. Each control
  966. * block anchors a chain of buffers, each containing an archive member.
  967. */
  968. Arfile *
  969. newtempfile(char *name) /* allocate a file control block */
  970. {
  971. Arfile *ap;
  972. ap = (Arfile *) armalloc(sizeof(Arfile));
  973. ap->fname = name;
  974. return ap;
  975. }
  976. Armember *
  977. newmember(void) /* allocate a member buffer */
  978. {
  979. return (Armember *)armalloc(sizeof(Armember));
  980. }
  981. void
  982. arread(Biobuf *b, Armember *bp, int n) /* read an image into a member buffer */
  983. {
  984. int i;
  985. bp->member = armalloc(n);
  986. i = Bread(b, bp->member, n);
  987. if (i < 0) {
  988. free(bp->member);
  989. bp->member = 0;
  990. rderr();
  991. }
  992. }
  993. /*
  994. * insert a member buffer into the member chain
  995. */
  996. void
  997. arinsert(Arfile *ap, Armember *bp)
  998. {
  999. bp->next = 0;
  1000. if (!ap->tail)
  1001. ap->head = bp;
  1002. else
  1003. ap->tail->next = bp;
  1004. ap->tail = bp;
  1005. }
  1006. /*
  1007. * stream the members in a temp file to the file referenced by 'fd'.
  1008. */
  1009. void
  1010. arstream(int fd, Arfile *ap)
  1011. {
  1012. Armember *bp;
  1013. int i;
  1014. char buf[8192];
  1015. if (ap->paged) { /* copy from disk */
  1016. seek(ap->fd, 0, 0);
  1017. for (;;) {
  1018. i = read(ap->fd, buf, sizeof(buf));
  1019. if (i < 0)
  1020. rderr();
  1021. if (i == 0)
  1022. break;
  1023. if (write(fd, buf, i) != i)
  1024. wrerr();
  1025. }
  1026. close(ap->fd);
  1027. ap->paged = 0;
  1028. }
  1029. /* dump the in-core buffers */
  1030. for (bp = ap->head; bp; bp = bp->next) {
  1031. if (!arwrite(fd, bp))
  1032. wrerr();
  1033. }
  1034. }
  1035. /*
  1036. * write a member to 'fd'.
  1037. */
  1038. int
  1039. arwrite(int fd, Armember *bp)
  1040. {
  1041. int len;
  1042. if(HEADER_IO(write, fd, bp->hdr))
  1043. return 0;
  1044. len = bp->size;
  1045. if (len & 01)
  1046. len++;
  1047. if (write(fd, bp->member, len) != len)
  1048. return 0;
  1049. return 1;
  1050. }
  1051. /*
  1052. * Spill a member to a disk copy of a temp file
  1053. */
  1054. int
  1055. page(Arfile *ap)
  1056. {
  1057. Armember *bp;
  1058. bp = ap->head;
  1059. if (!ap->paged) { /* not yet paged - create file */
  1060. ap->fname = mktemp(ap->fname);
  1061. ap->fd = create(ap->fname, ORDWR|ORCLOSE, 0600);
  1062. if (ap->fd < 0) {
  1063. fprint(2,"ar: can't create temp file\n");
  1064. return 0;
  1065. }
  1066. ap->paged = 1;
  1067. }
  1068. if (!arwrite(ap->fd, bp)) /* write member and free buffer block */
  1069. return 0;
  1070. ap->head = bp->next;
  1071. if (ap->tail == bp)
  1072. ap->tail = bp->next;
  1073. free(bp->member);
  1074. free(bp);
  1075. return 1;
  1076. }
  1077. /*
  1078. * try to reclaim space by paging. we try to spill the start, middle,
  1079. * and end files, in that order. there is no particular reason for the
  1080. * ordering.
  1081. */
  1082. int
  1083. getspace(void)
  1084. {
  1085. if (astart && astart->head && page(astart))
  1086. return 1;
  1087. if (amiddle && amiddle->head && page(amiddle))
  1088. return 1;
  1089. if (aend && aend->head && page(aend))
  1090. return 1;
  1091. return 0;
  1092. }
  1093. void
  1094. arfree(Arfile *ap) /* free a member buffer */
  1095. {
  1096. Armember *bp, *next;
  1097. for (bp = ap->head; bp; bp = next) {
  1098. next = bp->next;
  1099. if (bp->member)
  1100. free(bp->member);
  1101. free(bp);
  1102. }
  1103. free(ap);
  1104. }
  1105. /*
  1106. * allocate space for a control block or member buffer. if the malloc
  1107. * fails we try to reclaim space by spilling previously allocated
  1108. * member buffers.
  1109. */
  1110. char *
  1111. armalloc(int n)
  1112. {
  1113. char *cp;
  1114. do {
  1115. cp = malloc(n);
  1116. if (cp) {
  1117. memset(cp, 0, n);
  1118. return cp;
  1119. }
  1120. } while (getspace());
  1121. fprint(2, "ar: out of memory\n");
  1122. exits("malloc");
  1123. return 0;
  1124. }