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