blob: 76c5ac871ca71c9d33d55799c371d37916bb2183 [file] [log] [blame]
Mike Frysinger1fd98e02005-05-09 22:10:42 +00001/*
2 * lookup.c --- ext2fs directory lookup operations
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003 *
Mike Frysinger1fd98e02005-05-09 22:10:42 +00004 * 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
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;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000026};
Mike Frysinger1fd98e02005-05-09 22:10:42 +000027
28#ifdef __TURBOC__
Mike Frysingerf8855132006-03-28 02:35:56 +000029# pragma argsused
Mike Frysinger1fd98e02005-05-09 22:10:42 +000030#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