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 Public
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 if (ext2fs_inode_data_blocks(fs,&ei)) {
51 retval = ext2fs_get_mem(fs->blocksize, &buffer);
54 retval = io_channel_read_blk(fs->io, ei.i_block[0], 1, buffer);
56 ext2fs_free_mem(&buffer);
61 pathname = (char *)&(ei.i_block[0]);
62 retval = open_namei(fs, root, dir, pathname, ei.i_size, 1,
63 link_count, buf, res_inode);
65 ext2fs_free_mem(&buffer);
70 * This routine interprets a pathname in the context of the current
71 * directory and the root directory, and returns the inode of the
72 * containing directory, and a pointer to the filename of the file
73 * (pointing into the pathname) and the length of the filename.
75 static errcode_t dir_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t dir,
76 const char *pathname, int pathlen,
77 int link_count, char *buf,
78 const char **name, int *namelen,
79 ext2_ino_t *res_inode)
87 if ((c = *pathname) == '/') {
94 for (len=0; --pathlen >= 0;len++) {
101 retval = ext2fs_lookup (fs, dir, thisname, len, buf, &inode);
102 if (retval) return retval;
103 retval = follow_link (fs, root, dir, inode,
104 link_count, buf, &dir);
105 if (retval) return retval;
113 static errcode_t open_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t base,
114 const char *pathname, size_t pathlen, int follow,
115 int link_count, char *buf, ext2_ino_t *res_inode)
117 const char *basename;
119 ext2_ino_t dir, inode;
123 printf("open_namei: root=%lu, dir=%lu, path=%*s, lc=%d\n",
124 root, base, pathlen, pathname, link_count);
126 retval = dir_namei(fs, root, base, pathname, pathlen,
127 link_count, buf, &basename, &namelen, &dir);
128 if (retval) return retval;
129 if (!namelen) { /* special case: '/usr/' etc */
133 retval = ext2fs_lookup (fs, dir, basename, namelen, buf, &inode);
137 retval = follow_link(fs, root, dir, inode, link_count,
143 printf("open_namei: (link_count=%d) returns %lu\n",
150 errcode_t ext2fs_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
151 const char *name, ext2_ino_t *inode)
156 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
158 retval = ext2fs_get_mem(fs->blocksize, &buf);
162 retval = open_namei(fs, root, cwd, name, strlen(name), 0, 0,
165 ext2fs_free_mem(&buf);
169 errcode_t ext2fs_namei_follow(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
170 const char *name, ext2_ino_t *inode)
175 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
177 retval = ext2fs_get_mem(fs->blocksize, &buf);
181 retval = open_namei(fs, root, cwd, name, strlen(name), 1, 0,
184 ext2fs_free_mem(&buf);
188 errcode_t ext2fs_follow_link(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
189 ext2_ino_t inode, ext2_ino_t *res_inode)
194 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
196 retval = ext2fs_get_mem(fs->blocksize, &buf);
200 retval = follow_link(fs, root, cwd, inode, 0, buf, res_inode);
202 ext2fs_free_mem(&buf);