]> git.ozlabs.org Git - petitboot/blob - ui/common/url.c
Fix kexec call
[petitboot] / ui / common / url.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #define _GNU_SOURCE
24 #include <assert.h>
25 #include <string.h>
26
27 #include "log/log.h"
28 #include "talloc/talloc.h"
29 #include "url.h"
30
31 /**
32  * pb_scheme_info - Helper for parsing URLs.
33  */
34
35 struct pb_scheme_info {
36         enum pb_url_scheme scheme;
37         const char *str;
38         unsigned int str_len;
39 };
40
41 /**
42  * pb_url_find_scheme - Find the pb_scheme_info for a URL string.
43  */
44
45 static const struct pb_scheme_info *pb_url_find_scheme(const char *url_str)
46 {
47         static const struct pb_scheme_info a[] = {
48         {
49                 .scheme = pb_url_file,
50                 .str = "file://",
51                 .str_len = sizeof("file://") - 1,
52         },
53         {
54                 .scheme = pb_url_ftp,
55                 .str = "ftp://",
56                 .str_len = sizeof("ftp://") - 1,
57         },
58         {
59                 .scheme = pb_url_http,
60                 .str = "http://",
61                 .str_len = sizeof("http://") - 1,
62         },
63         {
64                 .scheme = pb_url_https,
65                 .str = "https://",
66                 .str_len = sizeof("https://") - 1,
67         },
68         {
69                 .scheme = pb_url_nfs,
70                 .str = "nfs://",
71                 .str_len = sizeof("nfs://") - 1,
72         },
73         {
74                 .scheme = pb_url_sftp,
75                 .str = "sftp://",
76                 .str_len = sizeof("sftp://") - 1,
77         },
78         {
79                 .scheme = pb_url_tftp,
80                 .str = "tftp://",
81                 .str_len = sizeof("tftp://") - 1,
82         },
83         };
84         static const struct pb_scheme_info file_scheme = {
85                 .str = "",
86                 .scheme = pb_url_file,
87         };
88         unsigned int i;
89
90         for (i = 0; i < sizeof(a) / sizeof(a[0]); i++)
91                 if (!strncasecmp(url_str, a[i].str, a[i].str_len))
92                         return &a[i];
93
94         /* Assume this is a non-url local file. */
95
96         return &file_scheme;
97 }
98
99 /**
100  * pb_url_parse - Parse a remote file URL.
101  * @ctx: The talloc context to associate with the returned string.
102  *
103  * Returns a talloc'ed struct pb_url instance on success, or NULL on error.
104  */
105
106 struct pb_url *pb_url_parse(void *ctx, const char *url_str)
107 {
108         const struct pb_scheme_info *si;
109         struct pb_url *url;
110         const char *p;
111
112         pb_log("%s: '%s'\n", __func__, url_str);
113
114         if (!url_str || !*url_str) {
115                 assert(0 && "bad url");
116                 return NULL;
117         }
118
119         url = talloc_zero(ctx, struct pb_url);
120
121         if (!url)
122                 return NULL;
123
124         si = pb_url_find_scheme(url_str);
125
126         url->scheme = si->scheme;
127         p = url_str + si->str_len;
128
129         url->full = talloc_strdup(url, url_str);
130
131         if (url->scheme == pb_url_file) {
132                 url->port = NULL;
133                 url->host = NULL;
134                 url->path = talloc_strdup(url, p);
135         } else {
136                 int len;
137                 const char *col;
138                 const char *path;
139
140                 path = strchr(p, '/');
141
142                 if (!path) {
143                         pb_log("%s: parse path failed '%s'\n", p);
144                         goto fail;
145                 }
146
147                 col = strchr(p, ':');
148
149                 if (col) {
150                         len = path - col - 1;
151                         url->port = len ? talloc_strndup(url, col + 1, len)
152                                 : NULL;
153                         len = col - p;
154                         url->host = len ? talloc_strndup(url, p, len) : NULL;
155                 } else {
156                         url->port = NULL;
157                         url->host = talloc_strndup(url, p, path - p);
158                 }
159                 url->path = talloc_strdup(url, path);
160         }
161
162         p = strrchr(url->path, '/');
163
164         if (p) {
165                 p++;
166                 url->dir = talloc_strndup(url, url->path, p - url->path);
167                 url->file = talloc_strdup(url, p);
168         } else {
169                 url->dir = NULL;
170                 url->file = talloc_strdup(url, url->path);
171         }
172
173         pb_log(" scheme %d\n", url->scheme);
174         pb_log(" host '%s'\n", url->host);
175         pb_log(" port '%s'\n", url->port);
176         pb_log(" path '%s'\n", url->path);
177         pb_log(" dir '%s'\n", url->dir);
178         pb_log(" file '%s'\n", url->file);
179
180         return url;
181
182 fail:
183         talloc_free(url);
184         return NULL;
185 }