forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 12
/
fake.c
419 lines (384 loc) · 11.6 KB
/
fake.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <stdarg.h>
#include <limits.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sqlite3.h>
#include "debug.h"
#include "kernel/errno.h"
#include "kernel/task.h"
#include "fs/fd.h"
#include "fs/dev.h"
#include "fs/inode.h"
#include "fs/real.h"
#define ISH_INTERNAL
#include "fs/fake.h"
// TODO document database
// this exists only to override readdir to fix the returned inode numbers
static struct fd_ops fakefs_fdops;
static struct fd *fakefs_open(struct mount *mount, const char *path, int flags, int mode) {
struct fakefs_db *fs = &mount->fakefs;
struct fd *fd = realfs.open(mount, path, flags, 0666);
if (IS_ERR(fd))
return fd;
db_begin(fs);
fd->fake_inode = path_get_inode(fs, path);
if (flags & O_CREAT_) {
struct ish_stat ishstat;
ishstat.mode = mode | S_IFREG;
ishstat.uid = current->euid;
ishstat.gid = current->egid;
ishstat.rdev = 0;
if (fd->fake_inode == 0) {
path_create(fs, path, &ishstat);
fd->fake_inode = path_get_inode(fs, path);
}
}
db_commit(fs);
if (fd->fake_inode == 0) {
// metadata for this file is missing
// TODO unlink the real file
fd_close(fd);
return ERR_PTR(_ENOENT);
}
fd->ops = &fakefs_fdops;
return fd;
}
// WARNING: giant hack, just for file providerws
struct fd *fakefs_open_inode(struct mount *mount, ino_t inode) {
struct fakefs_db *fs = &mount->fakefs;
db_begin(fs);
sqlite3_stmt *stmt = fs->stmt.path_from_inode;
sqlite3_bind_int64(stmt, 1, inode);
step:
if (!db_exec(fs, stmt)) {
db_reset(fs, stmt);
db_rollback(fs);
return ERR_PTR(_ENOENT);
}
const char *path = (const char *) sqlite3_column_text(stmt, 0);
struct fd *fd = realfs.open(mount, path, O_RDWR_, 0);
if (PTR_ERR(fd) == _EISDIR)
fd = realfs.open(mount, path, O_RDONLY_, 0);
if (PTR_ERR(fd) == _ENOENT)
goto step;
db_reset(fs, stmt);
db_commit(fs);
fd->fake_inode = inode;
fd->ops = &fakefs_fdops;
return fd;
}
static int fakefs_link(struct mount *mount, const char *src, const char *dst) {
struct fakefs_db *fs = &mount->fakefs;
db_begin(fs);
int err = realfs.link(mount, src, dst);
if (err < 0) {
db_rollback(fs);
return err;
}
path_link(fs, src, dst);
db_commit(fs);
return 0;
}
static int fakefs_unlink(struct mount *mount, const char *path) {
struct fakefs_db *fs = &mount->fakefs;
db_begin(fs);
int err = realfs.unlink(mount, path);
if (err < 0) {
db_rollback(fs);
return err;
}
ino_t ino = path_unlink(fs, path);
db_commit(fs);
inode_check_orphaned(mount, ino);
return 0;
}
static int fakefs_rmdir(struct mount *mount, const char *path) {
struct fakefs_db *fs = &mount->fakefs;
db_begin(fs);
int err = realfs.rmdir(mount, path);
if (err < 0) {
db_rollback(fs);
return err;
}
ino_t ino = path_unlink(fs, path);
db_commit(fs);
inode_check_orphaned(mount, ino);
return 0;
}
static int fakefs_rename(struct mount *mount, const char *src, const char *dst) {
struct fakefs_db *fs = &mount->fakefs;
db_begin(fs);
path_rename(fs, src, dst);
int err = realfs.rename(mount, src, dst);
if (err < 0) {
db_rollback(fs);
return err;
}
db_commit(fs);
return 0;
}
static int fakefs_symlink(struct mount *mount, const char *target, const char *link) {
struct fakefs_db *fs = &mount->fakefs;
db_begin(fs);
// create a file containing the target
int fd = openat(mount->root_fd, fix_path(link), O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd < 0) {
db_rollback(fs);
return errno_map();
}
ssize_t res = write(fd, target, strlen(target));
close(fd);
if (res < 0) {
int saved_errno = errno;
unlinkat(mount->root_fd, fix_path(link), 0);
db_rollback(fs);
errno = saved_errno;
return errno_map();
}
// customize the stat info so it looks like a link
struct ish_stat ishstat;
ishstat.mode = S_IFLNK | 0777; // symlinks always have full permissions
ishstat.uid = current->euid;
ishstat.gid = current->egid;
ishstat.rdev = 0;
path_create(fs, link, &ishstat);
db_commit(fs);
return 0;
}
static int fakefs_mknod(struct mount *mount, const char *path, mode_t_ mode, dev_t_ dev) {
struct fakefs_db *fs = &mount->fakefs;
mode_t_ real_mode = 0666;
if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
real_mode |= S_IFREG;
else
real_mode |= mode & S_IFMT;
db_begin(fs);
int err = realfs.mknod(mount, path, real_mode, 0);
if (err < 0) {
db_rollback(fs);
return err;
}
struct ish_stat stat;
stat.mode = mode;
stat.uid = current->euid;
stat.gid = current->egid;
stat.rdev = 0;
if (S_ISBLK(mode) || S_ISCHR(mode))
stat.rdev = dev;
path_create(fs, path, &stat);
db_commit(fs);
return err;
}
static int fakefs_stat(struct mount *mount, const char *path, struct statbuf *fake_stat) {
struct fakefs_db *fs = &mount->fakefs;
db_begin(fs);
struct ish_stat ishstat;
ino_t inode;
if (!path_read_stat(fs, path, &ishstat, &inode)) {
db_rollback(fs);
return _ENOENT;
}
int err = realfs.stat(mount, path, fake_stat);
db_commit(fs);
if (err < 0)
return err;
fake_stat->inode = inode;
fake_stat->mode = ishstat.mode;
fake_stat->uid = ishstat.uid;
fake_stat->gid = ishstat.gid;
fake_stat->rdev = ishstat.rdev;
return 0;
}
static int fakefs_fstat(struct fd *fd, struct statbuf *fake_stat) {
struct fakefs_db *fs = &fd->mount->fakefs;
int err = realfs.fstat(fd, fake_stat);
if (err < 0)
return err;
db_begin(fs);
struct ish_stat ishstat;
inode_read_stat(fs, fd->fake_inode, &ishstat);
db_commit(fs);
fake_stat->inode = fd->fake_inode;
fake_stat->mode = ishstat.mode;
fake_stat->uid = ishstat.uid;
fake_stat->gid = ishstat.gid;
fake_stat->rdev = ishstat.rdev;
return 0;
}
static void fake_stat_setattr(struct ish_stat *ishstat, struct attr attr) {
switch (attr.type) {
case attr_uid:
ishstat->uid = attr.uid;
break;
case attr_gid:
ishstat->gid = attr.gid;
break;
case attr_mode:
ishstat->mode = (ishstat->mode & S_IFMT) | (attr.mode & ~S_IFMT);
break;
}
}
static int fakefs_setattr(struct mount *mount, const char *path, struct attr attr) {
struct fakefs_db *fs = &mount->fakefs;
if (attr.type == attr_size)
return realfs.setattr(mount, path, attr);
db_begin(fs);
struct ish_stat ishstat;
ino_t inode;
if (!path_read_stat(fs, path, &ishstat, &inode)) {
db_rollback(fs);
return _ENOENT;
}
fake_stat_setattr(&ishstat, attr);
inode_write_stat(fs, inode, &ishstat);
db_commit(fs);
return 0;
}
static int fakefs_fsetattr(struct fd *fd, struct attr attr) {
struct fakefs_db *fs = &fd->mount->fakefs;
if (attr.type == attr_size)
return realfs.fsetattr(fd, attr);
db_begin(fs);
struct ish_stat ishstat;
inode_read_stat(fs, fd->fake_inode, &ishstat);
fake_stat_setattr(&ishstat, attr);
inode_write_stat(fs, fd->fake_inode, &ishstat);
db_commit(fs);
return 0;
}
static int fakefs_mkdir(struct mount *mount, const char *path, mode_t_ mode) {
struct fakefs_db *fs = &mount->fakefs;
db_begin(fs);
int err = realfs.mkdir(mount, path, 0777);
if (err < 0) {
db_rollback(fs);
return err;
}
struct ish_stat ishstat;
ishstat.mode = mode | S_IFDIR;
ishstat.uid = current->euid;
ishstat.gid = current->egid;
ishstat.rdev = 0;
path_create(fs, path, &ishstat);
db_commit(fs);
return 0;
}
static ssize_t file_readlink(struct mount *mount, const char *path, char *buf, size_t bufsize) {
// broken symlinks can't be included in an iOS app or else Xcode craps out
int fd = openat(mount->root_fd, fix_path(path), O_RDONLY);
if (fd < 0)
return errno_map();
int err = read(fd, buf, bufsize);
close(fd);
if (err < 0)
return errno_map();
return err;
}
static ssize_t fakefs_readlink(struct mount *mount, const char *path, char *buf, size_t bufsize) {
struct fakefs_db *fs = &mount->fakefs;
db_begin(fs);
struct ish_stat ishstat;
if (!path_read_stat(fs, path, &ishstat, NULL)) {
db_rollback(fs);
return _ENOENT;
}
if (!S_ISLNK(ishstat.mode)) {
db_rollback(fs);
return _EINVAL;
}
ssize_t err = realfs.readlink(mount, path, buf, bufsize);
if (err == _EINVAL)
err = file_readlink(mount, path, buf, bufsize);
db_commit(fs);
return err;
}
static int fakefs_readdir(struct fd *fd, struct dir_entry *entry) {
assert(fd->ops == &fakefs_fdops);
int res;
retry:
res = realfs_fdops.readdir(fd, entry);
if (res <= 0)
return res;
// this is annoying
char entry_path[MAX_PATH + 1];
realfs_getpath(fd, entry_path);
if (strcmp(entry->name, "..") == 0) {
if (strcmp(entry_path, "") != 0) {
*strrchr(entry_path, '/') = '\0';
}
} else if (strcmp(entry->name, ".") != 0) {
// god I don't know what to do if this would overflow
strcat(entry_path, "/");
strcat(entry_path, entry->name);
}
struct fakefs_db *fs = &fd->mount->fakefs;
db_begin(fs);
entry->inode = path_get_inode(fs, entry_path);
db_commit(fs);
// it's quite possible that due to some mishap there's no metadata for this file
// so just skip this entry, instead of crashing the program, so there's hope for recovery
if (entry->inode == 0)
goto retry;
return res;
}
static struct fd_ops fakefs_fdops;
static void __attribute__((constructor)) init_fake_fdops() {
fakefs_fdops = realfs_fdops;
fakefs_fdops.readdir = fakefs_readdir;
}
static int fakefs_mount(struct mount *mount) {
char db_path[PATH_MAX];
strncpy(db_path, mount->source, PATH_MAX -1);
char *basename = strrchr(db_path, '/') + 1;
assert(strcmp(basename, "data") == 0);
strncpy(basename, "meta.db", 8);
// do this now so rebuilding can use root_fd
int err = realfs.mount(mount);
if (err < 0)
return err;
err = fake_db_init(&mount->fakefs, db_path, mount->root_fd);
if (err < 0)
return err;
return 0;
}
static int fakefs_umount(struct mount *mount) {
int err = fake_db_deinit(&mount->fakefs);
if (err != SQLITE_OK) {
printk("ERROR: sqlite failed to close: %d\n", err);
}
/* return realfs.umount(mount); */
return 0;
}
static void fakefs_inode_orphaned(struct mount *mount, ino_t inode) {
struct fakefs_db *fs = &mount->fakefs;
db_begin(fs);
sqlite3_bind_int64(fs->stmt.try_cleanup_inode, 1, inode);
db_exec_reset(fs, fs->stmt.try_cleanup_inode);
db_commit(fs);
}
const struct fs_ops fakefs = {
.name = "fake", .magic = 0x66616b65,
.mount = fakefs_mount,
.umount = fakefs_umount,
.statfs = realfs_statfs,
.open = fakefs_open,
.readlink = fakefs_readlink,
.link = fakefs_link,
.unlink = fakefs_unlink,
.rename = fakefs_rename,
.symlink = fakefs_symlink,
.mknod = fakefs_mknod,
.close = realfs_close,
.stat = fakefs_stat,
.fstat = fakefs_fstat,
.flock = realfs_flock,
.setattr = fakefs_setattr,
.fsetattr = fakefs_fsetattr,
.getpath = realfs_getpath,
.utime = realfs_utime,
.mkdir = fakefs_mkdir,
.rmdir = fakefs_rmdir,
.inode_orphaned = fakefs_inode_orphaned,
};