2 * Functions for handling UObject package search paths.
3 * Copyright (C) 2009 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) == '/')
42 static struct avl_table *local_tree;
43 static int initialized;
45 static int localcmp(const void *_a, const void *_b, void *_data)
47 const struct local_pkg *a = _a, *b = _b;
48 return strcasecmp(a->name, b->name);
51 static const char *pkgname_base(const char *file)
53 const char *base = NULL;
56 for (size_t i = 0; file[i]; i++) {
57 if (IS_DIRSEP(file[i])) {
59 } else if (slash == 1) {
68 static size_t pkgname_len(const char *base)
72 for (i = 0; base[i]; i++) {
73 if (IS_DIRSEP(base[i]) || base[i] == '.')
80 const char *u_pkg_vfs_add_local(const char *name, const char *file)
82 size_t filelen = strlen(file)+1, namelen;
83 struct local_pkg *spec;
86 name = pkgname_base(file);
89 namelen = pkgname_len(name);
91 /* For simplicity, stuff everything in a single allocation. */
92 spec = malloc(sizeof *spec + filelen + namelen + 1);
97 spec->file = (char *)spec + sizeof *spec;
98 memcpy(spec->file, file, filelen);
100 spec->name = (char *)spec + sizeof *spec + filelen;
101 memcpy(spec->name, name, namelen);
102 spec->name[namelen] = 0;
104 if (avl_find(local_tree, spec)) {
105 fprintf(stderr, "%s: attempted to add duplicate local package.\n", __func__);
108 return name; /* "Success"-ish. */
111 if (avl_probe(local_tree, spec) == NULL) {
119 void u_pkg_vfs_del_local(const char *name)
121 struct local_pkg spec = { .name = (char *)name }, *item;
123 item = avl_find(local_tree, &spec);
127 struct upkg *u_pkg_vfs_open_by_name(const char *name)
129 struct local_pkg spec = { .name = (char *)name }, *item;
134 item = avl_find(local_tree, &spec);
136 return upkg_fopen(item->file);
140 int u_pkg_vfs_init(void)
143 local_tree = avl_create(localcmp, NULL, NULL);
145 fprintf(stderr, "%s: failed to create local module tree.\n", __func__);
154 static void local_destroy(void *item, void *data)
159 void u_pkg_vfs_exit(void)
161 if (--initialized == 0)
162 avl_destroy(local_tree, local_destroy);