File: ulzip.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 (327 lines) | stat: -rw-r--r-- 7,299 bytes parent folder | download | duplicates (3)
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
/*  
    AVFS: A Virtual File System Library
    Copyright (C) 2021 Ralf Hoffmann <ralf@boomerangsworld.de>

    This program can be distributed under the terms of the GNU GPL.
    See the file COPYING.

    ULZIP module (based on UZSTD module)
*/

#include "version.h"

#include "lzipfile.h"
#include "filecache.h"
#include "cache.h"
#include "oper.h"
#include "version.h"

struct lzipnode {
    avmutex lock;
    struct avstat sig;
    struct cacheobj *cache;
    avino_t ino;
};

struct lziphandle {
    struct lzipfile *zfil;
    vfile *base;
    struct lzipnode *node;
};


static void lzipnode_destroy(struct lzipnode *nod)
{
    av_unref_obj(nod->cache);
    AV_FREELOCK(nod->lock);
}

static struct lzipnode *lzip_new_node(ventry *ve, struct avstat *stbuf)
{
    struct lzipnode *nod;

    AV_NEW_OBJ(nod, lzipnode_destroy);
    AV_INITLOCK(nod->lock);
    nod->sig = *stbuf;
    nod->cache = NULL;
    nod->ino = av_new_ino(ve->mnt->avfs);
    
    return nod;
}

static int lzip_same(struct lzipnode *nod, struct avstat *stbuf)
{
    if(nod->sig.ino == stbuf->ino &&
       nod->sig.dev == stbuf->dev &&
       nod->sig.size == stbuf->size &&
       AV_TIME_EQ(nod->sig.mtime, stbuf->mtime))
        return 1;
    else
        return 0;
}

static struct lzipnode *lzip_do_get_node(ventry *ve, const char *key,
                                         struct avstat *stbuf)
{
    static AV_LOCK_DECL(lock);
    struct lzipnode *nod;

    AV_LOCK(lock);
    nod = (struct lzipnode *) av_filecache_get(key);
    if(nod != NULL) {
        if(!lzip_same(nod, stbuf)) {
            av_unref_obj(nod);
            nod = NULL;
        }
    }
    
    if(nod == NULL) {
        nod =  lzip_new_node(ve, stbuf);
        av_filecache_set(key, nod);
    }
    AV_UNLOCK(lock);

    return nod;
}

static int lzip_getnode(ventry *ve, vfile *base, struct lzipnode **resp)
{
    int res;
    struct avstat stbuf;
    const int attrmask = AVA_INO | AVA_DEV | AVA_SIZE | AVA_MTIME;
    struct lzipnode *nod;
    char *key;

    res = av_fgetattr(base, &stbuf, attrmask);
    if(res < 0)
        return res;

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

    nod = lzip_do_get_node(ve, key, &stbuf);

    av_free(key);

    *resp = nod;
    return 0;
}

static struct lzipcache *lzip_getcache(ventry *base, struct lzipnode *nod)
{
    struct lzipcache *cache;
    
    cache = (struct lzipcache *) av_cacheobj_get(nod->cache);
    if(cache == NULL) {
        int res;
        char *name;

        res = av_generate_path(base, &name);
        if(res < 0)
            name = NULL;
        else
            name = av_stradd(name, "(index)", NULL);

        cache = av_lzipcache_new();
        av_unref_obj(nod->cache);

        /* FIXME: the cacheobj should only be created when the lzipcache
           is nonempty */
        nod->cache = av_cacheobj_new(cache, name);
        av_free(name); 
    }

    return cache;
}

static int lzip_lookup(ventry *ve, const char *name, void **newp)
{
    char *path = (char *) ve->data;
    
    if(path == NULL) {
        if(name[0] != '\0')
            return -ENOENT;
	if(ve->mnt->opts[0] != '\0')
            return -ENOENT;
        path = av_strdup(name);
    }
    else if(name == NULL) {
        av_free(path);
        path = NULL;
    }
    else 
        return -ENOENT;
    
    *newp = path;
    return 0;
}

static int lzip_access(ventry *ve, int amode)
{
    return av_access(ve->mnt->base, amode);
}

static int lzip_open(ventry *ve, int flags, avmode_t mode, void **resp)
{
    int res;
    vfile *base;
    struct lzipnode *nod;
    struct lziphandle *fil;

    if(flags & AVO_DIRECTORY)
        return -ENOTDIR;

    if(AV_ISWRITE(flags))
        return -EROFS;

    res = av_open(ve->mnt->base, AVO_RDONLY, 0, &base);
    if(res < 0)
        return res;

    res = lzip_getnode(ve, base, &nod);
    if(res < 0) {
        av_close(base);
        return res;
    }

    AV_NEW(fil);
    if((flags & AVO_ACCMODE) != AVO_NOPERM)
        fil->zfil = av_lzipfile_new(base);
    else
        fil->zfil = NULL;

    fil->base = base;
    fil->node = nod;
    
    *resp = fil;
    return 0;
}

static int lzip_close(vfile *vf)
{
    struct lziphandle *fil = (struct lziphandle *) vf->data;

    av_unref_obj(fil->zfil);
    av_unref_obj(fil->node);
    av_close(fil->base);
    av_free(fil);

    return 0;
}

static avssize_t lzip_read(vfile *vf, char *buf, avsize_t nbyte)
{
    avssize_t res;
    struct lziphandle *fil = (struct lziphandle *) vf->data;
    struct lzipcache *zc;
    struct cacheobj *cobj;
    avoff_t prev_cachesize;

    AV_LOCK(fil->node->lock);
    zc = lzip_getcache(vf->mnt->base, fil->node);
    cobj = fil->node->cache;
    av_ref_obj(cobj);
    AV_UNLOCK(fil->node->lock);

    prev_cachesize = av_lzipcache_size(zc);
    
    res = av_lzipfile_pread(fil->zfil, zc, buf, nbyte, vf->ptr);
    if(res > 0) {
        avoff_t new_cachesize;

        vf->ptr += res;

        new_cachesize = av_lzipcache_size(zc);
        if (new_cachesize != prev_cachesize) {
            av_cacheobj_setsize(cobj, new_cachesize);
        }
    } else {
        AV_LOCK(fil->node->lock);
        av_unref_obj(fil->node->cache);
        fil->node->cache = NULL;
        AV_UNLOCK(fil->node->lock);
    }

    av_unref_obj(zc);
    av_unref_obj(cobj);

    return res;
}

static int lzip_getattr(vfile *vf, struct avstat *buf, int attrmask)
{
    int res;
    struct lziphandle *fil = (struct lziphandle *) vf->data;
    struct lzipnode *nod = fil->node;
    avoff_t size;
    const int basemask = AVA_MODE | AVA_UID | AVA_GID | AVA_MTIME | AVA_ATIME | AVA_CTIME;
    struct lzipcache *zc;
    struct cacheobj *cobj;

    res = av_fgetattr(fil->base, buf, basemask);
    if(res < 0)
        return res;

    AV_LOCK(fil->node->lock);
    zc = lzip_getcache(vf->mnt->base, fil->node);
    cobj = fil->node->cache;
    av_ref_obj(cobj);
    AV_UNLOCK(fil->node->lock);

    if((attrmask & (AVA_SIZE | AVA_BLKCNT)) != 0) {
        res = av_lzipfile_size(fil->zfil, zc, &size);
        if(res == 0 && size == -1) {
            fil->zfil = av_lzipfile_new(fil->base);
            res = av_lzipfile_size(fil->zfil, zc, &size);
        }
        if(res < 0) {
            av_unref_obj(zc);
            av_unref_obj(cobj);

            return res;
        }

        buf->size = size;
        buf->blocks = AV_BLOCKS(buf->size);
    }

    buf->mode &= ~(07000);
    buf->blksize = 4096;
    buf->dev = vf->mnt->avfs->dev;
    buf->ino = nod->ino;
    buf->nlink = 1;
    
    av_unref_obj(zc);
    av_unref_obj(cobj);

    return 0;
}

extern int av_init_module_ulzip(struct vmodule *module);

int av_init_module_ulzip(struct vmodule *module)
{
    int res;
    struct avfs *avfs;
    struct ext_info ulzip_exts[3];

    ulzip_exts[0].from = ".tar.lz",  ulzip_exts[0].to = ".tar";
    ulzip_exts[1].from = ".lz",  ulzip_exts[1].to = NULL;
    ulzip_exts[2].from = NULL;

    res = av_new_avfs("ulzip", ulzip_exts, AV_VER, AVF_NOLOCK, module, &avfs);
    if(res < 0)
        return res;

    avfs->lookup   = lzip_lookup;
    avfs->access   = lzip_access;
    avfs->open     = lzip_open;
    avfs->close    = lzip_close; 
    avfs->read     = lzip_read;
    avfs->getattr  = lzip_getattr;

    av_add_avfs(avfs);

    return 0;
}