~ubuntu-branches/ubuntu/trusty/mpd/trusty

« back to all changes in this revision

Viewing changes to src/archive/iso9660_archive_plugin.c

  • Committer: Bazaar Package Importer
  • Author(s): Angel Abad
  • Date: 2011-02-02 12:26:30 UTC
  • mfrom: (1.5.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20110202122630-bdyx8w4k94doz4fs
Tags: 0.16.1-1ubuntu1
* Merge from debian unstable. Remaining changes:
  - debian/control:
    + Don't build-depend on libmikmod2-dev (Debian bug #510675).
    + Move avahi-daemon from Suggests field to Recommends field.
  - debian/mpd.init.d:
    + Read mpd user from mpd.conf.
  - debian/control, debian/rules:
    + Add libmp3lame-dev to the build dependencies and enable lame.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2010 The Music Player Daemon Project
 
3
 * http://www.musicpd.org
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 */
 
19
 
 
20
/**
 
21
  * iso archive handling (requires cdio, and iso9660)
 
22
  */
 
23
 
 
24
#include "config.h"
 
25
#include "archive/iso9660_archive_plugin.h"
 
26
#include "archive_api.h"
 
27
#include "input_plugin.h"
 
28
#include "refcount.h"
 
29
 
 
30
#include <cdio/cdio.h>
 
31
#include <cdio/iso9660.h>
 
32
 
 
33
#include <glib.h>
 
34
#include <string.h>
 
35
 
 
36
#define CEILING(x, y) ((x+(y-1))/y)
 
37
 
 
38
struct iso9660_archive_file {
 
39
        struct archive_file base;
 
40
 
 
41
        struct refcount ref;
 
42
 
 
43
        iso9660_t *iso;
 
44
        GSList  *list;
 
45
        GSList  *iter;
 
46
};
 
47
 
 
48
static const struct input_plugin iso9660_input_plugin;
 
49
 
 
50
static inline GQuark
 
51
iso9660_quark(void)
 
52
{
 
53
        return g_quark_from_static_string("iso9660");
 
54
}
 
55
 
 
56
/* archive open && listing routine */
 
57
 
 
58
static void
 
59
listdir_recur(const char *psz_path, struct iso9660_archive_file *context)
 
60
{
 
61
        iso9660_t *iso = context->iso;
 
62
        CdioList_t *entlist;
 
63
        CdioListNode_t *entnode;
 
64
        iso9660_stat_t *statbuf;
 
65
        char pathname[4096];
 
66
 
 
67
        entlist = iso9660_ifs_readdir (iso, psz_path);
 
68
        if (!entlist) {
 
69
                return;
 
70
        }
 
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);
 
74
 
 
75
                strcpy(pathname, psz_path);
 
76
                strcat(pathname, statbuf->filename);
 
77
 
 
78
                if (_STAT_DIR == statbuf->type ) {
 
79
                        if (strcmp(statbuf->filename, ".") && strcmp(statbuf->filename, "..")) {
 
80
                                strcat(pathname, "/");
 
81
                                listdir_recur(pathname, context);
 
82
                        }
 
83
                } else {
 
84
                        //remove leading /
 
85
                        context->list = g_slist_prepend( context->list,
 
86
                                g_strdup(pathname + 1));
 
87
                }
 
88
        }
 
89
        _cdio_list_free (entlist, true);
 
90
}
 
91
 
 
92
static struct archive_file *
 
93
iso9660_archive_open(const char *pathname, GError **error_r)
 
94
{
 
95
        struct iso9660_archive_file *context =
 
96
                g_new(struct iso9660_archive_file, 1);
 
97
 
 
98
        archive_file_init(&context->base, &iso9660_archive_plugin);
 
99
        refcount_init(&context->ref);
 
100
 
 
101
        context->list = NULL;
 
102
 
 
103
        /* open archive */
 
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);
 
108
                return NULL;
 
109
        }
 
110
 
 
111
        listdir_recur("/", context);
 
112
 
 
113
        return &context->base;
 
114
}
 
115
 
 
116
static void
 
117
iso9660_archive_scan_reset(struct archive_file *file)
 
118
{
 
119
        struct iso9660_archive_file *context =
 
120
                (struct iso9660_archive_file *)file;
 
121
 
 
122
        //reset iterator
 
123
        context->iter = context->list;
 
124
}
 
125
 
 
126
static char *
 
127
iso9660_archive_scan_next(struct archive_file *file)
 
128
{
 
129
        struct iso9660_archive_file *context =
 
130
                (struct iso9660_archive_file *)file;
 
131
 
 
132
        char *data = NULL;
 
133
        if (context->iter != NULL) {
 
134
                ///fetch data and goto next
 
135
                data = context->iter->data;
 
136
                context->iter = g_slist_next(context->iter);
 
137
        }
 
138
        return data;
 
139
}
 
140
 
 
141
static void
 
142
iso9660_archive_close(struct archive_file *file)
 
143
{
 
144
        struct iso9660_archive_file *context =
 
145
                (struct iso9660_archive_file *)file;
 
146
        GSList *tmp;
 
147
 
 
148
        if (!refcount_dec(&context->ref))
 
149
                return;
 
150
 
 
151
        if (context->list) {
 
152
                //free list
 
153
                for (tmp = context->list; tmp != NULL; tmp = g_slist_next(tmp))
 
154
                        g_free(tmp->data);
 
155
                g_slist_free(context->list);
 
156
        }
 
157
        //close archive
 
158
        iso9660_close(context->iso);
 
159
 
 
160
        g_free(context);
 
161
}
 
162
 
 
163
/* single archive handling */
 
164
 
 
165
struct iso9660_input_stream {
 
166
        struct input_stream base;
 
167
 
 
168
        struct iso9660_archive_file *archive;
 
169
 
 
170
        iso9660_stat_t *statbuf;
 
171
        size_t max_blocks;
 
172
};
 
173
 
 
174
static struct input_stream *
 
175
iso9660_archive_open_stream(struct archive_file *file,
 
176
                const char *pathname, GError **error_r)
 
177
{
 
178
        struct iso9660_archive_file *context =
 
179
                (struct iso9660_archive_file *)file;
 
180
        struct iso9660_input_stream *iis;
 
181
 
 
182
        iis = g_new(struct iso9660_input_stream, 1);
 
183
        input_stream_init(&iis->base, &iso9660_input_plugin, pathname);
 
184
 
 
185
        iis->archive = context;
 
186
        iis->statbuf = iso9660_ifs_stat_translate(context->iso, pathname);
 
187
        if (iis->statbuf == NULL) {
 
188
                g_free(iis);
 
189
                g_set_error(error_r, iso9660_quark(), 0,
 
190
                            "not found in the ISO file: %s", pathname);
 
191
                return NULL;
 
192
        }
 
193
 
 
194
        iis->base.ready = true;
 
195
        //we are not seekable
 
196
        iis->base.seekable = false;
 
197
 
 
198
        iis->base.size = iis->statbuf->size;
 
199
 
 
200
        iis->max_blocks = CEILING(iis->statbuf->size, ISO_BLOCKSIZE);
 
201
 
 
202
        refcount_inc(&context->ref);
 
203
 
 
204
        return &iis->base;
 
205
}
 
206
 
 
207
static void
 
208
iso9660_input_close(struct input_stream *is)
 
209
{
 
210
        struct iso9660_input_stream *iis = (struct iso9660_input_stream *)is;
 
211
 
 
212
        g_free(iis->statbuf);
 
213
 
 
214
        iso9660_archive_close(&iis->archive->base);
 
215
 
 
216
        input_stream_deinit(&iis->base);
 
217
        g_free(iis);
 
218
}
 
219
 
 
220
 
 
221
static size_t
 
222
iso9660_input_read(struct input_stream *is, void *ptr, size_t size, GError **error_r)
 
223
{
 
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;
 
228
 
 
229
        size = (size * ISO_BLOCKSIZE) / ISO_BLOCKSIZE;
 
230
 
 
231
        if (left_bytes < size) {
 
232
                toread = left_bytes;
 
233
                no_blocks = CEILING(left_bytes,ISO_BLOCKSIZE);
 
234
        } else {
 
235
                toread = size;
 
236
                no_blocks = toread / ISO_BLOCKSIZE;
 
237
        }
 
238
        if (no_blocks > 0) {
 
239
 
 
240
                cur_block = is->offset / ISO_BLOCKSIZE;
 
241
 
 
242
                readed = iso9660_iso_seek_read (iis->archive->iso, ptr,
 
243
                        iis->statbuf->lsn + cur_block, no_blocks);
 
244
 
 
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);
 
249
                        return 0;
 
250
                }
 
251
                if (left_bytes < size) {
 
252
                        readed = left_bytes;
 
253
                }
 
254
 
 
255
                is->offset += readed;
 
256
        }
 
257
        return readed;
 
258
}
 
259
 
 
260
static bool
 
261
iso9660_input_eof(struct input_stream *is)
 
262
{
 
263
        return is->offset == is->size;
 
264
}
 
265
 
 
266
/* exported structures */
 
267
 
 
268
static const char *const iso9660_archive_extensions[] = {
 
269
        "iso",
 
270
        NULL
 
271
};
 
272
 
 
273
static const struct input_plugin iso9660_input_plugin = {
 
274
        .close = iso9660_input_close,
 
275
        .read = iso9660_input_read,
 
276
        .eof = iso9660_input_eof,
 
277
};
 
278
 
 
279
const struct archive_plugin iso9660_archive_plugin = {
 
280
        .name = "iso",
 
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
 
287
};