00001
00006 #include "system.h"
00007
00008 #include <rpmio_internal.h>
00009 #include <rpmbuild.h>
00010 #include "debug.h"
00011
00012
00013
00014
00015
00016 static int leaveDirs, skipDefaultAction;
00017
00018 static int createDir, quietly;
00019
00020 static const char * dirName = NULL;
00021
00022 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
00040
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
00068
00069
00070 static char *doPatch(Spec spec, int c, int strip, const char *db,
00071 int reverse, int removeEmpties, int fuzz)
00072
00073
00074 {
00075 const char *fn, *urlfn;
00076 static char buf[BUFSIZ];
00077 char args[BUFSIZ], *t = args;
00078 struct Source *sp;
00079 rpmCompressedMagic compressed = COMPRESSED_NOT;
00080 int urltype;
00081
00082 for (sp = spec->sources; sp != NULL; sp = sp->next) {
00083 if ((sp->flags & RPMBUILD_ISPATCH) && (sp->num == c)) {
00084 break;
00085 }
00086 }
00087 if (sp == NULL) {
00088 rpmError(RPMERR_BADSPEC, _("No patch number %d\n"), c);
00089 return NULL;
00090 }
00091
00092 urlfn = rpmGetPath("%{_sourcedir}/", sp->source, NULL);
00093
00094 *t = '\0';
00095 if (db) {
00096 #if HAVE_OLDPATCH_21 == 0
00097 t = stpcpy(t, "-b ");
00098 #endif
00099 t = stpcpy( stpcpy(t, "--suffix "), db);
00100 }
00101 if (fuzz >= 0) {
00102 t = stpcpy(t, " --fuzz=");
00103 sprintf(t, "%d", fuzz);
00104 t += strlen(t);
00105 }
00106 if (reverse)
00107 t = stpcpy(t, " -R");
00108 if (removeEmpties)
00109 t = stpcpy(t, " -E");
00110
00111
00112 if (!spec->force && (isCompressed(urlfn, &compressed) || checkOwners(urlfn))) {
00113 urlfn = _free(urlfn);
00114 return NULL;
00115 }
00116
00117 fn = NULL;
00118 urltype = urlPath(urlfn, &fn);
00119 switch (urltype) {
00120 case URL_IS_HTTPS:
00121 case URL_IS_HTTP:
00122 case URL_IS_FTP:
00123 case URL_IS_HKP:
00124 case URL_IS_PATH:
00125 case URL_IS_UNKNOWN:
00126 break;
00127 case URL_IS_DASH:
00128 urlfn = _free(urlfn);
00129 return NULL;
00130 break;
00131 }
00132
00133 if (compressed) {
00134 const char *zipper = rpmGetPath(
00135 (compressed == COMPRESSED_BZIP2 ? "%{_bzip2bin}" : "%{_gzipbin}"),
00136 NULL);
00137
00138 sprintf(buf,
00139 "echo \"Patch #%d (%s):\"\n"
00140 "%s -d < '%s' | patch -p%d %s -s\n"
00141 "STATUS=$?\n"
00142 "if [ $STATUS -ne 0 ]; then\n"
00143 " exit $STATUS\n"
00144 "fi",
00145 c, (const char *) basename(fn),
00146 zipper,
00147 fn, strip, args);
00148 zipper = _free(zipper);
00149 } else {
00150 sprintf(buf,
00151 "echo \"Patch #%d (%s):\"\n"
00152 "patch -p%d %s -s < '%s'", c, (const char *) basename(fn),
00153 strip, args, fn);
00154 }
00155
00156 urlfn = _free(urlfn);
00157 return buf;
00158 }
00159
00160
00168
00169 static const char *doUntar(Spec spec, int c, int quietly)
00170
00171
00172 {
00173 const char *fn, *urlfn;
00174 static char buf[BUFSIZ];
00175 char *taropts;
00176 char *t = NULL;
00177 struct Source *sp;
00178 rpmCompressedMagic compressed = COMPRESSED_NOT;
00179 int urltype;
00180
00181 for (sp = spec->sources; sp != NULL; sp = sp->next) {
00182 if ((sp->flags & RPMBUILD_ISSOURCE) && (sp->num == c)) {
00183 break;
00184 }
00185 }
00186 if (sp == NULL) {
00187 rpmError(RPMERR_BADSPEC, _("No source number %d\n"), c);
00188 return NULL;
00189 }
00190
00191 urlfn = rpmGetPath("%{_sourcedir}/", sp->source, NULL);
00192
00193
00194 taropts = ((rpmIsVerbose() && !quietly) ? "-xvvf" : "-xf");
00195
00196
00197 #ifdef AUTOFETCH_NOT
00198
00199
00200
00201
00202 if (sp->flags & RPMTAG_NOSOURCE && autofetchnosource) {
00203 struct stat st;
00204 int rc;
00205 if (Lstat(urlfn, &st) != 0 && errno == ENOENT &&
00206 urlIsUrl(sp->fullSource) != URL_IS_UNKNOWN) {
00207 if ((rc = urlGetFile(sp->fullSource, urlfn)) != 0) {
00208 rpmError(RPMERR_BADFILENAME,
00209 _("Couldn't download nosource %s: %s\n"),
00210 sp->fullSource, ftpStrerror(rc));
00211 return NULL;
00212 }
00213 }
00214 }
00215 #endif
00216
00217
00218 if (!spec->force && (isCompressed(urlfn, &compressed) || checkOwners(urlfn))) {
00219 urlfn = _free(urlfn);
00220 return NULL;
00221 }
00222
00223 fn = NULL;
00224 urltype = urlPath(urlfn, &fn);
00225 switch (urltype) {
00226 case URL_IS_HTTPS:
00227 case URL_IS_HTTP:
00228 case URL_IS_FTP:
00229 case URL_IS_HKP:
00230 case URL_IS_PATH:
00231 case URL_IS_UNKNOWN:
00232 break;
00233 case URL_IS_DASH:
00234 urlfn = _free(urlfn);
00235 return NULL;
00236 break;
00237 }
00238
00239 if (compressed != COMPRESSED_NOT) {
00240 const char *zipper;
00241 int needtar = 1;
00242
00243 switch (compressed) {
00244 case COMPRESSED_NOT:
00245 case COMPRESSED_OTHER:
00246 t = "%{_gzipbin} -dc";
00247 break;
00248 case COMPRESSED_BZIP2:
00249 t = "%{_bzip2bin} -dc";
00250 break;
00251 case COMPRESSED_ZIP:
00252 if (rpmIsVerbose() && !quietly)
00253 t = "%{_unzipbin}";
00254 else
00255 t = "%{_unzipbin} -qq";
00256 needtar = 0;
00257 break;
00258 case COMPRESSED_LZMA:
00259 t = "%{__lzma} -dc";
00260 break;
00261 }
00262 zipper = rpmGetPath(t, NULL);
00263 buf[0] = '\0';
00264 t = stpcpy(buf, zipper);
00265 zipper = _free(zipper);
00266 t = stpcpy(t, " '");
00267 t = stpcpy(t, fn);
00268 t = stpcpy(t, "'");
00269 if (needtar)
00270 t = stpcpy( stpcpy( stpcpy(t, " | tar "), taropts), " -");
00271 t = stpcpy(t,
00272 "\n"
00273 "STATUS=$?\n"
00274 "if [ $STATUS -ne 0 ]; then\n"
00275 " exit $STATUS\n"
00276 "fi");
00277 } else {
00278 buf[0] = '\0';
00279 t = stpcpy( stpcpy(buf, "tar "), taropts);
00280 *t++ = ' ';
00281 t = stpcpy(t, fn);
00282 }
00283
00284 urlfn = _free(urlfn);
00285 return buf;
00286 }
00287
00288
00296 static int doSetupMacro(Spec spec, char *line)
00297
00298
00299
00300 {
00301 char buf[BUFSIZ];
00302 StringBuf before;
00303 StringBuf after;
00304 poptContext optCon;
00305 int argc;
00306 const char ** argv;
00307 int arg;
00308 const char * optArg;
00309 int rc;
00310 int num;
00311
00312
00313 leaveDirs = skipDefaultAction = 0;
00314 createDir = quietly = 0;
00315 dirName = NULL;
00316
00317
00318 if ((rc = poptParseArgvString(line, &argc, &argv))) {
00319 rpmError(RPMERR_BADSPEC, _("Error parsing %%setup: %s\n"),
00320 poptStrerror(rc));
00321 return RPMERR_BADSPEC;
00322 }
00323
00324 before = newStringBuf();
00325 after = newStringBuf();
00326
00327 optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
00328 while ((arg = poptGetNextOpt(optCon)) > 0) {
00329 optArg = poptGetOptArg(optCon);
00330
00331
00332
00333 if (parseNum(optArg, &num)) {
00334 rpmError(RPMERR_BADSPEC, _("line %d: Bad arg to %%setup: %s\n"),
00335 spec->lineNum, (optArg ? optArg : "???"));
00336 before = freeStringBuf(before);
00337 after = freeStringBuf(after);
00338 optCon = poptFreeContext(optCon);
00339 argv = _free(argv);
00340 return RPMERR_BADSPEC;
00341 }
00342
00343 { const char *chptr = doUntar(spec, num, quietly);
00344 if (chptr == NULL)
00345 return RPMERR_BADSPEC;
00346
00347 appendLineStringBuf((arg == 'a' ? after : before), chptr);
00348 }
00349 }
00350
00351 if (arg < -1) {
00352 rpmError(RPMERR_BADSPEC, _("line %d: Bad %%setup option %s: %s\n"),
00353 spec->lineNum,
00354 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
00355 poptStrerror(arg));
00356 before = freeStringBuf(before);
00357 after = freeStringBuf(after);
00358 optCon = poptFreeContext(optCon);
00359 argv = _free(argv);
00360 return RPMERR_BADSPEC;
00361 }
00362
00363 if (dirName) {
00364 spec->buildSubdir = xstrdup(dirName);
00365 } else {
00366 const char *name, *version;
00367 (void) headerNVR(spec->packages->header, &name, &version, NULL);
00368 sprintf(buf, "%s-%s", name, version);
00369 spec->buildSubdir = xstrdup(buf);
00370 }
00371 addMacro(spec->macros, "buildsubdir", NULL, spec->buildSubdir, RMIL_SPEC);
00372
00373 optCon = poptFreeContext(optCon);
00374 argv = _free(argv);
00375
00376
00377 { const char * buildDirURL = rpmGenPath(spec->rootURL, "%{_builddir}", "");
00378 const char *buildDir;
00379
00380 (void) urlPath(buildDirURL, &buildDir);
00381 sprintf(buf, "cd '%s'", buildDir);
00382 appendLineStringBuf(spec->prep, buf);
00383 buildDirURL = _free(buildDirURL);
00384 }
00385
00386
00387 if (!leaveDirs) {
00388 sprintf(buf, "rm -rf '%s'", spec->buildSubdir);
00389 appendLineStringBuf(spec->prep, buf);
00390 }
00391
00392
00393 if (createDir) {
00394 sprintf(buf, MKDIR_P " %s\ncd '%s'",
00395 spec->buildSubdir, spec->buildSubdir);
00396 appendLineStringBuf(spec->prep, buf);
00397 }
00398
00399
00400 if (!createDir && !skipDefaultAction) {
00401 const char *chptr = doUntar(spec, 0, quietly);
00402 if (!chptr)
00403 return RPMERR_BADSPEC;
00404 appendLineStringBuf(spec->prep, chptr);
00405 }
00406
00407 appendStringBuf(spec->prep, getStringBuf(before));
00408 before = freeStringBuf(before);
00409
00410 if (!createDir) {
00411 sprintf(buf, "cd '%s'", spec->buildSubdir);
00412 appendLineStringBuf(spec->prep, buf);
00413 }
00414
00415 if (createDir && !skipDefaultAction) {
00416 const char * chptr = doUntar(spec, 0, quietly);
00417 if (chptr == NULL)
00418 return RPMERR_BADSPEC;
00419 appendLineStringBuf(spec->prep, chptr);
00420 }
00421
00422 appendStringBuf(spec->prep, getStringBuf(after));
00423 after = freeStringBuf(after);
00424
00425
00426
00427 { static const char *fixmacs[] =
00428 { "%{_fixowner}", "%{_fixgroup}", "%{_fixperms}", NULL };
00429 const char ** fm;
00430
00431 for (fm = fixmacs; *fm; fm++) {
00432 const char *fix;
00433
00434 fix = rpmExpand(*fm, " .", NULL);
00435 if (fix && *fix != '%')
00436 appendLineStringBuf(spec->prep, fix);
00437 fix = _free(fix);
00438
00439 }
00440 }
00441
00442 return 0;
00443 }
00444
00451
00452 static int doPatchMacro(Spec spec, char *line)
00453
00454
00455
00456
00457 {
00458 char *opt_b;
00459 int opt_P, opt_p, opt_R, opt_E, opt_F;
00460 char *s;
00461 char buf[BUFSIZ], *bp;
00462 int patch_nums[1024];
00463 int patch_index, x;
00464
00465 memset(patch_nums, 0, sizeof(patch_nums));
00466 opt_P = opt_p = opt_R = opt_E = 0;
00467 opt_F = rpmExpandNumeric("%{_default_patch_fuzz}");
00468 opt_b = NULL;
00469 patch_index = 0;
00470
00471 if (! strchr(" \t\n", line[6])) {
00472
00473 sprintf(buf, "%%patch -P %s", line + 6);
00474 } else {
00475 strcpy(buf, line);
00476 }
00477
00478
00479 for (bp = buf; (s = strtok(bp, " \t\n")) != NULL;) {
00480 if (bp) {
00481 bp = NULL;
00482 continue;
00483 }
00484 if (!strcmp(s, "-P")) {
00485 opt_P = 1;
00486 } else if (!strcmp(s, "-R")) {
00487 opt_R = 1;
00488 } else if (!strcmp(s, "-E")) {
00489 opt_E = 1;
00490 } else if (!strcmp(s, "-b")) {
00491
00492 opt_b = strtok(NULL, " \t\n");
00493 if (! opt_b) {
00494 rpmError(RPMERR_BADSPEC,
00495 _("line %d: Need arg to %%patch -b: %s\n"),
00496 spec->lineNum, spec->line);
00497 return RPMERR_BADSPEC;
00498 }
00499 } else if (!strcmp(s, "-z")) {
00500
00501 opt_b = strtok(NULL, " \t\n");
00502 if (! opt_b) {
00503 rpmError(RPMERR_BADSPEC,
00504 _("line %d: Need arg to %%patch -z: %s\n"),
00505 spec->lineNum, spec->line);
00506 return RPMERR_BADSPEC;
00507 }
00508 } else if (!strncmp(s, "-F", strlen("-F"))) {
00509
00510 const char * fnum = NULL;
00511 char * end = NULL;
00512
00513 if (! strchr(" \t\n", s[2])) {
00514 fnum = s + 2;
00515 } else {
00516 fnum = strtok(NULL, " \t\n");
00517 }
00518 opt_F = (fnum ? strtol(fnum, &end, 10) : 0);
00519 if (! opt_F || *end) {
00520 rpmError(RPMERR_BADSPEC,
00521 _("line %d: Bad arg to %%patch -F: %s\n"),
00522 spec->lineNum, spec->line);
00523 return RPMERR_BADSPEC;
00524 }
00525 } else if (!strncmp(s, "-p", sizeof("-p")-1)) {
00526
00527 if (! strchr(" \t\n", s[2])) {
00528 s = s + 2;
00529 } else {
00530 s = strtok(NULL, " \t\n");
00531 if (s == NULL) {
00532 rpmError(RPMERR_BADSPEC,
00533 _("line %d: Need arg to %%patch -p: %s\n"),
00534 spec->lineNum, spec->line);
00535 return RPMERR_BADSPEC;
00536 }
00537 }
00538 if (parseNum(s, &opt_p)) {
00539 rpmError(RPMERR_BADSPEC,
00540 _("line %d: Bad arg to %%patch -p: %s\n"),
00541 spec->lineNum, spec->line);
00542 return RPMERR_BADSPEC;
00543 }
00544 } else {
00545
00546 if (patch_index == 1024) {
00547 rpmError(RPMERR_BADSPEC, _("Too many patches!\n"));
00548 return RPMERR_BADSPEC;
00549 }
00550 if (parseNum(s, &(patch_nums[patch_index]))) {
00551 rpmError(RPMERR_BADSPEC, _("line %d: Bad arg to %%patch: %s\n"),
00552 spec->lineNum, spec->line);
00553 return RPMERR_BADSPEC;
00554 }
00555 patch_index++;
00556 }
00557 }
00558
00559
00560
00561
00562 if (! opt_P) {
00563 s = doPatch(spec, 0, opt_p, opt_b, opt_R, opt_E, opt_F);
00564 if (s == NULL)
00565 return RPMERR_BADSPEC;
00566 appendLineStringBuf(spec->prep, s);
00567 }
00568
00569 for (x = 0; x < patch_index; x++) {
00570 s = doPatch(spec, patch_nums[x], opt_p, opt_b, opt_R, opt_E, opt_F);
00571 if (s == NULL)
00572 return RPMERR_BADSPEC;
00573 appendLineStringBuf(spec->prep, s);
00574 }
00575
00576 return 0;
00577 }
00578
00579
00580 int parsePrep(Spec spec)
00581 {
00582 int nextPart, res, rc;
00583 StringBuf sb;
00584 char **lines, **saveLines;
00585
00586 if (spec->prep != NULL) {
00587 rpmError(RPMERR_BADSPEC, _("line %d: second %%prep\n"), spec->lineNum);
00588 return RPMERR_BADSPEC;
00589 }
00590
00591 spec->prep = newStringBuf();
00592
00593
00594 if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
00595 return PART_NONE;
00596 }
00597 if (rc)
00598 return rc;
00599
00600 sb = newStringBuf();
00601
00602 while (! (nextPart = isPart(spec->line))) {
00603
00604
00605 appendStringBuf(sb, spec->line);
00606 if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
00607 nextPart = PART_NONE;
00608 break;
00609 }
00610 if (rc)
00611 return rc;
00612 }
00613
00614 saveLines = splitString(getStringBuf(sb), strlen(getStringBuf(sb)), '\n');
00615
00616 for (lines = saveLines; *lines; lines++) {
00617 res = 0;
00618
00619 if (! strncmp(*lines, "%setup", sizeof("%setup")-1)) {
00620 res = doSetupMacro(spec, *lines);
00621 } else if (! strncmp(*lines, "%patch", sizeof("%patch")-1)) {
00622 res = doPatchMacro(spec, *lines);
00623 } else {
00624 appendLineStringBuf(spec->prep, *lines);
00625 }
00626
00627 if (res && !spec->force) {
00628 freeSplitString(saveLines);
00629 sb = freeStringBuf(sb);
00630 return res;
00631 }
00632 }
00633
00634
00635 freeSplitString(saveLines);
00636 sb = freeStringBuf(sb);
00637
00638 return nextPart;
00639 }