1 |
|
|
/* $OpenBSD: arch.c,v 1.89 2017/07/24 12:07:46 espie Exp $ */ |
2 |
|
|
/* $NetBSD: arch.c,v 1.17 1996/11/06 17:58:59 christos Exp $ */ |
3 |
|
|
|
4 |
|
|
/* |
5 |
|
|
* Copyright (c) 1999,2000 Marc Espie. |
6 |
|
|
* |
7 |
|
|
* Extensive code changes for the OpenBSD project. |
8 |
|
|
* |
9 |
|
|
* Redistribution and use in source and binary forms, with or without |
10 |
|
|
* modification, are permitted provided that the following conditions |
11 |
|
|
* are met: |
12 |
|
|
* 1. Redistributions of source code must retain the above copyright |
13 |
|
|
* notice, this list of conditions and the following disclaimer. |
14 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
|
|
* notice, this list of conditions and the following disclaimer in the |
16 |
|
|
* documentation and/or other materials provided with the distribution. |
17 |
|
|
* |
18 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS |
19 |
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 |
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 |
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD |
22 |
|
|
* PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 |
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 |
|
|
*/ |
30 |
|
|
/* |
31 |
|
|
* Copyright (c) 1988, 1989, 1990, 1993 |
32 |
|
|
* The Regents of the University of California. All rights reserved. |
33 |
|
|
* Copyright (c) 1989 by Berkeley Softworks |
34 |
|
|
* All rights reserved. |
35 |
|
|
* |
36 |
|
|
* This code is derived from software contributed to Berkeley by |
37 |
|
|
* Adam de Boor. |
38 |
|
|
* |
39 |
|
|
* Redistribution and use in source and binary forms, with or without |
40 |
|
|
* modification, are permitted provided that the following conditions |
41 |
|
|
* are met: |
42 |
|
|
* 1. Redistributions of source code must retain the above copyright |
43 |
|
|
* notice, this list of conditions and the following disclaimer. |
44 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
45 |
|
|
* notice, this list of conditions and the following disclaimer in the |
46 |
|
|
* documentation and/or other materials provided with the distribution. |
47 |
|
|
* 3. Neither the name of the University nor the names of its contributors |
48 |
|
|
* may be used to endorse or promote products derived from this software |
49 |
|
|
* without specific prior written permission. |
50 |
|
|
* |
51 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
52 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
53 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
54 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
55 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
56 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
57 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
58 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
59 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
60 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
61 |
|
|
* SUCH DAMAGE. |
62 |
|
|
*/ |
63 |
|
|
|
64 |
|
|
/* |
65 |
|
|
* Once again, cacheing/hashing comes into play in the manipulation |
66 |
|
|
* of archives. The first time an archive is referenced, all of its members' |
67 |
|
|
* headers are read and hashed and the archive closed again. All hashed |
68 |
|
|
* archives are kept in a hash (archives) which is searched each time |
69 |
|
|
* an archive member is referenced. |
70 |
|
|
* |
71 |
|
|
*/ |
72 |
|
|
|
73 |
|
|
#include <ar.h> |
74 |
|
|
#include <assert.h> |
75 |
|
|
#include <ctype.h> |
76 |
|
|
#include <fcntl.h> |
77 |
|
|
#include <limits.h> |
78 |
|
|
#include <stddef.h> |
79 |
|
|
#include <stdint.h> |
80 |
|
|
#include <stdio.h> |
81 |
|
|
#include <stdlib.h> |
82 |
|
|
#include <string.h> |
83 |
|
|
#include <unistd.h> |
84 |
|
|
#include <ohash.h> |
85 |
|
|
#include "config.h" |
86 |
|
|
#include "defines.h" |
87 |
|
|
#include "buf.h" |
88 |
|
|
#include "dir.h" |
89 |
|
|
#include "direxpand.h" |
90 |
|
|
#include "arch.h" |
91 |
|
|
#include "var.h" |
92 |
|
|
#include "targ.h" |
93 |
|
|
#include "memory.h" |
94 |
|
|
#include "gnode.h" |
95 |
|
|
#include "timestamp.h" |
96 |
|
|
#include "lst.h" |
97 |
|
|
|
98 |
|
|
#ifdef TARGET_MACHINE |
99 |
|
|
#undef MACHINE |
100 |
|
|
#define MACHINE TARGET_MACHINE |
101 |
|
|
#endif |
102 |
|
|
#ifdef TARGET_MACHINE_ARCH |
103 |
|
|
#undef MACHINE_ARCH |
104 |
|
|
#define MACHINE_ARCH TARGET_MACHINE_ARCH |
105 |
|
|
#endif |
106 |
|
|
#ifdef TARGET_MACHINE_CPU |
107 |
|
|
#undef MACHINE_CPU |
108 |
|
|
#define MACHINE_CPU TARGET_MACHINE_CPU |
109 |
|
|
#endif |
110 |
|
|
|
111 |
|
|
static struct ohash archives; /* Archives we've already examined. */ |
112 |
|
|
|
113 |
|
|
typedef struct Arch_ { |
114 |
|
|
struct ohash members; /* All the members of this archive, as |
115 |
|
|
* struct arch_member entries. */ |
116 |
|
|
char name[1]; /* Archive name. */ |
117 |
|
|
} Arch; |
118 |
|
|
|
119 |
|
|
/* Used to get to ar's field sizes. */ |
120 |
|
|
static struct ar_hdr *dummy; |
121 |
|
|
#define AR_NAME_SIZE (sizeof(dummy->ar_name)) |
122 |
|
|
#define AR_DATE_SIZE (sizeof(dummy->ar_date)) |
123 |
|
|
|
124 |
|
|
/* Each archive member is tied to an arch_member structure, |
125 |
|
|
* suitable for hashing. */ |
126 |
|
|
struct arch_member { |
127 |
|
|
struct timespec mtime; /* Member modification date. */ |
128 |
|
|
char date[AR_DATE_SIZE+1]; /* Same, before conversion to numeric |
129 |
|
|
* value. */ |
130 |
|
|
char name[1]; /* Member name. */ |
131 |
|
|
}; |
132 |
|
|
|
133 |
|
|
static struct ohash_info members_info = { |
134 |
|
|
offsetof(struct arch_member, name), NULL, |
135 |
|
|
hash_calloc, hash_free, element_alloc |
136 |
|
|
}; |
137 |
|
|
|
138 |
|
|
static struct ohash_info arch_info = { |
139 |
|
|
offsetof(Arch, name), NULL, hash_calloc, hash_free, element_alloc |
140 |
|
|
}; |
141 |
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
static struct arch_member *new_arch_member(struct ar_hdr *, const char *); |
145 |
|
|
static struct timespec mtime_of_member(struct arch_member *); |
146 |
|
|
static long field2long(const char *, size_t); |
147 |
|
|
static Arch *read_archive(const char *, const char *); |
148 |
|
|
|
149 |
|
|
static struct timespec ArchMTimeMember(const char *, const char *, bool); |
150 |
|
|
static FILE *ArchFindMember(const char *, const char *, struct ar_hdr *, const char *); |
151 |
|
|
static void ArchTouch(const char *, const char *); |
152 |
|
|
#if defined(__svr4__) || defined(__SVR4) || \ |
153 |
|
|
(defined(__OpenBSD__) && defined(__ELF__)) |
154 |
|
|
#define SVR4ARCHIVES |
155 |
|
|
#endif |
156 |
|
|
static bool parse_archive(Buffer, const char **, Lst, SymTable *); |
157 |
|
|
static void add_archive_node(Lst, const char *); |
158 |
|
|
|
159 |
|
|
struct SVR4namelist { |
160 |
|
|
char *fnametab; /* Extended name table strings */ |
161 |
|
|
size_t fnamesize; /* Size of the string table */ |
162 |
|
|
}; |
163 |
|
|
|
164 |
|
|
#ifdef SVR4ARCHIVES |
165 |
|
|
static const char *svr4list = "Archive list"; |
166 |
|
|
|
167 |
|
|
static char *ArchSVR4Entry(struct SVR4namelist *, const char *, size_t, FILE *); |
168 |
|
|
#endif |
169 |
|
|
|
170 |
|
|
static struct arch_member * |
171 |
|
|
new_arch_member(struct ar_hdr *hdr, const char *name) |
172 |
|
|
{ |
173 |
|
|
const char *end = NULL; |
174 |
|
|
struct arch_member *n; |
175 |
|
|
|
176 |
|
|
n = ohash_create_entry(&members_info, name, &end); |
177 |
|
|
/* XXX ar entries are NOT null terminated. */ |
178 |
|
|
memcpy(n->date, &(hdr->ar_date), AR_DATE_SIZE); |
179 |
|
|
n->date[AR_DATE_SIZE] = '\0'; |
180 |
|
|
/* Don't compute mtime before it is needed. */ |
181 |
|
|
ts_set_out_of_date(n->mtime); |
182 |
|
|
return n; |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
static struct timespec |
186 |
|
|
mtime_of_member(struct arch_member *m) |
187 |
|
|
{ |
188 |
|
|
if (is_out_of_date(m->mtime)) |
189 |
|
|
ts_set_from_time_t((time_t) strtoll(m->date, NULL, 10), |
190 |
|
|
m->mtime); |
191 |
|
|
return m->mtime; |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
bool |
195 |
|
|
Arch_ParseArchive(const char **line, Lst nodes, SymTable *ctxt) |
196 |
|
|
{ |
197 |
|
|
bool result; |
198 |
|
|
BUFFER expand; |
199 |
|
|
|
200 |
|
|
Buf_Init(&expand, MAKE_BSIZE); |
201 |
|
|
result = parse_archive(&expand, line, nodes, ctxt); |
202 |
|
|
Buf_Destroy(&expand); |
203 |
|
|
return result; |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
static void |
207 |
|
|
add_archive_node(Lst nodes, const char *name) |
208 |
|
|
{ |
209 |
|
|
GNode *gn; |
210 |
|
|
|
211 |
|
|
gn = Targ_FindNode(name, TARG_CREATE); |
212 |
|
|
gn->type |= OP_ARCHV; |
213 |
|
|
Lst_AtEnd(nodes, gn); |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
static bool |
217 |
|
|
parse_archive(Buffer expand, const char **linePtr, Lst nodeLst, SymTable *ctxt) |
218 |
|
|
{ |
219 |
|
|
const char *cp; /* Pointer into line */ |
220 |
|
|
const char *lib; /* Library-part of specification */ |
221 |
|
|
const char *elib; |
222 |
|
|
const char *member; /* Member-part of specification */ |
223 |
|
|
const char *emember; |
224 |
|
|
bool subst_lib; |
225 |
|
|
|
226 |
|
|
/* figure out the library name part */ |
227 |
|
|
lib = *linePtr; |
228 |
|
|
subst_lib = false; |
229 |
|
|
|
230 |
|
|
for (cp = lib; *cp != '(' && *cp != '\0';) { |
231 |
|
|
if (*cp == '$') { |
232 |
|
|
if (!Var_ParseSkip(&cp, ctxt)) |
233 |
|
|
return false; |
234 |
|
|
subst_lib = true; |
235 |
|
|
} else |
236 |
|
|
cp++; |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
elib = cp; |
240 |
|
|
if (subst_lib) { |
241 |
|
|
lib = Var_Substi(lib, elib, ctxt, true); |
242 |
|
|
elib = lib + strlen(lib); |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
if (*cp == '\0') { |
246 |
|
|
printf("Unclosed parenthesis in archive specification\n"); |
247 |
|
|
return false; |
248 |
|
|
} |
249 |
|
|
cp++; |
250 |
|
|
/* iterate on members, that may be separated by spaces */ |
251 |
|
|
for (;;) { |
252 |
|
|
/* First skip to the start of the member's name, mark that |
253 |
|
|
* place and skip to the end of it (either white-space or |
254 |
|
|
* a close paren). */ |
255 |
|
|
bool subst_member = false; |
256 |
|
|
|
257 |
|
|
while (ISSPACE(*cp)) |
258 |
|
|
cp++; |
259 |
|
|
member = cp; |
260 |
|
|
while (*cp != '\0' && *cp != ')' && !ISSPACE(*cp)) { |
261 |
|
|
if (*cp == '$') { |
262 |
|
|
if (!Var_ParseSkip(&cp, ctxt)) |
263 |
|
|
return false; |
264 |
|
|
subst_member = true; |
265 |
|
|
} else |
266 |
|
|
cp++; |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
/* If the specification ends without a closing parenthesis, |
270 |
|
|
* chances are there's something wrong (like a missing |
271 |
|
|
* backslash), so it's better to return failure than allow such |
272 |
|
|
* things to happen. */ |
273 |
|
|
if (*cp == '\0' || ISSPACE(*cp)) { |
274 |
|
|
printf("No closing parenthesis in archive specification\n"); |
275 |
|
|
return false; |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
/* If we didn't move anywhere, we must be done. */ |
279 |
|
|
if (cp == member) |
280 |
|
|
break; |
281 |
|
|
|
282 |
|
|
emember = cp; |
283 |
|
|
|
284 |
|
|
/* XXX: This should be taken care of intelligently by |
285 |
|
|
* SuffExpandChildren, both for the archive and the member |
286 |
|
|
* portions. */ |
287 |
|
|
|
288 |
|
|
/* If member contains variables, try and substitute for them. |
289 |
|
|
* This will slow down archive specs with dynamic sources, of |
290 |
|
|
* course, since we'll be (non-)substituting them three times, |
291 |
|
|
* but them's the breaks -- we need to do this since |
292 |
|
|
* SuffExpandChildren calls us, otherwise we could assume the |
293 |
|
|
* thing would be taken care of later. */ |
294 |
|
|
if (subst_member) { |
295 |
|
|
const char *oldMemberName = member; |
296 |
|
|
const char *result; |
297 |
|
|
|
298 |
|
|
member = Var_Substi(member, emember, ctxt, true); |
299 |
|
|
|
300 |
|
|
/* Now form an archive spec and recurse to deal with |
301 |
|
|
* nested variables and multi-word variable values.... |
302 |
|
|
* The results are just placed at the end of the |
303 |
|
|
* nodeLst we're returning. */ |
304 |
|
|
Buf_Addi(expand, lib, elib); |
305 |
|
|
Buf_AddChar(expand, '('); |
306 |
|
|
Buf_AddString(expand, member); |
307 |
|
|
Buf_AddChar(expand, ')'); |
308 |
|
|
result = Buf_Retrieve(expand); |
309 |
|
|
|
310 |
|
|
if (strchr(member, '$') && |
311 |
|
|
memcmp(member, oldMemberName, |
312 |
|
|
emember - oldMemberName) == 0) { |
313 |
|
|
/* Must contain dynamic sources, so we can't |
314 |
|
|
* deal with it now. let SuffExpandChildren |
315 |
|
|
* handle it later */ |
316 |
|
|
add_archive_node(nodeLst, result); |
317 |
|
|
} else if (!Arch_ParseArchive(&result, nodeLst, ctxt)) |
318 |
|
|
return false; |
319 |
|
|
Buf_Reset(expand); |
320 |
|
|
} else if (Dir_HasWildcardsi(member, emember)) { |
321 |
|
|
LIST members; |
322 |
|
|
char *m; |
323 |
|
|
|
324 |
|
|
Lst_Init(&members); |
325 |
|
|
|
326 |
|
|
Dir_Expandi(member, emember, defaultPath, &members); |
327 |
|
|
while ((m = Lst_DeQueue(&members)) != NULL) { |
328 |
|
|
Buf_Addi(expand, lib, elib); |
329 |
|
|
Buf_AddChar(expand, '('); |
330 |
|
|
Buf_AddString(expand, m); |
331 |
|
|
Buf_AddChar(expand, ')'); |
332 |
|
|
free(m); |
333 |
|
|
add_archive_node(nodeLst, Buf_Retrieve(expand)); |
334 |
|
|
Buf_Reset(expand); |
335 |
|
|
} |
336 |
|
|
} else { |
337 |
|
|
Buf_Addi(expand, lib, elib); |
338 |
|
|
Buf_AddChar(expand, '('); |
339 |
|
|
Buf_Addi(expand, member, emember); |
340 |
|
|
Buf_AddChar(expand, ')'); |
341 |
|
|
add_archive_node(nodeLst, Buf_Retrieve(expand)); |
342 |
|
|
Buf_Reset(expand); |
343 |
|
|
} |
344 |
|
|
if (subst_member) |
345 |
|
|
free((char *)member); |
346 |
|
|
|
347 |
|
|
} |
348 |
|
|
|
349 |
|
|
if (subst_lib) |
350 |
|
|
free((char *)lib); |
351 |
|
|
|
352 |
|
|
/* We promised the pointer would be set up at the next non-space, so |
353 |
|
|
* we must advance cp there before setting *linePtr... (note that on |
354 |
|
|
* entrance to the loop, cp is guaranteed to point at a ')') */ |
355 |
|
|
do { |
356 |
|
|
cp++; |
357 |
|
|
} while (ISSPACE(*cp)); |
358 |
|
|
|
359 |
|
|
*linePtr = cp; |
360 |
|
|
return true; |
361 |
|
|
} |
362 |
|
|
|
363 |
|
|
/* Helper function: ar fields are not null terminated. */ |
364 |
|
|
static long |
365 |
|
|
field2long(const char *field, size_t length) |
366 |
|
|
{ |
367 |
|
|
static char enough[32]; |
368 |
|
|
|
369 |
|
|
assert(length < sizeof(enough)); |
370 |
|
|
memcpy(enough, field, length); |
371 |
|
|
enough[length] = '\0'; |
372 |
|
|
return strtol(enough, NULL, 10); |
373 |
|
|
} |
374 |
|
|
|
375 |
|
|
static Arch * |
376 |
|
|
read_archive(const char *archive, const char *earchive) |
377 |
|
|
{ |
378 |
|
|
FILE *arch; /* Stream to archive */ |
379 |
|
|
char magic[SARMAG]; |
380 |
|
|
Arch *ar; |
381 |
|
|
struct SVR4namelist list; |
382 |
|
|
|
383 |
|
|
list.fnametab = NULL; |
384 |
|
|
|
385 |
|
|
/* When we encounter an archive for the first time, we read its |
386 |
|
|
* whole contents, to place it in the cache. */ |
387 |
|
|
arch = fopen(archive, "r"); |
388 |
|
|
if (arch == NULL) |
389 |
|
|
return NULL; |
390 |
|
|
|
391 |
|
|
/* Make sure this is an archive we can handle. */ |
392 |
|
|
if ((fread(magic, SARMAG, 1, arch) != 1) || |
393 |
|
|
(strncmp(magic, ARMAG, SARMAG) != 0)) { |
394 |
|
|
fclose(arch); |
395 |
|
|
return NULL; |
396 |
|
|
} |
397 |
|
|
|
398 |
|
|
ar = ohash_create_entry(&arch_info, archive, &earchive); |
399 |
|
|
ohash_init(&ar->members, 8, &members_info); |
400 |
|
|
|
401 |
|
|
for (;;) { |
402 |
|
|
size_t n; |
403 |
|
|
struct ar_hdr arHeader; /* Archive-member header */ |
404 |
|
|
off_t size; /* Size of archive member */ |
405 |
|
|
char buffer[PATH_MAX]; |
406 |
|
|
char *memberName; /* Current member name while hashing. */ |
407 |
|
|
char *cp; |
408 |
|
|
|
409 |
|
|
memberName = buffer; |
410 |
|
|
n = fread(&arHeader, 1, sizeof(struct ar_hdr), arch); |
411 |
|
|
|
412 |
|
|
/* Whole archive read ok. */ |
413 |
|
|
if (n == 0 && feof(arch)) { |
414 |
|
|
free(list.fnametab); |
415 |
|
|
fclose(arch); |
416 |
|
|
return ar; |
417 |
|
|
} |
418 |
|
|
if (n < sizeof(struct ar_hdr)) |
419 |
|
|
break; |
420 |
|
|
|
421 |
|
|
if (memcmp(arHeader.ar_fmag, ARFMAG, sizeof(arHeader.ar_fmag)) |
422 |
|
|
!= 0) { |
423 |
|
|
/* header is bogus. */ |
424 |
|
|
break; |
425 |
|
|
} else { |
426 |
|
|
/* We need to advance the stream's pointer to the start |
427 |
|
|
* of the next header. Records are padded with |
428 |
|
|
* newlines to an even-byte boundary, so we need to |
429 |
|
|
* extract the size of the record and round it up |
430 |
|
|
* during the seek. */ |
431 |
|
|
size = (off_t) field2long(arHeader.ar_size, |
432 |
|
|
sizeof(arHeader.ar_size)); |
433 |
|
|
|
434 |
|
|
(void)memcpy(memberName, arHeader.ar_name, |
435 |
|
|
AR_NAME_SIZE); |
436 |
|
|
/* Find real end of name (strip extranous ' ') */ |
437 |
|
|
for (cp = memberName + AR_NAME_SIZE - 1; *cp == ' ';) |
438 |
|
|
cp--; |
439 |
|
|
cp[1] = '\0'; |
440 |
|
|
|
441 |
|
|
#ifdef SVR4ARCHIVES |
442 |
|
|
/* SVR4 names are slash terminated. Also svr4 extended |
443 |
|
|
* AR format. |
444 |
|
|
*/ |
445 |
|
|
if (memberName[0] == '/') { |
446 |
|
|
/* SVR4 magic mode. */ |
447 |
|
|
memberName = ArchSVR4Entry(&list, memberName, |
448 |
|
|
size, arch); |
449 |
|
|
if (memberName == NULL) |
450 |
|
|
/* Invalid data */ |
451 |
|
|
break; |
452 |
|
|
else if (memberName == svr4list) |
453 |
|
|
/* List of files entry */ |
454 |
|
|
continue; |
455 |
|
|
/* Got the entry. */ |
456 |
|
|
/* XXX this assumes further processing, such as |
457 |
|
|
* AR_EFMT1, also applies to SVR4ARCHIVES. */ |
458 |
|
|
} |
459 |
|
|
else { |
460 |
|
|
if (cp[0] == '/') |
461 |
|
|
cp[0] = '\0'; |
462 |
|
|
} |
463 |
|
|
#endif |
464 |
|
|
|
465 |
|
|
#ifdef AR_EFMT1 |
466 |
|
|
/* BSD 4.4 extended AR format: #1/<namelen>, with name |
467 |
|
|
* as the first <namelen> bytes of the file. */ |
468 |
|
|
if (memcmp(memberName, AR_EFMT1, sizeof(AR_EFMT1) - 1) |
469 |
|
|
== 0 && ISDIGIT(memberName[sizeof(AR_EFMT1) - 1])) { |
470 |
|
|
|
471 |
|
|
int elen = atoi(memberName + |
472 |
|
|
sizeof(AR_EFMT1)-1); |
473 |
|
|
|
474 |
|
|
if (elen <= 0 || elen >= PATH_MAX) |
475 |
|
|
break; |
476 |
|
|
memberName = buffer; |
477 |
|
|
if (fread(memberName, elen, 1, arch) != 1) |
478 |
|
|
break; |
479 |
|
|
memberName[elen] = '\0'; |
480 |
|
|
if (fseek(arch, -elen, SEEK_CUR) != 0) |
481 |
|
|
break; |
482 |
|
|
if (DEBUG(ARCH) || DEBUG(MAKE)) |
483 |
|
|
printf("ArchStat: Extended format entry for %s\n", |
484 |
|
|
memberName); |
485 |
|
|
} |
486 |
|
|
#endif |
487 |
|
|
|
488 |
|
|
ohash_insert(&ar->members, |
489 |
|
|
ohash_qlookup(&ar->members, memberName), |
490 |
|
|
new_arch_member(&arHeader, memberName)); |
491 |
|
|
} |
492 |
|
|
if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) |
493 |
|
|
break; |
494 |
|
|
} |
495 |
|
|
|
496 |
|
|
fclose(arch); |
497 |
|
|
ohash_delete(&ar->members); |
498 |
|
|
free(list.fnametab); |
499 |
|
|
free(ar); |
500 |
|
|
return NULL; |
501 |
|
|
} |
502 |
|
|
|
503 |
|
|
/*- |
504 |
|
|
*----------------------------------------------------------------------- |
505 |
|
|
* ArchMTimeMember -- |
506 |
|
|
* Find the modification time of an archive's member, given the |
507 |
|
|
* path to the archive and the path to the desired member. |
508 |
|
|
* |
509 |
|
|
* Results: |
510 |
|
|
* The archive member's modification time, or OUT_OF_DATE if member |
511 |
|
|
* was not found (convenient, so that missing members are always |
512 |
|
|
* out of date). |
513 |
|
|
* |
514 |
|
|
* Side Effects: |
515 |
|
|
* Cache the whole archive contents if hash is true. |
516 |
|
|
*----------------------------------------------------------------------- |
517 |
|
|
*/ |
518 |
|
|
static struct timespec |
519 |
|
|
ArchMTimeMember( |
520 |
|
|
const char *archive, /* Path to the archive */ |
521 |
|
|
const char *member, /* Name of member. If it is a path, only the |
522 |
|
|
* last component is used. */ |
523 |
|
|
bool hash) /* true if archive should be hashed if not |
524 |
|
|
* already so. */ |
525 |
|
|
{ |
526 |
|
|
FILE *arch; /* Stream to archive */ |
527 |
|
|
Arch *ar; /* Archive descriptor */ |
528 |
|
|
unsigned int slot; /* Place of archive in the archives hash */ |
529 |
|
|
const char *end = NULL; |
530 |
|
|
const char *cp; |
531 |
|
|
struct timespec result; |
532 |
|
|
|
533 |
|
|
ts_set_out_of_date(result); |
534 |
|
|
/* Because of space constraints and similar things, files are archived |
535 |
|
|
* using their final path components, not the entire thing, so we need |
536 |
|
|
* to point 'member' to the final component, if there is one, to make |
537 |
|
|
* the comparisons easier... */ |
538 |
|
|
cp = strrchr(member, '/'); |
539 |
|
|
if (cp != NULL) |
540 |
|
|
member = cp + 1; |
541 |
|
|
|
542 |
|
|
/* Try to find archive in cache. */ |
543 |
|
|
slot = ohash_qlookupi(&archives, archive, &end); |
544 |
|
|
ar = ohash_find(&archives, slot); |
545 |
|
|
|
546 |
|
|
/* If not found, get it now. */ |
547 |
|
|
if (ar == NULL) { |
548 |
|
|
if (!hash) { |
549 |
|
|
/* Quick path: no need to hash the whole archive, just |
550 |
|
|
* use ArchFindMember to get the member's header and |
551 |
|
|
* close the stream again. */ |
552 |
|
|
struct ar_hdr arHeader; |
553 |
|
|
|
554 |
|
|
arch = ArchFindMember(archive, member, &arHeader, "r"); |
555 |
|
|
|
556 |
|
|
if (arch != NULL) { |
557 |
|
|
fclose(arch); |
558 |
|
|
ts_set_from_time_t( |
559 |
|
|
(time_t)strtol(arHeader.ar_date, NULL, 10), |
560 |
|
|
result); |
561 |
|
|
} |
562 |
|
|
return result; |
563 |
|
|
} |
564 |
|
|
ar = read_archive(archive, end); |
565 |
|
|
if (ar != NULL) |
566 |
|
|
ohash_insert(&archives, slot, ar); |
567 |
|
|
} |
568 |
|
|
|
569 |
|
|
/* If archive was found, get entry we seek. */ |
570 |
|
|
if (ar != NULL) { |
571 |
|
|
struct arch_member *he; |
572 |
|
|
end = NULL; |
573 |
|
|
|
574 |
|
|
he = ohash_find(&ar->members, ohash_qlookupi(&ar->members, |
575 |
|
|
member, &end)); |
576 |
|
|
if (he != NULL) |
577 |
|
|
return mtime_of_member(he); |
578 |
|
|
else { |
579 |
|
|
if ((size_t)(end - member) > AR_NAME_SIZE) { |
580 |
|
|
/* Try truncated name. */ |
581 |
|
|
end = member + AR_NAME_SIZE; |
582 |
|
|
he = ohash_find(&ar->members, |
583 |
|
|
ohash_qlookupi(&ar->members, member, &end)); |
584 |
|
|
if (he != NULL) |
585 |
|
|
return mtime_of_member(he); |
586 |
|
|
} |
587 |
|
|
} |
588 |
|
|
} |
589 |
|
|
return result; |
590 |
|
|
} |
591 |
|
|
|
592 |
|
|
#ifdef SVR4ARCHIVES |
593 |
|
|
/*- |
594 |
|
|
*----------------------------------------------------------------------- |
595 |
|
|
* ArchSVR4Entry -- |
596 |
|
|
* Parse an SVR4 style entry that begins with a slash. |
597 |
|
|
* If it is "//", then load the table of filenames |
598 |
|
|
* If it is "/<offset>", then try to substitute the long file name |
599 |
|
|
* from offset of a table previously read. |
600 |
|
|
* |
601 |
|
|
* Results: |
602 |
|
|
* svr4list: just read a list of names |
603 |
|
|
* NULL: error occurred |
604 |
|
|
* extended name |
605 |
|
|
* |
606 |
|
|
* Side-effect: |
607 |
|
|
* For a list of names, store the list in l. |
608 |
|
|
*----------------------------------------------------------------------- |
609 |
|
|
*/ |
610 |
|
|
|
611 |
|
|
static char * |
612 |
|
|
ArchSVR4Entry(struct SVR4namelist *l, const char *name, size_t size, FILE *arch) |
613 |
|
|
{ |
614 |
|
|
#define ARLONGNAMES1 "/" |
615 |
|
|
#define ARLONGNAMES2 "ARFILENAMES" |
616 |
|
|
size_t entry; |
617 |
|
|
char *ptr, *eptr; |
618 |
|
|
|
619 |
|
|
assert(name[0] == '/'); |
620 |
|
|
name++; |
621 |
|
|
/* First comes a table of archive names, to be used by subsequent |
622 |
|
|
* calls. */ |
623 |
|
|
if (memcmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 || |
624 |
|
|
memcmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) { |
625 |
|
|
|
626 |
|
|
if (l->fnametab != NULL) { |
627 |
|
|
if (DEBUG(ARCH)) |
628 |
|
|
printf("Attempted to redefine an SVR4 name table\n"); |
629 |
|
|
return NULL; |
630 |
|
|
} |
631 |
|
|
|
632 |
|
|
l->fnametab = emalloc(size); |
633 |
|
|
l->fnamesize = size; |
634 |
|
|
|
635 |
|
|
if (fread(l->fnametab, size, 1, arch) != 1) { |
636 |
|
|
if (DEBUG(ARCH)) |
637 |
|
|
printf("Reading an SVR4 name table failed\n"); |
638 |
|
|
return NULL; |
639 |
|
|
} |
640 |
|
|
|
641 |
|
|
eptr = l->fnametab + size; |
642 |
|
|
for (entry = 0, ptr = l->fnametab; ptr < eptr; ptr++) |
643 |
|
|
switch (*ptr) { |
644 |
|
|
case '/': |
645 |
|
|
entry++; |
646 |
|
|
*ptr = '\0'; |
647 |
|
|
break; |
648 |
|
|
|
649 |
|
|
case '\n': |
650 |
|
|
break; |
651 |
|
|
|
652 |
|
|
default: |
653 |
|
|
break; |
654 |
|
|
} |
655 |
|
|
if (DEBUG(ARCH)) |
656 |
|
|
printf("Found svr4 archive name table with %zu entries\n", |
657 |
|
|
entry); |
658 |
|
|
return (char *)svr4list; |
659 |
|
|
} |
660 |
|
|
/* Then the names themselves are given as offsets in this table. */ |
661 |
|
|
if (*name == ' ' || *name == '\0') |
662 |
|
|
return NULL; |
663 |
|
|
|
664 |
|
|
entry = (size_t) strtol(name, &eptr, 0); |
665 |
|
|
if ((*eptr != ' ' && *eptr != '\0') || eptr == name) { |
666 |
|
|
if (DEBUG(ARCH)) |
667 |
|
|
printf("Could not parse SVR4 name /%s\n", name); |
668 |
|
|
return NULL; |
669 |
|
|
} |
670 |
|
|
if (entry >= l->fnamesize) { |
671 |
|
|
if (DEBUG(ARCH)) |
672 |
|
|
printf("SVR4 entry offset /%s is greater than %zu\n", |
673 |
|
|
name, l->fnamesize); |
674 |
|
|
return NULL; |
675 |
|
|
} |
676 |
|
|
|
677 |
|
|
if (DEBUG(ARCH)) |
678 |
|
|
printf("Replaced /%s with %s\n", name, l->fnametab + entry); |
679 |
|
|
|
680 |
|
|
return l->fnametab + entry; |
681 |
|
|
} |
682 |
|
|
#endif |
683 |
|
|
|
684 |
|
|
|
685 |
|
|
/*- |
686 |
|
|
*----------------------------------------------------------------------- |
687 |
|
|
* ArchFindMember -- |
688 |
|
|
* Locate a member of an archive, given the path of the archive and |
689 |
|
|
* the path of the desired member. If the archive is to be modified, |
690 |
|
|
* the mode should be "r+", if not, it should be "r". |
691 |
|
|
* |
692 |
|
|
* Results: |
693 |
|
|
* A FILE *, opened for reading and writing, positioned right after |
694 |
|
|
* the member's header, or NULL if the member was nonexistent. |
695 |
|
|
* |
696 |
|
|
* Side Effects: |
697 |
|
|
* Fill the struct ar_hdr pointed by arHeaderPtr. |
698 |
|
|
*----------------------------------------------------------------------- |
699 |
|
|
*/ |
700 |
|
|
static FILE * |
701 |
|
|
ArchFindMember( |
702 |
|
|
const char *archive, /* Path to the archive */ |
703 |
|
|
const char *member, /* Name of member. If it is a path, only the |
704 |
|
|
* last component is used. */ |
705 |
|
|
struct ar_hdr *arHeaderPtr,/* Pointer to header structure to be filled in */ |
706 |
|
|
const char *mode) /* mode for opening the stream */ |
707 |
|
|
{ |
708 |
|
|
FILE * arch; /* Stream to archive */ |
709 |
|
|
char *cp; |
710 |
|
|
char magic[SARMAG]; |
711 |
|
|
size_t length; |
712 |
|
|
struct SVR4namelist list; |
713 |
|
|
|
714 |
|
|
list.fnametab = NULL; |
715 |
|
|
|
716 |
|
|
arch = fopen(archive, mode); |
717 |
|
|
if (arch == NULL) |
718 |
|
|
return NULL; |
719 |
|
|
|
720 |
|
|
/* Make sure this is an archive we can handle. */ |
721 |
|
|
if (fread(magic, SARMAG, 1, arch) != 1 || |
722 |
|
|
strncmp(magic, ARMAG, SARMAG) != 0) { |
723 |
|
|
fclose(arch); |
724 |
|
|
return NULL; |
725 |
|
|
} |
726 |
|
|
|
727 |
|
|
/* Because of space constraints and similar things, files are archived |
728 |
|
|
* using their final path components, not the entire thing, so we need |
729 |
|
|
* to point 'member' to the final component, if there is one, to make |
730 |
|
|
* the comparisons easier... */ |
731 |
|
|
cp = strrchr(member, '/'); |
732 |
|
|
if (cp != NULL) |
733 |
|
|
member = cp + 1; |
734 |
|
|
|
735 |
|
|
length = strlen(member); |
736 |
|
|
if (length >= AR_NAME_SIZE) |
737 |
|
|
length = AR_NAME_SIZE; |
738 |
|
|
|
739 |
|
|
/* Error handling is simpler than for read_archive, since we just |
740 |
|
|
* look for a given member. */ |
741 |
|
|
while (fread(arHeaderPtr, sizeof(struct ar_hdr), 1, arch) == 1) { |
742 |
|
|
off_t size; /* Size of archive member */ |
743 |
|
|
char *memberName; |
744 |
|
|
|
745 |
|
|
if (memcmp(arHeaderPtr->ar_fmag, ARFMAG, |
746 |
|
|
sizeof(arHeaderPtr->ar_fmag) ) != 0) |
747 |
|
|
/* The header is bogus, so the archive is bad. */ |
748 |
|
|
break; |
749 |
|
|
|
750 |
|
|
memberName = arHeaderPtr->ar_name; |
751 |
|
|
if (memcmp(member, memberName, length) == 0) { |
752 |
|
|
/* If the member's name doesn't take up the entire |
753 |
|
|
* 'name' field, we have to be careful of matching |
754 |
|
|
* prefixes. Names are space- padded to the right, so |
755 |
|
|
* if the character in 'name' at the end of the matched |
756 |
|
|
* string is anything but a space, this isn't the |
757 |
|
|
* member we sought. */ |
758 |
|
|
#ifdef SVR4ARCHIVES |
759 |
|
|
if (length < sizeof(arHeaderPtr->ar_name) && |
760 |
|
|
memberName[length] == '/') |
761 |
|
|
length++; |
762 |
|
|
#endif |
763 |
|
|
if (length == sizeof(arHeaderPtr->ar_name) || |
764 |
|
|
memberName[length] == ' ') { |
765 |
|
|
free(list.fnametab); |
766 |
|
|
return arch; |
767 |
|
|
} |
768 |
|
|
} |
769 |
|
|
|
770 |
|
|
size = (off_t) field2long(arHeaderPtr->ar_size, |
771 |
|
|
sizeof(arHeaderPtr->ar_size)); |
772 |
|
|
|
773 |
|
|
#ifdef SVR4ARCHIVES |
774 |
|
|
/* svr4 names are slash terminated. Also svr4 extended AR |
775 |
|
|
* format. |
776 |
|
|
*/ |
777 |
|
|
if (memberName[0] == '/') { |
778 |
|
|
/* svr4 magic mode. */ |
779 |
|
|
memberName = ArchSVR4Entry(&list, arHeaderPtr->ar_name, |
780 |
|
|
size, arch); |
781 |
|
|
if (memberName == NULL) |
782 |
|
|
/* Invalid data */ |
783 |
|
|
break; |
784 |
|
|
else if (memberName == svr4list) |
785 |
|
|
/* List of files entry */ |
786 |
|
|
continue; |
787 |
|
|
/* Got the entry. */ |
788 |
|
|
if (strcmp(memberName, member) == 0) { |
789 |
|
|
free(list.fnametab); |
790 |
|
|
return arch; |
791 |
|
|
} |
792 |
|
|
} |
793 |
|
|
#endif |
794 |
|
|
|
795 |
|
|
#ifdef AR_EFMT1 |
796 |
|
|
/* BSD 4.4 extended AR format: #1/<namelen>, with name as the |
797 |
|
|
* first <namelen> bytes of the file. */ |
798 |
|
|
if (memcmp(memberName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 && |
799 |
|
|
ISDIGIT(memberName[sizeof(AR_EFMT1) - 1])) { |
800 |
|
|
char ename[PATH_MAX]; |
801 |
|
|
|
802 |
|
|
int elength = atoi(memberName + sizeof(AR_EFMT1)-1); |
803 |
|
|
|
804 |
|
|
if (elength <= 0 || elength >= PATH_MAX) |
805 |
|
|
break; |
806 |
|
|
if (fread(ename, elength, 1, arch) != 1) |
807 |
|
|
break; |
808 |
|
|
if (fseek(arch, -elength, SEEK_CUR) != 0) |
809 |
|
|
break; |
810 |
|
|
ename[elength] = '\0'; |
811 |
|
|
if (DEBUG(ARCH) || DEBUG(MAKE)) |
812 |
|
|
printf("ArchFind: Extended format entry for %s\n", ename); |
813 |
|
|
/* Found as extended name. */ |
814 |
|
|
if (strcmp(ename, member) == 0) { |
815 |
|
|
free(list.fnametab); |
816 |
|
|
return arch; |
817 |
|
|
} |
818 |
|
|
} |
819 |
|
|
#endif |
820 |
|
|
/* This isn't the member we're after, so we need to advance the |
821 |
|
|
* stream's pointer to the start of the next header. */ |
822 |
|
|
if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) |
823 |
|
|
break; |
824 |
|
|
} |
825 |
|
|
|
826 |
|
|
/* We did not find the member, or we ran into an error while reading |
827 |
|
|
* the archive. */ |
828 |
|
|
#ifdef SVRARCHIVES |
829 |
|
|
free(list.fnametab); |
830 |
|
|
#endif |
831 |
|
|
fclose(arch); |
832 |
|
|
return NULL; |
833 |
|
|
} |
834 |
|
|
|
835 |
|
|
static void |
836 |
|
|
ArchTouch(const char *archive, const char *member) |
837 |
|
|
{ |
838 |
|
|
FILE *arch; |
839 |
|
|
struct ar_hdr arHeader; |
840 |
|
|
|
841 |
|
|
arch = ArchFindMember(archive, member, &arHeader, "r+"); |
842 |
|
|
if (arch != NULL) { |
843 |
|
|
snprintf(arHeader.ar_date, sizeof(arHeader.ar_date), |
844 |
|
|
"%-12ld", (long) time(NULL)); |
845 |
|
|
if (fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR) == 0) |
846 |
|
|
(void)fwrite(&arHeader, sizeof(struct ar_hdr), 1, arch); |
847 |
|
|
fclose(arch); |
848 |
|
|
} |
849 |
|
|
} |
850 |
|
|
|
851 |
|
|
/* |
852 |
|
|
* Side Effects: |
853 |
|
|
* The modification time of the entire archive is also changed. |
854 |
|
|
* For a library, this could necessitate the re-ranlib'ing of the |
855 |
|
|
* whole thing. |
856 |
|
|
*/ |
857 |
|
|
void |
858 |
|
|
Arch_Touch(GNode *gn) |
859 |
|
|
{ |
860 |
|
|
ArchTouch(Var(ARCHIVE_INDEX, gn), Var(MEMBER_INDEX, gn)); |
861 |
|
|
} |
862 |
|
|
|
863 |
|
|
struct timespec |
864 |
|
|
Arch_MTime(GNode *gn) |
865 |
|
|
{ |
866 |
|
|
gn->mtime = ArchMTimeMember(Var(ARCHIVE_INDEX, gn), |
867 |
|
|
Var(MEMBER_INDEX, gn), true); |
868 |
|
|
|
869 |
|
|
return gn->mtime; |
870 |
|
|
} |
871 |
|
|
|
872 |
|
|
struct timespec |
873 |
|
|
Arch_MemMTime(GNode *gn) |
874 |
|
|
{ |
875 |
|
|
LstNode ln; |
876 |
|
|
|
877 |
|
|
for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln)) { |
878 |
|
|
GNode *pgn; |
879 |
|
|
char *nameStart; |
880 |
|
|
char *nameEnd; |
881 |
|
|
|
882 |
|
|
pgn = Lst_Datum(ln); |
883 |
|
|
|
884 |
|
|
if (pgn->type & OP_ARCHV) { |
885 |
|
|
/* If the parent is an archive specification and is |
886 |
|
|
* being made and its member's name matches the name of |
887 |
|
|
* the node we were given, record the modification time |
888 |
|
|
* of the parent in the child. We keep searching its |
889 |
|
|
* parents in case some other parent requires this |
890 |
|
|
* child to exist... */ |
891 |
|
|
if ((nameStart = strchr(pgn->name, '(') ) != NULL) { |
892 |
|
|
nameStart++; |
893 |
|
|
nameEnd = strchr(nameStart, ')'); |
894 |
|
|
} else |
895 |
|
|
nameEnd = NULL; |
896 |
|
|
|
897 |
|
|
if (pgn->must_make && nameEnd != NULL && |
898 |
|
|
strncmp(nameStart, gn->name, nameEnd - nameStart) |
899 |
|
|
== 0 && gn->name[nameEnd-nameStart] == '\0') |
900 |
|
|
gn->mtime = Arch_MTime(pgn); |
901 |
|
|
} else if (pgn->must_make) { |
902 |
|
|
/* Something which isn't a library depends on the |
903 |
|
|
* existence of this target, so it needs to exist. */ |
904 |
|
|
ts_set_out_of_date(gn->mtime); |
905 |
|
|
break; |
906 |
|
|
} |
907 |
|
|
} |
908 |
|
|
return gn->mtime; |
909 |
|
|
} |
910 |
|
|
|
911 |
|
|
void |
912 |
|
|
Arch_Init(void) |
913 |
|
|
{ |
914 |
|
68248 |
ohash_init(&archives, 4, &arch_info); |
915 |
|
34124 |
} |