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

Generated at Thu Apr 19 15:29:41 2001 for rpm by doxygen1.2.6-20010408 written by Dimitri van Heesch, © 1997-2001