-
Notifications
You must be signed in to change notification settings - Fork 0
/
packfs.c
386 lines (331 loc) · 9.65 KB
/
packfs.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
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <dlfcn.h>
#include <assert.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/mman.h>
#define packfs_string_value_(x) #x
#define packfs_string_value(x) packfs_string_value_(x)
#ifdef PACKFS_PREFIX
#define packfs_prefix packfs_string_value(PACKFS_PREFIX)
#else
#define packfs_prefix "packfs/"
#endif
#define packfs_filefd_min 1000000000
#define packfs_filefd_max 1000001000
#define packfs_filefd_cnt (packfs_filefd_max - packfs_filefd_min)
#include "packfs.h"
int packfs_filecnt;
int packfs_filefd[packfs_filefd_cnt];
FILE* packfs_fileptr[packfs_filefd_cnt];
const char* packfs_sanitize_path(const char* path)
{
return (path != NULL && strlen(path) > 2 && path[0] == '.' && path[1] == '/') ? (path + 2) : path;
}
int packfs_open(const char* path, FILE** out)
{
path = packfs_sanitize_path(path);
if(strncmp(packfs_prefix, path, strlen(packfs_prefix)) != 0)
return -1;
for(int i = 0; i < packfsfilesnum; i++)
{
if(0 == strcmp(path, packfsinfos[i].path))
{
FILE* res = fmemopen((void*)packfsinfos[i].start, (size_t)(packfsinfos[i].end - packfsinfos[i].start), "r");
for(int k = 0; k < packfs_filefd_max - packfs_filefd_min; k++)
{
if(packfs_filefd[k] == 0)
{
packfs_filefd[k] = packfs_filefd_min + k;
packfs_fileptr[k] = res;
if(out != NULL)
*out = packfs_fileptr[k];
return packfs_filefd[k];
}
}
}
}
return -1;
}
int packfs_close(int fd)
{
if(fd < packfs_filefd_min || fd >= packfs_filefd_max)
return -1;
for(int k = 0; k < packfs_filefd_max - packfs_filefd_min; k++)
{
if(packfs_filefd[k] == fd)
{
packfs_filefd[k] = 0;
fclose(packfs_fileptr[k]);
packfs_fileptr[k] = NULL;
return 0;
}
}
return -1;
}
FILE* packfs_findptr(int fd)
{
if(fd < packfs_filefd_min || fd >= packfs_filefd_max)
return NULL;
for(int k = 0; k < packfs_filefd_max - packfs_filefd_min; k++)
{
if(packfs_filefd[k] == fd)
return packfs_fileptr[k];
}
return NULL;
}
int packfs_findfd(FILE* ptr)
{
for(int k = 0; k < packfs_filefd_max - packfs_filefd_min; k++)
{
if(packfs_fileptr[k] == ptr)
return packfs_filefd[k];
}
return -1;
}
ssize_t packfs_read(int fd, void* buf, size_t count)
{
FILE* ptr = packfs_findptr(fd);
if(!ptr)
return -1;
return (ssize_t)fread(buf, 1, count, ptr);
}
int packfs_seek(int fd, long offset, int whence)
{
FILE* ptr = packfs_findptr(fd);
if(!ptr)
return -1;
return fseek(ptr, offset, whence);
}
int packfs_access(const char* path)
{
path = packfs_sanitize_path(path);
if(strncmp(packfs_prefix, path, strlen(packfs_prefix)) == 0)
{
for(int i = 0; i < packfsfilesnum; i++)
{
if(0 == strcmp(path, packfsinfos[i].path))
{
return 0;
}
}
return -1;
}
return -2;
}
int packfs_stat(const char* path, struct stat *restrict statbuf)
{
path = packfs_sanitize_path(path);
if(strncmp(packfs_prefix, path, strlen(packfs_prefix)) == 0)
{
for(int i = 0; i < packfsfilesnum; i++)
{
if(0 == strcmp(path, packfsinfos[i].path))
{
*statbuf = (struct stat){0};
statbuf->st_size = (off_t)(packfsinfos[i].end - packfsinfos[i].start);
statbuf->st_mode = S_IFREG;
return 0;
}
}
for(int i = 0; i < packfsdirsnum; i++)
{
if(0 == strcmp(path, packfsdirs[i]))
{
*statbuf = (struct stat){0};
statbuf->st_mode = S_IFDIR;
return 0;
}
}
return -1;
}
return -2;
}
/*
int packfs_mirror(FILE* stream, const char* start, const char* end)
{
int res = -1;
if(stream == NULL)
{
res = memfd_create("", 0);
write(res, start, (size_t)(end - start));
lseek(res, 0, SEEK_SET);
//char buf[1024];
//sprintf(buf, ".tmp_%s.bin", packfsinfos[i].safe_path);
//FILE* f = fopen(buf, "w+");
//fwrite(packfsinfos[i].start, 1, (size_t)(packfsinfos[i].end - packfsinfos[i].start), f);
//fseek(f, 0, SEEK_SET);
//int res = fileno(f);
//fprintf(stderr, "packfs: open(\"%s\", %d) == %d\n", path, flags, res);
}
else
{
char buf[1024];
sprintf(buf, ".tmp_%p.bin", (void*)stream);
FILE* f = fopen(buf, "w+");
if(!f) return -1;
do
{
fwrite(buf, 1, fread(buf, 1, sizeof(buf), stream), f);
}
while(!feof(stream) && !ferror(stream));
fseek(f, 0, SEEK_SET);
res = fileno(f);
}
return res;
}
*/
FILE* fopen(const char *path, const char *mode)
{
typedef FILE* (*orig_fopen_func_type)(const char *path, const char *mode);
orig_fopen_func_type orig_func = (orig_fopen_func_type)dlsym(RTLD_NEXT, "fopen");
FILE* res = NULL;
if(packfs_open(path, &res) >= 0)
{
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: Fopen(\"%s\", \"%s\") == %p\n", path, mode, (void*)res);
#endif
return res;
}
res = orig_func(path, mode);
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: fopen(\"%s\", \"%s\") == %p\n", path, mode, (void*)res);
#endif
return res;
}
int fileno(FILE *stream)
{
typedef int (*orig_func_type)(FILE* stream);
orig_func_type orig_func = (orig_func_type)dlsym(RTLD_NEXT, "fileno");
if(!stream) return -1;
int res = orig_func(stream);
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: fileno(%p) == %d\n", (void*)stream, res);
#endif
if(res < 0)
{
res = packfs_findfd(stream);
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: Fileno(%p) == %d\n", (void*)stream, res);
#endif
}
return res;
}
typedef int (*orig_func_type_open)(const char *path, int flags);
typedef struct
{
orig_func_type_open open;
} packfs_context;
typedef struct
{
int packfsfilesnum, packfsdirsnum;
struct { const char* safe_path; const char *path; const char* start; const char* end; bool isdir; } packfsinfos[];
} packfs_index;
packfs_context packfs_ensure_context()
{
static packfs_context packfs_ctx;
static bool packfs_initialized = false;
if(!packfs_initialized)
{
packfs_ctx.open = dlsym(RTLD_NEXT, "open"); //(orig_func_type_open)
packfs_initialized = true;
}
return packfs_ctx;
}
int open(const char *path, int flags, ...)
{
packfs_context packfs_ctx = packfs_ensure_context();
//typedef int (*orig_func_type)(const char *path, int flags);
//orig_func_type orig_func = (orig_func_type)dlsym(RTLD_NEXT, "open");
int res = packfs_open(path, NULL);
if(res >= 0)
{
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: Open(\"%s\", %d) == %d\n", path, flags, res);
#endif
return res;
}
res = packfs_ctx.open(path, flags);
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: open(\"%s\", %d) == %d\n", path, flags, res);
#endif
return res;
}
// https://en.cppreference.com/w/c/io/fread
ssize_t read(int fd, void* buf, size_t count)
{
typedef ssize_t (*orig_func_type)(int fd, void* buf, size_t count);
orig_func_type orig_func = (orig_func_type)dlsym(RTLD_NEXT, "read");
ssize_t res = packfs_read(fd, buf, count);
if(res >= 0)
{
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: Read(%d, %p, %zu) == %d\n", fd, buf, count, (int)res);
#endif
return res;
}
res = orig_func(fd, buf, count);
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: read(%d, %p, %zu) == %d\n", fd, buf, count, (int)res);
#endif
return res;
}
// https://en.cppreference.com/w/c/io/fseek
off_t lseek(int fd, off_t offset, int whence)
{
typedef off_t (*orig_func_type)(int fd, off_t offset, int whence);
orig_func_type orig_func = (orig_func_type)dlsym(RTLD_NEXT, "lseek");
int res = packfs_seek(fd, (long)offset, whence);
if(res >= 0)
{
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: Seek(%d, %d, %d) == %d\n", fd, (int)offset, whence, (int)res);
#endif
return res;
}
res = orig_func(fd, offset, whence);
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: seek(%d, %d, %d) == %d\n", fd, (int)offset, whence, (int)res);
#endif
return res;
}
int access(const char *path, int flags)
{
typedef int (*orig_func_type)(const char *path, int flags);
orig_func_type orig_func = (orig_func_type)dlsym(RTLD_NEXT, "access");
int res = packfs_access(path);
if(res >= -1)
{
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: Access(\"%s\", %d) == 0\n", path, flags);
#endif
return res;
}
res = orig_func(path, flags);
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: access(\"%s\", %d) == %d\n", path, flags, res);
#endif
return res;
}
int stat(const char *restrict path, struct stat *restrict statbuf)
{
typedef int (*orig_func_type)(const char *restrict path, struct stat *restrict statbuf);
orig_func_type orig_func = (orig_func_type)dlsym(RTLD_NEXT, "stat");
int res = packfs_stat(path, statbuf);
if(res >= -1)
{
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: Stat(\"%s\", %p) == -1\n", path, (void*)statbuf);
#endif
return res;
}
res = orig_func(path, statbuf);
#ifdef PACKFS_LOG
fprintf(stderr, "packfs: stat(\"%s\", %p) == %d\n", path, (void*)statbuf, res);
#endif
return res;
}