Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / pkg_install / dist / lib / pkg_io.c
blobcf8e1329b8d48601b44bf771d1aae3d637bcb40f
1 /* $NetBSD: pkg_io.c,v 1.9 2009/08/16 21:10:15 joerg Exp $ */
2 /*-
3 * Copyright (c) 2008, 2009 Joerg Sonnenberger <joerg@NetBSD.org>.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
31 #if HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 #include <nbcompat.h>
35 #if HAVE_SYS_CDEFS_H
36 #include <sys/cdefs.h>
37 #endif
39 __RCSID("$NetBSD: pkg_io.c,v 1.9 2009/08/16 21:10:15 joerg Exp $");
41 #include <archive.h>
42 #include <archive_entry.h>
43 #if HAVE_ERR_H
44 #include <err.h>
45 #endif
46 #if HAVE_ERRNO_H
47 #include <errno.h>
48 #endif
49 #include <fetch.h>
50 #include <stdlib.h>
52 #include "lib.h"
54 struct pkg_path {
55 TAILQ_ENTRY(pkg_path) pl_link;
56 char *pl_path;
59 static char *orig_cwd, *last_toplevel;
60 static TAILQ_HEAD(, pkg_path) pkg_path = TAILQ_HEAD_INITIALIZER(pkg_path);
62 struct fetch_archive {
63 struct url *url;
64 fetchIO *fetch;
65 char buffer[32768];
68 static int
69 fetch_archive_open(struct archive *a, void *client_data)
71 struct fetch_archive *f = client_data;
73 f->fetch = fetchGet(f->url, fetch_flags);
74 if (f->fetch == NULL)
75 return ENOENT;
76 return 0;
79 static ssize_t
80 fetch_archive_read(struct archive *a, void *client_data,
81 const void **buffer)
83 struct fetch_archive *f = client_data;
85 *buffer = f->buffer;
86 return fetchIO_read(f->fetch, f->buffer, sizeof(f->buffer));
89 static int
90 fetch_archive_close(struct archive *a, void *client_data)
92 struct fetch_archive *f = client_data;
94 if (f->fetch != NULL)
95 fetchIO_close(f->fetch);
96 free(f);
97 return 0;
100 static struct archive *
101 open_archive_by_url(struct url *url)
103 struct fetch_archive *f;
104 struct archive *a;
106 f = xmalloc(sizeof(*f));
107 f->url = url;
109 a = archive_read_new();
110 archive_read_support_compression_all(a);
111 archive_read_support_format_all(a);
112 if (archive_read_open(a, f, fetch_archive_open, fetch_archive_read,
113 fetch_archive_close)) {
114 archive_read_finish(a);
115 return NULL;
118 return a;
121 struct archive *
122 open_archive(const char *url)
124 struct url *u;
125 struct archive *a;
127 if (!IS_URL(url)) {
128 a = archive_read_new();
129 archive_read_support_compression_all(a);
130 archive_read_support_format_all(a);
131 if (archive_read_open_filename(a, url, 1024)) {
132 archive_read_close(a);
133 return NULL;
135 return a;
138 if ((u = fetchParseURL(url)) == NULL)
139 return NULL;
141 a = open_archive_by_url(u);
143 fetchFreeURL(u);
144 return a;
147 static int
148 strip_suffix(char *filename)
150 size_t len;
152 len = strlen(filename);
153 if (len <= 4)
154 return 0;
155 if (strcmp(filename + len - 4, ".tgz") == 0 ||
156 strcmp(filename + len - 4, ".tbz") == 0) {
157 filename[len - 4] = '\0';
158 return 1;
159 } else
160 return 0;
163 static int
164 find_best_package_int(struct url *url, const char *pattern,
165 struct url **best_url)
167 char *cur_match, *url_pattern, *best_match = NULL;
168 struct url_list ue;
169 size_t i;
171 if (*best_url) {
172 if ((best_match = fetchUnquoteFilename(*best_url)) == NULL)
173 return -1;
174 } else
175 best_match = NULL;
177 if (best_match && strip_suffix(best_match) == 0) {
178 free(best_match);
179 return -1;
182 for (i = 0; pattern[i] != '\0'; ++i) {
183 if (!isalnum((unsigned char)(pattern[i])) &&
184 (pattern[i]) != '-')
185 break;
187 url_pattern = xasprintf("%*.*s*", (int)i, (int)i, pattern);
189 fetchInitURLList(&ue);
190 if (fetchList(&ue, url, url_pattern, fetch_flags)) {
191 char *base_url;
192 base_url = fetchStringifyURL(url);
193 warnx("Can't process %s/%s: %s", base_url, url_pattern,
194 fetchLastErrString);
195 free(base_url);
196 free(url_pattern);
197 fetchFreeURLList(&ue);
198 return -1;
200 free(url_pattern);
202 for (i = 0; i < ue.length; ++i) {
203 cur_match = fetchUnquoteFilename(ue.urls + i);
205 if (cur_match == NULL) {
206 free(best_match);
207 fetchFreeURLList(&ue);
208 return -1;
210 if (strip_suffix(cur_match) == 0) {
211 free(cur_match);
212 continue;
214 if (pkg_order(pattern, cur_match, best_match) == 1) {
215 if (*best_url)
216 fetchFreeURL(*best_url);
217 *best_url = fetchCopyURL(ue.urls + i);
218 free(best_match);
219 best_match = cur_match;
220 cur_match = NULL;
221 if (*best_url == NULL) {
222 free(best_match);
223 return -1;
226 free(cur_match);
228 free(best_match);
229 fetchFreeURLList(&ue);
230 return 0;
233 void
234 process_pkg_path(void)
236 char cwd[PATH_MAX];
237 int relative_path;
238 struct pkg_path *pl;
239 const char *start, *next;
240 size_t len;
242 if (getcwd(cwd, sizeof(cwd)) == NULL)
243 errx(EXIT_FAILURE, "getcwd failed");
245 orig_cwd = xstrdup(cwd);
247 if (config_pkg_path == NULL)
248 return;
250 for (start = config_pkg_path; *start; start = next) {
251 len = strcspn(start, ";");
252 if (*(next = start + len) != '\0')
253 ++next;
255 relative_path = !IS_FULLPATH(start) && !IS_URL(start);
256 pl = xmalloc(sizeof(*pl));
257 pl->pl_path = xasprintf("%s%s%*.*s",
258 relative_path ? cwd : "", len && relative_path ? "/" : "",
259 (int)len, (int)len, start);
260 TAILQ_INSERT_TAIL(&pkg_path, pl, pl_link);
264 struct url *
265 find_best_package(const char *toplevel, const char *pattern, int do_path)
267 struct url *url, *best_match = NULL;
268 struct pkg_path *pl;
270 if (toplevel) {
271 url = fetchParseURL(last_toplevel);
272 if (url != NULL) {
273 find_best_package_int(url, pattern, &best_match);
274 /* XXX Check return value and complain */
275 fetchFreeURL(url);
278 if (!do_path)
279 return best_match;
281 TAILQ_FOREACH(pl, &pkg_path, pl_link) {
282 url = fetchParseURL(pl->pl_path);
283 if (url != NULL) {
284 find_best_package_int(url, pattern, &best_match);
285 /* XXX Check return value and complain */
286 fetchFreeURL(url);
290 return best_match;
293 struct archive *
294 find_archive(const char *fname, int top_level)
296 struct archive *a;
297 struct url *best_match;
298 char *full_fname, *last_slash;
299 int search_path;
301 search_path = 0;
302 if (IS_FULLPATH(fname) || IS_URL(fname)) {
303 full_fname = xstrdup(fname);
304 } else {
305 if (strchr(fname, '/') == NULL)
306 search_path = 1;
307 full_fname = xasprintf("%s/%s", orig_cwd, fname);
310 last_slash = strrchr(full_fname, '/');
311 if (top_level) {
312 free(last_toplevel);
313 *last_slash = '\0';
314 last_toplevel = xstrdup(full_fname);
315 *last_slash = '/';
318 a = open_archive(full_fname);
319 if (a != NULL) {
320 free(full_fname);
321 return a;
324 fname = last_slash + 1;
325 *last_slash = '\0';
327 best_match = find_best_package(full_fname, fname, 0);
329 if (search_path && best_match == NULL)
330 best_match = find_best_package(last_toplevel, fname, 1);
332 free(full_fname);
334 if (best_match == NULL)
335 return NULL;
336 a = open_archive_by_url(best_match);
337 fetchFreeURL(best_match);
338 return a;