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