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

Generated on Wed Mar 13 15:34:46 2002 for rpm by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002