]> git.draconx.ca Git - upkg.git/blob - src/uobject/vfs.c
package: Get rid of separate mechanisms for opening packages.
[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 <ltdl.h>
27
28 #include "avl.h"
29
30 /* Check if a character is a directory separator. */
31 #ifdef LT_DIRSEP_CHAR
32 #       define IS_DIRSEP(x) ((x) == '/' || (x) == LT_DIRSEP_CHAR)
33 #else
34 #       define IS_DIRSEP(x) ((x) == '/')
35 #endif
36
37 struct local_pkg {
38         char *file, *name;
39 };
40
41 static struct avl_table *local_tree;
42 static int initialized;
43
44 static int localcmp(const void *_a, const void *_b, void *_data)
45 {
46         const struct local_pkg *a = _a, *b = _b;
47         return strcasecmp(a->name, b->name);
48 }
49
50 static const char *pkgname_base(const char *file)
51 {
52         const char *base = NULL;
53         int slash = 1;
54
55         for (size_t i = 0; file[i]; i++) {
56                 if (IS_DIRSEP(file[i])) {
57                         slash = 1;
58                 } else if (slash == 1) {
59                         base = file+i;
60                         slash = 0;
61                 }
62         }
63
64         return base;
65 }
66
67 static size_t pkgname_len(const char *base)
68 {
69         size_t i;
70
71         for (i = 0; base[i]; i++) {
72                 if (IS_DIRSEP(base[i]) || base[i] == '.')
73                         break;
74         }
75
76         return i;
77 }
78
79 const char *u_pkg_vfs_add_local(const char *name, const char *file)
80 {
81         size_t filelen = strlen(file)+1, namelen;
82         struct local_pkg *spec;
83
84         if (!name)
85                 name = pkgname_base(file);
86         if (!name)
87                 return NULL;
88         namelen = pkgname_len(name);
89
90         /* For simplicity, stuff everything in a single allocation. */
91         spec = malloc(sizeof *spec + filelen + namelen + 1);
92         if (!spec) {
93                 return NULL;
94         }
95
96         spec->file = (char *)spec + sizeof *spec;
97         memcpy(spec->file, file, filelen);
98
99         spec->name = (char *)spec + sizeof *spec + filelen;
100         memcpy(spec->name, name, namelen);
101         spec->name[namelen] = 0;
102
103         if (avl_find(local_tree, spec)) {
104                 fprintf(stderr, "%s: attempted to add duplicate local package.\n", __func__);
105                 name = spec->name;
106                 free(spec);
107                 return name; /* "Success"-ish. */
108         }
109
110         if (avl_probe(local_tree, spec) == NULL) {
111                 free(spec);
112                 return NULL;
113         }
114
115         return spec->name;
116 }
117
118 void u_pkg_vfs_del_local(const char *name)
119 {
120         struct local_pkg spec = { .name = (char *)name }, *item;
121
122         item = avl_find(local_tree, &spec);
123         free(item);
124 }
125
126 const char *u_pkg_vfs_lookup(const char *name)
127 {
128         struct local_pkg spec = { .name = (char *)name }, *item;
129
130         if (!local_tree)
131                 return NULL;
132
133         item = avl_find(local_tree, &spec);
134         if (!item)
135                 return NULL;
136         return item->file;
137 }
138
139 int u_pkg_vfs_init(void)
140 {
141         if (!initialized) {
142                 local_tree = avl_create(localcmp, NULL, NULL);
143                 if (!local_tree) {
144                         fprintf(stderr, "%s: failed to create local module tree.\n", __func__);
145                         return -1;
146                 }
147         }
148
149         initialized++;
150         return 0;
151 }
152
153 static void local_destroy(void *item, void *data)
154 {
155         free(item);
156 }
157
158 void u_pkg_vfs_exit(void)
159 {
160         if (--initialized == 0)
161                 avl_destroy(local_tree, local_destroy);
162 }