Axe the rpmsq debug code which was never getting built anyway
[platform/upstream/rpm.git] / rpmio / url.c
1 /** \ingroup rpmio
2  * \file rpmio/url.c
3  */
4
5 #include "system.h"
6
7 #include <sys/wait.h>
8
9 #include <rpm/rpmmacro.h>
10 #include <rpm/rpmlog.h>
11 #include <rpm/rpmurl.h>
12 #include <rpm/rpmio.h>
13 #include <rpm/argv.h>
14 #include <rpm/rpmstring.h>
15
16 #include "debug.h"
17
18 /**
19  */
20 static struct urlstring {
21     const char * leadin;
22     urltype     ret;
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 }
30 };
31
32 urltype urlIsURL(const char * url)
33 {
34     const struct urlstring *us;
35
36     if (url && *url) {
37         for (us = urlstrings; us->leadin != NULL; us++) {
38             if (!rstreqn(url, us->leadin, strlen(us->leadin)))
39                 continue;
40             return us->ret;
41         }
42         if (rstreq(url, "-")) 
43             return URL_IS_DASH;
44     }
45
46     return URL_IS_UNKNOWN;
47 }
48
49 /* Return path portion of url (or pointer to NUL if url == NULL) */
50 urltype urlPath(const char * url, const char ** pathp)
51 {
52     const char *path;
53     urltype type;
54
55     path = url;
56     type = urlIsURL(url);
57     switch (type) {
58     case URL_IS_FTP:
59         url += sizeof("ftp://") - 1;
60         path = strchr(url, '/');
61         if (path == NULL) path = url + strlen(url);
62         break;
63     case URL_IS_PATH:
64         url += sizeof("file://") - 1;
65         path = strchr(url, '/');
66         if (path == NULL) path = url + strlen(url);
67         break;
68     case URL_IS_HKP:
69         url += sizeof("hkp://") - 1;
70         path = strchr(url, '/');
71         if (path == NULL) path = url + strlen(url);
72         break;
73     case URL_IS_HTTP:
74         url += sizeof("http://") - 1;
75         path = strchr(url, '/');
76         if (path == NULL) path = url + strlen(url);
77         break;
78     case URL_IS_HTTPS:
79         url += sizeof("https://") - 1;
80         path = strchr(url, '/');
81         if (path == NULL) path = url + strlen(url);
82         break;
83     case URL_IS_UNKNOWN:
84         if (path == NULL) path = "";
85         break;
86     case URL_IS_DASH:
87         path = "";
88         break;
89     }
90     if (pathp)
91         *pathp = path;
92     return type;
93 }
94
95 int urlGetFile(const char * url, const char * dest)
96 {
97     char *cmd = NULL;
98     const char *target = NULL;
99     char *urlhelper = NULL;
100     int rc;
101     pid_t pid, wait;
102
103     urlhelper = rpmExpand("%{?_urlhelper}", NULL);
104
105     if (dest == NULL) {
106         urlPath(url, &target);
107     } else {
108         target = dest;
109     }
110
111     /* XXX TODO: sanity checks like target == dest... */
112
113     rasprintf(&cmd, "%s %s %s", urlhelper, target, url);
114     urlhelper = _free(urlhelper);
115
116     if ((pid = fork()) == 0) {
117         ARGV_t argv = NULL;
118         argvSplit(&argv, cmd, " ");
119         execvp(argv[0], argv);
120         exit(127); /* exit with 127 for compatibility with bash(1) */
121     }
122     wait = waitpid(pid, &rc, 0);
123     cmd = _free(cmd);
124
125     return rc;
126
127 }