2000-08-28 02:34:02 +08:00
|
|
|
/** \ingroup rpmio
|
|
|
|
* \file rpmio/url.c
|
|
|
|
*/
|
|
|
|
|
1999-07-04 07:36:35 +08:00
|
|
|
#include "system.h"
|
|
|
|
|
2010-01-05 18:35:54 +08:00
|
|
|
#include <sys/wait.h>
|
1999-07-04 07:36:35 +08:00
|
|
|
|
2007-12-08 20:02:32 +08:00
|
|
|
#include <rpm/rpmmacro.h>
|
|
|
|
#include <rpm/rpmlog.h>
|
|
|
|
#include <rpm/rpmurl.h>
|
|
|
|
#include <rpm/rpmio.h>
|
2008-04-11 16:18:25 +08:00
|
|
|
#include <rpm/argv.h>
|
|
|
|
#include <rpm/rpmstring.h>
|
1999-07-04 07:36:35 +08:00
|
|
|
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
/**
|
|
|
|
*/
|
1999-10-31 00:43:29 +08:00
|
|
|
static struct urlstring {
|
2008-03-07 01:27:19 +08:00
|
|
|
const char const * leadin;
|
1999-10-31 00:43:29 +08:00
|
|
|
urltype ret;
|
2008-03-07 01:27:19 +08:00
|
|
|
} const urlstrings[] = {
|
1999-10-31 00:43:29 +08:00
|
|
|
{ "file://", URL_IS_PATH },
|
|
|
|
{ "ftp://", URL_IS_FTP },
|
2004-11-29 05:14:41 +08:00
|
|
|
{ "hkp://", URL_IS_HKP },
|
1999-10-31 00:43:29 +08:00
|
|
|
{ "http://", URL_IS_HTTP },
|
2004-11-04 21:29:11 +08:00
|
|
|
{ "https://", URL_IS_HTTPS },
|
1999-10-31 00:43:29 +08:00
|
|
|
{ NULL, URL_IS_UNKNOWN }
|
|
|
|
};
|
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
urltype urlIsURL(const char * url)
|
|
|
|
{
|
2008-03-07 01:27:19 +08:00
|
|
|
const struct urlstring *us;
|
1999-10-31 00:43:29 +08:00
|
|
|
|
1999-11-13 01:20:49 +08:00
|
|
|
if (url && *url) {
|
|
|
|
for (us = urlstrings; us->leadin != NULL; us++) {
|
2009-08-31 16:15:16 +08:00
|
|
|
if (!rstreqn(url, us->leadin, strlen(us->leadin)))
|
1999-11-13 01:20:49 +08:00
|
|
|
continue;
|
|
|
|
return us->ret;
|
|
|
|
}
|
2009-08-31 16:15:16 +08:00
|
|
|
if (rstreq(url, "-"))
|
2009-07-14 16:15:30 +08:00
|
|
|
return URL_IS_DASH;
|
1999-10-31 00:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return URL_IS_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
1999-11-20 02:19:41 +08:00
|
|
|
/* Return path portion of url (or pointer to NUL if url == NULL) */
|
2000-08-28 02:34:02 +08:00
|
|
|
urltype urlPath(const char * url, const char ** pathp)
|
1999-11-13 01:20:49 +08:00
|
|
|
{
|
1999-11-19 02:07:46 +08:00
|
|
|
const char *path;
|
2010-09-21 17:06:24 +08:00
|
|
|
urltype type;
|
1999-11-13 01:20:49 +08:00
|
|
|
|
1999-11-19 02:07:46 +08:00
|
|
|
path = url;
|
2010-09-21 17:06:24 +08:00
|
|
|
type = urlIsURL(url);
|
|
|
|
switch (type) {
|
1999-11-13 01:20:49 +08:00
|
|
|
case URL_IS_FTP:
|
1999-11-24 08:03:54 +08:00
|
|
|
url += sizeof("ftp://") - 1;
|
|
|
|
path = strchr(url, '/');
|
|
|
|
if (path == NULL) path = url + strlen(url);
|
1999-11-13 01:20:49 +08:00
|
|
|
break;
|
|
|
|
case URL_IS_PATH:
|
1999-11-24 08:03:54 +08:00
|
|
|
url += sizeof("file://") - 1;
|
|
|
|
path = strchr(url, '/');
|
|
|
|
if (path == NULL) path = url + strlen(url);
|
1999-11-13 01:20:49 +08:00
|
|
|
break;
|
2004-11-29 05:14:41 +08:00
|
|
|
case URL_IS_HKP:
|
|
|
|
url += sizeof("hkp://") - 1;
|
|
|
|
path = strchr(url, '/');
|
|
|
|
if (path == NULL) path = url + strlen(url);
|
|
|
|
break;
|
2004-11-04 21:29:11 +08:00
|
|
|
case URL_IS_HTTP:
|
|
|
|
url += sizeof("http://") - 1;
|
|
|
|
path = strchr(url, '/');
|
|
|
|
if (path == NULL) path = url + strlen(url);
|
|
|
|
break;
|
|
|
|
case URL_IS_HTTPS:
|
|
|
|
url += sizeof("https://") - 1;
|
|
|
|
path = strchr(url, '/');
|
|
|
|
if (path == NULL) path = url + strlen(url);
|
|
|
|
break;
|
1999-11-13 01:20:49 +08:00
|
|
|
case URL_IS_UNKNOWN:
|
1999-11-24 08:03:54 +08:00
|
|
|
if (path == NULL) path = "";
|
1999-11-13 01:20:49 +08:00
|
|
|
break;
|
|
|
|
case URL_IS_DASH:
|
|
|
|
path = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pathp)
|
|
|
|
*pathp = path;
|
2010-09-21 17:06:24 +08:00
|
|
|
return type;
|
1999-11-13 01:20:49 +08:00
|
|
|
}
|
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
int urlGetFile(const char * url, const char * dest)
|
|
|
|
{
|
2008-04-11 16:18:25 +08:00
|
|
|
char *cmd = NULL;
|
|
|
|
const char *target = NULL;
|
|
|
|
char *urlhelper = NULL;
|
1999-07-04 07:36:35 +08:00
|
|
|
int rc;
|
2008-04-11 16:18:25 +08:00
|
|
|
pid_t pid, wait;
|
1999-07-04 07:36:35 +08:00
|
|
|
|
2008-04-11 16:18:25 +08:00
|
|
|
urlhelper = rpmExpand("%{?_urlhelper}", NULL);
|
1999-07-04 07:36:35 +08:00
|
|
|
|
1999-12-13 01:46:22 +08:00
|
|
|
if (dest == NULL) {
|
2008-04-11 16:18:25 +08:00
|
|
|
urlPath(url, &target);
|
|
|
|
} else {
|
|
|
|
target = dest;
|
1999-12-13 01:46:22 +08:00
|
|
|
}
|
1999-07-04 07:36:35 +08:00
|
|
|
|
2008-04-11 16:18:25 +08:00
|
|
|
/* XXX TODO: sanity checks like target == dest... */
|
1999-07-04 07:36:35 +08:00
|
|
|
|
2010-06-03 21:22:09 +08:00
|
|
|
rasprintf(&cmd, "%s %s %s", urlhelper, target, url);
|
2008-04-11 16:18:25 +08:00
|
|
|
urlhelper = _free(urlhelper);
|
1999-07-04 07:36:35 +08:00
|
|
|
|
2008-04-11 16:18:25 +08:00
|
|
|
if ((pid = fork()) == 0) {
|
|
|
|
ARGV_t argv = NULL;
|
|
|
|
argvSplit(&argv, cmd, " ");
|
|
|
|
execvp(argv[0], argv);
|
2008-11-04 17:09:31 +08:00
|
|
|
exit(127); /* exit with 127 for compatibility with bash(1) */
|
2008-04-11 16:18:25 +08:00
|
|
|
}
|
|
|
|
wait = waitpid(pid, &rc, 0);
|
|
|
|
cmd = _free(cmd);
|
1999-07-04 07:36:35 +08:00
|
|
|
|
|
|
|
return rc;
|
2008-04-11 16:18:25 +08:00
|
|
|
|
1999-07-04 07:36:35 +08:00
|
|
|
}
|