blob: 97aa08877352f766127873ff66198d0f7d59ecfb [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
12#include <stdio.h>
13#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000017
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000018#include "ext2_fs.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000019#include "ext2fs.h"
20
21struct lookup_struct {
22 const char *name;
23 int len;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000024 ext2_ino_t *inode;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000025 int found;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040026};
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000027
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000028#ifdef __TURBOC__
Theodore Ts'o48e6e812003-07-06 00:36:48 -040029 #pragma argsused
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000030#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000031static int lookup_proc(struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -050032 int offset EXT2FS_ATTR((unused)),
33 int blocksize EXT2FS_ATTR((unused)),
34 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000035 void *priv_data)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000036{
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000037 struct lookup_struct *ls = (struct lookup_struct *) priv_data;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000038
Theodore Ts'o674a4ee1998-03-23 02:06:52 +000039 if (ls->len != (dirent->name_len & 0xFF))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000040 return 0;
Theodore Ts'o674a4ee1998-03-23 02:06:52 +000041 if (strncmp(ls->name, dirent->name, (dirent->name_len & 0xFF)))
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000042 return 0;
43 *ls->inode = dirent->inode;
44 ls->found++;
45 return DIRENT_ABORT;
46}
47
48
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000049errcode_t ext2fs_lookup(ext2_filsys fs, ext2_ino_t dir, const char *name,
50 int namelen, char *buf, ext2_ino_t *inode)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000051{
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
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +000066 return (ls.found) ? 0 : EXT2_ET_FILE_NOT_FOUND;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000067}
68
69