2
* Copyright (C) 2003-2011 The Music Player Daemon Project
3
* http://www.musicpd.org
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.
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.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
* zip archive handling (requires zziplib)
25
#include "archive/zzip_archive_plugin.h"
26
#include "archive_api.h"
27
#include "archive_api.h"
28
#include "input_internal.h"
29
#include "input_plugin.h"
32
#include <zzip/zzip.h>
37
struct archive_file base;
46
static const struct input_plugin zzip_input_plugin;
51
return g_quark_from_static_string("zzip");
54
/* archive open && listing routine */
56
static struct archive_file *
57
zzip_archive_open(const char *pathname, GError **error_r)
59
struct zzip_archive *context = g_malloc(sizeof(*context));
62
archive_file_init(&context->base, &zzip_archive_plugin);
63
refcount_init(&context->ref);
67
context->dir = zzip_dir_open(pathname, NULL);
68
if (context->dir == NULL) {
69
g_set_error(error_r, zzip_quark(), 0,
70
"Failed to open ZIP file %s", pathname);
74
while (zzip_dir_read(context->dir, &dirent)) {
76
if (dirent.st_size > 0) {
77
context->list = g_slist_prepend(context->list,
78
g_strdup(dirent.d_name));
82
return &context->base;
86
zzip_archive_scan_reset(struct archive_file *file)
88
struct zzip_archive *context = (struct zzip_archive *) file;
90
context->iter = context->list;
94
zzip_archive_scan_next(struct archive_file *file)
96
struct zzip_archive *context = (struct zzip_archive *) file;
98
if (context->iter != NULL) {
99
///fetch data and goto next
100
data = context->iter->data;
101
context->iter = g_slist_next(context->iter);
107
zzip_archive_close(struct archive_file *file)
109
struct zzip_archive *context = (struct zzip_archive *) file;
111
if (!refcount_dec(&context->ref))
116
for (GSList *tmp = context->list; tmp != NULL; tmp = g_slist_next(tmp))
118
g_slist_free(context->list);
121
zzip_dir_close (context->dir);
126
/* single archive handling */
128
struct zzip_input_stream {
129
struct input_stream base;
131
struct zzip_archive *archive;
136
static struct input_stream *
137
zzip_archive_open_stream(struct archive_file *file,
138
const char *pathname,
139
GMutex *mutex, GCond *cond,
142
struct zzip_archive *context = (struct zzip_archive *) file;
143
struct zzip_input_stream *zis;
146
zis = g_new(struct zzip_input_stream, 1);
147
input_stream_init(&zis->base, &zzip_input_plugin, pathname,
150
zis->archive = context;
151
zis->file = zzip_file_open(context->dir, pathname, 0);
152
if (zis->file == NULL) {
154
g_set_error(error_r, zzip_quark(), 0,
155
"not found in the ZIP file: %s", pathname);
159
zis->base.ready = true;
160
//we are seekable (but its not recommendent to do so)
161
zis->base.seekable = true;
163
zzip_file_stat(zis->file, &z_stat);
164
zis->base.size = z_stat.st_size;
166
refcount_inc(&context->ref);
172
zzip_input_close(struct input_stream *is)
174
struct zzip_input_stream *zis = (struct zzip_input_stream *)is;
176
zzip_file_close(zis->file);
177
zzip_archive_close(&zis->archive->base);
178
input_stream_deinit(&zis->base);
183
zzip_input_read(struct input_stream *is, void *ptr, size_t size,
186
struct zzip_input_stream *zis = (struct zzip_input_stream *)is;
189
ret = zzip_file_read(zis->file, ptr, size);
191
g_set_error(error_r, zzip_quark(), ret,
192
"zzip_file_read() has failed");
196
is->offset = zzip_tell(zis->file);
202
zzip_input_eof(struct input_stream *is)
204
struct zzip_input_stream *zis = (struct zzip_input_stream *)is;
206
return (goffset)zzip_tell(zis->file) == is->size;
210
zzip_input_seek(struct input_stream *is,
211
goffset offset, int whence, GError **error_r)
213
struct zzip_input_stream *zis = (struct zzip_input_stream *)is;
214
zzip_off_t ofs = zzip_seek(zis->file, offset, whence);
216
g_set_error(error_r, zzip_quark(), ofs,
217
"zzip_seek() has failed");
224
/* exported structures */
226
static const char *const zzip_archive_extensions[] = {
231
static const struct input_plugin zzip_input_plugin = {
232
.close = zzip_input_close,
233
.read = zzip_input_read,
234
.eof = zzip_input_eof,
235
.seek = zzip_input_seek,
238
const struct archive_plugin zzip_archive_plugin = {
240
.open = zzip_archive_open,
241
.scan_reset = zzip_archive_scan_reset,
242
.scan_next = zzip_archive_scan_next,
243
.open_stream = zzip_archive_open_stream,
244
.close = zzip_archive_close,
245
.suffixes = zzip_archive_extensions