From e61ba50ebd77e992f36deddb7f579c090eff0995 Mon Sep 17 00:00:00 2001 From: Viliam Mateicka Date: Wed, 3 Dec 2008 20:26:08 +0100 Subject: decoder: audiofile plugin using input stream instead of file --- src/decoder/audiofile_plugin.c | 89 +++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/decoder/audiofile_plugin.c b/src/decoder/audiofile_plugin.c index c862074b5..c4c72ea42 100644 --- a/src/decoder/audiofile_plugin.c +++ b/src/decoder/audiofile_plugin.c @@ -20,8 +20,8 @@ #include "../decoder_api.h" -#include #include +#include #include #undef G_LOG_DOMAIN @@ -44,27 +44,79 @@ static int getAudiofileTotalTime(const char *file) return total_time; } +static ssize_t +audiofile_file_read(AFvirtualfile *vfile, void *data, size_t nbytes) +{ + struct input_stream *is = (struct input_stream *) vfile->closure; + return input_stream_read(is, data, nbytes); +} + +static long +audiofile_file_length(AFvirtualfile *vfile) +{ + struct input_stream *is = (struct input_stream *) vfile->closure; + return is->size; +} + +static long +audiofile_file_tell(AFvirtualfile *vfile) +{ + struct input_stream *is = (struct input_stream *) vfile->closure; + return is->offset; +} + +static void +audiofile_file_destroy(AFvirtualfile *vfile) +{ + struct input_stream *is = (struct input_stream *) vfile->closure; + vfile->closure = NULL; + input_stream_close(is); +} + +static long +audiofile_file_seek(AFvirtualfile *vfile, long offset, int is_relative) +{ + struct input_stream *is = (struct input_stream *) vfile->closure; + int whence = (is_relative ? SEEK_CUR : SEEK_SET); + if (input_stream_seek(is, offset, whence)) { + return is->offset; + } else { + return -1; + } +} + +static AFvirtualfile * +setup_virtual_fops(struct input_stream *stream) +{ + AFvirtualfile *vf = g_malloc(sizeof(AFvirtualfile)); + vf->closure = stream; + vf->write = NULL; + vf->read = audiofile_file_read; + vf->length = audiofile_file_length; + vf->destroy = audiofile_file_destroy; + vf->seek = audiofile_file_seek; + vf->tell = audiofile_file_tell; + return vf; +} + static void -audiofile_decode(struct decoder *decoder, const char *path) +audiofile_streamdecode(struct decoder * decoder, struct input_stream *inStream) { + AFvirtualfile *vf; int fs, frame_count; AFfilehandle af_fp; int bits; struct audio_format audio_format; float total_time; uint16_t bitRate; - struct stat st; int ret, current = 0; char chunk[CHUNK_SIZE]; - if (stat(path, &st) < 0) { - g_warning("failed to stat: %s\n", path); - return; - } + vf = setup_virtual_fops(inStream); - af_fp = afOpenFile(path, "r", NULL); + af_fp = afOpenVirtualFile(vf, "r", NULL); if (af_fp == AF_NULL_FILEHANDLE) { - g_warning("failed to open: %s\n", path); + g_warning("failed to input stream\n"); return; } @@ -89,7 +141,7 @@ audiofile_decode(struct decoder *decoder, const char *path) total_time = ((float)frame_count / (float)audio_format.sample_rate); - bitRate = (uint16_t)(st.st_size * 8.0 / total_time / 1000.0 + 0.5); + bitRate = (uint16_t)(inStream->size * 8.0 / total_time / 1000.0 + 0.5); fs = (int)afGetVirtualFrameSize(af_fp, AF_DEFAULT_TRACK, 1); @@ -118,7 +170,7 @@ audiofile_decode(struct decoder *decoder, const char *path) afCloseFile(af_fp); } -static struct tag *audiofileTagDup(const char *file) +static struct tag *audiofile_tag_dup(const char *file) { struct tag *ret = NULL; int total_time = getAudiofileTotalTime(file); @@ -134,13 +186,20 @@ static struct tag *audiofileTagDup(const char *file) return ret; } -static const char *const audiofileSuffixes[] = { +static const char *const audiofile_suffixes[] = { "wav", "au", "aiff", "aif", NULL }; +static const char *const audiofile_mime_types[] = { + "audio/x-wav", + "audio/x-aiff", + NULL +}; + const struct decoder_plugin audiofilePlugin = { .name = "audiofile", - .file_decode = audiofile_decode, - .tag_dup = audiofileTagDup, - .suffixes = audiofileSuffixes, + .stream_decode = audiofile_streamdecode, + .tag_dup = audiofile_tag_dup, + .suffixes = audiofile_suffixes, + .mime_types = audiofile_mime_types, }; -- cgit v1.2.3 From 01c591fb84dff8c481dc5dc59420300880e37fcc Mon Sep 17 00:00:00 2001 From: Viliam Mateicka Date: Tue, 9 Dec 2008 17:36:10 +0100 Subject: utils: moving stringFoundInStringArray() from decoder into utils --- src/decoder_list.c | 12 +----------- src/utils.c | 11 +++++++++++ src/utils.h | 2 ++ 3 files changed, 14 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/decoder_list.c b/src/decoder_list.c index c7f66289f..c70d6334c 100644 --- a/src/decoder_list.c +++ b/src/decoder_list.c @@ -18,6 +18,7 @@ #include "decoder_list.h" #include "decoder_api.h" +#include "utils.h" #include @@ -76,17 +77,6 @@ enum { /** which plugins have been initialized successfully? */ static bool decoder_plugins_enabled[num_decoder_plugins]; -static int stringFoundInStringArray(const char *const*array, const char *suffix) -{ - while (array && *array) { - if (strcasecmp(*array, suffix) == 0) - return 1; - array++; - } - - return 0; -} - const struct decoder_plugin * decoder_plugin_from_suffix(const char *suffix, unsigned int next) { diff --git a/src/utils.c b/src/utils.c index 9b75748da..1b12cd810 100644 --- a/src/utils.c +++ b/src/utils.c @@ -235,3 +235,14 @@ void xpthread_cond_destroy(pthread_cond_t *cond) if ((err = pthread_cond_destroy(cond))) FATAL("failed to destroy cond: %s\n", strerror(err)); } + +int stringFoundInStringArray(const char *const*array, const char *suffix) +{ + while (array && *array) { + if (strcasecmp(*array, suffix) == 0) + return 1; + array++; + } + + return 0; +} diff --git a/src/utils.h b/src/utils.h index 0990185ee..e7594c261 100644 --- a/src/utils.h +++ b/src/utils.h @@ -108,4 +108,6 @@ void xpthread_mutex_destroy(pthread_mutex_t *mutex); void xpthread_cond_destroy(pthread_cond_t *cond); +int stringFoundInStringArray(const char *const*array, const char *suffix); + #endif -- cgit v1.2.3 From c73ebac4af7a3158189ba82c5eb74ae26e2b1106 Mon Sep 17 00:00:00 2001 From: Viliam Mateicka Date: Tue, 16 Dec 2008 21:42:34 +0100 Subject: new archive api, input_archive stream --- src/Makefile.am | 10 +++- src/archive_api.c | 111 +++++++++++++++++++++++++++++++++++ src/archive_api.h | 94 ++++++++++++++++++++++++++++++ src/archive_internal.h | 26 +++++++++ src/archive_list.c | 105 +++++++++++++++++++++++++++++++++ src/archive_list.h | 44 ++++++++++++++ src/input_archive.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++ src/input_archive.h | 24 ++++++++ src/input_stream.c | 2 + src/input_stream.h | 2 + src/main.c | 8 +++ 11 files changed, 578 insertions(+), 2 deletions(-) create mode 100644 src/archive_api.c create mode 100644 src/archive_api.h create mode 100644 src/archive_internal.h create mode 100644 src/archive_list.c create mode 100644 src/archive_list.h create mode 100644 src/input_archive.c create mode 100644 src/input_archive.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 98b1cc830..006feeaa5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -81,7 +81,10 @@ mpd_headers = \ zeroconf.h \ locate.h \ stored_playlist.h \ - timer.h + timer.h \ + archive_api.h \ + archive_list.h \ + input_archive.h mpd_SOURCES = \ @@ -155,7 +158,10 @@ mpd_SOURCES = \ volume.c \ locate.c \ stored_playlist.c \ - timer.c + timer.c \ + archive_api.c \ + archive_list.c \ + input_archive.c if HAVE_LIBSAMPLERATE mpd_SOURCES += pcm_resample_libsamplerate.c 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 + * 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 + +#include +#include +#include +#include +#include +#include + +#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 + * 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 + +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 + * 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..97aefdac2 --- /dev/null +++ b/src/archive_list.c @@ -0,0 +1,105 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka + * 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 +#include + +static const struct archive_plugin *const archive_plugins[] = { + 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 + * 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 + +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 diff --git a/src/input_archive.c b/src/input_archive.c new file mode 100644 index 000000000..4f573bd78 --- /dev/null +++ b/src/input_archive.c @@ -0,0 +1,154 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka + * 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_api.h" +#include "archive_list.h" +#include "input_archive.h" +#include "input_stream.h" +#include "gcc.h" +#include "log.h" +#include "ls.h" +#include "utils.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct { + const struct archive_plugin *aplugin; + const struct input_plugin *iplugin; + struct archive_file *file; +} archive_context; + +/** + * select correct archive plugin to handle the input stream + * may allow stacking of archive plugins. for example for handling + * tar.gz a gzip handler opens file (through inputfile stream) + * then it opens a tar handler and sets gzip inputstream as + * parent_stream so tar plugin fetches file data from gzip + * plugin and gzip fetches file from disk + */ +static bool +input_archive_open(struct input_stream *is, const char *pathname) +{ + archive_context *arch_ctx; + const struct archive_plugin *arplug; + char *archive, *filename, *suffix, *pname; + bool opened; + + if (pathname[0] != '/') + return false; + + pname = g_strdup(pathname); + // archive_lookup will modify pname when true is returned + if (!archive_lookup(pname, &archive, &filename, &suffix)) { + g_debug("not an archive, lookup %s failed\n", pname); + g_free(pname); + return false; + } + + //check which archive plugin to use (by ext) + arplug = archive_plugin_from_suffix(suffix); + if (!arplug) { + g_warning("can't handle archive %s\n",archive); + g_free(pname); + return false; + } + + arch_ctx = (archive_context *) g_malloc(sizeof(archive_context)); + + //setup archive plugin pointer + arch_ctx->aplugin = arplug; + //open archive file + arch_ctx->file = arplug->open(archive); + //setup fileops + arplug->setup_stream(arch_ctx->file, is); + //setup input plugin backup + arch_ctx->iplugin = is->plugin; + is->plugin = &input_plugin_archive; + + //internal handle + is->data = arch_ctx; + + //open archive + opened = arch_ctx->iplugin->open(is, filename); + + if (!opened) { + g_warning("open inarchive file %s failed\n\n",filename); + } else { + is->ready = true; + } + g_free(pname); + return opened; +} + +static void +input_archive_close(struct input_stream *is) +{ + archive_context *arch_ctx = (archive_context *)is->data; + //close archive infile ops + arch_ctx->iplugin->close(is); + //close archive + arch_ctx->aplugin->close(arch_ctx->file); + //free private data + g_free(arch_ctx); +} + +static bool +input_archive_seek(struct input_stream *is, off_t offset, int whence) +{ + archive_context *arch_ctx = (archive_context *)is->data; + return arch_ctx->iplugin->seek(is, offset, whence); +} + +static size_t +input_archive_read(struct input_stream *is, void *ptr, size_t size) +{ + archive_context *arch_ctx = (archive_context *)is->data; + assert(ptr != NULL); + assert(size > 0); + return arch_ctx->iplugin->read(is, ptr, size); +} + +static bool +input_archive_eof(struct input_stream *is) +{ + archive_context *arch_ctx = (archive_context *)is->data; + return arch_ctx->iplugin->eof(is); +} + +static int +input_archive_buffer(struct input_stream *is) +{ + archive_context *arch_ctx = (archive_context *)is->data; + return arch_ctx->iplugin->buffer(is); +} + +const struct input_plugin input_plugin_archive = { + .open = input_archive_open, + .close = input_archive_close, + .buffer = input_archive_buffer, + .read = input_archive_read, + .eof = input_archive_eof, + .seek = input_archive_seek, +}; diff --git a/src/input_archive.h b/src/input_archive.h new file mode 100644 index 000000000..8fc93b433 --- /dev/null +++ b/src/input_archive.h @@ -0,0 +1,24 @@ +/* the Music Player Daemon (MPD) + * Copyright (C) 2008 Viliam Mateicka + * 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_INPUT_ARCHIVE_H +#define MPD_INPUT_ARCHIVE_H + +extern const struct input_plugin input_plugin_archive; + +#endif diff --git a/src/input_stream.c b/src/input_stream.c index 65b6a89b2..45b7ec5e9 100644 --- a/src/input_stream.c +++ b/src/input_stream.c @@ -20,6 +20,7 @@ #include "config.h" #include "input_file.h" +#include "input_archive.h" #ifdef HAVE_CURL #include "input_curl.h" @@ -30,6 +31,7 @@ static const struct input_plugin *const input_plugins[] = { &input_plugin_file, + &input_plugin_archive, #ifdef HAVE_CURL &input_plugin_curl, #endif diff --git a/src/input_stream.h b/src/input_stream.h index c8d8068d3..7ed0039a3 100644 --- a/src/input_stream.h +++ b/src/input_stream.h @@ -48,6 +48,8 @@ struct input_stream { void *data; char *meta_name; char *meta_title; + + void *archive; }; void input_stream_global_init(void); diff --git a/src/main.c b/src/main.c index 538b559ee..199d76471 100644 --- a/src/main.c +++ b/src/main.c @@ -40,6 +40,7 @@ #include "permission.h" #include "replay_gain.h" #include "decoder_list.h" +#include "archive_list.h" #include "audioOutput.h" #include "input_stream.h" #include "state_file.h" @@ -145,6 +146,11 @@ static void version(void) puts("\n" "Supported outputs:\n"); printAllOutputPluginTypes(stdout); + + puts("\n" + "Supported archives:\n"); + archive_plugin_init_all(); + archive_plugin_print_all_suffixes(stdout); } static void parseOptions(int argc, char **argv, Options * options) @@ -415,6 +421,7 @@ int main(int argc, char *argv[]) mapper_init(); initPermissions(); initPlaylist(); + archive_plugin_init_all(); decoder_plugin_init_all(); update_global_init(); @@ -500,6 +507,7 @@ int main(int argc, char *argv[]) command_finish(); update_global_finish(); decoder_plugin_deinit_all(); + archive_plugin_deinit_all(); music_pipe_free(); cleanUpPidFile(); finishConf(); -- cgit v1.2.3 From 4d604a7540ae43ff01346f388ef3dede95ac856f Mon Sep 17 00:00:00 2001 From: Viliam Mateicka Date: Tue, 16 Dec 2008 21:42:42 +0100 Subject: ls: adding get_archive_by_suffix() function --- src/ls.c | 15 +++++++++++++++ src/ls.h | 4 ++++ 2 files changed, 19 insertions(+) (limited to 'src') diff --git a/src/ls.c b/src/ls.c index 7a2eb11f7..b5cb5cfeb 100644 --- a/src/ls.c +++ b/src/ls.c @@ -128,3 +128,18 @@ hasMusicSuffix(const char *utf8file, unsigned int next) return ret; } + +const struct archive_plugin * +get_archive_by_suffix(const char *utf8file) +{ + const struct archive_plugin *ret = NULL; + + const char *s = getSuffix(utf8file); + if (s) { + ret = archive_plugin_from_suffix(s); + } else { + g_debug("get_archive_by_suffix: The file: %s has no valid suffix\n", + utf8file); + } + return ret; +} diff --git a/src/ls.h b/src/ls.h index ed0f0677f..92839e018 100644 --- a/src/ls.h +++ b/src/ls.h @@ -20,6 +20,7 @@ #define MPD_LS_H #include "decoder_list.h" +#include "archive_list.h" #include @@ -35,6 +36,9 @@ int isRemoteUrl(const char *url); const struct decoder_plugin * hasMusicSuffix(const char *utf8file, unsigned int next); +const struct archive_plugin * +get_archive_by_suffix(const char *utf8file); + int printRemoteUrlHandlers(struct client *client); #endif -- cgit v1.2.3 From 455b39fa264a21c1f8eaa0c558d7ca76541ad505 Mon Sep 17 00:00:00 2001 From: Viliam Mateicka Date: Tue, 16 Dec 2008 21:42:42 +0100 Subject: song: adding support for songs in archives --- src/directory.h | 2 ++ src/song.c | 35 ++++++++++++++++++++++++++++++++++- src/song.h | 3 +++ 3 files changed, 39 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/directory.h b/src/directory.h index 72efa9c91..afe81faad 100644 --- a/src/directory.h +++ b/src/directory.h @@ -34,6 +34,8 @@ #define DIRECTORY_MPD_VERSION "mpd_version: " #define DIRECTORY_FS_CHARSET "fs_charset: " +#define DEVICE_INARCHIVE (unsigned)(-1) + struct directory { struct dirvec children; struct songvec songs; diff --git a/src/song.c b/src/song.c index eafd1fb4a..56ece8eaf 100644 --- a/src/song.c +++ b/src/song.c @@ -74,7 +74,12 @@ song_file_load(const char *path, struct directory *parent) song = song_file_new(path, parent); - ret = song_file_update(song); + //in archive ? + if (parent->device == DEVICE_INARCHIVE) { + ret = song_file_update_inarchive(song); + } else { + ret = song_file_update(song); + } if (!ret) { song_free(song); return NULL; @@ -123,6 +128,34 @@ song_file_update(struct song *song) return song->tag != NULL; } +bool +song_file_update_inarchive(struct song *song) +{ + char buffer[MPD_PATH_MAX]; + const char *path_fs; + const struct decoder_plugin *plugin; + + assert(song_is_file(song)); + + path_fs = map_song_fs(song, buffer); + if (path_fs == NULL) + return false; + + if (song->tag != NULL) { + tag_free(song->tag); + song->tag = NULL; + } + //accept every file that has music suffix + //because we dont support tag reading throught + //input streams + plugin = hasMusicSuffix(path_fs, 0); + if (plugin) { + song->tag = tag_new(); + //tag_add_item(tag, TAG_ITEM_TITLE, f->title); + } + return song->tag != NULL; +} + char * song_get_url(const struct song *song, char *path_max_tmp) { diff --git a/src/song.h b/src/song.h index b8ee3aa99..7f40b7e9f 100644 --- a/src/song.h +++ b/src/song.h @@ -58,6 +58,9 @@ song_free(struct song *song); bool song_file_update(struct song *song); +bool +song_file_update_inarchive(struct song *song); + /* * song_get_url - Returns a path of a song in UTF8-encoded form * path_max_tmp is the argument that the URL is written to, this -- cgit v1.2.3 From 77946a901f1bf7bf7b2dfb5238b14f9d1941d49c Mon Sep 17 00:00:00 2001 From: Viliam Mateicka Date: Tue, 16 Dec 2008 21:42:42 +0100 Subject: update: adding archive updating related code --- src/update.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/update.c b/src/update.c index 2349d3e0f..5d85ffff4 100644 --- a/src/update.c +++ b/src/update.c @@ -276,6 +276,39 @@ make_subdir(struct directory *parent, const char *name) return directory; } +static void +update_archive_tree(struct directory *directory, char *name) +{ + struct directory *subdir; + struct song *song; + char *tmp; + + tmp = strchr(name, '/'); + if (tmp) { + *tmp = 0; + //add dir is not there already + if ((subdir = dirvec_find(&directory->children, name)) == NULL) { + //create new directory + subdir = make_subdir(directory, name); + subdir->device = DEVICE_INARCHIVE; + } + //create directories first + update_archive_tree(subdir, tmp+1); + } else { + //add file + song = songvec_find(&directory->songs, name); + if (song == NULL) { + song = song_file_load(name, directory); + if (song != NULL) { + songvec_add(&directory->songs, song); + modified = true; + LOG("added %s/%s\n", + directory_get_path(directory), name); + } + } + } +} + static bool updateDirectory(struct directory *directory, const struct stat *st); @@ -283,6 +316,7 @@ static void updateInDirectory(struct directory *directory, const char *name, const struct stat *st) { + const struct archive_plugin *archive; assert(strchr(name, '/') == NULL); if (S_ISREG(st->st_mode) && hasMusicSuffix(name, 0)) { @@ -317,8 +351,37 @@ updateInDirectory(struct directory *directory, ret = updateDirectory(subdir, st); if (!ret) delete_directory(subdir); + } else if (S_ISREG(st->st_mode) && (archive = get_archive_by_suffix(name))) { + struct archive_file *archfile; + char pathname[MPD_PATH_MAX]; + + map_directory_child_fs(directory, name, pathname); + //open archive + archfile = archive->open(pathname); + if (archfile) { + char *filepath; + struct directory *archdir; + + g_debug("archive %s opened\n",pathname); + archdir = dirvec_find(&directory->children, name); + if (archdir == NULL) { + g_debug("creating archive directory (%s)\n", name); + archdir = make_subdir(directory, name); + //mark this directory as archive (we use device for this) + archdir->device = DEVICE_INARCHIVE; + } + archive->scan_reset(archfile); + while ((filepath = archive->scan_next(archfile)) != NULL) { + //split name into directory and file + g_debug("adding archive file: %s\n", filepath); + update_archive_tree(archdir, filepath); + } + archive->close(archfile); + } else { + g_warning("unable to open archive %s\n", pathname); + } } else { - DEBUG("update: %s is not a directory or music\n", name); + g_debug("update: %s is not a directory, archive or music\n", name); } } -- cgit v1.2.3 From 98fd9b7da8547efe81d473371bb3ca7620c7b2b2 Mon Sep 17 00:00:00 2001 From: Viliam Mateicka Date: Tue, 16 Dec 2008 21:45:59 +0100 Subject: archiveapi: archive plugin for BZ2 files --- src/Makefile.am | 6 + src/archive/bz2_plugin.c | 303 +++++++++++++++++++++++++++++++++++++++++++++++ src/archive_list.c | 8 ++ 3 files changed, 317 insertions(+) create mode 100644 src/archive/bz2_plugin.c (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 006feeaa5..e694f3325 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -173,6 +173,12 @@ if HAVE_ID3TAG mpd_SOURCES += tag_id3.c endif +# archive plugins + +if HAVE_BZ2 +mpd_SOURCES += archive/bz2_plugin.c +endif + # decoder plugins 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 + * 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 +#include +#include +#include +#include + +#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_list.c b/src/archive_list.c index 97aefdac2..2f86c0ad6 100644 --- a/src/archive_list.c +++ b/src/archive_list.c @@ -1,3 +1,6 @@ + + + /* the Music Player Daemon (MPD) * Copyright (C) 2008 Viliam Mateicka * This project's homepage is: http://www.musicpd.org @@ -24,7 +27,12 @@ #include #include +extern const struct archive_plugin bz2_plugin; + static const struct archive_plugin *const archive_plugins[] = { +#ifdef HAVE_BZ2 + &bz2_plugin, +#endif NULL }; -- cgit v1.2.3 From ddcf0a407876ef886ca8634daa223c6f27c236dc Mon Sep 17 00:00:00 2001 From: Viliam Mateicka Date: Tue, 16 Dec 2008 21:46:11 +0100 Subject: archiveapi: archive plugin for ZIP files --- src/Makefile.am | 3 + src/archive/zip_plugin.c | 196 +++++++++++++++++++++++++++++++++++++++++++++++ src/archive_list.c | 4 + 3 files changed, 203 insertions(+) create mode 100644 src/archive/zip_plugin.c (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index e694f3325..3408ef723 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -179,6 +179,9 @@ if HAVE_BZ2 mpd_SOURCES += archive/bz2_plugin.c endif +if HAVE_ZIP +mpd_SOURCES += archive/zip_plugin.c +endif # decoder plugins 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 + * 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 +#include +#include + +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_list.c b/src/archive_list.c index 2f86c0ad6..9f0dca6a9 100644 --- a/src/archive_list.c +++ b/src/archive_list.c @@ -28,10 +28,14 @@ #include extern const struct archive_plugin bz2_plugin; +extern const struct archive_plugin zip_plugin; static const struct archive_plugin *const archive_plugins[] = { #ifdef HAVE_BZ2 &bz2_plugin, +#endif +#ifdef HAVE_ZIP + &zip_plugin, #endif NULL }; -- cgit v1.2.3 From 37796699cf751eb3ea8625d6c93e38abb46a6c87 Mon Sep 17 00:00:00 2001 From: Viliam Mateicka Date: Tue, 16 Dec 2008 21:48:26 +0100 Subject: archiveapi: archive plugin for ISO files --- src/Makefile.am | 4 + src/archive/iso_plugin.c | 258 +++++++++++++++++++++++++++++++++++++++++++++++ src/archive_list.c | 4 + 3 files changed, 266 insertions(+) create mode 100644 src/archive/iso_plugin.c (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 3408ef723..0ec95ee82 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -183,6 +183,10 @@ if HAVE_ZIP mpd_SOURCES += archive/zip_plugin.c endif +if HAVE_ISO +mpd_SOURCES += archive/iso_plugin.c +endif + # decoder plugins if HAVE_MAD 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 + * 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 +#include + +#include +#include + +#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_list.c b/src/archive_list.c index 9f0dca6a9..046c6dbe4 100644 --- a/src/archive_list.c +++ b/src/archive_list.c @@ -29,6 +29,7 @@ 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 @@ -36,6 +37,9 @@ static const struct archive_plugin *const archive_plugins[] = { #endif #ifdef HAVE_ZIP &zip_plugin, +#endif +#ifdef HAVE_ISO + &iso_plugin, #endif NULL }; -- cgit v1.2.3 From 4674d0442a4edc8647c060fbff2dd5e7ae9e3f84 Mon Sep 17 00:00:00 2001 From: Viliam Mateicka Date: Tue, 23 Dec 2008 18:00:37 +0100 Subject: shout: fixed bad error message printout --- src/output/shout_plugin.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/output/shout_plugin.c b/src/output/shout_plugin.c index fe5fc9a16..7fdf4c1e0 100644 --- a/src/output/shout_plugin.c +++ b/src/output/shout_plugin.c @@ -203,9 +203,9 @@ static void *my_shout_init_driver(struct audio_output *audio_output, } sd->encoder = shout_encoder_plugin_get(encoding); - if (sd->encoder == NULL) - g_error("couldn't find shout encoder plugin for \"%s\" " - "at line %i\n", encoding, block_param->line); + if (sd->encoder == NULL) { + g_error("couldn't find shout encoder plugin for \"%s\"\n", encoding); + } block_param = getBlockParam(param, "protocol"); if (block_param) { -- cgit v1.2.3