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