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