2
* Copyright (C) 2003-2010 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
* iso archive handling (requires cdio, and iso9660)
25
#include "archive/iso9660_archive_plugin.h"
26
#include "archive_api.h"
27
#include "input_plugin.h"
30
#include <cdio/cdio.h>
31
#include <cdio/iso9660.h>
36
#define CEILING(x, y) ((x+(y-1))/y)
38
struct iso9660_archive_file {
39
struct archive_file base;
48
static const struct input_plugin iso9660_input_plugin;
53
return g_quark_from_static_string("iso9660");
56
/* archive open && listing routine */
59
listdir_recur(const char *psz_path, struct iso9660_archive_file *context)
61
iso9660_t *iso = context->iso;
63
CdioListNode_t *entnode;
64
iso9660_stat_t *statbuf;
67
entlist = iso9660_ifs_readdir (iso, psz_path);
71
/* Iterate over the list of nodes that iso9660_ifs_readdir gives */
72
_CDIO_LIST_FOREACH (entnode, entlist) {
73
statbuf = (iso9660_stat_t *) _cdio_list_node_data (entnode);
75
strcpy(pathname, psz_path);
76
strcat(pathname, statbuf->filename);
78
if (_STAT_DIR == statbuf->type ) {
79
if (strcmp(statbuf->filename, ".") && strcmp(statbuf->filename, "..")) {
80
strcat(pathname, "/");
81
listdir_recur(pathname, context);
85
context->list = g_slist_prepend( context->list,
86
g_strdup(pathname + 1));
89
_cdio_list_free (entlist, true);
92
static struct archive_file *
93
iso9660_archive_open(const char *pathname, GError **error_r)
95
struct iso9660_archive_file *context =
96
g_new(struct iso9660_archive_file, 1);
98
archive_file_init(&context->base, &iso9660_archive_plugin);
99
refcount_init(&context->ref);
101
context->list = NULL;
104
context->iso = iso9660_open (pathname);
105
if (context->iso == NULL) {
106
g_set_error(error_r, iso9660_quark(), 0,
107
"Failed to open ISO9660 file %s", pathname);
111
listdir_recur("/", context);
113
return &context->base;
117
iso9660_archive_scan_reset(struct archive_file *file)
119
struct iso9660_archive_file *context =
120
(struct iso9660_archive_file *)file;
123
context->iter = context->list;
127
iso9660_archive_scan_next(struct archive_file *file)
129
struct iso9660_archive_file *context =
130
(struct iso9660_archive_file *)file;
133
if (context->iter != NULL) {
134
///fetch data and goto next
135
data = context->iter->data;
136
context->iter = g_slist_next(context->iter);
142
iso9660_archive_close(struct archive_file *file)
144
struct iso9660_archive_file *context =
145
(struct iso9660_archive_file *)file;
148
if (!refcount_dec(&context->ref))
153
for (tmp = context->list; tmp != NULL; tmp = g_slist_next(tmp))
155
g_slist_free(context->list);
158
iso9660_close(context->iso);
163
/* single archive handling */
165
struct iso9660_input_stream {
166
struct input_stream base;
168
struct iso9660_archive_file *archive;
170
iso9660_stat_t *statbuf;
174
static struct input_stream *
175
iso9660_archive_open_stream(struct archive_file *file,
176
const char *pathname, GError **error_r)
178
struct iso9660_archive_file *context =
179
(struct iso9660_archive_file *)file;
180
struct iso9660_input_stream *iis;
182
iis = g_new(struct iso9660_input_stream, 1);
183
input_stream_init(&iis->base, &iso9660_input_plugin, pathname);
185
iis->archive = context;
186
iis->statbuf = iso9660_ifs_stat_translate(context->iso, pathname);
187
if (iis->statbuf == NULL) {
189
g_set_error(error_r, iso9660_quark(), 0,
190
"not found in the ISO file: %s", pathname);
194
iis->base.ready = true;
195
//we are not seekable
196
iis->base.seekable = false;
198
iis->base.size = iis->statbuf->size;
200
iis->max_blocks = CEILING(iis->statbuf->size, ISO_BLOCKSIZE);
202
refcount_inc(&context->ref);
208
iso9660_input_close(struct input_stream *is)
210
struct iso9660_input_stream *iis = (struct iso9660_input_stream *)is;
212
g_free(iis->statbuf);
214
iso9660_archive_close(&iis->archive->base);
216
input_stream_deinit(&iis->base);
222
iso9660_input_read(struct input_stream *is, void *ptr, size_t size, GError **error_r)
224
struct iso9660_input_stream *iis = (struct iso9660_input_stream *)is;
225
int toread, readed = 0;
226
int no_blocks, cur_block;
227
size_t left_bytes = iis->statbuf->size - is->offset;
229
size = (size * ISO_BLOCKSIZE) / ISO_BLOCKSIZE;
231
if (left_bytes < size) {
233
no_blocks = CEILING(left_bytes,ISO_BLOCKSIZE);
236
no_blocks = toread / ISO_BLOCKSIZE;
240
cur_block = is->offset / ISO_BLOCKSIZE;
242
readed = iso9660_iso_seek_read (iis->archive->iso, ptr,
243
iis->statbuf->lsn + cur_block, no_blocks);
245
if (readed != no_blocks * ISO_BLOCKSIZE) {
246
g_set_error(error_r, iso9660_quark(), 0,
247
"error reading ISO file at lsn %lu",
248
(long unsigned int) cur_block);
251
if (left_bytes < size) {
255
is->offset += readed;
261
iso9660_input_eof(struct input_stream *is)
263
return is->offset == is->size;
266
/* exported structures */
268
static const char *const iso9660_archive_extensions[] = {
273
static const struct input_plugin iso9660_input_plugin = {
274
.close = iso9660_input_close,
275
.read = iso9660_input_read,
276
.eof = iso9660_input_eof,
279
const struct archive_plugin iso9660_archive_plugin = {
281
.open = iso9660_archive_open,
282
.scan_reset = iso9660_archive_scan_reset,
283
.scan_next = iso9660_archive_scan_next,
284
.open_stream = iso9660_archive_open_stream,
285
.close = iso9660_archive_close,
286
.suffixes = iso9660_archive_extensions