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