[BACK]Return to arch.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/arch.c, Revision 1.90

1.90    ! espie       1: /*     $OpenBSD: arch.c,v 1.89 2017/07/24 12:07:46 espie Exp $ */
1.9       millert     2: /*     $NetBSD: arch.c,v 1.17 1996/11/06 17:58:59 christos Exp $       */
1.1       deraadt     3:
                      4: /*
1.44      espie       5:  * Copyright (c) 1999,2000 Marc Espie.
1.38      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: /*
1.9       millert    31:  * Copyright (c) 1988, 1989, 1990, 1993
                     32:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt    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.
1.53      millert    47:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    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:
1.45      espie      64: /*
1.1       deraadt    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
1.69      espie      68:  * archives are kept in a hash (archives) which is searched each time
1.45      espie      69:  * an archive member is referenced.
1.1       deraadt    70:  *
                     71:  */
                     72:
1.45      espie      73: #include <ar.h>
                     74: #include <assert.h>
                     75: #include <ctype.h>
                     76: #include <fcntl.h>
                     77: #include <limits.h>
                     78: #include <stddef.h>
1.81      espie      79: #include <stdint.h>
1.45      espie      80: #include <stdio.h>
                     81: #include <stdlib.h>
                     82: #include <string.h>
                     83: #include <unistd.h>
1.81      espie      84: #include <ohash.h>
1.45      espie      85: #include "config.h"
                     86: #include "defines.h"
1.70      espie      87: #include "buf.h"
1.45      espie      88: #include "dir.h"
1.63      espie      89: #include "direxpand.h"
1.45      espie      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"
1.48      espie      97:
1.14      espie      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
1.77      miod      105: #endif
                    106: #ifdef TARGET_MACHINE_CPU
                    107: #undef MACHINE_CPU
                    108: #define MACHINE_CPU TARGET_MACHINE_CPU
1.14      espie     109: #endif
                    110:
1.70      espie     111: static struct ohash archives;  /* Archives we've already examined.  */
1.1       deraadt   112:
1.35      espie     113: typedef struct Arch_ {
1.64      espie     114:        struct ohash members;   /* All the members of this archive, as
1.70      espie     115:                                 * struct arch_member entries.  */
1.64      espie     116:        char name[1];           /* Archive name. */
1.1       deraadt   117: } Arch;
                    118:
1.35      espie     119: /* Used to get to ar's field sizes.  */
                    120: static struct ar_hdr *dummy;
1.44      espie     121: #define AR_NAME_SIZE           (sizeof(dummy->ar_name))
1.35      espie     122: #define AR_DATE_SIZE           (sizeof(dummy->ar_date))
                    123:
1.44      espie     124: /* Each archive member is tied to an arch_member structure,
1.35      espie     125:  * suitable for hashing.  */
                    126: struct arch_member {
1.81      espie     127:        struct timespec mtime;          /* Member modification date.  */
1.70      espie     128:        char date[AR_DATE_SIZE+1];      /* Same, before conversion to numeric
                    129:                                         * value.  */
                    130:        char name[1];                   /* Member name.  */
1.35      espie     131: };
                    132:
1.43      espie     133: static struct ohash_info members_info = {
1.64      espie     134:        offsetof(struct arch_member, name), NULL,
1.83      espie     135:        hash_calloc, hash_free, element_alloc
1.35      espie     136: };
                    137:
1.43      espie     138: static struct ohash_info arch_info = {
1.83      espie     139:        offsetof(Arch, name), NULL, hash_calloc, hash_free, element_alloc
1.38      espie     140: };
                    141:
1.44      espie     142:
                    143:
                    144: static struct arch_member *new_arch_member(struct ar_hdr *, const char *);
1.81      espie     145: static struct timespec mtime_of_member(struct arch_member *);
1.44      espie     146: static long field2long(const char *, size_t);
                    147: static Arch *read_archive(const char *, const char *);
1.35      espie     148:
1.81      espie     149: static struct timespec ArchMTimeMember(const char *, const char *, bool);
1.44      espie     150: static FILE *ArchFindMember(const char *, const char *, struct ar_hdr *, const char *);
                    151: static void ArchTouch(const char *, const char *);
1.7       niklas    152: #if defined(__svr4__) || defined(__SVR4) || \
1.51      matthieu  153:     (defined(__OpenBSD__) && defined(__ELF__))
1.6       briggs    154: #define SVR4ARCHIVES
1.40      espie     155: #endif
1.70      espie     156: static bool parse_archive(Buffer, const char **, Lst, SymTable *);
                    157: static void add_archive_node(Lst, const char *);
1.40      espie     158:
                    159: struct SVR4namelist {
1.70      espie     160:        char *fnametab;         /* Extended name table strings */
                    161:        size_t fnamesize;       /* Size of the string table */
1.40      espie     162: };
                    163:
1.73      deraadt   164: #ifdef SVR4ARCHIVES
1.40      espie     165: static const char *svr4list = "Archive list";
                    166:
1.54      espie     167: static char *ArchSVR4Entry(struct SVR4namelist *, const char *, size_t, FILE *);
1.73      deraadt   168: #endif
1.1       deraadt   169:
1.35      espie     170: static struct arch_member *
1.54      espie     171: new_arch_member(struct ar_hdr *hdr, const char *name)
1.35      espie     172: {
1.64      espie     173:        const char *end = NULL;
                    174:        struct arch_member *n;
1.35      espie     175:
1.64      espie     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;
1.35      espie     183: }
                    184:
1.81      espie     185: static struct timespec
1.54      espie     186: mtime_of_member(struct arch_member *m)
1.35      espie     187: {
1.64      espie     188:        if (is_out_of_date(m->mtime))
1.81      espie     189:                ts_set_from_time_t((time_t) strtoll(m->date, NULL, 10),
1.64      espie     190:                    m->mtime);
                    191:        return m->mtime;
1.35      espie     192: }
                    193:
1.70      espie     194: bool
                    195: Arch_ParseArchive(const char **line, Lst nodes, SymTable *ctxt)
                    196: {
                    197:        bool result;
                    198:        BUFFER expand;
1.1       deraadt   199:
1.70      espie     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;
1.1       deraadt   210:
1.70      espie     211:        gn = Targ_FindNode(name, TARG_CREATE);
                    212:        gn->type |= OP_ARCHV;
                    213:        Lst_AtEnd(nodes, gn);
                    214: }
1.64      espie     215:
1.70      espie     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;
1.64      espie     225:
1.70      espie     226:        /* figure out the library name part */
                    227:        lib = *linePtr;
                    228:        subst_lib = false;
1.64      espie     229:
1.70      espie     230:        for (cp = lib; *cp != '(' && *cp != '\0';) {
1.64      espie     231:                if (*cp == '$') {
                    232:                        if (!Var_ParseSkip(&cp, ctxt))
                    233:                                return false;
1.70      espie     234:                        subst_lib = true;
1.64      espie     235:                } else
                    236:                        cp++;
                    237:        }
                    238:
1.70      espie     239:        elib = cp;
                    240:        if (subst_lib) {
                    241:                lib = Var_Substi(lib, elib, ctxt, true);
                    242:                elib = lib + strlen(lib);
                    243:        }
1.64      espie     244:
1.88      espie     245:        if (*cp == '\0') {
                    246:                printf("Unclosed parenthesis in archive specification\n");
                    247:                return false;
                    248:        }
1.70      espie     249:        cp++;
                    250:        /* iterate on members, that may be separated by spaces */
1.64      espie     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).  */
1.70      espie     255:                bool subst_member = false;
1.64      espie     256:
1.82      espie     257:                while (ISSPACE(*cp))
1.64      espie     258:                        cp++;
1.70      espie     259:                member = cp;
1.82      espie     260:                while (*cp != '\0' && *cp != ')' && !ISSPACE(*cp)) {
1.64      espie     261:                        if (*cp == '$') {
                    262:                                if (!Var_ParseSkip(&cp, ctxt))
                    263:                                        return false;
1.70      espie     264:                                subst_member = true;
1.64      espie     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.  */
1.88      espie     273:                if (*cp == '\0' || ISSPACE(*cp)) {
1.64      espie     274:                        printf("No closing parenthesis in archive specification\n");
                    275:                        return false;
                    276:                }
1.1       deraadt   277:
1.64      espie     278:                /* If we didn't move anywhere, we must be done.  */
1.70      espie     279:                if (cp == member)
1.64      espie     280:                        break;
1.1       deraadt   281:
1.70      espie     282:                emember = cp;
1.1       deraadt   283:
1.64      espie     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.  */
1.70      espie     294:                if (subst_member) {
                    295:                        const char *oldMemberName = member;
                    296:                        const char *result;
1.64      espie     297:
1.70      espie     298:                        member = Var_Substi(member, emember, ctxt, true);
1.64      espie     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.  */
1.70      espie     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) {
1.64      espie     313:                                /* Must contain dynamic sources, so we can't
1.70      espie     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))
1.64      espie     318:                                return false;
1.70      espie     319:                        Buf_Reset(expand);
                    320:                } else if (Dir_HasWildcardsi(member, emember)) {
                    321:                        LIST  members;
                    322:                        char  *m;
1.64      espie     323:
                    324:                        Lst_Init(&members);
                    325:
1.70      espie     326:                        Dir_Expandi(member, emember, defaultPath, &members);
1.87      espie     327:                        while ((m = Lst_DeQueue(&members)) != NULL) {
1.70      espie     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);
1.64      espie     335:                        }
1.1       deraadt   336:                } else {
1.70      espie     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);
1.64      espie     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++;
1.82      espie     357:        } while (ISSPACE(*cp));
1.1       deraadt   358:
1.64      espie     359:        *linePtr = cp;
                    360:        return true;
1.1       deraadt   361: }
                    362:
1.44      espie     363: /* Helper function: ar fields are not null terminated. */
1.41      espie     364: static long
1.54      espie     365: field2long(const char *field, size_t length)
1.41      espie     366: {
1.64      espie     367:        static char enough[32];
1.41      espie     368:
1.64      espie     369:        assert(length < sizeof(enough));
                    370:        memcpy(enough, field, length);
                    371:        enough[length] = '\0';
                    372:        return strtol(enough, NULL, 10);
1.41      espie     373: }
                    374:
1.38      espie     375: static Arch *
1.54      espie     376: read_archive(const char *archive, const char *earchive)
1.1       deraadt   377: {
1.70      espie     378:        FILE *arch;       /* Stream to archive */
1.64      espie     379:        char magic[SARMAG];
                    380:        Arch *ar;
                    381:        struct SVR4namelist list;
1.40      espie     382:
1.64      espie     383:        list.fnametab = NULL;
1.35      espie     384:
1.64      espie     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;
1.9       millert   390:
1.64      espie     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:        }
1.1       deraadt   397:
1.64      espie     398:        ar = ohash_create_entry(&arch_info, archive, &earchive);
                    399:        ohash_init(&ar->members, 8, &members_info);
1.38      espie     400:
1.64      espie     401:        for (;;) {
                    402:                size_t n;
1.70      espie     403:                struct ar_hdr arHeader; /* Archive-member header */
                    404:                off_t size;             /* Size of archive member */
1.64      espie     405:                char buffer[PATH_MAX];
1.70      espie     406:                char *memberName;       /* Current member name while hashing. */
1.64      espie     407:                char *cp;
1.41      espie     408:
1.64      espie     409:                memberName = buffer;
                    410:                n = fread(&arHeader, 1, sizeof(struct ar_hdr), arch);
1.41      espie     411:
1.64      espie     412:                /*  Whole archive read ok.  */
                    413:                if (n == 0 && feof(arch)) {
1.89      espie     414:                        free(list.fnametab);
1.64      espie     415:                        fclose(arch);
                    416:                        return ar;
                    417:                }
                    418:                if (n < sizeof(struct ar_hdr))
                    419:                        break;
1.40      espie     420:
1.69      espie     421:                if (memcmp(arHeader.ar_fmag, ARFMAG, sizeof(arHeader.ar_fmag))
1.64      espie     422:                    != 0) {
1.70      espie     423:                        /* header is bogus.  */
1.64      espie     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.  */
1.69      espie     431:                        size = (off_t) field2long(arHeader.ar_size,
1.64      espie     432:                            sizeof(arHeader.ar_size));
                    433:
1.69      espie     434:                        (void)memcpy(memberName, arHeader.ar_name,
1.64      espie     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';
1.1       deraadt   440:
1.6       briggs    441: #ifdef SVR4ARCHIVES
1.64      espie     442:                        /* SVR4 names are slash terminated.  Also svr4 extended
                    443:                         * AR format.
                    444:                         */
                    445:                        if (memberName[0] == '/') {
                    446:                                /* SVR4 magic mode.  */
1.69      espie     447:                                memberName = ArchSVR4Entry(&list, memberName,
1.64      espie     448:                                    size, arch);
1.70      espie     449:                                if (memberName == NULL)
                    450:                                        /* Invalid data */
1.64      espie     451:                                        break;
                    452:                                else if (memberName == svr4list)
1.70      espie     453:                                        /* List of files entry */
1.64      espie     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:                        }
1.3       niklas    463: #endif
                    464:
1.1       deraadt   465: #ifdef AR_EFMT1
1.64      espie     466:                        /* BSD 4.4 extended AR format: #1/<namelen>, with name
                    467:                         * as the first <namelen> bytes of the file.  */
1.69      espie     468:                        if (memcmp(memberName, AR_EFMT1, sizeof(AR_EFMT1) - 1)
1.82      espie     469:                            == 0 && ISDIGIT(memberName[sizeof(AR_EFMT1) - 1])) {
1.64      espie     470:
1.69      espie     471:                                int elen = atoi(memberName +
1.64      espie     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))
1.69      espie     483:                                        printf("ArchStat: Extended format entry for %s\n",
1.64      espie     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)
1.41      espie     493:                        break;
1.1       deraadt   494:        }
                    495:
1.64      espie     496:        fclose(arch);
                    497:        ohash_delete(&ar->members);
1.89      espie     498:        free(list.fnametab);
1.64      espie     499:        free(ar);
                    500:        return NULL;
1.38      espie     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:
1.44      espie     510:  *     The archive member's modification time, or OUT_OF_DATE if member
                    511:  *     was not found (convenient, so that missing members are always
1.38      espie     512:  *     out of date).
                    513:  *
                    514:  * Side Effects:
1.45      espie     515:  *     Cache the whole archive contents if hash is true.
1.38      espie     516:  *-----------------------------------------------------------------------
                    517:  */
1.81      espie     518: static struct timespec
1.54      espie     519: ArchMTimeMember(
1.70      espie     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. */
1.38      espie     525: {
1.70      espie     526:        FILE *arch;             /* Stream to archive */
                    527:        Arch *ar;               /* Archive descriptor */
1.64      espie     528:        unsigned int slot;      /* Place of archive in the archives hash */
                    529:        const char *end = NULL;
                    530:        const char *cp;
1.81      espie     531:        struct timespec result;
1.64      espie     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) {
1.67      espie     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);
1.69      espie     558:                                ts_set_from_time_t(
                    559:                                    (time_t)strtol(arHeader.ar_date, NULL, 10),
1.67      espie     560:                                    result);
                    561:                        }
                    562:                        return result;
1.64      espie     563:                }
                    564:                ar = read_archive(archive, end);
                    565:                if (ar != NULL)
                    566:                        ohash_insert(&archives, slot, ar);
                    567:        }
1.38      espie     568:
1.64      espie     569:        /* If archive was found, get entry we seek.  */
                    570:        if (ar != NULL) {
                    571:                struct arch_member *he;
                    572:                end = NULL;
1.38      espie     573:
1.70      espie     574:                he = ohash_find(&ar->members, ohash_qlookupi(&ar->members,
                    575:                    member, &end));
1.38      espie     576:                if (he != NULL)
1.64      espie     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:                }
1.38      espie     588:        }
1.64      espie     589:        return result;
1.1       deraadt   590: }
1.6       briggs    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:
1.40      espie     602:  *     svr4list: just read a list of names
1.50      mpech     603:  *     NULL:     error occurred
1.40      espie     604:  *     extended name
1.6       briggs    605:  *
1.40      espie     606:  * Side-effect:
                    607:  *     For a list of names, store the list in l.
1.6       briggs    608:  *-----------------------------------------------------------------------
                    609:  */
1.44      espie     610:
1.40      espie     611: static char *
1.54      espie     612: ArchSVR4Entry(struct SVR4namelist *l, const char *name, size_t size, FILE *arch)
1.6       briggs    613: {
1.40      espie     614: #define ARLONGNAMES1 "/"
                    615: #define ARLONGNAMES2 "ARFILENAMES"
1.64      espie     616:        size_t entry;
                    617:        char *ptr, *eptr;
1.6       briggs    618:
1.64      espie     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:                }
1.6       briggs    631:
1.64      espie     632:                l->fnametab = emalloc(size);
                    633:                l->fnamesize = size;
1.6       briggs    634:
1.64      espie     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))
1.86      mmcc      656:                        printf("Found svr4 archive name table with %zu entries\n",
                    657:                            entry);
1.64      espie     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))
1.86      mmcc      672:                        printf("SVR4 entry offset /%s is greater than %zu\n",
                    673:                            name, l->fnamesize);
1.64      espie     674:                return NULL;
1.6       briggs    675:        }
1.44      espie     676:
1.40      espie     677:        if (DEBUG(ARCH))
1.64      espie     678:                printf("Replaced /%s with %s\n", name, l->fnametab + entry);
1.6       briggs    679:
1.64      espie     680:        return l->fnametab + entry;
1.6       briggs    681: }
                    682: #endif
                    683:
1.1       deraadt   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:
1.42      espie     693:  *     A FILE *, opened for reading and writing, positioned right after
1.44      espie     694:  *     the member's header, or NULL if the member was nonexistent.
1.1       deraadt   695:  *
                    696:  * Side Effects:
1.54      espie     697:  *     Fill the struct ar_hdr pointed by arHeaderPtr.
1.1       deraadt   698:  *-----------------------------------------------------------------------
                    699:  */
                    700: static FILE *
1.54      espie     701: ArchFindMember(
                    702:     const char   *archive,   /* Path to the archive */
                    703:     const char   *member,    /* Name of member. If it is a path, only the
1.1       deraadt   704:                               * last component is used. */
1.54      espie     705:     struct ar_hdr *arHeaderPtr,/* Pointer to header structure to be filled in */
                    706:     const char   *mode)      /* mode for opening the stream */
1.1       deraadt   707: {
1.70      espie     708:        FILE *    arch;       /* Stream to archive */
                    709:        char      *cp;
                    710:        char      magic[SARMAG];
                    711:        size_t    length;
1.64      espie     712:        struct SVR4namelist list;
1.42      espie     713:
1.64      espie     714:        list.fnametab = NULL;
1.42      espie     715:
1.64      espie     716:        arch = fopen(archive, mode);
                    717:        if (arch == NULL)
                    718:                return NULL;
1.9       millert   719:
1.64      espie     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:        }
1.1       deraadt   726:
1.64      espie     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) {
1.70      espie     742:                off_t size;       /* Size of archive member */
1.64      espie     743:                char *memberName;
                    744:
1.69      espie     745:                if (memcmp(arHeaderPtr->ar_fmag, ARFMAG,
1.64      espie     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.  */
1.42      espie     758: #ifdef SVR4ARCHIVES
1.69      espie     759:                        if (length < sizeof(arHeaderPtr->ar_name) &&
1.64      espie     760:                            memberName[length] == '/')
                    761:                                length++;
1.42      espie     762: #endif
1.64      espie     763:                        if (length == sizeof(arHeaderPtr->ar_name) ||
                    764:                            memberName[length] == ' ') {
1.89      espie     765:                                free(list.fnametab);
1.64      espie     766:                                return arch;
                    767:                        }
                    768:                }
1.42      espie     769:
1.69      espie     770:                size = (off_t) field2long(arHeaderPtr->ar_size,
1.64      espie     771:                    sizeof(arHeaderPtr->ar_size));
1.9       millert   772:
1.42      espie     773: #ifdef SVR4ARCHIVES
1.67      espie     774:                /* svr4 names are slash terminated. Also svr4 extended AR
                    775:                 * format.
                    776:                 */
                    777:                if (memberName[0] == '/') {
                    778:                        /* svr4 magic mode.  */
1.70      espie     779:                        memberName = ArchSVR4Entry(&list, arHeaderPtr->ar_name,
                    780:                            size, arch);
                    781:                        if (memberName == NULL)
                    782:                                /* Invalid data */
1.67      espie     783:                                break;
                    784:                        else if (memberName == svr4list)
1.70      espie     785:                                /* List of files entry */
1.67      espie     786:                                continue;
                    787:                        /* Got the entry.  */
                    788:                        if (strcmp(memberName, member) == 0) {
1.89      espie     789:                                free(list.fnametab);
1.67      espie     790:                                return arch;
                    791:                        }
                    792:                }
1.42      espie     793: #endif
                    794:
1.1       deraadt   795: #ifdef AR_EFMT1
1.64      espie     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 &&
1.82      espie     799:                    ISDIGIT(memberName[sizeof(AR_EFMT1) - 1])) {
1.64      espie     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) {
1.89      espie     815:                                free(list.fnametab);
1.64      espie     816:                                return arch;
                    817:                        }
1.1       deraadt   818:                }
1.64      espie     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;
1.42      espie     824:        }
1.1       deraadt   825:
1.64      espie     826:        /* We did not find the member, or we ran into an error while reading
                    827:         * the archive.  */
1.42      espie     828: #ifdef SVRARCHIVES
1.89      espie     829:        free(list.fnametab);
1.42      espie     830: #endif
1.64      espie     831:        fclose(arch);
                    832:        return NULL;
1.1       deraadt   833: }
                    834:
1.39      espie     835: static void
1.54      espie     836: ArchTouch(const char *archive, const char *member)
1.39      espie     837: {
1.67      espie     838:        FILE *arch;
                    839:        struct ar_hdr arHeader;
1.39      espie     840:
1.64      espie     841:        arch = ArchFindMember(archive, member, &arHeader, "r+");
                    842:        if (arch != NULL) {
1.70      espie     843:                snprintf(arHeader.ar_date, sizeof(arHeader.ar_date),
1.76      espie     844:                    "%-12ld", (long) time(NULL));
1.64      espie     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:        }
1.39      espie     849: }
                    850:
1.45      espie     851: /*
1.39      espie     852:  * Side Effects:
1.1       deraadt   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
1.54      espie     858: Arch_Touch(GNode *gn)
1.1       deraadt   859: {
1.75      espie     860:        ArchTouch(Var(ARCHIVE_INDEX, gn), Var(MEMBER_INDEX, gn));
1.1       deraadt   861: }
                    862:
1.81      espie     863: struct timespec
1.54      espie     864: Arch_MTime(GNode *gn)
1.1       deraadt   865: {
1.75      espie     866:        gn->mtime = ArchMTimeMember(Var(ARCHIVE_INDEX, gn),
                    867:             Var(MEMBER_INDEX, gn), true);
1.1       deraadt   868:
1.64      espie     869:        return gn->mtime;
1.1       deraadt   870: }
                    871:
1.81      espie     872: struct timespec
1.54      espie     873: Arch_MemMTime(GNode *gn)
1.1       deraadt   874: {
1.64      espie     875:        LstNode ln;
1.1       deraadt   876:
1.64      espie     877:        for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln)) {
                    878:                GNode *pgn;
                    879:                char *nameStart;
                    880:                char *nameEnd;
                    881:
1.87      espie     882:                pgn = Lst_Datum(ln);
1.64      espie     883:
                    884:                if (pgn->type & OP_ARCHV) {
                    885:                        /* If the parent is an archive specification and is
1.90    ! espie     886:                         * being built and its member's name matches the name of
1.64      espie     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:
1.74      espie     897:                        if (pgn->must_make && nameEnd != NULL &&
1.69      espie     898:                            strncmp(nameStart, gn->name, nameEnd - nameStart)
1.64      espie     899:                            == 0 && gn->name[nameEnd-nameStart] == '\0')
                    900:                                gn->mtime = Arch_MTime(pgn);
1.74      espie     901:                } else if (pgn->must_make) {
1.64      espie     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:                }
1.1       deraadt   907:        }
1.64      espie     908:        return gn->mtime;
1.1       deraadt   909: }
                    910:
                    911: void
1.54      espie     912: Arch_Init(void)
1.1       deraadt   913: {
1.64      espie     914:        ohash_init(&archives, 4, &arch_info);
1.1       deraadt   915: }