From 769bc86d6d9372c1b3e804309227a0e8554d72d1 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Fri, 11 Apr 2008 11:18:25 +0300 Subject: [PATCH] Simplify url io a lot - move urlhelper call to urlGetFile() - have urlOpen() call urlGetFile() instead of going the other way around and causing local copies and all sorts of craziness in the process --- rpmio/rpmio.c | 37 +++++++++-------------------- rpmio/url.c | 76 ++++++++++++++++++----------------------------------------- 2 files changed, 34 insertions(+), 79 deletions(-) diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c index 60c2c5c..7675d23 100644 --- a/rpmio/rpmio.c +++ b/rpmio/rpmio.c @@ -21,11 +21,9 @@ extern int h_errno; #define IPPORT_HTTP 80 #endif -#include #include #include #include -#include #include "rpmio/rpmio_internal.h" #include "rpmio/ugid.h" @@ -771,44 +769,31 @@ int ufdClose( void * cookie) * - better error checking + reporting * - curl & friends don't know about hkp://, transform to http? */ + static FD_t urlOpen(const char * url, int flags, mode_t mode) { - FD_t fd = NULL; - char *cmd = NULL; + FD_t fd; char *dest = NULL; - char *urlhelper = NULL; - int rc; - pid_t pid, wait; - - urlhelper = rpmExpand("%{?_urlhelper}", NULL); + int rc = 1; /* assume failure */ - dest = rpmGenPath(NULL, "%{_tmppath}/", "rpm-transfer.XXXXXX"); - close(mkstemp(dest)); - rasprintf(&cmd, "%s %s %s\n", urlhelper, dest, url); - urlhelper = _free(urlhelper); - - if ((pid = fork()) == 0) { - ARGV_t argv = NULL; - argvSplit(&argv, cmd, " "); - execvp(argv[0], (char *const *)argv); - exit(-1); /* error out if exec fails */ + fd = rpmMkTemp(NULL, &dest); + if (fd == NULL) { + return NULL; } - wait = waitpid(pid, &rc, 0); + Fclose(fd); - if (!WIFEXITED(rc) || WEXITSTATUS(rc)) { - rpmlog(RPMLOG_ERR, _("URL helper failed: %s (%d)\n"), - cmd, WEXITSTATUS(rc)); - } else { + rc = urlGetFile(url, dest); + if (rc == 0) { fd = fdOpen(dest, flags, mode); unlink(dest); + } else { + fd = NULL; } dest = _free(dest); - cmd = _free(cmd); return fd; } - static FD_t ufdOpen(const char * url, int flags, mode_t mode) { FD_t fd = NULL; diff --git a/rpmio/url.c b/rpmio/url.c index 2c90a6f..6fa2d1d 100644 --- a/rpmio/url.c +++ b/rpmio/url.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include "debug.h" @@ -244,69 +246,37 @@ int urlSplit(const char * url, urlinfo *uret) return 0; } + int urlGetFile(const char * url, const char * dest) { + char *cmd = NULL; + const char *target = NULL; + char *urlhelper = NULL; int rc; - FD_t sfd = NULL; - FD_t tfd = NULL; - const char * sfuPath = NULL; - int urlType = urlPath(url, &sfuPath); + pid_t pid, wait; - if (*sfuPath == '\0') - return FTPERR_UNKNOWN; - - sfd = Fopen(url, "r.ufdio"); - if (sfd == NULL || Ferror(sfd)) { - rpmlog(RPMLOG_ERR, _("failed to open %s: %s\n"), url, Fstrerror(sfd)); - rc = FTPERR_UNKNOWN; - goto exit; - } + urlhelper = rpmExpand("%{?_urlhelper}", NULL); if (dest == NULL) { - if ((dest = strrchr(sfuPath, '/')) != NULL) - dest++; - else - dest = sfuPath; + urlPath(url, &target); + } else { + target = dest; } - if (dest == NULL) - return FTPERR_UNKNOWN; - - tfd = Fopen(dest, "w.ufdio"); -if (_url_debug) -fprintf(stderr, "*** urlGetFile sfd %p %s tfd %p %s\n", sfd, url, (tfd ? tfd : NULL), dest); - if (tfd == NULL || Ferror(tfd)) { - /* XXX Fstrerror */ - rpmlog(RPMLOG_ERR, _("failed to create %s: %s\n"), dest, Fstrerror(tfd)); - rc = FTPERR_UNKNOWN; - goto exit; - } + /* XXX TODO: sanity checks like target == dest... */ - switch (urlType) { - case URL_IS_HTTPS: - case URL_IS_HTTP: - case URL_IS_HKP: - case URL_IS_FTP: - case URL_IS_PATH: - case URL_IS_DASH: - case URL_IS_UNKNOWN: - if ((rc = ufdGetFile(sfd, tfd))) { - (void) unlink(dest); - /* XXX FIXME: sfd possibly closed by copyData */ - (void) Fclose(sfd) ; - } - sfd = NULL; /* XXX Fclose(sfd) done by ufdGetFile */ - break; - default: - rc = FTPERR_UNKNOWN; - break; - } + rasprintf(&cmd, "%s %s %s\n", urlhelper, target, url); + urlhelper = _free(urlhelper); -exit: - if (tfd) - (void) Fclose(tfd); - if (sfd) - (void) Fclose(sfd); + if ((pid = fork()) == 0) { + ARGV_t argv = NULL; + argvSplit(&argv, cmd, " "); + execvp(argv[0], argv); + exit(-1); /* error out if exec fails */ + } + wait = waitpid(pid, &rc, 0); + cmd = _free(cmd); return rc; + } -- 2.7.4