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

Generated on Sun Feb 2 23:31:59 2003 for rpm by doxygen1.2.18