diff options
author | Viliam Mateicka <viliam.mateicka@gmail.com> | 2008-12-16 21:42:34 +0100 |
---|---|---|
committer | Viliam Mateicka <viliam.mateicka@gmail.com> | 2008-12-16 21:42:34 +0100 |
commit | c73ebac4af7a3158189ba82c5eb74ae26e2b1106 (patch) | |
tree | 32b460ff927e6e15dcdc49b7c886e494514c8f4a | |
parent | 01c591fb84dff8c481dc5dc59420300880e37fcc (diff) | |
download | mpd-c73ebac4af7a3158189ba82c5eb74ae26e2b1106.tar.gz mpd-c73ebac4af7a3158189ba82c5eb74ae26e2b1106.tar.xz mpd-c73ebac4af7a3158189ba82c5eb74ae26e2b1106.zip |
new archive api, input_archive stream
Diffstat (limited to '')
-rw-r--r-- | src/Makefile.am | 10 | ||||
-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 | 105 | ||||
-rw-r--r-- | src/archive_list.h | 44 | ||||
-rw-r--r-- | src/input_archive.c | 154 | ||||
-rw-r--r-- | src/input_archive.h | 24 | ||||
-rw-r--r-- | src/input_stream.c | 2 | ||||
-rw-r--r-- | src/input_stream.h | 2 | ||||
-rw-r--r-- | src/main.c | 8 |
11 files changed, 578 insertions, 2 deletions
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 <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..97aefdac2 --- /dev/null +++ b/src/archive_list.c @@ -0,0 +1,105 @@ +/* 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> + +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 <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 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 <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_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 <stdbool.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> +#include <assert.h> +#include <glib.h> + +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 <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_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(); |