arch.c revision 1.117 1 /* $NetBSD: arch.c,v 1.117 2020/09/22 04:05:41 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
38 *
39 * This code is derived from software contributed to Berkeley by
40 * Adam de Boor.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71 /*-
72 * arch.c --
73 * Functions to manipulate libraries, archives and their members.
74 *
75 * Once again, cacheing/hashing comes into play in the manipulation
76 * of archives. The first time an archive is referenced, all of its members'
77 * headers are read and hashed and the archive closed again. All hashed
78 * archives are kept on a list which is searched each time an archive member
79 * is referenced.
80 *
81 * The interface to this module is:
82 * Arch_ParseArchive Given an archive specification, return a list
83 * of GNode's, one for each member in the spec.
84 * FALSE is returned if the specification is
85 * invalid for some reason.
86 *
87 * Arch_Touch Alter the modification time of the archive
88 * member described by the given node to be
89 * the current time.
90 *
91 * Arch_TouchLib Update the modification time of the library
92 * described by the given node. This is special
93 * because it also updates the modification time
94 * of the library's table of contents.
95 *
96 * Arch_MTime Find the modification time of a member of
97 * an archive *in the archive*. The time is also
98 * placed in the member's GNode. Returns the
99 * modification time.
100 *
101 * Arch_MemTime Find the modification time of a member of
102 * an archive. Called when the member doesn't
103 * already exist. Looks in the archive for the
104 * modification time. Returns the modification
105 * time.
106 *
107 * Arch_FindLib Search for a library along a path. The
108 * library name in the GNode should be in
109 * -l<name> format.
110 *
111 * Arch_LibOODate Special function to decide if a library node
112 * is out-of-date.
113 *
114 * Arch_Init Initialize this module.
115 *
116 * Arch_End Cleanup this module.
117 */
118
119 #include <sys/types.h>
120 #include <sys/stat.h>
121 #include <sys/time.h>
122 #include <sys/param.h>
123
124 #include <ar.h>
125 #include <ctype.h>
126 #include <stdio.h>
127 #include <stdlib.h>
128 #include <utime.h>
129
130 #include "make.h"
131 #include "hash.h"
132 #include "dir.h"
133 #include "config.h"
134
135 /* "@(#)arch.c 8.2 (Berkeley) 1/2/94" */
136 MAKE_RCSID("$NetBSD: arch.c,v 1.117 2020/09/22 04:05:41 rillig Exp $");
137
138 #ifdef TARGET_MACHINE
139 #undef MAKE_MACHINE
140 #define MAKE_MACHINE TARGET_MACHINE
141 #endif
142 #ifdef TARGET_MACHINE_ARCH
143 #undef MAKE_MACHINE_ARCH
144 #define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH
145 #endif
146
147 typedef struct List ArchList;
148 typedef struct ListNode ArchListNode;
149
150 static ArchList *archives; /* The archives we've already examined */
151
152 typedef struct Arch {
153 char *name; /* Name of archive */
154 Hash_Table members; /* All the members of the archive described
155 * by <name, struct ar_hdr *> key/value pairs */
156 char *fnametab; /* Extended name table strings */
157 size_t fnamesize; /* Size of the string table */
158 } Arch;
159
160 static struct ar_hdr *ArchStatMember(const char *, const char *, Boolean);
161 static FILE *ArchFindMember(const char *, const char *,
162 struct ar_hdr *, const char *);
163 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
164 #define SVR4ARCHIVES
165 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
166 #endif
167
168 #ifdef CLEANUP
169 static void
170 ArchFree(void *ap)
171 {
172 Arch *a = (Arch *)ap;
173 Hash_Search search;
174 Hash_Entry *entry;
175
176 /* Free memory from hash entries */
177 for (entry = Hash_EnumFirst(&a->members, &search);
178 entry != NULL;
179 entry = Hash_EnumNext(&search))
180 free(Hash_GetValue(entry));
181
182 free(a->name);
183 free(a->fnametab);
184 Hash_DeleteTable(&a->members);
185 free(a);
186 }
187 #endif
188
189
190 /*-
191 *-----------------------------------------------------------------------
192 * Arch_ParseArchive --
193 * Parse the archive specification in the given line and find/create
194 * the nodes for the specified archive members, placing their nodes
195 * on the given list.
196 *
197 * Input:
198 * linePtr Pointer to start of specification
199 * nodeLst Lst on which to place the nodes
200 * ctxt Context in which to expand variables
201 *
202 * Results:
203 * TRUE if it was a valid specification. The linePtr is updated
204 * to point to the first non-space after the archive spec. The
205 * nodes for the members are placed on the given list.
206 *-----------------------------------------------------------------------
207 */
208 Boolean
209 Arch_ParseArchive(char **linePtr, GNodeList *nodeLst, GNode *ctxt)
210 {
211 char *cp; /* Pointer into line */
212 GNode *gn; /* New node */
213 char *libName; /* Library-part of specification */
214 char *memName; /* Member-part of specification */
215 char saveChar; /* Ending delimiter of member-name */
216 Boolean subLibName; /* TRUE if libName should have/had
217 * variable substitution performed on it */
218
219 libName = *linePtr;
220
221 subLibName = FALSE;
222
223 for (cp = libName; *cp != '(' && *cp != '\0';) {
224 if (*cp == '$') {
225 /*
226 * Variable spec, so call the Var module to parse the puppy
227 * so we can safely advance beyond it...
228 */
229 const char *nested_p = cp;
230 void *result_freeIt;
231 const char *result;
232 Boolean isError;
233
234 (void)Var_Parse(&nested_p, ctxt, VARE_UNDEFERR|VARE_WANTRES,
235 &result, &result_freeIt);
236 /* TODO: handle errors */
237 isError = result == var_Error;
238 free(result_freeIt);
239 if (isError)
240 return FALSE;
241
242 subLibName = TRUE;
243 cp += nested_p - cp;
244 } else
245 cp++;
246 }
247
248 *cp++ = '\0';
249 if (subLibName) {
250 libName = Var_Subst(libName, ctxt, VARE_UNDEFERR|VARE_WANTRES);
251 }
252
253
254 for (;;) {
255 /*
256 * First skip to the start of the member's name, mark that
257 * place and skip to the end of it (either white-space or
258 * a close paren).
259 */
260 Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */
261
262 while (*cp != '\0' && *cp != ')' && ch_isspace(*cp)) {
263 cp++;
264 }
265 memName = cp;
266 while (*cp != '\0' && *cp != ')' && !ch_isspace(*cp)) {
267 if (*cp == '$') {
268 /*
269 * Variable spec, so call the Var module to parse the puppy
270 * so we can safely advance beyond it...
271 */
272 void *freeIt;
273 const char *result;
274 Boolean isError;
275 const char *nested_p = cp;
276
277 (void)Var_Parse(&nested_p, ctxt, VARE_UNDEFERR|VARE_WANTRES,
278 &result, &freeIt);
279 /* TODO: handle errors */
280 isError = result == var_Error;
281 free(freeIt);
282
283 if (isError)
284 return FALSE;
285
286 doSubst = TRUE;
287 cp += nested_p - cp;
288 } else {
289 cp++;
290 }
291 }
292
293 /*
294 * If the specification ends without a closing parenthesis,
295 * chances are there's something wrong (like a missing backslash),
296 * so it's better to return failure than allow such things to happen
297 */
298 if (*cp == '\0') {
299 printf("No closing parenthesis in archive specification\n");
300 return FALSE;
301 }
302
303 /*
304 * If we didn't move anywhere, we must be done
305 */
306 if (cp == memName) {
307 break;
308 }
309
310 saveChar = *cp;
311 *cp = '\0';
312
313 /*
314 * XXX: This should be taken care of intelligently by
315 * SuffExpandChildren, both for the archive and the member portions.
316 */
317 /*
318 * If member contains variables, try and substitute for them.
319 * This will slow down archive specs with dynamic sources, of course,
320 * since we'll be (non-)substituting them three times, but them's
321 * the breaks -- we need to do this since SuffExpandChildren calls
322 * us, otherwise we could assume the thing would be taken care of
323 * later.
324 */
325 if (doSubst) {
326 char *buf;
327 char *sacrifice;
328 char *oldMemName = memName;
329
330 memName = Var_Subst(memName, ctxt, VARE_UNDEFERR | VARE_WANTRES);
331
332 /*
333 * Now form an archive spec and recurse to deal with nested
334 * variables and multi-word variable values.... The results
335 * are just placed at the end of the nodeLst we're returning.
336 */
337 buf = sacrifice = str_concat4(libName, "(", memName, ")");
338
339 if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
340 /*
341 * Must contain dynamic sources, so we can't deal with it now.
342 * Just create an ARCHV node for the thing and let
343 * SuffExpandChildren handle it...
344 */
345 gn = Targ_FindNode(buf, TARG_CREATE);
346
347 if (gn == NULL) {
348 free(buf);
349 return FALSE;
350 } else {
351 gn->type |= OP_ARCHV;
352 Lst_Append(nodeLst, gn);
353 }
354 } else if (!Arch_ParseArchive(&sacrifice, nodeLst, ctxt)) {
355 /*
356 * Error in nested call -- free buffer and return FALSE
357 * ourselves.
358 */
359 free(buf);
360 return FALSE;
361 }
362 /*
363 * Free buffer and continue with our work.
364 */
365 free(buf);
366 } else if (Dir_HasWildcards(memName)) {
367 StringList *members = Lst_Init();
368 Dir_Expand(memName, dirSearchPath, members);
369
370 while (!Lst_IsEmpty(members)) {
371 char *member = Lst_Dequeue(members);
372 char *fullname = str_concat4(libName, "(", member, ")");
373 free(member);
374
375 gn = Targ_FindNode(fullname, TARG_CREATE);
376 free(fullname);
377
378 if (gn == NULL)
379 return FALSE;
380
381 /*
382 * We've found the node, but have to make sure the rest of
383 * the world knows it's an archive member, without having
384 * to constantly check for parentheses, so we type the
385 * thing with the OP_ARCHV bit before we place it on the
386 * end of the provided list.
387 */
388 gn->type |= OP_ARCHV;
389 Lst_Append(nodeLst, gn);
390 }
391 Lst_Free(members);
392 } else {
393 char *fullname = str_concat4(libName, "(", memName, ")");
394 gn = Targ_FindNode(fullname, TARG_CREATE);
395 free(fullname);
396
397 if (gn == NULL)
398 return FALSE;
399
400 /*
401 * We've found the node, but have to make sure the rest of the
402 * world knows it's an archive member, without having to
403 * constantly check for parentheses, so we type the thing with
404 * the OP_ARCHV bit before we place it on the end of the
405 * provided list.
406 */
407 gn->type |= OP_ARCHV;
408 Lst_Append(nodeLst, gn);
409 }
410 if (doSubst) {
411 free(memName);
412 }
413
414 *cp = saveChar;
415 }
416
417 /*
418 * If substituted libName, free it now, since we need it no longer.
419 */
420 if (subLibName) {
421 free(libName);
422 }
423
424 /*
425 * We promised the pointer would be set up at the next non-space, so
426 * we must advance cp there before setting *linePtr... (note that on
427 * entrance to the loop, cp is guaranteed to point at a ')')
428 */
429 do {
430 cp++;
431 } while (*cp != '\0' && ch_isspace(*cp));
432
433 *linePtr = cp;
434 return TRUE;
435 }
436
437 /* See if the given archive is the one we are looking for.
438 * Called via Lst_Find. */
439 static Boolean
440 ArchFindArchive(const void *ar, const void *desiredName)
441 {
442 return strcmp(((const Arch *)ar)->name, desiredName) == 0;
443 }
444
445 /*-
446 *-----------------------------------------------------------------------
447 * ArchStatMember --
448 * Locate a member of an archive, given the path of the archive and
449 * the path of the desired member.
450 *
451 * Input:
452 * archive Path to the archive
453 * member Name of member. If it is a path, only the last
454 * component is used.
455 * hash TRUE if archive should be hashed if not already so.
456 *
457 * Results:
458 * A pointer to the current struct ar_hdr structure for the member. Note
459 * That no position is returned, so this is not useful for touching
460 * archive members. This is mostly because we have no assurances that
461 * The archive will remain constant after we read all the headers, so
462 * there's not much point in remembering the position...
463 *-----------------------------------------------------------------------
464 */
465 static struct ar_hdr *
466 ArchStatMember(const char *archive, const char *member, Boolean hash)
467 {
468 #define AR_MAX_NAME_LEN (sizeof(arh.ar_name)-1)
469 FILE * arch; /* Stream to archive */
470 size_t size; /* Size of archive member */
471 char magic[SARMAG];
472 ArchListNode *ln;
473 Arch *ar; /* Archive descriptor */
474 Hash_Entry *he; /* Entry containing member's description */
475 struct ar_hdr arh; /* archive-member header for reading archive */
476 char memName[MAXPATHLEN+1];
477 /* Current member name while hashing. */
478
479 /*
480 * Because of space constraints and similar things, files are archived
481 * using their final path components, not the entire thing, so we need
482 * to point 'member' to the final component, if there is one, to make
483 * the comparisons easier...
484 */
485 const char *base = strrchr(member, '/');
486 if (base != NULL) {
487 member = base + 1;
488 }
489
490 ln = Lst_Find(archives, ArchFindArchive, archive);
491 if (ln != NULL) {
492 ar = LstNode_Datum(ln);
493
494 he = Hash_FindEntry(&ar->members, member);
495
496 if (he != NULL) {
497 return (struct ar_hdr *)Hash_GetValue(he);
498 } else {
499 /* Try truncated name */
500 char copy[AR_MAX_NAME_LEN+1];
501 size_t len = strlen(member);
502
503 if (len > AR_MAX_NAME_LEN) {
504 len = AR_MAX_NAME_LEN;
505 snprintf(copy, sizeof copy, "%s", member);
506 }
507 if ((he = Hash_FindEntry(&ar->members, copy)) != NULL)
508 return (struct ar_hdr *)Hash_GetValue(he);
509 return NULL;
510 }
511 }
512
513 if (!hash) {
514 /*
515 * Caller doesn't want the thing hashed, just use ArchFindMember
516 * to read the header for the member out and close down the stream
517 * again. Since the archive is not to be hashed, we assume there's
518 * no need to allocate extra room for the header we're returning,
519 * so just declare it static.
520 */
521 static struct ar_hdr sarh;
522
523 arch = ArchFindMember(archive, member, &sarh, "r");
524
525 if (arch == NULL) {
526 return NULL;
527 } else {
528 fclose(arch);
529 return &sarh;
530 }
531 }
532
533 /*
534 * We don't have this archive on the list yet, so we want to find out
535 * everything that's in it and cache it so we can get at it quickly.
536 */
537 arch = fopen(archive, "r");
538 if (arch == NULL) {
539 return NULL;
540 }
541
542 /*
543 * We use the ARMAG string to make sure this is an archive we
544 * can handle...
545 */
546 if ((fread(magic, SARMAG, 1, arch) != 1) ||
547 (strncmp(magic, ARMAG, SARMAG) != 0)) {
548 fclose(arch);
549 return NULL;
550 }
551
552 ar = bmake_malloc(sizeof(Arch));
553 ar->name = bmake_strdup(archive);
554 ar->fnametab = NULL;
555 ar->fnamesize = 0;
556 Hash_InitTable(&ar->members);
557 memName[AR_MAX_NAME_LEN] = '\0';
558
559 while (fread((char *)&arh, sizeof(struct ar_hdr), 1, arch) == 1) {
560 if (strncmp( arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
561 /*
562 * The header is bogus, so the archive is bad
563 * and there's no way we can recover...
564 */
565 goto badarch;
566 } else {
567 char *nameend;
568
569 /*
570 * We need to advance the stream's pointer to the start of the
571 * next header. Files are padded with newlines to an even-byte
572 * boundary, so we need to extract the size of the file from the
573 * 'size' field of the header and round it up during the seek.
574 */
575 arh.ar_size[sizeof(arh.ar_size)-1] = '\0';
576 size = (size_t)strtol(arh.ar_size, NULL, 10);
577
578 memcpy(memName, arh.ar_name, sizeof(arh.ar_name));
579 nameend = memName + AR_MAX_NAME_LEN;
580 while (*nameend == ' ') {
581 nameend--;
582 }
583 nameend[1] = '\0';
584
585 #ifdef SVR4ARCHIVES
586 /*
587 * svr4 names are slash terminated. Also svr4 extended AR format.
588 */
589 if (memName[0] == '/') {
590 /*
591 * svr4 magic mode; handle it
592 */
593 switch (ArchSVR4Entry(ar, memName, size, arch)) {
594 case -1: /* Invalid data */
595 goto badarch;
596 case 0: /* List of files entry */
597 continue;
598 default: /* Got the entry */
599 break;
600 }
601 }
602 else {
603 if (nameend[0] == '/')
604 nameend[0] = '\0';
605 }
606 #endif
607
608 #ifdef AR_EFMT1
609 /*
610 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
611 * first <namelen> bytes of the file
612 */
613 if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
614 ch_isdigit(memName[sizeof(AR_EFMT1) - 1])) {
615
616 int elen = atoi(&memName[sizeof(AR_EFMT1)-1]);
617
618 if ((unsigned int)elen > MAXPATHLEN)
619 goto badarch;
620 if (fread(memName, (size_t)elen, 1, arch) != 1)
621 goto badarch;
622 memName[elen] = '\0';
623 if (fseek(arch, -elen, SEEK_CUR) != 0)
624 goto badarch;
625 if (DEBUG(ARCH) || DEBUG(MAKE)) {
626 fprintf(debug_file, "ArchStat: Extended format entry for %s\n", memName);
627 }
628 }
629 #endif
630
631 he = Hash_CreateEntry(&ar->members, memName, NULL);
632 Hash_SetValue(he, bmake_malloc(sizeof(struct ar_hdr)));
633 memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr));
634 }
635 if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0)
636 goto badarch;
637 }
638
639 fclose(arch);
640
641 Lst_Append(archives, ar);
642
643 /*
644 * Now that the archive has been read and cached, we can look into
645 * the hash table to find the desired member's header.
646 */
647 he = Hash_FindEntry(&ar->members, member);
648
649 if (he != NULL) {
650 return (struct ar_hdr *)Hash_GetValue(he);
651 } else {
652 return NULL;
653 }
654
655 badarch:
656 fclose(arch);
657 Hash_DeleteTable(&ar->members);
658 free(ar->fnametab);
659 free(ar);
660 return NULL;
661 }
662
663 #ifdef SVR4ARCHIVES
664 /*-
665 *-----------------------------------------------------------------------
666 * ArchSVR4Entry --
667 * Parse an SVR4 style entry that begins with a slash.
668 * If it is "//", then load the table of filenames
669 * If it is "/<offset>", then try to substitute the long file name
670 * from offset of a table previously read.
671 * If a table is read, the file pointer is moved to the next archive
672 * member.
673 *
674 * Results:
675 * -1: Bad data in archive
676 * 0: A table was loaded from the file
677 * 1: Name was successfully substituted from table
678 * 2: Name was not successfully substituted from table
679 *-----------------------------------------------------------------------
680 */
681 static int
682 ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch)
683 {
684 #define ARLONGNAMES1 "//"
685 #define ARLONGNAMES2 "/ARFILENAMES"
686 size_t entry;
687 char *ptr, *eptr;
688
689 if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
690 strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
691
692 if (ar->fnametab != NULL) {
693 if (DEBUG(ARCH)) {
694 fprintf(debug_file, "Attempted to redefine an SVR4 name table\n");
695 }
696 return -1;
697 }
698
699 /*
700 * This is a table of archive names, so we build one for
701 * ourselves
702 */
703 ar->fnametab = bmake_malloc(size);
704 ar->fnamesize = size;
705
706 if (fread(ar->fnametab, size, 1, arch) != 1) {
707 if (DEBUG(ARCH)) {
708 fprintf(debug_file, "Reading an SVR4 name table failed\n");
709 }
710 return -1;
711 }
712 eptr = ar->fnametab + size;
713 for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
714 switch (*ptr) {
715 case '/':
716 entry++;
717 *ptr = '\0';
718 break;
719
720 case '\n':
721 break;
722
723 default:
724 break;
725 }
726 if (DEBUG(ARCH)) {
727 fprintf(debug_file, "Found svr4 archive name table with %lu entries\n",
728 (unsigned long)entry);
729 }
730 return 0;
731 }
732
733 if (name[1] == ' ' || name[1] == '\0')
734 return 2;
735
736 entry = (size_t)strtol(&name[1], &eptr, 0);
737 if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
738 if (DEBUG(ARCH)) {
739 fprintf(debug_file, "Could not parse SVR4 name %s\n", name);
740 }
741 return 2;
742 }
743 if (entry >= ar->fnamesize) {
744 if (DEBUG(ARCH)) {
745 fprintf(debug_file, "SVR4 entry offset %s is greater than %lu\n",
746 name, (unsigned long)ar->fnamesize);
747 }
748 return 2;
749 }
750
751 if (DEBUG(ARCH)) {
752 fprintf(debug_file, "Replaced %s with %s\n", name, &ar->fnametab[entry]);
753 }
754
755 snprintf(name, MAXPATHLEN + 1, "%s", &ar->fnametab[entry]);
756 return 1;
757 }
758 #endif
759
760
761 /*-
762 *-----------------------------------------------------------------------
763 * ArchFindMember --
764 * Locate a member of an archive, given the path of the archive and
765 * the path of the desired member. If the archive is to be modified,
766 * the mode should be "r+", if not, it should be "r".
767 * The passed struct ar_hdr structure is filled in.
768 *
769 * Input:
770 * archive Path to the archive
771 * member Name of member. If it is a path, only the last
772 * component is used.
773 * arhPtr Pointer to header structure to be filled in
774 * mode The mode for opening the stream
775 *
776 * Results:
777 * An FILE *, opened for reading and writing, positioned at the
778 * start of the member's struct ar_hdr, or NULL if the member was
779 * nonexistent. The current struct ar_hdr for member.
780 *-----------------------------------------------------------------------
781 */
782 static FILE *
783 ArchFindMember(const char *archive, const char *member, struct ar_hdr *arhPtr,
784 const char *mode)
785 {
786 FILE * arch; /* Stream to archive */
787 int size; /* Size of archive member */
788 char magic[SARMAG];
789 size_t len, tlen;
790 const char * base;
791
792 arch = fopen(archive, mode);
793 if (arch == NULL) {
794 return NULL;
795 }
796
797 /*
798 * We use the ARMAG string to make sure this is an archive we
799 * can handle...
800 */
801 if ((fread(magic, SARMAG, 1, arch) != 1) ||
802 (strncmp(magic, ARMAG, SARMAG) != 0)) {
803 fclose(arch);
804 return NULL;
805 }
806
807 /*
808 * Because of space constraints and similar things, files are archived
809 * using their final path components, not the entire thing, so we need
810 * to point 'member' to the final component, if there is one, to make
811 * the comparisons easier...
812 */
813 base = strrchr(member, '/');
814 if (base != NULL) {
815 member = base + 1;
816 }
817 len = tlen = strlen(member);
818 if (len > sizeof(arhPtr->ar_name)) {
819 tlen = sizeof(arhPtr->ar_name);
820 }
821
822 while (fread((char *)arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
823 if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag) ) != 0) {
824 /*
825 * The header is bogus, so the archive is bad
826 * and there's no way we can recover...
827 */
828 fclose(arch);
829 return NULL;
830 } else if (strncmp(member, arhPtr->ar_name, tlen) == 0) {
831 /*
832 * If the member's name doesn't take up the entire 'name' field,
833 * we have to be careful of matching prefixes. Names are space-
834 * padded to the right, so if the character in 'name' at the end
835 * of the matched string is anything but a space, this isn't the
836 * member we sought.
837 */
838 if (tlen != sizeof(arhPtr->ar_name) && arhPtr->ar_name[tlen] != ' '){
839 goto skip;
840 } else {
841 /*
842 * To make life easier, we reposition the file at the start
843 * of the header we just read before we return the stream.
844 * In a more general situation, it might be better to leave
845 * the file at the actual member, rather than its header, but
846 * not here...
847 */
848 if (fseek(arch, -(long)sizeof(struct ar_hdr), SEEK_CUR) != 0) {
849 fclose(arch);
850 return NULL;
851 }
852 return arch;
853 }
854 } else
855 #ifdef AR_EFMT1
856 /*
857 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
858 * first <namelen> bytes of the file
859 */
860 if (strncmp(arhPtr->ar_name, AR_EFMT1,
861 sizeof(AR_EFMT1) - 1) == 0 &&
862 ch_isdigit(arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) {
863
864 int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1)-1]);
865 char ename[MAXPATHLEN + 1];
866
867 if ((unsigned int)elen > MAXPATHLEN) {
868 fclose(arch);
869 return NULL;
870 }
871 if (fread(ename, (size_t)elen, 1, arch) != 1) {
872 fclose(arch);
873 return NULL;
874 }
875 ename[elen] = '\0';
876 if (DEBUG(ARCH) || DEBUG(MAKE)) {
877 fprintf(debug_file, "ArchFind: Extended format entry for %s\n", ename);
878 }
879 if (strncmp(ename, member, len) == 0) {
880 /* Found as extended name */
881 if (fseek(arch, -(long)sizeof(struct ar_hdr) - elen,
882 SEEK_CUR) != 0) {
883 fclose(arch);
884 return NULL;
885 }
886 return arch;
887 }
888 if (fseek(arch, -elen, SEEK_CUR) != 0) {
889 fclose(arch);
890 return NULL;
891 }
892 goto skip;
893 } else
894 #endif
895 {
896 skip:
897 /*
898 * This isn't the member we're after, so we need to advance the
899 * stream's pointer to the start of the next header. Files are
900 * padded with newlines to an even-byte boundary, so we need to
901 * extract the size of the file from the 'size' field of the
902 * header and round it up during the seek.
903 */
904 arhPtr->ar_size[sizeof(arhPtr->ar_size)-1] = '\0';
905 size = (int)strtol(arhPtr->ar_size, NULL, 10);
906 if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) {
907 fclose(arch);
908 return NULL;
909 }
910 }
911 }
912
913 /*
914 * We've looked everywhere, but the member is not to be found. Close the
915 * archive and return NULL -- an error.
916 */
917 fclose(arch);
918 return NULL;
919 }
920
921 /*-
922 *-----------------------------------------------------------------------
923 * Arch_Touch --
924 * Touch a member of an archive.
925 * The modification time of the entire archive is also changed.
926 * For a library, this could necessitate the re-ranlib'ing of the
927 * whole thing.
928 *
929 * Input:
930 * gn Node of member to touch
931 *
932 * Results:
933 * The 'time' field of the member's header is updated.
934 *-----------------------------------------------------------------------
935 */
936 void
937 Arch_Touch(GNode *gn)
938 {
939 FILE * arch; /* Stream open to archive, positioned properly */
940 struct ar_hdr arh; /* Current header describing member */
941 char *p1, *p2;
942
943 arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1),
944 Var_Value(MEMBER, gn, &p2),
945 &arh, "r+");
946
947 bmake_free(p1);
948 bmake_free(p2);
949
950 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
951
952 if (arch != NULL) {
953 (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
954 fclose(arch);
955 }
956 }
957
958 /* Given a node which represents a library, touch the thing, making sure that
959 * the table of contents also is touched.
960 *
961 * Both the modification time of the library and of the RANLIBMAG member are
962 * set to 'now'.
963 *
964 * Input:
965 * gn The node of the library to touch
966 */
967 void
968 Arch_TouchLib(GNode *gn)
969 {
970 #ifdef RANLIBMAG
971 FILE * arch; /* Stream open to archive */
972 struct ar_hdr arh; /* Header describing table of contents */
973 struct utimbuf times; /* Times for utime() call */
974
975 arch = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
976 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
977
978 if (arch != NULL) {
979 (void)fwrite((char *)&arh, sizeof(struct ar_hdr), 1, arch);
980 fclose(arch);
981
982 times.actime = times.modtime = now;
983 utime(gn->path, ×);
984 }
985 #else
986 (void)gn;
987 #endif
988 }
989
990 /* Return the modification time of a member of an archive. The mtime field
991 * of the given node is filled in with the value returned by the function.
992 *
993 * Input:
994 * gn Node describing archive member
995 */
996 time_t
997 Arch_MTime(GNode *gn)
998 {
999 struct ar_hdr *arhPtr; /* Header of desired member */
1000 time_t modTime; /* Modification time as an integer */
1001 char *p1, *p2;
1002
1003 arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1),
1004 Var_Value(MEMBER, gn, &p2),
1005 TRUE);
1006
1007 bmake_free(p1);
1008 bmake_free(p2);
1009
1010 if (arhPtr != NULL) {
1011 modTime = (time_t)strtol(arhPtr->ar_date, NULL, 10);
1012 } else {
1013 modTime = 0;
1014 }
1015
1016 gn->mtime = modTime;
1017 return modTime;
1018 }
1019
1020 /* Given a non-existent archive member's node, get its modification time from
1021 * its archived form, if it exists. gn->mtime is filled in as well. */
1022 time_t
1023 Arch_MemMTime(GNode *gn)
1024 {
1025 GNodeListNode *ln;
1026 GNode *pgn;
1027
1028 Lst_Open(gn->parents);
1029 while ((ln = Lst_Next(gn->parents)) != NULL) {
1030 pgn = LstNode_Datum(ln);
1031
1032 if (pgn->type & OP_ARCHV) {
1033 /*
1034 * If the parent is an archive specification and is being made
1035 * and its member's name matches the name of the node we were
1036 * given, record the modification time of the parent in the
1037 * child. We keep searching its parents in case some other
1038 * parent requires this child to exist...
1039 */
1040 const char *nameStart = strchr(pgn->name, '(') + 1;
1041 const char *nameEnd = strchr(nameStart, ')');
1042 size_t nameLen = (size_t)(nameEnd - nameStart);
1043
1044 if ((pgn->flags & REMAKE) &&
1045 strncmp(nameStart, gn->name, nameLen) == 0) {
1046 gn->mtime = Arch_MTime(pgn);
1047 }
1048 } else if (pgn->flags & REMAKE) {
1049 /*
1050 * Something which isn't a library depends on the existence of
1051 * this target, so it needs to exist.
1052 */
1053 gn->mtime = 0;
1054 break;
1055 }
1056 }
1057
1058 Lst_Close(gn->parents);
1059
1060 return gn->mtime;
1061 }
1062
1063 /* Search for a library along the given search path.
1064 *
1065 * The node's 'path' field is set to the found path (including the
1066 * actual file name, not -l...). If the system can handle the -L
1067 * flag when linking (or we cannot find the library), we assume that
1068 * the user has placed the .LIBS variable in the final linking
1069 * command (or the linker will know where to find it) and set the
1070 * TARGET variable for this node to be the node's name. Otherwise,
1071 * we set the TARGET variable to be the full path of the library,
1072 * as returned by Dir_FindFile.
1073 *
1074 * Input:
1075 * gn Node of library to find
1076 * path Search path
1077 */
1078 void
1079 Arch_FindLib(GNode *gn, SearchPath *path)
1080 {
1081 char *libName; /* file name for archive */
1082 size_t sz = strlen(gn->name) + 6 - 2;
1083
1084 libName = bmake_malloc(sz);
1085 snprintf(libName, sz, "lib%s.a", &gn->name[2]);
1086
1087 gn->path = Dir_FindFile(libName, path);
1088
1089 free(libName);
1090
1091 #ifdef LIBRARIES
1092 Var_Set(TARGET, gn->name, gn);
1093 #else
1094 Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn);
1095 #endif
1096 }
1097
1098 /* Decide if a node with the OP_LIB attribute is out-of-date. Called from
1099 * Make_OODate to make its life easier.
1100 * The library will be hashed if it hasn't been already.
1101 *
1102 * There are several ways for a library to be out-of-date that are
1103 * not available to ordinary files. In addition, there are ways
1104 * that are open to regular files that are not available to
1105 * libraries. A library that is only used as a source is never
1106 * considered out-of-date by itself. This does not preclude the
1107 * library's modification time from making its parent be out-of-date.
1108 * A library will be considered out-of-date for any of these reasons,
1109 * given that it is a target on a dependency line somewhere:
1110 *
1111 * Its modification time is less than that of one of its sources
1112 * (gn->mtime < gn->cmgn->mtime).
1113 *
1114 * Its modification time is greater than the time at which the make
1115 * began (i.e. it's been modified in the course of the make, probably
1116 * by archiving).
1117 *
1118 * The modification time of one of its sources is greater than the one
1119 * of its RANLIBMAG member (i.e. its table of contents is out-of-date).
1120 * We don't compare of the archive time vs. TOC time because they can be
1121 * too close. In my opinion we should not bother with the TOC at all
1122 * since this is used by 'ar' rules that affect the data contents of the
1123 * archive, not by ranlib rules, which affect the TOC.
1124 *
1125 * Input:
1126 * gn The library's graph node
1127 *
1128 * Results:
1129 * TRUE if the library is out-of-date. FALSE otherwise.
1130 */
1131 Boolean
1132 Arch_LibOODate(GNode *gn)
1133 {
1134 Boolean oodate;
1135
1136 if (gn->type & OP_PHONY) {
1137 oodate = TRUE;
1138 } else if (OP_NOP(gn->type) && Lst_IsEmpty(gn->children)) {
1139 oodate = FALSE;
1140 } else if ((!Lst_IsEmpty(gn->children) && gn->cmgn == NULL) ||
1141 (gn->mtime > now) ||
1142 (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime)) {
1143 oodate = TRUE;
1144 } else {
1145 #ifdef RANLIBMAG
1146 struct ar_hdr *arhPtr; /* Header for __.SYMDEF */
1147 int modTimeTOC; /* The table-of-contents's mod time */
1148
1149 arhPtr = ArchStatMember(gn->path, RANLIBMAG, FALSE);
1150
1151 if (arhPtr != NULL) {
1152 modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10);
1153
1154 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1155 fprintf(debug_file, "%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
1156 }
1157 oodate = (gn->cmgn == NULL || gn->cmgn->mtime > modTimeTOC);
1158 } else {
1159 /*
1160 * A library w/o a table of contents is out-of-date
1161 */
1162 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1163 fprintf(debug_file, "No t.o.c....");
1164 }
1165 oodate = TRUE;
1166 }
1167 #else
1168 oodate = FALSE;
1169 #endif
1170 }
1171 return oodate;
1172 }
1173
1174 /* Initialize things for this module. */
1175 void
1176 Arch_Init(void)
1177 {
1178 archives = Lst_Init();
1179 }
1180
1181 /* Clean up things for this module. */
1182 void
1183 Arch_End(void)
1184 {
1185 #ifdef CLEANUP
1186 Lst_Destroy(archives, ArchFree);
1187 #endif
1188 }
1189
1190 Boolean
1191 Arch_IsLib(GNode *gn)
1192 {
1193 static const char armag[] = "!<arch>\n";
1194 char buf[sizeof armag - 1];
1195 int fd;
1196
1197 if ((fd = open(gn->path, O_RDONLY)) == -1)
1198 return FALSE;
1199
1200 if (read(fd, buf, sizeof buf) != sizeof buf) {
1201 (void)close(fd);
1202 return FALSE;
1203 }
1204
1205 (void)close(fd);
1206
1207 return memcmp(buf, armag, sizeof buf) == 0;
1208 }
1209