forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 11
/
fakefs.c
343 lines (305 loc) · 11.9 KB
/
fakefs.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
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <archive.h>
#include <archive_entry.h>
#define ISH_INTERNAL
#include "fs/fake-db.h"
#include "fs/sqlutil.h"
#include "tools/fakefs.h"
#include "misc.h"
#ifndef MAX_PATH
#define MAX_PATH 4096
#endif
// I have a weird way of error handling
#define FILL_ERR(_type, _code, _message) do { \
err_out->line = __LINE__; \
err_out->type = _type; \
err_out->code = _code; \
err_out->message = strdup(_message); \
return false; \
} while (0)
#define ARCHIVE_ERR(archive) FILL_ERR(ERR_ARCHIVE, archive_errno(archive), archive_error_string(archive))
#define POSIX_ERR() FILL_ERR(ERR_POSIX, errno, strerror(errno))
#undef HANDLE_ERR // for sqlite
#define HANDLE_ERR(db) FILL_ERR(ERR_SQLITE, sqlite3_extended_errcode(db), sqlite3_errmsg(db))
#define CANCEL() FILL_ERR(ERR_CANCELLED, 0, "");
static bool progress_update(struct progress *p, double progress, const char *message) {
bool cancelled = false;
if (p && p->callback)
p->callback(p->cookie, progress, message, &cancelled);
return !cancelled;
}
// This isn't linked with ish which is why there's so much copy/pasted code
// I hate this code
static bool path_normalize(const char *path, char *out) {
#define ends_path(c) (c == '\0' || c == '/')
// normalized format:
// ( '/' path-component ) *
while (path[0] != '\0') {
while (path[0] == '/')
path++;
if (path[0] == '\0')
break; // if the path ends with a slash
// path points to the start of a path component
if (path[0] == '.' && path[1] == '.' && ends_path(path[2]))
return false; // no dotdot allowed!
if (path[0] == '.' && ends_path(path[1])) {
path++;
} else {
*out++ = '/';
while (path[0] != '/' && path[0] != '\0')
*out++ = *path++;
}
}
*out = '\0';
return true;
}
static const char *schema = Q(
create table meta (id integer unique default 0, db_inode integer);
insert into meta (db_inode) values (0);
create table stats (inode integer primary key, stat blob);
create table paths (path blob primary key, inode integer references stats(inode));
create index inode_to_path on paths (inode, path);
// no index is needed on stats, because the rows are ordered by the primary key
pragma user_version=3;
);
bool fakefs_import(const char *archive_path, const char *fs, struct fakefsify_error *err_out, struct progress p) {
int err = mkdir(fs, 0777);
if (err < 0)
POSIX_ERR();
// make the data root dir
char path_tmp[PATH_MAX];
snprintf(path_tmp, sizeof(path_tmp), "%s/data", fs);
err = mkdir(path_tmp, 0777);
int root_fd = open(path_tmp, O_RDONLY);
if (root_fd < 0)
POSIX_ERR();
// open the database
snprintf(path_tmp, sizeof(path_tmp), "%s/meta.db", fs);
sqlite3 *db;
err = sqlite3_open_v2(path_tmp, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
CHECK_ERR();
EXEC("pragma journal_mode=wal")
EXEC("begin");
EXEC(schema);
// open the archive
struct archive *archive = archive_read_new();
if (archive == NULL)
ARCHIVE_ERR(archive);
archive_read_support_filter_gzip(archive);
archive_read_support_format_tar(archive);
if (archive_read_open_filename(archive, archive_path, 65536) != ARCHIVE_OK)
ARCHIVE_ERR(archive);
struct stat real_stat;
if (stat(archive_path, &real_stat) < 0)
POSIX_ERR();
size_t archive_bytes = real_stat.st_size;
sqlite3_stmt *insert_stat = PREPARE("insert into stats (stat) values (?)");
sqlite3_stmt *insert_path = PREPARE("insert or replace into paths values (?, ?)");
sqlite3_stmt *insert_hardlink = PREPARE("insert or replace into paths values (?, (select inode from paths where path = ? limit 1))");
bool archive_has_root = false;
// do actual shit
struct archive_entry *entry;
while ((err = archive_read_next_header(archive, &entry)) == ARCHIVE_OK) {
char entry_path[MAX_PATH];
if (!path_normalize(archive_entry_pathname(entry), entry_path)) {
// Avoid pwnage
fprintf(stderr, "warning: skipped possible path traversal %s\n", archive_entry_pathname(entry));
continue;
}
if (!progress_update(&p, (double) archive_filter_bytes(archive, -1) / archive_bytes, entry_path))
CANCEL();
if (strcmp(entry_path, "") == 0)
archive_has_root = true;
const char *hardlink = archive_entry_hardlink(entry);
if (hardlink) {
char hardlink_path[MAX_PATH];
if (!path_normalize(hardlink, hardlink_path)) {
fprintf(stderr, "warning: almost pwned by hardlink %s\n", hardlink);
continue;
}
if (linkat(root_fd, fix_path(hardlink_path), root_fd, fix_path(entry_path), 0) < 0)
POSIX_ERR();
sqlite3_bind_blob64(insert_hardlink, 1, entry_path, strlen(entry_path), SQLITE_TRANSIENT);
sqlite3_bind_blob64(insert_hardlink, 2, hardlink_path, strlen(hardlink_path), SQLITE_TRANSIENT);
STEP_RESET(insert_hardlink);
continue;
}
// mkdir -p
char *entry_path_copy = strdup(entry_path);
char *slash = entry_path_copy;
while ((slash = strchr(*slash ? slash + 1 : slash, '/')) != NULL) {
*slash = '\0';
int err = mkdirat(root_fd, fix_path(entry_path_copy), 0777);
*slash = '/';
if (err < 0) {
if (errno == EEXIST) continue;
POSIX_ERR();
}
}
free(entry_path_copy);
int fd = -1;
if (archive_entry_filetype(entry) != AE_IFDIR) {
fd = openat(root_fd, fix_path(entry_path), O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0) {
if (errno == EISDIR) continue; // assuming it's case insensitivity
POSIX_ERR();
}
}
switch (archive_entry_filetype(entry)) {
case AE_IFDIR:
err = mkdirat(root_fd, fix_path(entry_path), 0777);
if (err < 0 && errno != EEXIST)
POSIX_ERR();
break;
case AE_IFREG:
if (archive_read_data_into_fd(archive, fd) != ARCHIVE_OK)
ARCHIVE_ERR(archive);
break;
case AE_IFLNK:
err = (int) write(fd, archive_entry_symlink(entry), strlen(archive_entry_symlink(entry)));
if (err < 0)
POSIX_ERR();
}
if (fd != -1)
close(fd);
struct ish_stat stat = {
.mode = (uint32_t) archive_entry_mode(entry),
.uid = (uint32_t) archive_entry_uid(entry),
.gid = (uint32_t) archive_entry_gid(entry),
.rdev = (uint32_t) archive_entry_rdev(entry),
};
sqlite3_bind_blob64(insert_stat, 1, &stat, sizeof(stat), SQLITE_TRANSIENT);
STEP_RESET(insert_stat);
sqlite3_bind_blob64(insert_path, 1, entry_path, strlen(entry_path), SQLITE_TRANSIENT);
sqlite3_bind_int64(insert_path, 2, sqlite3_last_insert_rowid(db));
STEP_RESET(insert_path);
}
if (err != ARCHIVE_EOF)
ARCHIVE_ERR(archive);
// Add a path entry for the root if it's missing
if (!archive_has_root) {
struct ish_stat stat = {.mode = 0755};
sqlite3_bind_blob64(insert_stat, 1, &stat, sizeof(stat), SQLITE_TRANSIENT);
STEP_RESET(insert_stat);
sqlite3_bind_blob64(insert_path, 1, "", 0, SQLITE_TRANSIENT);
sqlite3_bind_int64(insert_path, 2, sqlite3_last_insert_rowid(db));
STEP_RESET(insert_path);
}
FINALIZE(insert_stat);
FINALIZE(insert_path);
FINALIZE(insert_hardlink);
EXEC("commit");
sqlite3_close(db);
close(root_fd);
if (archive_read_free(archive) != ARCHIVE_OK)
ARCHIVE_ERR(archive);
return true;
}
bool fakefs_export(const char *fs, const char *archive_path, struct fakefsify_error *err_out, struct progress p) {
// open the archive
struct archive *archive = archive_write_new();
if (archive == NULL)
ARCHIVE_ERR(archive);
archive_write_add_filter_gzip(archive);
archive_write_set_format_pax_restricted(archive);
if (archive_write_open_filename(archive, archive_path) != ARCHIVE_OK)
ARCHIVE_ERR(archive);
// open the data root dir
char path_tmp[PATH_MAX];
snprintf(path_tmp, sizeof(path_tmp), "%s/data", fs);
int root_fd = open(path_tmp, O_RDONLY);
if (root_fd < 0)
POSIX_ERR();
// open the database
snprintf(path_tmp, sizeof(path_tmp), "%s/meta.db", fs);
sqlite3 *db;
int err = sqlite3_open_v2(path_tmp, &db, SQLITE_OPEN_READONLY, NULL);
CHECK_ERR();
EXEC("begin");
sqlite3_stmt *count_stmt = PREPARE("select count(path) from paths");
STEP(count_stmt);
int64_t paths_total = sqlite3_column_int64(count_stmt, 0);
FINALIZE(count_stmt);
int64_t paths_done = 0;
struct archive_entry_linkresolver *linkresolver = archive_entry_linkresolver_new();
archive_entry_linkresolver_set_strategy(linkresolver, ARCHIVE_FORMAT_TAR_PAX_RESTRICTED);
sqlite3_stmt *query = PREPARE("select path, inode, stat from paths, stats using (inode)");
while (STEP(query)) {
struct archive_entry *entry = archive_entry_new();
const char *path_in_db = sqlite3_column_blob(query, 0);
size_t path_len = sqlite3_column_bytes(query, 0);
char *path = malloc(path_len + 2);
path[0] = '.';
memcpy(path + 1, path_in_db, path_len);
path[path_len + 1] = '\0';
archive_entry_set_pathname(entry, path);
if (!progress_update(&p, (double) paths_done / paths_total, path))
CANCEL();
archive_entry_set_ino64(entry, sqlite3_column_int64(query, 1));
struct ish_stat stat = *(struct ish_stat *) sqlite3_column_blob(query, 2);
archive_entry_set_mode(entry, stat.mode);
archive_entry_set_uid(entry, stat.uid);
archive_entry_set_gid(entry, stat.gid);
archive_entry_set_rdev(entry, stat.rdev);
struct stat real_stat;
if (fstatat(root_fd, path, &real_stat, 0) < 0) {
if (errno == ENOENT) {
printf("skipping %s\n", path);
goto skip;
}
POSIX_ERR();
}
archive_entry_set_size(entry, real_stat.st_size);
int fd = -1;
S_IFMT;
if (S_ISREG(stat.mode) || S_ISLNK(stat.mode))
fd = openat(root_fd, path, O_RDONLY);
if (S_ISLNK(stat.mode)) {
char buf[MAX_PATH+1];
ssize_t len = read(fd, buf, sizeof(buf)-1);
if (len < 0)
POSIX_ERR();
buf[len] = '\0';
archive_entry_set_symlink(entry, buf);
}
struct archive_entry *sparse;
archive_entry_linkify(linkresolver, &entry, &sparse);
if (entry != NULL)
archive_write_header(archive, entry);
if (sparse != NULL)
archive_write_header(archive, sparse);
if (S_ISREG(stat.mode) && archive_entry_size(entry) != 0) {
char buf[8192];
ssize_t len;
while ((len = read(fd, buf, sizeof(buf))) > 0) {
ssize_t written = archive_write_data(archive, buf, len);
if (written < 0)
ARCHIVE_ERR(archive);
if (written != len)
printf("uh oh\n");
}
if (len < 0)
POSIX_ERR();
}
if (fd != -1)
close(fd);
skip:
paths_done++;
free(path);
archive_entry_free(entry);
}
FINALIZE(query);
archive_entry_linkresolver_free(linkresolver);
sqlite3_close(db);
close(root_fd);
if (archive_write_close(archive) != ARCHIVE_OK)
ARCHIVE_ERR(archive);
archive_write_free(archive);
return true;
}