virtdir.c revision 1.1 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.1 agc /* initialise the tree */
65 1.1 agc int
66 1.1 agc virtdir_init(virtdir_t *tp, struct stat *d, struct stat *f, struct stat *l)
67 1.1 agc {
68 1.1 agc (void) memcpy(&tp->dir, d, sizeof(tp->dir));
69 1.1 agc tp->dir.st_mode = S_IFDIR | 0755;
70 1.1 agc tp->dir.st_nlink = 2;
71 1.1 agc (void) memcpy(&tp->file, f, sizeof(tp->file));
72 1.1 agc tp->file.st_mode = S_IFREG | 0644;
73 1.1 agc tp->file.st_nlink = 1;
74 1.1 agc (void) memcpy(&tp->lnk, l, sizeof(tp->lnk));
75 1.1 agc tp->lnk.st_mode = S_IFLNK | 0644;
76 1.1 agc tp->lnk.st_nlink = 1;
77 1.1 agc return 1;
78 1.1 agc }
79 1.1 agc
80 1.1 agc /* add an entry to the tree */
81 1.1 agc int
82 1.1 agc virtdir_add(virtdir_t *tp, const char *name, size_t size, uint8_t type, char *tgt)
83 1.1 agc {
84 1.1 agc struct stat st;
85 1.1 agc
86 1.1 agc if (tp->v == NULL) {
87 1.1 agc (void) stat(".", &st);
88 1.1 agc virtdir_init(tp, &st, &st, &st);
89 1.1 agc }
90 1.1 agc ALLOC(virt_dirent_t, tp->v, tp->size, tp->c, 10, 10, "virtdir_add",
91 1.1 agc return 0);
92 1.1 agc tp->v[tp->c].namelen = size;
93 1.1 agc if ((tp->v[tp->c].name = strnsave(name, size)) == NULL) {
94 1.1 agc return 0;
95 1.1 agc }
96 1.1 agc tp->v[tp->c].d_name = strrchr(tp->v[tp->c].name, '/') + 1;
97 1.1 agc tp->v[tp->c].type = type;
98 1.1 agc if (tgt != NULL) {
99 1.1 agc tp->v[tp->c].tgt = strdup(tgt);
100 1.1 agc tp->v[tp->c].tgtlen = strlen(tgt);
101 1.1 agc }
102 1.1 agc tp->c += 1;
103 1.1 agc qsort(tp->v, tp->c, sizeof(tp->v[0]), compare);
104 1.1 agc return 1;
105 1.1 agc }
106 1.1 agc
107 1.1 agc /* add an entry to the tree */
108 1.1 agc int
109 1.1 agc virtdir_del(virtdir_t *tp, const char *name, size_t size)
110 1.1 agc {
111 1.1 agc virt_dirent_t *ep;
112 1.1 agc int i;
113 1.1 agc
114 1.1 agc if ((ep = virtdir_find(tp, name, size)) == NULL) {
115 1.1 agc return 0;
116 1.1 agc }
117 1.1 agc i = (int)(ep - tp->v) / sizeof(tp->v[0]);
118 1.1 agc for (tp->c -= 1 ; i < tp->c ; i++) {
119 1.1 agc tp->v[i] = tp->v[i + 1];
120 1.1 agc }
121 1.1 agc return 1;
122 1.1 agc }
123 1.1 agc
124 1.1 agc /* find an entry in the tree */
125 1.1 agc virt_dirent_t *
126 1.1 agc virtdir_find(virtdir_t *tp, const char *name, size_t namelen)
127 1.1 agc {
128 1.1 agc virt_dirent_t e;
129 1.1 agc
130 1.1 agc (void) memset(&e, 0x0, sizeof(e));
131 1.1 agc e.name = __UNCONST(name);
132 1.1 agc e.namelen = namelen;
133 1.1 agc return bsearch(&e, tp->v, tp->c, sizeof(tp->v[0]), compare);
134 1.1 agc }
135 1.1 agc
136 1.1 agc /* analogous to opendir(3) - open a directory, save information, and
137 1.1 agc * return a pointer to the dynamically allocated structure */
138 1.1 agc VIRTDIR *
139 1.1 agc openvirtdir(virtdir_t *tp, const char *d)
140 1.1 agc {
141 1.1 agc VIRTDIR *dirp;
142 1.1 agc
143 1.1 agc NEW(VIRTDIR, dirp, "openvirtdir", exit(EXIT_FAILURE));
144 1.1 agc dirp->dirname = strdup(d);
145 1.1 agc dirp->dirnamelen = strlen(d);
146 1.1 agc dirp->tp = tp;
147 1.1 agc dirp->i = 0;
148 1.1 agc return dirp;
149 1.1 agc }
150 1.1 agc
151 1.1 agc /* analogous to readdir(3) - read the next entry in the directory that
152 1.1 agc * was opened, and return a pointer to it */
153 1.1 agc virt_dirent_t *
154 1.1 agc readvirtdir(VIRTDIR *dirp)
155 1.1 agc {
156 1.1 agc char *from;
157 1.1 agc
158 1.1 agc for ( ; dirp->i < dirp->tp->c ; dirp->i++) {
159 1.1 agc from = (strcmp(dirp->dirname, "/") == 0) ?
160 1.1 agc &dirp->tp->v[dirp->i].name[1] :
161 1.1 agc &dirp->tp->v[dirp->i].name[dirp->dirnamelen + 1];
162 1.1 agc if (strncmp(dirp->tp->v[dirp->i].name, dirp->dirname,
163 1.1 agc dirp->dirnamelen) == 0 &&
164 1.1 agc strchr(from, '/') == NULL) {
165 1.1 agc return &dirp->tp->v[dirp->i++];
166 1.1 agc }
167 1.1 agc }
168 1.1 agc return NULL;
169 1.1 agc }
170 1.1 agc
171 1.1 agc /* free the storage associated with the virtual directory structure */
172 1.1 agc void
173 1.1 agc closevirtdir(VIRTDIR *dirp)
174 1.1 agc {
175 1.1 agc free(dirp->dirname);
176 1.1 agc FREE(dirp);
177 1.1 agc }
178 1.1 agc
179 1.1 agc /* find a target in the tree -- not quick! */
180 1.1 agc virt_dirent_t *
181 1.1 agc virtdir_find_tgt(virtdir_t *tp, const char *tgt, size_t tgtlen)
182 1.1 agc {
183 1.1 agc /* we don't need no stinking binary searches */
184 1.1 agc int i;
185 1.1 agc
186 1.1 agc for (i = 0 ; i < tp->c ; i++) {
187 1.1 agc if (tp->v[i].tgt && strcmp(tp->v[i].tgt, tgt) == 0) {
188 1.1 agc return &tp->v[i];
189 1.1 agc }
190 1.1 agc }
191 1.1 agc return NULL;
192 1.1 agc }
193 1.1 agc
194 1.1 agc /* kill all of the space allocated to the tree */
195 1.1 agc void
196 1.1 agc virtdir_drop(virtdir_t *tp)
197 1.1 agc {
198 1.1 agc int i;
199 1.1 agc
200 1.1 agc for (i = 0 ; i < tp->c ; i++) {
201 1.1 agc FREE(tp->v[i].name);
202 1.1 agc if (tp->v[i].tgt) {
203 1.1 agc FREE(tp->v[i].tgt);
204 1.1 agc }
205 1.1 agc }
206 1.1 agc FREE(tp->v);
207 1.1 agc }
208 1.1 agc
209 1.1 agc #ifdef VIRTDIR_DEBUG
210 1.1 agc static void
211 1.1 agc ptree(virtdir_t * tp)
212 1.1 agc {
213 1.1 agc int i;
214 1.1 agc
215 1.1 agc for (i = 0 ; i < tp->c ; i++) {
216 1.1 agc printf("%s, tgt %s\n", tp->v[i].name, tp->v[i].tgt);
217 1.1 agc }
218 1.1 agc }
219 1.1 agc #endif
220 1.1 agc
221 1.1 agc #ifdef VIRTDIR_DEBUG
222 1.1 agc int
223 1.1 agc main(int argc, char **argv)
224 1.1 agc {
225 1.1 agc virt_dirent_t *tp;
226 1.1 agc virtdir_t t;
227 1.1 agc struct stat st;
228 1.1 agc
229 1.1 agc (void) memset(&t, 0x0, sizeof(t));
230 1.1 agc stat(".", &st);
231 1.1 agc virtdir_add(&t, ".", 1, 'd', NULL);
232 1.1 agc stat("..", &st);
233 1.1 agc virtdir_add(&t, "..", 2, 'd', NULL);
234 1.1 agc st.st_mode = S_IFREG | 0644;
235 1.1 agc virtdir_add(&t, "file1", 5, 'f', NULL);
236 1.1 agc ptree(&t);
237 1.1 agc virtdir_add(&t, "file2", 5, 'f', NULL);
238 1.1 agc virtdir_add(&t, "file0", 5, 'f', NULL);
239 1.1 agc virtdir_add(&t, "abcde", 5, 'f', NULL);
240 1.1 agc virtdir_add(&t, "bcdef", 5, 'f', NULL);
241 1.1 agc virtdir_add(&t, "a", 1, 'f', NULL);
242 1.1 agc ptree(&t);
243 1.1 agc if ((tp = virtdir_find(&t, "a", 1)) == NULL) {
244 1.1 agc printf("borked2\n");
245 1.1 agc } else {
246 1.1 agc printf("a found\n");
247 1.1 agc }
248 1.1 agc virtdir_drop(&t);
249 1.1 agc exit(EXIT_SUCCESS);
250 1.1 agc }
251 1.1 agc #endif
252