File: fuseiso.c

package info (click to toggle)
fuseiso 20070708-3+deb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,828 kB
  • sloc: sh: 9,081; ansic: 2,182; makefile: 14
file content (421 lines) | stat: -rw-r--r-- 11,991 bytes parent folder | download | duplicates (2)
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
/***************************************************************************
 *   Copyright (C) 2005, 2006 by Dmitry Morozhnikov   *
 *   dmiceman@mail.ru   *
 *                                                                         *
 *   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.             *
 ***************************************************************************/


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <linux/stat.h>
#include <unistd.h>
#include <getopt.h>
#include <mntent.h>
#include <sys/param.h>
#include <pwd.h>

#include <linux/iso_fs.h>

#define FUSE_USE_VERSION 22
#include <fuse.h>

#include <zlib.h>
#include <locale.h>
#include <langinfo.h>

#include "isofs.h"

#ifdef __GNUC__
# define UNUSED(x) x __attribute__((unused))
#else
# define UNUSED(x) x
#endif

static char *imagefile = NULL;
static char *mount_point = NULL;
static int image_fd = -1;

int maintain_mount_point;
int maintain_mtab;
char* iocharset;

char* normalize_name(const char* fname) {
    char* abs_fname = (char *) malloc(PATH_MAX);
    realpath(fname, abs_fname);
    // ignore errors from realpath()
    return abs_fname;
};

int check_mount_point() {
    struct stat st;
    int rc = lstat(mount_point, &st);
    if(rc == -1 && errno == ENOENT) {
        // directory does not exists, createcontext
        rc = mkdir(mount_point, 0777); // let`s underlying filesystem manage permissions
        if(rc != 0) {
            perror("Can`t create mount point");
            return -EIO;
        };
    } else if(rc == -1) {
        perror("Can`t check mount point");
        return -1;
    };
    return 0;
};

void del_mount_point() {
    int rc = rmdir(mount_point);
    if(rc != 0) {
        perror("Can`t delete mount point");
    };
};

char* get_mtab_path() {
    char* mtab_path = (char*) malloc(PATH_MAX);
    uid_t uid = getuid();
    struct passwd* passwd = getpwuid(uid);
    if(!passwd) {
        fprintf(stderr, "Can`t get home directory for user %d: %s\n", uid, strerror(errno));
        return NULL;
    };
    mtab_path[0] = 0;
    if(passwd->pw_dir) { // may be NULL, who know..
        strncpy(mtab_path, passwd->pw_dir, PATH_MAX - 16);
        mtab_path[PATH_MAX - 1] = 0;
    };
    strcat(mtab_path, "/.mtab.fuseiso");
    return mtab_path;
};

int add_mtab_record() {
    int rc;
    char* mtab_path = get_mtab_path();
    if(!mtab_path) {
        return -EIO;
    };
    int fd = open(mtab_path, O_RDWR | O_CREAT, 0644);
    if(fd < 0) {
        perror("Can`t open mtab");
        return -EIO;
    };
    rc = lockf(fd, F_LOCK, 0);
    if(rc != 0) {
        perror("Can`t lock mtab");
        return -EIO;
    };
    FILE* mtab = setmntent(mtab_path, "a");
    if(!mtab) {
        perror("Can`t open mtab");
        return -EIO;
    };
    struct mntent ent;
    ent.mnt_fsname = imagefile;
    ent.mnt_dir = mount_point;
    ent.mnt_type = "fuseiso";
    ent.mnt_opts = "defaults";
    ent.mnt_freq = 0;
    ent.mnt_passno = 0;    
    rc = addmntent(mtab, &ent);
    if(rc != 0) {
        perror("Can`t add mtab entry");
        return -EIO;
    };
    endmntent(mtab);
    rc = lockf(fd, F_ULOCK, 0);
    if(rc != 0) {
        perror("Can`t unlock mtab");
        return -EIO;
    };
    close(fd);
    free(mtab_path);
    return 0;
};

int del_mtab_record() {
    int rc;
    char* mtab_path = get_mtab_path();
    char new_mtab_path[PATH_MAX];
    if(!mtab_path) {
        return -EIO;
    };
    int fd = open(mtab_path, O_RDWR | O_CREAT, 0644);
    if(fd < 0) {
        perror("Can`t open mtab");
        return -EIO;
    };
    rc = lockf(fd, F_LOCK, 0);
    if(rc != 0) {
        perror("Can`t lock mtab");
        return -EIO;
    };
    strncpy(new_mtab_path, mtab_path, PATH_MAX - 16);
    new_mtab_path[PATH_MAX - 1] = 0;
    strcat(new_mtab_path, ".new");
    FILE* mtab = setmntent(mtab_path, "r");
    if(!mtab) {
        perror("Can`t open mtab");
        return -EIO;
    };
    FILE* new_mtab = setmntent(new_mtab_path, "a+");
    if(!new_mtab) {
        perror("Can`t open new mtab");
        return -EIO;
    };
    struct mntent* ent;
    while((ent = getmntent(mtab)) != NULL) {
        if(strcmp(ent->mnt_fsname, imagefile) == 0 && 
            strcmp(ent->mnt_dir, mount_point) == 0 &&
            strcmp(ent->mnt_type, "fuseiso") == 0) {
            // skip
        } else {
            rc = addmntent(new_mtab, ent);
            if(rc != 0) {
                perror("Can`t add mtab entry");
                return -EIO;
            };
        };
    };
    endmntent(mtab);
    endmntent(new_mtab);
    rc = rename(new_mtab_path, mtab_path);
    if(rc != 0) {
        perror("Can`t rewrite mtab");
        return -EIO;
    };
    rc = lockf(fd, F_ULOCK, 0);
    if(rc != 0) {
        perror("Can`t unlock mtab");
        return -EIO;
    };
    close(fd);
    free(mtab_path);
    return 0;
};

static int isofs_getattr(const char *path, struct stat *stbuf)
{
    return isofs_real_getattr(path, stbuf);
}

static int isofs_readlink(const char *path, char *target, size_t size) {
    return isofs_real_readlink(path, target, size);
};

static int isofs_open(const char *path, struct fuse_file_info *UNUSED(fi))
{
    return isofs_real_open(path);
}

static int isofs_read(const char *path, char *buf, size_t size,
                     off_t offset, struct fuse_file_info *UNUSED(fi))
{
    return isofs_real_read(path, buf, size, offset);
}

static int isofs_flush(const char *UNUSED(path), struct fuse_file_info *UNUSED(fi)) {
    return 0;
};

static void* isofs_init() {
    int rc;
    if(maintain_mtab) {
        rc = add_mtab_record();
        if(rc != 0) {
            exit(EXIT_FAILURE);
        };
    };
    return isofs_real_init();
};

static void isofs_destroy(void* param) {
    if(maintain_mtab) {
        del_mtab_record();
    };
    return;
};

static int isofs_opendir(const char *path, struct fuse_file_info *UNUSED(fi)) {
    return isofs_real_opendir(path);
};

static int isofs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t UNUSED(offset),
    struct fuse_file_info *UNUSED(fi)) {
    return isofs_real_readdir(path, buf, filler);
};

static int isofs_statfs(const char *UNUSED(path), struct statfs *stbuf)
{
    return isofs_real_statfs(stbuf);
}

static struct fuse_operations isofs_oper = {
    .getattr    = isofs_getattr,
    .readlink   = isofs_readlink,
    .open       = isofs_open,
    .read       = isofs_read,
    .flush      = isofs_flush,
    .init       = isofs_init,
    .destroy    = isofs_destroy,
    .opendir    = isofs_opendir,
    .readdir    = isofs_readdir,
    .statfs     = isofs_statfs,
};

void usage(const char* prog) {
    printf("Version: %s\nUsage: %s [-n] [-p] [-c <iocharset>] [-h] <isofs_image_file> <mount_point> [<FUSE library options>]\n"
        "Where options are:\n"
        "    -n                 -- do NOT maintain file $HOME/.mtab.fuseiso\n"
        "    -p                 -- maintain mount point; \n"
        "                          create it if it does not exists and delete it on exit\n"
        "    -c <iocharset>     -- specify iocharset for Joliet filesystem\n"
        "    -h                 -- print this screen\n"
        "\nCommon FUSE library options are:\n"
        "    -f                 -- run in foreground, do not daemonize\n"
        "    -d                 -- run in foreground and print debug information\n"
        "    -s                 -- run single-threaded\n"
        "\nPlease consult with FUSE documentation for more information\n",
        VERSION, 
        prog);
};

int main(int argc, char *argv[])
{
    setlocale(LC_ALL, ""); // set current locale for proper iocharset
    
    // defaults
    maintain_mount_point = 0;
    maintain_mtab = 1;
    iocharset = NULL;
    
    char c;
    while((c = getopt(argc, argv, "+npc:h")) > 0) {
        switch(c) {
            case 'n':
                maintain_mtab = 0;
                break;
            case 'p':
                maintain_mount_point = 1;
                break;
            case 'c':
                if(optarg) {
                    iocharset = optarg;
                };
                break;
            case 'h':
                usage(argv[0]);
                exit(0);
                break;
            case '?':
            case ':':
                usage(argv[0]);
                exit(EXIT_FAILURE);
                break;
        };
    };
    
    if((argc - optind) < 2) {
        usage(argv[0]);
        exit(EXIT_FAILURE);
    };
    
    imagefile = normalize_name(argv[optind]);
    
    image_fd = open(imagefile, O_RDONLY);
    if(image_fd == -1) {
        fprintf(stderr, "Supplied image file name: \"%s\"\n", imagefile);
        perror("Can`t open image file");
        exit(EXIT_FAILURE);
    };
    
    mount_point = normalize_name(argv[optind + 1]);
    
    // with space for possible -o use_ino arguments
    char **nargv = (char **) malloc((argc + 2) * sizeof(char *));
    int nargc = argc - optind;
    
    nargv[0] = argv[0];
    
    int i;
    int next_opt = 0;
    int use_ino_found = 0;
    for(i = 0; i < nargc - 1; ++i) {
        if(next_opt && !use_ino_found) {
            if(strstr(argv[i + optind + 1], "use_ino")) { // ok, already there
                use_ino_found = 1;
                nargv[i + 1] = argv[i + optind + 1];
            } else { // add it
                char* str = (char*) malloc(strlen(argv[i + optind + 1]) + 10);
                strcpy(str, argv[i + optind + 1]);
                strcat(str, ",use_ino");
                nargv[i + 1] = str;
                use_ino_found = 1;
            };
        } else {
            nargv[i + 1] = argv[i + optind + 1];
        };
        // check if this is -o string mean that next argument should be options string
        if(i > 1 && nargv[i + 1][0] == '-' && nargv[i + 1][1] == 'o') {
            next_opt = 1;
        };
    };
    if(!use_ino_found) {
        nargv[nargc] = "-o";
        nargc++;
        nargv[nargc] = "use_ino";
        nargc++;
    };
    
    if(!iocharset) {
        char *nlcharset = nl_langinfo(CODESET);
        if(nlcharset) {
            iocharset = (char *) malloc(strlen(nlcharset) + 9);
            strcpy(iocharset, nlcharset);
            strcat(iocharset, "//IGNORE");
        };
        if(!iocharset) {
            iocharset = "UTF-8//IGNORE";
        };
    };
    
    int rc;
    if(maintain_mount_point) {
        rc = check_mount_point();
        if(rc != 0) {
            exit(EXIT_FAILURE);
        };
    };
    if(maintain_mount_point) {
        rc = atexit(del_mount_point);
        if(rc != 0) {
            fprintf(stderr, "Can`t set exit function\n");
            exit(EXIT_FAILURE);
        }
    };
    
    // will exit in case of failure
    rc = isofs_real_preinit(imagefile, image_fd);
    
    return fuse_main(nargc, nargv, &isofs_oper);
};