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