# build/parsePrep.c

Go to the documentation of this file.

    00001
    00006 #include "system.h"
    00007
    00008 #include <rpmio_internal.h>
    00009 #include <rpmbuild.h>
    00010 #include "debug.h"
    00011
    00012 /*@access StringBuf @*/ /* compared with NULL */
    00013
    00014 /* These have to be global to make up for stupid compilers */
    00015 /*@unchecked@*/
    00016     static int leaveDirs, skipDefaultAction;
    00017 /*@unchecked@*/
    00018     static int createDir, quietly;
    00019 /*@unchecked@*/
    00020 /*@observer@*/ /*@null@*/ static const char * dirName = NULL;
    00021 /*@unchecked@*/
    00022 /*@observer@*/ static struct poptOption optionsTable[] = {
    00023             { NULL, 'a', POPT_ARG_STRING, NULL, 'a',    NULL, NULL},
    00024             { NULL, 'b', POPT_ARG_STRING, NULL, 'b',    NULL, NULL},
    00025             { NULL, 'c', 0, &createDir, 0,              NULL, NULL},
    00026             { NULL, 'D', 0, &leaveDirs, 0,              NULL, NULL},
    00027             { NULL, 'n', POPT_ARG_STRING, &dirName, 0,  NULL, NULL},
    00028             { NULL, 'T', 0, &skipDefaultAction, 0,      NULL, NULL},
    00029             { NULL, 'q', 0, &quietly, 0,                NULL, NULL},
    00030             { 0, 0, 0, 0, 0,    NULL, NULL}
    00031     };
    00032
    00038 static int checkOwners(const char * urlfn)
    00039         /*@globals fileSystem @*/
    00040         /*@modifies fileSystem @*/
    00041 {
    00042     struct stat sb;
    00043
    00044     if (Lstat(urlfn, &sb)) {
    00045         rpmError(RPMERR_BADSPEC, _("Bad source: %s: %s\n"),
    00046                 urlfn, strerror(errno));
    00047         return RPMERR_BADSPEC;
    00048     }
    00049     if (!getUname(sb.st_uid) || !getGname(sb.st_gid)) {
    00050         rpmError(RPMERR_BADSPEC, _("Bad owner/group: %s\n"), urlfn);
    00051         return RPMERR_BADSPEC;
    00052     }
    00053
    00054     return 0;
    00055 }
    00056
    00067 /*@observer@*/ static char *doPatch(Spec spec, int c, int strip, const char *db,
    00068                      int reverse, int removeEmpties)
    00069         /*@globals rpmGlobalMacroContext,
    00070                 fileSystem@*/
    00071         /*@modifies rpmGlobalMacroContext, fileSystem @*/
    00072 {
    00073     const char *fn, *urlfn;
    00074     static char buf[BUFSIZ];
    00075     char args[BUFSIZ];
    00076     struct Source *sp;
    00077     rpmCompressedMagic compressed = COMPRESSED_NOT;
    00078     int urltype;
    00079
    00080     for (sp = spec->sources; sp != NULL; sp = sp->next) {
    00081         if ((sp->flags & RPMBUILD_ISPATCH) && (sp->num == c)) {
    00082             break;
    00083         }
    00084     }
    00085     if (sp == NULL) {
    00086         rpmError(RPMERR_BADSPEC, _("No patch number %d\n"), c);
    00087         return NULL;
    00088     }
    00089
    00090     urlfn = rpmGetPath("%{_sourcedir}/", sp->source, NULL);
    00091
    00092     args[0] = '\0';
    00093     if (db) {
    00094 #if HAVE_OLDPATCH_21 == 0
    00095         strcat(args, "-b ");
    00096 #endif
    00097         strcat(args, "--suffix ");
    00098         strcat(args, db);
    00099     }
    00100     if (reverse) {
    00101         strcat(args, " -R");
    00102     }
    00103     if (removeEmpties) {
    00104         strcat(args, " -E");
    00105     }
    00106
    00107     /* XXX On non-build parse's, file cannot be stat'd or read */
    00108     if (!spec->force && (isCompressed(urlfn, &compressed) || checkOwners(urlfn))) {
    00109         urlfn = _free(urlfn);
    00110         return NULL;
    00111     }
    00112
    00113     fn = NULL;
    00114     urltype = urlPath(urlfn, &fn);
    00115     switch (urltype) {
    00116     case URL_IS_HTTP:   /* XXX WRONG WRONG WRONG */
    00117     case URL_IS_FTP:    /* XXX WRONG WRONG WRONG */
    00118     case URL_IS_PATH:
    00119     case URL_IS_UNKNOWN:
    00120         break;
    00121     case URL_IS_DASH:
    00122         urlfn = _free(urlfn);
    00123         return NULL;
    00124         /*@notreached@*/ break;
    00125     }
    00126
    00127     if (compressed) {
    00128         const char *zipper = rpmGetPath(
    00129             (compressed == COMPRESSED_BZIP2 ? "%{_bzip2bin}" : "%{_gzipbin}"),
    00130             NULL);
    00131
    00132         sprintf(buf,
    00133                 "echo \"Patch #%d (%s):\"\n"
    00134                 "%s -d < %s | patch -p%d %s -s\n"
    00135                 "STATUS=$?\n"
    00136                 "if [ $STATUS -ne 0 ]; then\n"
    00137                 "  exit $STATUS\n"
    00138                 "fi",
    00139                 c, /*@-unrecog@*/ (const char *) basename(fn), /*@=unrecog@*/
    00140                 zipper,
    00141                 fn, strip, args);
    00142         zipper = _free(zipper);
    00143     } else {
    00144         sprintf(buf,
    00145                 "echo \"Patch #%d (%s):\"\n"
    00146                 "patch -p%d %s -s < %s", c, (const char *) basename(fn),
    00147                 strip, args, fn);
    00148     }
    00149
    00150     urlfn = _free(urlfn);
    00151     return buf;
    00152 }
    00153
    00161 /*@observer@*/ static const char *doUntar(Spec spec, int c, int quietly)
    00162         /*@globals rpmGlobalMacroContext,
    00163                 fileSystem@*/
    00164         /*@modifies rpmGlobalMacroContext, fileSystem @*/
    00165 {
    00166     const char *fn, *urlfn;
    00167     static char buf[BUFSIZ];
    00168     char *taropts;
    00169     char *t = NULL;
    00170     struct Source *sp;
    00171     rpmCompressedMagic compressed = COMPRESSED_NOT;
    00172     int urltype;
    00173
    00174     for (sp = spec->sources; sp != NULL; sp = sp->next) {
    00175         if ((sp->flags & RPMBUILD_ISSOURCE) && (sp->num == c)) {
    00176             break;
    00177         }
    00178     }
    00179     if (sp == NULL) {
    00180         rpmError(RPMERR_BADSPEC, _("No source number %d\n"), c);
    00181         return NULL;
    00182     }
    00183
    00184     urlfn = rpmGetPath("%{_sourcedir}/", sp->source, NULL);
    00185
    00186     /*@-internalglobs@*/ /* FIX: shrug */
    00187     taropts = ((rpmIsVerbose() && !quietly) ? "-xvvf" : "-xf");
    00188     /*@=internalglobs@*/
    00189
    00190 #ifdef AUTOFETCH_NOT    /* XXX don't expect this code to be enabled */
    00191     /* XXX
    00192      * XXX If nosource file doesn't exist, try to fetch from url.
    00193      * XXX TODO: add a "--fetch" enabler.
    00194      */
    00195     if (sp->flags & RPMTAG_NOSOURCE && autofetchnosource) {
    00196         struct stat st;
    00197         int rc;
    00198         if (Lstat(urlfn, &st) != 0 && errno == ENOENT &&
    00199             urlIsUrl(sp->fullSource) != URL_IS_UNKNOWN) {
    00200             if ((rc = urlGetFile(sp->fullSource, urlfn)) != 0) {
    00201                 rpmError(RPMERR_BADFILENAME,
    00202                         _("Couldn't download nosource %s: %s\n"),
    00203                         sp->fullSource, ftpStrerror(rc));
    00204                 return NULL;
    00205             }
    00206         }
    00207     }
    00208 #endif
    00209
    00210     /* XXX On non-build parse's, file cannot be stat'd or read */
    00211     if (!spec->force && (isCompressed(urlfn, &compressed) || checkOwners(urlfn))) {
    00212         urlfn = _free(urlfn);
    00213         return NULL;
    00214     }
    00215
    00216     fn = NULL;
    00217     urltype = urlPath(urlfn, &fn);
    00218     switch (urltype) {
    00219     case URL_IS_HTTP:   /* XXX WRONG WRONG WRONG */
    00220     case URL_IS_FTP:    /* XXX WRONG WRONG WRONG */
    00221     case URL_IS_PATH:
    00222     case URL_IS_UNKNOWN:
    00223         break;
    00224     case URL_IS_DASH:
    00225         urlfn = _free(urlfn);
    00226         return NULL;
    00227         /*@notreached@*/ break;
    00228     }
    00229
    00230     if (compressed != COMPRESSED_NOT) {
    00231         const char *zipper;
    00232         int needtar = 1;
    00233
    00234         switch (compressed) {
    00235         case COMPRESSED_NOT:    /* XXX can't happen */
    00236         case COMPRESSED_OTHER:
    00237             t = "%{_gzipbin} -dc";
    00238             break;
    00239         case COMPRESSED_BZIP2:
    00240             t = "%{_bzip2bin} -dc";
    00241             break;
    00242         case COMPRESSED_ZIP:
    00243             t = "%{_unzipbin}";
    00244             needtar = 0;
    00245             break;
    00246         }
    00247         zipper = rpmGetPath(t, NULL);
    00248         buf[0] = '\0';
    00249         t = stpcpy(buf, zipper);
    00250         zipper = _free(zipper);
    00251         *t++ = ' ';
    00252         t = stpcpy(t, fn);
    00253         if (needtar)
    00254             t = stpcpy( stpcpy( stpcpy(t, " | tar "), taropts), " -");
    00255         t = stpcpy(t,
    00256                 "\n"
    00257                 "STATUS=$?\n"
    00258                 "if [ $STATUS -ne 0 ]; then\n"
    00259                 "  exit $STATUS\n"
    00260                 "fi");
    00261     } else {
    00262         buf[0] = '\0';
    00263         t = stpcpy( stpcpy(buf, "tar "), taropts);
    00264         *t++ = ' ';
    00265         t = stpcpy(t, fn);
    00266     }
    00267
    00268     urlfn = _free(urlfn);
    00269     return buf;
    00270 }
    00271
    00279 static int doSetupMacro(Spec spec, char *line)
    00280         /*@globals rpmGlobalMacroContext,
    00281                 fileSystem@*/
    00282         /*@modifies spec->buildSubdir, spec->macros, spec->prep,
    00283                 rpmGlobalMacroContext, fileSystem @*/
    00284 {
    00285     char buf[BUFSIZ];
    00286     StringBuf before;
    00287     StringBuf after;
    00288     poptContext optCon;
    00289     int argc;
    00290     const char ** argv;
    00291     int arg;
    00292     const char * optArg;
    00293     int rc;
    00294     int num;
    00295
    00296     /*@-mods@*/
    00297     leaveDirs = skipDefaultAction = 0;
    00298     createDir = quietly = 0;
    00299     dirName = NULL;
    00300     /*@=mods@*/
    00301
    00302     if ((rc = poptParseArgvString(line, &argc, &argv))) {
    00303         rpmError(RPMERR_BADSPEC, _("Error parsing %%setup: %s\n"),
    00304                         poptStrerror(rc));
    00305         return RPMERR_BADSPEC;
    00306     }
    00307
    00308     before = newStringBuf();
    00309     after = newStringBuf();
    00310
    00311     optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
    00312     while ((arg = poptGetNextOpt(optCon)) > 0) {
    00313         optArg = poptGetOptArg(optCon);
    00314
    00315         /* We only parse -a and -b here */
    00316
    00317         if (parseNum(optArg, &num)) {
    00318             rpmError(RPMERR_BADSPEC, _("line %d: Bad arg to %%setup: %s\n"),
    00319                      spec->lineNum, (optArg ? optArg : "???"));
    00320             before = freeStringBuf(before);
    00321             after = freeStringBuf(after);
    00322             optCon = poptFreeContext(optCon);
    00323             argv = _free(argv);
    00324             return RPMERR_BADSPEC;
    00325         }
    00326
    00327         {   const char *chptr = doUntar(spec, num, quietly);
    00328             if (chptr == NULL)
    00329                 return RPMERR_BADSPEC;
    00330
    00331             appendLineStringBuf((arg == 'a' ? after : before), chptr);
    00332         }
    00333     }
    00334
    00335     if (arg < -1) {
    00336         rpmError(RPMERR_BADSPEC, _("line %d: Bad %%setup option %s: %s\n"),
    00337                  spec->lineNum,
    00338                  poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
    00339                  poptStrerror(arg));
    00340         before = freeStringBuf(before);
    00341         after = freeStringBuf(after);
    00342         optCon = poptFreeContext(optCon);
    00343         argv = _free(argv);
    00344         return RPMERR_BADSPEC;
    00345     }
    00346
    00347     if (dirName) {
    00348         spec->buildSubdir = xstrdup(dirName);
    00349     } else {
    00350         const char *name, *version;
    00351         (void) headerNVR(spec->packages->header, &name, &version, NULL);
    00352         sprintf(buf, "%s-%s", name, version);
    00353         spec->buildSubdir = xstrdup(buf);
    00354     }
    00355     addMacro(spec->macros, "buildsubdir", NULL, spec->buildSubdir, RMIL_SPEC);
    00356
    00357     optCon = poptFreeContext(optCon);
    00358     argv = _free(argv);
    00359
    00360     /* cd to the build dir */
    00361     {   const char * buildDirURL = rpmGenPath(spec->rootURL, "%{_builddir}", "");
    00362         const char *buildDir;
    00363
    00364         (void) urlPath(buildDirURL, &buildDir);
    00365         sprintf(buf, "cd %s", buildDir);
    00366         appendLineStringBuf(spec->prep, buf);
    00367         buildDirURL = _free(buildDirURL);
    00368     }
    00369
    00370     /* delete any old sources */
    00371     if (!leaveDirs) {
    00372         sprintf(buf, "rm -rf %s", spec->buildSubdir);
    00373         appendLineStringBuf(spec->prep, buf);
    00374     }
    00375
    00376     /* if necessary, create and cd into the proper dir */
    00377     if (createDir) {
    00378         sprintf(buf, MKDIR_P " %s\ncd %s",
    00379                 spec->buildSubdir, spec->buildSubdir);
    00380         appendLineStringBuf(spec->prep, buf);
    00381     }
    00382
    00383     /* do the default action */
    00384    if (!createDir && !skipDefaultAction) {
    00385         const char *chptr = doUntar(spec, 0, quietly);
    00386         if (!chptr)
    00387             return RPMERR_BADSPEC;
    00388         appendLineStringBuf(spec->prep, chptr);
    00389     }
    00390
    00391     appendStringBuf(spec->prep, getStringBuf(before));
    00392     before = freeStringBuf(before);
    00393
    00394     if (!createDir) {
    00395         sprintf(buf, "cd %s", spec->buildSubdir);
    00396         appendLineStringBuf(spec->prep, buf);
    00397     }
    00398
    00399     if (createDir && !skipDefaultAction) {
    00400         const char * chptr = doUntar(spec, 0, quietly);
    00401         if (chptr == NULL)
    00402             return RPMERR_BADSPEC;
    00403         appendLineStringBuf(spec->prep, chptr);
    00404     }
    00405
    00406     appendStringBuf(spec->prep, getStringBuf(after));
    00407     after = freeStringBuf(after);
    00408
    00409     /* XXX FIXME: owner & group fixes were conditioned on !geteuid() */
    00410     /* Fix the owner, group, and permissions of the setup build tree */
    00411     {   /*@observer@*/ static const char *fixmacs[] =
    00412                 { "%{_fixowner}", "%{_fixgroup}", "%{_fixperms}", NULL };
    00413         const char ** fm;
    00414
    00415         for (fm = fixmacs; *fm; fm++) {
    00416             const char *fix;
    00417             /*@-nullpass@*/
    00418             fix = rpmExpand(*fm, " .", NULL);
    00419             /*@=nullpass@*/
    00420             if (fix && *fix != '%')
    00421                 appendLineStringBuf(spec->prep, fix);
    00422             fix = _free(fix);
    00423         }
    00424     }
    00425
    00426     return 0;
    00427 }
    00428
    00435 static int doPatchMacro(Spec spec, char *line)
    00436         /*@globals rpmGlobalMacroContext,
    00437                 fileSystem@*/
    00438         /*@modifies spec->prep, rpmGlobalMacroContext, fileSystem @*/
    00439 {
    00440     char *opt_b;
    00441     int opt_P, opt_p, opt_R, opt_E;
    00442     char *s;
    00443     char buf[BUFSIZ], *bp;
    00444     int patch_nums[1024];  /* XXX - we can only handle 1024 patches! */
    00445     int patch_index, x;
    00446
    00447     memset(patch_nums, 0, sizeof(patch_nums));
    00448     opt_P = opt_p = opt_R = opt_E = 0;
    00449     opt_b = NULL;
    00450     patch_index = 0;
    00451
    00452     if (! strchr(" \t\n", line[6])) {
    00453         /* %patchN */
    00454         sprintf(buf, "%%patch -P %s", line + 6);
    00455     } else {
    00456         strcpy(buf, line);
    00457     }
    00458
    00459     /*@-internalglobs@*/        /* FIX: strtok has state */
    00460     for (bp = buf; (s = strtok(bp, " \t\n")) != NULL;) {
    00461         if (bp) {       /* remove 1st token (%patch) */
    00462             bp = NULL;
    00463             continue;
    00464         }
    00465         if (!strcmp(s, "-P")) {
    00466             opt_P = 1;
    00467         } else if (!strcmp(s, "-R")) {
    00468             opt_R = 1;
    00469         } else if (!strcmp(s, "-E")) {
    00470             opt_E = 1;
    00471         } else if (!strcmp(s, "-b")) {
    00472             /* orig suffix */
    00473             opt_b = strtok(NULL, " \t\n");
    00474             if (! opt_b) {
    00475                 rpmError(RPMERR_BADSPEC,
    00476                         _("line %d: Need arg to %%patch -b: %s\n"),
    00477                         spec->lineNum, spec->line);
    00478                 return RPMERR_BADSPEC;
    00479             }
    00480         } else if (!strcmp(s, "-z")) {
    00481             /* orig suffix */
    00482             opt_b = strtok(NULL, " \t\n");
    00483             if (! opt_b) {
    00484                 rpmError(RPMERR_BADSPEC,
    00485                         _("line %d: Need arg to %%patch -z: %s\n"),
    00486                         spec->lineNum, spec->line);
    00487                 return RPMERR_BADSPEC;
    00488             }
    00489         } else if (!strncmp(s, "-p", sizeof("-p")-1)) {
    00490             /* unfortunately, we must support -pX */
    00491             if (! strchr(" \t\n", s[2])) {
    00492                 s = s + 2;
    00493             } else {
    00494                 s = strtok(NULL, " \t\n");
    00495                 if (s == NULL) {
    00496                     rpmError(RPMERR_BADSPEC,
    00497                              _("line %d: Need arg to %%patch -p: %s\n"),
    00498                              spec->lineNum, spec->line);
    00499                     return RPMERR_BADSPEC;
    00500                 }
    00501             }
    00502             if (parseNum(s, &opt_p)) {
    00503                 rpmError(RPMERR_BADSPEC,
    00504                         _("line %d: Bad arg to %%patch -p: %s\n"),
    00505                         spec->lineNum, spec->line);
    00506                 return RPMERR_BADSPEC;
    00507             }
    00508         } else {
    00509             /* Must be a patch num */
    00510             if (patch_index == 1024) {
    00511                 rpmError(RPMERR_BADSPEC, _("Too many patches!\n"));
    00512                 return RPMERR_BADSPEC;
    00513             }
    00514             if (parseNum(s, &(patch_nums[patch_index]))) {
    00515                 rpmError(RPMERR_BADSPEC, _("line %d: Bad arg to %%patch: %s\n"),
    00516                          spec->lineNum, spec->line);
    00517                 return RPMERR_BADSPEC;
    00518             }
    00519             patch_index++;
    00520         }
    00521     }
    00522     /*@=internalglobs@*/
    00523
    00524     /* All args processed */
    00525
    00526     if (! opt_P) {
    00527         s = doPatch(spec, 0, opt_p, opt_b, opt_R, opt_E);
    00528         if (s == NULL)
    00529             return RPMERR_BADSPEC;
    00530         appendLineStringBuf(spec->prep, s);
    00531     }
    00532
    00533     for (x = 0; x < patch_index; x++) {
    00534         s = doPatch(spec, patch_nums[x], opt_p, opt_b, opt_R, opt_E);
    00535         if (s == NULL)
    00536             return RPMERR_BADSPEC;
    00537         appendLineStringBuf(spec->prep, s);
    00538     }
    00539
    00540     return 0;
    00541 }
    00542
    00543 int parsePrep(Spec spec)
    00544 {
    00545     int nextPart, res, rc;
    00546     StringBuf sb;
    00547     char **lines, **saveLines;
    00548
    00549     if (spec->prep != NULL) {
    00550         rpmError(RPMERR_BADSPEC, _("line %d: second %%prep\n"), spec->lineNum);
    00551         return RPMERR_BADSPEC;
    00552     }
    00553
    00554     spec->prep = newStringBuf();
    00555
    00556     /* There are no options to %prep */
    00557     if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
    00558         return PART_NONE;
    00559     }
    00560     if (rc)
    00561         return rc;
    00562
    00563     sb = newStringBuf();
    00564
    00565     while (! (nextPart = isPart(spec->line))) {
    00566         /* Need to expand the macros inline.  That way we  */
    00567         /* can give good line number information on error. */
    00568         appendStringBuf(sb, spec->line);
    00569         if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
    00570             nextPart = PART_NONE;
    00571             break;
    00572         }
    00573         if (rc)
    00574             return rc;
    00575     }
    00576
    00577     saveLines = splitString(getStringBuf(sb), strlen(getStringBuf(sb)), '\n');
    00578     /*@-usereleased@*/
    00579     for (lines = saveLines; *lines; lines++) {
    00580         res = 0;
    00581         if (! strncmp(*lines, "%setup", sizeof("%setup")-1)) {
    00582             res = doSetupMacro(spec, *lines);
    00583         } else if (! strncmp(*lines, "%patch", sizeof("%patch")-1)) {
    00584             res = doPatchMacro(spec, *lines);
    00585         } else {
    00586             appendLineStringBuf(spec->prep, *lines);
    00587         }
    00588         if (res && !spec->force) {
    00589             freeSplitString(saveLines);
    00590             sb = freeStringBuf(sb);
    00591             return res;
    00592         }
    00593     }
    00594     /*@=usereleased@*/
    00595
    00596     freeSplitString(saveLines);
    00597     sb = freeStringBuf(sb);
    00598
    00599     return nextPart;
    00600 }