Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

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

Generated on Sun Oct 26 13:01:57 2003 for rpm by doxygen1.2.18