lookup.c revision 674a4ee1e3e05133ddad701730bfc21c283272a4
119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * lookup.c --- ext2fs directory lookup operations
319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o *
419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * Copyright (C) 1993, 1994, 1994, 1995 Theodore Ts'o.
519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o *
619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %Begin-Header%
719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * This file may be redistributed under the terms of the GNU Public
819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * License.
919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %End-Header%
1019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
1119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
1219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <stdio.h>
1319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <string.h>
144cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#if HAVE_UNISTD_H
1519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <unistd.h>
164cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#endif
1719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
18b5abe6fac9c9e7caf4710501d1657d30e4857ef6Theodore Ts'o#if EXT2_FLAT_INCLUDES
19b5abe6fac9c9e7caf4710501d1657d30e4857ef6Theodore Ts'o#include "ext2_fs.h"
20b5abe6fac9c9e7caf4710501d1657d30e4857ef6Theodore Ts'o#else
2119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <linux/ext2_fs.h>
22b5abe6fac9c9e7caf4710501d1657d30e4857ef6Theodore Ts'o#endif
2319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
2419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include "ext2fs.h"
2519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
2619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostruct lookup_struct  {
2719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	const char	*name;
2819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	int		len;
2919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	ino_t		*inode;
3019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	int		found;
3119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o};
3219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
333cb6c5021d722e17b7105d1bc090880671f6fc6dTheodore Ts'o#ifdef __TURBOC__
343cb6c5021d722e17b7105d1bc090880671f6fc6dTheodore Ts'o#pragma argsused
353cb6c5021d722e17b7105d1bc090880671f6fc6dTheodore Ts'o#endif
3619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic int lookup_proc(struct ext2_dir_entry *dirent,
3719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		       int	offset,
3819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		       int	blocksize,
3919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		       char	*buf,
40b5abe6fac9c9e7caf4710501d1657d30e4857ef6Theodore Ts'o		       void	*priv_data)
4119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
42b5abe6fac9c9e7caf4710501d1657d30e4857ef6Theodore Ts'o	struct lookup_struct *ls = (struct lookup_struct *) priv_data;
4319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
44674a4ee1e3e05133ddad701730bfc21c283272a4Theodore Ts'o	if (ls->len != (dirent->name_len & 0xFF))
4519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return 0;
46674a4ee1e3e05133ddad701730bfc21c283272a4Theodore Ts'o	if (strncmp(ls->name, dirent->name, (dirent->name_len & 0xFF)))
4719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return 0;
4819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	*ls->inode = dirent->inode;
4919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	ls->found++;
5019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return DIRENT_ABORT;
5119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
5219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
5319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
5419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'oerrcode_t ext2fs_lookup(ext2_filsys fs, ino_t dir, const char *name,
5519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			int namelen, char *buf, ino_t *inode)
5619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
5719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval;
5819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct lookup_struct ls;
5919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
6019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
6119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
6219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	ls.name = name;
6319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	ls.len = namelen;
6419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	ls.inode = inode;
6519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	ls.found = 0;
6619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
6719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	retval = ext2fs_dir_iterate(fs, dir, 0, buf, lookup_proc, &ls);
6819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (retval)
6919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return retval;
7019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
711f0b6c1f895d189fea6999d0c07a7fee936a4baaTheodore Ts'o	return (ls.found) ? 0 : EXT2_ET_FILE_NOT_FOUND;
7219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
7319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
7419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
75