~ubuntu-branches/ubuntu/saucy/fuse-umfuse-ext2/saucy

« back to all changes in this revision

Viewing changes to fuse-ext2/fuse-ext2.c

  • Committer: Package Import Robot
  • Author(s): Ludovico Gardenghi, Filippo Giunchedi, Ludovico Gardenghi
  • Date: 2012-06-24 20:58:20 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120624205820-d9avp9pa6zgiyac7
Tags: 0.4-1
[ Filippo Giunchedi ]
* Add DM-Upload-Allowed field

[ Ludovico Gardenghi ]
* New upstream release
  + Source code completely changed, now it's a fork of fuse-ext2 by
    Alper Akcan
  + Update copyright file accordingly
  + Resolve old codebase issues (Closes: #562177, #670622)
* Update to S-V 3.9.3
* Switch to machine-readable copyright file
* Update mail address for Ludovico Gardenghi
* Add compatibility symlinks (old version used fuseext2, this one uses
  fuse-ext2)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright (c) 2008-2010 Alper Akcan <alper.akcan@gmail.com>
 
3
 * Copyright (c) 2009-2010 Renzo Davoli <renzo@cs.unibo.it>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program (in the main directory of the fuse-ext2
 
17
 * distribution in the file COPYING); if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 */
 
20
 
 
21
#include "fuse-ext2.h"
 
22
 
 
23
FUSE_EXT2_DEFINE_MUTEX;
 
24
 
 
25
static const char *V2 = "virtualsquare wiki: http://wiki.virtualsquare.org";
 
26
static const char *HOME = "original fuse-ext2 home: http://sourceforge.net/projects/fuse-ext2/";
 
27
 
 
28
#if __FreeBSD__ == 10
 
29
static char def_opts[] = "allow_other,local,";
 
30
static char def_opts_rd[] = "noappledouble,";
 
31
#else
 
32
static char def_opts[] = "";
 
33
static char def_opts_rd[] = "";
 
34
#endif
 
35
 
 
36
static const char *usage_msg =
 
37
"\n"
 
38
"%s %s %d - FUSE EXT2FS Driver\n"
 
39
"\n"
 
40
"Copyright (C) 2008-2010 Alper Akcan <alper.akcan@gmail.com>\n"
 
41
"Copyright (C) 2009 Renzo Davoli <renzo@cs.unibo.it>\n"
 
42
"(The version number is for the VirtualSquare fork only)\n"
 
43
"\n"
 
44
"Usage:    fuse-ext2 <device|image_file> <mount_point> [-o option[,...]]\n"
 
45
"\n"
 
46
"Options:  ro, force, allow_others\n"
 
47
"          Please see details in the manual.\n"
 
48
"\n"
 
49
"Example:  fuse-ext2 /dev/sda1 /mnt/sda1\n"
 
50
"\n"
 
51
"%s\n"
 
52
"%s\n"
 
53
"\n";
 
54
 
 
55
static int strappend (char **dest, const char *append)
 
56
{
 
57
        char *p;
 
58
        size_t size;
 
59
 
 
60
        if (!dest) {
 
61
                return -1;
 
62
        }
 
63
        if (!append) {
 
64
                return 0;
 
65
        }
 
66
 
 
67
        size = strlen(append) + 1;
 
68
        if (*dest) {
 
69
                size += strlen(*dest);
 
70
        }
 
71
 
 
72
        p = realloc(*dest, size);
 
73
        if (!p) {
 
74
                debugf_main("Memory realloction failed");
 
75
                return -1;
 
76
        }
 
77
 
 
78
        if (*dest) {
 
79
                strcat(p, append);
 
80
        } else {
 
81
                strcpy(p, append);
 
82
        }
 
83
        *dest = p;
 
84
 
 
85
        return 0;
 
86
}
 
87
 
 
88
static void usage (void)
 
89
{
 
90
        printf(usage_msg, PACKAGE, VERSION, fuse_version(), V2, HOME);
 
91
}
 
92
 
 
93
static int parse_options (int argc, char *argv[], struct extfs_data *opts)
 
94
{
 
95
        int c;
 
96
 
 
97
        static const char *sopt = "o:hv";
 
98
        static const struct option lopt[] = {
 
99
                { "options",     required_argument,     NULL, 'o' },
 
100
                { "help",        no_argument,           NULL, 'h' },
 
101
                { "verbose",     no_argument,           NULL, 'v' },
 
102
                { NULL,          0,                     NULL,  0  }
 
103
        };
 
104
 
 
105
#if 0
 
106
        printf("arguments;\n");
 
107
        for (c = 0; c < argc; c++) {
 
108
                printf("%d: %s\n", c, argv[c]);
 
109
        }
 
110
        printf("done\n");
 
111
#endif
 
112
 
 
113
        opterr = 0; /* We'll handle the errors, thank you. */
 
114
 
 
115
        while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) {
 
116
                switch (c) {
 
117
                        case 'o':
 
118
                                if (opts->options)
 
119
                                        if (strappend(&opts->options, ","))
 
120
                                                return -1;
 
121
                                if (strappend(&opts->options, optarg))
 
122
                                        return -1;
 
123
                                break;
 
124
                        case 'h':
 
125
                                usage();
 
126
                                exit(9);
 
127
                        case 'v':
 
128
                                /*
 
129
                                 * We must handle the 'verbose' option even if
 
130
                                 * we don't use it because mount(8) passes it.
 
131
                                 */
 
132
                                opts->debug = 1;
 
133
                                break;
 
134
                        default:
 
135
                                debugf_main("Unknown option '%s'", argv[optind - 1]);
 
136
                                return -1;
 
137
                }
 
138
        }
 
139
 
 
140
        if (optind < argc) {
 
141
                optarg=argv[optind++];
 
142
                if (optarg[0] != '/') {
 
143
                        char fulldevice[PATH_MAX+1];
 
144
                        if (!realpath(optarg, fulldevice)) {
 
145
                                debugf_main("Cannot mount %s", optarg);
 
146
                                free(opts->device);
 
147
                                opts->device = NULL;
 
148
                                return -1;
 
149
                        } else
 
150
                                opts->device = strdup(fulldevice);
 
151
                } else
 
152
                        opts->device = strdup(optarg);
 
153
        }
 
154
 
 
155
        if (optind < argc) {
 
156
                opts->mnt_point = argv[optind++];
 
157
        }
 
158
 
 
159
        if (optind < argc) {
 
160
                debugf_main("You must specify exactly one device and exactly one mount point");
 
161
                return -1;
 
162
        }
 
163
 
 
164
        if (!opts->device) {
 
165
                debugf_main("No device is specified");
 
166
                return -1;
 
167
        }
 
168
        if (!opts->mnt_point) {
 
169
                debugf_main("No mountpoint is specified");
 
170
                return -1;
 
171
        }
 
172
 
 
173
        return 0;
 
174
}
 
175
 
 
176
static char * parse_mount_options (const char *orig_opts, struct extfs_data *opts)
 
177
{
 
178
        char *options, *s, *opt, *val, *ret;
 
179
 
 
180
        ret = malloc(strlen(def_opts) + strlen(def_opts_rd) + strlen(orig_opts) + 256 + PATH_MAX);
 
181
        if (!ret) {
 
182
                return NULL;
 
183
        }
 
184
 
 
185
        *ret = 0;
 
186
        options = strdup(orig_opts);
 
187
        if (!options) {
 
188
                debugf_main("strdup failed");
 
189
                return NULL;
 
190
        }
 
191
 
 
192
        s = options;
 
193
        while (s && *s) {
 
194
                val = strsep(&s, ",");
 
195
                opt = strsep(&val, "=");
 
196
                if (!strcmp(opt, "ro")) { /* Read-only mount. */
 
197
                        if (val) {
 
198
                                debugf_main("'ro' option should not have value");
 
199
                                goto err_exit;
 
200
                        }
 
201
                        opts->readonly = 1;
 
202
                } else if (!strcmp(opt, "rw")) { /* Read-write mount */
 
203
                        if (val) {
 
204
                                debugf_main("'rw' option should not have value");
 
205
                                goto err_exit;
 
206
                        }
 
207
                        opts->readonly = 0;
 
208
                } else if (!strcmp(opt, "rw+")) { /* Read-write mount */
 
209
                        if (val) {
 
210
                                debugf_main("'rw+' option should not have value");
 
211
                                goto err_exit;
 
212
                        }
 
213
                        opts->readonly = 0;
 
214
                        opts->force = 1;
 
215
                } else if (!strcmp(opt, "debug")) { /* enable debug */
 
216
                        if (val) {
 
217
                                debugf_main("'debug' option should not have value");
 
218
                                goto err_exit;
 
219
                        }
 
220
                        opts->debug = 1;
 
221
                        strcat(ret, "debug,");
 
222
                } else if (!strcmp(opt, "silent")) { /* keep silent */
 
223
                        if (val) {
 
224
                                debugf_main("'silent' option should not have value");
 
225
                                goto err_exit;
 
226
                        }
 
227
                        opts->silent = 1;
 
228
                } else if (!strcmp(opt, "force")) { /* enable read/write */
 
229
                        if (val) {
 
230
                                debugf_main("'force option should no have value");
 
231
                                goto err_exit;
 
232
                        }
 
233
                        opts->force = 1;
 
234
#if __FreeBSD__ == 10
 
235
                        strcat(ret, "force,");
 
236
#endif
 
237
                } else { /* Probably FUSE option. */
 
238
                        strcat(ret, opt);
 
239
                        if (val) {
 
240
                                strcat(ret, "=");
 
241
                                strcat(ret, val);
 
242
                        }
 
243
                        strcat(ret, ",");
 
244
                }
 
245
        }
 
246
 
 
247
        if (opts->readonly == 0 && opts->force == 0) {
 
248
                fprintf(stderr, "Mounting %s Read-Only.\nUse \'force\' or \'rw+\' options to enable Read-Write mode\n",opts->device);
 
249
                opts->readonly = 1;
 
250
        }
 
251
 
 
252
        strcat(ret, def_opts);
 
253
        if (opts->readonly == 1) {
 
254
                strcat(ret, def_opts_rd);
 
255
                strcat(ret, "ro,");
 
256
        } else 
 
257
                strcat(ret, "rw,");
 
258
        strcat(ret, "fsname=");
 
259
        strcat(ret, opts->device);
 
260
#if __FreeBSD__ == 10
 
261
        strcat(ret, ",fstypename=");
 
262
        strcat(ret, "ext2");
 
263
        strcat(ret, ",volname=");
 
264
        if (opts->volname == NULL || opts->volname[0] == '\0') {
 
265
                s = strrchr(opts->device, '/');
 
266
                if (s != NULL) {
 
267
                        strcat(ret, s + 1);
 
268
                } else {
 
269
                        strcat(ret, opts->device);
 
270
                }
 
271
        } else {
 
272
                strcat(ret, opts->volname);
 
273
        }
 
274
#endif
 
275
exit:
 
276
        free(options);
 
277
        return ret;
 
278
err_exit:
 
279
        free(ret);
 
280
        ret = NULL;
 
281
        goto exit;
 
282
}
 
283
 
 
284
static const struct fuse_operations ext2fs_ops = {
 
285
        .getattr        = op_getattr,
 
286
        .readlink       = op_readlink,
 
287
        .mknod          = op_mknod,
 
288
        .mkdir          = op_mkdir,
 
289
        .unlink         = op_unlink,
 
290
        .rmdir          = op_rmdir,
 
291
        .symlink        = op_symlink,
 
292
        .rename         = op_rename,
 
293
        .link           = op_link,
 
294
        .chmod          = op_chmod,
 
295
        .chown          = op_chown,
 
296
        .truncate       = op_truncate,
 
297
        .open           = op_open,
 
298
        .read           = op_read,
 
299
        .write          = op_write,
 
300
        .statfs         = op_statfs,
 
301
        .flush          = op_flush,
 
302
        .release        = op_release,
 
303
        .fsync          = op_fsync,
 
304
        .setxattr       = NULL,
 
305
        .getxattr       = NULL,
 
306
        .listxattr      = NULL,
 
307
        .removexattr    = NULL,
 
308
        .opendir        = op_open,
 
309
        .readdir        = op_readdir,
 
310
        .releasedir     = op_release,
 
311
        .fsyncdir       = op_fsync,
 
312
        .init           = op_init,
 
313
        .destroy        = op_destroy,
 
314
        .access         = op_access,
 
315
        .create         = op_create,
 
316
        .ftruncate      = op_ftruncate,
 
317
        .fgetattr       = op_fgetattr,
 
318
        .lock           = NULL,
 
319
        .utimens        = op_utimens,
 
320
        .bmap           = NULL,
 
321
};
 
322
 
 
323
int main (int argc, char *argv[])
 
324
{
 
325
        int err = 0;
 
326
        struct stat sbuf;
 
327
        char *parsed_options = NULL;
 
328
        struct fuse_args fargs = FUSE_ARGS_INIT(0, NULL);
 
329
        struct extfs_data opts;
 
330
 
 
331
        debugf("version:'%s', fuse_version:'%d'", VERSION, fuse_version());
 
332
 
 
333
        memset(&opts, 0, sizeof(opts));
 
334
 
 
335
        if (parse_options(argc, argv, &opts)) {
 
336
                usage();
 
337
                return -1;
 
338
        }
 
339
 
 
340
        if (stat(opts.device, &sbuf)) {
 
341
                debugf_main("Failed to access '%s'", opts.device);
 
342
                err = -3;
 
343
                goto err_out;
 
344
        }
 
345
 
 
346
        if (do_probe(&opts) != 0) {
 
347
                debugf_main("Probe failed");
 
348
                err = -4;
 
349
                goto err_out;
 
350
        }
 
351
 
 
352
        parsed_options = parse_mount_options(opts.options ? opts.options : "", &opts);
 
353
        if (!parsed_options) {
 
354
                err = -2;
 
355
                goto err_out;
 
356
        }
 
357
 
 
358
        debugf_main("opts.device: %s", opts.device);
 
359
        debugf_main("opts.mnt_point: %s", opts.mnt_point);
 
360
        debugf_main("opts.volname: %s", (opts.volname != NULL) ? opts.volname : "");
 
361
        debugf_main("opts.options: %s", opts.options);
 
362
        debugf_main("parsed_options: %s", parsed_options);
 
363
 
 
364
        if (fuse_opt_add_arg(&fargs, PACKAGE) == -1 ||
 
365
            fuse_opt_add_arg(&fargs, "-s") == -1 ||
 
366
            fuse_opt_add_arg(&fargs, "-o") == -1 ||
 
367
            fuse_opt_add_arg(&fargs, parsed_options) == -1 ||
 
368
            fuse_opt_add_arg(&fargs, opts.mnt_point) == -1) {
 
369
                debugf_main("Failed to set FUSE options");
 
370
                fuse_opt_free_args(&fargs);
 
371
                err = -5;
 
372
                goto err_out;
 
373
        }
 
374
 
 
375
        if (opts.readonly == 0) {
 
376
                debugf_main("mounting read-write");
 
377
        } else {
 
378
                debugf_main("mounting read-only");
 
379
        }
 
380
 
 
381
        fuse_main(fargs.argc, fargs.argv, &ext2fs_ops, &opts);
 
382
 
 
383
err_out:
 
384
        fuse_opt_free_args(&fargs);
 
385
        free(parsed_options);
 
386
        free(opts.options);
 
387
        free(opts.device);
 
388
        free(opts.volname);
 
389
        return err;
 
390
}