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