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