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