diff options
Diffstat (limited to '')
-rw-r--r-- | src/archive/bz2_plugin.c | 303 | ||||
-rw-r--r-- | src/archive/iso_plugin.c | 258 | ||||
-rw-r--r-- | src/archive/zip_plugin.c | 196 | ||||
-rw-r--r-- | src/archive_api.c | 111 | ||||
-rw-r--r-- | src/archive_api.h | 94 | ||||
-rw-r--r-- | src/archive_internal.h | 26 | ||||
-rw-r--r-- | src/archive_list.c | 121 | ||||
-rw-r--r-- | src/archive_list.h | 44 |
8 files changed, 1153 insertions, 0 deletions
diff --git a/src/archive/bz2_plugin.c b/src/archive/bz2_plugin.c new file mode 100644 index 000000000..00c59c693 --- /dev/null +++ b/src/archive/bz2_plugin.c @@ -0,0 +1,303 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka <viliam.mateicka@gmail.com> + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * single bz2 archive handling (requires libbz2) + */ + +#include "archive_api.h" +#include "input_stream.h" +#include "utils.h" + +#include <stdint.h> +#include <stddef.h> +#include <string.h> +#include <glib.h> +#include <bzlib.h> + +#ifdef HAVE_OLDER_BZIP2 +#define BZ2_bzDecompressInit bzDecompressInit +#define BZ2_bzDecompress bzDecompress +#endif + +#define BZ_BUFSIZE 5000 + +typedef struct { + char *name; + bool reset; + struct input_stream istream; + int last_bz_result; + int last_parent_result; + bz_stream bzstream; + char *buffer; +} bz2_context; + + +static const struct input_plugin bz2_inputplugin; + +/* single archive handling allocation helpers */ + +static bool +bz2_alloc(bz2_context *data) +{ + data->bzstream.bzalloc = NULL; + data->bzstream.bzfree = NULL; + data->bzstream.opaque = NULL; + + data->buffer = g_malloc(BZ_BUFSIZE); + data->bzstream.next_in = (void *) data->buffer; + data->bzstream.avail_in = 0; + + if (BZ2_bzDecompressInit(&data->bzstream, 0, 0) != BZ_OK) { + g_free(data->buffer); + g_free(data); + return false; + } + + data->last_bz_result = BZ_OK; + data->last_parent_result = 0; + return true; +} + +static void +bz2_destroy(bz2_context *data) +{ + BZ2_bzDecompressEnd(&data->bzstream); + g_free(data->buffer); +} + +/* archive open && listing routine */ + +static struct archive_file * +bz2_open(char * pathname) +{ + bz2_context *context; + char *name; + int len; + + context = g_malloc(sizeof(bz2_context)); + if (!context) { + return NULL; + } + //open archive + if (!input_stream_open(&context->istream, pathname)) { + g_warning("failed to open an bzip2 archive %s\n",pathname); + g_free(context); + return NULL; + } + //capture filename + name = strrchr(pathname, '/'); + if (name == NULL) { + g_warning("failed to get bzip2 name from %s\n",pathname); + g_free(context); + return NULL; + } + context->name = g_strdup(name+1); + //remove suffix + len = strlen(context->name); + if (len > 4) { + context->name[len-4] = 0; //remove .bz2 suffix + } + return (struct archive_file *) context; +} + +static void +bz2_scan_reset(struct archive_file *file) +{ + bz2_context *context = (bz2_context *) file; + context->reset = true; +} + +static char * +bz2_scan_next(struct archive_file *file) +{ + bz2_context *context = (bz2_context *) file; + char *name = NULL; + if (context->reset) { + name = context->name; + context->reset = false; + } + return name; +} + +static void +bz2_close(struct archive_file *file) +{ + bz2_context *context = (bz2_context *) file; + if (context->name) + g_free(context->name); + + input_stream_close(&context->istream); + g_free(context); +} + +/* single archive handling */ + +static void +bz2_setup_stream(struct archive_file *file, struct input_stream *is) +{ + bz2_context *context = (bz2_context *) file; + //setup file ops + is->plugin = &bz2_inputplugin; + //insert back reference + is->archive = context; + is->seekable = false; +} + + +static bool +bz2_is_open(struct input_stream *is, G_GNUC_UNUSED const char *url) +{ + bz2_context *context = (bz2_context *) is->archive; + + if (!bz2_alloc(context)) { + g_warning("alloc bz2 failed\n"); + return false; + } + return true; +} + +static void +bz2_is_close(struct input_stream *is) +{ + bz2_context *context = (bz2_context *) is->archive; + bz2_destroy(context); + is->data = NULL; +} + +static int +bz2_fillbuffer(bz2_context *context, + size_t numBytes) +{ + size_t count; + bz_stream *bzstream; + + bzstream = &context->bzstream; + + if (bzstream->avail_in > 0) + return 0; + + count = input_stream_read(&context->istream, + context->buffer, BZ_BUFSIZE); + + if (count == 0) { + if (bzstream->avail_out == numBytes) + return -1; + if (!input_stream_eof(&context->istream)) + context->last_parent_result = 1; + } else { + bzstream->next_in = context->buffer; + bzstream->avail_in = count; + } + + return 0; +} + +static size_t +bz2_is_read(struct input_stream *is, void *ptr, size_t size) +{ + bz2_context *context = (bz2_context *) is->archive; + bz_stream *bzstream; + int bz_result; + size_t numBytes = size; + size_t bytesRead = 0; + + if (context->last_bz_result != BZ_OK) + return 0; + if (context->last_parent_result != 0) + return 0; + + bzstream = &context->bzstream; + bzstream->next_out = ptr; + bzstream->avail_out = numBytes; + + while (bzstream->avail_out != 0) { + if (bz2_fillbuffer(context, numBytes) != 0) + break; + + bz_result = BZ2_bzDecompress(bzstream); + + if (context->last_bz_result != BZ_OK + && bzstream->avail_out == numBytes) { + context->last_bz_result = bz_result; + break; + } + + if (bz_result == BZ_STREAM_END) { + context->last_bz_result = bz_result; + break; + } + } + + bytesRead = numBytes - bzstream->avail_out; + is->offset += bytesRead; + + return bytesRead; +} + +static bool +bz2_is_eof(struct input_stream *is) +{ + bz2_context *context = (bz2_context *) is->archive; + + if (context->last_bz_result == BZ_STREAM_END) { + return true; + } + + return false; +} + +static bool +bz2_is_seek(G_GNUC_UNUSED struct input_stream *is, + G_GNUC_UNUSED off_t offset, G_GNUC_UNUSED int whence) +{ + return false; +} + +static int +bz2_is_buffer(G_GNUC_UNUSED struct input_stream *is) +{ + return 0; +} + +/* exported structures */ + +static const char *const bz2_extensions[] = { + "bz2", + NULL +}; + +static const struct input_plugin bz2_inputplugin = { + .open = bz2_is_open, + .close = bz2_is_close, + .read = bz2_is_read, + .eof = bz2_is_eof, + .seek = bz2_is_seek, + .buffer = bz2_is_buffer +}; + +const struct archive_plugin bz2_plugin = { + .name = "bz2", + .open = bz2_open, + .scan_reset = bz2_scan_reset, + .scan_next = bz2_scan_next, + .setup_stream = bz2_setup_stream, + .close = bz2_close, + .suffixes = bz2_extensions +}; + diff --git a/src/archive/iso_plugin.c b/src/archive/iso_plugin.c new file mode 100644 index 000000000..b374b9dfe --- /dev/null +++ b/src/archive/iso_plugin.c @@ -0,0 +1,258 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka <viliam.mateicka@gmail.com> + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * iso archive handling (requires cdio, and iso9660) + */ + +#include "archive_api.h" +#include "input_stream.h" +#include "utils.h" + +#include <cdio/cdio.h> +#include <cdio/iso9660.h> + +#include <glib.h> +#include <string.h> + +#define CEILING(x, y) ((x+(y-1))/y) + +typedef struct { + iso9660_t *iso; + iso9660_stat_t *statbuf; + size_t cur_ofs; + size_t max_blocks; + GSList *list; + GSList *iter; +} iso_context; + +static const struct input_plugin iso_inputplugin; + +/* archive open && listing routine */ + +static void +listdir_recur(const char *psz_path, iso_context *context) +{ + iso9660_t *iso = context->iso; + CdioList_t *entlist; + CdioListNode_t *entnode; + iso9660_stat_t *statbuf; + char pathname[4096]; + + entlist = iso9660_ifs_readdir (iso, psz_path); + if (!entlist) { + return; + } + /* Iterate over the list of nodes that iso9660_ifs_readdir gives */ + _CDIO_LIST_FOREACH (entnode, entlist) { + statbuf = (iso9660_stat_t *) _cdio_list_node_data (entnode); + + strcpy(pathname, psz_path); + strcat(pathname, statbuf->filename); + + if (_STAT_DIR == statbuf->type ) { + if (strcmp(statbuf->filename, ".") && strcmp(statbuf->filename, "..")) { + strcat(pathname, "/"); + listdir_recur(pathname, context); + } + } else { + //remove leading / + context->list = g_slist_prepend( context->list, + xstrdup(pathname+1)); + } + } + _cdio_list_free (entlist, true); +} + +static struct archive_file * +iso_open(char * pathname) +{ + iso_context *context = g_malloc(sizeof(iso_context)); + + context->list = NULL; + + /* open archive */ + context->iso = iso9660_open (pathname); + if (context->iso == NULL) { + g_warning("iso %s open failed\n", pathname); + return NULL; + } + + listdir_recur("/", context); + + return (struct archive_file *)context; +} + +static void +iso_scan_reset(struct archive_file *file) +{ + iso_context *context = (iso_context *) file; + //reset iterator + context->iter = context->list; +} + +static char * +iso_scan_next(struct archive_file *file) +{ + iso_context *context = (iso_context *) file; + char *data = NULL; + if (context->iter != NULL) { + ///fetch data and goto next + data = context->iter->data; + context->iter = g_slist_next(context->iter); + } + return data; +} + +static void +iso_close(struct archive_file *file) +{ + iso_context *context = (iso_context *) file; + GSList *tmp; + if (context->list) { + //free list + for (tmp = context->list; tmp != NULL; tmp = g_slist_next(tmp)) + g_free(tmp->data); + g_slist_free(context->list); + } + //close archive + iso9660_close(context->iso); + context->iso = NULL; +} + +/* single archive handling */ + +static void +iso_setup_stream(struct archive_file *file, struct input_stream *is) +{ + iso_context *context = (iso_context *) file; + //setup file ops + is->plugin = &iso_inputplugin; + //insert back reference + is->archive = context; + //we are not seekable + is->seekable = false; +} + + +static bool +iso_is_open(struct input_stream *is, const char *pathname) +{ + iso_context *context = (iso_context *) is->archive; + + context->statbuf = iso9660_ifs_stat_translate (context->iso, pathname); + + if (context->statbuf == NULL) { + g_warning("file %s not found in iso\n", pathname); + return false; + } + context->cur_ofs = 0; + context->max_blocks = CEILING(context->statbuf->size, ISO_BLOCKSIZE); + return true; +} + +static void +iso_is_close(struct input_stream *is) +{ + iso_context *context = (iso_context *) is->archive; + g_free(context->statbuf); +} + + +static size_t +iso_is_read(struct input_stream *is, void *ptr, size_t size) +{ + iso_context *context = (iso_context *) is->archive; + int toread, readed = 0; + int no_blocks, cur_block; + size_t left_bytes = context->statbuf->size - context->cur_ofs; + + size = (size * ISO_BLOCKSIZE) / ISO_BLOCKSIZE; + + if (left_bytes < size) { + toread = left_bytes; + no_blocks = CEILING(left_bytes,ISO_BLOCKSIZE); + } else { + toread = size; + no_blocks = toread / ISO_BLOCKSIZE; + } + if (no_blocks > 0) { + + cur_block = context->cur_ofs / ISO_BLOCKSIZE; + + readed = iso9660_iso_seek_read (context->iso, ptr, + context->statbuf->lsn + cur_block, no_blocks); + + if (readed != no_blocks * ISO_BLOCKSIZE) { + g_warning("error reading ISO file at lsn %lu\n", + (long unsigned int) cur_block ); + return -1; + } + if (left_bytes < size) { + readed = left_bytes; + } + context->cur_ofs += readed; + } + return readed; +} + +static bool +iso_is_eof(struct input_stream *is) +{ + iso_context *context = (iso_context *) is->archive; + return (context->cur_ofs == context->statbuf->size); +} + +static bool +iso_is_seek(G_GNUC_UNUSED struct input_stream *is, + G_GNUC_UNUSED off_t offset, G_GNUC_UNUSED int whence) +{ + return false; +} + +static int +iso_is_buffer(G_GNUC_UNUSED struct input_stream *is) +{ + return 0; +} + +/* exported structures */ + +static const char *const iso_extensions[] = { + "iso", + NULL +}; + +static const struct input_plugin iso_inputplugin = { + .open = iso_is_open, + .close = iso_is_close, + .read = iso_is_read, + .eof = iso_is_eof, + .seek = iso_is_seek, + .buffer = iso_is_buffer +}; + +const struct archive_plugin iso_plugin = { + .name = "iso", + .open = iso_open, + .scan_reset = iso_scan_reset, + .scan_next = iso_scan_next, + .setup_stream = iso_setup_stream, + .close = iso_close, + .suffixes = iso_extensions +}; diff --git a/src/archive/zip_plugin.c b/src/archive/zip_plugin.c new file mode 100644 index 000000000..6ef53a625 --- /dev/null +++ b/src/archive/zip_plugin.c @@ -0,0 +1,196 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka <viliam.mateicka@gmail.com> + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * zip archive handling (requires zziplib) + */ + +#include "archive_api.h" +#include "archive_api.h" +#include "input_stream.h" +#include "utils.h" + +#include <zzip/zzip.h> +#include <glib.h> +#include <string.h> + +typedef struct { + ZZIP_DIR *dir; + ZZIP_FILE *file; + size_t length; + GSList *list; + GSList *iter; +} zip_context; + +static const struct input_plugin zip_inputplugin; + +/* archive open && listing routine */ + +static struct archive_file * +zip_open(char * pathname) +{ + zip_context *context = g_malloc(sizeof(zip_context)); + ZZIP_DIRENT dirent; + + // open archive + context->list = NULL; + context->dir = zzip_dir_open(pathname, 0); + if (context->dir == NULL) { + g_warning("zipfile %s open failed\n", pathname); + return NULL; + } + + while (zzip_dir_read(context->dir, &dirent)) { + context->list = g_slist_prepend( context->list, xstrdup(dirent.d_name)); + } + + return (struct archive_file *)context; +} + +static void +zip_scan_reset(struct archive_file *file) +{ + zip_context *context = (zip_context *) file; + //reset iterator + context->iter = context->list; +} + +static char * +zip_scan_next(struct archive_file *file) +{ + zip_context *context = (zip_context *) file; + char *data = NULL; + if (context->iter != NULL) { + ///fetch data and goto next + data = context->iter->data; + context->iter = g_slist_next(context->iter); + } + return data; +} + +static void +zip_close(struct archive_file *file) +{ + zip_context *context = (zip_context *) file; + if (context->list) { + //free list + for (GSList *tmp = context->list; tmp != NULL; tmp = g_slist_next(tmp)) + g_free(tmp->data); + g_slist_free(context->list); + } + //close archive + zzip_dir_close (context->dir); + context->dir = NULL; +} + +/* single archive handling */ + +static void +zip_setup_stream(struct archive_file *file, struct input_stream *is) +{ + zip_context *context = (zip_context *) file; + //setup file ops + is->plugin = &zip_inputplugin; + //insert back reference + is->archive = context; + //we are not seekable + is->seekable = false; +} + + +static bool +zip_is_open(struct input_stream *is, const char *pathname) +{ + zip_context *context = (zip_context *) is->archive; + ZZIP_STAT z_stat; + + context->file = zzip_file_open(context->dir, pathname, 0); + if (!context->file) { + g_warning("file %s not found in the zipfile\n", pathname); + return false; + } + zzip_file_stat(context->file, &z_stat); + context->length = z_stat.st_size; + return true; +} + +static void +zip_is_close(struct input_stream *is) +{ + zip_context *context = (zip_context *) is->archive; + zzip_file_close (context->file); +} + +static size_t +zip_is_read(struct input_stream *is, void *ptr, size_t size) +{ + zip_context *context = (zip_context *) is->archive; + int ret; + ret = zzip_file_read(context->file, ptr, size); + if (ret < 0) { + g_warning("error %d reading zipfile\n", ret); + return 0; + } + return ret; +} + +static bool +zip_is_eof(struct input_stream *is) +{ + zip_context *context = (zip_context *) is->archive; + return ((size_t) zzip_tell(context->file) == context->length); +} + +static bool +zip_is_seek(G_GNUC_UNUSED struct input_stream *is, + G_GNUC_UNUSED off_t offset, G_GNUC_UNUSED int whence) +{ + return false; +} + +static int +zip_is_buffer(G_GNUC_UNUSED struct input_stream *is) +{ + return 0; +} + +/* exported structures */ + +static const char *const zip_extensions[] = { + "zip", + NULL +}; + +static const struct input_plugin zip_inputplugin = { + .open = zip_is_open, + .close = zip_is_close, + .read = zip_is_read, + .eof = zip_is_eof, + .seek = zip_is_seek, + .buffer = zip_is_buffer +}; + +const struct archive_plugin zip_plugin = { + .name = "zip", + .open = zip_open, + .scan_reset = zip_scan_reset, + .scan_next = zip_scan_next, + .setup_stream = zip_setup_stream, + .close = zip_close, + .suffixes = zip_extensions +}; diff --git a/src/archive_api.c b/src/archive_api.c new file mode 100644 index 000000000..ddbcc43b3 --- /dev/null +++ b/src/archive_api.c @@ -0,0 +1,111 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka <viliam.mateicka@gmail.com> + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include <stdio.h> + +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <errno.h> +#include <glib.h> + +#include "utils.h" +#include "archive_api.h" + +/** + * + * archive_lookup is used to determine if part of pathname refers to an regular + * file (archive). If so then its also used to split pathname into archive file + * and path used to locate file in archive. It also returns suffix of the file. + * How it works: + * We do stat of the parent of input pathname as long as we find an regular file + * Normally this should never happen. When routine returns true pathname modified + * and split into archive, inpath and suffix. Otherwise nothing happens + * + * For example: + * + * /music/path/Talco.zip/Talco - Combat Circus/12 - A la pachenka.mp3 + * is split into archive: /music/path/Talco.zip + * inarchive pathname: Talco - Combat Circus/12 - A la pachenka.mp3 + * and suffix: zip + */ + +bool archive_lookup(char *pathname, char **archive, char **inpath, char **suffix) +{ + char *pathdupe; + int len, idx; + struct stat st_info; + bool ret = false; + + *archive = NULL; + *inpath = NULL; + *suffix = NULL; + + pathdupe = g_strdup(pathname); + len = idx = strlen(pathname); + + while (idx > 0) { + //try to stat if its real directory + if (stat(pathdupe, &st_info) == -1) { + if (errno != ENOTDIR) { + g_warning("stat %s failed (errno=%d)\n", pathdupe, errno); + break; + } + } else { + //is something found ins original path (is not an archive) + if (idx == len) { + break; + } + //its a file ? + if (S_ISREG(st_info.st_mode)) { + //so the upper should be file + pathname[idx] = 0; + ret = true; + *archive = pathname; + *inpath = pathname + idx+1; + + //try to get suffix + *suffix = NULL; + while (idx > 0) { + if (pathname[idx] == '.') { + *suffix = pathname + idx + 1; + break; + } + idx--; + } + break; + } else { + g_warning("not a regular file %s\n", pathdupe); + break; + } + } + //find one dir up + while (idx > 0) { + if (pathdupe[idx] == '/') { + pathdupe[idx] = 0; + break; + } + idx--; + } + } + g_free(pathdupe); + return ret; +} + diff --git a/src/archive_api.h b/src/archive_api.h new file mode 100644 index 000000000..3c9e979f6 --- /dev/null +++ b/src/archive_api.h @@ -0,0 +1,94 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka <viliam.mateicka@gmail.com> + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef MPD_ARCHIVE_API_H +#define MPD_ARCHIVE_API_H + +/* + * This is the public API which is used by archive plugins to + * provide transparent archive decompression layer for mpd + * + */ + +#include "archive_internal.h" +#include "input_stream.h" + +#include <stdbool.h> + +struct archive_file; + +struct archive_plugin { + const char *name; + + /** + * optional, set this to NULL if the archive plugin doesn't + * have/need one this must false if there is an error and + * true otherwise + */ + bool (*init)(void); + + /** + * optional, set this to NULL if the archive plugin doesn't + * have/need one + */ + void (*finish)(void); + + /** + * tryes to open archive file and associates handle with archive + * returns pointer to handle used is all operations with this archive + * or NULL when opening fails + */ + struct archive_file *(*open)(char * pathname); + + /** + * reset routine will move current read index in archive to default + * position and then the filenames from archives can be read + * via scan_next routine + */ + void (*scan_reset)(struct archive_file *); + + /** + * the read method will return corresponding files from archive + * (as pathnames) and move read index to next file. When there is no + * next file it return NULL. + */ + char *(*scan_next)(struct archive_file *); + + /** + * this is used to setup input stream handle, to be able to read + * from archive. open method of inputstream can be the used to + * extract particular file + */ + void (*setup_stream)(struct archive_file *, struct input_stream *is); + + /** + * closes archive file. + */ + void (*close)(struct archive_file *); + + /** + * suffixes handled by this plugin. + * last element in these arrays must always be a NULL + */ + const char *const*suffixes; +}; + +bool archive_lookup(char *pathname, char **archive, char **inpath, char **suffix); + +#endif + diff --git a/src/archive_internal.h b/src/archive_internal.h new file mode 100644 index 000000000..828cd4caf --- /dev/null +++ b/src/archive_internal.h @@ -0,0 +1,26 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka <viliam.mateicka@gmail.com> + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef MPD_ARCHIVE_INTERNAL_H +#define MPD_ARCHIVE_INTERNAL_H + +struct archive_file { + int placeholder; +}; + +#endif diff --git a/src/archive_list.c b/src/archive_list.c new file mode 100644 index 000000000..046c6dbe4 --- /dev/null +++ b/src/archive_list.c @@ -0,0 +1,121 @@ + + + +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka <viliam.mateicka@gmail.com> + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "archive_list.h" +#include "archive_api.h" +#include "utils.h" +#include "../config.h" + +#include <string.h> +#include <glib.h> + +extern const struct archive_plugin bz2_plugin; +extern const struct archive_plugin zip_plugin; +extern const struct archive_plugin iso_plugin; + +static const struct archive_plugin *const archive_plugins[] = { +#ifdef HAVE_BZ2 + &bz2_plugin, +#endif +#ifdef HAVE_ZIP + &zip_plugin, +#endif +#ifdef HAVE_ISO + &iso_plugin, +#endif + NULL +}; + +enum { + num_archive_plugins = G_N_ELEMENTS(archive_plugins)-1, +}; + +/** which plugins have been initialized successfully? */ +static bool archive_plugins_enabled[num_archive_plugins+1]; + +const struct archive_plugin * +archive_plugin_from_suffix(const char *suffix) +{ + unsigned i; + + if (suffix == NULL) + return NULL; + + for (i=0; i < num_archive_plugins; ++i) { + const struct archive_plugin *plugin = archive_plugins[i]; + if (archive_plugins_enabled[i] && + stringFoundInStringArray(plugin->suffixes, suffix)) { + ++i; + return plugin; + } + } + return NULL; +} + +const struct archive_plugin * +archive_plugin_from_name(const char *name) +{ + for (unsigned i = 0; i < num_archive_plugins; ++i) { + const struct archive_plugin *plugin = archive_plugins[i]; + if (archive_plugins_enabled[i] && + strcmp(plugin->name, name) == 0) + return plugin; + } + return NULL; +} + +void archive_plugin_print_all_suffixes(FILE * fp) +{ + const char *const*suffixes; + + for (unsigned i = 0; i < num_archive_plugins; ++i) { + const struct archive_plugin *plugin = archive_plugins[i]; + if (!archive_plugins_enabled[i]) + continue; + + suffixes = plugin->suffixes; + while (suffixes && *suffixes) { + fprintf(fp, "%s ", *suffixes); + suffixes++; + } + } + fprintf(fp, "\n"); + fflush(fp); +} + +void archive_plugin_init_all(void) +{ + for (unsigned i = 0; i < num_archive_plugins; ++i) { + const struct archive_plugin *plugin = archive_plugins[i]; + if (plugin->init == NULL || archive_plugins[i]->init()) + archive_plugins_enabled[i] = true; + } +} + +void archive_plugin_deinit_all(void) +{ + for (unsigned i = 0; i < num_archive_plugins; ++i) { + const struct archive_plugin *plugin = archive_plugins[i]; + if (archive_plugins_enabled[i] && plugin->finish != NULL) + archive_plugins[i]->finish(); + } +} + diff --git a/src/archive_list.h b/src/archive_list.h new file mode 100644 index 000000000..d2070c3a5 --- /dev/null +++ b/src/archive_list.h @@ -0,0 +1,44 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka <viliam.mateicka@gmail.com> + * This project's homepage is: http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef MPD_ARCHIVE_LIST_H +#define MPD_ARCHIVE_LIST_H + +#include "archive_api.h" + +#include <stdio.h> + +struct archive_plugin; + +/* interface for using plugins */ + +const struct archive_plugin * +archive_plugin_from_suffix(const char *suffix); + +const struct archive_plugin * +archive_plugin_from_name(const char *name); + +void archive_plugin_print_all_suffixes(FILE * fp); + +/* this is where we "load" all the "plugins" ;-) */ +void archive_plugin_init_all(void); + +/* this is where we "unload" all the "plugins" */ +void archive_plugin_deinit_all(void); + +#endif |