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

Generated at Sun Apr 8 18:42:58 2001 for rpm by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000