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 }
00259 zipper = rpmGetPath(t, NULL);
00260 buf[0] = '\0';
00261 t = stpcpy(buf, zipper);
00262 zipper = _free(zipper);
00263 *t++ = ' ';
00264 t = stpcpy(t, fn);
00265 if (needtar)
00266 t = stpcpy( stpcpy( stpcpy(t, " | tar "), taropts), " -");
00267 t = stpcpy(t,
00268 "\n"
00269 "STATUS=$?\n"
00270 "if [ $STATUS -ne 0 ]; then\n"
00271 " exit $STATUS\n"
00272 "fi");
00273 } else {
00274 buf[0] = '\0';
00275 t = stpcpy( stpcpy(buf, "tar "), taropts);
00276 *t++ = ' ';
00277 t = stpcpy(t, fn);
00278 }
00279
00280 urlfn = _free(urlfn);
00281 return buf;
00282 }
00283
00284
00292 static int doSetupMacro(Spec spec, char *line)
00293
00294
00295
00296 {
00297 char buf[BUFSIZ];
00298 StringBuf before;
00299 StringBuf after;
00300 poptContext optCon;
00301 int argc;
00302 const char ** argv;
00303 int arg;
00304 const char * optArg;
00305 int rc;
00306 int num;
00307
00308
00309 leaveDirs = skipDefaultAction = 0;
00310 createDir = quietly = 0;
00311 dirName = NULL;
00312
00313
00314 if ((rc = poptParseArgvString(line, &argc, &argv))) {
00315 rpmError(RPMERR_BADSPEC, _("Error parsing %%setup: %s\n"),
00316 poptStrerror(rc));
00317 return RPMERR_BADSPEC;
00318 }
00319
00320 before = newStringBuf();
00321 after = newStringBuf();
00322
00323 optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
00324 while ((arg = poptGetNextOpt(optCon)) > 0) {
00325 optArg = poptGetOptArg(optCon);
00326
00327
00328
00329 if (parseNum(optArg, &num)) {
00330 rpmError(RPMERR_BADSPEC, _("line %d: Bad arg to %%setup: %s\n"),
00331 spec->lineNum, (optArg ? optArg : "???"));
00332 before = freeStringBuf(before);
00333 after = freeStringBuf(after);
00334 optCon = poptFreeContext(optCon);
00335 argv = _free(argv);
00336 return RPMERR_BADSPEC;
00337 }
00338
00339 { const char *chptr = doUntar(spec, num, quietly);
00340 if (chptr == NULL)
00341 return RPMERR_BADSPEC;
00342
00343 appendLineStringBuf((arg == 'a' ? after : before), chptr);
00344 }
00345 }
00346
00347 if (arg < -1) {
00348 rpmError(RPMERR_BADSPEC, _("line %d: Bad %%setup option %s: %s\n"),
00349 spec->lineNum,
00350 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
00351 poptStrerror(arg));
00352 before = freeStringBuf(before);
00353 after = freeStringBuf(after);
00354 optCon = poptFreeContext(optCon);
00355 argv = _free(argv);
00356 return RPMERR_BADSPEC;
00357 }
00358
00359 if (dirName) {
00360 spec->buildSubdir = xstrdup(dirName);
00361 } else {
00362 const char *name, *version;
00363 (void) headerNVR(spec->packages->header, &name, &version, NULL);
00364 sprintf(buf, "%s-%s", name, version);
00365 spec->buildSubdir = xstrdup(buf);
00366 }
00367 addMacro(spec->macros, "buildsubdir", NULL, spec->buildSubdir, RMIL_SPEC);
00368
00369 optCon = poptFreeContext(optCon);
00370 argv = _free(argv);
00371
00372
00373 { const char * buildDirURL = rpmGenPath(spec->rootURL, "%{_builddir}", "");
00374 const char *buildDir;
00375
00376 (void) urlPath(buildDirURL, &buildDir);
00377 sprintf(buf, "cd %s", buildDir);
00378 appendLineStringBuf(spec->prep, buf);
00379 buildDirURL = _free(buildDirURL);
00380 }
00381
00382
00383 if (!leaveDirs) {
00384 sprintf(buf, "rm -rf %s", spec->buildSubdir);
00385 appendLineStringBuf(spec->prep, buf);
00386 }
00387
00388
00389 if (createDir) {
00390 sprintf(buf, MKDIR_P " %s\ncd %s",
00391 spec->buildSubdir, spec->buildSubdir);
00392 appendLineStringBuf(spec->prep, buf);
00393 }
00394
00395
00396 if (!createDir && !skipDefaultAction) {
00397 const char *chptr = doUntar(spec, 0, quietly);
00398 if (!chptr)
00399 return RPMERR_BADSPEC;
00400 appendLineStringBuf(spec->prep, chptr);
00401 }
00402
00403 appendStringBuf(spec->prep, getStringBuf(before));
00404 before = freeStringBuf(before);
00405
00406 if (!createDir) {
00407 sprintf(buf, "cd %s", spec->buildSubdir);
00408 appendLineStringBuf(spec->prep, buf);
00409 }
00410
00411 if (createDir && !skipDefaultAction) {
00412 const char * chptr = doUntar(spec, 0, quietly);
00413 if (chptr == NULL)
00414 return RPMERR_BADSPEC;
00415 appendLineStringBuf(spec->prep, chptr);
00416 }
00417
00418 appendStringBuf(spec->prep, getStringBuf(after));
00419 after = freeStringBuf(after);
00420
00421
00422
00423 { static const char *fixmacs[] =
00424 { "%{_fixowner}", "%{_fixgroup}", "%{_fixperms}", NULL };
00425 const char ** fm;
00426
00427 for (fm = fixmacs; *fm; fm++) {
00428 const char *fix;
00429
00430 fix = rpmExpand(*fm, " .", NULL);
00431 if (fix && *fix != '%')
00432 appendLineStringBuf(spec->prep, fix);
00433 fix = _free(fix);
00434
00435 }
00436 }
00437
00438 return 0;
00439 }
00440
00447
00448 static int doPatchMacro(Spec spec, char *line)
00449
00450
00451
00452
00453 {
00454 char *opt_b;
00455 int opt_P, opt_p, opt_R, opt_E, opt_F;
00456 char *s;
00457 char buf[BUFSIZ], *bp;
00458 int patch_nums[1024];
00459 int patch_index, x;
00460
00461 memset(patch_nums, 0, sizeof(patch_nums));
00462 opt_P = opt_p = opt_R = opt_E = opt_F = 0;
00463 opt_b = NULL;
00464 patch_index = 0;
00465
00466 if (! strchr(" \t\n", line[6])) {
00467
00468 sprintf(buf, "%%patch -P %s", line + 6);
00469 } else {
00470 strcpy(buf, line);
00471 }
00472
00473
00474 for (bp = buf; (s = strtok(bp, " \t\n")) != NULL;) {
00475 if (bp) {
00476 bp = NULL;
00477 continue;
00478 }
00479 if (!strcmp(s, "-P")) {
00480 opt_P = 1;
00481 } else if (!strcmp(s, "-R")) {
00482 opt_R = 1;
00483 } else if (!strcmp(s, "-E")) {
00484 opt_E = 1;
00485 } else if (!strcmp(s, "-b")) {
00486
00487 opt_b = strtok(NULL, " \t\n");
00488 if (! opt_b) {
00489 rpmError(RPMERR_BADSPEC,
00490 _("line %d: Need arg to %%patch -b: %s\n"),
00491 spec->lineNum, spec->line);
00492 return RPMERR_BADSPEC;
00493 }
00494 } else if (!strcmp(s, "-z")) {
00495
00496 opt_b = strtok(NULL, " \t\n");
00497 if (! opt_b) {
00498 rpmError(RPMERR_BADSPEC,
00499 _("line %d: Need arg to %%patch -z: %s\n"),
00500 spec->lineNum, spec->line);
00501 return RPMERR_BADSPEC;
00502 }
00503 } else if (!strncmp(s, "-F", strlen("-F"))) {
00504
00505 const char * fnum = NULL;
00506 char * end = NULL;
00507
00508 if (! strchr(" \t\n", s[2])) {
00509 fnum = s + 2;
00510 } else {
00511 fnum = strtok(NULL, " \t\n");
00512 }
00513 opt_F = (fnum ? strtol(fnum, &end, 10) : 0);
00514 if (! opt_F || *end) {
00515 rpmError(RPMERR_BADSPEC,
00516 _("line %d: Bad arg to %%patch -F: %s\n"),
00517 spec->lineNum, spec->line);
00518 return RPMERR_BADSPEC;
00519 }
00520 } else if (!strncmp(s, "-p", sizeof("-p")-1)) {
00521
00522 if (! strchr(" \t\n", s[2])) {
00523 s = s + 2;
00524 } else {
00525 s = strtok(NULL, " \t\n");
00526 if (s == NULL) {
00527 rpmError(RPMERR_BADSPEC,
00528 _("line %d: Need arg to %%patch -p: %s\n"),
00529 spec->lineNum, spec->line);
00530 return RPMERR_BADSPEC;
00531 }
00532 }
00533 if (parseNum(s, &opt_p)) {
00534 rpmError(RPMERR_BADSPEC,
00535 _("line %d: Bad arg to %%patch -p: %s\n"),
00536 spec->lineNum, spec->line);
00537 return RPMERR_BADSPEC;
00538 }
00539 } else {
00540
00541 if (patch_index == 1024) {
00542 rpmError(RPMERR_BADSPEC, _("Too many patches!\n"));
00543 return RPMERR_BADSPEC;
00544 }
00545 if (parseNum(s, &(patch_nums[patch_index]))) {
00546 rpmError(RPMERR_BADSPEC, _("line %d: Bad arg to %%patch: %s\n"),
00547 spec->lineNum, spec->line);
00548 return RPMERR_BADSPEC;
00549 }
00550 patch_index++;
00551 }
00552 }
00553
00554
00555
00556
00557 if (! opt_P) {
00558 s = doPatch(spec, 0, opt_p, opt_b, opt_R, opt_E, opt_F);
00559 if (s == NULL)
00560 return RPMERR_BADSPEC;
00561 appendLineStringBuf(spec->prep, s);
00562 }
00563
00564 for (x = 0; x < patch_index; x++) {
00565 s = doPatch(spec, patch_nums[x], opt_p, opt_b, opt_R, opt_E, opt_F);
00566 if (s == NULL)
00567 return RPMERR_BADSPEC;
00568 appendLineStringBuf(spec->prep, s);
00569 }
00570
00571 return 0;
00572 }
00573
00574
00575 int parsePrep(Spec spec)
00576 {
00577 int nextPart, res, rc;
00578 StringBuf sb;
00579 char **lines, **saveLines;
00580
00581 if (spec->prep != NULL) {
00582 rpmError(RPMERR_BADSPEC, _("line %d: second %%prep\n"), spec->lineNum);
00583 return RPMERR_BADSPEC;
00584 }
00585
00586 spec->prep = newStringBuf();
00587
00588
00589 if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
00590 return PART_NONE;
00591 }
00592 if (rc)
00593 return rc;
00594
00595 sb = newStringBuf();
00596
00597 while (! (nextPart = isPart(spec->line))) {
00598
00599
00600 appendStringBuf(sb, spec->line);
00601 if ((rc = readLine(spec, STRIP_NOTHING)) > 0) {
00602 nextPart = PART_NONE;
00603 break;
00604 }
00605 if (rc)
00606 return rc;
00607 }
00608
00609 saveLines = splitString(getStringBuf(sb), strlen(getStringBuf(sb)), '\n');
00610
00611 for (lines = saveLines; *lines; lines++) {
00612 res = 0;
00613
00614 if (! strncmp(*lines, "%setup", sizeof("%setup")-1)) {
00615 res = doSetupMacro(spec, *lines);
00616 } else if (! strncmp(*lines, "%patch", sizeof("%patch")-1)) {
00617 res = doPatchMacro(spec, *lines);
00618 } else {
00619 appendLineStringBuf(spec->prep, *lines);
00620 }
00621
00622 if (res && !spec->force) {
00623 freeSplitString(saveLines);
00624 sb = freeStringBuf(sb);
00625 return res;
00626 }
00627 }
00628
00629
00630 freeSplitString(saveLines);
00631 sb = freeStringBuf(sb);
00632
00633 return nextPart;
00634 }