-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusemfs.c
468 lines (419 loc) · 14.9 KB
/
fusemfs.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
/*
* FuseMFS - Macintosh File System implementation (read-only)
* Copyright (C) 2008-2009 Jesus A. Alvarez
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#if defined(__APPLE__)
#define _DARWIN_USE_64_BIT_INODE 1
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#define __USE_BSD // linux strdup
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <iconv.h>
#include <libgen.h>
#include "fusemfs.h"
#ifdef DEBUG
#define dprintf(args...) fprintf(stderr, args)
#else
#define dprintf(args...)
#endif
static struct {
char *path;
int flat;
} options = {
.path = NULL,
.flat = 0
};
static MFSVolume *_fusemfs_vol;
static iconv_t _fusemfs_iconv;
static iconv_t _fusemfs_vnoci;
enum {
KEY_VERSION,
KEY_HELP,
KEY_FLAT
};
#define FUSEMFS_OPT(t, p, v) { t, offsetof(struct options, p), v }
#define COMMENT_XATTR "com.apple.metadata:kMDItemFinderComment"
static struct fuse_opt fusemfs_opts[] =
{
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_KEY("--flat", KEY_FLAT),
FUSE_OPT_END
};
static struct fuse_operations fusemfs_ops = {
.getattr = fusemfs_getattr,
.readdir = fusemfs_readdir,
.open = fusemfs_open,
.release = fusemfs_release,
.read = fusemfs_read,
.statfs = fusemfs_statfs,
.init = fusemfs_init
};
static int fusemfs_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs)
{
switch (key) {
case FUSE_OPT_KEY_NONOPT:
if (options.path == NULL) {
options.path = strdup(arg);
return 0;
}
return 1;
case KEY_VERSION:
printf("fuseMFS %s, (c)2008-2009 Jesus A. Alvarez, namedfork.net\n", FUSEMFS_VERSION);
exit(1);
case KEY_HELP:
printf("usage: fusemfs [--flat] [fuse options] device mountpoint\n");
exit(0);
case KEY_FLAT:
options.flat = 1;
return 0;
}
}
char * mfs_to_utf8 (const char * in, char * out, size_t outlen) {
size_t len = strlen(in);
size_t outleft;
char * outp = out;
if (out == NULL) {
outlen = (len*3)+1;
out = malloc(outlen);
}
outleft = outlen-1;
iconv(_fusemfs_iconv, (const char **restrict)&in, &len, &outp, &outleft);
iconv(_fusemfs_iconv, NULL, NULL, NULL, NULL);
out[outlen-outleft-1] = '\0';
// replace /:
for(outp=out;*outp;outp++) {
if (*outp == ':') *outp = '/';
else if (*outp == '/') *outp = ':';
}
return out;
}
char * utf8_to_mfs (const char * in) {
size_t len, outlen, outleft;
len = outleft = strlen(in);
outlen = len+1;
char * out = malloc(outlen);
char * outp = out;
iconv(_fusemfs_vnoci, &in, &len, &outp, &outleft);
iconv(_fusemfs_vnoci, NULL, NULL, NULL, NULL);
out[outlen-outleft-1] = '\0';
// replace /:
for(outp=out;*outp;outp++) {
if (*outp == ':') *outp = '/';
else if (*outp == '/') *outp = ':';
}
return out;
}
int main(int argc, char *argv[])
{
int ret;
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
#ifdef DEBUG
freopen("/fusemfs.log", "a", stderr);
fprintf(stderr, "fusemfs %d\n", getpid());
for(int i=0; i<argc; i++) fprintf(stderr, "%d: %s\n", i, argv[i]);
#endif
bzero(&options, sizeof options);
if (fuse_opt_parse(&args, &options, fusemfs_opts, fusemfs_opt_proc) == -1) {
perror("fuse_opt_parse");
return -1;
}
// open volume
if (options.path == NULL) {
fprintf(stderr, "invalid volume\n");
return 1;
}
int mfsflags = 0;
if (!options.flat) mfsflags |= MFS_FOLDERS;
_fusemfs_iconv = iconv_open("UTF-8", "Macintosh");
if (_fusemfs_iconv == (iconv_t)-1) {
perror("iconv_open");
return 1;
}
_fusemfs_vnoci = iconv_open("Macintosh", "UTF-8");
if (_fusemfs_vnoci == (iconv_t)-1) {
perror("iconv_open");
return 1;
}
_fusemfs_vol = mfs_vopen(options.path, 0, mfsflags);
if (_fusemfs_vol == NULL) {
perror("mfs_vopen");
exit(1);
}
// fuse options
fuse_opt_add_arg(&args, "-oro");
fuse_opt_add_arg(&args, "-s");
// MacFUSE options
#if defined(__APPLE__)
char volnameOption[128] = "-ovolname=";
mfs_to_utf8(_fusemfs_vol->name, volnameOption+10, 120);
fuse_opt_add_arg(&args, volnameOption);
fuse_opt_add_arg(&args, "-ofstypename=MFS");
fuse_opt_add_arg(&args, "-olocal");
fuse_opt_add_arg(&args, "-oallow_other");
fuse_opt_add_arg(&args, "-odefer_permissions");
fuse_opt_add_arg(&args, "-okill_on_unmount");
char *fsnameOption = malloc(strlen(options.path)+10);
strcpy(fsnameOption, "-ofsname=");
strcat(fsnameOption, options.path);
fuse_opt_add_arg(&args, fsnameOption);
free(fsnameOption);
fuse_opt_add_arg(&args, "-ovolicon=/System/Library/Extensions/IOSCSIArchitectureModelFamily.kext/Contents/Resources/Floppy.icns");
#endif
// run fuse
dprintf("calling fuse_main\n");
ret = fuse_main(args.argc, args.argv, &fusemfs_ops, NULL);
mfs_vclose(_fusemfs_vol);
iconv_close(_fusemfs_iconv);
iconv_close(_fusemfs_vnoci);
fuse_opt_free_args(&args);
return ret;
}
#if 0
#pragma mark -
#pragma mark FUSE Callbacks
#endif
static void * fusemfs_init (struct fuse_conn_info *conn) {
#ifdef DEBUG
freopen("/fusemfs.log", "a", stderr);
fprintf(stderr, "fusemfs_init %d\n", getpid()); fflush(stderr);
#endif
return NULL;
}
static int fusemfs_getattr (const char *path, struct stat *stbuf) {
dprintf("getattr %s\n", path);
MFSDirectoryRecord *rec;
MFSFolder *folder;
int ret = -ENOENT;
char *fpath = utf8_to_mfs(path);
char *fname = strrchr(fpath, ':')+1;
int pathType;
if (strcmp(path, "/") == 0) {
// root dir info
mfs_root_stat(&_fusemfs_vol->mdb, stbuf);
ret = 0;
} else if (rec = mfs_directory_find_name(_fusemfs_vol->directory, fname)) {
// data fork
if ((options.flat) || (mfs_path_info(_fusemfs_vol, fpath) == kMFSPathFile)) {
mfs_record_stat(&_fusemfs_vol->mdb, rec, stbuf, kMFSForkData);
ret = 0;
}
} else if ((strncmp(fname, "._", 2) == 0) && (rec = mfs_directory_find_name(_fusemfs_vol->directory, fname+2))) {
// AppleDouble headerfile
if (!options.flat) {
memmove(fname, fname+2, strlen(fname)-2);
fname[strlen(fname)-2] = '\0';
pathType = mfs_path_info(_fusemfs_vol, fpath);
memmove(fname+2, fname, strlen(fname));
memcpy(fname, "._", 2);
} else {
pathType = kMFSPathFile;
};
if (pathType == kMFSPathFile) {
mfs_record_stat(&_fusemfs_vol->mdb, rec, stbuf, kMFSForkAppleDouble);
ret = 0;
}
} else if (!options.flat && (mfs_path_info(_fusemfs_vol, fpath) == kMFSPathFolder)) {
// folder
folder = mfs_folder_find_name(_fusemfs_vol, fname);
if (folder) {
mfs_folder_stat(folder, stbuf);
ret = 0;
}
}
free(fpath);
dprintf("getattr: %d\n", ret);
return ret;
}
static int fusemfs_readdir (const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) {
dprintf("readdir %s\n", path);
MFSDirectoryRecord *rec;
MFSFolder *dir, *subdir;
struct stat stbuf;
char fname[256];
int16_t folderID;
int i;
if (strcmp(path, "/") == 0) {
// root directory
mfs_root_stat(&_fusemfs_vol->mdb, &stbuf);
filler(buf, ".", &stbuf, 0);
filler(buf, "..", NULL, 0);
for(i=0; _fusemfs_vol->directory[i]; i++) {
rec = _fusemfs_vol->directory[i];
folderID = ntohs(rec->flUsrWds.folder);
if (!options.flat &&
(folderID != kMFSFolderRoot) &&
(folderID != kMFSFolderDesktop))
// if using folders, skip files that aren't on the root
// directory or on the desktop
continue;
// data file
mfs_record_stat(&_fusemfs_vol->mdb, rec, &stbuf, kMFSForkData);
mfs_to_utf8(rec->flCName, fname, sizeof fname);
filler(buf, fname, &stbuf, 0);
// AppleDouble headerfile
char *appleDoubleName = malloc(strlen(fname)+3);
strcpy(appleDoubleName, "._");
strcat(appleDoubleName, fname);
mfs_record_stat(&_fusemfs_vol->mdb, rec, &stbuf, kMFSForkAppleDouble);
filler(buf, appleDoubleName, &stbuf, 0);
free(appleDoubleName);
}
// subdirectories
if (!options.flat && (dir = mfs_folder_find(_fusemfs_vol, kMFSFolderRoot))) {
for(i = 0; i < _fusemfs_vol->numFolders; i++) {
subdir = &_fusemfs_vol->folders[i];
if (subdir->fdID == kMFSFolderRoot) continue;
if ((subdir->fdParent != kMFSFolderRoot) && (subdir->fdParent != kMFSFolderDesktop)) continue;
// directories in root or desktop
mfs_folder_stat(subdir, &stbuf);
mfs_to_utf8(subdir->fdCNam, fname, sizeof fname);
filler(buf, fname, &stbuf, 0);
}
}
} else if (options.flat) {
return -ENOENT;
} else {
// a folder
char *pathdup = strdup(path);
char *dirname = utf8_to_mfs(basename(pathdup));
dir = mfs_folder_find_name(_fusemfs_vol, dirname);
free(pathdup); free(dirname);
if (dir == NULL) return -ENOENT;
// TODO: check that the folder really exists at that path
// by constructing folder path and comparing it with the asked one
mfs_folder_stat(dir, &stbuf);
filler(buf, ".", &stbuf, 0);
filler(buf, "..", NULL, 0);
// files
for(i=0; _fusemfs_vol->directory[i]; i++) {
rec = _fusemfs_vol->directory[i];
folderID = ntohs(rec->flUsrWds.folder);
if (folderID != dir->fdID) continue;
// data file
mfs_record_stat(&_fusemfs_vol->mdb, rec, &stbuf, kMFSForkData);
mfs_to_utf8(rec->flCName, fname, sizeof fname);
filler(buf, fname, &stbuf, 0);
// AppleDouble headerfile
char *appleDoubleName = malloc(strlen(fname)+3);
strcpy(appleDoubleName, "._");
strcat(appleDoubleName, fname);
mfs_record_stat(&_fusemfs_vol->mdb, rec, &stbuf, kMFSForkAppleDouble);
filler(buf, appleDoubleName, &stbuf, 0);
free(appleDoubleName);
}
// subdirectories
for(i = 0; i < _fusemfs_vol->numFolders; i++) {
subdir = &_fusemfs_vol->folders[i];
if (subdir->fdParent != dir->fdID) continue;
mfs_folder_stat(subdir, &stbuf);
mfs_to_utf8(subdir->fdCNam, fname, sizeof fname);
filler(buf, fname, &stbuf, 0);
}
}
return 0;
}
static int fusemfs_open (const char *path, struct fuse_file_info *fi) {
// find record and fork
// TODO: check that path really exists if folders are on
dprintf("open %s\n", path);
char *pathdup = strdup(path);
char *fname = utf8_to_mfs(basename(pathdup));
MFSDirectoryRecord *rec = mfs_directory_find_name(_fusemfs_vol->directory, fname);
free(pathdup);
int mode = kMFSForkData;
if ((rec == NULL) && (strncmp(fname, "._", 2) == 0)) {
// open as AppleDouble
rec = mfs_directory_find_name(_fusemfs_vol->directory, fname+2);
mode = kMFSForkAppleDouble;
}
free(fname);
if (rec == NULL) return -ENOENT;
// open
MFSFork *fk = mfs_fkopen(_fusemfs_vol, rec, mode, 0);
if (fk == NULL) return (errno*-1);
fi->fh = (uint64_t)fk;
return 0;
}
static int fusemfs_release (const char *path, struct fuse_file_info *fi) {
dprintf("close %s\n", path); fflush(stderr);
MFSFork *fk = (MFSFork*)fi->fh;
if (mfs_fkclose(fk) == -1) return (errno*-1);
fi->fh = 0;
return 0;
}
static int fusemfs_read (const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
dprintf("read %s %d@%d\n", path, size, offset);
MFSFork *fk = (MFSFork*)fi->fh;
return mfs_fkread_at(fk, size, (size_t)offset, (void*)buf);
}
static int fusemfs_statfs (const char *path, struct statvfs *stbuf) {
dprintf("statfs %s\n", path);
bzero(stbuf, sizeof(struct statvfs));
stbuf->f_bsize = (unsigned long)_fusemfs_vol->mdb.drAlBlkSiz;
stbuf->f_frsize = (unsigned long)_fusemfs_vol->mdb.drAlBlkSiz;
stbuf->f_blocks = (fsblkcnt_t)_fusemfs_vol->mdb.drNmAlBlks;
stbuf->f_bfree = stbuf->f_bavail = (fsblkcnt_t)_fusemfs_vol->mdb.drFreeBks;
stbuf->f_files = (fsfilcnt_t)(_fusemfs_vol->mdb.drNmFls + _fusemfs_vol->numFolders + 1);
stbuf->f_ffree = stbuf->f_favail = 0;
stbuf->f_namemax = 255;
return 0;
}
#if 0
#pragma mark -
#pragma mark Glue
#endif
int mfs_record_stat (MFSMasterDirectoryBlock *mdb, MFSDirectoryRecord *rec, struct stat *stbuf, int mode) {
bzero(stbuf, sizeof(struct stat));
if (rec == NULL) return -1;
stbuf->st_ino = rec->flFlNum;
stbuf->st_mode = S_IFREG | 0777;
stbuf->st_nlink = 1;
if (mode == kMFSForkData) stbuf->st_size = rec->flLgLen;
if (mode == kMFSForkRsrc) stbuf->st_size = rec->flRLgLen;
if (mode == kMFSForkAppleDouble) stbuf->st_size = rec->flRLgLen + kAppleDoubleHeaderLength;
stbuf->st_mtime = mfs_time(rec->flMdDat);
return 0;
}
int mfs_root_stat (MFSMasterDirectoryBlock *mdb, struct stat *stbuf) {
bzero(stbuf, sizeof(struct stat));
if (!options.flat) {
MFSFolder *root = mfs_folder_find(_fusemfs_vol, kMFSFolderRoot);
stbuf->st_nlink = root->fdSubdirs + 2;
} else stbuf->st_nlink = 2;
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_mtime = mfs_time(mdb->drCrDate);
stbuf->st_size = mdb->drAlBlkSiz * mdb->drNmAlBlks;
stbuf->st_blksize = mdb->drAlBlkSiz;
stbuf->st_blocks = mdb->drNmAlBlks - mdb->drFreeBks;
return 0;
}
int mfs_folder_stat (MFSFolder *folder, struct stat *stbuf) {
bzero(stbuf, sizeof(struct stat));
stbuf->st_nlink = folder->fdSubdirs + 2;
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_mtime = mfs_time(folder->fdMdDat);
return 0;
}