openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

vfs.c (4543B)


      1 /* $Id: //depot/blt/srv/vfs/sandbox/vfs.c#2 $
      2 **
      3 ** Copyright 1999 Sidney Cammeresi
      4 ** All rights reserved.
      5 **
      6 ** Redistribution and use in source and binary forms, with or without
      7 ** modification, are permitted provided that the following conditions
      8 ** are met:
      9 ** 1. Redistributions of source code must retain the above copyright
     10 **    notice, this list of conditions, and the following disclaimer.
     11 ** 2. Redistributions in binary form must reproduce the above copyright
     12 **    notice, this list of conditions, and the following disclaimer in the
     13 **    documentation and/or other materials provided with the distribution.
     14 ** 3. The name of the author may not be used to endorse or promote products
     15 **    derived from this software without specific prior written permission.
     16 **
     17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 */
     28 
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <string.h>
     32 #include <dirent.h>
     33 #include <sys/stat.h>
     34 #include <dlfcn.h>
     35 #include "../vfs-int.h"
     36 
     37 #define NAME_PREFIX    "../drivers/"
     38 #define NAME_SUFFIX    ".so"
     39 
     40 struct fs_type *fs_drivers;
     41 struct superblock *mount_list = NULL;
     42 
     43 void usage (void)
     44 {
     45 	printf ("usage: vfs driver file\n");
     46 	exit (1);
     47 }
     48 
     49 int main (int argc, char **argv)
     50 {
     51 	char *buf, c;
     52 	int i, len, num, pos, res;
     53 	void *fs, *cookie;
     54 	struct stat statbuf;
     55 	struct dirent dirent;
     56 	struct vnode *vn;
     57 
     58 	if (argc < 3)
     59 		usage ();
     60 
     61 	buf = malloc (strlen (NAME_PREFIX) + strlen (NAME_SUFFIX) +
     62 		strlen (argv[1]) * 2 + 2);
     63 	strcpy (buf, NAME_PREFIX);
     64 	strcat (buf, argv[1]);
     65 	strcat (buf, "/");
     66 	strcat (buf, argv[1]);
     67 	strcat (buf, NAME_SUFFIX);
     68 
     69 	if (stat (buf, &statbuf))
     70 	{
     71 		printf ("driver `%s' not found\n", argv[1]);
     72 		exit (1);
     73 	}
     74 	if ((fs = dlopen (buf, 0)) == NULL)
     75 	{
     76 		printf ("error loading driver `%s'\n", argv[1]);
     77 		exit (1);
     78 	}
     79 
     80 	vfs_mount ("/", argv[1], 0, argv[2]);
     81 	free (buf);
     82 
     83 /*
     84 	vn = fs_drivers->t_vops->walk (mount_list->sb_root, "etc/afs/ThisCell");
     85 	vput (vn);
     86 	vn = fs_drivers->t_vops->walk (mount_list->sb_root,
     87 		"etc/kerberosIV/krb.conf");
     88 	vput (vn);
     89 	vn = fs_drivers->t_vops->walk (mount_list->sb_root, "bin/df");
     90 	vput (vn);
     91 */
     92 /*
     93 	vn = fs_drivers->t_vops->walk (mount_list->sb_root, "etc");
     94 	fs_drivers->t_vops->opendir (vn, &cookie);
     95 	vput (vn);
     96 */
     97 /*
     98 	vn = fs_drivers->t_vops->walk (mount_list->sb_root, "bin");
     99 	printf ("vnid is %lld\n", vn->v_vnid);
    100 	fs_drivers->t_vops->opendir (vn, &cookie);
    101 	while (!fs_drivers->t_vops->readdir (vn, &dirent, cookie))
    102 		printf ("name %s\n", dirent.d_name);
    103 	fs_drivers->t_vops->rewinddir (vn, cookie);
    104 	fs_drivers->t_vops->closedir (vn, cookie);
    105 	fs_drivers->t_vops->free_dircookie (cookie);
    106 	vput (vn);
    107 */
    108 /*
    109 	buf = malloc (len = 13062);
    110 	vn = fs_drivers->t_vops->walk (mount_list->sb_root, "etc/rc");
    111 	fs_drivers->t_vops->open (vn, &cookie);
    112 	res = fs_drivers->t_vops->read (vn, buf, len, pos = 0, &num, cookie);
    113 	printf ("read %d %d bytes\n", res, num);
    114 	for (i = 0; i < len; i++)
    115 		printf ("%c", buf[i]);
    116 	vput (vn);
    117 */
    118 /*
    119 	pos = 0;
    120 	vn = fs_drivers->t_vops->walk (mount_list->sb_root, "etc/rc");
    121 	fs_drivers->t_vops->open (vn, &cookie);
    122 	do
    123 	{
    124 		res = fs_drivers->t_vops->read (vn, &c, 1, pos++, &num, cookie);
    125 		printf ("%c", c);
    126 	}
    127 	while (!res && num);
    128 	vput (vn);
    129 */
    130 /*
    131 	//pos = 12 * 8192;
    132 	pos = 0;
    133 	buf = malloc (len = 8192);
    134 	vn = fs_drivers->t_vops->walk (mount_list->sb_root, "etc/magic");
    135 	fs_drivers->t_vops->open (vn, &cookie);
    136 	do
    137 	{
    138 		res = fs_drivers->t_vops->read (vn, buf, len, pos, &num, cookie);
    139 		pos += num;
    140 		for (i = 0; i < num; i++)
    141 			printf ("%c", buf[i]);
    142 	}
    143 	while (!res && num);
    144 	vput (vn);
    145 	free (buf);
    146 */
    147 
    148 	vn = fs_drivers->t_vops->walk (mount_list->sb_root, "etc/magic");
    149 	fs_drivers->t_vops->rstat (vn, &statbuf);
    150 	printf ("inode is %lld, nlink is %d, uid is %d, gid is %d, size is %d\n",
    151 		statbuf.st_ino, statbuf.st_nlink, statbuf.st_uid, statbuf.st_gid,
    152 		statbuf.st_size);
    153 	vput (vn);
    154 
    155 	return 0;
    156 }
    157