openblt

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

dir.c (4158B)


      1 /* $Id: //depot/blt/srv/vfs/drivers/ffs/dir.c#3 $
      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 "vfs-int.h"
     34 
     35 #ifndef VFS_SANDBOX
     36 #include <blt/blkdev.h>
     37 #else
     38 #include "../../sandbox/blkdev.h"
     39 #endif
     40 
     41 #include "ffs.h"
     42 #include "ffs-blt.h"
     43 #include "dinode.h"
     44 #include "dir.h"
     45 
     46 int ffs_opendir (struct vnode *dir, void **cookie)
     47 {
     48 	char *buf;
     49 	int i, j, offset;
     50 	struct vfs_dirent_node *head, *p;
     51 	struct ffs_super *fs;
     52 	struct ffs_super_data *data;
     53 	struct ffs_dinode *di;
     54 	struct ffs_dircookie *fc;
     55 	struct ffs_direct *direct;
     56 
     57 #ifdef FFS_DEBUG
     58 	printf ("ffs_opendir %lld\n", dir->v_vnid);
     59 #endif
     60 
     61 	head = NULL;
     62 	data = dir->v_sb->sb_data;
     63 	fs = data->sbbuf;
     64 	di = dir->v_data;
     65 	buf = malloc (BLKSIZE);
     66 	for (i = 0; i < NDADDR; i++)
     67 		if (di->di_db[i])
     68 		{
     69 			blk_read (data->dev, buf, fsbtodb (fs, di->di_db[i]),
     70 				BLKSIZE / data->dev->blksize);
     71 			for (j = offset = 0; offset < BLKSIZE; offset += direct->d_reclen)
     72 			{
     73 				direct = (struct ffs_direct *) (buf + offset);
     74 				if (!direct->d_ino)
     75 					break;
     76 				//printf ("opendir %s %d %d\n", direct->d_name, offset, j++);
     77 				//if (j > 10) for (;;) ;
     78 				p = malloc (sizeof (struct vfs_dirent_node));
     79 				p->dirent = malloc (sizeof (struct dirent));
     80 				p->dirent->d_fileno = direct->d_ino;
     81 				p->dirent->d_reclen = sizeof (struct dirent);
     82 				strncpy (p->dirent->d_name, direct->d_name,
     83 					sizeof (p->dirent->d_name));
     84 				p->next = head;
     85 				head = p;
     86 			}
     87 		}
     88 	for (i = 0; i < NIADDR; i++)
     89 		if (di->di_ib[i])
     90 		{
     91 		}
     92 	free (buf);
     93 	fc = malloc (sizeof (struct ffs_dircookie));
     94 	fc->head = fc->current = head;
     95 	*cookie = fc;
     96 	return 0;
     97 }
     98 
     99 void ffs_closedir (struct vnode *dir, void *cookie)
    100 {
    101 #ifdef FFS_DEBUG
    102 	printf ("ffs_closedir %lld\n", dir->v_vnid);
    103 #endif
    104 }
    105 
    106 void ffs_free_dircookie (void *cookie)
    107 {
    108 	struct ffs_dircookie *fc;
    109 
    110 #ifdef FFS_DEBUG
    111 	printf ("ffs_freedircookie\n");
    112 #endif
    113 	fc = cookie;
    114 	while (fc->head != NULL)
    115 	{
    116 		fc->current = fc->head->next;
    117 		free (fc->head);
    118 		fc->head = fc->current;
    119 	}
    120 	free (fc);
    121 }
    122 
    123 int ffs_rewinddir (struct vnode *dir, void *cookie)
    124 {
    125 	struct ffs_dircookie *fc;
    126 
    127 #ifdef FFS_DEBUG
    128 	printf ("ffs_rewinddir %lld\n", dir->v_vnid);
    129 #endif
    130 	fc = cookie;
    131 	fc->current = fc->head;
    132 	return 0;
    133 }
    134 
    135 int ffs_readdir (struct vnode *dir, struct dirent *dirent, void *cookie)
    136 {
    137 	struct dirent *orig;
    138 	struct ffs_dircookie *fc;
    139 
    140 #ifdef FFS_DEBUG
    141 	//printf ("ffs_readdir %lld\n", dir->v_vnid);
    142 #endif
    143 	fc = cookie;
    144 	if (fc->current == NULL)
    145 		return 1;
    146 	orig = fc->current->dirent;
    147 	dirent->d_fileno = orig->d_fileno;
    148 	dirent->d_reclen = orig->d_reclen;
    149 	strncpy (dirent->d_name, orig->d_name, MAXNAMLEN);
    150 	fc->current = fc->current->next;
    151 	return 0;
    152 }
    153