virtdir.c revision 1.3 1 1.1 agc /*
2 1.1 agc * Copyright 2007 Alistair Crooks. All rights reserved.
3 1.1 agc *
4 1.1 agc * Redistribution and use in source and binary forms, with or without
5 1.1 agc * modification, are permitted provided that the following conditions
6 1.1 agc * are met:
7 1.1 agc * 1. Redistributions of source code must retain the above copyright
8 1.1 agc * notice, this list of conditions and the following disclaimer.
9 1.1 agc * 2. Redistributions in binary form must reproduce the above copyright
10 1.1 agc * notice, this list of conditions and the following disclaimer in the
11 1.1 agc * documentation and/or other materials provided with the distribution.
12 1.1 agc * 3. The name of the author may not be used to endorse or promote
13 1.1 agc * products derived from this software without specific prior written
14 1.1 agc * permission.
15 1.1 agc *
16 1.1 agc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 1.1 agc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.1 agc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 1.1 agc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 1.1 agc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 agc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 1.1 agc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 1.1 agc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 1.1 agc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 agc */
28 1.1 agc #include <sys/types.h>
29 1.1 agc #include <sys/stat.h>
30 1.1 agc
31 1.1 agc #include <fuse.h>
32 1.1 agc #include <stdio.h>
33 1.1 agc #include <stdlib.h>
34 1.1 agc #include <unistd.h>
35 1.1 agc
36 1.1 agc #include "virtdir.h"
37 1.1 agc #include "defs.h"
38 1.1 agc
39 1.1 agc /* utility comparison routine for sorting and searching */
40 1.1 agc static int
41 1.1 agc compare(const void *vp1, const void *vp2)
42 1.1 agc {
43 1.1 agc const virt_dirent_t *tp1 = (const virt_dirent_t *) vp1;
44 1.1 agc const virt_dirent_t *tp2 = (const virt_dirent_t *) vp2;
45 1.1 agc
46 1.1 agc return strcmp(tp1->name, tp2->name);
47 1.1 agc }
48 1.1 agc
49 1.1 agc /* save `n' chars of `s' in allocated storage */
50 1.1 agc static char *
51 1.1 agc strnsave(const char *s, int n)
52 1.1 agc {
53 1.1 agc char *cp;
54 1.1 agc
55 1.1 agc if (n < 0) {
56 1.1 agc n = strlen(s);
57 1.1 agc }
58 1.1 agc NEWARRAY(char, cp, n + 1, "strnsave", return NULL);
59 1.1 agc (void) memcpy(cp, s, n);
60 1.1 agc cp[n] = 0x0;
61 1.1 agc return cp;
62 1.1 agc }
63 1.1 agc
64 1.3 agc /* ensure intermediate directories exist */
65 1.3 agc static void
66 1.3 agc mkdirs(virtdir_t *tp, const char *path, size_t size)
67 1.3 agc {
68 1.3 agc virt_dirent_t *ep;
69 1.3 agc char name[MAXPATHLEN];
70 1.3 agc char *slash;
71 1.3 agc
72 1.3 agc (void) strlcpy(name, path, sizeof(name));
73 1.3 agc for (slash = name + 1 ; (slash = strchr(slash + 1, '/')) != NULL ; ) {
74 1.3 agc *slash = 0x0;
75 1.3 agc if ((ep = virtdir_find(tp, name, strlen(name))) == NULL) {
76 1.3 agc virtdir_add(tp, name, strlen(name), 'd', NULL);
77 1.3 agc }
78 1.3 agc *slash = '/';
79 1.3 agc }
80 1.3 agc }
81 1.3 agc
82 1.1 agc /* initialise the tree */
83 1.1 agc int
84 1.1 agc virtdir_init(virtdir_t *tp, struct stat *d, struct stat *f, struct stat *l)
85 1.1 agc {
86 1.1 agc (void) memcpy(&tp->dir, d, sizeof(tp->dir));
87 1.1 agc tp->dir.st_mode = S_IFDIR | 0755;
88 1.1 agc tp->dir.st_nlink = 2;
89 1.1 agc (void) memcpy(&tp->file, f, sizeof(tp->file));
90 1.1 agc tp->file.st_mode = S_IFREG | 0644;
91 1.1 agc tp->file.st_nlink = 1;
92 1.1 agc (void) memcpy(&tp->lnk, l, sizeof(tp->lnk));
93 1.1 agc tp->lnk.st_mode = S_IFLNK | 0644;
94 1.1 agc tp->lnk.st_nlink = 1;
95 1.1 agc return 1;
96 1.1 agc }
97 1.1 agc
98 1.1 agc /* add an entry to the tree */
99 1.1 agc int
100 1.1 agc virtdir_add(virtdir_t *tp, const char *name, size_t size, uint8_t type, char *tgt)
101 1.1 agc {
102 1.1 agc struct stat st;
103 1.1 agc
104 1.1 agc if (tp->v == NULL) {
105 1.1 agc (void) stat(".", &st);
106 1.1 agc virtdir_init(tp, &st, &st, &st);
107 1.1 agc }
108 1.2 agc if (virtdir_find(tp, name, size) != NULL) {
109 1.2 agc /* attempt to add a duplicate directory entry */
110 1.2 agc return 0;
111 1.2 agc }
112 1.1 agc ALLOC(virt_dirent_t, tp->v, tp->size, tp->c, 10, 10, "virtdir_add",
113 1.1 agc return 0);
114 1.1 agc tp->v[tp->c].namelen = size;
115 1.1 agc if ((tp->v[tp->c].name = strnsave(name, size)) == NULL) {
116 1.1 agc return 0;
117 1.1 agc }
118 1.1 agc tp->v[tp->c].d_name = strrchr(tp->v[tp->c].name, '/') + 1;
119 1.1 agc tp->v[tp->c].type = type;
120 1.1 agc if (tgt != NULL) {
121 1.1 agc tp->v[tp->c].tgt = strdup(tgt);
122 1.1 agc tp->v[tp->c].tgtlen = strlen(tgt);
123 1.1 agc }
124 1.1 agc tp->c += 1;
125 1.1 agc qsort(tp->v, tp->c, sizeof(tp->v[0]), compare);
126 1.3 agc mkdirs(tp, name, size);
127 1.1 agc return 1;
128 1.1 agc }
129 1.1 agc
130 1.2 agc /* delete an entry from the tree */
131 1.1 agc int
132 1.1 agc virtdir_del(virtdir_t *tp, const char *name, size_t size)
133 1.1 agc {
134 1.1 agc virt_dirent_t *ep;
135 1.1 agc int i;
136 1.1 agc
137 1.1 agc if ((ep = virtdir_find(tp, name, size)) == NULL) {
138 1.1 agc return 0;
139 1.1 agc }
140 1.1 agc i = (int)(ep - tp->v) / sizeof(tp->v[0]);
141 1.1 agc for (tp->c -= 1 ; i < tp->c ; i++) {
142 1.1 agc tp->v[i] = tp->v[i + 1];
143 1.1 agc }
144 1.1 agc return 1;
145 1.1 agc }
146 1.1 agc
147 1.1 agc /* find an entry in the tree */
148 1.1 agc virt_dirent_t *
149 1.1 agc virtdir_find(virtdir_t *tp, const char *name, size_t namelen)
150 1.1 agc {
151 1.1 agc virt_dirent_t e;
152 1.1 agc
153 1.1 agc (void) memset(&e, 0x0, sizeof(e));
154 1.1 agc e.name = __UNCONST(name);
155 1.1 agc e.namelen = namelen;
156 1.1 agc return bsearch(&e, tp->v, tp->c, sizeof(tp->v[0]), compare);
157 1.1 agc }
158 1.1 agc
159 1.1 agc /* analogous to opendir(3) - open a directory, save information, and
160 1.1 agc * return a pointer to the dynamically allocated structure */
161 1.1 agc VIRTDIR *
162 1.1 agc openvirtdir(virtdir_t *tp, const char *d)
163 1.1 agc {
164 1.1 agc VIRTDIR *dirp;
165 1.1 agc
166 1.1 agc NEW(VIRTDIR, dirp, "openvirtdir", exit(EXIT_FAILURE));
167 1.1 agc dirp->dirname = strdup(d);
168 1.1 agc dirp->dirnamelen = strlen(d);
169 1.1 agc dirp->tp = tp;
170 1.1 agc dirp->i = 0;
171 1.1 agc return dirp;
172 1.1 agc }
173 1.1 agc
174 1.1 agc /* analogous to readdir(3) - read the next entry in the directory that
175 1.1 agc * was opened, and return a pointer to it */
176 1.1 agc virt_dirent_t *
177 1.1 agc readvirtdir(VIRTDIR *dirp)
178 1.1 agc {
179 1.1 agc char *from;
180 1.1 agc
181 1.1 agc for ( ; dirp->i < dirp->tp->c ; dirp->i++) {
182 1.1 agc from = (strcmp(dirp->dirname, "/") == 0) ?
183 1.1 agc &dirp->tp->v[dirp->i].name[1] :
184 1.1 agc &dirp->tp->v[dirp->i].name[dirp->dirnamelen + 1];
185 1.1 agc if (strncmp(dirp->tp->v[dirp->i].name, dirp->dirname,
186 1.1 agc dirp->dirnamelen) == 0 &&
187 1.3 agc *from != 0x0 &&
188 1.1 agc strchr(from, '/') == NULL) {
189 1.1 agc return &dirp->tp->v[dirp->i++];
190 1.1 agc }
191 1.1 agc }
192 1.1 agc return NULL;
193 1.1 agc }
194 1.1 agc
195 1.1 agc /* free the storage associated with the virtual directory structure */
196 1.1 agc void
197 1.1 agc closevirtdir(VIRTDIR *dirp)
198 1.1 agc {
199 1.1 agc free(dirp->dirname);
200 1.1 agc FREE(dirp);
201 1.1 agc }
202 1.1 agc
203 1.1 agc /* find a target in the tree -- not quick! */
204 1.1 agc virt_dirent_t *
205 1.1 agc virtdir_find_tgt(virtdir_t *tp, const char *tgt, size_t tgtlen)
206 1.1 agc {
207 1.1 agc /* we don't need no stinking binary searches */
208 1.1 agc int i;
209 1.1 agc
210 1.1 agc for (i = 0 ; i < tp->c ; i++) {
211 1.1 agc if (tp->v[i].tgt && strcmp(tp->v[i].tgt, tgt) == 0) {
212 1.1 agc return &tp->v[i];
213 1.1 agc }
214 1.1 agc }
215 1.1 agc return NULL;
216 1.1 agc }
217 1.1 agc
218 1.1 agc /* kill all of the space allocated to the tree */
219 1.1 agc void
220 1.1 agc virtdir_drop(virtdir_t *tp)
221 1.1 agc {
222 1.1 agc int i;
223 1.1 agc
224 1.1 agc for (i = 0 ; i < tp->c ; i++) {
225 1.1 agc FREE(tp->v[i].name);
226 1.1 agc if (tp->v[i].tgt) {
227 1.1 agc FREE(tp->v[i].tgt);
228 1.1 agc }
229 1.1 agc }
230 1.1 agc FREE(tp->v);
231 1.1 agc }
232 1.1 agc
233 1.1 agc #ifdef VIRTDIR_DEBUG
234 1.1 agc static void
235 1.1 agc ptree(virtdir_t * tp)
236 1.1 agc {
237 1.1 agc int i;
238 1.1 agc
239 1.1 agc for (i = 0 ; i < tp->c ; i++) {
240 1.1 agc printf("%s, tgt %s\n", tp->v[i].name, tp->v[i].tgt);
241 1.1 agc }
242 1.1 agc }
243 1.1 agc #endif
244 1.1 agc
245 1.1 agc #ifdef VIRTDIR_DEBUG
246 1.1 agc int
247 1.1 agc main(int argc, char **argv)
248 1.1 agc {
249 1.1 agc virt_dirent_t *tp;
250 1.1 agc virtdir_t t;
251 1.1 agc struct stat st;
252 1.1 agc
253 1.1 agc (void) memset(&t, 0x0, sizeof(t));
254 1.1 agc stat(".", &st);
255 1.1 agc virtdir_add(&t, ".", 1, 'd', NULL);
256 1.1 agc stat("..", &st);
257 1.1 agc virtdir_add(&t, "..", 2, 'd', NULL);
258 1.1 agc st.st_mode = S_IFREG | 0644;
259 1.1 agc virtdir_add(&t, "file1", 5, 'f', NULL);
260 1.1 agc ptree(&t);
261 1.1 agc virtdir_add(&t, "file2", 5, 'f', NULL);
262 1.1 agc virtdir_add(&t, "file0", 5, 'f', NULL);
263 1.1 agc virtdir_add(&t, "abcde", 5, 'f', NULL);
264 1.1 agc virtdir_add(&t, "bcdef", 5, 'f', NULL);
265 1.1 agc virtdir_add(&t, "a", 1, 'f', NULL);
266 1.1 agc ptree(&t);
267 1.1 agc if ((tp = virtdir_find(&t, "a", 1)) == NULL) {
268 1.1 agc printf("borked2\n");
269 1.1 agc } else {
270 1.1 agc printf("a found\n");
271 1.1 agc }
272 1.1 agc virtdir_drop(&t);
273 1.1 agc exit(EXIT_SUCCESS);
274 1.1 agc }
275 1.1 agc #endif
276