-
Notifications
You must be signed in to change notification settings - Fork 69
/
dir.c
324 lines (253 loc) · 8.38 KB
/
dir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Copyright (c) 2012 Dave Vasilevsky <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "dir.h"
#include "fs.h"
#include "swap.h"
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
/* Read some directory metadata, updating the dir structure as necessary */
static sqfs_err sqfs_dir_md_read(sqfs *fs, sqfs_dir *dir, void *buf,
size_t size);
/* Fast forwards to a directory header. */
typedef sqfs_err sqfs_dir_header_f(sqfs *fs, sqfs_md_cursor *cur,
struct squashfs_dir_index *index, bool *stop, void *arg);
static sqfs_err sqfs_dir_ff_header(sqfs *fs, sqfs_inode *inode, sqfs_dir *dir,
sqfs_dir_header_f func, void *arg);
/* Fast forward a directory to the given offset. Return error if it doesn't
exist. */
static sqfs_err sqfs_dir_ff_offset(sqfs *fs, sqfs_inode *inode, sqfs_dir *dir,
sqfs_off_t offset);
static sqfs_err sqfs_dir_md_read(sqfs *fs, sqfs_dir *dir, void *buf,
size_t size) {
dir->offset += size;
return sqfs_md_read(fs, &dir->cur, buf, size);
}
sqfs_err sqfs_dir_open(sqfs *fs, sqfs_inode *inode, sqfs_dir *dir,
off_t offset) {
if (!S_ISDIR(inode->base.mode))
return SQFS_ERR;
memset(dir, 0, sizeof(*dir));
dir->cur.block = inode->xtra.dir.start_block +
fs->sb.directory_table_start;
dir->cur.offset = inode->xtra.dir.offset;
dir->offset = 0;
dir->total = inode->xtra.dir.dir_size <= 3 ? 0 :
inode->xtra.dir.dir_size - 3;
if (offset) {
/* Fast forward to the given offset */
sqfs_err err = sqfs_dir_ff_offset(fs, inode, dir, offset);
if (err)
return err;
}
return SQFS_OK;
}
void sqfs_dentry_init(sqfs_dir_entry *entry, char *namebuf) {
entry->name = namebuf;
}
sqfs_off_t sqfs_dentry_offset(sqfs_dir_entry *entry) {
return entry->offset;
}
sqfs_off_t sqfs_dentry_next_offset(sqfs_dir_entry *entry) {
return entry->next_offset;
}
int sqfs_dentry_type(sqfs_dir_entry *entry) {
return entry->type;
}
sqfs_mode_t sqfs_dentry_mode(sqfs_dir_entry *entry) {
return sqfs_mode(sqfs_dentry_type(entry));
}
sqfs_inode_id sqfs_dentry_inode(sqfs_dir_entry *entry) {
return entry->inode;
}
sqfs_inode_num sqfs_dentry_inode_num(sqfs_dir_entry *entry) {
return entry->inode_number;
}
size_t sqfs_dentry_name_size(sqfs_dir_entry *entry) {
return entry->name_size;
}
const char *sqfs_dentry_name(sqfs_dir_entry *entry) {
if (!entry->name)
return NULL;
entry->name[sqfs_dentry_name_size(entry)] = '\0';
return entry->name;
}
bool sqfs_dentry_is_dir(sqfs_dir_entry *entry) {
return S_ISDIR(sqfs_dentry_mode(entry));
}
bool sqfs_dir_next(sqfs *fs, sqfs_dir *dir, sqfs_dir_entry *entry,
sqfs_err *err) {
struct squashfs_dir_entry e;
*err = SQFS_OK;
entry->offset = dir->offset;
while (dir->header.count == 0) {
if (dir->offset >= dir->total)
return false;
if ((*err = sqfs_dir_md_read(fs, dir, &dir->header, sizeof(dir->header))))
return false;
sqfs_swapin_dir_header(&dir->header);
++(dir->header.count); /* biased by one */
}
if ((*err = sqfs_dir_md_read(fs, dir, &e, sizeof(e))))
return false;
sqfs_swapin_dir_entry(&e);
--(dir->header.count);
entry->type = e.type;
entry->name_size = e.size + 1;
entry->inode = ((uint64_t)dir->header.start_block << 16) + e.offset;
/* e.inode_number is signed */
entry->inode_number = dir->header.inode_number + (int16_t)e.inode_number;
*err = sqfs_dir_md_read(fs, dir, entry->name, sqfs_dentry_name_size(entry));
if (*err)
return false;
entry->next_offset = dir->offset;
return true;
}
static sqfs_err sqfs_dir_ff_header(sqfs *fs, sqfs_inode *inode,
sqfs_dir *dir, sqfs_dir_header_f func, void *arg) {
struct squashfs_dir_index idx;
sqfs_md_cursor cur = inode->next;
size_t count = inode->xtra.dir.idx_count;
if (count == 0)
return SQFS_OK;
while (count--) {
sqfs_err err;
bool stop = 0;
if ((err = sqfs_md_read(fs, &cur, &idx, sizeof(idx))))
return err;
sqfs_swapin_dir_index(&idx);
if ((err = func(fs, &cur, &idx, &stop, arg)))
return err;
if (stop)
break;
dir->cur.block = idx.start_block + fs->sb.directory_table_start;
dir->offset = idx.index;
}
dir->cur.offset = (dir->cur.offset + dir->offset) % SQUASHFS_METADATA_SIZE;
return SQFS_OK;
}
/* Helper for sqfs_dir_ff_offset */
static sqfs_err sqfs_dir_ff_offset_f(sqfs *fs, sqfs_md_cursor *cur,
struct squashfs_dir_index *index, bool *stop, void *arg) {
sqfs_off_t offset = *(sqfs_off_t*)arg;
if (index->index >= offset) {
*stop = true;
return SQFS_OK;
}
return sqfs_md_read(fs, cur, NULL, index->size + 1); /* skip name */
}
static sqfs_err sqfs_dir_ff_offset(sqfs *fs, sqfs_inode *inode, sqfs_dir *dir,
sqfs_off_t offset) {
sqfs_err err;
sqfs_dir_entry entry;
err = sqfs_dir_ff_header(fs, inode, dir, sqfs_dir_ff_offset_f, &offset);
if (err)
return err;
sqfs_dentry_init(&entry, NULL);
while (dir->offset < offset && sqfs_dir_next(fs, dir, &entry, &err))
; /* pass */
if (err)
return err;
return dir->offset == offset ? SQFS_OK : SQFS_ERR;
}
/* Helper for sqfs_dir_lookup */
typedef struct {
const char *cmp;
size_t cmplen;
char *name;
} sqfs_dir_ff_name_t;
static sqfs_err sqfs_dir_ff_name_f(sqfs *fs, sqfs_md_cursor *cur,
struct squashfs_dir_index *index, bool *stop, void *arg) {
sqfs_err err;
sqfs_dir_ff_name_t *args = (sqfs_dir_ff_name_t*)arg;
size_t name_size = index->size + 1;
if ((err = sqfs_md_read(fs, cur, args->name, name_size)))
return err;
args->name[name_size] = '\0';
int order = strncmp(args->name, args->cmp, args->cmplen);
if (order > 0 || (order == 0 && name_size > args->cmplen))
*stop = true;
return SQFS_OK;
}
sqfs_err sqfs_dir_lookup(sqfs *fs, sqfs_inode *inode,
const char *name, size_t namelen, sqfs_dir_entry *entry, bool *found) {
sqfs_err err;
sqfs_dir dir;
sqfs_dir_ff_name_t arg;
*found = false;
if ((err = sqfs_dir_open(fs, inode, &dir, 0)))
return err;
/* Fast forward to header */
arg.cmp = name;
arg.cmplen = namelen;
arg.name = entry->name;
if ((err = sqfs_dir_ff_header(fs, inode, &dir, sqfs_dir_ff_name_f, &arg)))
return err;
/* Iterate to find the right entry */
while (sqfs_dir_next(fs, &dir, entry, &err)) {
int order = strncmp(sqfs_dentry_name(entry), name, namelen);
if (order == 0 && sqfs_dentry_name_size(entry) == namelen)
*found = true;
if (order >= 0)
break;
}
return err;
}
sqfs_err sqfs_lookup_path_with_id(sqfs *fs, sqfs_inode *inode, const char *path,
bool *found, sqfs_inode_id *id) {
sqfs_err err;
sqfs_name buf;
sqfs_dir_entry entry;
*found = false;
sqfs_dentry_init(&entry, buf);
while (*path) {
const char *name;
size_t size;
bool dfound;
/* Find next path component */
while (*path == '/') /* skip leading slashes */
++path;
name = path;
while (*path && *path != '/')
++path;
size = path - name;
if (size == 0) /* we're done */
break;
if ((err = sqfs_dir_lookup(fs, inode, name, size, &entry, &dfound)))
return err;
if (!dfound)
return SQFS_OK; /* not found */
if (id)
*id = sqfs_dentry_inode(&entry);
if ((err = sqfs_inode_get(fs, inode, sqfs_dentry_inode(&entry))))
return err;
}
*found = true;
return SQFS_OK;
}
sqfs_err sqfs_lookup_path(sqfs *fs, sqfs_inode *inode, const char *path,
bool *found) {
return sqfs_lookup_path_with_id(fs, inode, path, found, NULL);
};