Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / lookup.c
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 Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <stdlib.h>
18 #if HAVE_ERRNO_H
19 #include <errno.h>
20 #endif
21
22 #include <linux/ext2_fs.h>
23
24 #include "ext2fs.h"
25
26 struct lookup_struct  {
27         const char      *name;
28         int             len;
29         ino_t           *inode;
30         int             found;
31 };      
32
33 #ifdef __TURBOC__
34 #pragma argsused
35 #endif
36 static int lookup_proc(struct ext2_dir_entry *dirent,
37                        int      offset,
38                        int      blocksize,
39                        char     *buf,
40                        void     *private)
41 {
42         struct lookup_struct *ls = (struct lookup_struct *) private;
43
44         if (ls->len != dirent->name_len)
45                 return 0;
46         if (strncmp(ls->name, dirent->name, dirent->name_len))
47                 return 0;
48         *ls->inode = dirent->inode;
49         ls->found++;
50         return DIRENT_ABORT;
51 }
52
53
54 errcode_t ext2fs_lookup(ext2_filsys fs, ino_t dir, const char *name,
55                         int namelen, char *buf, ino_t *inode)
56 {
57         errcode_t       retval;
58         struct lookup_struct ls;
59
60         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
61
62         ls.name = name;
63         ls.len = namelen;
64         ls.inode = inode;
65         ls.found = 0;
66
67         retval = ext2fs_dir_iterate(fs, dir, 0, buf, lookup_proc, &ls);
68         if (retval)
69                 return retval;
70
71         return (ls.found) ? 0 : ENOENT;
72 }
73
74