]> git.draconx.ca Git - upkg.git/blob - src/uobject/vfs.c
package: Move package search code to the VFS.
[upkg.git] / src / uobject / vfs.c
1 /*
2  *  Functions for handling UObject package search paths.
3  *  Copyright (C) 2009 Nick Bowler
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, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <strings.h>
24
25 #include <uobject/vfs.h>
26 #include <upkg.h>
27 #include <ltdl.h>
28
29 #include "avl.h"
30
31 /* Check if a character is a directory separator. */
32 #ifdef LT_DIRSEP_CHAR
33 #       define IS_DIRSEP(x) ((x) == '/' || (x) == LT_DIRSEP_CHAR)
34 #else
35 #       define IS_DIRSEP(x) ((x) == '/')
36 #endif
37
38 /* Number of times the library has been initialized. */
39 static int initialized;
40
41 /* Local package definitions. */
42 struct local_pkg {
43         char *file, *name;
44 };
45 static struct avl_table *local_tree;
46
47 /* Global package search path. */
48 static char  *search_path;
49 static size_t search_path_sz;
50
51 /* Package file extensions, in decreasing order of precedence. */
52 static const char u_pkg_exts[][5] = { ".u", ".utx", ".uax", ".umx", ".unr" };
53
54 static int localcmp(const void *_a, const void *_b, void *_data)
55 {
56         const struct local_pkg *a = _a, *b = _b;
57         return strcasecmp(a->name, b->name);
58 }
59
60 static const char *pkgname_base(const char *file)
61 {
62         const char *base = NULL;
63         int slash = 1;
64
65         for (size_t i = 0; file[i]; i++) {
66                 if (IS_DIRSEP(file[i])) {
67                         slash = 1;
68                 } else if (slash == 1) {
69                         base = file+i;
70                         slash = 0;
71                 }
72         }
73
74         return base;
75 }
76
77 static size_t pkgname_len(const char *base)
78 {
79         size_t i;
80
81         for (i = 0; base[i]; i++) {
82                 if (IS_DIRSEP(base[i]) || base[i] == '.')
83                         break;
84         }
85
86         return i;
87 }
88
89 const char *u_pkg_vfs_add_local(const char *name, const char *file)
90 {
91         size_t filelen = strlen(file)+1, namelen;
92         struct local_pkg *spec;
93
94         if (!name)
95                 name = pkgname_base(file);
96         if (!name)
97                 return NULL;
98         namelen = pkgname_len(name);
99
100         /* For simplicity, stuff everything in a single allocation. */
101         spec = malloc(sizeof *spec + filelen + namelen + 1);
102         if (!spec) {
103                 return NULL;
104         }
105
106         spec->file = (char *)spec + sizeof *spec;
107         memcpy(spec->file, file, filelen);
108
109         spec->name = (char *)spec + sizeof *spec + filelen;
110         memcpy(spec->name, name, namelen);
111         spec->name[namelen] = 0;
112
113         if (avl_find(local_tree, spec)) {
114                 fprintf(stderr, "%s: attempted to add duplicate local package.\n", __func__);
115                 name = spec->name;
116                 free(spec);
117                 return name; /* "Success"-ish. */
118         }
119
120         if (avl_probe(local_tree, spec) == NULL) {
121                 free(spec);
122                 return NULL;
123         }
124
125         return spec->name;
126 }
127
128 /* Enlarge the search path buffer so that it can store at least need bytes. */
129 static int expand_search_path(size_t need)
130 {
131         size_t want = search_path_sz;
132         if (want == 0) want = 1;
133
134         while (want < need)
135                 want *= 2;
136
137         if (want > search_path_sz) {
138                 char *new = realloc(search_path, want);
139                 if (!new) {
140                         return -1;
141                 }
142
143                 search_path    = new;
144                 search_path_sz = want;
145         }
146
147         return 0;
148 }
149
150 const char *u_pkg_vfs_get_search_path(void)
151 {
152         return search_path ? search_path : "";
153 }
154
155 int u_pkg_vfs_set_search_path(const char *path)
156 {
157         if (expand_search_path(strlen(path)+1) != 0)
158                 return -1;
159         strcpy(search_path, path);
160         return 0;
161 }
162
163 int u_pkg_vfs_add_search_dir(const char *path)
164 {
165         size_t end = search_path ? strlen(search_path) : 0;
166
167         if (end == 0) {
168                 return u_pkg_vfs_set_search_path(path);
169         }
170
171         if (expand_search_path(end + strlen(path) + 2) != 0)
172                 return -1;
173         search_path[end] = LT_PATHSEP_CHAR;
174         strcpy(search_path+end+1, path);
175         return 0;
176 }
177
178 void u_pkg_vfs_del_local(const char *name)
179 {
180         struct local_pkg spec = { .name = (char *)name }, *item;
181
182         item = avl_find(local_tree, &spec);
183         free(item);
184 }
185
186 struct foreach_state {
187         const char *name;
188         struct upkg *f;
189         size_t sz;
190         char buf[];
191 };
192
193 static int foreachfile(const char *filename, void *_st)
194 {
195         struct foreach_state **st = _st, *tmp;
196         size_t need, len;
197         const char *base;
198
199         /* Check if the filename matches the package name. */
200         base = pkgname_base(filename);
201         if (!base || strcasecmp(base, (*st)->name) != 0)
202                 return 0;
203
204         /* Enlarge the state buffer, if necessary. */
205         need = strlen(filename) + sizeof **u_pkg_exts;
206         if ((*st)->sz < need) {
207                 tmp = realloc(*st, sizeof **st + need);
208                 if (!tmp)
209                         return -1;
210                 *st = tmp;
211                 (*st)->sz = need;
212         }
213
214         /* Try each file extension, in order. */
215         len = sprintf((*st)->buf, "%s", filename);
216         for (unsigned i = 0; i < sizeof u_pkg_exts / sizeof *u_pkg_exts; i++) {
217                 strcpy((*st)->buf+len, u_pkg_exts[i]);
218
219                 (*st)->f = upkg_fopen((*st)->buf);
220                 if ((*st)->f != NULL)
221                         return 1;
222         }
223
224         return 0;
225 }
226
227 struct upkg *u_pkg_vfs_open_by_name(const char *name)
228 {
229         struct local_pkg spec = { .name = (char *)name }, *item;
230         struct foreach_state *st;
231         struct upkg *f = NULL;
232
233         if (!initialized)
234                 return NULL;
235
236         item = avl_find(local_tree, &spec);
237         if (item)
238                 return upkg_fopen(item->file);
239
240         st = malloc(sizeof *st + 256);
241         if (!st)
242                 return NULL;
243         *st = (struct foreach_state) { .sz = 256, .name = name };
244
245         if (lt_dlforeachfile(u_pkg_vfs_get_search_path(), foreachfile, &st) > 0)
246                 f = st->f;
247
248         free(st);
249         return f;
250 }
251
252 int u_pkg_vfs_init(void)
253 {
254         if (!initialized) {
255                 local_tree = avl_create(localcmp, NULL, NULL);
256                 if (!local_tree) {
257                         fprintf(stderr, "%s: failed to create local module tree.\n", __func__);
258                         return -1;
259                 }
260         }
261
262         initialized++;
263         return 0;
264 }
265
266 static void local_destroy(void *item, void *data)
267 {
268         free(item);
269 }
270
271 void u_pkg_vfs_exit(void)
272 {
273         if (--initialized == 0)
274                 avl_destroy(local_tree, local_destroy);
275 }