1 #include "inc.h" 2 3 static int search_dir( 4 struct inode *ldir_ptr, /* dir record parent */ 5 char string[NAME_MAX], /* component to search for */ 6 ino_t *numb /* pointer to new dir record */ 7 ) { 8 /* 9 * The search_dir function performs the operation of searching for the 10 * component ``string" in ldir_ptr. It returns the response and the 11 * number of the inode in numb. 12 */ 13 int r, i; 14 15 /* 16 * This function search a particular element (in string) in a inode and 17 * return its number. 18 */ 19 20 if ((ldir_ptr->i_stat.st_mode & S_IFMT) != S_IFDIR) 21 return ENOTDIR; 22 23 r = read_directory(ldir_ptr); 24 if (r != OK) 25 return r; 26 27 if (strcmp(".", string) == 0) { 28 *numb = ldir_ptr->i_stat.st_ino; 29 return OK; 30 } 31 32 /* Walk the directory listing. */ 33 for (i = 0; i < ldir_ptr->dir_size; i++) { 34 if (strcmp(string, ldir_ptr->dir_contents[i].name) == 0) { 35 *numb = ldir_ptr->dir_contents[i].i_node->i_stat.st_ino; 36 return OK; 37 } 38 } 39 40 return ENOENT; 41 } 42 43 int fs_lookup(ino_t dir_nr, char *name, struct fsdriver_node *node, 44 int *is_mountpt) 45 { 46 /* Given a directory and a component of a path, look up the component 47 * in the directory, find the inode, open it, and return its details. 48 */ 49 struct inode *dirp, *rip; 50 ino_t ino_nr; 51 int r; 52 53 /* Find the starting inode. */ 54 if ((dirp = get_inode(dir_nr)) == NULL) 55 return EINVAL; 56 57 /* Look up the directory entry. */ 58 if ((r = search_dir(dirp, name, &ino_nr)) != OK) 59 return r; 60 61 /* The component has been found in the directory. Get the inode. */ 62 if ((rip = open_inode(ino_nr)) == NULL) 63 return EIO; /* FIXME: this could have multiple causes */ 64 65 /* Return its details to the caller. */ 66 node->fn_ino_nr = rip->i_stat.st_ino; 67 node->fn_mode = rip->i_stat.st_mode; 68 node->fn_size = rip->i_stat.st_size; 69 node->fn_uid = rip->i_stat.st_uid; 70 node->fn_gid = rip->i_stat.st_gid; 71 node->fn_dev = rip->i_stat.st_rdev; 72 73 *is_mountpt = rip->i_mountpoint; 74 75 return OK; 76 } 77