blob: 0e66e71203ad240c604aae9a68a876b60b0f5c30 [file] [log] [blame]
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001/*
2 * lookup.c --- ext2fs directory lookup operations
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1993, 1994, 1994, 1995 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
10 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000013#include <stdio.h>
14#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#if HAVE_UNISTD_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000016#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000017#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000018
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000019#include "ext2_fs.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000020#include "ext2fs.h"
21
22struct lookup_struct {
23 const char *name;
24 int len;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000025 ext2_ino_t *inode;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000026 int found;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040027};
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000028
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000029#ifdef __TURBOC__
Theodore Ts'o48e6e812003-07-06 00:36:48 -040030 #pragma argsused
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000031#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000032static int lookup_proc(struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -050033 int offset EXT2FS_ATTR((unused)),
34 int blocksize EXT2FS_ATTR((unused)),
35 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000036 void *priv_data)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000037{
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000038 struct lookup_struct *ls = (struct lookup_struct *) priv_data;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000039
Theodore Ts'o674a4ee1998-03-23 02:06:52 +000040 if (ls->len != (dirent->name_len & 0xFF))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000041 return 0;
Theodore Ts'o674a4ee1998-03-23 02:06:52 +000042 if (strncmp(ls->name, dirent->name, (dirent->name_len & 0xFF)))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000043 return 0;
44 *ls->inode = dirent->inode;
45 ls->found++;
46 return DIRENT_ABORT;
47}
48
49
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000050errcode_t ext2fs_lookup(ext2_filsys fs, ext2_ino_t dir, const char *name,
51 int namelen, char *buf, ext2_ino_t *inode)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000052{
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
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +000067 return (ls.found) ? 0 : EXT2_ET_FILE_NOT_FOUND;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000068}
69
70