From a654f146d1b39a363cb10853a7670ef099570e54 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 2 Jan 2013 19:22:15 +0100 Subject: update: convert to C++ --- Makefile.am | 38 ++-- src/InotifyQueue.cxx | 135 +++++++++++++ src/InotifyQueue.hxx | 32 +++ src/InotifySource.cxx | 174 +++++++++++++++++ src/InotifySource.hxx | 61 ++++++ src/InotifyUpdate.cxx | 378 +++++++++++++++++++++++++++++++++++ src/InotifyUpdate.hxx | 47 +++++ src/Main.cxx | 10 +- src/OtherCommands.cxx | 2 +- src/PlayerCommands.cxx | 2 +- src/UpdateArchive.cxx | 159 +++++++++++++++ src/UpdateArchive.hxx | 54 +++++ src/UpdateContainer.cxx | 126 ++++++++++++ src/UpdateContainer.hxx | 36 ++++ src/UpdateDatabase.cxx | 107 ++++++++++ src/UpdateDatabase.hxx | 50 +++++ src/UpdateGlue.cxx | 186 ++++++++++++++++++ src/UpdateGlue.hxx | 40 ++++ src/UpdateIO.cxx | 120 ++++++++++++ src/UpdateIO.hxx | 50 +++++ src/UpdateInternal.hxx | 30 +++ src/UpdateQueue.cxx | 66 +++++++ src/UpdateQueue.hxx | 31 +++ src/UpdateRemove.cxx | 108 ++++++++++ src/UpdateRemove.hxx | 41 ++++ src/UpdateSong.cxx | 116 +++++++++++ src/UpdateSong.hxx | 34 ++++ src/UpdateWalk.cxx | 511 ++++++++++++++++++++++++++++++++++++++++++++++++ src/UpdateWalk.hxx | 39 ++++ src/inotify_queue.c | 135 ------------- src/inotify_queue.h | 32 --- src/inotify_source.c | 169 ---------------- src/inotify_source.h | 61 ------ src/inotify_update.c | 374 ----------------------------------- src/inotify_update.h | 47 ----- src/update.c | 184 ----------------- src/update.h | 42 ---- src/update_archive.c | 156 --------------- src/update_archive.h | 54 ----- src/update_container.c | 123 ------------ src/update_container.h | 37 ---- src/update_db.c | 104 ---------- src/update_db.h | 52 ----- src/update_internal.h | 30 --- src/update_io.c | 117 ----------- src/update_io.h | 51 ----- src/update_queue.c | 66 ------- src/update_queue.h | 33 ---- src/update_remove.c | 102 ---------- src/update_remove.h | 41 ---- src/update_song.c | 113 ----------- src/update_song.h | 35 ---- src/update_walk.c | 508 ----------------------------------------------- src/update_walk.h | 39 ---- test/RunInotify.cxx | 95 +++++++++ test/run_inotify.c | 95 --------- 56 files changed, 2850 insertions(+), 2828 deletions(-) create mode 100644 src/InotifyQueue.cxx create mode 100644 src/InotifyQueue.hxx create mode 100644 src/InotifySource.cxx create mode 100644 src/InotifySource.hxx create mode 100644 src/InotifyUpdate.cxx create mode 100644 src/InotifyUpdate.hxx create mode 100644 src/UpdateArchive.cxx create mode 100644 src/UpdateArchive.hxx create mode 100644 src/UpdateContainer.cxx create mode 100644 src/UpdateContainer.hxx create mode 100644 src/UpdateDatabase.cxx create mode 100644 src/UpdateDatabase.hxx create mode 100644 src/UpdateGlue.cxx create mode 100644 src/UpdateGlue.hxx create mode 100644 src/UpdateIO.cxx create mode 100644 src/UpdateIO.hxx create mode 100644 src/UpdateInternal.hxx create mode 100644 src/UpdateQueue.cxx create mode 100644 src/UpdateQueue.hxx create mode 100644 src/UpdateRemove.cxx create mode 100644 src/UpdateRemove.hxx create mode 100644 src/UpdateSong.cxx create mode 100644 src/UpdateSong.hxx create mode 100644 src/UpdateWalk.cxx create mode 100644 src/UpdateWalk.hxx delete mode 100644 src/inotify_queue.c delete mode 100644 src/inotify_queue.h delete mode 100644 src/inotify_source.c delete mode 100644 src/inotify_source.h delete mode 100644 src/inotify_update.c delete mode 100644 src/inotify_update.h delete mode 100644 src/update.c delete mode 100644 src/update.h delete mode 100644 src/update_archive.c delete mode 100644 src/update_archive.h delete mode 100644 src/update_container.c delete mode 100644 src/update_container.h delete mode 100644 src/update_db.c delete mode 100644 src/update_db.h delete mode 100644 src/update_internal.h delete mode 100644 src/update_io.c delete mode 100644 src/update_io.h delete mode 100644 src/update_queue.c delete mode 100644 src/update_queue.h delete mode 100644 src/update_remove.c delete mode 100644 src/update_remove.h delete mode 100644 src/update_song.c delete mode 100644 src/update_song.h delete mode 100644 src/update_walk.c delete mode 100644 src/update_walk.h create mode 100644 test/RunInotify.cxx delete mode 100644 test/run_inotify.c diff --git a/Makefile.am b/Makefile.am index 4f6b82388..5bfabe752 100644 --- a/Makefile.am +++ b/Makefile.am @@ -91,10 +91,6 @@ mpd_headers = \ src/fd_util.h \ src/gerror.h \ src/glib_compat.h \ - src/update.h \ - src/inotify_source.h \ - src/inotify_queue.h \ - src/inotify_update.h \ src/gcc.h \ src/decoder_list.h \ src/decoder_print.h \ @@ -263,15 +259,15 @@ src_mpd_SOURCES = \ src/filter_config.c \ src/filter_plugin.c \ src/filter_registry.c \ - src/update.c \ - src/update_queue.c src/update_queue.h \ - src/update_io.c src/update_io.h \ - src/update_db.c src/update_db.h \ - src/update_walk.c src/update_walk.h \ - src/update_song.c src/update_song.h \ - src/update_container.c src/update_container.h \ - src/update_internal.h \ - src/update_remove.c src/update_remove.h \ + src/UpdateGlue.cxx src/UpdateGlue.hxx \ + src/UpdateQueue.cxx src/UpdateQueue.hxx \ + src/UpdateIO.cxx src/UpdateIO.hxx \ + src/UpdateDatabase.cxx src/UpdateDatabase.hxx \ + src/UpdateWalk.cxx src/UpdateWalk.hxx \ + src/UpdateSong.cxx src/UpdateSong.hxx \ + src/UpdateContainer.cxx src/UpdateContainer.hxx \ + src/UpdateInternal.hxx \ + src/UpdateRemove.cxx src/UpdateRemove.hxx \ src/client.c \ src/client_event.c \ src/client_expire.c \ @@ -374,9 +370,9 @@ endif if ENABLE_INOTIFY src_mpd_SOURCES += \ - src/inotify_source.c \ - src/inotify_queue.c \ - src/inotify_update.c + src/InotifySource.cxx src/InotifySource.hxx \ + src/InotifyQueue.cxx src/InotifyQueue.hxx \ + src/InotifyUpdate.cxx src/InotifyUpdate.hxx endif if ENABLE_SQLITE @@ -449,7 +445,7 @@ if ENABLE_ARCHIVE noinst_LIBRARIES += libarchive.a src_mpd_SOURCES += \ - src/update_archive.c src/update_archive.h + src/UpdateArchive.cxx src/UpdateArchive.hxx libarchive_a_SOURCES = \ src/archive_api.c \ @@ -1351,12 +1347,12 @@ TESTS += test/test_archive_iso9660.sh endif if ENABLE_INOTIFY -noinst_PROGRAMS += test/run_inotify -test_run_inotify_SOURCES = test/run_inotify.c \ +noinst_PROGRAMS += test/RunInotify +test_RunInotify_SOURCES = test/RunInotify.c \ src/fd_util.c \ src/fifo_buffer.c \ - src/inotify_source.c -test_run_inotify_LDADD = $(GLIB_LIBS) + src/InotifySource.cxx +test_RunInotify_LDADD = $(GLIB_LIBS) endif test_test_byte_reverse_SOURCES = \ diff --git a/src/InotifyQueue.cxx b/src/InotifyQueue.cxx new file mode 100644 index 000000000..572e53fc3 --- /dev/null +++ b/src/InotifyQueue.cxx @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "InotifyQueue.hxx" +#include "UpdateGlue.hxx" + +#include + +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "inotify" + +enum { + /** + * Wait this long after the last change before calling + * update_enqueue(). This increases the probability that + * updates can be bundled. + */ + INOTIFY_UPDATE_DELAY_S = 5, +}; + +static GSList *inotify_queue; +static guint queue_source_id; + +void +mpd_inotify_queue_init(void) +{ +} + +static void +free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data) +{ + g_free(data); +} + +void +mpd_inotify_queue_finish(void) +{ + if (queue_source_id != 0) + g_source_remove(queue_source_id); + + g_slist_foreach(inotify_queue, free_callback, NULL); + g_slist_free(inotify_queue); +} + +static gboolean +mpd_inotify_run_update(G_GNUC_UNUSED gpointer data) +{ + unsigned id; + + while (inotify_queue != NULL) { + char *uri_utf8 = (char *)inotify_queue->data; + + id = update_enqueue(uri_utf8, false); + if (id == 0) + /* retry later */ + return true; + + g_debug("updating '%s' job=%u", uri_utf8, id); + + g_free(uri_utf8); + inotify_queue = g_slist_delete_link(inotify_queue, + inotify_queue); + } + + /* done, remove the timer event by returning false */ + queue_source_id = 0; + return false; +} + +static bool +path_in(const char *path, const char *possible_parent) +{ + size_t length = strlen(possible_parent); + + return path[0] == 0 || + (memcmp(possible_parent, path, length) == 0 && + (path[length] == 0 || path[length] == '/')); +} + +void +mpd_inotify_enqueue(char *uri_utf8) +{ + GSList *old_queue = inotify_queue; + + if (queue_source_id != 0) + g_source_remove(queue_source_id); + queue_source_id = g_timeout_add_seconds(INOTIFY_UPDATE_DELAY_S, + mpd_inotify_run_update, NULL); + + inotify_queue = NULL; + while (old_queue != NULL) { + char *current_uri = (char *)old_queue->data; + + if (path_in(uri_utf8, current_uri)) { + /* already enqueued */ + g_free(uri_utf8); + inotify_queue = g_slist_concat(inotify_queue, + old_queue); + return; + } + + old_queue = g_slist_delete_link(old_queue, old_queue); + + if (path_in(current_uri, uri_utf8)) + /* existing path is a sub-path of the new + path; we can dequeue the existing path and + update the new path instead */ + g_free(current_uri); + else + /* move the existing path to the new queue */ + inotify_queue = g_slist_prepend(inotify_queue, + current_uri); + } + + inotify_queue = g_slist_prepend(inotify_queue, uri_utf8); +} diff --git a/src/InotifyQueue.hxx b/src/InotifyQueue.hxx new file mode 100644 index 000000000..0c4863ab5 --- /dev/null +++ b/src/InotifyQueue.hxx @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_INOTIFY_QUEUE_HXX +#define MPD_INOTIFY_QUEUE_HXX + +void +mpd_inotify_queue_init(void); + +void +mpd_inotify_queue_finish(void); + +void +mpd_inotify_enqueue(char *uri_utf8); + +#endif diff --git a/src/InotifySource.cxx b/src/InotifySource.cxx new file mode 100644 index 000000000..689468102 --- /dev/null +++ b/src/InotifySource.cxx @@ -0,0 +1,174 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "InotifySource.hxx" + +extern "C" { +#include "fifo_buffer.h" +} + +#include "fd_util.h" +#include "mpd_error.h" + +#include + +#include +#include +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "inotify" + +struct mpd_inotify_source { + int fd; + + GIOChannel *channel; + + /** + * The channel's source id in the GLib main loop. + */ + guint id; + + struct fifo_buffer *buffer; + + mpd_inotify_callback_t callback; + void *callback_ctx; +}; + +/** + * A GQuark for GError instances. + */ +static inline GQuark +mpd_inotify_quark(void) +{ + return g_quark_from_static_string("inotify"); +} + +static gboolean +mpd_inotify_in_event(G_GNUC_UNUSED GIOChannel *_source, + G_GNUC_UNUSED GIOCondition condition, + gpointer data) +{ + struct mpd_inotify_source *source = (struct mpd_inotify_source *)data; + void *dest; + size_t length; + ssize_t nbytes; + + dest = fifo_buffer_write(source->buffer, &length); + if (dest == NULL) + MPD_ERROR("buffer full"); + + nbytes = read(source->fd, dest, length); + if (nbytes < 0) + MPD_ERROR("failed to read from inotify: %s", + g_strerror(errno)); + if (nbytes == 0) + MPD_ERROR("end of file from inotify"); + + fifo_buffer_append(source->buffer, nbytes); + + while (true) { + const char *name; + + const struct inotify_event *event = + (const struct inotify_event *) + fifo_buffer_read(source->buffer, &length); + if (event == NULL || length < sizeof(*event) || + length < sizeof(*event) + event->len) + break; + + if (event->len > 0 && event->name[event->len - 1] == 0) + name = event->name; + else + name = NULL; + + source->callback(event->wd, event->mask, name, + source->callback_ctx); + fifo_buffer_consume(source->buffer, + sizeof(*event) + event->len); + } + + return true; +} + +struct mpd_inotify_source * +mpd_inotify_source_new(mpd_inotify_callback_t callback, void *callback_ctx, + GError **error_r) +{ + struct mpd_inotify_source *source = + g_new(struct mpd_inotify_source, 1); + + source->fd = inotify_init_cloexec(); + if (source->fd < 0) { + g_set_error(error_r, mpd_inotify_quark(), errno, + "inotify_init() has failed: %s", + g_strerror(errno)); + g_free(source); + return NULL; + } + + source->buffer = fifo_buffer_new(4096); + + source->channel = g_io_channel_unix_new(source->fd); + source->id = g_io_add_watch(source->channel, G_IO_IN, + mpd_inotify_in_event, source); + + source->callback = callback; + source->callback_ctx = callback_ctx; + + return source; +} + +void +mpd_inotify_source_free(struct mpd_inotify_source *source) +{ + g_source_remove(source->id); + g_io_channel_unref(source->channel); + fifo_buffer_free(source->buffer); + close(source->fd); + g_free(source); +} + +int +mpd_inotify_source_add(struct mpd_inotify_source *source, + const char *path_fs, unsigned mask, + GError **error_r) +{ + int wd = inotify_add_watch(source->fd, path_fs, mask); + if (wd < 0) + g_set_error(error_r, mpd_inotify_quark(), errno, + "inotify_add_watch() has failed: %s", + g_strerror(errno)); + + return wd; +} + +void +mpd_inotify_source_rm(struct mpd_inotify_source *source, unsigned wd) +{ + int ret = inotify_rm_watch(source->fd, wd); + if (ret < 0 && errno != EINVAL) + g_warning("inotify_rm_watch() has failed: %s", + g_strerror(errno)); + + /* EINVAL may happen here when the file has been deleted; the + kernel seems to auto-unregister deleted files */ +} diff --git a/src/InotifySource.hxx b/src/InotifySource.hxx new file mode 100644 index 000000000..cde2bc269 --- /dev/null +++ b/src/InotifySource.hxx @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_INOTIFY_SOURCE_HXX +#define MPD_INOTIFY_SOURCE_HXX + +#include "gerror.h" + +typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask, + const char *name, void *ctx); + +struct mpd_inotify_source; + +/** + * Creates a new inotify source and registers it in the GLib main + * loop. + * + * @param a callback invoked for events received from the kernel + */ +struct mpd_inotify_source * +mpd_inotify_source_new(mpd_inotify_callback_t callback, void *callback_ctx, + GError **error_r); + +void +mpd_inotify_source_free(struct mpd_inotify_source *source); + +/** + * Adds a path to the notify list. + * + * @return a watch descriptor or -1 on error + */ +int +mpd_inotify_source_add(struct mpd_inotify_source *source, + const char *path_fs, unsigned mask, + GError **error_r); + +/** + * Removes a path from the notify list. + * + * @param wd the watch descriptor returned by mpd_inotify_source_add() + */ +void +mpd_inotify_source_rm(struct mpd_inotify_source *source, unsigned wd); + +#endif diff --git a/src/InotifyUpdate.cxx b/src/InotifyUpdate.cxx new file mode 100644 index 000000000..e9350591b --- /dev/null +++ b/src/InotifyUpdate.cxx @@ -0,0 +1,378 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "InotifyUpdate.hxx" +#include "InotifySource.hxx" +#include "InotifyQueue.hxx" +#include "database.h" + +extern "C" { +#include "mapper.h" +#include "path.h" +} + +#include +#include +#include +#include +#include +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "inotify" + +enum { + IN_MASK = IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF + |IN_MOVE|IN_MOVE_SELF +#ifdef IN_ONLYDIR + |IN_ONLYDIR +#endif +}; + +struct watch_directory { + struct watch_directory *parent; + + char *name; + + int descriptor; + + GList *children; +}; + +static struct mpd_inotify_source *inotify_source; + +static unsigned inotify_max_depth; +static struct watch_directory inotify_root; +static GTree *inotify_directories; + +static gint +compare(gconstpointer a, gconstpointer b) +{ + if (a < b) + return -1; + else if (a > b) + return 1; + else + return 0; +} + +static void +tree_add_watch_directory(struct watch_directory *directory) +{ + g_tree_insert(inotify_directories, + GINT_TO_POINTER(directory->descriptor), directory); +} + +static void +tree_remove_watch_directory(struct watch_directory *directory) +{ + G_GNUC_UNUSED + bool found = g_tree_remove(inotify_directories, + GINT_TO_POINTER(directory->descriptor)); + assert(found); +} + +static struct watch_directory * +tree_find_watch_directory(int wd) +{ + return (struct watch_directory *) + g_tree_lookup(inotify_directories, GINT_TO_POINTER(wd)); +} + +static void +remove_watch_directory(struct watch_directory *directory) +{ + assert(directory != NULL); + + if (directory->parent == NULL) { + g_warning("music directory was removed - " + "cannot continue to watch it"); + return; + } + + assert(directory->parent->children != NULL); + + tree_remove_watch_directory(directory); + + while (directory->children != NULL) + remove_watch_directory((struct watch_directory *)directory->children->data); + + directory->parent->children = + g_list_remove(directory->parent->children, directory); + + mpd_inotify_source_rm(inotify_source, directory->descriptor); + g_free(directory->name); + g_slice_free(struct watch_directory, directory); +} + +static char * +watch_directory_get_uri_fs(const struct watch_directory *directory) +{ + char *parent_uri, *uri; + + if (directory->parent == NULL) + return NULL; + + parent_uri = watch_directory_get_uri_fs(directory->parent); + if (parent_uri == NULL) + return g_strdup(directory->name); + + uri = g_strconcat(parent_uri, "/", directory->name, NULL); + g_free(parent_uri); + + return uri; +} + +/* we don't look at "." / ".." nor files with newlines in their name */ +static bool skip_path(const char *path) +{ + return (path[0] == '.' && path[1] == 0) || + (path[0] == '.' && path[1] == '.' && path[2] == 0) || + strchr(path, '\n') != NULL; +} + +static void +recursive_watch_subdirectories(struct watch_directory *directory, + const char *path_fs, unsigned depth) +{ + GError *error = NULL; + DIR *dir; + struct dirent *ent; + + assert(directory != NULL); + assert(depth <= inotify_max_depth); + assert(path_fs != NULL); + + ++depth; + + if (depth > inotify_max_depth) + return; + + dir = opendir(path_fs); + if (dir == NULL) { + g_warning("Failed to open directory %s: %s", + path_fs, g_strerror(errno)); + return; + } + + while ((ent = readdir(dir))) { + char *child_path_fs; + struct stat st; + int ret; + struct watch_directory *child; + + if (skip_path(ent->d_name)) + continue; + + child_path_fs = g_strconcat(path_fs, "/", ent->d_name, NULL); + ret = stat(child_path_fs, &st); + if (ret < 0) { + g_warning("Failed to stat %s: %s", + child_path_fs, g_strerror(errno)); + g_free(child_path_fs); + continue; + } + + if (!S_ISDIR(st.st_mode)) { + g_free(child_path_fs); + continue; + } + + ret = mpd_inotify_source_add(inotify_source, child_path_fs, + IN_MASK, &error); + if (ret < 0) { + g_warning("Failed to register %s: %s", + child_path_fs, error->message); + g_error_free(error); + error = NULL; + g_free(child_path_fs); + continue; + } + + child = tree_find_watch_directory(ret); + if (child != NULL) { + /* already being watched */ + g_free(child_path_fs); + continue; + } + + child = g_slice_new(struct watch_directory); + child->parent = directory; + child->name = g_strdup(ent->d_name); + child->descriptor = ret; + child->children = NULL; + + directory->children = g_list_prepend(directory->children, + child); + + tree_add_watch_directory(child); + + recursive_watch_subdirectories(child, child_path_fs, depth); + g_free(child_path_fs); + } + + closedir(dir); +} + +G_GNUC_PURE +static unsigned +watch_directory_depth(const struct watch_directory *d) +{ + assert(d != NULL); + + unsigned depth = 0; + while ((d = d->parent) != NULL) + ++depth; + + return depth; +} + +static void +mpd_inotify_callback(int wd, unsigned mask, + G_GNUC_UNUSED const char *name, G_GNUC_UNUSED void *ctx) +{ + struct watch_directory *directory; + char *uri_fs; + + /*g_debug("wd=%d mask=0x%x name='%s'", wd, mask, name);*/ + + directory = tree_find_watch_directory(wd); + if (directory == NULL) + return; + + uri_fs = watch_directory_get_uri_fs(directory); + + if ((mask & (IN_DELETE_SELF|IN_MOVE_SELF)) != 0) { + g_free(uri_fs); + remove_watch_directory(directory); + return; + } + + if ((mask & (IN_ATTRIB|IN_CREATE|IN_MOVE)) != 0 && + (mask & IN_ISDIR) != 0) { + /* a sub directory was changed: register those in + inotify */ + const char *root = mapper_get_music_directory_fs(); + const char *path_fs; + char *allocated = NULL; + + if (uri_fs != NULL) + path_fs = allocated = + g_strconcat(root, "/", uri_fs, NULL); + else + path_fs = root; + + recursive_watch_subdirectories(directory, path_fs, + watch_directory_depth(directory)); + g_free(allocated); + } + + if ((mask & (IN_CLOSE_WRITE|IN_MOVE|IN_DELETE)) != 0 || + /* at the maximum depth, we watch out for newly created + directories */ + (watch_directory_depth(directory) == inotify_max_depth && + (mask & (IN_CREATE|IN_ISDIR)) == (IN_CREATE|IN_ISDIR))) { + /* a file was changed, or a directory was + moved/deleted: queue a database update */ + char *uri_utf8 = uri_fs != NULL + ? fs_charset_to_utf8(uri_fs) + : g_strdup(""); + + if (uri_utf8 != NULL) + /* this function will take care of freeing + uri_utf8 */ + mpd_inotify_enqueue(uri_utf8); + } + + g_free(uri_fs); +} + +void +mpd_inotify_init(unsigned max_depth) +{ + GError *error = NULL; + + g_debug("initializing inotify"); + + const char *path = mapper_get_music_directory_fs(); + if (path == NULL) { + g_debug("no music directory configured"); + return; + } + + inotify_source = mpd_inotify_source_new(mpd_inotify_callback, NULL, + &error); + if (inotify_source == NULL) { + g_warning("%s", error->message); + g_error_free(error); + return; + } + + inotify_max_depth = max_depth; + + inotify_root.name = g_strdup(path); + inotify_root.descriptor = mpd_inotify_source_add(inotify_source, path, + IN_MASK, &error); + if (inotify_root.descriptor < 0) { + g_warning("%s", error->message); + g_error_free(error); + mpd_inotify_source_free(inotify_source); + inotify_source = NULL; + return; + } + + inotify_directories = g_tree_new(compare); + tree_add_watch_directory(&inotify_root); + + recursive_watch_subdirectories(&inotify_root, path, 0); + + mpd_inotify_queue_init(); + + g_debug("watching music directory"); +} + +static gboolean +free_watch_directory(G_GNUC_UNUSED gpointer key, gpointer value, + G_GNUC_UNUSED gpointer data) +{ + struct watch_directory *directory = (struct watch_directory *)value; + + g_free(directory->name); + g_list_free(directory->children); + + if (directory != &inotify_root) + g_slice_free(struct watch_directory, directory); + + return false; +} + +void +mpd_inotify_finish(void) +{ + if (inotify_source == NULL) + return; + + mpd_inotify_queue_finish(); + mpd_inotify_source_free(inotify_source); + + g_tree_foreach(inotify_directories, free_watch_directory, NULL); + g_tree_destroy(inotify_directories); +} diff --git a/src/InotifyUpdate.hxx b/src/InotifyUpdate.hxx new file mode 100644 index 000000000..ceb421553 --- /dev/null +++ b/src/InotifyUpdate.hxx @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_INOTIFY_UPDATE_HXX +#define MPD_INOTIFY_UPDATE_HXX + +#include "check.h" + +#ifdef HAVE_INOTIFY_INIT + +void +mpd_inotify_init(unsigned max_depth); + +void +mpd_inotify_finish(void); + +#else /* !HAVE_INOTIFY_INIT */ + +static inline void +mpd_inotify_init(G_GNUC_UNUSED unsigned max_depth) +{ +} + +static inline void +mpd_inotify_finish(void) +{ +} + +#endif /* !HAVE_INOTIFY_INIT */ + +#endif diff --git a/src/Main.cxx b/src/Main.cxx index 057551391..44042d8ca 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2012 The Music Player Daemon Project + * Copyright (C) 2003-2013 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -20,6 +20,7 @@ #include "config.h" #include "Main.hxx" #include "PlaylistFile.hxx" +#include "UpdateGlue.hxx" #include "chunk.h" extern "C" { @@ -31,7 +32,6 @@ extern "C" { #include "AllCommands.h" #include "playlist.h" #include "database.h" -#include "update.h" #include "player_thread.h" #include "listen.h" #include "cmdline.h" @@ -59,12 +59,12 @@ extern "C" { #include "mpd_error.h" -extern "C" { - #ifdef ENABLE_INOTIFY -#include "inotify_update.h" +#include "InotifyUpdate.hxx" #endif +extern "C" { + #ifdef ENABLE_SQLITE #include "sticker.h" #endif diff --git a/src/OtherCommands.cxx b/src/OtherCommands.cxx index a703a2ffe..8518f437f 100644 --- a/src/OtherCommands.cxx +++ b/src/OtherCommands.cxx @@ -21,6 +21,7 @@ #include "OtherCommands.hxx" #include "DatabaseCommands.hxx" #include "CommandError.hxx" +#include "UpdateGlue.hxx" #include "directory.h" #include "song.h" @@ -31,7 +32,6 @@ extern "C" { #include "ls.h" #include "uri.h" #include "decoder_print.h" -#include "update.h" #include "volume.h" #include "stats.h" #include "permission.h" diff --git a/src/PlayerCommands.cxx b/src/PlayerCommands.cxx index 015ac9a15..3aa3f7e63 100644 --- a/src/PlayerCommands.cxx +++ b/src/PlayerCommands.cxx @@ -21,13 +21,13 @@ #include "PlayerCommands.hxx" #include "CommandError.hxx" #include "PlaylistPrint.hxx" +#include "UpdateGlue.hxx" extern "C" { #include "protocol/argparser.h" #include "protocol/result.h" #include "player_control.h" #include "playlist.h" -#include "update.h" #include "volume.h" #include "client.h" #include "client_internal.h" diff --git a/src/UpdateArchive.cxx b/src/UpdateArchive.cxx new file mode 100644 index 000000000..c57534ea4 --- /dev/null +++ b/src/UpdateArchive.cxx @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateArchive.hxx" +#include "UpdateInternal.hxx" +#include "db_lock.h" +#include "directory.h" +#include "song.h" + +extern "C" { +#include "mapper.h" +#include "archive_list.h" +#include "archive_plugin.h" +} + +#include + +#include + +static void +update_archive_tree(struct directory *directory, char *name) +{ + char *tmp = strchr(name, '/'); + if (tmp) { + *tmp = 0; + //add dir is not there already + db_lock(); + struct directory *subdir = + directory_make_child(directory, name); + subdir->device = DEVICE_INARCHIVE; + db_unlock(); + //create directories first + update_archive_tree(subdir, tmp+1); + } else { + if (strlen(name) == 0) { + g_warning("archive returned directory only"); + return; + } + + //add file + db_lock(); + struct song *song = directory_get_song(directory, name); + db_unlock(); + if (song == NULL) { + song = song_file_load(name, directory); + if (song != NULL) { + db_lock(); + directory_add_song(directory, song); + db_unlock(); + + modified = true; + g_message("added %s/%s", + directory_get_path(directory), name); + } + } + } +} + +/** + * Updates the file listing from an archive file. + * + * @param parent the parent directory the archive file resides in + * @param name the UTF-8 encoded base name of the archive file + * @param st stat() information on the archive file + * @param plugin the archive plugin which fits this archive type + */ +static void +update_archive_file2(struct directory *parent, const char *name, + const struct stat *st, + const struct archive_plugin *plugin) +{ + db_lock(); + struct directory *directory = directory_get_child(parent, name); + db_unlock(); + + if (directory != NULL && directory->mtime == st->st_mtime && + !walk_discard) + /* MPD has already scanned the archive, and it hasn't + changed since - don't consider updating it */ + return; + + char *path_fs = map_directory_child_fs(parent, name); + + /* open archive */ + GError *error = NULL; + struct archive_file *file = archive_file_open(plugin, path_fs, &error); + if (file == NULL) { + g_free(path_fs); + g_warning("%s", error->message); + g_error_free(error); + return; + } + + g_debug("archive %s opened", path_fs); + g_free(path_fs); + + if (directory == NULL) { + g_debug("creating archive directory: %s", name); + db_lock(); + directory = directory_new_child(parent, name); + /* mark this directory as archive (we use device for + this) */ + directory->device = DEVICE_INARCHIVE; + db_unlock(); + } + + directory->mtime = st->st_mtime; + + archive_file_scan_reset(file); + + char *filepath; + while ((filepath = archive_file_scan_next(file)) != NULL) { + /* split name into directory and file */ + g_debug("adding archive file: %s", filepath); + update_archive_tree(directory, filepath); + } + + archive_file_close(file); +} + +bool +update_archive_file(struct directory *directory, + const char *name, const char *suffix, + const struct stat *st) +{ +#ifdef ENABLE_ARCHIVE + const struct archive_plugin *plugin = + archive_plugin_from_suffix(suffix); + if (plugin == NULL) + return false; + + update_archive_file2(directory, name, st, plugin); + return true; +#else + (void)directory; + (void)name; + (void)suffix; + (void)st; + + return false; +#endif +} diff --git a/src/UpdateArchive.hxx b/src/UpdateArchive.hxx new file mode 100644 index 000000000..533f8ffd0 --- /dev/null +++ b/src/UpdateArchive.hxx @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_ARCHIVE_HXX +#define MPD_UPDATE_ARCHIVE_HXX + +#include "check.h" +#include "gcc.h" + +#include +#include + +struct directory; +struct archive_plugin; + +#ifdef ENABLE_ARCHIVE + +bool +update_archive_file(struct directory *directory, + const char *name, const char *suffix, + const struct stat *st); + +#else + +#include + +static inline bool +update_archive_file(gcc_unused struct directory *directory, + gcc_unused const char *name, + gcc_unused const char *suffix, + gcc_unused const struct stat *st) +{ + return false; +} + +#endif + +#endif diff --git a/src/UpdateContainer.cxx b/src/UpdateContainer.cxx new file mode 100644 index 000000000..68f8c3b98 --- /dev/null +++ b/src/UpdateContainer.cxx @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateContainer.hxx" +#include "UpdateInternal.hxx" +#include "UpdateDatabase.hxx" +#include "db_lock.h" +#include "directory.h" +#include "song.h" +#include "decoder_plugin.h" + +extern "C" { +#include "mapper.h" +#include "tag.h" +#include "tag_handler.h" +} + +#include + +/** + * Create the specified directory object if it does not exist already + * or if the #stat object indicates that it has been modified since + * the last update. Returns NULL when it exists already and is + * unmodified. + * + * The caller must lock the database. + */ +static struct directory * +make_directory_if_modified(struct directory *parent, const char *name, + const struct stat *st) +{ + struct directory *directory = directory_get_child(parent, name); + + // directory exists already + if (directory != NULL) { + if (directory->mtime == st->st_mtime && !walk_discard) { + /* not modified */ + db_unlock(); + return NULL; + } + + delete_directory(directory); + modified = true; + } + + directory = directory_make_child(parent, name); + directory->mtime = st->st_mtime; + return directory; +} + +bool +update_container_file(struct directory *directory, + const char *name, + const struct stat *st, + const struct decoder_plugin *plugin) +{ + if (plugin->container_scan == NULL) + return false; + + db_lock(); + struct directory *contdir = + make_directory_if_modified(directory, name, st); + if (contdir == NULL) { + /* not modified */ + db_unlock(); + return true; + } + + contdir->device = DEVICE_CONTAINER; + db_unlock(); + + char *const pathname = map_directory_child_fs(directory, name); + + char *vtrack; + unsigned int tnum = 0; + while ((vtrack = plugin->container_scan(pathname, ++tnum)) != NULL) { + struct song *song = song_file_new(vtrack, contdir); + + // shouldn't be necessary but it's there.. + song->mtime = st->st_mtime; + + char *child_path_fs = map_directory_child_fs(contdir, vtrack); + + song->tag = tag_new(); + decoder_plugin_scan_file(plugin, child_path_fs, + &add_tag_handler, song->tag); + g_free(child_path_fs); + + db_lock(); + directory_add_song(contdir, song); + db_unlock(); + + modified = true; + + g_message("added %s/%s", + directory_get_path(directory), vtrack); + g_free(vtrack); + } + + g_free(pathname); + + if (tnum == 1) { + db_lock(); + delete_directory(contdir); + db_unlock(); + return false; + } else + return true; +} diff --git a/src/UpdateContainer.hxx b/src/UpdateContainer.hxx new file mode 100644 index 000000000..81f081a59 --- /dev/null +++ b/src/UpdateContainer.hxx @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_CONTAINER_HXX +#define MPD_UPDATE_CONTAINER_HXX + +#include "check.h" + +#include + +struct directory; +struct decoder_plugin; + +bool +update_container_file(struct directory *directory, + const char *name, + const struct stat *st, + const struct decoder_plugin *plugin); + +#endif diff --git a/src/UpdateDatabase.cxx b/src/UpdateDatabase.cxx new file mode 100644 index 000000000..fa3408d66 --- /dev/null +++ b/src/UpdateDatabase.cxx @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2003-2012 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateDatabase.hxx" +#include "UpdateRemove.hxx" +#include "directory.h" +#include "song.h" +#include "db_lock.h" + +extern "C" { +#include "playlist_vector.h" +} + +#include +#include + +void +delete_song(struct directory *dir, struct song *del) +{ + assert(del->parent == dir); + + /* first, prevent traversers in main task from getting this */ + directory_remove_song(dir, del); + + db_unlock(); /* temporary unlock, because update_remove_song() blocks */ + + /* now take it out of the playlist (in the main_task) */ + update_remove_song(del); + + /* finally, all possible references gone, free it */ + song_free(del); + + db_lock(); +} + +/** + * Recursively remove all sub directories and songs from a directory, + * leaving an empty directory. + * + * Caller must lock the #db_mutex. + */ +static void +clear_directory(struct directory *directory) +{ + struct directory *child, *n; + directory_for_each_child_safe(child, n, directory) + delete_directory(child); + + struct song *song, *ns; + directory_for_each_song_safe(song, ns, directory) { + assert(song->parent == directory); + delete_song(directory, song); + } +} + +void +delete_directory(struct directory *directory) +{ + assert(directory->parent != NULL); + + clear_directory(directory); + + directory_delete(directory); +} + +bool +delete_name_in(struct directory *parent, const char *name) +{ + bool modified = false; + + db_lock(); + struct directory *directory = directory_get_child(parent, name); + + if (directory != NULL) { + delete_directory(directory); + modified = true; + } + + struct song *song = directory_get_song(parent, name); + if (song != NULL) { + delete_song(parent, song); + modified = true; + } + + playlist_vector_remove(&parent->playlists, name); + + db_unlock(); + + return modified; +} diff --git a/src/UpdateDatabase.hxx b/src/UpdateDatabase.hxx new file mode 100644 index 000000000..511f75690 --- /dev/null +++ b/src/UpdateDatabase.hxx @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_DATABASE_HXX +#define MPD_UPDATE_DATABASE_HXX + +#include "check.h" + +struct directory; +struct song; + +/** + * Caller must lock the #db_mutex. + */ +void +delete_song(struct directory *parent, struct song *song); + +/** + * Recursively free a directory and all its contents. + * + * Caller must lock the #db_mutex. + */ +void +delete_directory(struct directory *directory); + +/** + * Caller must NOT lock the #db_mutex. + * + * @return true if the database was modified + */ +bool +delete_name_in(struct directory *parent, const char *name); + +#endif diff --git a/src/UpdateGlue.cxx b/src/UpdateGlue.cxx new file mode 100644 index 000000000..203f16b76 --- /dev/null +++ b/src/UpdateGlue.cxx @@ -0,0 +1,186 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "UpdateGlue.hxx" +#include "UpdateQueue.hxx" +#include "UpdateWalk.hxx" +#include "UpdateRemove.hxx" + +extern "C" { +#include "database.h" +#include "mapper.h" +#include "playlist.h" +#include "event_pipe.h" +#include "idle.h" +#include "stats.h" +} + +#include "Main.hxx" +#include "mpd_error.h" + +#include + +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "update" + +static enum update_progress { + UPDATE_PROGRESS_IDLE = 0, + UPDATE_PROGRESS_RUNNING = 1, + UPDATE_PROGRESS_DONE = 2 +} progress; + +static bool modified; + +static GThread *update_thr; + +static const unsigned update_task_id_max = 1 << 15; + +static unsigned update_task_id; + +/* XXX this flag is passed to update_task() */ +static bool discard; + +unsigned +isUpdatingDB(void) +{ + return (progress != UPDATE_PROGRESS_IDLE) ? update_task_id : 0; +} + +static void * update_task(void *_path) +{ + const char *path = (const char *)_path; + + if (path != NULL && *path != 0) + g_debug("starting: %s", path); + else + g_debug("starting"); + + modified = update_walk(path, discard); + + if (modified || !db_exists()) { + GError *error = NULL; + if (!db_save(&error)) { + g_warning("Failed to save database: %s", + error->message); + g_error_free(error); + } + } + + if (path != NULL && *path != 0) + g_debug("finished: %s", path); + else + g_debug("finished"); + g_free(_path); + + progress = UPDATE_PROGRESS_DONE; + event_pipe_emit(PIPE_EVENT_UPDATE); + return NULL; +} + +static void +spawn_update_task(const char *path) +{ + GError *e = NULL; + + assert(g_thread_self() == main_task); + + progress = UPDATE_PROGRESS_RUNNING; + modified = false; + + update_thr = g_thread_create(update_task, g_strdup(path), TRUE, &e); + if (update_thr == NULL) + MPD_ERROR("Failed to spawn update task: %s", e->message); + + if (++update_task_id > update_task_id_max) + update_task_id = 1; + g_debug("spawned thread for update job id %i", update_task_id); +} + +unsigned +update_enqueue(const char *path, bool _discard) +{ + assert(g_thread_self() == main_task); + + if (!db_is_simple() || !mapper_has_music_directory()) + return 0; + + if (progress != UPDATE_PROGRESS_IDLE) { + unsigned next_task_id = + update_queue_push(path, discard, update_task_id); + if (next_task_id == 0) + return 0; + + return next_task_id > update_task_id_max ? 1 : next_task_id; + } + + discard = _discard; + spawn_update_task(path); + + idle_add(IDLE_UPDATE); + + return update_task_id; +} + +/** + * Called in the main thread after the database update is finished. + */ +static void update_finished_event(void) +{ + char *path; + + assert(progress == UPDATE_PROGRESS_DONE); + + g_thread_join(update_thr); + + idle_add(IDLE_UPDATE); + + if (modified) { + /* send "idle" events */ + playlist_increment_version_all(&g_playlist); + idle_add(IDLE_DATABASE); + } + + path = update_queue_shift(&discard); + if (path != NULL) { + /* schedule the next path */ + spawn_update_task(path); + g_free(path); + } else { + progress = UPDATE_PROGRESS_IDLE; + + stats_update(); + } +} + +void update_global_init(void) +{ + event_pipe_register(PIPE_EVENT_UPDATE, update_finished_event); + + update_remove_global_init(); + update_walk_global_init(); +} + +void update_global_finish(void) +{ + update_walk_global_finish(); + update_remove_global_finish(); +} diff --git a/src/UpdateGlue.hxx b/src/UpdateGlue.hxx new file mode 100644 index 000000000..9d546a2a3 --- /dev/null +++ b/src/UpdateGlue.hxx @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_GLUE_HXX +#define MPD_UPDATE_GLUE_HXX + +void update_global_init(void); + +void update_global_finish(void); + +unsigned +isUpdatingDB(void); + +/** + * Add this path to the database update queue. + * + * @param path a path to update; if NULL or an empty string, + * the whole music directory is updated + * @return the job id, or 0 on error + */ +unsigned +update_enqueue(const char *path, bool discard); + +#endif diff --git a/src/UpdateIO.cxx b/src/UpdateIO.cxx new file mode 100644 index 000000000..fefbc4d11 --- /dev/null +++ b/src/UpdateIO.cxx @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2003-2012 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateIO.hxx" +#include "directory.h" +#include "glib_compat.h" + +extern "C" { +#include "mapper.h" +} + +#include + +#include +#include + +int +stat_directory(const struct directory *directory, struct stat *st) +{ + char *path_fs = map_directory_fs(directory); + if (path_fs == NULL) + return -1; + + int ret = stat(path_fs, st); + if (ret < 0) + g_warning("Failed to stat %s: %s", path_fs, g_strerror(errno)); + + g_free(path_fs); + return ret; +} + +int +stat_directory_child(const struct directory *parent, const char *name, + struct stat *st) +{ + char *path_fs = map_directory_child_fs(parent, name); + if (path_fs == NULL) + return -1; + + int ret = stat(path_fs, st); + if (ret < 0) + g_warning("Failed to stat %s: %s", path_fs, g_strerror(errno)); + + g_free(path_fs); + return ret; +} + +bool +directory_exists(const struct directory *directory) +{ + char *path_fs = map_directory_fs(directory); + if (path_fs == NULL) + /* invalid path: cannot exist */ + return false; + + GFileTest test = directory->device == DEVICE_INARCHIVE || + directory->device == DEVICE_CONTAINER + ? G_FILE_TEST_IS_REGULAR + : G_FILE_TEST_IS_DIR; + + bool exists = g_file_test(path_fs, test); + g_free(path_fs); + + return exists; +} + +bool +directory_child_is_regular(const struct directory *directory, + const char *name_utf8) +{ + char *path_fs = map_directory_child_fs(directory, name_utf8); + if (path_fs == NULL) + return false; + + struct stat st; + bool is_regular = stat(path_fs, &st) == 0 && S_ISREG(st.st_mode); + g_free(path_fs); + + return is_regular; +} + +bool +directory_child_access(const struct directory *directory, + const char *name, int mode) +{ +#ifdef WIN32 + /* access() is useless on WIN32 */ + (void)directory; + (void)name; + (void)mode; + return true; +#else + char *path = map_directory_child_fs(directory, name); + if (path == NULL) + /* something went wrong, but that isn't a permission + problem */ + return true; + + bool success = access(path, mode) == 0 || errno != EACCES; + g_free(path); + return success; +#endif +} diff --git a/src/UpdateIO.hxx b/src/UpdateIO.hxx new file mode 100644 index 000000000..7b6175fdf --- /dev/null +++ b/src/UpdateIO.hxx @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_IO_HXX +#define MPD_UPDATE_IO_HXX + +#include "check.h" + +#include + +struct directory; + +int +stat_directory(const struct directory *directory, struct stat *st); + +int +stat_directory_child(const struct directory *parent, const char *name, + struct stat *st); + +bool +directory_exists(const struct directory *directory); + +bool +directory_child_is_regular(const struct directory *directory, + const char *name_utf8); + +/** + * Checks if the given permissions on the mapped file are given. + */ +bool +directory_child_access(const struct directory *directory, + const char *name, int mode); + +#endif diff --git a/src/UpdateInternal.hxx b/src/UpdateInternal.hxx new file mode 100644 index 000000000..76ab7bed3 --- /dev/null +++ b/src/UpdateInternal.hxx @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_INTERNAL_H +#define MPD_UPDATE_INTERNAL_H + +#include "check.h" + +#include + +extern bool walk_discard; +extern bool modified; + +#endif diff --git a/src/UpdateQueue.cxx b/src/UpdateQueue.cxx new file mode 100644 index 000000000..85eb22358 --- /dev/null +++ b/src/UpdateQueue.cxx @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "UpdateQueue.hxx" + +#include + +#include +#include + +/* make this dynamic?, or maybe this is big enough... */ +static struct { + char *path; + bool discard; +} update_queue[32]; + +static size_t update_queue_length; + +unsigned +update_queue_push(const char *path, bool discard, unsigned base) +{ + assert(update_queue_length <= G_N_ELEMENTS(update_queue)); + + if (update_queue_length == G_N_ELEMENTS(update_queue)) + return 0; + + update_queue[update_queue_length].path = g_strdup(path); + update_queue[update_queue_length].discard = discard; + + ++update_queue_length; + + return base + update_queue_length; +} + +char * +update_queue_shift(bool *discard_r) +{ + char *path; + + if (update_queue_length == 0) + return NULL; + + path = update_queue[0].path; + *discard_r = update_queue[0].discard; + + memmove(&update_queue[0], &update_queue[1], + --update_queue_length * sizeof(update_queue[0])); + return path; +} diff --git a/src/UpdateQueue.hxx b/src/UpdateQueue.hxx new file mode 100644 index 000000000..7de06964f --- /dev/null +++ b/src/UpdateQueue.hxx @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_QUEUE_HXX +#define MPD_UPDATE_QUEUE_HXX + +#include "check.h" + +unsigned +update_queue_push(const char *path, bool discard, unsigned base); + +char * +update_queue_shift(bool *discard_r); + +#endif diff --git a/src/UpdateRemove.cxx b/src/UpdateRemove.cxx new file mode 100644 index 000000000..71852ea96 --- /dev/null +++ b/src/UpdateRemove.cxx @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateRemove.hxx" + +extern "C" { +#include "event_pipe.h" +#include "playlist.h" +} + +#include "song.h" +#include "Main.hxx" + +#ifdef ENABLE_SQLITE +extern "C" { +#include "sticker.h" +#include "song_sticker.h" +} +#endif + +#include + +#include + +static const struct song *removed_song; + +static GMutex *remove_mutex; +static GCond *remove_cond; + +/** + * Safely remove a song from the database. This must be done in the + * main task, to be sure that there is no pointer left to it. + */ +static void +song_remove_event(void) +{ + char *uri; + + assert(removed_song != NULL); + + uri = song_get_uri(removed_song); + g_message("removing %s", uri); + g_free(uri); + +#ifdef ENABLE_SQLITE + /* if the song has a sticker, remove it */ + if (sticker_enabled()) + sticker_song_delete(removed_song); +#endif + + playlist_delete_song(&g_playlist, global_player_control, removed_song); + + /* clear "removed_song" and send signal to update thread */ + g_mutex_lock(remove_mutex); + removed_song = NULL; + g_cond_signal(remove_cond); + g_mutex_unlock(remove_mutex); +} + +void +update_remove_global_init(void) +{ + remove_mutex = g_mutex_new(); + remove_cond = g_cond_new(); + + event_pipe_register(PIPE_EVENT_DELETE, song_remove_event); +} + +void +update_remove_global_finish(void) +{ + g_mutex_free(remove_mutex); + g_cond_free(remove_cond); +} + +void +update_remove_song(const struct song *song) +{ + assert(removed_song == NULL); + + removed_song = song; + + event_pipe_emit(PIPE_EVENT_DELETE); + + g_mutex_lock(remove_mutex); + + while (removed_song != NULL) + g_cond_wait(remove_cond, remove_mutex); + + g_mutex_unlock(remove_mutex); +} diff --git a/src/UpdateRemove.hxx b/src/UpdateRemove.hxx new file mode 100644 index 000000000..dc15622fc --- /dev/null +++ b/src/UpdateRemove.hxx @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_REMOVE_HXX +#define MPD_UPDATE_REMOVE_HXX + +#include "check.h" + +struct song; + +void +update_remove_global_init(void); + +void +update_remove_global_finish(void); + +/** + * Sends a signal to the main thread which will in turn remove the + * song: from the sticker database and from the playlist. This + * serialized access is implemented to avoid excessive locking. + */ +void +update_remove_song(const struct song *song); + +#endif diff --git a/src/UpdateSong.cxx b/src/UpdateSong.cxx new file mode 100644 index 000000000..8241d46f2 --- /dev/null +++ b/src/UpdateSong.cxx @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2003-2012 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateSong.hxx" +#include "UpdateInternal.hxx" +#include "UpdateIO.hxx" +#include "UpdateDatabase.hxx" +#include "UpdateContainer.hxx" +#include "db_lock.h" +#include "directory.h" +#include "song.h" +#include "decoder_plugin.h" + +extern "C" { +#include "decoder_list.h" +} + +#include + +#include + +static void +update_song_file2(struct directory *directory, + const char *name, const struct stat *st, + const struct decoder_plugin *plugin) +{ + db_lock(); + struct song *song = directory_get_song(directory, name); + db_unlock(); + + if (!directory_child_access(directory, name, R_OK)) { + g_warning("no read permissions on %s/%s", + directory_get_path(directory), name); + if (song != NULL) { + db_lock(); + delete_song(directory, song); + db_unlock(); + } + + return; + } + + if (!(song != NULL && st->st_mtime == song->mtime && + !walk_discard) && + update_container_file(directory, name, st, plugin)) { + if (song != NULL) { + db_lock(); + delete_song(directory, song); + db_unlock(); + } + + return; + } + + if (song == NULL) { + g_debug("reading %s/%s", + directory_get_path(directory), name); + song = song_file_load(name, directory); + if (song == NULL) { + g_debug("ignoring unrecognized file %s/%s", + directory_get_path(directory), name); + return; + } + + db_lock(); + directory_add_song(directory, song); + db_unlock(); + + modified = true; + g_message("added %s/%s", + directory_get_path(directory), name); + } else if (st->st_mtime != song->mtime || walk_discard) { + g_message("updating %s/%s", + directory_get_path(directory), name); + if (!song_file_update(song)) { + g_debug("deleting unrecognized file %s/%s", + directory_get_path(directory), name); + db_lock(); + delete_song(directory, song); + db_unlock(); + } + + modified = true; + } +} + +bool +update_song_file(struct directory *directory, + const char *name, const char *suffix, + const struct stat *st) +{ + const struct decoder_plugin *plugin = + decoder_plugin_from_suffix(suffix, nullptr); + if (plugin == NULL) + return false; + + update_song_file2(directory, name, st, plugin); + return true; +} diff --git a/src/UpdateSong.hxx b/src/UpdateSong.hxx new file mode 100644 index 000000000..abf25aae9 --- /dev/null +++ b/src/UpdateSong.hxx @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_SONG_HXX +#define MPD_UPDATE_SONG_HXX + +#include "check.h" + +#include + +struct directory; + +bool +update_song_file(struct directory *directory, + const char *name, const char *suffix, + const struct stat *st); + +#endif diff --git a/src/UpdateWalk.cxx b/src/UpdateWalk.cxx new file mode 100644 index 000000000..a8ca1e4ed --- /dev/null +++ b/src/UpdateWalk.cxx @@ -0,0 +1,511 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateWalk.hxx" +#include "UpdateIO.hxx" +#include "UpdateDatabase.hxx" +#include "UpdateSong.hxx" +#include "UpdateArchive.hxx" +#include "db_lock.h" +#include "directory.h" +#include "song.h" + +extern "C" { +#include "exclude.h" +#include "database.h" +#include "playlist_vector.h" +#include "uri.h" +#include "mapper.h" +#include "path.h" +#include "playlist_list.h" +#include "conf.h" +} + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "update" + +bool walk_discard; +bool modified; + +#ifndef WIN32 + +enum { + DEFAULT_FOLLOW_INSIDE_SYMLINKS = true, + DEFAULT_FOLLOW_OUTSIDE_SYMLINKS = true, +}; + +static bool follow_inside_symlinks; +static bool follow_outside_symlinks; + +#endif + +void +update_walk_global_init(void) +{ +#ifndef WIN32 + follow_inside_symlinks = + config_get_bool(CONF_FOLLOW_INSIDE_SYMLINKS, + DEFAULT_FOLLOW_INSIDE_SYMLINKS); + + follow_outside_symlinks = + config_get_bool(CONF_FOLLOW_OUTSIDE_SYMLINKS, + DEFAULT_FOLLOW_OUTSIDE_SYMLINKS); +#endif +} + +void +update_walk_global_finish(void) +{ +} + +static void +directory_set_stat(struct directory *dir, const struct stat *st) +{ + dir->inode = st->st_ino; + dir->device = st->st_dev; + dir->have_stat = true; +} + +static void +remove_excluded_from_directory(struct directory *directory, + GSList *exclude_list) +{ + db_lock(); + + struct directory *child, *n; + directory_for_each_child_safe(child, n, directory) { + char *name_fs = utf8_to_fs_charset(directory_get_name(child)); + + if (exclude_list_check(exclude_list, name_fs)) { + delete_directory(child); + modified = true; + } + + g_free(name_fs); + } + + struct song *song, *ns; + directory_for_each_song_safe(song, ns, directory) { + assert(song->parent == directory); + + char *name_fs = utf8_to_fs_charset(song->uri); + if (exclude_list_check(exclude_list, name_fs)) { + delete_song(directory, song); + modified = true; + } + + g_free(name_fs); + } + + db_unlock(); +} + +static void +purge_deleted_from_directory(struct directory *directory) +{ + struct directory *child, *n; + directory_for_each_child_safe(child, n, directory) { + if (directory_exists(child)) + continue; + + db_lock(); + delete_directory(child); + db_unlock(); + + modified = true; + } + + struct song *song, *ns; + directory_for_each_song_safe(song, ns, directory) { + char *path; + struct stat st; + if ((path = map_song_fs(song)) == NULL || + stat(path, &st) < 0 || !S_ISREG(st.st_mode)) { + db_lock(); + delete_song(directory, song); + db_unlock(); + + modified = true; + } + + g_free(path); + } + + struct playlist_metadata *pm, *np; + directory_for_each_playlist_safe(pm, np, directory) { + if (!directory_child_is_regular(directory, pm->name)) { + db_lock(); + playlist_vector_remove(&directory->playlists, pm->name); + db_unlock(); + } + } +} + +#ifndef G_OS_WIN32 +static int +update_directory_stat(struct directory *directory) +{ + struct stat st; + if (stat_directory(directory, &st) < 0) + return -1; + + directory_set_stat(directory, &st); + return 0; +} +#endif + +static int +find_inode_ancestor(struct directory *parent, ino_t inode, dev_t device) +{ +#ifndef G_OS_WIN32 + while (parent) { + if (!parent->have_stat && update_directory_stat(parent) < 0) + return -1; + + if (parent->inode == inode && parent->device == device) { + g_debug("recursive directory found"); + return 1; + } + + parent = parent->parent; + } +#else + (void)parent; + (void)inode; + (void)device; +#endif + + return 0; +} + +static bool +update_playlist_file2(struct directory *directory, + const char *name, const char *suffix, + const struct stat *st) +{ + if (!playlist_suffix_supported(suffix)) + return false; + + db_lock(); + if (playlist_vector_update_or_add(&directory->playlists, name, + st->st_mtime)) + modified = true; + db_unlock(); + return true; +} + +static bool +update_regular_file(struct directory *directory, + const char *name, const struct stat *st) +{ + const char *suffix = uri_get_suffix(name); + if (suffix == NULL) + return false; + + return update_song_file(directory, name, suffix, st) || + update_archive_file(directory, name, suffix, st) || + update_playlist_file2(directory, name, suffix, st); +} + +static bool +update_directory(struct directory *directory, const struct stat *st); + +static void +update_directory_child(struct directory *directory, + const char *name, const struct stat *st) +{ + assert(strchr(name, '/') == NULL); + + if (S_ISREG(st->st_mode)) { + update_regular_file(directory, name, st); + } else if (S_ISDIR(st->st_mode)) { + if (find_inode_ancestor(directory, st->st_ino, st->st_dev)) + return; + + db_lock(); + struct directory *subdir = + directory_make_child(directory, name); + db_unlock(); + + assert(directory == subdir->parent); + + if (!update_directory(subdir, st)) { + db_lock(); + delete_directory(subdir); + db_unlock(); + } + } else { + g_debug("update: %s is not a directory, archive or music", name); + } +} + +/* we don't look at "." / ".." nor files with newlines in their name */ +G_GNUC_PURE +static bool skip_path(const char *path) +{ + return (path[0] == '.' && path[1] == 0) || + (path[0] == '.' && path[1] == '.' && path[2] == 0) || + strchr(path, '\n') != NULL; +} + +G_GNUC_PURE +static bool +skip_symlink(const struct directory *directory, const char *utf8_name) +{ +#ifndef WIN32 + char *path_fs = map_directory_child_fs(directory, utf8_name); + if (path_fs == NULL) + return true; + + char buffer[MPD_PATH_MAX]; + ssize_t length = readlink(path_fs, buffer, sizeof(buffer)); + g_free(path_fs); + if (length < 0) + /* don't skip if this is not a symlink */ + return errno != EINVAL; + + if ((size_t)length >= sizeof(buffer)) + /* skip symlinks when the buffer is too small for the + link target */ + return true; + + /* null-terminate the buffer, because readlink() will not */ + buffer[length] = 0; + + if (!follow_inside_symlinks && !follow_outside_symlinks) { + /* ignore all symlinks */ + return true; + } else if (follow_inside_symlinks && follow_outside_symlinks) { + /* consider all symlinks */ + return false; + } + + if (g_path_is_absolute(buffer)) { + /* if the symlink points to an absolute path, see if + that path is inside the music directory */ + const char *relative = map_to_relative_path(buffer); + return relative > buffer + ? !follow_inside_symlinks + : !follow_outside_symlinks; + } + + const char *p = buffer; + while (*p == '.') { + if (p[1] == '.' && G_IS_DIR_SEPARATOR(p[2])) { + /* "../" moves to parent directory */ + directory = directory->parent; + if (directory == NULL) { + /* we have moved outside the music + directory - skip this symlink + if such symlinks are not allowed */ + return !follow_outside_symlinks; + } + p += 3; + } else if (G_IS_DIR_SEPARATOR(p[1])) + /* eliminate "./" */ + p += 2; + else + break; + } + + /* we are still in the music directory, so this symlink points + to a song which is already in the database - skip according + to the follow_inside_symlinks param*/ + return !follow_inside_symlinks; +#else + /* no symlink checking on WIN32 */ + + (void)directory; + (void)utf8_name; + + return false; +#endif +} + +static bool +update_directory(struct directory *directory, const struct stat *st) +{ + assert(S_ISDIR(st->st_mode)); + + directory_set_stat(directory, st); + + char *path_fs = map_directory_fs(directory); + if (path_fs == NULL) + return false; + + DIR *dir = opendir(path_fs); + if (!dir) { + g_warning("Failed to open directory %s: %s", + path_fs, g_strerror(errno)); + g_free(path_fs); + return false; + } + + char *exclude_path_fs = g_build_filename(path_fs, ".mpdignore", NULL); + GSList *exclude_list = exclude_list_load(exclude_path_fs); + g_free(exclude_path_fs); + + g_free(path_fs); + + if (exclude_list != NULL) + remove_excluded_from_directory(directory, exclude_list); + + purge_deleted_from_directory(directory); + + struct dirent *ent; + while ((ent = readdir(dir))) { + char *utf8; + struct stat st2; + + if (skip_path(ent->d_name) || + exclude_list_check(exclude_list, ent->d_name)) + continue; + + utf8 = fs_charset_to_utf8(ent->d_name); + if (utf8 == NULL) + continue; + + if (skip_symlink(directory, utf8)) { + modified |= delete_name_in(directory, utf8); + g_free(utf8); + continue; + } + + if (stat_directory_child(directory, utf8, &st2) == 0) + update_directory_child(directory, utf8, &st2); + else + modified |= delete_name_in(directory, utf8); + + g_free(utf8); + } + + exclude_list_free(exclude_list); + + closedir(dir); + + directory->mtime = st->st_mtime; + + return true; +} + +static struct directory * +directory_make_child_checked(struct directory *parent, const char *name_utf8) +{ + db_lock(); + struct directory *directory = directory_get_child(parent, name_utf8); + db_unlock(); + + if (directory != NULL) + return directory; + + struct stat st; + if (stat_directory_child(parent, name_utf8, &st) < 0 || + find_inode_ancestor(parent, st.st_ino, st.st_dev)) + return NULL; + + if (skip_symlink(parent, name_utf8)) + return NULL; + + /* if we're adding directory paths, make sure to delete filenames + with potentially the same name */ + db_lock(); + struct song *conflicting = directory_get_song(parent, name_utf8); + if (conflicting) + delete_song(parent, conflicting); + + directory = directory_new_child(parent, name_utf8); + db_unlock(); + + directory_set_stat(directory, &st); + return directory; +} + +static struct directory * +directory_make_uri_parent_checked(const char *uri) +{ + struct directory *directory = db_get_root(); + char *duplicated = g_strdup(uri); + char *name_utf8 = duplicated, *slash; + + while ((slash = strchr(name_utf8, '/')) != NULL) { + *slash = 0; + + if (*name_utf8 == 0) + continue; + + directory = directory_make_child_checked(directory, name_utf8); + if (directory == NULL) + break; + + name_utf8 = slash + 1; + } + + g_free(duplicated); + return directory; +} + +static void +update_uri(const char *uri) +{ + struct directory *parent = directory_make_uri_parent_checked(uri); + if (parent == NULL) + return; + + char *name = g_path_get_basename(uri); + + struct stat st; + if (!skip_symlink(parent, name) && + stat_directory_child(parent, name, &st) == 0) + update_directory_child(parent, name, &st); + else + modified |= delete_name_in(parent, name); + + g_free(name); +} + +bool +update_walk(const char *path, bool discard) +{ + walk_discard = discard; + modified = false; + + if (path != NULL && !isRootDirectory(path)) { + update_uri(path); + } else { + struct directory *directory = db_get_root(); + struct stat st; + + if (stat_directory(directory, &st) == 0) + update_directory(directory, &st); + } + + return modified; +} diff --git a/src/UpdateWalk.hxx b/src/UpdateWalk.hxx new file mode 100644 index 000000000..7fa3083fc --- /dev/null +++ b/src/UpdateWalk.hxx @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_WALK_HXX +#define MPD_UPDATE_WALK_HXX + +#include "check.h" + +#include + +void +update_walk_global_init(void); + +void +update_walk_global_finish(void); + +/** + * Returns true if the database was modified. + */ +bool +update_walk(const char *path, bool discard); + +#endif diff --git a/src/inotify_queue.c b/src/inotify_queue.c deleted file mode 100644 index d5e2228c3..000000000 --- a/src/inotify_queue.c +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "inotify_queue.h" -#include "update.h" - -#include - -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "inotify" - -enum { - /** - * Wait this long after the last change before calling - * update_enqueue(). This increases the probability that - * updates can be bundled. - */ - INOTIFY_UPDATE_DELAY_S = 5, -}; - -static GSList *inotify_queue; -static guint queue_source_id; - -void -mpd_inotify_queue_init(void) -{ -} - -static void -free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data) -{ - g_free(data); -} - -void -mpd_inotify_queue_finish(void) -{ - if (queue_source_id != 0) - g_source_remove(queue_source_id); - - g_slist_foreach(inotify_queue, free_callback, NULL); - g_slist_free(inotify_queue); -} - -static gboolean -mpd_inotify_run_update(G_GNUC_UNUSED gpointer data) -{ - unsigned id; - - while (inotify_queue != NULL) { - char *uri_utf8 = inotify_queue->data; - - id = update_enqueue(uri_utf8, false); - if (id == 0) - /* retry later */ - return true; - - g_debug("updating '%s' job=%u", uri_utf8, id); - - g_free(uri_utf8); - inotify_queue = g_slist_delete_link(inotify_queue, - inotify_queue); - } - - /* done, remove the timer event by returning false */ - queue_source_id = 0; - return false; -} - -static bool -path_in(const char *path, const char *possible_parent) -{ - size_t length = strlen(possible_parent); - - return path[0] == 0 || - (memcmp(possible_parent, path, length) == 0 && - (path[length] == 0 || path[length] == '/')); -} - -void -mpd_inotify_enqueue(char *uri_utf8) -{ - GSList *old_queue = inotify_queue; - - if (queue_source_id != 0) - g_source_remove(queue_source_id); - queue_source_id = g_timeout_add_seconds(INOTIFY_UPDATE_DELAY_S, - mpd_inotify_run_update, NULL); - - inotify_queue = NULL; - while (old_queue != NULL) { - char *current_uri = old_queue->data; - - if (path_in(uri_utf8, current_uri)) { - /* already enqueued */ - g_free(uri_utf8); - inotify_queue = g_slist_concat(inotify_queue, - old_queue); - return; - } - - old_queue = g_slist_delete_link(old_queue, old_queue); - - if (path_in(current_uri, uri_utf8)) - /* existing path is a sub-path of the new - path; we can dequeue the existing path and - update the new path instead */ - g_free(current_uri); - else - /* move the existing path to the new queue */ - inotify_queue = g_slist_prepend(inotify_queue, - current_uri); - } - - inotify_queue = g_slist_prepend(inotify_queue, uri_utf8); -} diff --git a/src/inotify_queue.h b/src/inotify_queue.h deleted file mode 100644 index cfc28ebfe..000000000 --- a/src/inotify_queue.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_INOTIFY_QUEUE_H -#define MPD_INOTIFY_QUEUE_H - -void -mpd_inotify_queue_init(void); - -void -mpd_inotify_queue_finish(void); - -void -mpd_inotify_enqueue(char *uri_utf8); - -#endif diff --git a/src/inotify_source.c b/src/inotify_source.c deleted file mode 100644 index 2fbfdff7e..000000000 --- a/src/inotify_source.c +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "inotify_source.h" -#include "fifo_buffer.h" -#include "fd_util.h" -#include "mpd_error.h" - -#include - -#include -#include -#include -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "inotify" - -struct mpd_inotify_source { - int fd; - - GIOChannel *channel; - - /** - * The channel's source id in the GLib main loop. - */ - guint id; - - struct fifo_buffer *buffer; - - mpd_inotify_callback_t callback; - void *callback_ctx; -}; - -/** - * A GQuark for GError instances. - */ -static inline GQuark -mpd_inotify_quark(void) -{ - return g_quark_from_static_string("inotify"); -} - -static gboolean -mpd_inotify_in_event(G_GNUC_UNUSED GIOChannel *_source, - G_GNUC_UNUSED GIOCondition condition, - gpointer data) -{ - struct mpd_inotify_source *source = data; - void *dest; - size_t length; - ssize_t nbytes; - const struct inotify_event *event; - - dest = fifo_buffer_write(source->buffer, &length); - if (dest == NULL) - MPD_ERROR("buffer full"); - - nbytes = read(source->fd, dest, length); - if (nbytes < 0) - MPD_ERROR("failed to read from inotify: %s", - g_strerror(errno)); - if (nbytes == 0) - MPD_ERROR("end of file from inotify"); - - fifo_buffer_append(source->buffer, nbytes); - - while (true) { - const char *name; - - event = fifo_buffer_read(source->buffer, &length); - if (event == NULL || length < sizeof(*event) || - length < sizeof(*event) + event->len) - break; - - if (event->len > 0 && event->name[event->len - 1] == 0) - name = event->name; - else - name = NULL; - - source->callback(event->wd, event->mask, name, - source->callback_ctx); - fifo_buffer_consume(source->buffer, - sizeof(*event) + event->len); - } - - return true; -} - -struct mpd_inotify_source * -mpd_inotify_source_new(mpd_inotify_callback_t callback, void *callback_ctx, - GError **error_r) -{ - struct mpd_inotify_source *source = - g_new(struct mpd_inotify_source, 1); - - source->fd = inotify_init_cloexec(); - if (source->fd < 0) { - g_set_error(error_r, mpd_inotify_quark(), errno, - "inotify_init() has failed: %s", - g_strerror(errno)); - g_free(source); - return NULL; - } - - source->buffer = fifo_buffer_new(4096); - - source->channel = g_io_channel_unix_new(source->fd); - source->id = g_io_add_watch(source->channel, G_IO_IN, - mpd_inotify_in_event, source); - - source->callback = callback; - source->callback_ctx = callback_ctx; - - return source; -} - -void -mpd_inotify_source_free(struct mpd_inotify_source *source) -{ - g_source_remove(source->id); - g_io_channel_unref(source->channel); - fifo_buffer_free(source->buffer); - close(source->fd); - g_free(source); -} - -int -mpd_inotify_source_add(struct mpd_inotify_source *source, - const char *path_fs, unsigned mask, - GError **error_r) -{ - int wd = inotify_add_watch(source->fd, path_fs, mask); - if (wd < 0) - g_set_error(error_r, mpd_inotify_quark(), errno, - "inotify_add_watch() has failed: %s", - g_strerror(errno)); - - return wd; -} - -void -mpd_inotify_source_rm(struct mpd_inotify_source *source, unsigned wd) -{ - int ret = inotify_rm_watch(source->fd, wd); - if (ret < 0 && errno != EINVAL) - g_warning("inotify_rm_watch() has failed: %s", - g_strerror(errno)); - - /* EINVAL may happen here when the file has been deleted; the - kernel seems to auto-unregister deleted files */ -} diff --git a/src/inotify_source.h b/src/inotify_source.h deleted file mode 100644 index 7aec18b3d..000000000 --- a/src/inotify_source.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_INOTIFY_SOURCE_H -#define MPD_INOTIFY_SOURCE_H - -#include "gerror.h" - -typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask, - const char *name, void *ctx); - -struct mpd_inotify_source; - -/** - * Creates a new inotify source and registers it in the GLib main - * loop. - * - * @param a callback invoked for events received from the kernel - */ -struct mpd_inotify_source * -mpd_inotify_source_new(mpd_inotify_callback_t callback, void *callback_ctx, - GError **error_r); - -void -mpd_inotify_source_free(struct mpd_inotify_source *source); - -/** - * Adds a path to the notify list. - * - * @return a watch descriptor or -1 on error - */ -int -mpd_inotify_source_add(struct mpd_inotify_source *source, - const char *path_fs, unsigned mask, - GError **error_r); - -/** - * Removes a path from the notify list. - * - * @param wd the watch descriptor returned by mpd_inotify_source_add() - */ -void -mpd_inotify_source_rm(struct mpd_inotify_source *source, unsigned wd); - -#endif diff --git a/src/inotify_update.c b/src/inotify_update.c deleted file mode 100644 index 3f4a8c0c4..000000000 --- a/src/inotify_update.c +++ /dev/null @@ -1,374 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "inotify_update.h" -#include "inotify_source.h" -#include "inotify_queue.h" -#include "database.h" -#include "mapper.h" -#include "path.h" - -#include -#include -#include -#include -#include -#include -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "inotify" - -enum { - IN_MASK = IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF - |IN_MOVE|IN_MOVE_SELF -#ifdef IN_ONLYDIR - |IN_ONLYDIR -#endif -}; - -struct watch_directory { - struct watch_directory *parent; - - char *name; - - int descriptor; - - GList *children; -}; - -static struct mpd_inotify_source *inotify_source; - -static unsigned inotify_max_depth; -static struct watch_directory inotify_root; -static GTree *inotify_directories; - -static gint -compare(gconstpointer a, gconstpointer b) -{ - if (a < b) - return -1; - else if (a > b) - return 1; - else - return 0; -} - -static void -tree_add_watch_directory(struct watch_directory *directory) -{ - g_tree_insert(inotify_directories, - GINT_TO_POINTER(directory->descriptor), directory); -} - -static void -tree_remove_watch_directory(struct watch_directory *directory) -{ - G_GNUC_UNUSED - bool found = g_tree_remove(inotify_directories, - GINT_TO_POINTER(directory->descriptor)); - assert(found); -} - -static struct watch_directory * -tree_find_watch_directory(int wd) -{ - return g_tree_lookup(inotify_directories, GINT_TO_POINTER(wd)); -} - -static void -remove_watch_directory(struct watch_directory *directory) -{ - assert(directory != NULL); - - if (directory->parent == NULL) { - g_warning("music directory was removed - " - "cannot continue to watch it"); - return; - } - - assert(directory->parent->children != NULL); - - tree_remove_watch_directory(directory); - - while (directory->children != NULL) - remove_watch_directory(directory->children->data); - - directory->parent->children = - g_list_remove(directory->parent->children, directory); - - mpd_inotify_source_rm(inotify_source, directory->descriptor); - g_free(directory->name); - g_slice_free(struct watch_directory, directory); -} - -static char * -watch_directory_get_uri_fs(const struct watch_directory *directory) -{ - char *parent_uri, *uri; - - if (directory->parent == NULL) - return NULL; - - parent_uri = watch_directory_get_uri_fs(directory->parent); - if (parent_uri == NULL) - return g_strdup(directory->name); - - uri = g_strconcat(parent_uri, "/", directory->name, NULL); - g_free(parent_uri); - - return uri; -} - -/* we don't look at "." / ".." nor files with newlines in their name */ -static bool skip_path(const char *path) -{ - return (path[0] == '.' && path[1] == 0) || - (path[0] == '.' && path[1] == '.' && path[2] == 0) || - strchr(path, '\n') != NULL; -} - -static void -recursive_watch_subdirectories(struct watch_directory *directory, - const char *path_fs, unsigned depth) -{ - GError *error = NULL; - DIR *dir; - struct dirent *ent; - - assert(directory != NULL); - assert(depth <= inotify_max_depth); - assert(path_fs != NULL); - - ++depth; - - if (depth > inotify_max_depth) - return; - - dir = opendir(path_fs); - if (dir == NULL) { - g_warning("Failed to open directory %s: %s", - path_fs, g_strerror(errno)); - return; - } - - while ((ent = readdir(dir))) { - char *child_path_fs; - struct stat st; - int ret; - struct watch_directory *child; - - if (skip_path(ent->d_name)) - continue; - - child_path_fs = g_strconcat(path_fs, "/", ent->d_name, NULL); - ret = stat(child_path_fs, &st); - if (ret < 0) { - g_warning("Failed to stat %s: %s", - child_path_fs, g_strerror(errno)); - g_free(child_path_fs); - continue; - } - - if (!S_ISDIR(st.st_mode)) { - g_free(child_path_fs); - continue; - } - - ret = mpd_inotify_source_add(inotify_source, child_path_fs, - IN_MASK, &error); - if (ret < 0) { - g_warning("Failed to register %s: %s", - child_path_fs, error->message); - g_error_free(error); - error = NULL; - g_free(child_path_fs); - continue; - } - - child = tree_find_watch_directory(ret); - if (child != NULL) { - /* already being watched */ - g_free(child_path_fs); - continue; - } - - child = g_slice_new(struct watch_directory); - child->parent = directory; - child->name = g_strdup(ent->d_name); - child->descriptor = ret; - child->children = NULL; - - directory->children = g_list_prepend(directory->children, - child); - - tree_add_watch_directory(child); - - recursive_watch_subdirectories(child, child_path_fs, depth); - g_free(child_path_fs); - } - - closedir(dir); -} - -G_GNUC_PURE -static unsigned -watch_directory_depth(const struct watch_directory *d) -{ - assert(d != NULL); - - unsigned depth = 0; - while ((d = d->parent) != NULL) - ++depth; - - return depth; -} - -static void -mpd_inotify_callback(int wd, unsigned mask, - G_GNUC_UNUSED const char *name, G_GNUC_UNUSED void *ctx) -{ - struct watch_directory *directory; - char *uri_fs; - - /*g_debug("wd=%d mask=0x%x name='%s'", wd, mask, name);*/ - - directory = tree_find_watch_directory(wd); - if (directory == NULL) - return; - - uri_fs = watch_directory_get_uri_fs(directory); - - if ((mask & (IN_DELETE_SELF|IN_MOVE_SELF)) != 0) { - g_free(uri_fs); - remove_watch_directory(directory); - return; - } - - if ((mask & (IN_ATTRIB|IN_CREATE|IN_MOVE)) != 0 && - (mask & IN_ISDIR) != 0) { - /* a sub directory was changed: register those in - inotify */ - const char *root = mapper_get_music_directory_fs(); - const char *path_fs; - char *allocated = NULL; - - if (uri_fs != NULL) - path_fs = allocated = - g_strconcat(root, "/", uri_fs, NULL); - else - path_fs = root; - - recursive_watch_subdirectories(directory, path_fs, - watch_directory_depth(directory)); - g_free(allocated); - } - - if ((mask & (IN_CLOSE_WRITE|IN_MOVE|IN_DELETE)) != 0 || - /* at the maximum depth, we watch out for newly created - directories */ - (watch_directory_depth(directory) == inotify_max_depth && - (mask & (IN_CREATE|IN_ISDIR)) == (IN_CREATE|IN_ISDIR))) { - /* a file was changed, or a directory was - moved/deleted: queue a database update */ - char *uri_utf8 = uri_fs != NULL - ? fs_charset_to_utf8(uri_fs) - : g_strdup(""); - - if (uri_utf8 != NULL) - /* this function will take care of freeing - uri_utf8 */ - mpd_inotify_enqueue(uri_utf8); - } - - g_free(uri_fs); -} - -void -mpd_inotify_init(unsigned max_depth) -{ - GError *error = NULL; - - g_debug("initializing inotify"); - - const char *path = mapper_get_music_directory_fs(); - if (path == NULL) { - g_debug("no music directory configured"); - return; - } - - inotify_source = mpd_inotify_source_new(mpd_inotify_callback, NULL, - &error); - if (inotify_source == NULL) { - g_warning("%s", error->message); - g_error_free(error); - return; - } - - inotify_max_depth = max_depth; - - inotify_root.name = g_strdup(path); - inotify_root.descriptor = mpd_inotify_source_add(inotify_source, path, - IN_MASK, &error); - if (inotify_root.descriptor < 0) { - g_warning("%s", error->message); - g_error_free(error); - mpd_inotify_source_free(inotify_source); - inotify_source = NULL; - return; - } - - inotify_directories = g_tree_new(compare); - tree_add_watch_directory(&inotify_root); - - recursive_watch_subdirectories(&inotify_root, path, 0); - - mpd_inotify_queue_init(); - - g_debug("watching music directory"); -} - -static gboolean -free_watch_directory(G_GNUC_UNUSED gpointer key, gpointer value, - G_GNUC_UNUSED gpointer data) -{ - struct watch_directory *directory = value; - - g_free(directory->name); - g_list_free(directory->children); - - if (directory != &inotify_root) - g_slice_free(struct watch_directory, directory); - - return false; -} - -void -mpd_inotify_finish(void) -{ - if (inotify_source == NULL) - return; - - mpd_inotify_queue_finish(); - mpd_inotify_source_free(inotify_source); - - g_tree_foreach(inotify_directories, free_watch_directory, NULL); - g_tree_destroy(inotify_directories); -} diff --git a/src/inotify_update.h b/src/inotify_update.h deleted file mode 100644 index ca75c0f45..000000000 --- a/src/inotify_update.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_INOTIFY_UPDATE_H -#define MPD_INOTIFY_UPDATE_H - -#include "check.h" - -#ifdef HAVE_INOTIFY_INIT - -void -mpd_inotify_init(unsigned max_depth); - -void -mpd_inotify_finish(void); - -#else /* !HAVE_INOTIFY_INIT */ - -static inline void -mpd_inotify_init(G_GNUC_UNUSED unsigned max_depth) -{ -} - -static inline void -mpd_inotify_finish(void) -{ -} - -#endif /* !HAVE_INOTIFY_INIT */ - -#endif diff --git a/src/update.c b/src/update.c deleted file mode 100644 index e47db8f09..000000000 --- a/src/update.c +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "update.h" -#include "update_queue.h" -#include "update_walk.h" -#include "update_remove.h" -#include "update.h" -#include "database.h" -#include "mapper.h" -#include "playlist.h" -#include "event_pipe.h" -#include "update.h" -#include "idle.h" -#include "stats.h" -#include "Main.hxx" -#include "mpd_error.h" - -#include - -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "update" - -static enum update_progress { - UPDATE_PROGRESS_IDLE = 0, - UPDATE_PROGRESS_RUNNING = 1, - UPDATE_PROGRESS_DONE = 2 -} progress; - -static bool modified; - -static GThread *update_thr; - -static const unsigned update_task_id_max = 1 << 15; - -static unsigned update_task_id; - -/* XXX this flag is passed to update_task() */ -static bool discard; - -unsigned -isUpdatingDB(void) -{ - return (progress != UPDATE_PROGRESS_IDLE) ? update_task_id : 0; -} - -static void * update_task(void *_path) -{ - const char *path = _path; - - if (path != NULL && *path != 0) - g_debug("starting: %s", path); - else - g_debug("starting"); - - modified = update_walk(path, discard); - - if (modified || !db_exists()) { - GError *error = NULL; - if (!db_save(&error)) { - g_warning("Failed to save database: %s", - error->message); - g_error_free(error); - } - } - - if (path != NULL && *path != 0) - g_debug("finished: %s", path); - else - g_debug("finished"); - g_free(_path); - - progress = UPDATE_PROGRESS_DONE; - event_pipe_emit(PIPE_EVENT_UPDATE); - return NULL; -} - -static void -spawn_update_task(const char *path) -{ - GError *e = NULL; - - assert(g_thread_self() == main_task); - - progress = UPDATE_PROGRESS_RUNNING; - modified = false; - - update_thr = g_thread_create(update_task, g_strdup(path), TRUE, &e); - if (update_thr == NULL) - MPD_ERROR("Failed to spawn update task: %s", e->message); - - if (++update_task_id > update_task_id_max) - update_task_id = 1; - g_debug("spawned thread for update job id %i", update_task_id); -} - -unsigned -update_enqueue(const char *path, bool _discard) -{ - assert(g_thread_self() == main_task); - - if (!db_is_simple() || !mapper_has_music_directory()) - return 0; - - if (progress != UPDATE_PROGRESS_IDLE) { - unsigned next_task_id = - update_queue_push(path, discard, update_task_id); - if (next_task_id == 0) - return 0; - - return next_task_id > update_task_id_max ? 1 : next_task_id; - } - - discard = _discard; - spawn_update_task(path); - - idle_add(IDLE_UPDATE); - - return update_task_id; -} - -/** - * Called in the main thread after the database update is finished. - */ -static void update_finished_event(void) -{ - char *path; - - assert(progress == UPDATE_PROGRESS_DONE); - - g_thread_join(update_thr); - - idle_add(IDLE_UPDATE); - - if (modified) { - /* send "idle" events */ - playlist_increment_version_all(&g_playlist); - idle_add(IDLE_DATABASE); - } - - path = update_queue_shift(&discard); - if (path != NULL) { - /* schedule the next path */ - spawn_update_task(path); - g_free(path); - } else { - progress = UPDATE_PROGRESS_IDLE; - - stats_update(); - } -} - -void update_global_init(void) -{ - event_pipe_register(PIPE_EVENT_UPDATE, update_finished_event); - - update_remove_global_init(); - update_walk_global_init(); -} - -void update_global_finish(void) -{ - update_walk_global_finish(); - update_remove_global_finish(); -} diff --git a/src/update.h b/src/update.h deleted file mode 100644 index 3d586b694..000000000 --- a/src/update.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_H -#define MPD_UPDATE_H - -#include - -void update_global_init(void); - -void update_global_finish(void); - -unsigned -isUpdatingDB(void); - -/** - * Add this path to the database update queue. - * - * @param path a path to update; if NULL or an empty string, - * the whole music directory is updated - * @return the job id, or 0 on error - */ -unsigned -update_enqueue(const char *path, bool discard); - -#endif diff --git a/src/update_archive.c b/src/update_archive.c deleted file mode 100644 index 3fb2bc18c..000000000 --- a/src/update_archive.c +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "update_archive.h" -#include "update_internal.h" -#include "db_lock.h" -#include "directory.h" -#include "song.h" -#include "mapper.h" -#include "archive_list.h" -#include "archive_plugin.h" - -#include - -#include - -static void -update_archive_tree(struct directory *directory, char *name) -{ - char *tmp = strchr(name, '/'); - if (tmp) { - *tmp = 0; - //add dir is not there already - db_lock(); - struct directory *subdir = - directory_make_child(directory, name); - subdir->device = DEVICE_INARCHIVE; - db_unlock(); - //create directories first - update_archive_tree(subdir, tmp+1); - } else { - if (strlen(name) == 0) { - g_warning("archive returned directory only"); - return; - } - - //add file - db_lock(); - struct song *song = directory_get_song(directory, name); - db_unlock(); - if (song == NULL) { - song = song_file_load(name, directory); - if (song != NULL) { - db_lock(); - directory_add_song(directory, song); - db_unlock(); - - modified = true; - g_message("added %s/%s", - directory_get_path(directory), name); - } - } - } -} - -/** - * Updates the file listing from an archive file. - * - * @param parent the parent directory the archive file resides in - * @param name the UTF-8 encoded base name of the archive file - * @param st stat() information on the archive file - * @param plugin the archive plugin which fits this archive type - */ -static void -update_archive_file2(struct directory *parent, const char *name, - const struct stat *st, - const struct archive_plugin *plugin) -{ - db_lock(); - struct directory *directory = directory_get_child(parent, name); - db_unlock(); - - if (directory != NULL && directory->mtime == st->st_mtime && - !walk_discard) - /* MPD has already scanned the archive, and it hasn't - changed since - don't consider updating it */ - return; - - char *path_fs = map_directory_child_fs(parent, name); - - /* open archive */ - GError *error = NULL; - struct archive_file *file = archive_file_open(plugin, path_fs, &error); - if (file == NULL) { - g_free(path_fs); - g_warning("%s", error->message); - g_error_free(error); - return; - } - - g_debug("archive %s opened", path_fs); - g_free(path_fs); - - if (directory == NULL) { - g_debug("creating archive directory: %s", name); - db_lock(); - directory = directory_new_child(parent, name); - /* mark this directory as archive (we use device for - this) */ - directory->device = DEVICE_INARCHIVE; - db_unlock(); - } - - directory->mtime = st->st_mtime; - - archive_file_scan_reset(file); - - char *filepath; - while ((filepath = archive_file_scan_next(file)) != NULL) { - /* split name into directory and file */ - g_debug("adding archive file: %s", filepath); - update_archive_tree(directory, filepath); - } - - archive_file_close(file); -} - -bool -update_archive_file(struct directory *directory, - const char *name, const char *suffix, - const struct stat *st) -{ -#ifdef ENABLE_ARCHIVE - const struct archive_plugin *plugin = - archive_plugin_from_suffix(suffix); - if (plugin == NULL) - return false; - - update_archive_file2(directory, name, st, plugin); - return true; -#else - (void)directory; - (void)name; - (void)suffix; - (void)st; - - return false; -#endif -} diff --git a/src/update_archive.h b/src/update_archive.h deleted file mode 100644 index a9139b0e0..000000000 --- a/src/update_archive.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_ARCHIVE_H -#define MPD_UPDATE_ARCHIVE_H - -#include "check.h" -#include "gcc.h" - -#include -#include - -struct directory; -struct archive_plugin; - -#ifdef ENABLE_ARCHIVE - -bool -update_archive_file(struct directory *directory, - const char *name, const char *suffix, - const struct stat *st); - -#else - -#include - -static inline bool -update_archive_file(gcc_unused struct directory *directory, - gcc_unused const char *name, - gcc_unused const char *suffix, - gcc_unused const struct stat *st) -{ - return false; -} - -#endif - -#endif diff --git a/src/update_container.c b/src/update_container.c deleted file mode 100644 index bda95dabe..000000000 --- a/src/update_container.c +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "update_container.h" -#include "update_internal.h" -#include "update_db.h" -#include "db_lock.h" -#include "directory.h" -#include "song.h" -#include "mapper.h" -#include "decoder_plugin.h" -#include "tag.h" -#include "tag_handler.h" - -#include - -/** - * Create the specified directory object if it does not exist already - * or if the #stat object indicates that it has been modified since - * the last update. Returns NULL when it exists already and is - * unmodified. - * - * The caller must lock the database. - */ -static struct directory * -make_directory_if_modified(struct directory *parent, const char *name, - const struct stat *st) -{ - struct directory *directory = directory_get_child(parent, name); - - // directory exists already - if (directory != NULL) { - if (directory->mtime == st->st_mtime && !walk_discard) { - /* not modified */ - db_unlock(); - return NULL; - } - - delete_directory(directory); - modified = true; - } - - directory = directory_make_child(parent, name); - directory->mtime = st->st_mtime; - return directory; -} - -bool -update_container_file(struct directory *directory, - const char *name, - const struct stat *st, - const struct decoder_plugin *plugin) -{ - if (plugin->container_scan == NULL) - return false; - - db_lock(); - struct directory *contdir = - make_directory_if_modified(directory, name, st); - if (contdir == NULL) { - /* not modified */ - db_unlock(); - return true; - } - - contdir->device = DEVICE_CONTAINER; - db_unlock(); - - char *const pathname = map_directory_child_fs(directory, name); - - char *vtrack; - unsigned int tnum = 0; - while ((vtrack = plugin->container_scan(pathname, ++tnum)) != NULL) { - struct song *song = song_file_new(vtrack, contdir); - - // shouldn't be necessary but it's there.. - song->mtime = st->st_mtime; - - char *child_path_fs = map_directory_child_fs(contdir, vtrack); - - song->tag = tag_new(); - decoder_plugin_scan_file(plugin, child_path_fs, - &add_tag_handler, song->tag); - g_free(child_path_fs); - - db_lock(); - directory_add_song(contdir, song); - db_unlock(); - - modified = true; - - g_message("added %s/%s", - directory_get_path(directory), vtrack); - g_free(vtrack); - } - - g_free(pathname); - - if (tnum == 1) { - db_lock(); - delete_directory(contdir); - db_unlock(); - return false; - } else - return true; -} diff --git a/src/update_container.h b/src/update_container.h deleted file mode 100644 index 7f42c80ca..000000000 --- a/src/update_container.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_CONTAINER_H -#define MPD_UPDATE_CONTAINER_H - -#include "check.h" - -#include -#include - -struct directory; -struct decoder_plugin; - -bool -update_container_file(struct directory *directory, - const char *name, - const struct stat *st, - const struct decoder_plugin *plugin); - -#endif diff --git a/src/update_db.c b/src/update_db.c deleted file mode 100644 index 8982a53e2..000000000 --- a/src/update_db.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "update_db.h" -#include "update_remove.h" -#include "directory.h" -#include "song.h" -#include "playlist_vector.h" -#include "db_lock.h" - -#include -#include - -void -delete_song(struct directory *dir, struct song *del) -{ - assert(del->parent == dir); - - /* first, prevent traversers in main task from getting this */ - directory_remove_song(dir, del); - - db_unlock(); /* temporary unlock, because update_remove_song() blocks */ - - /* now take it out of the playlist (in the main_task) */ - update_remove_song(del); - - /* finally, all possible references gone, free it */ - song_free(del); - - db_lock(); -} - -/** - * Recursively remove all sub directories and songs from a directory, - * leaving an empty directory. - * - * Caller must lock the #db_mutex. - */ -static void -clear_directory(struct directory *directory) -{ - struct directory *child, *n; - directory_for_each_child_safe(child, n, directory) - delete_directory(child); - - struct song *song, *ns; - directory_for_each_song_safe(song, ns, directory) { - assert(song->parent == directory); - delete_song(directory, song); - } -} - -void -delete_directory(struct directory *directory) -{ - assert(directory->parent != NULL); - - clear_directory(directory); - - directory_delete(directory); -} - -bool -delete_name_in(struct directory *parent, const char *name) -{ - bool modified = false; - - db_lock(); - struct directory *directory = directory_get_child(parent, name); - - if (directory != NULL) { - delete_directory(directory); - modified = true; - } - - struct song *song = directory_get_song(parent, name); - if (song != NULL) { - delete_song(parent, song); - modified = true; - } - - playlist_vector_remove(&parent->playlists, name); - - db_unlock(); - - return modified; -} diff --git a/src/update_db.h b/src/update_db.h deleted file mode 100644 index 0a9e46b05..000000000 --- a/src/update_db.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_DB_H -#define MPD_UPDATE_DB_H - -#include "check.h" - -#include - -struct directory; -struct song; - -/** - * Caller must lock the #db_mutex. - */ -void -delete_song(struct directory *parent, struct song *song); - -/** - * Recursively free a directory and all its contents. - * - * Caller must lock the #db_mutex. - */ -void -delete_directory(struct directory *directory); - -/** - * Caller must NOT lock the #db_mutex. - * - * @return true if the database was modified - */ -bool -delete_name_in(struct directory *parent, const char *name); - -#endif diff --git a/src/update_internal.h b/src/update_internal.h deleted file mode 100644 index 76ab7bed3..000000000 --- a/src/update_internal.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_INTERNAL_H -#define MPD_UPDATE_INTERNAL_H - -#include "check.h" - -#include - -extern bool walk_discard; -extern bool modified; - -#endif diff --git a/src/update_io.c b/src/update_io.c deleted file mode 100644 index c6a540a0f..000000000 --- a/src/update_io.c +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "update_io.h" -#include "mapper.h" -#include "directory.h" -#include "glib_compat.h" - -#include - -#include -#include - -int -stat_directory(const struct directory *directory, struct stat *st) -{ - char *path_fs = map_directory_fs(directory); - if (path_fs == NULL) - return -1; - - int ret = stat(path_fs, st); - if (ret < 0) - g_warning("Failed to stat %s: %s", path_fs, g_strerror(errno)); - - g_free(path_fs); - return ret; -} - -int -stat_directory_child(const struct directory *parent, const char *name, - struct stat *st) -{ - char *path_fs = map_directory_child_fs(parent, name); - if (path_fs == NULL) - return -1; - - int ret = stat(path_fs, st); - if (ret < 0) - g_warning("Failed to stat %s: %s", path_fs, g_strerror(errno)); - - g_free(path_fs); - return ret; -} - -bool -directory_exists(const struct directory *directory) -{ - char *path_fs = map_directory_fs(directory); - if (path_fs == NULL) - /* invalid path: cannot exist */ - return false; - - GFileTest test = directory->device == DEVICE_INARCHIVE || - directory->device == DEVICE_CONTAINER - ? G_FILE_TEST_IS_REGULAR - : G_FILE_TEST_IS_DIR; - - bool exists = g_file_test(path_fs, test); - g_free(path_fs); - - return exists; -} - -bool -directory_child_is_regular(const struct directory *directory, - const char *name_utf8) -{ - char *path_fs = map_directory_child_fs(directory, name_utf8); - if (path_fs == NULL) - return false; - - struct stat st; - bool is_regular = stat(path_fs, &st) == 0 && S_ISREG(st.st_mode); - g_free(path_fs); - - return is_regular; -} - -bool -directory_child_access(const struct directory *directory, - const char *name, int mode) -{ -#ifdef WIN32 - /* access() is useless on WIN32 */ - (void)directory; - (void)name; - (void)mode; - return true; -#else - char *path = map_directory_child_fs(directory, name); - if (path == NULL) - /* something went wrong, but that isn't a permission - problem */ - return true; - - bool success = access(path, mode) == 0 || errno != EACCES; - g_free(path); - return success; -#endif -} diff --git a/src/update_io.h b/src/update_io.h deleted file mode 100644 index 6ff1ccebd..000000000 --- a/src/update_io.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_IO_H -#define MPD_UPDATE_IO_H - -#include "check.h" - -#include -#include - -struct directory; - -int -stat_directory(const struct directory *directory, struct stat *st); - -int -stat_directory_child(const struct directory *parent, const char *name, - struct stat *st); - -bool -directory_exists(const struct directory *directory); - -bool -directory_child_is_regular(const struct directory *directory, - const char *name_utf8); - -/** - * Checks if the given permissions on the mapped file are given. - */ -bool -directory_child_access(const struct directory *directory, - const char *name, int mode); - -#endif diff --git a/src/update_queue.c b/src/update_queue.c deleted file mode 100644 index 2150fa4e4..000000000 --- a/src/update_queue.c +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "update_queue.h" - -#include - -#include -#include - -/* make this dynamic?, or maybe this is big enough... */ -static struct { - char *path; - bool discard; -} update_queue[32]; - -static size_t update_queue_length; - -unsigned -update_queue_push(const char *path, bool discard, unsigned base) -{ - assert(update_queue_length <= G_N_ELEMENTS(update_queue)); - - if (update_queue_length == G_N_ELEMENTS(update_queue)) - return 0; - - update_queue[update_queue_length].path = g_strdup(path); - update_queue[update_queue_length].discard = discard; - - ++update_queue_length; - - return base + update_queue_length; -} - -char * -update_queue_shift(bool *discard_r) -{ - char *path; - - if (update_queue_length == 0) - return NULL; - - path = update_queue[0].path; - *discard_r = update_queue[0].discard; - - memmove(&update_queue[0], &update_queue[1], - --update_queue_length * sizeof(update_queue[0])); - return path; -} diff --git a/src/update_queue.h b/src/update_queue.h deleted file mode 100644 index 84ba474b0..000000000 --- a/src/update_queue.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_QUEUE_H -#define MPD_UPDATE_QUEUE_H - -#include "check.h" - -#include - -unsigned -update_queue_push(const char *path, bool discard, unsigned base); - -char * -update_queue_shift(bool *discard_r); - -#endif diff --git a/src/update_remove.c b/src/update_remove.c deleted file mode 100644 index c2e353c63..000000000 --- a/src/update_remove.c +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "update_remove.h" -#include "event_pipe.h" -#include "song.h" -#include "playlist.h" -#include "Main.hxx" - -#ifdef ENABLE_SQLITE -#include "sticker.h" -#include "song_sticker.h" -#endif - -#include - -#include - -static const struct song *removed_song; - -static GMutex *remove_mutex; -static GCond *remove_cond; - -/** - * Safely remove a song from the database. This must be done in the - * main task, to be sure that there is no pointer left to it. - */ -static void -song_remove_event(void) -{ - char *uri; - - assert(removed_song != NULL); - - uri = song_get_uri(removed_song); - g_message("removing %s", uri); - g_free(uri); - -#ifdef ENABLE_SQLITE - /* if the song has a sticker, remove it */ - if (sticker_enabled()) - sticker_song_delete(removed_song); -#endif - - playlist_delete_song(&g_playlist, global_player_control, removed_song); - - /* clear "removed_song" and send signal to update thread */ - g_mutex_lock(remove_mutex); - removed_song = NULL; - g_cond_signal(remove_cond); - g_mutex_unlock(remove_mutex); -} - -void -update_remove_global_init(void) -{ - remove_mutex = g_mutex_new(); - remove_cond = g_cond_new(); - - event_pipe_register(PIPE_EVENT_DELETE, song_remove_event); -} - -void -update_remove_global_finish(void) -{ - g_mutex_free(remove_mutex); - g_cond_free(remove_cond); -} - -void -update_remove_song(const struct song *song) -{ - assert(removed_song == NULL); - - removed_song = song; - - event_pipe_emit(PIPE_EVENT_DELETE); - - g_mutex_lock(remove_mutex); - - while (removed_song != NULL) - g_cond_wait(remove_cond, remove_mutex); - - g_mutex_unlock(remove_mutex); -} diff --git a/src/update_remove.h b/src/update_remove.h deleted file mode 100644 index 479ef83ff..000000000 --- a/src/update_remove.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_REMOVE_H -#define MPD_UPDATE_REMOVE_H - -#include "check.h" - -struct song; - -void -update_remove_global_init(void); - -void -update_remove_global_finish(void); - -/** - * Sends a signal to the main thread which will in turn remove the - * song: from the sticker database and from the playlist. This - * serialized access is implemented to avoid excessive locking. - */ -void -update_remove_song(const struct song *song); - -#endif diff --git a/src/update_song.c b/src/update_song.c deleted file mode 100644 index 1126ad115..000000000 --- a/src/update_song.c +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "update_song.h" -#include "update_internal.h" -#include "update_io.h" -#include "update_db.h" -#include "update_container.h" -#include "db_lock.h" -#include "directory.h" -#include "song.h" -#include "decoder_list.h" -#include "decoder_plugin.h" - -#include - -#include - -static void -update_song_file2(struct directory *directory, - const char *name, const struct stat *st, - const struct decoder_plugin *plugin) -{ - db_lock(); - struct song *song = directory_get_song(directory, name); - db_unlock(); - - if (!directory_child_access(directory, name, R_OK)) { - g_warning("no read permissions on %s/%s", - directory_get_path(directory), name); - if (song != NULL) { - db_lock(); - delete_song(directory, song); - db_unlock(); - } - - return; - } - - if (!(song != NULL && st->st_mtime == song->mtime && - !walk_discard) && - update_container_file(directory, name, st, plugin)) { - if (song != NULL) { - db_lock(); - delete_song(directory, song); - db_unlock(); - } - - return; - } - - if (song == NULL) { - g_debug("reading %s/%s", - directory_get_path(directory), name); - song = song_file_load(name, directory); - if (song == NULL) { - g_debug("ignoring unrecognized file %s/%s", - directory_get_path(directory), name); - return; - } - - db_lock(); - directory_add_song(directory, song); - db_unlock(); - - modified = true; - g_message("added %s/%s", - directory_get_path(directory), name); - } else if (st->st_mtime != song->mtime || walk_discard) { - g_message("updating %s/%s", - directory_get_path(directory), name); - if (!song_file_update(song)) { - g_debug("deleting unrecognized file %s/%s", - directory_get_path(directory), name); - db_lock(); - delete_song(directory, song); - db_unlock(); - } - - modified = true; - } -} - -bool -update_song_file(struct directory *directory, - const char *name, const char *suffix, - const struct stat *st) -{ - const struct decoder_plugin *plugin = - decoder_plugin_from_suffix(suffix, false); - if (plugin == NULL) - return false; - - update_song_file2(directory, name, st, plugin); - return true; -} diff --git a/src/update_song.h b/src/update_song.h deleted file mode 100644 index cff63f576..000000000 --- a/src/update_song.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_SONG_H -#define MPD_UPDATE_SONG_H - -#include "check.h" - -#include -#include - -struct directory; - -bool -update_song_file(struct directory *directory, - const char *name, const char *suffix, - const struct stat *st); - -#endif diff --git a/src/update_walk.c b/src/update_walk.c deleted file mode 100644 index 8554e8f3c..000000000 --- a/src/update_walk.c +++ /dev/null @@ -1,508 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "update_walk.h" -#include "update_io.h" -#include "update_db.h" -#include "update_song.h" -#include "update_archive.h" -#include "database.h" -#include "db_lock.h" -#include "exclude.h" -#include "directory.h" -#include "song.h" -#include "playlist_vector.h" -#include "uri.h" -#include "mapper.h" -#include "path.h" -#include "playlist_list.h" -#include "conf.h" - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "update" - -bool walk_discard; -bool modified; - -#ifndef WIN32 - -enum { - DEFAULT_FOLLOW_INSIDE_SYMLINKS = true, - DEFAULT_FOLLOW_OUTSIDE_SYMLINKS = true, -}; - -static bool follow_inside_symlinks; -static bool follow_outside_symlinks; - -#endif - -void -update_walk_global_init(void) -{ -#ifndef WIN32 - follow_inside_symlinks = - config_get_bool(CONF_FOLLOW_INSIDE_SYMLINKS, - DEFAULT_FOLLOW_INSIDE_SYMLINKS); - - follow_outside_symlinks = - config_get_bool(CONF_FOLLOW_OUTSIDE_SYMLINKS, - DEFAULT_FOLLOW_OUTSIDE_SYMLINKS); -#endif -} - -void -update_walk_global_finish(void) -{ -} - -static void -directory_set_stat(struct directory *dir, const struct stat *st) -{ - dir->inode = st->st_ino; - dir->device = st->st_dev; - dir->have_stat = true; -} - -static void -remove_excluded_from_directory(struct directory *directory, - GSList *exclude_list) -{ - db_lock(); - - struct directory *child, *n; - directory_for_each_child_safe(child, n, directory) { - char *name_fs = utf8_to_fs_charset(directory_get_name(child)); - - if (exclude_list_check(exclude_list, name_fs)) { - delete_directory(child); - modified = true; - } - - g_free(name_fs); - } - - struct song *song, *ns; - directory_for_each_song_safe(song, ns, directory) { - assert(song->parent == directory); - - char *name_fs = utf8_to_fs_charset(song->uri); - if (exclude_list_check(exclude_list, name_fs)) { - delete_song(directory, song); - modified = true; - } - - g_free(name_fs); - } - - db_unlock(); -} - -static void -purge_deleted_from_directory(struct directory *directory) -{ - struct directory *child, *n; - directory_for_each_child_safe(child, n, directory) { - if (directory_exists(child)) - continue; - - db_lock(); - delete_directory(child); - db_unlock(); - - modified = true; - } - - struct song *song, *ns; - directory_for_each_song_safe(song, ns, directory) { - char *path; - struct stat st; - if ((path = map_song_fs(song)) == NULL || - stat(path, &st) < 0 || !S_ISREG(st.st_mode)) { - db_lock(); - delete_song(directory, song); - db_unlock(); - - modified = true; - } - - g_free(path); - } - - struct playlist_metadata *pm, *np; - directory_for_each_playlist_safe(pm, np, directory) { - if (!directory_child_is_regular(directory, pm->name)) { - db_lock(); - playlist_vector_remove(&directory->playlists, pm->name); - db_unlock(); - } - } -} - -#ifndef G_OS_WIN32 -static int -update_directory_stat(struct directory *directory) -{ - struct stat st; - if (stat_directory(directory, &st) < 0) - return -1; - - directory_set_stat(directory, &st); - return 0; -} -#endif - -static int -find_inode_ancestor(struct directory *parent, ino_t inode, dev_t device) -{ -#ifndef G_OS_WIN32 - while (parent) { - if (!parent->have_stat && update_directory_stat(parent) < 0) - return -1; - - if (parent->inode == inode && parent->device == device) { - g_debug("recursive directory found"); - return 1; - } - - parent = parent->parent; - } -#else - (void)parent; - (void)inode; - (void)device; -#endif - - return 0; -} - -static bool -update_playlist_file2(struct directory *directory, - const char *name, const char *suffix, - const struct stat *st) -{ - if (!playlist_suffix_supported(suffix)) - return false; - - db_lock(); - if (playlist_vector_update_or_add(&directory->playlists, name, - st->st_mtime)) - modified = true; - db_unlock(); - return true; -} - -static bool -update_regular_file(struct directory *directory, - const char *name, const struct stat *st) -{ - const char *suffix = uri_get_suffix(name); - if (suffix == NULL) - return false; - - return update_song_file(directory, name, suffix, st) || - update_archive_file(directory, name, suffix, st) || - update_playlist_file2(directory, name, suffix, st); -} - -static bool -update_directory(struct directory *directory, const struct stat *st); - -static void -update_directory_child(struct directory *directory, - const char *name, const struct stat *st) -{ - assert(strchr(name, '/') == NULL); - - if (S_ISREG(st->st_mode)) { - update_regular_file(directory, name, st); - } else if (S_ISDIR(st->st_mode)) { - if (find_inode_ancestor(directory, st->st_ino, st->st_dev)) - return; - - db_lock(); - struct directory *subdir = - directory_make_child(directory, name); - db_unlock(); - - assert(directory == subdir->parent); - - if (!update_directory(subdir, st)) { - db_lock(); - delete_directory(subdir); - db_unlock(); - } - } else { - g_debug("update: %s is not a directory, archive or music", name); - } -} - -/* we don't look at "." / ".." nor files with newlines in their name */ -G_GNUC_PURE -static bool skip_path(const char *path) -{ - return (path[0] == '.' && path[1] == 0) || - (path[0] == '.' && path[1] == '.' && path[2] == 0) || - strchr(path, '\n') != NULL; -} - -G_GNUC_PURE -static bool -skip_symlink(const struct directory *directory, const char *utf8_name) -{ -#ifndef WIN32 - char *path_fs = map_directory_child_fs(directory, utf8_name); - if (path_fs == NULL) - return true; - - char buffer[MPD_PATH_MAX]; - ssize_t length = readlink(path_fs, buffer, sizeof(buffer)); - g_free(path_fs); - if (length < 0) - /* don't skip if this is not a symlink */ - return errno != EINVAL; - - if ((size_t)length >= sizeof(buffer)) - /* skip symlinks when the buffer is too small for the - link target */ - return true; - - /* null-terminate the buffer, because readlink() will not */ - buffer[length] = 0; - - if (!follow_inside_symlinks && !follow_outside_symlinks) { - /* ignore all symlinks */ - return true; - } else if (follow_inside_symlinks && follow_outside_symlinks) { - /* consider all symlinks */ - return false; - } - - if (g_path_is_absolute(buffer)) { - /* if the symlink points to an absolute path, see if - that path is inside the music directory */ - const char *relative = map_to_relative_path(buffer); - return relative > buffer - ? !follow_inside_symlinks - : !follow_outside_symlinks; - } - - const char *p = buffer; - while (*p == '.') { - if (p[1] == '.' && G_IS_DIR_SEPARATOR(p[2])) { - /* "../" moves to parent directory */ - directory = directory->parent; - if (directory == NULL) { - /* we have moved outside the music - directory - skip this symlink - if such symlinks are not allowed */ - return !follow_outside_symlinks; - } - p += 3; - } else if (G_IS_DIR_SEPARATOR(p[1])) - /* eliminate "./" */ - p += 2; - else - break; - } - - /* we are still in the music directory, so this symlink points - to a song which is already in the database - skip according - to the follow_inside_symlinks param*/ - return !follow_inside_symlinks; -#else - /* no symlink checking on WIN32 */ - - (void)directory; - (void)utf8_name; - - return false; -#endif -} - -static bool -update_directory(struct directory *directory, const struct stat *st) -{ - assert(S_ISDIR(st->st_mode)); - - directory_set_stat(directory, st); - - char *path_fs = map_directory_fs(directory); - if (path_fs == NULL) - return false; - - DIR *dir = opendir(path_fs); - if (!dir) { - g_warning("Failed to open directory %s: %s", - path_fs, g_strerror(errno)); - g_free(path_fs); - return false; - } - - char *exclude_path_fs = g_build_filename(path_fs, ".mpdignore", NULL); - GSList *exclude_list = exclude_list_load(exclude_path_fs); - g_free(exclude_path_fs); - - g_free(path_fs); - - if (exclude_list != NULL) - remove_excluded_from_directory(directory, exclude_list); - - purge_deleted_from_directory(directory); - - struct dirent *ent; - while ((ent = readdir(dir))) { - char *utf8; - struct stat st2; - - if (skip_path(ent->d_name) || - exclude_list_check(exclude_list, ent->d_name)) - continue; - - utf8 = fs_charset_to_utf8(ent->d_name); - if (utf8 == NULL) - continue; - - if (skip_symlink(directory, utf8)) { - modified |= delete_name_in(directory, utf8); - g_free(utf8); - continue; - } - - if (stat_directory_child(directory, utf8, &st2) == 0) - update_directory_child(directory, utf8, &st2); - else - modified |= delete_name_in(directory, utf8); - - g_free(utf8); - } - - exclude_list_free(exclude_list); - - closedir(dir); - - directory->mtime = st->st_mtime; - - return true; -} - -static struct directory * -directory_make_child_checked(struct directory *parent, const char *name_utf8) -{ - db_lock(); - struct directory *directory = directory_get_child(parent, name_utf8); - db_unlock(); - - if (directory != NULL) - return directory; - - struct stat st; - if (stat_directory_child(parent, name_utf8, &st) < 0 || - find_inode_ancestor(parent, st.st_ino, st.st_dev)) - return NULL; - - if (skip_symlink(parent, name_utf8)) - return NULL; - - /* if we're adding directory paths, make sure to delete filenames - with potentially the same name */ - db_lock(); - struct song *conflicting = directory_get_song(parent, name_utf8); - if (conflicting) - delete_song(parent, conflicting); - - directory = directory_new_child(parent, name_utf8); - db_unlock(); - - directory_set_stat(directory, &st); - return directory; -} - -static struct directory * -directory_make_uri_parent_checked(const char *uri) -{ - struct directory *directory = db_get_root(); - char *duplicated = g_strdup(uri); - char *name_utf8 = duplicated, *slash; - - while ((slash = strchr(name_utf8, '/')) != NULL) { - *slash = 0; - - if (*name_utf8 == 0) - continue; - - directory = directory_make_child_checked(directory, name_utf8); - if (directory == NULL) - break; - - name_utf8 = slash + 1; - } - - g_free(duplicated); - return directory; -} - -static void -update_uri(const char *uri) -{ - struct directory *parent = directory_make_uri_parent_checked(uri); - if (parent == NULL) - return; - - char *name = g_path_get_basename(uri); - - struct stat st; - if (!skip_symlink(parent, name) && - stat_directory_child(parent, name, &st) == 0) - update_directory_child(parent, name, &st); - else - modified |= delete_name_in(parent, name); - - g_free(name); -} - -bool -update_walk(const char *path, bool discard) -{ - walk_discard = discard; - modified = false; - - if (path != NULL && !isRootDirectory(path)) { - update_uri(path); - } else { - struct directory *directory = db_get_root(); - struct stat st; - - if (stat_directory(directory, &st) == 0) - update_directory(directory, &st); - } - - return modified; -} diff --git a/src/update_walk.h b/src/update_walk.h deleted file mode 100644 index ab1e41fb9..000000000 --- a/src/update_walk.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2003-2012 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_WALK_H -#define MPD_UPDATE_WALK_H - -#include "check.h" - -#include - -void -update_walk_global_init(void); - -void -update_walk_global_finish(void); - -/** - * Returns true if the database was modified. - */ -bool -update_walk(const char *path, bool discard); - -#endif diff --git a/test/RunInotify.cxx b/test/RunInotify.cxx new file mode 100644 index 000000000..491cddb78 --- /dev/null +++ b/test/RunInotify.cxx @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "InotifySource.hxx" + +#include + +#include +#include +#include + +static GMainLoop *main_loop; + +static void +exit_signal_handler(G_GNUC_UNUSED int signum) +{ + g_main_loop_quit(main_loop); +} + +enum { + IN_MASK = IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF + |IN_MOVE|IN_MOVE_SELF +#ifdef IN_ONLYDIR + |IN_ONLYDIR +#endif +}; + +static void +my_inotify_callback(G_GNUC_UNUSED int wd, unsigned mask, + const char *name, G_GNUC_UNUSED void *ctx) +{ + g_print("mask=0x%x name='%s'\n", mask, name); +} + +int main(int argc, char **argv) +{ + GError *error = NULL; + const char *path; + + if (argc != 2) { + g_printerr("Usage: run_inotify PATH\n"); + return 1; + } + + path = argv[1]; + + struct mpd_inotify_source *source = + mpd_inotify_source_new(my_inotify_callback, NULL, + &error); + if (source == NULL) { + g_warning("%s", error->message); + g_error_free(error); + return 2; + } + + int descriptor = mpd_inotify_source_add(source, path, + IN_MASK, &error); + if (descriptor < 0) { + mpd_inotify_source_free(source); + g_warning("%s", error->message); + g_error_free(error); + return 2; + } + + main_loop = g_main_loop_new(NULL, false); + + struct sigaction sa; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sa.sa_handler = exit_signal_handler; + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + + g_main_loop_run(main_loop); + g_main_loop_unref(main_loop); + + mpd_inotify_source_free(source); +} diff --git a/test/run_inotify.c b/test/run_inotify.c deleted file mode 100644 index 4ceb60229..000000000 --- a/test/run_inotify.c +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "inotify_source.h" - -#include - -#include -#include -#include - -static GMainLoop *main_loop; - -static void -exit_signal_handler(G_GNUC_UNUSED int signum) -{ - g_main_loop_quit(main_loop); -} - -enum { - IN_MASK = IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF - |IN_MOVE|IN_MOVE_SELF -#ifdef IN_ONLYDIR - |IN_ONLYDIR -#endif -}; - -static void -my_inotify_callback(G_GNUC_UNUSED int wd, unsigned mask, - const char *name, G_GNUC_UNUSED void *ctx) -{ - g_print("mask=0x%x name='%s'\n", mask, name); -} - -int main(int argc, char **argv) -{ - GError *error = NULL; - const char *path; - - if (argc != 2) { - g_printerr("Usage: run_inotify PATH\n"); - return 1; - } - - path = argv[1]; - - struct mpd_inotify_source *source = - mpd_inotify_source_new(my_inotify_callback, NULL, - &error); - if (source == NULL) { - g_warning("%s", error->message); - g_error_free(error); - return 2; - } - - int descriptor = mpd_inotify_source_add(source, path, - IN_MASK, &error); - if (descriptor < 0) { - mpd_inotify_source_free(source); - g_warning("%s", error->message); - g_error_free(error); - return 2; - } - - main_loop = g_main_loop_new(NULL, false); - - struct sigaction sa; - sa.sa_flags = 0; - sigemptyset(&sa.sa_mask); - sa.sa_handler = exit_signal_handler; - sigaction(SIGINT, &sa, NULL); - sigaction(SIGTERM, &sa, NULL); - - g_main_loop_run(main_loop); - g_main_loop_unref(main_loop); - - mpd_inotify_source_free(source); -} -- cgit v1.2.3