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