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