2 * Functions for handling UObject package search paths.
3 * Copyright © 2009-2011 Nick Bowler
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.
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.
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/>.
25 #include <uobject/vfs.h>
31 /* Check if a character is a directory separator. */
33 # define IS_DIRSEP(x) ((x) == '/' || (x) == LT_DIRSEP_CHAR)
35 # define IS_DIRSEP(x) ((x) == '/')
38 /* Number of times the library has been initialized. */
39 static int initialized;
41 /* Local package definitions. */
45 static struct avl_table *local_tree;
47 /* Global package search path. */
48 static char *search_path;
49 static size_t search_path_sz;
51 /* Package file extensions, in decreasing order of precedence. */
52 static const char u_pkg_exts[][5] = { ".u", ".utx", ".uax", ".umx", ".unr" };
54 static int localcmp(const void *_a, const void *_b, void *_data)
56 const struct local_pkg *a = _a, *b = _b;
57 return strcasecmp(a->name, b->name);
60 static const char *pkgname_base(const char *file)
62 const char *base = NULL;
65 for (size_t i = 0; file[i]; i++) {
66 if (IS_DIRSEP(file[i])) {
68 } else if (slash == 1) {
77 static size_t pkgname_len(const char *base)
81 for (i = 0; base[i]; i++) {
82 if (IS_DIRSEP(base[i]) || base[i] == '.')
89 const char *u_pkg_vfs_add_local(const char *name, const char *file)
91 size_t filelen = strlen(file)+1, namelen;
92 struct local_pkg *spec;
95 name = pkgname_base(file);
98 namelen = pkgname_len(name);
100 /* For simplicity, stuff everything in a single allocation. */
101 spec = malloc(sizeof *spec + filelen + namelen + 1);
106 spec->file = (char *)spec + sizeof *spec;
107 memcpy(spec->file, file, filelen);
109 spec->name = (char *)spec + sizeof *spec + filelen;
110 memcpy(spec->name, name, namelen);
111 spec->name[namelen] = 0;
113 if (avl_find(local_tree, spec)) {
114 fprintf(stderr, "%s: attempted to add duplicate local package.\n", __func__);
117 return name; /* "Success"-ish. */
120 if (avl_probe(local_tree, spec) == NULL) {
128 /* Enlarge the search path buffer so that it can store at least need bytes. */
129 static int expand_search_path(size_t need)
131 size_t want = search_path_sz;
132 if (want == 0) want = 1;
137 if (want > search_path_sz) {
138 char *new = realloc(search_path, want);
144 search_path_sz = want;
150 const char *u_pkg_vfs_get_search_path(void)
152 return search_path ? search_path : "";
155 int u_pkg_vfs_set_search_path(const char *path)
157 if (expand_search_path(strlen(path)+1) != 0)
159 strcpy(search_path, path);
163 int u_pkg_vfs_add_search_dir(const char *path)
165 size_t end = search_path ? strlen(search_path) : 0;
168 return u_pkg_vfs_set_search_path(path);
171 if (expand_search_path(end + strlen(path) + 2) != 0)
173 search_path[end] = LT_PATHSEP_CHAR;
174 strcpy(search_path+end+1, path);
178 void u_pkg_vfs_del_local(const char *name)
180 struct local_pkg spec = { .name = (char *)name }, *item;
182 item = avl_find(local_tree, &spec);
186 struct foreach_state {
193 static int foreachfile(const char *filename, void *_st)
195 struct foreach_state **st = _st, *tmp;
199 /* Check if the filename matches the package name. */
200 base = pkgname_base(filename);
201 if (!base || strcasecmp(base, (*st)->name) != 0)
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);
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]);
219 (*st)->f = upkg_fopen((*st)->buf);
220 if ((*st)->f != NULL)
227 struct upkg *u_pkg_vfs_open_by_name(const char *name)
229 struct local_pkg spec = { .name = (char *)name }, *item;
230 struct foreach_state *st;
231 struct upkg *f = NULL;
236 item = avl_find(local_tree, &spec);
238 return upkg_fopen(item->file);
240 st = malloc(sizeof *st + 256);
243 *st = (struct foreach_state) { .sz = 256, .name = name };
245 if (lt_dlforeachfile(u_pkg_vfs_get_search_path(), foreachfile, &st) > 0)
252 int u_pkg_vfs_init(void)
255 local_tree = avl_create(localcmp, NULL, NULL);
257 fprintf(stderr, "%s: failed to create local module tree.\n", __func__);
266 static void local_destroy(void *item, void *data)
271 void u_pkg_vfs_exit(void)
273 if (--initialized == 0)
274 avl_destroy(local_tree, local_destroy);