"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame^] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * lookup.c --- ext2fs directory lookup operations |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 4 | * |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 5 | * Copyright (C) 1993, 1994, 1994, 1995 Theodore Ts'o. |
| 6 | * |
| 7 | * %Begin-Header% |
| 8 | * This file may be redistributed under the terms of the GNU Public |
| 9 | * License. |
| 10 | * %End-Header% |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | #if HAVE_UNISTD_H |
| 16 | #include <unistd.h> |
| 17 | #endif |
| 18 | |
| 19 | #include "ext2_fs.h" |
| 20 | #include "ext2fs.h" |
| 21 | |
| 22 | struct lookup_struct { |
| 23 | const char *name; |
| 24 | int len; |
| 25 | ext2_ino_t *inode; |
| 26 | int found; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 27 | }; |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 28 | |
| 29 | #ifdef __TURBOC__ |
Mike Frysinger | f885513 | 2006-03-28 02:35:56 +0000 | [diff] [blame] | 30 | # pragma argsused |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 31 | #endif |
| 32 | static int lookup_proc(struct ext2_dir_entry *dirent, |
| 33 | int offset EXT2FS_ATTR((unused)), |
| 34 | int blocksize EXT2FS_ATTR((unused)), |
| 35 | char *buf EXT2FS_ATTR((unused)), |
| 36 | void *priv_data) |
| 37 | { |
| 38 | struct lookup_struct *ls = (struct lookup_struct *) priv_data; |
| 39 | |
| 40 | if (ls->len != (dirent->name_len & 0xFF)) |
| 41 | return 0; |
| 42 | if (strncmp(ls->name, dirent->name, (dirent->name_len & 0xFF))) |
| 43 | return 0; |
| 44 | *ls->inode = dirent->inode; |
| 45 | ls->found++; |
| 46 | return DIRENT_ABORT; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | errcode_t ext2fs_lookup(ext2_filsys fs, ext2_ino_t dir, const char *name, |
| 51 | int namelen, char *buf, ext2_ino_t *inode) |
| 52 | { |
| 53 | errcode_t retval; |
| 54 | struct lookup_struct ls; |
| 55 | |
| 56 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 57 | |
| 58 | ls.name = name; |
| 59 | ls.len = namelen; |
| 60 | ls.inode = inode; |
| 61 | ls.found = 0; |
| 62 | |
| 63 | retval = ext2fs_dir_iterate(fs, dir, 0, buf, lookup_proc, &ls); |
| 64 | if (retval) |
| 65 | return retval; |
| 66 | |
| 67 | return (ls.found) ? 0 : EXT2_ET_FILE_NOT_FOUND; |
| 68 | } |
| 69 | |
| 70 | |