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 <stdio.h>
13#include <string.h>
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17
18#include "ext2_fs.h"
19#include "ext2fs.h"
20
21struct lookup_struct  {
22	const char	*name;
23	int		len;
24	ext2_ino_t	*inode;
25	int		found;
26};
27
28#ifdef __TURBOC__
29 #pragma argsused
30#endif
31static int lookup_proc(struct ext2_dir_entry *dirent,
32		       int	offset EXT2FS_ATTR((unused)),
33		       int	blocksize EXT2FS_ATTR((unused)),
34		       char	*buf EXT2FS_ATTR((unused)),
35		       void	*priv_data)
36{
37	struct lookup_struct *ls = (struct lookup_struct *) priv_data;
38
39	if (ls->len != (dirent->name_len & 0xFF))
40		return 0;
41	if (strncmp(ls->name, dirent->name, (dirent->name_len & 0xFF)))
42		return 0;
43	*ls->inode = dirent->inode;
44	ls->found++;
45	return DIRENT_ABORT;
46}
47
48
49errcode_t ext2fs_lookup(ext2_filsys fs, ext2_ino_t dir, const char *name,
50			int namelen, char *buf, ext2_ino_t *inode)
51{
52	errcode_t	retval;
53	struct lookup_struct ls;
54
55	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
56
57	ls.name = name;
58	ls.len = namelen;
59	ls.inode = inode;
60	ls.found = 0;
61
62	retval = ext2fs_dir_iterate(fs, dir, 0, buf, lookup_proc, &ls);
63	if (retval)
64		return retval;
65
66	return (ls.found) ? 0 : EXT2_ET_FILE_NOT_FOUND;
67}
68
69
70