Mercurial > molko
comparison src/libmlk-core/core/vfs-zip.c @ 335:68287c7bcdb5
core: implement vfs-zip (read-only)
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 13 Oct 2021 15:58:02 +0200 |
parents | |
children | 460c78706989 |
comparison
equal
deleted
inserted
replaced
334:1d3e6108cb99 | 335:68287c7bcdb5 |
---|---|
1 /* | |
2 * vfs-zip.c -- VFS subsystem for zip archives | |
3 * | |
4 * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <assert.h> | |
20 #include <string.h> | |
21 | |
22 #include <zip.h> | |
23 | |
24 #include "error.h" | |
25 #include "vfs-zip.h" | |
26 #include "vfs.h" | |
27 | |
28 static inline int | |
29 mkflags(const char *mode) | |
30 { | |
31 /* TODO: this should check for mutual exclusions. */ | |
32 int flags = 0; | |
33 | |
34 for (; *mode; ++mode) { | |
35 switch (*mode) { | |
36 case 'w': | |
37 flags |= ZIP_CREATE; | |
38 break; | |
39 case 'x': | |
40 flags |= ZIP_EXCL; | |
41 break; | |
42 case '+': | |
43 flags |= ZIP_TRUNCATE; | |
44 break; | |
45 case 'r': | |
46 flags |= ZIP_RDONLY; | |
47 break; | |
48 default: | |
49 break; | |
50 } | |
51 } | |
52 | |
53 return flags; | |
54 } | |
55 | |
56 static size_t | |
57 file_read(struct vfs_file *file, void *buf, size_t bufsz) | |
58 { | |
59 return zip_fread(file->data, buf, bufsz); | |
60 } | |
61 | |
62 static size_t | |
63 file_write(struct vfs_file *file, const void *buf, size_t bufsz) | |
64 { | |
65 (void)file; | |
66 (void)buf; | |
67 (void)bufsz; | |
68 | |
69 return errorf("operation not supported"); | |
70 } | |
71 | |
72 static int | |
73 file_flush(struct vfs_file *file) | |
74 { | |
75 (void)file; | |
76 | |
77 return 0; | |
78 } | |
79 | |
80 static void | |
81 file_finish(struct vfs_file *file) | |
82 { | |
83 zip_fclose(file->data); | |
84 } | |
85 | |
86 static int | |
87 impl_open(struct vfs *vfs, struct vfs_file *file, const char *entry, const char *mode) | |
88 { | |
89 (void)mode; | |
90 | |
91 if (!(file->data = zip_fopen(vfs->data, entry, 0))) | |
92 return errorf("unable to open file in archive"); | |
93 | |
94 file->read = file_read; | |
95 file->write = file_write; | |
96 file->flush = file_flush; | |
97 file->finish = file_finish; | |
98 | |
99 return 0; | |
100 } | |
101 | |
102 static void | |
103 impl_finish(struct vfs *vfs) | |
104 { | |
105 zip_close(vfs->data); | |
106 } | |
107 | |
108 int | |
109 vfs_zip(struct vfs *vfs, const char *file, const char *mode) | |
110 { | |
111 assert(vfs); | |
112 assert(file); | |
113 | |
114 memset(vfs, 0, sizeof (*vfs)); | |
115 | |
116 if (!(vfs->data = zip_open(file, mkflags(mode), NULL))) | |
117 return errorf("%s: unable to open zip file", file); | |
118 | |
119 vfs->open = impl_open; | |
120 vfs->finish = impl_finish; | |
121 | |
122 return 0; | |
123 } |