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) {
00102 t = stpcpy(t, " -F");
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 = opt_F = 0;
00467 opt_b = NULL;
00468 patch_index = 0;
00469
00470 if (! strchr(" \t\n", line[6])) {
00471
00472 sprintf(buf, "%%patch -P %s", line + 6);
00473 } else {
00474 strcpy(buf, line);
00475 }
00476
00477
00478 for (bp = buf; (s = strtok(bp, " \t\n")) != NULL;) {
00479 if (bp) {
00480 bp = NULL;
00481 continue;
00482 }
00483 if (!strcmp(s, "-P")) {
00484 opt_P = 1;
00485 } else if (!strcmp(s, "-R")) {
00486 opt_R = 1;
00487 } else if (!strcmp(s, "-E")) {
00488 opt_E = 1;
00489 } else if (!strcmp(s, "-b")) {
00490
00491 opt_b = strtok(NULL, " \t\n");
00492 if (! opt_b) {
00493 rpmError(RPMERR_BADSPEC,
00494 _("line %d: Need arg to %%patch -b: %s\n"),
00495 spec->lineNum, spec->line);
00496 return RPMERR_BADSPEC;
00497 }
00498 } else if (!strcmp(s, "-z")) {
00499
00500 opt_b = strtok(NULL, " \t\n");
00501 if (! opt_b) {
00502 rpmError(RPMERR_BADSPEC,
00503 _("line %d: Need arg to %%patch -z: %s\n"),
00504 spec->lineNum, spec->line);
00505 return RPMERR_BADSPEC;
00506 }
00507 } else if (!strncmp(s, "-F", strlen("-F"))) {
00508
00509 const char * fnum = NULL;
00510 char * end = NULL;
00511
00512 if (! strchr(" \t\n", s[2])) {
00513 fnum = s + 2;
00514 } else {
00515 fnum = strtok(NULL, " \t\n");
00516 }
00517 opt_F = (fnum ? strtol(fnum, &end, 10) : 0);
00518 if (! opt_F || *end) {
00519 rpmError(RPMERR_BADSPEC,
00520 _("line %d: Bad arg to %%patch -F: %s\n"),
00521 spec->lineNum, spec->line);
00522 return RPMERR_BADSPEC;
00523 }
00524 } else if (!strncmp(s, "-p", sizeof("-p")-1)) {
00525
00526 if (! strchr(" \t\n", s[2])) {
00527 s = s + 2;
00528 } else {
00529 s = strtok(NULL, " \t\n");
00530 if (s == NULL) {
00531 rpmError(RPMERR_BADSPEC,
00532 _("line %d: Need arg to %%patch -p: %s\n"),
00533 spec->lineNum, spec->line);
00534 return RPMERR_BADSPEC;
00535 }
00536 }
00537 if (parseNum(s, &opt_p)) {
00538 rpmError(RPMERR_BADSPEC,
00539 _("line %d: Bad arg to %%patch -p: %s\n"),
00540 spec->lineNum, spec->line);
00541 return RPMERR_BADSPEC;
00542 }
00543 } else {
00544
00545 if (patch_index == 1024) {
00546 rpmError(RPMERR_BADSPEC, _("Too many patches!\n"));
00547 return RPMERR_BADSPEC;
00548 }
00549 if (parseNum(s, &(patch_nums[patch_index]))) {
00550 rpmError(RPMERR_BADSPEC, _("line %d: Bad arg to %%patch: %s\n"),
00551 spec->lineNum, spec->line);
00552 return RPMERR_BADSPEC;
00553 }
00554 patch_index++;
00555 }
00556 }
00557
00558
00559
00560
00561 if (! opt_P) {
00562 s = doPatch(spec, 0, opt_p, opt_b, opt_R, opt_E, opt_F);
00563 if (s == NULL)
00564 return RPMERR_BADSPEC;
00565 appendLineStringBuf(spec->prep, s);
00566 }
00567
00568 for (x = 0; x < patch_index; x++) {
00569 s = doPatch(spec, patch_nums[x], opt_p, opt_b, opt_R, opt_E, opt_F);
00570 if (s == NULL)
00571 return RPMERR_BADSPEC;
00572 appendLineStringBuf(spec->prep, s);
00573 }
00574
00575 return 0;
00576 }
00577
00578
00579 int parsePrep(Spec spec)
00580 {
00581 int nextPart, res, rc;
00582 StringBuf sb;
00583 char **lines, **saveLines;
00584
00585 if (spec->prep != NULL) {
00586 rpmError(RPMERR_BADSPEC, _("line %d: second %%prep\n"), spec->lineNum);
00587 return RPMERR_BADSPEC;
00588 }
00589
00590 spec->prep = newStringBuf();
00591
00592
00593 if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
00594 return PART_NONE;
00595 }
00596 if (rc)
00597 return rc;
00598
00599 sb = newStringBuf();
00600
00601 while (! (nextPart = isPart(spec->line))) {
00602
00603
00604 appendStringBuf(sb, spec->line);
00605 if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
00606 nextPart = PART_NONE;
00607 break;
00608 }
00609 if (rc)
00610 return rc;
00611 }
00612
00613 saveLines = splitString(getStringBuf(sb), strlen(getStringBuf(sb)), '\n');
00614
00615 for (lines = saveLines; *lines; lines++) {
00616 res = 0;
00617
00618 if (! strncmp(*lines, "%setup", sizeof("%setup")-1)) {
00619 res = doSetupMacro(spec, *lines);
00620 } else if (! strncmp(*lines, "%patch", sizeof("%patch")-1)) {
00621 res = doPatchMacro(spec, *lines);
00622 } else {
00623 appendLineStringBuf(spec->prep, *lines);
00624 }
00625
00626 if (res && !spec->force) {
00627 freeSplitString(saveLines);
00628 sb = freeStringBuf(sb);
00629 return res;
00630 }
00631 }
00632
00633
00634 freeSplitString(saveLines);
00635 sb = freeStringBuf(sb);
00636
00637 return nextPart;
00638 }