File: extfs.c

package info (click to toggle)
avfs 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,916 kB
  • sloc: ansic: 31,364; sh: 6,482; perl: 1,916; makefile: 351
file content (522 lines) | stat: -rw-r--r-- 12,293 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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
    AVFS: A Virtual File System Library
    Copyright (C) 1998  Miklos Szeredi <miklos@szeredi.hu>
    Copyright (C) 2006  Ralf Hoffmann (ralf@boomerangsworld.de)
    
    This program can be distributed under the terms of the GNU GPL.
    See the file COPYING.

    EXTFS module

    This module is partly based on the 'extfs.c' module of 
    Midnight Commander VFS, by Jakub Jelinek and Pavel Machek.
*/

#include "archive.h"
#include "version.h"
#include "filebuf.h"
#include "parsels.h"
#include "realfile.h"
#include "runprog.h"
#include "filecache.h"
#include "cache.h"
#include "exit.h"
#include "tmpfile.h"
#include "operutil.h"

#include <unistd.h>
#include <fcntl.h>

struct extfsdata {
    int needbase;
    char *progpath;
};

struct extfsnode {
    char *fullpath;
    avmutex lock;
};

struct extfscacheentry {
    char *tmpfile;
    avdev_t          basefile_dev;
    avino_t          basefile_ino;
    avtimestruc_t    basefile_mtime;
};

struct extfsfile {
    struct extfscacheentry *cent;
    int fd;
};

static void extfscacheentry_delete(struct extfscacheentry *cent)
{
    if( cent->tmpfile != NULL ) {
        av_del_tmpfile(cent->tmpfile);
    }
}

static void fill_extfs_link(struct archive *arch, struct entry *ent,
                           char *linkname)
{
    struct entry *link;
    struct archnode *nod = NULL;

    link = av_arch_resolve(arch, linkname, 0, 0);
    if(link != NULL)
        nod = (struct archnode *) av_namespace_get(link);

    if(nod == NULL || AV_ISDIR(nod->st.mode))
        av_log(AVLOG_WARNING, "EXTFS: Illegal hard link");
    else {
        nod->st.nlink ++;
        av_namespace_set(ent, nod);
        av_ref_obj(ent);
        av_ref_obj(nod);
    }

    av_unref_obj(link);
}

static void extfsnode_delete(struct extfsnode *enod)
{
    av_free(enod->fullpath);
    AV_FREELOCK(enod->lock);
}

static void fill_extfs_node(struct archive *arch, struct entry *ent, 
                            struct avstat *stbuf, char *path, char *linkname)
{
    struct archnode *nod;
    struct extfsnode *enod;
    char *s;

    nod = av_arch_new_node(arch, ent, AV_ISDIR(stbuf->mode));
        
    stbuf->dev = nod->st.dev;
    stbuf->ino = nod->st.ino;
    stbuf->nlink = nod->st.nlink;

    nod->st = *stbuf;
    nod->offset = 0;
    nod->realsize = 0;

    AV_NEW_OBJ(enod, extfsnode_delete);

    AV_INITLOCK(enod->lock);

    nod->data = enod;

    /* Fullpath should be without leading slashes */
    for(s = path; *s && *s == '/'; s++);
    enod->fullpath = av_strdup(s);

    if(AV_ISLNK(stbuf->mode))
        nod->linkname = av_strdup(linkname);
}


static void insert_extfs_entry(struct archive *arch, struct avstat *stbuf,
			      char *path, char *linkname)
{
    struct entry *ent;

    if(!path[0])
        return;

    ent = av_arch_create(arch, path, 0);
    if(ent == NULL)
        return;

    /* if linkname is not null but mode is not a link
       then this should be a hard link */
    if(linkname != NULL && !AV_ISLNK(stbuf->mode)) 
        fill_extfs_link(arch, ent, linkname);
    else
        fill_extfs_node(arch, ent, stbuf, path, linkname);

    av_unref_obj(ent);
}

static void parse_extfs_line(struct lscache *lc, char *line,
                             struct archive *arch)
{
    int res;
    char *filename;
    char *linkname;
    struct avstat stbuf;
    
    res = av_parse_ls(lc, line, &stbuf, &filename, &linkname);
    if(res != 1)
        return;
    
    insert_extfs_entry(arch, &stbuf, filename, linkname);
    av_free(filename);
    av_free(linkname);
}

static int read_extfs_list(struct program *pr, struct lscache *lc,
                           struct archive *arch)
{
    int res;

    while(1) {
        char *line;

        res = av_program_getline(pr, &line, -1);
        if(res <= 0)
            return res;
        if(line == NULL)
            return 0;
        parse_extfs_line(lc, line, arch);
        av_free(line);
    }
}

static int extfs_list(void *data, ventry *ve, struct archive *arch)
{
    int res;
    const char *prog[4];
    struct realfile *rf;
    struct program *pr;
    struct extfsdata *info = (struct extfsdata *) data;    

    if(info->needbase) {
        res = av_get_realfile(ve->mnt->base, &rf);
        if(res < 0)
            return res;
    }
    else
        rf = NULL;
    
    prog[0] = info->progpath;
    prog[1] = "list";
    prog[2] = rf == NULL ? NULL : rf->name;
    prog[3] = NULL;

    res = av_start_program(prog, &pr);
    if(res == 0) {
        struct lscache *lc = av_new_lscache();
        res = read_extfs_list(pr, lc, arch);
        av_unref_obj(lc);
        av_unref_obj(pr);
    }
    av_unref_obj(rf);

    return res;
}

static int get_key_for_node(ventry *ve, struct archfile *fil, char **resp)
{
    struct extfsnode *enod = (struct extfsnode *) fil->nod->data;
    char *key;
    int res;

    if(enod == NULL) {
        return -EISDIR;
    }

    res = av_filecache_getkey(ve, &key);
    if(res < 0)
        return res;

    key = av_stradd(key, "/", enod->fullpath, NULL);
    *resp = key;
    return 0;
}

static int get_extfs_file(ventry *ve, struct archfile *fil,
                          const char *tmpfile)
{
    int res;
    struct archparams *ap = (struct archparams *) ve->mnt->avfs->data;
    struct extfsdata *info = (struct extfsdata *) ap->data;
    struct extfsnode *enod = (struct extfsnode *) fil->nod->data;
    const char *prog[6];
    struct realfile *rf;

    if(enod == NULL) {
        /* no extfsnode means someone tries to access the extfs
	   archive as a file (e.g. open( "test.lha#" ) )
	   Although open on a directory is not forbidden we cannot
	   create an appropriate tmpfile so we return EISDIR */
        return -EISDIR;
    }
  
    if(info->needbase) {
        res = av_get_realfile(ve->mnt->base, &rf);
        if(res < 0)
            return res;
    }
    else 
        rf = NULL;
  
    prog[0] = info->progpath;
    prog[1] = "copyout";
    prog[2] = rf == NULL ? "/" : rf->name;
    prog[3] = enod->fullpath;
    prog[4] = tmpfile;
    prog[5] = NULL;
  
    res = av_run_program(prog);
    av_unref_obj(rf);

    return res;
}

static struct ext_info *create_exts(char *line)
{
    struct ext_info *exts;
    char *elist, *newelist;
    int i, n;
  
    while(*line && !isspace((unsigned char) *line)) line++;
    if(*line) *line++ = '\0';
    while(isspace((unsigned char) *line)) line++;
    elist = line;

    for(n = 0; *line && *line != '#'; n++) {
        while(*line && !isspace((unsigned char) *line)) line++;
        while(isspace((unsigned char) *line)) line++;
    }
    if(!n) return NULL;  /* No extensions */
  
    exts = av_malloc((n + 1) * sizeof(*exts) + strlen(elist) + 1);

    newelist = (char *) (&exts[n+1]);
    strcpy(newelist, elist);
  
    for(i = 0; i < n; i++) {
        exts[i].from = newelist;
        exts[i].to   = NULL;
        while(*newelist && !isspace((unsigned char) *newelist)) newelist++;
        if(*newelist) *newelist++ = '\0';
        while(isspace((unsigned char) *newelist)) newelist++;

    }
    exts[n].from = NULL;
    exts[n].to   = NULL;

    return exts;
}

static avssize_t extfs_read(vfile *vf, char *buf, avsize_t nbyte)
{
    avssize_t res;
    struct archfile *fil = arch_vfile_file(vf);
    struct extfsfile *efil = (struct extfsfile *) fil->data;

    if(lseek(efil->fd, vf->ptr, SEEK_SET) == -1)
        return -errno;

    res = read(efil->fd, buf, nbyte);
    if(res == -1)
        return -errno;

    vf->ptr += res;

    return res;
}

static int extfs_open(ventry *ve, struct archfile *fil)
{
    int res;
    struct extfsfile *efil;
    struct extfsnode *enod = (struct extfsnode *) fil->nod->data;
    int fd;
    char *key;
    struct extfscacheentry *cent;
    struct avstat basefile_stat;
    
    /* get key for extfscache */
    res = get_key_for_node(ve, fil, &key);
    if(res < 0)
        return res;

    res = av_file_getattr(fil->basefile, &basefile_stat, AVA_MTIME | AVA_DEV | AVA_INO);
    if (res < 0) return res;

    AV_LOCK(enod->lock);
    cent = av_cache2_get(key);
    if (cent == NULL ||
        cent->basefile_dev != basefile_stat.dev ||
        cent->basefile_ino != basefile_stat.ino ||
        cent->basefile_mtime.sec != basefile_stat.mtime.sec ||
        cent->basefile_mtime.nsec != basefile_stat.mtime.nsec) {
        char *tmpfile;
        avoff_t tmpsize;

	/* no entry in cache so create a temporary file... */
        res = av_get_tmpfile(&tmpfile);
        if(res < 0) {
	    av_free(key);
	    AV_UNLOCK(enod->lock);
            return res;
	}
	res = get_extfs_file(ve, fil, tmpfile);
	if(res < 0) {
	    av_free(key);
	    av_del_tmpfile(tmpfile);
	    AV_UNLOCK(enod->lock);
	    return res;
	}

	/* ...create an object to store tmpfile */
	AV_NEW_OBJ(cent, extfscacheentry_delete);
	cent->tmpfile = tmpfile;
	cent->basefile_dev = basefile_stat.dev;
	cent->basefile_ino = basefile_stat.ino;
	cent->basefile_mtime = basefile_stat.mtime;

	/* put it in the extfscache */
	av_cache2_set(cent,key);
	AV_UNLOCK(enod->lock);

        tmpsize = av_tmpfile_blksize(tmpfile);
        if(tmpsize > 0)
            av_cache2_setsize(key, tmpsize);
    } else {
	AV_UNLOCK(enod->lock);
    }
    av_free(key);

    fd = open(cent->tmpfile, O_RDONLY);
    if(fd == -1) {
        res = -errno; 
        av_log(AVLOG_ERROR, "EXTFS: Could not open %s: %s", cent->tmpfile,
               strerror(errno));
	av_unref_obj(cent);
        return res;
    }

    AV_NEW(efil);
    efil->cent = cent;
    efil->fd = fd;

    fil->data = efil;
    
    return 0;
}

static int extfs_close(struct archfile *fil)
{
    struct extfsfile *efil = (struct extfsfile *) fil->data;

    close(efil->fd);

    av_unref_obj(efil->cent);
    av_free(efil);
    
    return 0;
}

static void extfsdata_delete(struct extfsdata *info)
{
    av_free(info->progpath);
}

static int create_extfs_handler(struct vmodule *module, const char *extfs_dir,
                                char *name)
{
    int res;
    struct avfs *avfs;
    struct archparams *ap;
    struct extfsdata *info;
    struct ext_info *extlist;
    int needbase;
    int end;

    /* Creates extension list, and strips name of the extensions */
    extlist = create_exts(name);
    end = strlen(name) - 1;

    if(name[end] == ':') {
        needbase = 0;
        name[end] = '\0';
    }
    else
        needbase = 1;

    res = av_archive_init(name, extlist, AV_VER, module, &avfs);
    av_free(extlist);
    if(res < 0)
        return res;

    ap = (struct archparams *) avfs->data;

    /* FIXME: If there is no basefile then cache the listing forever? */
    AV_NEW_OBJ(info, extfsdata_delete);
    ap->data = info;
    ap->parse = extfs_list;
    ap->read = extfs_read;
    ap->open = extfs_open;
    ap->close = extfs_close;

    if(!needbase)
        ap->flags |= ARF_NOBASE;
  
    info->progpath = av_stradd(NULL, extfs_dir, "/", name, NULL);
    info->needbase = needbase;
    
    av_add_avfs(avfs);

    return 0;
}

static int extfs_init(struct vmodule *module)
{
    char *extfs_dir, *extfs_conf;
    struct filebuf *fb;
    int fd;
    int res;
    char *line;
    char *c;

    extfs_dir = av_get_config("moduledir");
    extfs_dir = av_stradd(extfs_dir, "/extfs", NULL);
    extfs_conf = av_stradd(NULL, extfs_dir, "/extfs.ini", NULL);

    fd = open(extfs_conf, O_RDONLY);
    if(fd == -1) {
        res = -errno;
        av_log(AVLOG_WARNING, "Could not open extfs config file %s: %s", 
                 extfs_conf, strerror(errno));
        av_free(extfs_conf);
        av_free(extfs_dir);
        return res;
    }
    av_free(extfs_conf);
  
    fb = av_filebuf_new(fd, 0);

    while(1) {
        res = av_filebuf_getline(fb, &line, -1);
        if(res < 0 || line == NULL)
            break;

        if (*line != '#') {
            c = line + strlen(line) - 1;
            if(*c == '\n') *c-- = '\0';

            if(*line) 
                res = create_extfs_handler(module, extfs_dir, line);
        }
        av_free(line);
        if(res < 0) 
            break;
    }
    av_unref_obj(fb);
    av_free(extfs_dir);
    
    if(res < 0)
        return res;

    return 0;
}

extern int av_init_module_extfs(struct vmodule *module);

int av_init_module_extfs(struct vmodule *module)
{
    return extfs_init(module);
}