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