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