~jeanfrancois.roy/mpqkit/1.0b1

« back to all changes in this revision

Viewing changes to mpqfs/mpqfsd.m

  • Committer: bahamut
  • Date: 2007-04-03 01:12:37 UTC
  • Revision ID: svn-v4:08a27de9-96f8-0310-9aec-cd9f9b5b01a8:trunk:158
- svn refactor phase 2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  mpqfsd.m
 
3
//  MPQKit
 
4
//
 
5
//  Created by Jean-Francois Roy on 26/03/2007.
 
6
//  Copyright MacStorm 2007. All rights reserved.
 
7
//
 
8
 
 
9
#import <Foundation/Foundation.h>
 
10
#import <unistd.h>
 
11
 
 
12
#import "MPQFileSystem.h"
 
13
 
 
14
#define FUSE_USE_VERSION 26
 
15
#define _FILE_OFFSET_BITS 64
 
16
#import <fuse.h>
 
17
 
 
18
static NSString *archive_path = nil;
 
19
static NSMutableArray *listfiles = nil;
 
20
static NSString *mount_point = nil;
 
21
 
 
22
static BOOL has_volname = NO;
 
23
static NSString *mount_icon = nil;
 
24
 
 
25
enum {
 
26
    KEY_LISTFILE,
 
27
    KEY_VOLICON,
 
28
    KEY_HELP,
 
29
    KEY_VERSION,
 
30
};
 
31
 
 
32
#define MPQFS_OPT(t, p, v) { t, offsetof(struct sshfs, p), v }
 
33
static struct fuse_opt mpqfs_opts[] = {
 
34
    FUSE_OPT_KEY("-l ",         KEY_LISTFILE),
 
35
    FUSE_OPT_KEY("--listfile=", KEY_LISTFILE),
 
36
    FUSE_OPT_KEY("-i ",         KEY_VOLICON),
 
37
    FUSE_OPT_KEY("--icon=",     KEY_VOLICON),
 
38
    FUSE_OPT_KEY("--version",   KEY_VERSION),
 
39
    FUSE_OPT_KEY("-h",          KEY_HELP),
 
40
    FUSE_OPT_KEY("--help",      KEY_HELP),
 
41
    FUSE_OPT_END
 
42
};
 
43
 
 
44
static void usage(const char *program) {
 
45
    fprintf(stderr, 
 
46
"usage: %s archive mountpoint [options]\n"
 
47
"\n"
 
48
"general options:\n"
 
49
"    -o opt,[opt...]        mount options\n"
 
50
"    -h   --help            print help\n"
 
51
"    -V   --version         print version\n"
 
52
"\n"
 
53
"MPQFS options:\n"
 
54
"    -l   --listfile        supply an external listfile to MPQFS\n"
 
55
"    -i   --icon            specify a volume icon file for the Finder\n"
 
56
"\n", program);
 
57
}
 
58
 
 
59
static struct fuse_operations dummy_opts;
 
60
 
 
61
static int mpqfs_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs) {
 
62
    switch (key) {
 
63
        case FUSE_OPT_KEY_OPT:
 
64
            if (strstr(arg, "volname") != NULL) {
 
65
                has_volname = YES;
 
66
            }
 
67
            return 1;
 
68
            
 
69
        case FUSE_OPT_KEY_NONOPT:
 
70
            if (!archive_path) {
 
71
                archive_path = [[NSString alloc] initWithCString:arg encoding:NSUTF8StringEncoding];
 
72
                return 0;
 
73
            }
 
74
            if (!mount_point) {
 
75
                mount_point = [[NSString alloc] initWithCString:arg encoding:NSUTF8StringEncoding];
 
76
                return 1;
 
77
            }
 
78
            return 1;
 
79
        
 
80
        case KEY_LISTFILE:
 
81
            NSLog(@"KEY_LISTFILE: %s", arg);
 
82
            return 0;
 
83
        
 
84
        case KEY_VOLICON:
 
85
            NSLog(@"KEY_VOLICON: %s", arg);
 
86
            [mount_icon release]; mount_icon = nil;
 
87
            if (strstr(arg, "-i") == arg) mount_icon = [[[NSString stringWithCString:arg + 2 encoding:NSUTF8StringEncoding] stringByStandardizingPath] retain];
 
88
            if (strstr(arg, "--icon=") == arg) mount_icon = [[[NSString stringWithCString:arg + 7 encoding:NSUTF8StringEncoding] stringByStandardizingPath] retain];
 
89
            return 0;
 
90
        
 
91
        case KEY_HELP:
 
92
            usage(outargs->argv[0]);
 
93
            fuse_opt_add_arg(outargs, "-ho");
 
94
            fuse_main(outargs->argc, outargs->argv, &dummy_opts, NULL);
 
95
            exit(1);
 
96
            
 
97
        case KEY_VERSION:
 
98
            fprintf(stderr, "MPQFS version %s\n", "0.1");
 
99
            fuse_opt_add_arg(outargs, "--version");
 
100
            fuse_main(outargs->argc, outargs->argv, &dummy_opts, NULL);
 
101
            exit(0);
 
102
            
 
103
        default:
 
104
            fprintf(stderr, "internal error\n");
 
105
            abort();
 
106
    }
 
107
}
 
108
 
 
109
int main(int argc, char *argv[]) {
 
110
    NSAutoreleasePool *p = [NSAutoreleasePool new];
 
111
    NSError *error = nil;
 
112
    
 
113
    struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
 
114
    listfiles = [[NSMutableArray alloc] init];
 
115
    
 
116
    if (fuse_opt_parse(&args, NULL, mpqfs_opts, mpqfs_opt_proc) == -1) exit(1);
 
117
    
 
118
    if (archive_path == nil) {
 
119
        fprintf(stderr, "missing archive path\n");
 
120
        fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
 
121
        goto Exit1;
 
122
    }
 
123
    
 
124
    if (mount_point == nil) {
 
125
        fprintf(stderr, "missing mount point\n");
 
126
        fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
 
127
        goto Exit1;
 
128
    }
 
129
    
 
130
    MPQArchive *archive = [[MPQArchive alloc] initWithPath:archive_path error:&error];
 
131
    if (!archive) {
 
132
        fprintf(stderr, "error opening archive: %s\n", [[error description] UTF8String]);
 
133
        fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
 
134
        goto Exit1;
 
135
    }
 
136
    [archive_path release];
 
137
    
 
138
    if ([listfiles count] > 0) {
 
139
        
 
140
    }
 
141
    [listfiles release];
 
142
    
 
143
    MPQFileSystem *fs = [[MPQFileSystem alloc] initWithArchive:archive mountPoint:mount_point arguments:&args error:&error];
 
144
    if (!fs) {
 
145
        fprintf(stderr, "error creating MPQ filesystem: %s\n", [[error description] UTF8String]);
 
146
        [archive release];
 
147
        goto Exit1;
 
148
    }
 
149
    [archive release];
 
150
    [mount_point release];
 
151
    
 
152
    // Set options
 
153
    [fs setValue:[NSNumber numberWithBool:(has_volname) ? NO : YES] forKey:@"overwriteVolname"];
 
154
    if (mount_icon) [fs setValue:mount_icon forKey:@"mountIcon"];
 
155
    [mount_icon release];
 
156
    
 
157
    // Start fuse (does not return until unmount)
 
158
    [fs startFuse];
 
159
    
 
160
    [fs release];
 
161
    [p release];
 
162
    return 0;
 
163
 
 
164
Exit1:
 
165
    fuse_opt_free_args(&args);
 
166
    [p release];
 
167
    exit(1);
 
168
}