9 #include <rpm/rpmmacro.h>
10 #include <rpm/rpmlog.h>
11 #include <rpm/rpmurl.h>
12 #include <rpm/rpmio.h>
14 #include <rpm/rpmstring.h>
20 static struct urlstring {
21 const char const * leadin;
23 } const urlstrings[] = {
24 { "file://", URL_IS_PATH },
25 { "ftp://", URL_IS_FTP },
26 { "hkp://", URL_IS_HKP },
27 { "http://", URL_IS_HTTP },
28 { "https://", URL_IS_HTTPS },
29 { NULL, URL_IS_UNKNOWN }
32 urltype urlIsURL(const char * url)
34 const struct urlstring *us;
37 for (us = urlstrings; us->leadin != NULL; us++) {
38 if (!rstreqn(url, us->leadin, strlen(us->leadin)))
46 return URL_IS_UNKNOWN;
49 /* Return path portion of url (or pointer to NUL if url == NULL) */
50 urltype urlPath(const char * url, const char ** pathp)
56 urltype = urlIsURL(url);
59 url += sizeof("ftp://") - 1;
60 path = strchr(url, '/');
61 if (path == NULL) path = url + strlen(url);
64 url += sizeof("file://") - 1;
65 path = strchr(url, '/');
66 if (path == NULL) path = url + strlen(url);
69 url += sizeof("hkp://") - 1;
70 path = strchr(url, '/');
71 if (path == NULL) path = url + strlen(url);
74 url += sizeof("http://") - 1;
75 path = strchr(url, '/');
76 if (path == NULL) path = url + strlen(url);
79 url += sizeof("https://") - 1;
80 path = strchr(url, '/');
81 if (path == NULL) path = url + strlen(url);
84 if (path == NULL) path = "";
95 int urlGetFile(const char * url, const char * dest)
98 const char *target = NULL;
99 char *urlhelper = NULL;
103 urlhelper = rpmExpand("%{?_urlhelper}", NULL);
106 urlPath(url, &target);
111 /* XXX TODO: sanity checks like target == dest... */
113 rasprintf(&cmd, "%s %s %s\n", urlhelper, target, url);
114 urlhelper = _free(urlhelper);
116 if ((pid = fork()) == 0) {
118 argvSplit(&argv, cmd, " ");
119 execvp(argv[0], argv);
120 exit(127); /* exit with 127 for compatibility with bash(1) */
122 wait = waitpid(pid, &rc, 0);