Annotation of src/usr.bin/make/arch.c, Revision 1.67
1.44 espie 1: /* $OpenPackages$ */
1.67 ! espie 2: /* $OpenBSD: arch.c,v 1.66 2007/09/16 12:09:36 espie Exp $ */
1.9 millert 3: /* $NetBSD: arch.c,v 1.17 1996/11/06 17:58:59 christos Exp $ */
1.1 deraadt 4:
5: /*
1.44 espie 6: * Copyright (c) 1999,2000 Marc Espie.
1.38 espie 7: *
8: * Extensive code changes for the OpenBSD project.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
23: * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30: */
31: /*
1.9 millert 32: * Copyright (c) 1988, 1989, 1990, 1993
33: * The Regents of the University of California. All rights reserved.
1.1 deraadt 34: * Copyright (c) 1989 by Berkeley Softworks
35: * All rights reserved.
36: *
37: * This code is derived from software contributed to Berkeley by
38: * Adam de Boor.
39: *
40: * Redistribution and use in source and binary forms, with or without
41: * modification, are permitted provided that the following conditions
42: * are met:
43: * 1. Redistributions of source code must retain the above copyright
44: * notice, this list of conditions and the following disclaimer.
45: * 2. Redistributions in binary form must reproduce the above copyright
46: * notice, this list of conditions and the following disclaimer in the
47: * documentation and/or other materials provided with the distribution.
1.53 millert 48: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 49: * may be used to endorse or promote products derived from this software
50: * without specific prior written permission.
51: *
52: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62: * SUCH DAMAGE.
63: */
64:
1.45 espie 65: /*
1.1 deraadt 66: * Once again, cacheing/hashing comes into play in the manipulation
67: * of archives. The first time an archive is referenced, all of its members'
68: * headers are read and hashed and the archive closed again. All hashed
1.45 espie 69: * archives are kept in a hash (archives) which is searched each time
70: * an archive member is referenced.
1.1 deraadt 71: *
72: */
73:
1.46 espie 74: #include <sys/param.h>
1.45 espie 75: #include <ar.h>
76: #include <assert.h>
77: #include <ctype.h>
78: #include <fcntl.h>
79: #include <limits.h>
80: #include <stddef.h>
81: #include <stdio.h>
1.55 espie 82: #include <stdint.h>
1.45 espie 83: #include <stdlib.h>
84: #include <string.h>
85: #include <unistd.h>
86: #include "ohash.h"
87: #include "config.h"
88: #include "defines.h"
89: #include "dir.h"
1.63 espie 90: #include "direxpand.h"
1.45 espie 91: #include "arch.h"
92: #include "var.h"
93: #include "targ.h"
94: #include "memory.h"
95: #include "gnode.h"
96: #include "timestamp.h"
97: #include "lst.h"
1.44 espie 98:
1.48 espie 99: #ifndef PATH_MAX
100: # ifdef MAXPATHLEN
101: # define PATH_MAX (MAXPATHLEN+1)
102: # else
103: # define PATH_MAX 1024
104: # endif
105: #endif
106:
1.14 espie 107: #ifdef TARGET_MACHINE
108: #undef MACHINE
109: #define MACHINE TARGET_MACHINE
110: #endif
111: #ifdef TARGET_MACHINE_ARCH
112: #undef MACHINE_ARCH
113: #define MACHINE_ARCH TARGET_MACHINE_ARCH
114: #endif
115:
1.64 espie 116: static struct ohash archives; /* Archives we've already examined. */
1.1 deraadt 117:
1.35 espie 118: typedef struct Arch_ {
1.64 espie 119: struct ohash members; /* All the members of this archive, as
120: * struct arch_member entries. */
121: char name[1]; /* Archive name. */
1.1 deraadt 122: } Arch;
123:
1.35 espie 124: /* Used to get to ar's field sizes. */
125: static struct ar_hdr *dummy;
1.44 espie 126: #define AR_NAME_SIZE (sizeof(dummy->ar_name))
1.35 espie 127: #define AR_DATE_SIZE (sizeof(dummy->ar_date))
128:
1.44 espie 129: /* Each archive member is tied to an arch_member structure,
1.35 espie 130: * suitable for hashing. */
131: struct arch_member {
1.64 espie 132: TIMESTAMP mtime; /* Member modification date. */
133: char date[AR_DATE_SIZE+1];
1.44 espie 134: /* Same, before conversion to numeric value. */
1.64 espie 135: char name[1]; /* Member name. */
1.35 espie 136: };
137:
1.43 espie 138: static struct ohash_info members_info = {
1.64 espie 139: offsetof(struct arch_member, name), NULL,
140: hash_alloc, hash_free, element_alloc
1.35 espie 141: };
142:
1.43 espie 143: static struct ohash_info arch_info = {
1.64 espie 144: offsetof(Arch, name), NULL, hash_alloc, hash_free, element_alloc
1.38 espie 145: };
146:
1.44 espie 147:
148:
149: static struct arch_member *new_arch_member(struct ar_hdr *, const char *);
150: static TIMESTAMP mtime_of_member(struct arch_member *);
151: static long field2long(const char *, size_t);
152: static Arch *read_archive(const char *, const char *);
1.35 espie 153:
1.16 espie 154: #ifdef CLEANUP
1.44 espie 155: static void ArchFree(Arch *);
1.16 espie 156: #endif
1.45 espie 157: static TIMESTAMP ArchMTimeMember(const char *, const char *, bool);
1.44 espie 158: static FILE *ArchFindMember(const char *, const char *, struct ar_hdr *, const char *);
159: static void ArchTouch(const char *, const char *);
1.7 niklas 160: #if defined(__svr4__) || defined(__SVR4) || \
1.51 matthieu 161: (defined(__OpenBSD__) && defined(__ELF__))
1.6 briggs 162: #define SVR4ARCHIVES
1.40 espie 163: #endif
164:
165: #ifdef SVR4ARCHIVES
166: struct SVR4namelist {
1.64 espie 167: char *fnametab; /* Extended name table strings */
168: size_t fnamesize; /* Size of the string table */
1.40 espie 169: };
170:
171: static const char *svr4list = "Archive list";
172:
1.54 espie 173: static char *ArchSVR4Entry(struct SVR4namelist *, const char *, size_t, FILE *);
1.6 briggs 174: #endif
1.1 deraadt 175:
1.35 espie 176: static struct arch_member *
1.54 espie 177: new_arch_member(struct ar_hdr *hdr, const char *name)
1.35 espie 178: {
1.64 espie 179: const char *end = NULL;
180: struct arch_member *n;
1.35 espie 181:
1.64 espie 182: n = ohash_create_entry(&members_info, name, &end);
183: /* XXX ar entries are NOT null terminated. */
184: memcpy(n->date, &(hdr->ar_date), AR_DATE_SIZE);
185: n->date[AR_DATE_SIZE] = '\0';
186: /* Don't compute mtime before it is needed. */
187: ts_set_out_of_date(n->mtime);
188: return n;
1.35 espie 189: }
190:
191: static TIMESTAMP
1.54 espie 192: mtime_of_member(struct arch_member *m)
1.35 espie 193: {
1.64 espie 194: if (is_out_of_date(m->mtime))
195: ts_set_from_time_t((time_t) strtol(m->date, NULL, 10),
196: m->mtime);
197: return m->mtime;
1.35 espie 198: }
199:
1.16 espie 200: #ifdef CLEANUP
1.1 deraadt 201: static void
1.54 espie 202: ArchFree(Arch *a)
1.1 deraadt 203: {
1.64 espie 204: /* Free memory from hash entries */
1.65 espie 205: free_hash(&a->members);
1.64 espie 206: free(a);
1.1 deraadt 207: }
1.16 espie 208: #endif
1.9 millert 209:
1.1 deraadt 210:
211:
1.45 espie 212: /* Side-effects: Some nodes may be created. */
213: bool
1.54 espie 214: Arch_ParseArchive(char **linePtr, /* Pointer to start of specification */
215: Lst nodeLst, /* Lst on which to place the nodes */
216: SymTable *ctxt) /* Context in which to expand variables */
1.1 deraadt 217: {
1.64 espie 218: char *cp; /* Pointer into line */
219: GNode *gn; /* New node */
220: char *libName; /* Library-part of specification */
221: char *memberName; /* Member-part of specification */
222: char nameBuf[MAKE_BSIZE]; /* temporary place for node name */
223: char saveChar; /* Ending delimiter of member-name */
224: bool subLibName; /* true if libName should have/had
225: * variable substitution performed on it */
226:
227: libName = *linePtr;
228:
229: subLibName = false;
230:
231: for (cp = libName; *cp != '(' && *cp != '\0';) {
232: if (*cp == '$') {
233: if (!Var_ParseSkip(&cp, ctxt))
234: return false;
235: subLibName = true;
236: } else
237: cp++;
238: }
239:
240: *cp++ = '\0';
241: if (subLibName)
242: libName = Var_Subst(libName, ctxt, true);
243:
244: for (;;) {
245: /* First skip to the start of the member's name, mark that
246: * place and skip to the end of it (either white-space or
247: * a close paren). */
248: bool doSubst = false; /* true if need to substitute in
249: * memberName */
250:
251: while (isspace(*cp))
252: cp++;
253: memberName = cp;
254: while (*cp != '\0' && *cp != ')' && !isspace(*cp)) {
255: if (*cp == '$') {
256: if (!Var_ParseSkip(&cp, ctxt))
257: return false;
258: doSubst = true;
259: } else
260: cp++;
261: }
262:
263: /* If the specification ends without a closing parenthesis,
264: * chances are there's something wrong (like a missing
265: * backslash), so it's better to return failure than allow such
266: * things to happen. */
267: if (*cp == '\0') {
268: printf("No closing parenthesis in archive specification\n");
269: return false;
270: }
1.1 deraadt 271:
1.64 espie 272: /* If we didn't move anywhere, we must be done. */
273: if (cp == memberName)
274: break;
1.1 deraadt 275:
1.64 espie 276: saveChar = *cp;
277: *cp = '\0';
1.1 deraadt 278:
1.64 espie 279: /* XXX: This should be taken care of intelligently by
280: * SuffExpandChildren, both for the archive and the member
281: * portions. */
282:
283: /* If member contains variables, try and substitute for them.
284: * This will slow down archive specs with dynamic sources, of
285: * course, since we'll be (non-)substituting them three times,
286: * but them's the breaks -- we need to do this since
287: * SuffExpandChildren calls us, otherwise we could assume the
288: * thing would be taken care of later. */
289: if (doSubst) {
290: char *buf;
291: char *sacrifice;
292: char *oldMemberName = memberName;
293: size_t length;
294:
295: memberName = Var_Subst(memberName, ctxt, true);
296:
297: /* Now form an archive spec and recurse to deal with
298: * nested variables and multi-word variable values....
299: * The results are just placed at the end of the
300: * nodeLst we're returning. */
301: length = strlen(memberName)+strlen(libName)+3;
302: buf = sacrifice = emalloc(length);
303:
304: snprintf(buf, length, "%s(%s)", libName, memberName);
305:
306: if (strchr(memberName, '$') &&
307: strcmp(memberName, oldMemberName) == 0) {
308: /* Must contain dynamic sources, so we can't
309: * deal with it now. Just create an ARCHV node
310: * for the thing and let SuffExpandChildren
311: * handle it... */
312: gn = Targ_FindNode(buf, TARG_CREATE);
313:
314: if (gn == NULL) {
315: free(buf);
316: return false;
317: } else {
318: gn->type |= OP_ARCHV;
319: Lst_AtEnd(nodeLst, gn);
320: }
321: } else if (!Arch_ParseArchive(&sacrifice, nodeLst,
322: ctxt)) {
323: /* Error in nested call -- free buffer and
324: * return false ourselves. */
325: free(buf);
326: return false;
327: }
328: /* Free buffer and continue with our work. */
329: free(buf);
330: } else if (Dir_HasWildcards(memberName)) {
331: LIST members;
332: char *member;
333:
334: Lst_Init(&members);
335:
336: Dir_Expand(memberName, dirSearchPath, &members);
337: while ((member = (char *)Lst_DeQueue(&members))
338: != NULL) {
339: snprintf(nameBuf, MAKE_BSIZE, "%s(%s)",
340: libName, member);
341: free(member);
342: gn = Targ_FindNode(nameBuf, TARG_CREATE);
343: /* We've found the node, but have to make sure
344: * the rest of the world knows it's an archive
345: * member, without having to constantly check
346: * for parentheses, so we type the thing with
347: * the OP_ARCHV bit before we place it on the
348: * end of the provided list. */
349: gn->type |= OP_ARCHV;
350: Lst_AtEnd(nodeLst, gn);
351: }
1.1 deraadt 352: } else {
1.64 espie 353: snprintf(nameBuf, MAKE_BSIZE, "%s(%s)", libName,
354: memberName);
355: gn = Targ_FindNode(nameBuf, TARG_CREATE);
356: /* We've found the node, but have to make sure the rest
357: * of the world knows it's an archive member, without
358: * having to constantly check for parentheses, so we
359: * type the thing with the OP_ARCHV bit before we place
360: * it on the end of the provided list. */
361: gn->type |= OP_ARCHV;
362: Lst_AtEnd(nodeLst, gn);
1.1 deraadt 363: }
1.64 espie 364: if (doSubst)
365: free(memberName);
366:
367: *cp = saveChar;
1.1 deraadt 368: }
1.9 millert 369:
1.64 espie 370: /* If substituted libName, free it now, since we need it no longer. */
371: if (subLibName)
372: free(libName);
373:
374: /* We promised the pointer would be set up at the next non-space, so
375: * we must advance cp there before setting *linePtr... (note that on
376: * entrance to the loop, cp is guaranteed to point at a ')') */
377: do {
378: cp++;
379: } while (isspace(*cp));
1.1 deraadt 380:
1.64 espie 381: *linePtr = cp;
382: return true;
1.1 deraadt 383: }
384:
1.44 espie 385: /* Helper function: ar fields are not null terminated. */
1.41 espie 386: static long
1.54 espie 387: field2long(const char *field, size_t length)
1.41 espie 388: {
1.64 espie 389: static char enough[32];
1.41 espie 390:
1.64 espie 391: assert(length < sizeof(enough));
392: memcpy(enough, field, length);
393: enough[length] = '\0';
394: return strtol(enough, NULL, 10);
1.41 espie 395: }
396:
1.38 espie 397: static Arch *
1.54 espie 398: read_archive(const char *archive, const char *earchive)
1.1 deraadt 399: {
1.64 espie 400: FILE *arch; /* Stream to archive */
401: char magic[SARMAG];
402: Arch *ar;
1.40 espie 403: #ifdef SVR4ARCHIVES
1.64 espie 404: struct SVR4namelist list;
1.40 espie 405:
1.64 espie 406: list.fnametab = NULL;
1.40 espie 407: #endif
1.35 espie 408:
1.64 espie 409: /* When we encounter an archive for the first time, we read its
410: * whole contents, to place it in the cache. */
411: arch = fopen(archive, "r");
412: if (arch == NULL)
413: return NULL;
1.9 millert 414:
1.64 espie 415: /* Make sure this is an archive we can handle. */
416: if ((fread(magic, SARMAG, 1, arch) != 1) ||
417: (strncmp(magic, ARMAG, SARMAG) != 0)) {
418: fclose(arch);
419: return NULL;
420: }
1.1 deraadt 421:
1.64 espie 422: ar = ohash_create_entry(&arch_info, archive, &earchive);
423: ohash_init(&ar->members, 8, &members_info);
1.38 espie 424:
1.64 espie 425: for (;;) {
426: size_t n;
427: struct ar_hdr arHeader;
428: /* Archive-member header for reading archive */
429: off_t size; /* Size of archive member */
430: char buffer[PATH_MAX];
431: char *memberName;
1.40 espie 432: /* Current member name while hashing. */
1.64 espie 433: char *cp;
1.41 espie 434:
1.64 espie 435: memberName = buffer;
436: n = fread(&arHeader, 1, sizeof(struct ar_hdr), arch);
1.41 espie 437:
1.64 espie 438: /* Whole archive read ok. */
439: if (n == 0 && feof(arch)) {
1.41 espie 440: #ifdef SVR4ARCHIVES
1.64 espie 441: efree(list.fnametab);
1.41 espie 442: #endif
1.64 espie 443: fclose(arch);
444: return ar;
445: }
446: if (n < sizeof(struct ar_hdr))
447: break;
1.40 espie 448:
1.64 espie 449: if (memcmp(arHeader.ar_fmag, ARFMAG, sizeof(arHeader.ar_fmag))
450: != 0) {
451: /* The header is bogus. */
452: break;
453: } else {
454: /* We need to advance the stream's pointer to the start
455: * of the next header. Records are padded with
456: * newlines to an even-byte boundary, so we need to
457: * extract the size of the record and round it up
458: * during the seek. */
459: size = (off_t) field2long(arHeader.ar_size,
460: sizeof(arHeader.ar_size));
461:
462: (void)memcpy(memberName, arHeader.ar_name,
463: AR_NAME_SIZE);
464: /* Find real end of name (strip extranous ' ') */
465: for (cp = memberName + AR_NAME_SIZE - 1; *cp == ' ';)
466: cp--;
467: cp[1] = '\0';
1.1 deraadt 468:
1.6 briggs 469: #ifdef SVR4ARCHIVES
1.64 espie 470: /* SVR4 names are slash terminated. Also svr4 extended
471: * AR format.
472: */
473: if (memberName[0] == '/') {
474: /* SVR4 magic mode. */
475: memberName = ArchSVR4Entry(&list, memberName,
476: size, arch);
477: if (memberName == NULL) /* Invalid data */
478: break;
479: else if (memberName == svr4list)
480: /* List of files entry */
481: continue;
482: /* Got the entry. */
483: /* XXX this assumes further processing, such as
484: * AR_EFMT1, also applies to SVR4ARCHIVES. */
485: }
486: else {
487: if (cp[0] == '/')
488: cp[0] = '\0';
489: }
1.3 niklas 490: #endif
491:
1.1 deraadt 492: #ifdef AR_EFMT1
1.64 espie 493: /* BSD 4.4 extended AR format: #1/<namelen>, with name
494: * as the first <namelen> bytes of the file. */
495: if (memcmp(memberName, AR_EFMT1, sizeof(AR_EFMT1) - 1)
496: == 0 && isdigit(memberName[sizeof(AR_EFMT1) - 1])) {
497:
498: int elen = atoi(memberName +
499: sizeof(AR_EFMT1)-1);
500:
501: if (elen <= 0 || elen >= PATH_MAX)
502: break;
503: memberName = buffer;
504: if (fread(memberName, elen, 1, arch) != 1)
505: break;
506: memberName[elen] = '\0';
507: if (fseek(arch, -elen, SEEK_CUR) != 0)
508: break;
509: if (DEBUG(ARCH) || DEBUG(MAKE))
510: printf("ArchStat: Extended format entry for %s\n",
511: memberName);
512: }
513: #endif
514:
515: ohash_insert(&ar->members,
516: ohash_qlookup(&ar->members, memberName),
517: new_arch_member(&arHeader, memberName));
518: }
519: if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0)
1.41 espie 520: break;
1.1 deraadt 521: }
522:
1.64 espie 523: fclose(arch);
524: ohash_delete(&ar->members);
1.40 espie 525: #ifdef SVR4ARCHIVES
1.64 espie 526: efree(list.fnametab);
1.40 espie 527: #endif
1.64 espie 528: free(ar);
529: return NULL;
1.38 espie 530: }
531:
532: /*-
533: *-----------------------------------------------------------------------
534: * ArchMTimeMember --
535: * Find the modification time of an archive's member, given the
536: * path to the archive and the path to the desired member.
537: *
538: * Results:
1.44 espie 539: * The archive member's modification time, or OUT_OF_DATE if member
540: * was not found (convenient, so that missing members are always
1.38 espie 541: * out of date).
542: *
543: * Side Effects:
1.45 espie 544: * Cache the whole archive contents if hash is true.
1.38 espie 545: *-----------------------------------------------------------------------
546: */
547: static TIMESTAMP
1.54 espie 548: ArchMTimeMember(
549: const char *archive, /* Path to the archive */
550: const char *member, /* Name of member. If it is a path, only the
1.38 espie 551: * last component is used. */
1.54 espie 552: bool hash) /* true if archive should be hashed if not
1.44 espie 553: * already so. */
1.38 espie 554: {
1.64 espie 555: FILE *arch; /* Stream to archive */
556: Arch *ar; /* Archive descriptor */
557: unsigned int slot; /* Place of archive in the archives hash */
558: const char *end = NULL;
559: const char *cp;
560: TIMESTAMP result;
561:
562: ts_set_out_of_date(result);
563: /* Because of space constraints and similar things, files are archived
564: * using their final path components, not the entire thing, so we need
565: * to point 'member' to the final component, if there is one, to make
566: * the comparisons easier... */
567: cp = strrchr(member, '/');
568: if (cp != NULL)
569: member = cp + 1;
570:
571: /* Try to find archive in cache. */
572: slot = ohash_qlookupi(&archives, archive, &end);
573: ar = ohash_find(&archives, slot);
574:
575: /* If not found, get it now. */
576: if (ar == NULL) {
577: if (!hash) {
1.67 ! espie 578: /* Quick path: no need to hash the whole archive, just
! 579: * use ArchFindMember to get the member's header and
! 580: * close the stream again. */
! 581: struct ar_hdr arHeader;
! 582:
! 583: arch = ArchFindMember(archive, member, &arHeader, "r");
! 584:
! 585: if (arch != NULL) {
! 586: fclose(arch);
! 587: ts_set_from_time_t(
! 588: (time_t)strtol(arHeader.ar_date, NULL, 10),
! 589: result);
! 590: }
! 591: return result;
1.64 espie 592: }
593: ar = read_archive(archive, end);
594: if (ar != NULL)
595: ohash_insert(&archives, slot, ar);
596: }
1.38 espie 597:
1.64 espie 598: /* If archive was found, get entry we seek. */
599: if (ar != NULL) {
600: struct arch_member *he;
601: end = NULL;
1.38 espie 602:
1.64 espie 603: he = ohash_find(&ar->members,
1.43 espie 604: ohash_qlookupi(&ar->members, member, &end));
1.38 espie 605: if (he != NULL)
1.64 espie 606: return mtime_of_member(he);
607: else {
608: if ((size_t)(end - member) > AR_NAME_SIZE) {
609: /* Try truncated name. */
610: end = member + AR_NAME_SIZE;
611: he = ohash_find(&ar->members,
612: ohash_qlookupi(&ar->members, member, &end));
613: if (he != NULL)
614: return mtime_of_member(he);
615: }
616: }
1.38 espie 617: }
1.64 espie 618: return result;
1.1 deraadt 619: }
1.6 briggs 620:
621: #ifdef SVR4ARCHIVES
622: /*-
623: *-----------------------------------------------------------------------
624: * ArchSVR4Entry --
625: * Parse an SVR4 style entry that begins with a slash.
626: * If it is "//", then load the table of filenames
627: * If it is "/<offset>", then try to substitute the long file name
628: * from offset of a table previously read.
629: *
630: * Results:
1.40 espie 631: * svr4list: just read a list of names
1.50 mpech 632: * NULL: error occurred
1.40 espie 633: * extended name
1.6 briggs 634: *
1.40 espie 635: * Side-effect:
636: * For a list of names, store the list in l.
1.6 briggs 637: *-----------------------------------------------------------------------
638: */
1.44 espie 639:
1.40 espie 640: static char *
1.54 espie 641: ArchSVR4Entry(struct SVR4namelist *l, const char *name, size_t size, FILE *arch)
1.6 briggs 642: {
1.40 espie 643: #define ARLONGNAMES1 "/"
644: #define ARLONGNAMES2 "ARFILENAMES"
1.64 espie 645: size_t entry;
646: char *ptr, *eptr;
1.6 briggs 647:
1.64 espie 648: assert(name[0] == '/');
649: name++;
650: /* First comes a table of archive names, to be used by subsequent
651: * calls. */
652: if (memcmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
653: memcmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
654:
655: if (l->fnametab != NULL) {
656: if (DEBUG(ARCH))
657: printf("Attempted to redefine an SVR4 name table\n");
658: return NULL;
659: }
1.6 briggs 660:
1.64 espie 661: l->fnametab = emalloc(size);
662: l->fnamesize = size;
1.6 briggs 663:
1.64 espie 664: if (fread(l->fnametab, size, 1, arch) != 1) {
665: if (DEBUG(ARCH))
666: printf("Reading an SVR4 name table failed\n");
667: return NULL;
668: }
669:
670: eptr = l->fnametab + size;
671: for (entry = 0, ptr = l->fnametab; ptr < eptr; ptr++)
672: switch (*ptr) {
673: case '/':
674: entry++;
675: *ptr = '\0';
676: break;
677:
678: case '\n':
679: break;
680:
681: default:
682: break;
683: }
684: if (DEBUG(ARCH))
685: printf("Found svr4 archive name table with %lu entries\n",
686: (u_long)entry);
687: return (char *)svr4list;
688: }
689: /* Then the names themselves are given as offsets in this table. */
690: if (*name == ' ' || *name == '\0')
691: return NULL;
692:
693: entry = (size_t) strtol(name, &eptr, 0);
694: if ((*eptr != ' ' && *eptr != '\0') || eptr == name) {
695: if (DEBUG(ARCH))
696: printf("Could not parse SVR4 name /%s\n", name);
697: return NULL;
698: }
699: if (entry >= l->fnamesize) {
700: if (DEBUG(ARCH))
701: printf("SVR4 entry offset /%s is greater than %lu\n",
702: name, (u_long)l->fnamesize);
703: return NULL;
1.6 briggs 704: }
1.44 espie 705:
1.40 espie 706: if (DEBUG(ARCH))
1.64 espie 707: printf("Replaced /%s with %s\n", name, l->fnametab + entry);
1.6 briggs 708:
1.64 espie 709: return l->fnametab + entry;
1.6 briggs 710: }
711: #endif
712:
1.1 deraadt 713:
714: /*-
715: *-----------------------------------------------------------------------
716: * ArchFindMember --
717: * Locate a member of an archive, given the path of the archive and
718: * the path of the desired member. If the archive is to be modified,
719: * the mode should be "r+", if not, it should be "r".
720: *
721: * Results:
1.42 espie 722: * A FILE *, opened for reading and writing, positioned right after
1.44 espie 723: * the member's header, or NULL if the member was nonexistent.
1.1 deraadt 724: *
725: * Side Effects:
1.54 espie 726: * Fill the struct ar_hdr pointed by arHeaderPtr.
1.1 deraadt 727: *-----------------------------------------------------------------------
728: */
729: static FILE *
1.54 espie 730: ArchFindMember(
731: const char *archive, /* Path to the archive */
732: const char *member, /* Name of member. If it is a path, only the
1.1 deraadt 733: * last component is used. */
1.54 espie 734: struct ar_hdr *arHeaderPtr,/* Pointer to header structure to be filled in */
735: const char *mode) /* mode for opening the stream */
1.1 deraadt 736: {
1.64 espie 737: FILE *arch; /* Stream to archive */
738: char *cp;
739: char magic[SARMAG];
740: size_t length;
1.42 espie 741: #ifdef SVR4ARCHIVES
1.64 espie 742: struct SVR4namelist list;
1.42 espie 743:
1.64 espie 744: list.fnametab = NULL;
1.42 espie 745: #endif
746:
1.64 espie 747: arch = fopen(archive, mode);
748: if (arch == NULL)
749: return NULL;
1.9 millert 750:
1.64 espie 751: /* Make sure this is an archive we can handle. */
752: if (fread(magic, SARMAG, 1, arch) != 1 ||
753: strncmp(magic, ARMAG, SARMAG) != 0) {
754: fclose(arch);
755: return NULL;
756: }
1.1 deraadt 757:
1.64 espie 758: /* Because of space constraints and similar things, files are archived
759: * using their final path components, not the entire thing, so we need
760: * to point 'member' to the final component, if there is one, to make
761: * the comparisons easier... */
762: cp = strrchr(member, '/');
763: if (cp != NULL)
764: member = cp + 1;
765:
766: length = strlen(member);
767: if (length >= AR_NAME_SIZE)
768: length = AR_NAME_SIZE;
769:
770: /* Error handling is simpler than for read_archive, since we just
771: * look for a given member. */
772: while (fread(arHeaderPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
773: off_t size; /* Size of archive member */
774: char *memberName;
775:
776: if (memcmp(arHeaderPtr->ar_fmag, ARFMAG,
777: sizeof(arHeaderPtr->ar_fmag) ) != 0)
778: /* The header is bogus, so the archive is bad. */
779: break;
780:
781: memberName = arHeaderPtr->ar_name;
782: if (memcmp(member, memberName, length) == 0) {
783: /* If the member's name doesn't take up the entire
784: * 'name' field, we have to be careful of matching
785: * prefixes. Names are space- padded to the right, so
786: * if the character in 'name' at the end of the matched
787: * string is anything but a space, this isn't the
788: * member we sought. */
1.42 espie 789: #ifdef SVR4ARCHIVES
1.64 espie 790: if (length < sizeof(arHeaderPtr->ar_name) &&
791: memberName[length] == '/')
792: length++;
1.42 espie 793: #endif
1.64 espie 794: if (length == sizeof(arHeaderPtr->ar_name) ||
795: memberName[length] == ' ') {
1.42 espie 796: #ifdef SVR4ARCHIVES
1.64 espie 797: efree(list.fnametab);
1.42 espie 798: #endif
1.64 espie 799: return arch;
800: }
801: }
1.42 espie 802:
1.64 espie 803: size = (off_t) field2long(arHeaderPtr->ar_size,
804: sizeof(arHeaderPtr->ar_size));
1.9 millert 805:
1.42 espie 806: #ifdef SVR4ARCHIVES
1.67 ! espie 807: /* svr4 names are slash terminated. Also svr4 extended AR
! 808: * format.
! 809: */
! 810: if (memberName[0] == '/') {
! 811: /* svr4 magic mode. */
! 812: memberName = ArchSVR4Entry(&list,
! 813: arHeaderPtr->ar_name, size, arch);
! 814: if (memberName == NULL) /* Invalid data */
! 815: break;
! 816: else if (memberName == svr4list)
! 817: /* List of files entry */
! 818: continue;
! 819: /* Got the entry. */
! 820: if (strcmp(memberName, member) == 0) {
! 821: efree(list.fnametab);
! 822: return arch;
! 823: }
! 824: }
1.42 espie 825: #endif
826:
1.1 deraadt 827: #ifdef AR_EFMT1
1.64 espie 828: /* BSD 4.4 extended AR format: #1/<namelen>, with name as the
829: * first <namelen> bytes of the file. */
830: if (memcmp(memberName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
831: isdigit(memberName[sizeof(AR_EFMT1) - 1])) {
832: char ename[PATH_MAX];
833:
834: int elength = atoi(memberName + sizeof(AR_EFMT1)-1);
835:
836: if (elength <= 0 || elength >= PATH_MAX)
837: break;
838: if (fread(ename, elength, 1, arch) != 1)
839: break;
840: if (fseek(arch, -elength, SEEK_CUR) != 0)
841: break;
842: ename[elength] = '\0';
843: if (DEBUG(ARCH) || DEBUG(MAKE))
844: printf("ArchFind: Extended format entry for %s\n", ename);
845: /* Found as extended name. */
846: if (strcmp(ename, member) == 0) {
1.42 espie 847: #ifdef SVR4ARCHIVES
1.64 espie 848: efree(list.fnametab);
1.42 espie 849: #endif
1.64 espie 850: return arch;
851: }
1.1 deraadt 852: }
1.64 espie 853: #endif
854: /* This isn't the member we're after, so we need to advance the
855: * stream's pointer to the start of the next header. */
856: if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0)
857: break;
1.42 espie 858: }
1.1 deraadt 859:
1.64 espie 860: /* We did not find the member, or we ran into an error while reading
861: * the archive. */
1.42 espie 862: #ifdef SVRARCHIVES
1.64 espie 863: efree(list.fnametab);
1.42 espie 864: #endif
1.64 espie 865: fclose(arch);
866: return NULL;
1.1 deraadt 867: }
868:
1.39 espie 869: static void
1.54 espie 870: ArchTouch(const char *archive, const char *member)
1.39 espie 871: {
1.67 ! espie 872: FILE *arch;
! 873: struct ar_hdr arHeader;
1.39 espie 874:
1.64 espie 875: arch = ArchFindMember(archive, member, &arHeader, "r+");
876: if (arch != NULL) {
877: snprintf(arHeader.ar_date, sizeof(arHeader.ar_date), "%-12ld",
878: (long) timestamp2time_t(now));
879: if (fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR) == 0)
880: (void)fwrite(&arHeader, sizeof(struct ar_hdr), 1, arch);
881: fclose(arch);
882: }
1.39 espie 883: }
884:
1.45 espie 885: /*
1.39 espie 886: * Side Effects:
1.1 deraadt 887: * The modification time of the entire archive is also changed.
888: * For a library, this could necessitate the re-ranlib'ing of the
889: * whole thing.
890: */
891: void
1.54 espie 892: Arch_Touch(GNode *gn)
1.1 deraadt 893: {
1.64 espie 894: ArchTouch(Varq_Value(ARCHIVE_INDEX, gn), Varq_Value(MEMBER_INDEX, gn));
1.1 deraadt 895: }
896:
1.47 deraadt 897: /*ARGSUSED*/
1.1 deraadt 898: void
1.54 espie 899: Arch_TouchLib(GNode *gn UNUSED)
1.49 espie 900: /* ^ Non RANLIBMAG does nothing with it */
1.1 deraadt 901: {
1.3 niklas 902: #ifdef RANLIBMAG
1.64 espie 903: if (gn->path != NULL) {
904: ArchTouch(gn->path, RANLIBMAG);
905: set_times(gn->path);
1.1 deraadt 906: }
1.3 niklas 907: #endif
1.1 deraadt 908: }
909:
1.34 espie 910: TIMESTAMP
1.54 espie 911: Arch_MTime(GNode *gn)
1.1 deraadt 912: {
1.64 espie 913: gn->mtime = ArchMTimeMember(Varq_Value(ARCHIVE_INDEX, gn),
914: Varq_Value(MEMBER_INDEX, gn), true);
1.1 deraadt 915:
1.64 espie 916: return gn->mtime;
1.1 deraadt 917: }
918:
1.36 espie 919: TIMESTAMP
1.54 espie 920: Arch_MemMTime(GNode *gn)
1.1 deraadt 921: {
1.64 espie 922: LstNode ln;
1.1 deraadt 923:
1.64 espie 924: for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln)) {
925: GNode *pgn;
926: char *nameStart;
927: char *nameEnd;
928:
929: pgn = (GNode *)Lst_Datum(ln);
930:
931: if (pgn->type & OP_ARCHV) {
932: /* If the parent is an archive specification and is
933: * being made and its member's name matches the name of
934: * the node we were given, record the modification time
935: * of the parent in the child. We keep searching its
936: * parents in case some other parent requires this
937: * child to exist... */
938: if ((nameStart = strchr(pgn->name, '(') ) != NULL) {
939: nameStart++;
940: nameEnd = strchr(nameStart, ')');
941: } else
942: nameEnd = NULL;
943:
944: if (pgn->make && nameEnd != NULL &&
945: strncmp(nameStart, gn->name, nameEnd - nameStart)
946: == 0 && gn->name[nameEnd-nameStart] == '\0')
947: gn->mtime = Arch_MTime(pgn);
948: } else if (pgn->make) {
949: /* Something which isn't a library depends on the
950: * existence of this target, so it needs to exist. */
951: ts_set_out_of_date(gn->mtime);
952: break;
953: }
1.1 deraadt 954: }
1.64 espie 955: return gn->mtime;
1.1 deraadt 956: }
957:
1.45 espie 958: /* If the system can handle the -L flag when linking (or we cannot find
959: * the library), we assume that the user has placed the .LIBRARIES variable
960: * in the final linking command (or the linker will know where to find it)
961: * and set the TARGET variable for this node to be the node's name. Otherwise,
962: * we set the TARGET variable to be the full path of the library,
963: * as returned by Dir_FindFile.
1.1 deraadt 964: */
965: void
1.54 espie 966: Arch_FindLib(GNode *gn, Lst path)
1.1 deraadt 967: {
1.64 espie 968: char *libName; /* file name for archive */
969: size_t length = strlen(gn->name) + 6 - 2;
1.1 deraadt 970:
1.64 espie 971: libName = emalloc(length);
972: snprintf(libName, length, "lib%s.a", &gn->name[2]);
1.1 deraadt 973:
1.64 espie 974: gn->path = Dir_FindFile(libName, path);
1.1 deraadt 975:
1.64 espie 976: free(libName);
1.1 deraadt 977:
1.64 espie 978: Varq_Set(TARGET_INDEX, gn->name, gn);
1.1 deraadt 979: }
980:
981: /*-
982: *-----------------------------------------------------------------------
983: * Arch_LibOODate --
984: * Decide if a node with the OP_LIB attribute is out-of-date. Called
985: * from Make_OODate to make its life easier.
986: *
987: * There are several ways for a library to be out-of-date that are
988: * not available to ordinary files. In addition, there are ways
989: * that are open to regular files that are not available to
990: * libraries. A library that is only used as a source is never
991: * considered out-of-date by itself. This does not preclude the
992: * library's modification time from making its parent be out-of-date.
993: * A library will be considered out-of-date for any of these reasons,
994: * given that it is a target on a dependency line somewhere:
995: * Its modification time is less than that of one of its
1.44 espie 996: * sources (gn->mtime < gn->cmtime).
1.1 deraadt 997: * Its modification time is greater than the time at which the
1.44 espie 998: * make began (i.e. it's been modified in the course
999: * of the make, probably by archiving).
1.1 deraadt 1000: * The modification time of one of its sources is greater than
1001: * the one of its RANLIBMAG member (i.e. its table of contents
1.44 espie 1002: * is out-of-date). We don't compare of the archive time
1.1 deraadt 1003: * vs. TOC time because they can be too close. In my
1004: * opinion we should not bother with the TOC at all since
1005: * this is used by 'ar' rules that affect the data contents
1006: * of the archive, not by ranlib rules, which affect the
1.9 millert 1007: * TOC.
1.1 deraadt 1008: *
1009: * Results:
1.45 espie 1010: * true if the library is out-of-date. false otherwise.
1.1 deraadt 1011: *
1012: * Side Effects:
1013: * The library will be hashed if it hasn't been already.
1014: *-----------------------------------------------------------------------
1015: */
1.45 espie 1016: bool
1.54 espie 1017: Arch_LibOODate(GNode *gn)
1.1 deraadt 1018: {
1.47 deraadt 1019: #ifdef RANLIBMAG
1.64 espie 1020: TIMESTAMP modTimeTOC; /* mod time of __.SYMDEF */
1.47 deraadt 1021: #endif
1.9 millert 1022:
1.64 espie 1023: if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->children))
1024: return false;
1025: if (is_strictly_before(now, gn->mtime) ||
1026: is_strictly_before(gn->mtime, gn->cmtime) ||
1027: is_out_of_date(gn->mtime))
1028: return true;
1.2 deraadt 1029: #ifdef RANLIBMAG
1.64 espie 1030: /* non existent libraries are always out-of-date. */
1031: if (gn->path == NULL)
1032: return true;
1033: modTimeTOC = ArchMTimeMember(gn->path, RANLIBMAG, false);
1.37 espie 1034:
1.64 espie 1035: if (!is_out_of_date(modTimeTOC)) {
1036: if (DEBUG(ARCH) || DEBUG(MAKE))
1037: printf("%s modified %s...", RANLIBMAG,
1.66 espie 1038: time_to_string(modTimeTOC));
1.64 espie 1039: return is_strictly_before(modTimeTOC, gn->cmtime);
1040: }
1041: /* A library w/o a table of contents is out-of-date. */
1.37 espie 1042: if (DEBUG(ARCH) || DEBUG(MAKE))
1.64 espie 1043: printf("No t.o.c....");
1044: return true;
1.2 deraadt 1045: #else
1.64 espie 1046: return false;
1.2 deraadt 1047: #endif
1.1 deraadt 1048: }
1049:
1050: void
1.54 espie 1051: Arch_Init(void)
1.1 deraadt 1052: {
1.64 espie 1053: ohash_init(&archives, 4, &arch_info);
1.1 deraadt 1054: }
1055:
1.45 espie 1056: #ifdef CLEANUP
1.1 deraadt 1057: void
1.54 espie 1058: Arch_End(void)
1.1 deraadt 1059: {
1.64 espie 1060: Arch *e;
1061: unsigned int i;
1.38 espie 1062:
1.64 espie 1063: for (e = ohash_first(&archives, &i); e != NULL;
1064: e = ohash_next(&archives, &i))
1065: ArchFree(e);
1066: ohash_delete(&archives);
1.45 espie 1067: }
1.16 espie 1068: #endif
1.9 millert 1069:
1.45 espie 1070: bool
1.54 espie 1071: Arch_IsLib(GNode *gn)
1.9 millert 1072: {
1.64 espie 1073: char buf[SARMAG];
1074: int fd;
1075:
1076: if (gn->path == NULL || (fd = open(gn->path, O_RDONLY)) == -1)
1077: return false;
1.9 millert 1078:
1.64 espie 1079: if (read(fd, buf, SARMAG) != SARMAG) {
1080: (void)close(fd);
1081: return false;
1082: }
1.9 millert 1083:
1.44 espie 1084: (void)close(fd);
1.9 millert 1085:
1.64 espie 1086: return memcmp(buf, ARMAG, SARMAG) == 0;
1.1 deraadt 1087: }