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