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