Attempt to fetch sources/patches when they are missing from %_sourcedir
authorJindrich Novy <jnovy@redhat.com>
Tue, 19 Apr 2011 11:24:34 +0000 (13:24 +0200)
committerJindrich Novy <jnovy@redhat.com>
Tue, 19 Apr 2011 11:24:34 +0000 (13:24 +0200)
- use _default_source_url macro to specify default URL when it is
  missing from the spec Source/Patch line
- this feature can be disabled by defining _disable_source_fetch to 1

build/parsePreamble.c
build/parsePrep.c

index d9f2fdb..4ca5df5 100644 (file)
@@ -6,9 +6,11 @@
 #include "system.h"
 
 #include <ctype.h>
+#include <errno.h>
 
 #include <rpm/header.h>
 #include <rpm/rpmlog.h>
+#include <rpm/rpmurl.h>
 #include <rpm/rpmfileutil.h>
 #include "rpmio/rpmlua.h"
 #include "build/rpmbuild_internal.h"
@@ -252,6 +254,7 @@ static int addSource(rpmSpec spec, Package pkg, const char *field, rpmTagVal tag
     p->flags = flag;
     p->source = strrchr(p->fullSource, '/');
     if (p->source) {
+       if ((buf = strrchr(p->source,'='))) p->source = buf;
        p->source++;
     } else {
        p->source = p->fullSource;
@@ -269,6 +272,28 @@ static int addSource(rpmSpec spec, Package pkg, const char *field, rpmTagVal tag
 
     if (tag != RPMTAG_ICON) {
        char *body = rpmGetPath("%{_sourcedir}/", p->source, NULL);
+       struct stat st;
+
+       /* try to download source/patch if it's missing */
+       if (lstat(body, &st) != 0 && errno == ENOENT && !rpmExpandNumeric("%{_disable_source_fetch}")) {
+           char *url = NULL;
+           if (urlIsURL(p->fullSource) != URL_IS_UNKNOWN) {
+               url = rstrdup(p->fullSource);
+           } else {
+               url = rpmExpand("%{_default_source_url}", NULL);
+               rstrcat(&url, p->source);
+               if (*url == '%') url = _free(url);
+           }
+           if (url) {
+               rpmlog(RPMLOG_WARNING, _("Downloading %s to %s\n"), url, body);
+               if (urlGetFile(url, body) != 0) {
+                   free(url);
+                   rpmlog(RPMLOG_ERR, _("Couldn't download %s\n"), p->fullSource);
+                   return RPMRC_FAIL;
+               }
+               free(url);
+           }
+       }
 
        rasprintf(&buf, "%s%d",
                (flag & RPMBUILD_ISPATCH) ? "PATCH" : "SOURCE", num);
index 1a9ee6a..de2b2a0 100644 (file)
@@ -158,26 +158,6 @@ static char *doUntar(rpmSpec spec, uint32_t c, int quietly)
 
     fn = rpmGetPath("%{_sourcedir}/", sp->source, NULL);
 
-#ifdef AUTOFETCH_NOT   /* XXX don't expect this code to be enabled */
-    /* XXX
-     * XXX If nosource file doesn't exist, try to fetch from url.
-     * XXX TODO: add a "--fetch" enabler.
-     */
-    if (sp->flags & RPMTAG_NOSOURCE && autofetchnosource) {
-       struct stat st;
-       int rc;
-       if (lstat(fn, &st) != 0 && errno == ENOENT &&
-           urlIsUrl(sp->fullSource) != URL_IS_UNKNOWN) {
-           if ((rc = urlGetFile(sp->fullSource, fn)) != 0) {
-               rpmlog(RPMLOG_ERR,
-                       _("Couldn't download nosource %s: %s\n"),
-                       sp->fullSource);
-               return NULL;
-           }
-       }
-    }
-#endif
-
     /* XXX On non-build parse's, file cannot be stat'd or read */
     if (!(spec->flags & RPMSPEC_FORCE) && (rpmFileIsCompressed(fn, &compressed) || checkOwners(fn))) {
        fn = _free(fn);