2 * namei.c --- ext2fs directory lookup operations
4 * Copyright (C) 1993, 1994, 1994, 1995 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
19 /* #define NAMEI_DEBUG */
25 static errcode_t open_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t base,
26 const char *pathname, size_t pathlen, int follow,
27 int link_count, char *buf, ext2_ino_t *res_inode);
29 static errcode_t follow_link(ext2_filsys fs, ext2_ino_t root, ext2_ino_t dir,
30 ext2_ino_t inode, int link_count,
31 char *buf, ext2_ino_t *res_inode)
39 printf("follow_link: root=%lu, dir=%lu, inode=%lu, lc=%d\n",
40 root, dir, inode, link_count);
43 retval = ext2fs_read_inode (fs, inode, &ei);
44 if (retval) return retval;
45 if (!LINUX_S_ISLNK (ei.i_mode)) {
49 if (link_count++ >= EXT2FS_MAX_NESTED_LINKS)
50 return EXT2_ET_SYMLINK_LOOP;
52 /* FIXME-64: Actually, this is FIXME EXTENTS */
53 if (ext2fs_inode_data_blocks(fs,&ei)) {
54 retval = ext2fs_get_mem(fs->blocksize, &buffer);
57 retval = io_channel_read_blk(fs->io, ei.i_block[0], 1, buffer);
59 ext2fs_free_mem(&buffer);
64 pathname = (char *)&(ei.i_block[0]);
65 retval = open_namei(fs, root, dir, pathname, ei.i_size, 1,
66 link_count, buf, res_inode);
68 ext2fs_free_mem(&buffer);
73 * This routine interprets a pathname in the context of the current
74 * directory and the root directory, and returns the inode of the
75 * containing directory, and a pointer to the filename of the file
76 * (pointing into the pathname) and the length of the filename.
78 static errcode_t dir_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t dir,
79 const char *pathname, int pathlen,
80 int link_count, char *buf,
81 const char **name, int *namelen,
82 ext2_ino_t *res_inode)
90 if ((c = *pathname) == '/') {
97 for (len=0; --pathlen >= 0;len++) {
104 retval = ext2fs_lookup (fs, dir, thisname, len, buf, &inode);
105 if (retval) return retval;
106 retval = follow_link (fs, root, dir, inode,
107 link_count, buf, &dir);
108 if (retval) return retval;
116 static errcode_t open_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t base,
117 const char *pathname, size_t pathlen, int follow,
118 int link_count, char *buf, ext2_ino_t *res_inode)
120 const char *base_name;
122 ext2_ino_t dir, inode;
126 printf("open_namei: root=%lu, dir=%lu, path=%*s, lc=%d\n",
127 root, base, pathlen, pathname, link_count);
129 retval = dir_namei(fs, root, base, pathname, pathlen,
130 link_count, buf, &base_name, &namelen, &dir);
131 if (retval) return retval;
132 if (!namelen) { /* special case: '/usr/' etc */
136 retval = ext2fs_lookup (fs, dir, base_name, namelen, buf, &inode);
140 retval = follow_link(fs, root, dir, inode, link_count,
146 printf("open_namei: (link_count=%d) returns %lu\n",
153 errcode_t ext2fs_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
154 const char *name, ext2_ino_t *inode)
159 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
161 retval = ext2fs_get_mem(fs->blocksize, &buf);
165 retval = open_namei(fs, root, cwd, name, strlen(name), 0, 0,
168 ext2fs_free_mem(&buf);
172 errcode_t ext2fs_namei_follow(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
173 const char *name, ext2_ino_t *inode)
178 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
180 retval = ext2fs_get_mem(fs->blocksize, &buf);
184 retval = open_namei(fs, root, cwd, name, strlen(name), 1, 0,
187 ext2fs_free_mem(&buf);
191 errcode_t ext2fs_follow_link(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
192 ext2_ino_t inode, ext2_ino_t *res_inode)
197 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
199 retval = ext2fs_get_mem(fs->blocksize, &buf);
203 retval = follow_link(fs, root, cwd, inode, 0, buf, res_inode);
205 ext2fs_free_mem(&buf);