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.
18 /* #define NAMEI_DEBUG */
23 static errcode_t open_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t base,
24 const char *pathname, size_t pathlen, int follow,
25 int link_count, char *buf, ext2_ino_t *res_inode);
27 static errcode_t follow_link(ext2_filsys fs, ext2_ino_t root, ext2_ino_t dir,
28 ext2_ino_t inode, int link_count,
29 char *buf, ext2_ino_t *res_inode)
37 printf("follow_link: root=%lu, dir=%lu, inode=%lu, lc=%d\n",
38 root, dir, inode, link_count);
41 retval = ext2fs_read_inode (fs, inode, &ei);
42 if (retval) return retval;
43 if (!LINUX_S_ISLNK (ei.i_mode)) {
47 if (link_count++ > 5) {
48 return EXT2_ET_SYMLINK_LOOP;
50 /* FIXME-64: Actually, this is FIXME EXTENTS */
51 if (ext2fs_inode_data_blocks(fs,&ei)) {
52 retval = ext2fs_get_mem(fs->blocksize, &buffer);
55 retval = io_channel_read_blk(fs->io, ei.i_block[0], 1, buffer);
57 ext2fs_free_mem(&buffer);
62 pathname = (char *)&(ei.i_block[0]);
63 retval = open_namei(fs, root, dir, pathname, ei.i_size, 1,
64 link_count, buf, res_inode);
66 ext2fs_free_mem(&buffer);
71 * This routine interprets a pathname in the context of the current
72 * directory and the root directory, and returns the inode of the
73 * containing directory, and a pointer to the filename of the file
74 * (pointing into the pathname) and the length of the filename.
76 static errcode_t dir_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t dir,
77 const char *pathname, int pathlen,
78 int link_count, char *buf,
79 const char **name, int *namelen,
80 ext2_ino_t *res_inode)
88 if ((c = *pathname) == '/') {
95 for (len=0; --pathlen >= 0;len++) {
102 retval = ext2fs_lookup (fs, dir, thisname, len, buf, &inode);
103 if (retval) return retval;
104 retval = follow_link (fs, root, dir, inode,
105 link_count, buf, &dir);
106 if (retval) return retval;
114 static errcode_t open_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t base,
115 const char *pathname, size_t pathlen, int follow,
116 int link_count, char *buf, ext2_ino_t *res_inode)
118 const char *base_name;
120 ext2_ino_t dir, inode;
124 printf("open_namei: root=%lu, dir=%lu, path=%*s, lc=%d\n",
125 root, base, pathlen, pathname, link_count);
127 retval = dir_namei(fs, root, base, pathname, pathlen,
128 link_count, buf, &base_name, &namelen, &dir);
129 if (retval) return retval;
130 if (!namelen) { /* special case: '/usr/' etc */
134 retval = ext2fs_lookup (fs, dir, base_name, namelen, buf, &inode);
138 retval = follow_link(fs, root, dir, inode, link_count,
144 printf("open_namei: (link_count=%d) returns %lu\n",
151 errcode_t ext2fs_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
152 const char *name, ext2_ino_t *inode)
157 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
159 retval = ext2fs_get_mem(fs->blocksize, &buf);
163 retval = open_namei(fs, root, cwd, name, strlen(name), 0, 0,
166 ext2fs_free_mem(&buf);
170 errcode_t ext2fs_namei_follow(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
171 const char *name, ext2_ino_t *inode)
176 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
178 retval = ext2fs_get_mem(fs->blocksize, &buf);
182 retval = open_namei(fs, root, cwd, name, strlen(name), 1, 0,
185 ext2fs_free_mem(&buf);
189 errcode_t ext2fs_follow_link(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
190 ext2_ino_t inode, ext2_ino_t *res_inode)
195 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
197 retval = ext2fs_get_mem(fs->blocksize, &buf);
201 retval = follow_link(fs, root, cwd, inode, 0, buf, res_inode);
203 ext2fs_free_mem(&buf);