• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * namei.c --- ext2fs directory lookup operations
3  *
4  * Copyright (C) 1993, 1994, 1994, 1995 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11 
12 #include "config.h"
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 
19 /* #define NAMEI_DEBUG */
20 
21 #include "ext2_fs.h"
22 #include "ext2fs.h"
23 #include "ext2fsP.h"
24 
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);
28 
follow_link(ext2_filsys fs,ext2_ino_t root,ext2_ino_t dir,ext2_ino_t inode,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)
32 {
33 	char *pathname;
34 	char *buffer = 0;
35 	errcode_t retval;
36 	struct ext2_inode ei;
37 	blk64_t blk;
38 
39 #ifdef NAMEI_DEBUG
40 	printf("follow_link: root=%lu, dir=%lu, inode=%lu, lc=%d\n",
41 	       root, dir, inode, link_count);
42 
43 #endif
44 	retval = ext2fs_read_inode (fs, inode, &ei);
45 	if (retval) return retval;
46 	if (!LINUX_S_ISLNK (ei.i_mode)) {
47 		*res_inode = inode;
48 		return 0;
49 	}
50 	if (link_count++ >= EXT2FS_MAX_NESTED_LINKS)
51 		return EXT2_ET_SYMLINK_LOOP;
52 
53 	if (ext2fs_is_fast_symlink(&ei))
54 		pathname = (char *)&(ei.i_block[0]);
55 	else if (ei.i_flags & EXT4_INLINE_DATA_FL) {
56 		retval = ext2fs_get_memzero(ei.i_size, &buffer);
57 		if (retval)
58 			return retval;
59 
60 		retval = ext2fs_inline_data_get(fs, inode,
61 						&ei, buffer, NULL);
62 		if (retval) {
63 			ext2fs_free_mem(&buffer);
64 			return retval;
65 		}
66 		pathname = buffer;
67 	} else {
68 		retval = ext2fs_bmap2(fs, inode, &ei, NULL, 0, 0, NULL, &blk);
69 		if (retval)
70 			return retval;
71 
72 		retval = ext2fs_get_mem(fs->blocksize, &buffer);
73 		if (retval)
74 			return retval;
75 
76 		retval = io_channel_read_blk64(fs->io, blk, 1, buffer);
77 		if (retval) {
78 			ext2fs_free_mem(&buffer);
79 			return retval;
80 		}
81 		pathname = buffer;
82 	}
83 
84 	retval = open_namei(fs, root, dir, pathname, ei.i_size, 1,
85 			    link_count, buf, res_inode);
86 	if (buffer)
87 		ext2fs_free_mem(&buffer);
88 	return retval;
89 }
90 
91 /*
92  * This routine interprets a pathname in the context of the current
93  * directory and the root directory, and returns the inode of the
94  * containing directory, and a pointer to the filename of the file
95  * (pointing into the pathname) and the length of the filename.
96  */
dir_namei(ext2_filsys fs,ext2_ino_t root,ext2_ino_t dir,const char * pathname,int pathlen,int link_count,char * buf,const char ** name,int * namelen,ext2_ino_t * res_inode)97 static errcode_t dir_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t dir,
98 			   const char *pathname, int pathlen,
99 			   int link_count, char *buf,
100 			   const char **name, int *namelen,
101 			   ext2_ino_t *res_inode)
102 {
103 	char c;
104 	const char *thisname;
105 	int len;
106 	ext2_ino_t inode;
107 	errcode_t retval;
108 
109 	if ((c = *pathname) == '/') {
110         	dir = root;
111 		pathname++;
112 		pathlen--;
113 	}
114 	while (1) {
115         	thisname = pathname;
116 		for (len=0; --pathlen >= 0;len++) {
117 			c = *(pathname++);
118 			if (c == '/')
119 				break;
120 		}
121 		if (pathlen < 0)
122 			break;
123 		retval = ext2fs_lookup (fs, dir, thisname, len, buf, &inode);
124 		if (retval) return retval;
125         	retval = follow_link (fs, root, dir, inode,
126 				      link_count, buf, &dir);
127         	if (retval) return retval;
128     	}
129 	*name = thisname;
130 	*namelen = len;
131 	*res_inode = dir;
132 	return 0;
133 }
134 
open_namei(ext2_filsys fs,ext2_ino_t root,ext2_ino_t base,const char * pathname,size_t pathlen,int follow,int link_count,char * buf,ext2_ino_t * res_inode)135 static errcode_t open_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t base,
136 			    const char *pathname, size_t pathlen, int follow,
137 			    int link_count, char *buf, ext2_ino_t *res_inode)
138 {
139 	const char *base_name;
140 	int namelen;
141 	ext2_ino_t dir, inode;
142 	errcode_t retval;
143 
144 #ifdef NAMEI_DEBUG
145 	printf("open_namei: root=%lu, dir=%lu, path=%.*s, lc=%d\n",
146 	       root, base, pathlen, pathname, link_count);
147 #endif
148 	retval = dir_namei(fs, root, base, pathname, pathlen,
149 			   link_count, buf, &base_name, &namelen, &dir);
150 	if (retval) return retval;
151 	if (!namelen) {                     /* special case: '/usr/' etc */
152 		*res_inode=dir;
153 		return 0;
154 	}
155 	retval = ext2fs_lookup (fs, dir, base_name, namelen, buf, &inode);
156 	if (retval)
157 		return retval;
158 	if (follow) {
159 		retval = follow_link(fs, root, dir, inode, link_count,
160 				     buf, &inode);
161 		if (retval)
162 			return retval;
163 	}
164 #ifdef NAMEI_DEBUG
165 	printf("open_namei: (link_count=%d) returns %lu\n",
166 	       link_count, inode);
167 #endif
168 	*res_inode = inode;
169 	return 0;
170 }
171 
ext2fs_namei(ext2_filsys fs,ext2_ino_t root,ext2_ino_t cwd,const char * name,ext2_ino_t * inode)172 errcode_t ext2fs_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
173 		       const char *name, ext2_ino_t *inode)
174 {
175 	char *buf;
176 	errcode_t retval;
177 
178 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
179 
180 	retval = ext2fs_get_mem(fs->blocksize, &buf);
181 	if (retval)
182 		return retval;
183 
184 	retval = open_namei(fs, root, cwd, name, strlen(name), 0, 0,
185 			    buf, inode);
186 
187 	ext2fs_free_mem(&buf);
188 	return retval;
189 }
190 
ext2fs_namei_follow(ext2_filsys fs,ext2_ino_t root,ext2_ino_t cwd,const char * name,ext2_ino_t * inode)191 errcode_t ext2fs_namei_follow(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
192 			      const char *name, ext2_ino_t *inode)
193 {
194 	char *buf;
195 	errcode_t retval;
196 
197 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
198 
199 	retval = ext2fs_get_mem(fs->blocksize, &buf);
200 	if (retval)
201 		return retval;
202 
203 	retval = open_namei(fs, root, cwd, name, strlen(name), 1, 0,
204 			    buf, inode);
205 
206 	ext2fs_free_mem(&buf);
207 	return retval;
208 }
209 
ext2fs_follow_link(ext2_filsys fs,ext2_ino_t root,ext2_ino_t cwd,ext2_ino_t inode,ext2_ino_t * res_inode)210 errcode_t ext2fs_follow_link(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
211 			ext2_ino_t inode, ext2_ino_t *res_inode)
212 {
213 	char *buf;
214 	errcode_t retval;
215 
216 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
217 
218 	retval = ext2fs_get_mem(fs->blocksize, &buf);
219 	if (retval)
220 		return retval;
221 
222 	retval = follow_link(fs, root, cwd, inode, 0, buf, res_inode);
223 
224 	ext2fs_free_mem(&buf);
225 	return retval;
226 }
227 
228