-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuse-unecm.c
483 lines (415 loc) · 14.8 KB
/
fuse-unecm.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
/***************************************************************************/
/*
* ECM FUSE Module
* by Ronnie Sahlberg <ronniesahlberg@gmail.com>
*
* This is an overlay filesystem to transparently uncompress ECM files
* created by ECM-COMPRESS and which have a matching .ecm.edi index file.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define _GNU_SOURCE
#define FUSE_USE_VERSION 26
#define _FILE_OFFSET_BITS 64
#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <fuse.h>
#include <getopt.h>
#include <pwd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <time.h>
#include <unistd.h>
#include "libunecm.h"
#include <tdb.h>
#define discard_const(ptr) ((void *)((intptr_t)(ptr)))
#define LOG(...) { \
if (logfile) { \
FILE *fh = fopen(logfile, "a+"); \
time_t t = time(NULL); \
char tmp[256]; \
strftime(tmp, sizeof(tmp), "%T", localtime(&t));\
fprintf(fh, "[UNECM] %s ", tmp); \
fprintf(fh, __VA_ARGS__); \
fclose(fh); \
} \
}
struct file {
struct ecm *ecm;
int fd;
};
static char *logfile;
static struct tdb_context *nu_tdb;
static struct tdb_context *filesize_tdb;
/* descriptor for the underlying directory */
static int dir_fd;
/* This function takes a path to a file and returns true if this needs
* ecm unpacking.
* For a file <file> we need to unpack the file if
* <file> does not exist
* <file>.ecm exists
* <file>.ecm.edi exists
* In that situation READDIR will just turn a single instance for the name
* <file> and hide the entries for <file>.ecm and <file>.ecm.edi
*
* IF all three files exist, we do not mutate any of the READDIR data
* and return all three names. Then we just redirect all I/O to the unpacked
* file. The reason we do not hide the <file>.ecm and <file>.ecm.edi is to make
* it possible for the user to see that something is odd/wrong and possibly
* take action. (action == delete the unpacked file, it is redundant.)
*/
static int need_ecm_uncompress(const char *file) {
char stripped[PATH_MAX];
char tmp[PATH_MAX];
struct stat st;
TDB_DATA key, data;
uint8_t ret = 1;
LOG("NEED_ECM_UNCOMPRESS [%s]\n", file);
key.dptr = discard_const(file);
key.dsize = strlen(file);
data = tdb_fetch(nu_tdb, key);
if (data.dptr) {
uint8_t val = data.dptr[0];
free(data.dptr);
return val;
}
LOG("NEED_ECM_UNCOMPRESS SLOW PATH [%s]\n", file);
snprintf(stripped, PATH_MAX,"%s", file);
if (strlen(stripped) > 4 &&
!strcmp(stripped + strlen(stripped) - 4, ".edi")) {
stripped[strlen(stripped) - 4] = 0;
}
if (strlen(stripped) > 4 &&
!strcmp(stripped + strlen(stripped) - 4, ".ecm")) {
stripped[strlen(stripped) - 4] = 0;
}
if (fstatat(dir_fd, stripped, &st, AT_NO_AUTOMOUNT) == 0) {
ret = 0;
goto finished;
}
snprintf(tmp, PATH_MAX, "%s.ecm", stripped);
if (fstatat(dir_fd, tmp, &st, AT_NO_AUTOMOUNT) != 0) {
ret = 0;
goto finished;
}
snprintf(tmp, PATH_MAX, "%s.ecm.edi", stripped);
if (fstatat(dir_fd, tmp, &st, AT_NO_AUTOMOUNT) != 0) {
ret = 0;
goto finished;
}
finished:
data.dptr = &ret;
data.dsize = 1;
tdb_store(nu_tdb, key, data, TDB_REPLACE);
return ret;
}
static int fuse_unecm_read(const char *path, char *buf, size_t size,
off_t offset, struct fuse_file_info *ffi)
{
struct file *file;
int ret;
if (path[0] == '/') {
path++;
}
LOG("READ [%s]\n", path);
file = (void *)ffi->fh;
if (file->ecm) {
ret = ecm_read(file->ecm, buf, offset, size);
if (ret == -1) {
LOG("READ ecm_read failed [%s] %jd:%zu %s\n",
path, offset, size, strerror(errno));
return -errno;
}
LOG("READ ECM [%s] %jd:%zu %d\n", path, offset, size, ret);
return ret;
}
/* Passthrough to underlying filesystem */
ret = pread(file->fd, buf, size, offset);
LOG("READ underlying file [%s] %jd:%zu %d\n", path, offset, size, ret);
return (ret == -1) ? -errno : ret;
}
static int fuse_unecm_release(const char *path, struct fuse_file_info *ffi)
{
struct file *file = (struct file *)ffi->fh;
if (path[0] == '/') {
path++;
}
LOG("RELEASE [%s]\n", path);
if (file == NULL) {
return 0;
}
if (file->ecm) {
ecm_close_file(file->ecm);
}
if (file->fd != -1) {
close(file->fd);
}
free(file);
return 0;
}
static int fuse_unecm_open(const char *path, struct fuse_file_info *ffi)
{
struct stat st;
int ret;
struct file *file = malloc(sizeof(struct file));
file->ecm = NULL;
file->fd = -1;
if (path[0] == '/') {
path++;
}
LOG("OPEN [%s]\n", path);
ret = fstatat(dir_fd, path, &st, AT_NO_AUTOMOUNT);
if (ret && errno == ENOENT) {
if (need_ecm_uncompress(path)) {
char tmp[PATH_MAX];
snprintf(tmp, PATH_MAX, "%s.ecm", path);
file->ecm = ecm_open_file(dir_fd, tmp);
if (file->ecm == NULL) {
free(file);
LOG("OPEN Failed to open ECM [%s]\n", path);
return -ENOENT;
}
ffi->fh = (uint64_t)file;
return 0;
}
}
file->fd = openat(dir_fd, path, O_RDONLY);
if (file->fd == -1) {
free(file);
LOG("OPEN FD [%s] %s\n", path, strerror(errno));
return -errno;
}
ffi->fh = (uint64_t)file;
LOG("OPEN FD [%s] SUCCESS\n", path);
return 0;
}
/* returns the size of the uncompressed file, or 0 if it could not be
* determined.
*/
static off_t get_uncompressed_size(const char *path)
{
struct ecm *ecm;
size_t pos;
TDB_DATA key, data;
LOG("GET_UNCOMPRESSED_SIZE [%s]\n", path);
key.dptr = discard_const(path);
key.dsize = strlen(path);
data = tdb_fetch(filesize_tdb, key);
if (data.dptr) {
off_t size = *(off_t *)data.dptr;
free(data.dptr);
return size;
}
LOG("GET_UNCOMPRESSED_SIZE SLOW PATH [%s]\n", path);
ecm = ecm_open_file(dir_fd, path);
if (ecm == NULL) {
LOG("Failed to open ECM file %s in get_uncompressed_size\n",
path);
return 0;
}
pos = ecm_get_file_size(ecm);
ecm_close_file(ecm);
LOG("GET_UNCOMPRESSED_SIZE [%s] %zu\n", path, pos);
data.dptr = (uint8_t *)&pos;
data.dsize = sizeof(pos);
tdb_store(filesize_tdb, key, data, TDB_REPLACE);
return pos;
}
static int fuse_unecm_getattr(const char *path, struct stat *stbuf)
{
int ret;
LOG("GETATTR [%s]\n", path);
if (path[0] == '/') {
path++;
}
ret = fstatat(dir_fd, path, stbuf, AT_NO_AUTOMOUNT|AT_EMPTY_PATH);
if (ret && errno == ENOENT) {
if (need_ecm_uncompress(path)) {
char tmp[PATH_MAX];
int fd;
snprintf(tmp, PATH_MAX, "%s.ecm", path);
ret = fstatat(dir_fd, tmp, stbuf, AT_NO_AUTOMOUNT);
if (ret) {
LOG("GETATTR fstatat failed [%s] %s\n",
path, strerror(errno));
return -errno;
}
stbuf->st_size = get_uncompressed_size(tmp);
LOG("GETATTR [%s] SUCCESS\n", path);
return 0;
}
}
if (ret) {
LOG("GETATTR [%s] SUCCESS\n", path);
return -errno;
}
LOG("GETATTR [%s] SUCCESS\n", path);
return 0;
}
static int fuse_unecm_readdir(const char *path, void *buf,
fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
DIR *dir;
struct dirent *ent;
int fd;
if (path[0] == '/') {
path++;
}
if (path[0] == '\0') {
path = ".";
}
LOG("READDIR [%s]\n", path);
fd = openat(dir_fd, path, O_DIRECTORY);
dir = fdopendir(fd);
if (dir == NULL) {
return -errno;
}
while ((ent = readdir(dir)) != NULL) {
struct ecm *e = NULL;
char full_path[PATH_MAX];
if (strcmp(path, ".")) {
snprintf(full_path, PATH_MAX, "%s/%s",
path, ent->d_name);
} else {
strcpy(full_path, ent->d_name);
}
if (need_ecm_uncompress(full_path)) {
char tmp[PATH_MAX];
snprintf(tmp, PATH_MAX, "%s", ent->d_name);
if (strlen(tmp) > 8 &&
!strcmp(tmp + strlen(tmp) - 8, ".ecm.edi")) {
tmp[strlen(tmp) - 8] = 0;
filler(buf, tmp, NULL, 0);
}
continue;
}
filler(buf, ent->d_name, NULL, 0);
}
closedir(dir);
return 0;
}
static int fuse_unecm_statfs(const char *path, struct statvfs* stbuf)
{
LOG("STATFS [%s]\n", path);
return fstatvfs(dir_fd, stbuf);
}
static struct fuse_operations unecm_oper = {
.getattr = fuse_unecm_getattr,
.open = fuse_unecm_open,
.release = fuse_unecm_release,
.read = fuse_unecm_read,
.readdir = fuse_unecm_readdir,
.statfs = fuse_unecm_statfs,
};
static void print_usage(char *name)
{
printf("Usage: %s [-?|--help] [-a|--allow-other] "
"[-m|--mountpoint=mountpoint] "
"[-l|--logfile=<file> [-f|--foreground]", name);
exit(0);
}
int main(int argc, char *argv[])
{
int c, ret = 0, opt_idx = 0;
char tdbdir[PATH_MAX];
char tdbfile[PATH_MAX];
char *mnt = NULL;
static struct option long_opts[] = {
{ "help", no_argument, 0, '?' },
{ "allow-other", no_argument, 0, 'a' },
{ "foreground", no_argument, 0, 'f' },
{ "logfile", required_argument, 0, 'l' },
{ "mountpoint", required_argument, 0, 'm' },
{ NULL, 0, 0, 0 }
};
int fuse_unecm_argc = 6;
char *fuse_unecm_argv[16] = {
"fuse-unecm",
"<export>",
"-omax_write=32768",
"-ononempty",
"-s",
"-odefault_permissions",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
char fs_name[1024], fs_type[1024];
while ((c = getopt_long(argc, argv, "?hafl:m:", long_opts,
&opt_idx)) > 0) {
switch (c) {
case 'h':
case '?':
print_usage(argv[0]);
return 0;
case 'a':
fuse_unecm_argv[fuse_unecm_argc++] = "-oallow_other";
break;
case 'f':
fuse_unecm_argv[fuse_unecm_argc++] = "-f";
break;
case 'l':
logfile = strdup(optarg);
break;
case 'm':
mnt = strdup(optarg);
break;
}
}
snprintf(fs_name, sizeof(fs_name), "-ofsname=%s", mnt);
fuse_unecm_argv[fuse_unecm_argc++] = fs_name;
snprintf(fs_type, sizeof(fs_type), "-osubtype=UNECM");
fuse_unecm_argv[fuse_unecm_argc++] = fs_type;
if (mnt == NULL) {
fprintf(stderr, "-m was not specified.\n");
print_usage(argv[0]);
ret = 10;
exit(1);
}
dir_fd = open(mnt, O_DIRECTORY);
fuse_unecm_argv[1] = mnt;
snprintf(tdbdir, sizeof(tdbdir), "%s/.fuse-unecm",
getpwuid(getuid())->pw_dir);
mkdir(tdbdir, 0700);
nu_tdb = tdb_open(NULL, 10000001, TDB_INTERNAL, O_RDWR, 0);
if (nu_tdb == NULL) {
printf("Failed to open NEEDS-UNCOMPRESSION TDB\n");
exit(1);
}
snprintf(tdbfile, sizeof(tdbfile), "%s/file_size.tdb", tdbdir);
errno = 0;
filesize_tdb = tdb_open(tdbfile, 10000001, 0, O_CREAT|O_RDWR, 0600);
if (filesize_tdb == NULL) {
printf("Failed to open FILE-SIZE TDB : %s\n", strerror(errno));
exit(1);
}
return fuse_main(fuse_unecm_argc, fuse_unecm_argv, &unecm_oper, NULL);
}