Add has_key() method to header object
[platform/upstream/rpm.git] / build.c
diff --git a/build.c b/build.c
index 1cf6aec..5c1f530 100644 (file)
--- a/build.c
+++ b/build.c
@@ -4,6 +4,8 @@
 
 #include "system.h"
 
+#include <libgen.h>
+
 #include <rpm/rpmcli.h>
 #include <rpm/rpmtag.h>
 #include <rpm/rpmlib.h>                /* rpmrc, MACHTABLE .. */
@@ -14,6 +16,7 @@
 #include <rpm/rpmts.h>
 #include <rpm/rpmfileutil.h>
 #include <rpm/rpmlog.h>
+#include <lib/misc.h>
 
 #include "build.h"
 #include "debug.h"
@@ -53,18 +56,21 @@ static int isSpecFile(const char * specfile)
 {
     char buf[256];
     const char * s;
-    FD_t fd;
+    FILE * f;
     int count;
     int checking;
 
-    fd = Fopen(specfile, "r.ufdio");
-    if (fd == NULL || Ferror(fd)) {
+    f = fopen(specfile, "r");
+    if (f == NULL || ferror(f)) {
        rpmlog(RPMLOG_ERR, _("Unable to open spec file %s: %s\n"),
-               specfile, Fstrerror(fd));
+               specfile, strerror(errno));
        return 0;
     }
-    count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
-    (void) Fclose(fd);
+    count = fread(buf, sizeof(buf[0]), sizeof(buf), f);
+    (void) fclose(f);
+
+    if (count == 0)
+       return 0;
 
     checking = 1;
     for (s = buf; count--; s++) {
@@ -89,6 +95,78 @@ static int isSpecFile(const char * specfile)
     return 1;
 }
 
+/* 
+ * Try to find a spec from a tarball pointed to by arg. 
+ * Return absolute path to spec name on success, otherwise NULL.
+ */
+static char * getTarSpec(const char *arg)
+{
+    char *specFile = NULL;
+    char *specDir;
+    char *specBase;
+    char *tmpSpecFile;
+    const char **try;
+    char tarbuf[BUFSIZ];
+    int gotspec = 0, res;
+    static const char *tryspec[] = { "Specfile", "\\*.spec", NULL };
+
+    specDir = rpmGetPath("%{_specdir}", NULL);
+    tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
+
+    (void) close(mkstemp(tmpSpecFile));
+
+    for (try = tryspec; *try != NULL; try++) {
+       FILE *fp;
+       char *cmd;
+
+       cmd = rpmExpand("%{uncompress: ", arg, "} | ",
+                       "%{__tar} xOvf - --wildcards ", *try,
+                       " 2>&1 > ", tmpSpecFile, NULL);
+
+       if (!(fp = popen(cmd, "r"))) {
+           rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
+       } else {
+           char *fok = fgets(tarbuf, sizeof(tarbuf) - 1, fp);
+           pclose(fp);
+           gotspec = (fok != NULL) && isSpecFile(tmpSpecFile);
+       }
+
+       if (!gotspec) 
+           unlink(tmpSpecFile);
+       free(cmd);
+    }
+
+    if (!gotspec) {
+       rpmlog(RPMLOG_ERR, _("Failed to read spec file from %s\n"), arg);
+       goto exit;
+    }
+
+    specBase = basename(tarbuf);
+    /* remove trailing \n */
+    specBase[strlen(specBase)-1] = '\0';
+
+    rasprintf(&specFile, "%s/%s", specDir, specBase);
+    res = rename(tmpSpecFile, specFile);
+
+    if (res) {
+       rpmlog(RPMLOG_ERR, _("Failed to rename %s to %s: %m\n"),
+               tmpSpecFile, specFile);
+       free(specFile);
+       specFile = NULL;
+    } else {
+       /* mkstemp() can give unnecessarily strict permissions, fixup */
+       mode_t mask;
+       umask(mask = umask(0));
+       (void) chmod(specFile, 0666 & ~mask);
+    }
+
+exit:
+    (void) unlink(tmpSpecFile);
+    free(tmpSpecFile);
+    free(specDir);
+    return specFile;
+}
+
 /**
  */
 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
@@ -97,12 +175,9 @@ static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
     const char * cookie = ba->cookie;
     int buildAmount = ba->buildAmount;
     char * buildRootURL = NULL;
-    const char * specFile;
-    const char * specURL;
-    int specut;
-    char buf[BUFSIZ];
+    char * specFile = NULL;
     rpmSpec spec = NULL;
-    int rc;
+    int rc = 1; /* assume failure */
 
 #ifndef        DYING
     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
@@ -111,156 +186,84 @@ static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
     if (ba->buildRootOverride)
        buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
 
-    /* FIX: static zcmds heartburn */
-    if (ba->buildMode == 't') {
-       FILE *fp;
-       char * specDir;
-       char * tmpSpecFile;
-       char * cmd, * s;
-       rpmCompressedMagic res = COMPRESSED_OTHER;
-       static const char *zcmds[] =
-               { "cat", "gunzip", "bunzip2", "cat" };
-
-       specDir = rpmGetPath("%{_specdir}", NULL);
-
-       tmpSpecFile = (char *) rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
-#if defined(HAVE_MKSTEMP)
-       (void) close(mkstemp(tmpSpecFile));
-#else
-       (void) mktemp(tmpSpecFile);
-#endif
-
-       (void) rpmFileIsCompressed(arg, &res);
-
-       cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
-       sprintf(cmd, "%s < '%s' | tar xOvf - Specfile 2>&1 > '%s'",
-                       zcmds[res & 0x3], arg, tmpSpecFile);
-       if (!(fp = popen(cmd, "r"))) {
-           rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
-           specDir = _free(specDir);
-           tmpSpecFile = _free(tmpSpecFile);
-           return 1;
-       }
-       if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
-           /* Try again */
-           (void) pclose(fp);
-
-           sprintf(cmd, "%s < '%s' | tar xOvf - --wildcards \\*.spec 2>&1 > '%s'",
-                   zcmds[res & 0x3], arg, tmpSpecFile);
-           if (!(fp = popen(cmd, "r"))) {
-               rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
-               specDir = _free(specDir);
-               tmpSpecFile = _free(tmpSpecFile);
-               return 1;
-           }
-           if (!fgets(buf, sizeof(buf) - 1, fp)) {
-               /* Give up */
-               rpmlog(RPMLOG_ERR, _("Failed to read spec file from %s\n"),
-                       arg);
-               (void) unlink(tmpSpecFile);
-               specDir = _free(specDir);
-               tmpSpecFile = _free(tmpSpecFile);
-               return 1;
-           }
-       }
-       (void) pclose(fp);
-
-       cmd = s = buf;
-       while (*cmd != '\0') {
-           if (*cmd == '/') s = cmd + 1;
-           cmd++;
-       }
+    /* Create build tree if necessary */
+    const char * buildtree = "%{_topdir}:%{_specdir}:%{_sourcedir}:%{_builddir}:%{_rpmdir}:%{_srcrpmdir}:%{_buildrootdir}";
+    const char * rootdir = rpmtsRootDir(ts);
+    if (rpmMkdirs(!rstreq(rootdir, "/") ? rootdir : NULL , buildtree)) {
+       goto exit;
+    }
 
-       cmd = s;
-
-       /* remove trailing \n */
-       s = cmd + strlen(cmd) - 1;
-       *s = '\0';
-
-       specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
-       sprintf(s, "%s/%s", specDir, cmd);
-       res = rename(tmpSpecFile, s);
-       specDir = _free(specDir);
-       
-       if (res) {
-           rpmlog(RPMLOG_ERR, _("Failed to rename %s to %s: %m\n"),
-                       tmpSpecFile, s);
-           (void) unlink(tmpSpecFile);
-           tmpSpecFile = _free(tmpSpecFile);
-           return 1;
-       }
-       tmpSpecFile = _free(tmpSpecFile);
+    if (ba->buildMode == 't') {
+       char *srcdir = NULL, *dir;
 
-       /* Make the directory which contains the tarball the source 
-          directory for this run */
+       specFile = getTarSpec(arg);
+       if (!specFile)
+           goto exit;
 
+       /* Make the directory of the tarball %_sourcedir for this run */
+       /* dirname() may modify contents so extra hoops needed. */
        if (*arg != '/') {
-           if (!getcwd(buf, BUFSIZ)) {
-               rpmlog(RPMLOG_ERR, "getcwd failed: %m\n");
-               return 1;
-           }
-           strcat(buf, "/");
-           strcat(buf, arg);
-       } else 
-           strcpy(buf, arg);
-
-       cmd = buf + strlen(buf) - 1;
-       while (*cmd != '/') cmd--;
-       *cmd = '\0';
-
-       addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
+           dir = rpmGetCwd();
+           rstrscat(&dir, "/", arg, NULL);
+       } else {
+           dir = xstrdup(arg);
+       }
+       srcdir = dirname(dir);
+       addMacro(NULL, "_sourcedir", NULL, srcdir, RMIL_TARBALL);
+       free(dir);
     } else {
-       specURL = arg;
+       specFile = xstrdup(arg);
     }
 
-    specut = urlPath(specURL, &specFile);
     if (*specFile != '/') {
-       char *s = alloca(BUFSIZ);
-       if (!getcwd(s, BUFSIZ)) {
-           rpmlog(RPMLOG_ERR, "getcwd failed: %m\n");
-           rc = 1;
-           goto exit;
-       }
-       strcat(s, "/");
-       strcat(s, arg);
-       specURL = s;
+       char *cwd = rpmGetCwd();
+       char *s = NULL;
+       rasprintf(&s, "%s/%s", cwd, arg);
+       free(cwd);
+       free(specFile);
+       specFile = s;
     }
 
-    if (specut != URL_IS_DASH) {
-       struct stat st;
-       if (stat(specURL, &st) < 0) {
-           rpmlog(RPMLOG_ERR, _("failed to stat %s: %m\n"), specURL);
-           rc = 1;
-           goto exit;
-       }
-       if (! S_ISREG(st.st_mode)) {
-           rpmlog(RPMLOG_ERR, _("File %s is not a regular file.\n"),
-               specURL);
-           rc = 1;
-           goto exit;
-       }
+    struct stat st;
+    if (stat(specFile, &st) < 0) {
+       rpmlog(RPMLOG_ERR, _("failed to stat %s: %m\n"), specFile);
+       goto exit;
+    }
+    if (! S_ISREG(st.st_mode)) {
+       rpmlog(RPMLOG_ERR, _("File %s is not a regular file.\n"), specFile);
+       goto exit;
+    }
 
-       /* Try to verify that the file is actually a specfile */
-       if (!isSpecFile(specURL)) {
-           rpmlog(RPMLOG_ERR,
-               _("File %s does not appear to be a specfile.\n"), specURL);
-           rc = 1;
-           goto exit;
-       }
+    /* Try to verify that the file is actually a specfile */
+    if (!isSpecFile(specFile)) {
+       rpmlog(RPMLOG_ERR,
+               _("File %s does not appear to be a specfile.\n"), specFile);
+       goto exit;
     }
     
+    /* Don't parse spec if only its removal is requested */
+    if (ba->buildAmount == RPMBUILD_RMSPEC) {
+       rc = unlink(specFile);
+       goto exit;
+    }
+
     /* Parse the spec file */
 #define        _anyarch(_f)    \
 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
-    if (parseSpec(ts, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
+    if (parseSpec(ts, specFile, ba->rootdir, buildRootURL, 0, passPhrase,
                cookie, _anyarch(buildAmount), ba->force))
     {
-       rc = 1;
        goto exit;
     }
 #undef _anyarch
     if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
-       rc = 1;
+       goto exit;
+    }
+
+    if ( ba->buildAmount&RPMBUILD_RMSOURCE && !(ba->buildAmount&~(RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)) ) {
+       rc = doRmSource(spec);
+       if ( rc == RPMRC_OK && ba->buildAmount&RPMBUILD_RMSPEC )
+           rc = unlink(specFile);
        goto exit;
     }
 
@@ -269,22 +272,21 @@ static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
 
     /* Check build prerequisites */
     if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
-       rc = 1;
        goto exit;
     }
 
     if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
-       rc = 1;
        goto exit;
     }
     
     if (ba->buildMode == 't')
-       (void) unlink(specURL);
+       (void) unlink(specFile);
     rc = 0;
 
 exit:
-    spec = freeSpec(spec);
-    buildRootURL = _free(buildRootURL);
+    free(specFile);
+    freeSpec(spec);
+    free(buildRootURL);
     return rc;
 }
 
@@ -320,7 +322,7 @@ int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
        char *target;
        if ((te = strchr(t, ',')) == NULL)
            te = t + strlen(t);
-       target = alloca(te-t+1);
+       target = xmalloc(te-t+1);
        strncpy(target, t, (te-t));
        target[te-t] = '\0';
        if (*te != '\0')
@@ -334,6 +336,7 @@ int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
        rpmFreeMacros(NULL);
        rpmFreeRpmrc();
        (void) rpmReadConfigFiles(rcfile, target);
+       free(target);
        rc = buildForTarget(ts, arg, ba);
        if (rc)
            break;