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