/src/e2fsprogs/lib/ext2fs/lookup.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * lookup.c --- ext2fs directory lookup operations |
3 | | * |
4 | | * Copyright (C) 1993, 1994, 1994, 1995 Theodore Ts'o. |
5 | | * |
6 | | * %Begin-Header% |
7 | | * This file may be redistributed under the terms of the GNU Library |
8 | | * General Public License, version 2. |
9 | | * %End-Header% |
10 | | */ |
11 | | |
12 | | #include "config.h" |
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; |
27 | | }; |
28 | | |
29 | | #ifdef __TURBOC__ |
30 | | #pragma argsused |
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 | 0 | { |
38 | 0 | struct lookup_struct *ls = (struct lookup_struct *) priv_data; |
39 | |
|
40 | 0 | if (ls->len != ext2fs_dirent_name_len(dirent)) |
41 | 0 | return 0; |
42 | 0 | if (strncmp(ls->name, dirent->name, ext2fs_dirent_name_len(dirent))) |
43 | 0 | return 0; |
44 | 0 | *ls->inode = dirent->inode; |
45 | 0 | ls->found++; |
46 | 0 | return DIRENT_ABORT; |
47 | 0 | } |
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 | 0 | { |
53 | 0 | errcode_t retval; |
54 | 0 | struct lookup_struct ls; |
55 | |
|
56 | 0 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
57 | | |
58 | 0 | ls.name = name; |
59 | 0 | ls.len = namelen; |
60 | 0 | ls.inode = inode; |
61 | 0 | ls.found = 0; |
62 | |
|
63 | 0 | retval = ext2fs_dir_iterate(fs, dir, 0, buf, lookup_proc, &ls); |
64 | 0 | if (retval) |
65 | 0 | return retval; |
66 | | |
67 | 0 | return (ls.found) ? 0 : EXT2_ET_FILE_NOT_FOUND; |
68 | 0 | } |
69 | | |
70 | | |