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

Generated on Wed Jan 28 12:45:24 2009 for rpm by  doxygen 1.4.7