blob: 6a48c50592c1edce32a1d158222bfc79ac21a36a [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2007 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <string.h>
24#include <stdlib.h>
25#include <debug.h>
26#include <trace.h>
27#include <err.h>
28#include "ext2_priv.h"
29
30#define LOCAL_TRACE 0
31
32/* read in the dir, look for the entry */
33static int ext2_dir_lookup(ext2_t *ext2, struct ext2_inode *dir_inode, const char *name, inodenum_t *inum)
34{
35 uint file_blocknum;
36 int err;
37 uint8_t *buf;
38 size_t namelen = strlen(name);
39
40 if (!S_ISDIR(dir_inode->i_mode))
41 return ERR_NOT_DIR;
42
43 buf = malloc(EXT2_BLOCK_SIZE(ext2->sb));
44
45 file_blocknum = 0;
46 for (;;) {
47 /* read in the offset */
48 err = ext2_read_inode(ext2, dir_inode, buf, file_blocknum * EXT2_BLOCK_SIZE(ext2->sb), EXT2_BLOCK_SIZE(ext2->sb));
49 if (err <= 0) {
50 free(buf);
51 return -1;
52 }
53
54 /* walk through the directory entries, looking for the one that matches */
55 struct ext2_dir_entry_2 *ent;
56 uint pos = 0;
57 while (pos < EXT2_BLOCK_SIZE(ext2->sb)) {
58 ent = (struct ext2_dir_entry_2 *)&buf[pos];
59
60 LTRACEF("ent %d:%d: inode 0x%x, reclen %d, namelen %d\n",
61 file_blocknum, pos, LE32(ent->inode), LE16(ent->rec_len), ent->name_len/* , ent->name*/);
62
63 /* sanity check the record length */
64 if (LE16(ent->rec_len) == 0)
65 break;
66
67 if (ent->name_len == namelen && memcmp(name, ent->name, ent->name_len) == 0) {
68 // match
69 *inum = LE32(ent->inode);
70 LTRACEF("match: inode %d\n", *inum);
71 free(buf);
72 return 1;
73 }
74
75 pos += ROUNDUP(LE16(ent->rec_len), 4);
76 }
77
78 file_blocknum++;
79
80 /* sanity check the directory. 4MB should be enough */
81 if (file_blocknum > 1024) {
82 free(buf);
83 return -1;
84 }
85 }
86}
87
88/* note, trashes path */
89static int ext2_walk(ext2_t *ext2, char *path, struct ext2_inode *start_inode, inodenum_t *inum, int recurse)
90{
91 char *ptr;
92 struct ext2_inode inode;
93 struct ext2_inode dir_inode;
94 int err;
95 bool done;
96
97 LTRACEF("path '%s', start_inode %p, inum %p, recurse %d\n", path, start_inode, inum, recurse);
98
99 if (recurse > 4)
100 return ERR_RECURSE_TOO_DEEP;
101
102 /* chew up leading slashes */
103 ptr = &path[0];
104 while (*ptr == '/')
105 ptr++;
106
107 done = false;
108 memcpy(&dir_inode, start_inode, sizeof(struct ext2_inode));
109 while (!done) {
110 /* process the first component */
111 char *next_sep = strchr(ptr, '/');
112 if (next_sep) {
113 /* terminate the next component, giving us a substring */
114 *next_sep = 0;
115 } else {
116 /* this is the last component */
117 done = true;
118 }
119
120 LTRACEF("component '%s', done %d\n", ptr, done);
121
122 /* do the lookup on this component */
123 err = ext2_dir_lookup(ext2, &dir_inode, ptr, inum);
124 if (err < 0)
125 return err;
126
127nextcomponent:
128 LTRACEF("inum %u\n", *inum);
129
130 /* load the next inode */
131 err = ext2_load_inode(ext2, *inum, &inode);
132 if (err < 0)
133 return err;
134
135 /* is it a symlink? */
136 if (S_ISLNK(inode.i_mode)) {
137 char link[512];
138
139 LTRACEF("hit symlink\n");
140
141 err = ext2_read_link(ext2, &inode, link, sizeof(link));
142 if (err < 0)
143 return err;
144
145 LTRACEF("symlink read returns %d '%s'\n", err, link);
146
147 /* recurse, parsing the link */
148 if (link[0] == '/') {
149 /* link starts with '/', so start over again at the rootfs */
150 err = ext2_walk(ext2, link, &ext2->root_inode, inum, recurse + 1);
151 } else {
152 err = ext2_walk(ext2, link, &dir_inode, inum, recurse + 1);
153 }
154
155 LTRACEF("recursive walk returns %d\n", err);
156
157 if (err < 0)
158 return err;
159
160 /* if we weren't done with our path parsing, start again with the result of this recurse */
161 if (!done) {
162 goto nextcomponent;
163 }
164 } else if (S_ISDIR(inode.i_mode)) {
165 /* for the next cycle, point the dir inode at our new directory */
166 memcpy(&dir_inode, &inode, sizeof(struct ext2_inode));
167 } else {
168 if (!done) {
169 /* we aren't done and this walked over a nondir, abort */
170 LTRACEF("not finished and component is nondir\n");
171 return ERR_NOT_FOUND;
172 }
173 }
174
175 if (!done) {
176 /* move to the next seperator */
177 ptr = next_sep + 1;
178
179 /* consume multiple seperators */
180 while (*ptr == '/')
181 ptr++;
182 }
183 }
184
185 return 0;
186}
187
188/* do a path parse, looking up each component */
189int ext2_lookup(ext2_t *ext2, const char *_path, inodenum_t *inum)
190{
191 LTRACEF("path '%s', inum %p\n", _path, inum);
192
193 char path[512];
194 strlcpy(path, _path, sizeof(path));
195
196 return ext2_walk(ext2, path, &ext2->root_inode, inum, 1);
197}
198