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