aboutsummaryrefslogtreecommitdiffstats
path: root/src/db/update
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/db/update/Archive.cxx167
-rw-r--r--src/db/update/Container.cxx132
-rw-r--r--src/db/update/Editor.cxx120
-rw-r--r--src/db/update/Editor.hxx70
-rw-r--r--src/db/update/ExcludeList.cxx93
-rw-r--r--src/db/update/ExcludeList.hxx92
-rw-r--r--src/db/update/InotifyDomain.cxx23
-rw-r--r--src/db/update/InotifyDomain.hxx25
-rw-r--r--src/db/update/InotifyQueue.cxx89
-rw-r--r--src/db/update/InotifyQueue.hxx46
-rw-r--r--src/db/update/InotifySource.cxx114
-rw-r--r--src/db/update/InotifySource.hxx74
-rw-r--r--src/db/update/InotifyUpdate.cxx344
-rw-r--r--src/db/update/InotifyUpdate.hxx37
-rw-r--r--src/db/update/Queue.cxx67
-rw-r--r--src/db/update/Queue.hxx77
-rw-r--r--src/db/update/Remove.cxx68
-rw-r--r--src/db/update/Remove.hxx61
-rw-r--r--src/db/update/Service.cxx279
-rw-r--r--src/db/update/Service.hxx115
-rw-r--r--src/db/update/UpdateDomain.cxx23
-rw-r--r--src/db/update/UpdateDomain.hxx25
-rw-r--r--src/db/update/UpdateIO.cxx106
-rw-r--r--src/db/update/UpdateIO.hxx62
-rw-r--r--src/db/update/UpdateSong.cxx103
-rw-r--r--src/db/update/Walk.cxx495
-rw-r--r--src/db/update/Walk.hxx156
27 files changed, 3063 insertions, 0 deletions
diff --git a/src/db/update/Archive.cxx b/src/db/update/Archive.cxx
new file mode 100644
index 000000000..fc8f1fcbf
--- /dev/null
+++ b/src/db/update/Archive.cxx
@@ -0,0 +1,167 @@
+/*
+ * Copyright (C) 2003-2014 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 "Walk.hxx"
+#include "UpdateDomain.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/plugins/simple/Directory.hxx"
+#include "db/plugins/simple/Song.hxx"
+#include "storage/StorageInterface.hxx"
+#include "fs/AllocatedPath.hxx"
+#include "storage/FileInfo.hxx"
+#include "archive/ArchiveList.hxx"
+#include "archive/ArchivePlugin.hxx"
+#include "archive/ArchiveFile.hxx"
+#include "archive/ArchiveVisitor.hxx"
+#include "util/Error.hxx"
+#include "Log.hxx"
+
+#include <string>
+
+#include <sys/stat.h>
+#include <string.h>
+
+void
+UpdateWalk::UpdateArchiveTree(Directory &directory, const char *name)
+{
+ const char *tmp = strchr(name, '/');
+ if (tmp) {
+ const std::string child_name(name, tmp);
+ //add dir is not there already
+ db_lock();
+ Directory *subdir =
+ directory.MakeChild(child_name.c_str());
+ subdir->device = DEVICE_INARCHIVE;
+ db_unlock();
+
+ //create directories first
+ UpdateArchiveTree(*subdir, tmp + 1);
+ } else {
+ if (strlen(name) == 0) {
+ LogWarning(update_domain,
+ "archive returned directory only");
+ return;
+ }
+
+ //add file
+ db_lock();
+ Song *song = directory.FindSong(name);
+ db_unlock();
+ if (song == nullptr) {
+ song = Song::LoadFile(storage, name, directory);
+ if (song != nullptr) {
+ db_lock();
+ directory.AddSong(song);
+ db_unlock();
+
+ modified = true;
+ FormatDefault(update_domain, "added %s/%s",
+ directory.GetPath(), name);
+ }
+ }
+ }
+}
+
+class UpdateArchiveVisitor final : public ArchiveVisitor {
+ UpdateWalk &walk;
+ Directory *directory;
+
+ public:
+ UpdateArchiveVisitor(UpdateWalk &_walk, Directory *_directory)
+ :walk(_walk), directory(_directory) {}
+
+ virtual void VisitArchiveEntry(const char *path_utf8) override {
+ FormatDebug(update_domain,
+ "adding archive file: %s", path_utf8);
+ walk.UpdateArchiveTree(*directory, path_utf8);
+ }
+};
+
+/**
+ * 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
+ */
+void
+UpdateWalk::UpdateArchiveFile(Directory &parent, const char *name,
+ const FileInfo &info,
+ const ArchivePlugin &plugin)
+{
+ db_lock();
+ Directory *directory = parent.FindChild(name);
+ db_unlock();
+
+ if (directory != nullptr && directory->mtime == info.mtime &&
+ !walk_discard)
+ /* MPD has already scanned the archive, and it hasn't
+ changed since - don't consider updating it */
+ return;
+
+ const auto path_fs = storage.MapChildFS(parent.GetPath(), name);
+ if (path_fs.IsNull())
+ /* not a local file: skip, because the archive API
+ supports only local files */
+ return;
+
+ /* open archive */
+ Error error;
+ ArchiveFile *file = archive_file_open(&plugin, path_fs, error);
+ if (file == nullptr) {
+ LogError(error);
+ if (directory != nullptr)
+ editor.LockDeleteDirectory(directory);
+ return;
+ }
+
+ FormatDebug(update_domain, "archive %s opened", path_fs.c_str());
+
+ if (directory == nullptr) {
+ FormatDebug(update_domain,
+ "creating archive directory: %s", name);
+ db_lock();
+ directory = parent.CreateChild(name);
+ /* mark this directory as archive (we use device for
+ this) */
+ directory->device = DEVICE_INARCHIVE;
+ db_unlock();
+ }
+
+ directory->mtime = info.mtime;
+
+ UpdateArchiveVisitor visitor(*this, directory);
+ file->Visit(visitor);
+ file->Close();
+}
+
+bool
+UpdateWalk::UpdateArchiveFile(Directory &directory,
+ const char *name, const char *suffix,
+ const FileInfo &info)
+{
+ const ArchivePlugin *plugin = archive_plugin_from_suffix(suffix);
+ if (plugin == nullptr)
+ return false;
+
+ UpdateArchiveFile(directory, name, info, *plugin);
+ return true;
+}
diff --git a/src/db/update/Container.cxx b/src/db/update/Container.cxx
new file mode 100644
index 000000000..1c420fa99
--- /dev/null
+++ b/src/db/update/Container.cxx
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2003-2014 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 "Walk.hxx"
+#include "UpdateDomain.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/plugins/simple/Directory.hxx"
+#include "db/plugins/simple/Song.hxx"
+#include "storage/StorageInterface.hxx"
+#include "decoder/DecoderPlugin.hxx"
+#include "decoder/DecoderList.hxx"
+#include "fs/AllocatedPath.hxx"
+#include "storage/FileInfo.hxx"
+#include "tag/TagHandler.hxx"
+#include "tag/TagBuilder.hxx"
+#include "Log.hxx"
+
+#include <sys/stat.h>
+
+Directory *
+UpdateWalk::MakeDirectoryIfModified(Directory &parent, const char *name,
+ const FileInfo &info)
+{
+ Directory *directory = parent.FindChild(name);
+
+ // directory exists already
+ if (directory != nullptr) {
+ if (directory->IsMount())
+ return nullptr;
+
+ if (directory->mtime == info.mtime && !walk_discard) {
+ /* not modified */
+ return nullptr;
+ }
+
+ editor.DeleteDirectory(directory);
+ modified = true;
+ }
+
+ directory = parent.MakeChild(name);
+ directory->mtime = info.mtime;
+ return directory;
+}
+
+static bool
+SupportsContainerSuffix(const DecoderPlugin &plugin, const char *suffix)
+{
+ return plugin.container_scan != nullptr &&
+ plugin.SupportsSuffix(suffix);
+}
+
+bool
+UpdateWalk::UpdateContainerFile(Directory &directory,
+ const char *name, const char *suffix,
+ const FileInfo &info)
+{
+ const DecoderPlugin *_plugin = decoder_plugins_find([suffix](const DecoderPlugin &plugin){
+ return SupportsContainerSuffix(plugin, suffix);
+ });
+ if (_plugin == nullptr)
+ return false;
+ const DecoderPlugin &plugin = *_plugin;
+
+ db_lock();
+ Directory *contdir = MakeDirectoryIfModified(directory, name, info);
+ if (contdir == nullptr) {
+ /* not modified */
+ db_unlock();
+ return true;
+ }
+
+ contdir->device = DEVICE_CONTAINER;
+ db_unlock();
+
+ const auto pathname = storage.MapFS(contdir->GetPath());
+ if (pathname.IsNull()) {
+ /* not a local file: skip, because the container API
+ supports only local files */
+ editor.LockDeleteDirectory(contdir);
+ return false;
+ }
+
+ char *vtrack;
+ unsigned int tnum = 0;
+ TagBuilder tag_builder;
+ while ((vtrack = plugin.container_scan(pathname, ++tnum)) != nullptr) {
+ Song *song = Song::NewFile(vtrack, *contdir);
+
+ // shouldn't be necessary but it's there..
+ song->mtime = info.mtime;
+
+ const auto child_path_fs = AllocatedPath::Build(pathname,
+ vtrack);
+ plugin.ScanFile(child_path_fs,
+ add_tag_handler, &tag_builder);
+
+ tag_builder.Commit(song->tag);
+
+ db_lock();
+ contdir->AddSong(song);
+ db_unlock();
+
+ modified = true;
+
+ FormatDefault(update_domain, "added %s/%s",
+ directory.GetPath(), vtrack);
+ delete[] vtrack;
+ }
+
+ if (tnum == 1) {
+ editor.LockDeleteDirectory(contdir);
+ return false;
+ } else
+ return true;
+}
diff --git a/src/db/update/Editor.cxx b/src/db/update/Editor.cxx
new file mode 100644
index 000000000..c3c1a23d6
--- /dev/null
+++ b/src/db/update/Editor.cxx
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2003-2014 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 "Editor.hxx"
+#include "Remove.hxx"
+#include "db/PlaylistVector.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/plugins/simple/Directory.hxx"
+#include "db/plugins/simple/Song.hxx"
+
+#include <assert.h>
+#include <stddef.h>
+
+void
+DatabaseEditor::DeleteSong(Directory &dir, Song *del)
+{
+ assert(del->parent == &dir);
+
+ /* first, prevent traversers in main task from getting this */
+ dir.RemoveSong(del);
+
+ db_unlock(); /* temporary unlock, because update_remove_song() blocks */
+
+ /* now take it out of the playlist (in the main_task) */
+ remove.Remove(del);
+
+ /* finally, all possible references gone, free it */
+ del->Free();
+
+ db_lock();
+}
+
+void
+DatabaseEditor::LockDeleteSong(Directory &parent, Song *song)
+{
+ db_lock();
+ DeleteSong(parent, song);
+ db_unlock();
+}
+
+/**
+ * Recursively remove all sub directories and songs from a directory,
+ * leaving an empty directory.
+ *
+ * Caller must lock the #db_mutex.
+ */
+inline void
+DatabaseEditor::ClearDirectory(Directory &directory)
+{
+ Directory *child, *n;
+ directory_for_each_child_safe(child, n, directory)
+ DeleteDirectory(child);
+
+ Song *song, *ns;
+ directory_for_each_song_safe(song, ns, directory) {
+ assert(song->parent == &directory);
+ DeleteSong(directory, song);
+ }
+}
+
+void
+DatabaseEditor::DeleteDirectory(Directory *directory)
+{
+ assert(directory->parent != nullptr);
+
+ ClearDirectory(*directory);
+
+ directory->Delete();
+}
+
+void
+DatabaseEditor::LockDeleteDirectory(Directory *directory)
+{
+ db_lock();
+ DeleteDirectory(directory);
+ db_unlock();
+}
+
+bool
+DatabaseEditor::DeleteNameIn(Directory &parent, const char *name)
+{
+ bool modified = false;
+
+ db_lock();
+ Directory *directory = parent.FindChild(name);
+
+ if (directory != nullptr) {
+ DeleteDirectory(directory);
+ modified = true;
+ }
+
+ Song *song = parent.FindSong(name);
+ if (song != nullptr) {
+ DeleteSong(parent, song);
+ modified = true;
+ }
+
+ parent.playlists.erase(name);
+
+ db_unlock();
+
+ return modified;
+}
diff --git a/src/db/update/Editor.hxx b/src/db/update/Editor.hxx
new file mode 100644
index 000000000..fc08c2659
--- /dev/null
+++ b/src/db/update/Editor.hxx
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2003-2014 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"
+#include "Remove.hxx"
+
+struct Directory;
+struct Song;
+class UpdateRemoveService;
+
+class DatabaseEditor final {
+ UpdateRemoveService remove;
+
+public:
+ DatabaseEditor(EventLoop &_loop, DatabaseListener &_listener)
+ :remove(_loop, _listener) {}
+
+ /**
+ * Caller must lock the #db_mutex.
+ */
+ void DeleteSong(Directory &parent, Song *song);
+
+ /**
+ * DeleteSong() with automatic locking.
+ */
+ void LockDeleteSong(Directory &parent, Song *song);
+
+ /**
+ * Recursively free a directory and all its contents.
+ *
+ * Caller must lock the #db_mutex.
+ */
+ void DeleteDirectory(Directory *directory);
+
+ /**
+ * DeleteDirectory() with automatic locking.
+ */
+ void LockDeleteDirectory(Directory *directory);
+
+ /**
+ * Caller must NOT lock the #db_mutex.
+ *
+ * @return true if the database was modified
+ */
+ bool DeleteNameIn(Directory &parent, const char *name);
+
+private:
+ void ClearDirectory(Directory &directory);
+};
+
+#endif
diff --git a/src/db/update/ExcludeList.cxx b/src/db/update/ExcludeList.cxx
new file mode 100644
index 000000000..cf92ac8f7
--- /dev/null
+++ b/src/db/update/ExcludeList.cxx
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2003-2014 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.
+ */
+
+/*
+ * The .mpdignore backend code.
+ *
+ */
+
+#include "config.h"
+#include "ExcludeList.hxx"
+#include "fs/Path.hxx"
+#include "fs/FileSystem.hxx"
+#include "util/StringUtil.hxx"
+#include "util/Domain.hxx"
+#include "Log.hxx"
+
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+
+static constexpr Domain exclude_list_domain("exclude_list");
+
+bool
+ExcludeList::LoadFile(Path path_fs)
+{
+#ifdef HAVE_GLIB
+ FILE *file = FOpen(path_fs, FOpenMode::ReadText);
+ if (file == nullptr) {
+ const int e = errno;
+ if (e != ENOENT) {
+ const auto path_utf8 = path_fs.ToUTF8();
+ FormatErrno(exclude_list_domain,
+ "Failed to open %s",
+ path_utf8.c_str());
+ }
+
+ return false;
+ }
+
+ char line[1024];
+ while (fgets(line, sizeof(line), file) != nullptr) {
+ char *p = strchr(line, '#');
+ if (p != nullptr)
+ *p = 0;
+
+ p = Strip(line);
+ if (*p != 0)
+ patterns.emplace_front(p);
+ }
+
+ fclose(file);
+#else
+ // TODO: implement
+ (void)path_fs;
+#endif
+
+ return true;
+}
+
+bool
+ExcludeList::Check(Path name_fs) const
+{
+ assert(!name_fs.IsNull());
+
+ /* XXX include full path name in check */
+
+#ifdef HAVE_GLIB
+ for (const auto &i : patterns)
+ if (i.Check(name_fs.c_str()))
+ return true;
+#else
+ // TODO: implement
+ (void)name_fs;
+#endif
+
+ return false;
+}
diff --git a/src/db/update/ExcludeList.hxx b/src/db/update/ExcludeList.hxx
new file mode 100644
index 000000000..ef6c4d53e
--- /dev/null
+++ b/src/db/update/ExcludeList.hxx
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2003-2014 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.
+ */
+
+/*
+ * The .mpdignore backend code.
+ *
+ */
+
+#ifndef MPD_EXCLUDE_H
+#define MPD_EXCLUDE_H
+
+#include "check.h"
+#include "Compiler.h"
+
+#include <forward_list>
+
+#ifdef HAVE_GLIB
+#include <glib.h>
+#endif
+
+class Path;
+
+class ExcludeList {
+#ifdef HAVE_GLIB
+ class Pattern {
+ GPatternSpec *pattern;
+
+ public:
+ Pattern(const char *_pattern)
+ :pattern(g_pattern_spec_new(_pattern)) {}
+
+ Pattern(Pattern &&other)
+ :pattern(other.pattern) {
+ other.pattern = nullptr;
+ }
+
+ ~Pattern() {
+ g_pattern_spec_free(pattern);
+ }
+
+ gcc_pure
+ bool Check(const char *name_fs) const {
+ return g_pattern_match_string(pattern, name_fs);
+ }
+ };
+
+ std::forward_list<Pattern> patterns;
+#else
+ // TODO: implement
+#endif
+
+public:
+ gcc_pure
+ bool IsEmpty() const {
+#ifdef HAVE_GLIB
+ return patterns.empty();
+#else
+ // TODO: implement
+ return true;
+#endif
+ }
+
+ /**
+ * Loads and parses a .mpdignore file.
+ */
+ bool LoadFile(Path path_fs);
+
+ /**
+ * Checks whether one of the patterns in the .mpdignore file matches
+ * the specified file name.
+ */
+ bool Check(Path name_fs) const;
+};
+
+
+#endif
diff --git a/src/db/update/InotifyDomain.cxx b/src/db/update/InotifyDomain.cxx
new file mode 100644
index 000000000..4a3ab2d79
--- /dev/null
+++ b/src/db/update/InotifyDomain.cxx
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2003-2014 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 "InotifyDomain.hxx"
+#include "util/Domain.hxx"
+
+const Domain inotify_domain("inotify");
diff --git a/src/db/update/InotifyDomain.hxx b/src/db/update/InotifyDomain.hxx
new file mode 100644
index 000000000..ad6202361
--- /dev/null
+++ b/src/db/update/InotifyDomain.hxx
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2003-2014 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_DOMAIN_HXX
+#define MPD_INOTIFY_DOMAIN_HXX
+
+extern const class Domain inotify_domain;
+
+#endif
diff --git a/src/db/update/InotifyQueue.cxx b/src/db/update/InotifyQueue.cxx
new file mode 100644
index 000000000..013200f98
--- /dev/null
+++ b/src/db/update/InotifyQueue.cxx
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2003-2014 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 "InotifyDomain.hxx"
+#include "Service.hxx"
+#include "Log.hxx"
+
+#include <string.h>
+
+/**
+ * Wait this long after the last change before calling
+ * update_enqueue(). This increases the probability that updates can
+ * be bundled.
+ */
+static constexpr unsigned INOTIFY_UPDATE_DELAY_S = 5;
+
+void
+InotifyQueue::OnTimeout()
+{
+ unsigned id;
+
+ while (!queue.empty()) {
+ const char *uri_utf8 = queue.front().c_str();
+
+ id = update.Enqueue(uri_utf8, false);
+ if (id == 0) {
+ /* retry later */
+ ScheduleSeconds(INOTIFY_UPDATE_DELAY_S);
+ return;
+ }
+
+ FormatDebug(inotify_domain, "updating '%s' job=%u",
+ uri_utf8, id);
+
+ queue.pop_front();
+ }
+}
+
+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
+InotifyQueue::Enqueue(const char *uri_utf8)
+{
+ ScheduleSeconds(INOTIFY_UPDATE_DELAY_S);
+
+ for (auto i = queue.begin(), end = queue.end(); i != end;) {
+ const char *current_uri = i->c_str();
+
+ if (path_in(uri_utf8, current_uri))
+ /* already enqueued */
+ return;
+
+ 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 */
+ i = queue.erase(i);
+ else
+ ++i;
+ }
+
+ queue.emplace_back(uri_utf8);
+}
diff --git a/src/db/update/InotifyQueue.hxx b/src/db/update/InotifyQueue.hxx
new file mode 100644
index 000000000..a9abc2969
--- /dev/null
+++ b/src/db/update/InotifyQueue.hxx
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2003-2014 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
+
+#include "event/TimeoutMonitor.hxx"
+#include "Compiler.h"
+
+#include <list>
+#include <string>
+
+class UpdateService;
+
+class InotifyQueue final : private TimeoutMonitor {
+ UpdateService &update;
+
+ std::list<std::string> queue;
+
+public:
+ InotifyQueue(EventLoop &_loop, UpdateService &_update)
+ :TimeoutMonitor(_loop), update(_update) {}
+
+ void Enqueue(const char *uri_utf8);
+
+private:
+ virtual void OnTimeout() override;
+};
+
+#endif
diff --git a/src/db/update/InotifySource.cxx b/src/db/update/InotifySource.cxx
new file mode 100644
index 000000000..c2783690e
--- /dev/null
+++ b/src/db/update/InotifySource.cxx
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2003-2014 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 "InotifyDomain.hxx"
+#include "util/Error.hxx"
+#include "system/fd_util.h"
+#include "system/FatalError.hxx"
+#include "Log.hxx"
+
+#include <sys/inotify.h>
+#include <unistd.h>
+#include <errno.h>
+
+bool
+InotifySource::OnSocketReady(gcc_unused unsigned flags)
+{
+ const auto dest = buffer.Write();
+ if (dest.IsEmpty())
+ FatalError("buffer full");
+
+ ssize_t nbytes = read(Get(), dest.data, dest.size);
+ if (nbytes < 0)
+ FatalSystemError("Failed to read from inotify");
+ if (nbytes == 0)
+ FatalError("end of file from inotify");
+
+ buffer.Append(nbytes);
+
+ while (true) {
+ const char *name;
+
+ auto range = buffer.Read();
+ const struct inotify_event *event =
+ (const struct inotify_event *)
+ range.data;
+ if (range.size < sizeof(*event) ||
+ range.size < sizeof(*event) + event->len)
+ break;
+
+ if (event->len > 0 && event->name[event->len - 1] == 0)
+ name = event->name;
+ else
+ name = nullptr;
+
+ callback(event->wd, event->mask, name, callback_ctx);
+ buffer.Consume(sizeof(*event) + event->len);
+ }
+
+ return true;
+}
+
+inline
+InotifySource::InotifySource(EventLoop &_loop,
+ mpd_inotify_callback_t _callback, void *_ctx,
+ int _fd)
+ :SocketMonitor(_fd, _loop),
+ callback(_callback), callback_ctx(_ctx)
+{
+ ScheduleRead();
+
+}
+
+InotifySource *
+InotifySource::Create(EventLoop &loop,
+ mpd_inotify_callback_t callback, void *callback_ctx,
+ Error &error)
+{
+ int fd = inotify_init_cloexec();
+ if (fd < 0) {
+ error.SetErrno("inotify_init() has failed");
+ return nullptr;
+ }
+
+ return new InotifySource(loop, callback, callback_ctx, fd);
+}
+
+int
+InotifySource::Add(const char *path_fs, unsigned mask, Error &error)
+{
+ int wd = inotify_add_watch(Get(), path_fs, mask);
+ if (wd < 0)
+ error.SetErrno("inotify_add_watch() has failed");
+
+ return wd;
+}
+
+void
+InotifySource::Remove(unsigned wd)
+{
+ int ret = inotify_rm_watch(Get(), wd);
+ if (ret < 0 && errno != EINVAL)
+ LogErrno(inotify_domain, "inotify_rm_watch() has failed");
+
+ /* EINVAL may happen here when the file has been deleted; the
+ kernel seems to auto-unregister deleted files */
+}
diff --git a/src/db/update/InotifySource.hxx b/src/db/update/InotifySource.hxx
new file mode 100644
index 000000000..77c11093c
--- /dev/null
+++ b/src/db/update/InotifySource.hxx
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2003-2014 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 "event/SocketMonitor.hxx"
+#include "util/FifoBuffer.hxx"
+
+class Error;
+
+typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask,
+ const char *name, void *ctx);
+
+class InotifySource final : private SocketMonitor {
+ mpd_inotify_callback_t callback;
+ void *callback_ctx;
+
+ FifoBuffer<uint8_t, 4096> buffer;
+
+ InotifySource(EventLoop &_loop,
+ mpd_inotify_callback_t callback, void *ctx, int fd);
+
+public:
+ ~InotifySource() {
+ Close();
+ }
+
+ /**
+ * Creates a new inotify source and registers it in the GLib main
+ * loop.
+ *
+ * @param a callback invoked for events received from the kernel
+ */
+ static InotifySource *Create(EventLoop &_loop,
+ mpd_inotify_callback_t callback,
+ void *ctx,
+ Error &error);
+
+ /**
+ * Adds a path to the notify list.
+ *
+ * @return a watch descriptor or -1 on error
+ */
+ int Add(const char *path_fs, unsigned mask, Error &error);
+
+ /**
+ * Removes a path from the notify list.
+ *
+ * @param wd the watch descriptor returned by mpd_inotify_source_add()
+ */
+ void Remove(unsigned wd);
+
+private:
+ virtual bool OnSocketReady(unsigned flags) override;
+};
+
+#endif
diff --git a/src/db/update/InotifyUpdate.cxx b/src/db/update/InotifyUpdate.cxx
new file mode 100644
index 000000000..26bdddf8d
--- /dev/null
+++ b/src/db/update/InotifyUpdate.cxx
@@ -0,0 +1,344 @@
+/*
+ * Copyright (C) 2003-2014 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 "InotifyDomain.hxx"
+#include "storage/StorageInterface.hxx"
+#include "fs/AllocatedPath.hxx"
+#include "fs/FileSystem.hxx"
+#include "util/Error.hxx"
+#include "Log.hxx"
+
+#include <string>
+#include <map>
+#include <forward_list>
+
+#include <assert.h>
+#include <sys/inotify.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <dirent.h>
+
+static constexpr unsigned IN_MASK =
+#ifdef IN_ONLYDIR
+ IN_ONLYDIR|
+#endif
+ IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF
+ |IN_MOVE|IN_MOVE_SELF;
+
+struct WatchDirectory {
+ WatchDirectory *parent;
+
+ AllocatedPath name;
+
+ int descriptor;
+
+ std::forward_list<WatchDirectory> children;
+
+ template<typename N>
+ WatchDirectory(WatchDirectory *_parent, N &&_name,
+ int _descriptor)
+ :parent(_parent), name(std::forward<N>(_name)),
+ descriptor(_descriptor) {}
+
+ WatchDirectory(const WatchDirectory &) = delete;
+ WatchDirectory &operator=(const WatchDirectory &) = delete;
+
+ gcc_pure
+ unsigned GetDepth() const;
+
+ gcc_pure
+ AllocatedPath GetUriFS() const;
+};
+
+static InotifySource *inotify_source;
+static InotifyQueue *inotify_queue;
+
+static unsigned inotify_max_depth;
+static WatchDirectory *inotify_root;
+static std::map<int, WatchDirectory *> inotify_directories;
+
+static void
+tree_add_watch_directory(WatchDirectory *directory)
+{
+ inotify_directories.insert(std::make_pair(directory->descriptor,
+ directory));
+}
+
+static void
+tree_remove_watch_directory(WatchDirectory *directory)
+{
+ auto i = inotify_directories.find(directory->descriptor);
+ assert(i != inotify_directories.end());
+ inotify_directories.erase(i);
+}
+
+static WatchDirectory *
+tree_find_watch_directory(int wd)
+{
+ auto i = inotify_directories.find(wd);
+ if (i == inotify_directories.end())
+ return nullptr;
+
+ return i->second;
+}
+
+static void
+disable_watch_directory(WatchDirectory &directory)
+{
+ tree_remove_watch_directory(&directory);
+
+ for (WatchDirectory &child : directory.children)
+ disable_watch_directory(child);
+
+ inotify_source->Remove(directory.descriptor);
+}
+
+static void
+remove_watch_directory(WatchDirectory *directory)
+{
+ assert(directory != nullptr);
+
+ if (directory->parent == nullptr) {
+ LogWarning(inotify_domain,
+ "music directory was removed - "
+ "cannot continue to watch it");
+ return;
+ }
+
+ disable_watch_directory(*directory);
+
+ /* remove it from the parent, which effectively deletes it */
+ directory->parent->children.remove_if([directory](const WatchDirectory &child){
+ return &child == directory;
+ });
+}
+
+AllocatedPath
+WatchDirectory::GetUriFS() const
+{
+ if (parent == nullptr)
+ return AllocatedPath::Null();
+
+ const auto uri = parent->GetUriFS();
+ if (uri.IsNull())
+ return name;
+
+ return AllocatedPath::Build(uri, name);
+}
+
+/* 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') != nullptr;
+}
+
+static void
+recursive_watch_subdirectories(WatchDirectory *directory,
+ const AllocatedPath &path_fs, unsigned depth)
+{
+ Error error;
+ DIR *dir;
+ struct dirent *ent;
+
+ assert(directory != nullptr);
+ assert(depth <= inotify_max_depth);
+ assert(!path_fs.IsNull());
+
+ ++depth;
+
+ if (depth > inotify_max_depth)
+ return;
+
+ dir = opendir(path_fs.c_str());
+ if (dir == nullptr) {
+ FormatErrno(inotify_domain,
+ "Failed to open directory %s", path_fs.c_str());
+ return;
+ }
+
+ while ((ent = readdir(dir))) {
+ struct stat st;
+ int ret;
+
+ if (skip_path(ent->d_name))
+ continue;
+
+ const auto child_path_fs =
+ AllocatedPath::Build(path_fs, ent->d_name);
+ ret = StatFile(child_path_fs, st);
+ if (ret < 0) {
+ FormatErrno(inotify_domain,
+ "Failed to stat %s",
+ child_path_fs.c_str());
+ continue;
+ }
+
+ if (!S_ISDIR(st.st_mode))
+ continue;
+
+ ret = inotify_source->Add(child_path_fs.c_str(), IN_MASK,
+ error);
+ if (ret < 0) {
+ FormatError(error,
+ "Failed to register %s",
+ child_path_fs.c_str());
+ error.Clear();
+ continue;
+ }
+
+ WatchDirectory *child = tree_find_watch_directory(ret);
+ if (child != nullptr)
+ /* already being watched */
+ continue;
+
+ directory->children.emplace_front(directory,
+ AllocatedPath::FromFS(ent->d_name),
+ ret);
+ child = &directory->children.front();
+
+ tree_add_watch_directory(child);
+
+ recursive_watch_subdirectories(child, child_path_fs, depth);
+ }
+
+ closedir(dir);
+}
+
+gcc_pure
+unsigned
+WatchDirectory::GetDepth() const
+{
+ const WatchDirectory *d = this;
+ unsigned depth = 0;
+ while ((d = d->parent) != nullptr)
+ ++depth;
+
+ return depth;
+}
+
+static void
+mpd_inotify_callback(int wd, unsigned mask,
+ gcc_unused const char *name, gcc_unused void *ctx)
+{
+ WatchDirectory *directory;
+
+ /*FormatDebug(inotify_domain, "wd=%d mask=0x%x name='%s'", wd, mask, name);*/
+
+ directory = tree_find_watch_directory(wd);
+ if (directory == nullptr)
+ return;
+
+ const auto uri_fs = directory->GetUriFS();
+
+ if ((mask & (IN_DELETE_SELF|IN_MOVE_SELF)) != 0) {
+ 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 auto &root = inotify_root->name;
+
+ const auto path_fs = uri_fs.IsNull()
+ ? root
+ : AllocatedPath::Build(root, uri_fs.c_str());
+
+ recursive_watch_subdirectories(directory, path_fs,
+ directory->GetDepth());
+ }
+
+ if ((mask & (IN_CLOSE_WRITE|IN_MOVE|IN_DELETE)) != 0 ||
+ /* at the maximum depth, we watch out for newly created
+ directories */
+ (directory->GetDepth() == 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 */
+
+ if (!uri_fs.IsNull()) {
+ const std::string uri_utf8 = uri_fs.ToUTF8();
+ if (!uri_utf8.empty())
+ inotify_queue->Enqueue(uri_utf8.c_str());
+ }
+ else
+ inotify_queue->Enqueue("");
+ }
+}
+
+void
+mpd_inotify_init(EventLoop &loop, Storage &storage, UpdateService &update,
+ unsigned max_depth)
+{
+ LogDebug(inotify_domain, "initializing inotify");
+
+ const auto path = storage.MapFS("");
+ if (path.IsNull()) {
+ LogDebug(inotify_domain, "no music directory configured");
+ return;
+ }
+
+ Error error;
+ inotify_source = InotifySource::Create(loop,
+ mpd_inotify_callback, nullptr,
+ error);
+ if (inotify_source == nullptr) {
+ LogError(error);
+ return;
+ }
+
+ inotify_max_depth = max_depth;
+
+ int descriptor = inotify_source->Add(path.c_str(), IN_MASK, error);
+ if (descriptor < 0) {
+ LogError(error);
+ delete inotify_source;
+ inotify_source = nullptr;
+ return;
+ }
+
+ inotify_root = new WatchDirectory(nullptr, path, descriptor);
+
+ tree_add_watch_directory(inotify_root);
+
+ recursive_watch_subdirectories(inotify_root, path, 0);
+
+ inotify_queue = new InotifyQueue(loop, update);
+
+ LogDebug(inotify_domain, "watching music directory");
+}
+
+void
+mpd_inotify_finish(void)
+{
+ if (inotify_source == nullptr)
+ return;
+
+ delete inotify_queue;
+ delete inotify_source;
+ delete inotify_root;
+ inotify_directories.clear();
+}
diff --git a/src/db/update/InotifyUpdate.hxx b/src/db/update/InotifyUpdate.hxx
new file mode 100644
index 000000000..0f78db71f
--- /dev/null
+++ b/src/db/update/InotifyUpdate.hxx
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2003-2014 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"
+#include "Compiler.h"
+
+class EventLoop;
+class Storage;
+class UpdateService;
+
+void
+mpd_inotify_init(EventLoop &loop, Storage &storage, UpdateService &update,
+ unsigned max_depth);
+
+void
+mpd_inotify_finish(void);
+
+#endif
diff --git a/src/db/update/Queue.cxx b/src/db/update/Queue.cxx
new file mode 100644
index 000000000..6d6d80131
--- /dev/null
+++ b/src/db/update/Queue.cxx
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2003-2014 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 "Queue.hxx"
+
+bool
+UpdateQueue::Push(SimpleDatabase &db, Storage &storage,
+ const char *path, bool discard, unsigned id)
+{
+ if (update_queue.size() >= MAX_UPDATE_QUEUE_SIZE)
+ return false;
+
+ update_queue.emplace_back(db, storage, path, discard, id);
+ return true;
+}
+
+UpdateQueueItem
+UpdateQueue::Pop()
+{
+ if (update_queue.empty())
+ return UpdateQueueItem();
+
+ auto i = std::move(update_queue.front());
+ update_queue.pop_front();
+ return i;
+}
+
+void
+UpdateQueue::Erase(SimpleDatabase &db)
+{
+ for (auto i = update_queue.begin(), end = update_queue.end();
+ i != end;) {
+ if (i->db == &db)
+ i = update_queue.erase(i);
+ else
+ ++i;
+ }
+}
+
+void
+UpdateQueue::Erase(Storage &storage)
+{
+ for (auto i = update_queue.begin(), end = update_queue.end();
+ i != end;) {
+ if (i->storage == &storage)
+ i = update_queue.erase(i);
+ else
+ ++i;
+ }
+}
diff --git a/src/db/update/Queue.hxx b/src/db/update/Queue.hxx
new file mode 100644
index 000000000..9064ea481
--- /dev/null
+++ b/src/db/update/Queue.hxx
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2003-2014 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"
+#include "Compiler.h"
+
+#include <string>
+#include <list>
+
+class SimpleDatabase;
+class Storage;
+
+struct UpdateQueueItem {
+ SimpleDatabase *db;
+ Storage *storage;
+
+ std::string path_utf8;
+ unsigned id;
+ bool discard;
+
+ UpdateQueueItem():id(0) {}
+
+ UpdateQueueItem(SimpleDatabase &_db,
+ Storage &_storage,
+ const char *_path, bool _discard,
+ unsigned _id)
+ :db(&_db), storage(&_storage), path_utf8(_path),
+ id(_id), discard(_discard) {}
+
+ bool IsDefined() const {
+ return id != 0;
+ }
+};
+
+class UpdateQueue {
+ static constexpr unsigned MAX_UPDATE_QUEUE_SIZE = 32;
+
+ std::list<UpdateQueueItem> update_queue;
+
+public:
+ gcc_nonnull_all
+ bool Push(SimpleDatabase &db, Storage &storage,
+ const char *path, bool discard, unsigned id);
+
+ UpdateQueueItem Pop();
+
+ void Clear() {
+ update_queue.clear();
+ }
+
+ gcc_nonnull_all
+ void Erase(SimpleDatabase &db);
+
+ gcc_nonnull_all
+ void Erase(Storage &storage);
+};
+
+#endif
diff --git a/src/db/update/Remove.cxx b/src/db/update/Remove.cxx
new file mode 100644
index 000000000..dfada05b2
--- /dev/null
+++ b/src/db/update/Remove.cxx
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2003-2014 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 "Remove.hxx"
+#include "UpdateDomain.hxx"
+#include "db/plugins/simple/Song.hxx"
+#include "db/LightSong.hxx"
+#include "db/DatabaseListener.hxx"
+#include "Log.hxx"
+
+#include <assert.h>
+
+/**
+ * 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.
+ */
+void
+UpdateRemoveService::RunDeferred()
+{
+ assert(removed_song != nullptr);
+
+ {
+ const auto uri = removed_song->GetURI();
+ FormatDefault(update_domain, "removing %s", uri.c_str());
+ }
+
+ listener.OnDatabaseSongRemoved(removed_song->Export());
+
+ /* clear "removed_song" and send signal to update thread */
+ remove_mutex.lock();
+ removed_song = nullptr;
+ remove_cond.signal();
+ remove_mutex.unlock();
+}
+
+void
+UpdateRemoveService::Remove(const Song *song)
+{
+ assert(removed_song == nullptr);
+
+ removed_song = song;
+
+ DeferredMonitor::Schedule();
+
+ remove_mutex.lock();
+
+ while (removed_song != nullptr)
+ remove_cond.wait(remove_mutex);
+
+ remove_mutex.unlock();
+}
diff --git a/src/db/update/Remove.hxx b/src/db/update/Remove.hxx
new file mode 100644
index 000000000..f0457efa1
--- /dev/null
+++ b/src/db/update/Remove.hxx
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2003-2014 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"
+#include "event/DeferredMonitor.hxx"
+#include "thread/Mutex.hxx"
+#include "thread/Cond.hxx"
+
+struct Song;
+class DatabaseListener;
+
+/**
+ * This class handles #Song removal. It defers the action to the main
+ * thread to ensure that all references to the #Song are gone.
+ */
+class UpdateRemoveService final : DeferredMonitor {
+ DatabaseListener &listener;
+
+ Mutex remove_mutex;
+ Cond remove_cond;
+
+ const Song *removed_song;
+
+public:
+ UpdateRemoveService(EventLoop &_loop, DatabaseListener &_listener)
+ :DeferredMonitor(_loop), listener(_listener),
+ removed_song(nullptr){}
+
+ /**
+ * 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 Remove(const Song *song);
+
+private:
+ /* virtual methods from class DeferredMonitor */
+ virtual void RunDeferred() override;
+};
+
+#endif
diff --git a/src/db/update/Service.cxx b/src/db/update/Service.cxx
new file mode 100644
index 000000000..e8a1f6b02
--- /dev/null
+++ b/src/db/update/Service.cxx
@@ -0,0 +1,279 @@
+/*
+ * Copyright (C) 2003-2014 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 "Service.hxx"
+#include "Walk.hxx"
+#include "UpdateDomain.hxx"
+#include "db/DatabaseListener.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
+#include "db/plugins/simple/Directory.hxx"
+#include "storage/CompositeStorage.hxx"
+#include "Idle.hxx"
+#include "util/Error.hxx"
+#include "Log.hxx"
+#include "Instance.hxx"
+#include "system/FatalError.hxx"
+#include "thread/Id.hxx"
+#include "thread/Thread.hxx"
+#include "thread/Util.hxx"
+
+#ifndef NDEBUG
+#include "event/Loop.hxx"
+#endif
+
+#include <assert.h>
+
+UpdateService::UpdateService(EventLoop &_loop, SimpleDatabase &_db,
+ CompositeStorage &_storage,
+ DatabaseListener &_listener)
+ :DeferredMonitor(_loop),
+ db(_db), storage(_storage),
+ listener(_listener),
+ progress(UPDATE_PROGRESS_IDLE),
+ update_task_id(0),
+ walk(nullptr)
+{
+}
+
+UpdateService::~UpdateService()
+{
+ CancelAllAsync();
+
+ if (update_thread.IsDefined())
+ update_thread.Join();
+
+ delete walk;
+}
+
+void
+UpdateService::CancelAllAsync()
+{
+ assert(GetEventLoop().IsInsideOrNull());
+
+ queue.Clear();
+
+ if (walk != nullptr)
+ walk->Cancel();
+}
+
+void
+UpdateService::CancelMount(const char *uri)
+{
+ /* determine which (mounted) database will be updated and what
+ storage will be scanned */
+
+ db_lock();
+ const auto lr = db.GetRoot().LookupDirectory(uri);
+ db_unlock();
+
+ if (!lr.directory->IsMount())
+ return;
+
+ bool cancel_current = false;
+
+ Storage *storage2 = storage.GetMount(uri);
+ if (storage2 != nullptr) {
+ queue.Erase(*storage2);
+ cancel_current = next.IsDefined() && next.storage == storage2;
+ }
+
+ Database &_db2 = *lr.directory->mounted_database;
+ if (_db2.IsPlugin(simple_db_plugin)) {
+ SimpleDatabase &db2 = static_cast<SimpleDatabase &>(_db2);
+ queue.Erase(db2);
+ cancel_current |= next.IsDefined() && next.db == &db2;
+ }
+
+ if (cancel_current && walk != nullptr) {
+ walk->Cancel();
+
+ if (update_thread.IsDefined())
+ update_thread.Join();
+ }
+}
+
+inline void
+UpdateService::Task()
+{
+ assert(walk != nullptr);
+
+ if (!next.path_utf8.empty())
+ FormatDebug(update_domain, "starting: %s",
+ next.path_utf8.c_str());
+ else
+ LogDebug(update_domain, "starting");
+
+ SetThreadIdlePriority();
+
+ modified = walk->Walk(next.db->GetRoot(), next.path_utf8.c_str(),
+ next.discard);
+
+ if (modified || !next.db->FileExists()) {
+ Error error;
+ if (!next.db->Save(error))
+ LogError(error, "Failed to save database");
+ }
+
+ if (!next.path_utf8.empty())
+ FormatDebug(update_domain, "finished: %s",
+ next.path_utf8.c_str());
+ else
+ LogDebug(update_domain, "finished");
+
+ progress = UPDATE_PROGRESS_DONE;
+ DeferredMonitor::Schedule();
+}
+
+void
+UpdateService::Task(void *ctx)
+{
+ UpdateService &service = *(UpdateService *)ctx;
+ return service.Task();
+}
+
+void
+UpdateService::StartThread(UpdateQueueItem &&i)
+{
+ assert(GetEventLoop().IsInsideOrNull());
+ assert(walk == nullptr);
+
+ progress = UPDATE_PROGRESS_RUNNING;
+ modified = false;
+
+ next = std::move(i);
+ walk = new UpdateWalk(GetEventLoop(), listener, *next.storage);
+
+ Error error;
+ if (!update_thread.Start(Task, this, error))
+ FatalError(error);
+
+ FormatDebug(update_domain,
+ "spawned thread for update job id %i", next.id);
+}
+
+unsigned
+UpdateService::GenerateId()
+{
+ unsigned id = update_task_id + 1;
+ if (id > update_task_id_max)
+ id = 1;
+ return id;
+}
+
+unsigned
+UpdateService::Enqueue(const char *path, bool discard)
+{
+ assert(GetEventLoop().IsInsideOrNull());
+
+ /* determine which (mounted) database will be updated and what
+ storage will be scanned */
+ SimpleDatabase *db2;
+ Storage *storage2;
+
+ db_lock();
+ const auto lr = db.GetRoot().LookupDirectory(path);
+ db_unlock();
+ if (lr.directory->IsMount()) {
+ /* follow the mountpoint, update the mounted
+ database */
+
+ Database &_db2 = *lr.directory->mounted_database;
+ if (!_db2.IsPlugin(simple_db_plugin))
+ /* cannot update this type of database */
+ return 0;
+
+ db2 = static_cast<SimpleDatabase *>(&_db2);
+
+ if (lr.uri == nullptr) {
+ storage2 = storage.GetMount(path);
+ path = "";
+ } else {
+ assert(lr.uri > path);
+ assert(lr.uri < path + strlen(path));
+ assert(lr.uri[-1] == '/');
+
+ const std::string mountpoint(path, lr.uri - 1);
+ storage2 = storage.GetMount(mountpoint.c_str());
+ path = lr.uri;
+ }
+ } else {
+ /* use the "root" database/storage */
+
+ db2 = &db;
+ storage2 = storage.GetMount("");
+ }
+
+ if (storage2 == nullptr)
+ /* no storage found at this mount point - should not
+ happen */
+ return 0;
+
+ if (progress != UPDATE_PROGRESS_IDLE) {
+ const unsigned id = GenerateId();
+ if (!queue.Push(*db2, *storage2, path, discard, id))
+ return 0;
+
+ update_task_id = id;
+ return id;
+ }
+
+ const unsigned id = update_task_id = GenerateId();
+ StartThread(UpdateQueueItem(*db2, *storage2, path, discard, id));
+
+ idle_add(IDLE_UPDATE);
+
+ return id;
+}
+
+/**
+ * Called in the main thread after the database update is finished.
+ */
+void
+UpdateService::RunDeferred()
+{
+ assert(progress == UPDATE_PROGRESS_DONE);
+ assert(next.IsDefined());
+ assert(walk != nullptr);
+
+ /* wait for thread to finish only if it wasn't cancelled by
+ CancelMount() */
+ if (update_thread.IsDefined())
+ update_thread.Join();
+
+ delete walk;
+ walk = nullptr;
+
+ next = UpdateQueueItem();
+
+ idle_add(IDLE_UPDATE);
+
+ if (modified)
+ /* send "idle" events */
+ listener.OnDatabaseModified();
+
+ auto i = queue.Pop();
+ if (i.IsDefined()) {
+ /* schedule the next path */
+ StartThread(std::move(i));
+ } else {
+ progress = UPDATE_PROGRESS_IDLE;
+ }
+}
diff --git a/src/db/update/Service.hxx b/src/db/update/Service.hxx
new file mode 100644
index 000000000..cbb4a3f9d
--- /dev/null
+++ b/src/db/update/Service.hxx
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2003-2014 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_SERVICE_HXX
+#define MPD_UPDATE_SERVICE_HXX
+
+#include "check.h"
+#include "Queue.hxx"
+#include "event/DeferredMonitor.hxx"
+#include "thread/Thread.hxx"
+
+class SimpleDatabase;
+class DatabaseListener;
+class UpdateWalk;
+class CompositeStorage;
+
+/**
+ * This class manages the update queue and runs the update thread.
+ */
+class UpdateService final : DeferredMonitor {
+ enum Progress {
+ UPDATE_PROGRESS_IDLE = 0,
+ UPDATE_PROGRESS_RUNNING = 1,
+ UPDATE_PROGRESS_DONE = 2
+ };
+
+ SimpleDatabase &db;
+ CompositeStorage &storage;
+
+ DatabaseListener &listener;
+
+ Progress progress;
+
+ bool modified;
+
+ Thread update_thread;
+
+ static const unsigned update_task_id_max = 1 << 15;
+
+ unsigned update_task_id;
+
+ UpdateQueue queue;
+
+ UpdateQueueItem next;
+
+ UpdateWalk *walk;
+
+public:
+ UpdateService(EventLoop &_loop, SimpleDatabase &_db,
+ CompositeStorage &_storage,
+ DatabaseListener &_listener);
+
+ ~UpdateService();
+
+ /**
+ * Returns a non-zero job id when we are currently updating
+ * the database.
+ */
+ unsigned GetId() const {
+ return next.id;
+ }
+
+ /**
+ * Add this path to the database update queue.
+ *
+ * @param path a path to update; if an empty string,
+ * the whole music directory is updated
+ * @return the job id, or 0 on error
+ */
+ gcc_nonnull_all
+ unsigned Enqueue(const char *path, bool discard);
+
+ /**
+ * Clear the queue and cancel the current update. Does not
+ * wait for the thread to exit.
+ */
+ void CancelAllAsync();
+
+ /**
+ * Cancel all updates for the given mount point. If an update
+ * is already running for it, the method will wait for
+ * cancellation to complete.
+ */
+ void CancelMount(const char *uri);
+
+private:
+ /* virtual methods from class DeferredMonitor */
+ virtual void RunDeferred() override;
+
+ /* the update thread */
+ void Task();
+ static void Task(void *ctx);
+
+ void StartThread(UpdateQueueItem &&i);
+
+ unsigned GenerateId();
+};
+
+#endif
diff --git a/src/db/update/UpdateDomain.cxx b/src/db/update/UpdateDomain.cxx
new file mode 100644
index 000000000..80ad4fd22
--- /dev/null
+++ b/src/db/update/UpdateDomain.cxx
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2003-2014 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 "UpdateDomain.hxx"
+#include "util/Domain.hxx"
+
+const Domain update_domain("update");
diff --git a/src/db/update/UpdateDomain.hxx b/src/db/update/UpdateDomain.hxx
new file mode 100644
index 000000000..a6e994390
--- /dev/null
+++ b/src/db/update/UpdateDomain.hxx
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2003-2014 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_DOMAIN_HXX
+#define MPD_UPDATE_DOMAIN_HXX
+
+extern const class Domain update_domain;
+
+#endif
diff --git a/src/db/update/UpdateIO.cxx b/src/db/update/UpdateIO.cxx
new file mode 100644
index 000000000..fa19a8b5a
--- /dev/null
+++ b/src/db/update/UpdateIO.cxx
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2003-2014 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 "UpdateDomain.hxx"
+#include "db/plugins/simple/Directory.hxx"
+#include "storage/FileInfo.hxx"
+#include "storage/StorageInterface.hxx"
+#include "fs/Traits.hxx"
+#include "fs/FileSystem.hxx"
+#include "util/Error.hxx"
+#include "Log.hxx"
+
+#include <errno.h>
+#include <unistd.h>
+
+bool
+GetInfo(Storage &storage, const char *uri_utf8, FileInfo &info)
+{
+ Error error;
+ bool success = storage.GetInfo(uri_utf8, true, info, error);
+ if (!success)
+ LogError(error);
+ return success;
+}
+
+bool
+GetInfo(StorageDirectoryReader &reader, FileInfo &info)
+{
+ Error error;
+ bool success = reader.GetInfo(true, info, error);
+ if (!success)
+ LogError(error);
+ return success;
+}
+
+bool
+DirectoryExists(Storage &storage, const Directory &directory)
+{
+ FileInfo info;
+ if (!storage.GetInfo(directory.GetPath(), true, info, IgnoreError()))
+ return false;
+
+ return directory.device == DEVICE_INARCHIVE ||
+ directory.device == DEVICE_CONTAINER
+ ? info.IsRegular()
+ : info.IsDirectory();
+}
+
+static bool
+GetDirectoryChildInfo(Storage &storage, const Directory &directory,
+ const char *name_utf8, FileInfo &info, Error &error)
+{
+ const auto uri_utf8 = PathTraitsUTF8::Build(directory.GetPath(),
+ name_utf8);
+ return storage.GetInfo(uri_utf8.c_str(), true, info, error);
+}
+
+bool
+directory_child_is_regular(Storage &storage, const Directory &directory,
+ const char *name_utf8)
+{
+ FileInfo info;
+ return GetDirectoryChildInfo(storage, directory, name_utf8, info,
+ IgnoreError()) &&
+ info.IsRegular();
+}
+
+bool
+directory_child_access(Storage &storage, const Directory &directory,
+ const char *name, int mode)
+{
+#ifdef WIN32
+ /* CheckAccess() is useless on WIN32 */
+ (void)storage;
+ (void)directory;
+ (void)name;
+ (void)mode;
+ return true;
+#else
+ const auto path = storage.MapChildFS(directory.GetPath(), name);
+ if (path.IsNull())
+ /* does not point to local file: silently ignore the
+ check */
+ return true;
+
+ return CheckAccess(path, mode) || errno != EACCES;
+#endif
+}
diff --git a/src/db/update/UpdateIO.hxx b/src/db/update/UpdateIO.hxx
new file mode 100644
index 000000000..2dbb4ae83
--- /dev/null
+++ b/src/db/update/UpdateIO.hxx
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2003-2014 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 "Compiler.h"
+
+struct Directory;
+struct FileInfo;
+class Storage;
+class StorageDirectoryReader;
+
+/**
+ * Wrapper for Storage::GetInfo() that logs errors instead of
+ * returning them.
+ */
+bool
+GetInfo(Storage &storage, const char *uri_utf8, FileInfo &info);
+
+/**
+ * Wrapper for LocalDirectoryReader::GetInfo() that logs errors
+ * instead of returning them.
+ */
+bool
+GetInfo(StorageDirectoryReader &reader, FileInfo &info);
+
+gcc_pure
+bool
+DirectoryExists(Storage &storage, const Directory &directory);
+
+gcc_pure
+bool
+directory_child_is_regular(Storage &storage, const Directory &directory,
+ const char *name_utf8);
+
+/**
+ * Checks if the given permissions on the mapped file are given.
+ */
+gcc_pure
+bool
+directory_child_access(Storage &storage, const Directory &directory,
+ const char *name, int mode);
+
+#endif
diff --git a/src/db/update/UpdateSong.cxx b/src/db/update/UpdateSong.cxx
new file mode 100644
index 000000000..005bf8992
--- /dev/null
+++ b/src/db/update/UpdateSong.cxx
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2003-2014 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 "Walk.hxx"
+#include "UpdateIO.hxx"
+#include "UpdateDomain.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/plugins/simple/Directory.hxx"
+#include "db/plugins/simple/Song.hxx"
+#include "decoder/DecoderList.hxx"
+#include "storage/FileInfo.hxx"
+#include "Log.hxx"
+
+#include <unistd.h>
+
+inline void
+UpdateWalk::UpdateSongFile2(Directory &directory,
+ const char *name, const char *suffix,
+ const FileInfo &info)
+{
+ db_lock();
+ Song *song = directory.FindSong(name);
+ db_unlock();
+
+ if (!directory_child_access(storage, directory, name, R_OK)) {
+ FormatError(update_domain,
+ "no read permissions on %s/%s",
+ directory.GetPath(), name);
+ if (song != nullptr)
+ editor.LockDeleteSong(directory, song);
+
+ return;
+ }
+
+ if (!(song != nullptr && info.mtime == song->mtime &&
+ !walk_discard) &&
+ UpdateContainerFile(directory, name, suffix, info)) {
+ if (song != nullptr)
+ editor.LockDeleteSong(directory, song);
+
+ return;
+ }
+
+ if (song == nullptr) {
+ FormatDebug(update_domain, "reading %s/%s",
+ directory.GetPath(), name);
+ song = Song::LoadFile(storage, name, directory);
+ if (song == nullptr) {
+ FormatDebug(update_domain,
+ "ignoring unrecognized file %s/%s",
+ directory.GetPath(), name);
+ return;
+ }
+
+ db_lock();
+ directory.AddSong(song);
+ db_unlock();
+
+ modified = true;
+ FormatDefault(update_domain, "added %s/%s",
+ directory.GetPath(), name);
+ } else if (info.mtime != song->mtime || walk_discard) {
+ FormatDefault(update_domain, "updating %s/%s",
+ directory.GetPath(), name);
+ if (!song->UpdateFile(storage)) {
+ FormatDebug(update_domain,
+ "deleting unrecognized file %s/%s",
+ directory.GetPath(), name);
+ editor.LockDeleteSong(directory, song);
+ }
+
+ modified = true;
+ }
+}
+
+bool
+UpdateWalk::UpdateSongFile(Directory &directory,
+ const char *name, const char *suffix,
+ const FileInfo &info)
+{
+ if (!decoder_plugins_supports_suffix(suffix))
+ return false;
+
+ UpdateSongFile2(directory, name, suffix, info);
+ return true;
+}
diff --git a/src/db/update/Walk.cxx b/src/db/update/Walk.cxx
new file mode 100644
index 000000000..c329865ff
--- /dev/null
+++ b/src/db/update/Walk.cxx
@@ -0,0 +1,495 @@
+/*
+ * Copyright (C) 2003-2014 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 "Walk.hxx"
+#include "UpdateIO.hxx"
+#include "Editor.hxx"
+#include "UpdateDomain.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/PlaylistVector.hxx"
+#include "db/Uri.hxx"
+#include "db/plugins/simple/Directory.hxx"
+#include "db/plugins/simple/Song.hxx"
+#include "storage/StorageInterface.hxx"
+#include "playlist/PlaylistRegistry.hxx"
+#include "ExcludeList.hxx"
+#include "config/ConfigGlobal.hxx"
+#include "config/ConfigOption.hxx"
+#include "fs/AllocatedPath.hxx"
+#include "fs/Traits.hxx"
+#include "fs/FileSystem.hxx"
+#include "fs/Charset.hxx"
+#include "storage/FileInfo.hxx"
+#include "util/Alloc.hxx"
+#include "util/UriUtil.hxx"
+#include "util/Error.hxx"
+#include "Log.hxx"
+
+#include <assert.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <memory>
+
+UpdateWalk::UpdateWalk(EventLoop &_loop, DatabaseListener &_listener,
+ Storage &_storage)
+ :cancel(false),
+ storage(_storage),
+ editor(_loop, _listener)
+{
+#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
+}
+
+static void
+directory_set_stat(Directory &dir, const FileInfo &info)
+{
+ dir.inode = info.inode;
+ dir.device = info.device;
+ dir.have_stat = true;
+}
+
+inline void
+UpdateWalk::RemoveExcludedFromDirectory(Directory &directory,
+ const ExcludeList &exclude_list)
+{
+ db_lock();
+
+ Directory *child, *n;
+ directory_for_each_child_safe(child, n, directory) {
+ const auto name_fs = AllocatedPath::FromUTF8(child->GetName());
+
+ if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
+ editor.DeleteDirectory(child);
+ modified = true;
+ }
+ }
+
+ Song *song, *ns;
+ directory_for_each_song_safe(song, ns, directory) {
+ assert(song->parent == &directory);
+
+ const auto name_fs = AllocatedPath::FromUTF8(song->uri);
+ if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
+ editor.DeleteSong(directory, song);
+ modified = true;
+ }
+ }
+
+ db_unlock();
+}
+
+inline void
+UpdateWalk::PurgeDeletedFromDirectory(Directory &directory)
+{
+ Directory *child, *n;
+ directory_for_each_child_safe(child, n, directory) {
+ if (DirectoryExists(storage, *child))
+ continue;
+
+ editor.LockDeleteDirectory(child);
+
+ modified = true;
+ }
+
+ Song *song, *ns;
+ directory_for_each_song_safe(song, ns, directory) {
+ if (!directory_child_is_regular(storage, directory,
+ song->uri)) {
+ editor.LockDeleteSong(directory, song);
+
+ modified = true;
+ }
+ }
+
+ for (auto i = directory.playlists.begin(),
+ end = directory.playlists.end();
+ i != end;) {
+ if (!directory_child_is_regular(storage, directory,
+ i->name.c_str())) {
+ db_lock();
+ i = directory.playlists.erase(i);
+ db_unlock();
+ } else
+ ++i;
+ }
+}
+
+#ifndef WIN32
+static bool
+update_directory_stat(Storage &storage, Directory &directory)
+{
+ FileInfo info;
+ if (!GetInfo(storage, directory.GetPath(), info))
+ return false;
+
+ directory_set_stat(directory, info);
+ return true;
+}
+#endif
+
+/**
+ * Check the ancestors of the given #Directory and see if there's one
+ * with the same device/inode number, building a loop.
+ *
+ * @return 1 if a loop was found, 0 if not, -1 on I/O error
+ */
+static int
+FindAncestorLoop(Storage &storage, Directory *parent,
+ unsigned inode, unsigned device)
+{
+#ifndef WIN32
+ if (device == 0 && inode == 0)
+ /* can't detect loops if the Storage does not support
+ these numbers */
+ return 0;
+
+ while (parent) {
+ if (!parent->have_stat &&
+ !update_directory_stat(storage, *parent))
+ return -1;
+
+ if (parent->inode == inode && parent->device == device) {
+ LogDebug(update_domain, "recursive directory found");
+ return 1;
+ }
+
+ parent = parent->parent;
+ }
+#else
+ (void)storage;
+ (void)parent;
+ (void)inode;
+ (void)device;
+#endif
+
+ return 0;
+}
+
+inline bool
+UpdateWalk::UpdatePlaylistFile(Directory &directory,
+ const char *name, const char *suffix,
+ const FileInfo &info)
+{
+ if (!playlist_suffix_supported(suffix))
+ return false;
+
+ PlaylistInfo pi(name, info.mtime);
+
+ db_lock();
+ if (directory.playlists.UpdateOrInsert(std::move(pi)))
+ modified = true;
+ db_unlock();
+ return true;
+}
+
+inline bool
+UpdateWalk::UpdateRegularFile(Directory &directory,
+ const char *name, const FileInfo &info)
+{
+ const char *suffix = uri_get_suffix(name);
+ if (suffix == nullptr)
+ return false;
+
+ return UpdateSongFile(directory, name, suffix, info) ||
+ UpdateArchiveFile(directory, name, suffix, info) ||
+ UpdatePlaylistFile(directory, name, suffix, info);
+}
+
+void
+UpdateWalk::UpdateDirectoryChild(Directory &directory,
+ const char *name, const FileInfo &info)
+{
+ assert(strchr(name, '/') == nullptr);
+
+ if (info.IsRegular()) {
+ UpdateRegularFile(directory, name, info);
+ } else if (info.IsDirectory()) {
+ if (FindAncestorLoop(storage, &directory,
+ info.inode, info.device))
+ return;
+
+ db_lock();
+ Directory *subdir = directory.MakeChild(name);
+ db_unlock();
+
+ assert(&directory == subdir->parent);
+
+ if (!UpdateDirectory(*subdir, info))
+ editor.LockDeleteDirectory(subdir);
+ } else {
+ FormatDebug(update_domain,
+ "%s is not a directory, archive or music", name);
+ }
+}
+
+/* we don't look at "." / ".." nor files with newlines in their name */
+gcc_pure
+static bool
+skip_path(const char *name_utf8)
+{
+ return strchr(name_utf8, '\n') != nullptr;
+}
+
+gcc_pure
+bool
+UpdateWalk::SkipSymlink(const Directory *directory,
+ const char *utf8_name) const
+{
+#ifndef WIN32
+ const auto path_fs = storage.MapChildFS(directory->GetPath(),
+ utf8_name);
+ if (path_fs.IsNull())
+ /* not a local file: don't skip */
+ return false;
+
+ const auto target = ReadLink(path_fs);
+ if (target.IsNull())
+ /* don't skip if this is not a symlink */
+ return errno != EINVAL;
+
+ 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;
+ }
+
+ const char *target_str = target.c_str();
+
+ if (PathTraitsFS::IsAbsolute(target_str)) {
+ /* if the symlink points to an absolute path, see if
+ that path is inside the music directory */
+ const auto target_utf8 = PathToUTF8(target_str);
+ if (target_utf8.empty())
+ return true;
+
+ const char *relative =
+ storage.MapToRelativeUTF8(target_utf8.c_str());
+ return relative != nullptr
+ ? !follow_inside_symlinks
+ : !follow_outside_symlinks;
+ }
+
+ const char *p = target_str;
+ while (*p == '.') {
+ if (p[1] == '.' && PathTraitsFS::IsSeparator(p[2])) {
+ /* "../" moves to parent directory */
+ directory = directory->parent;
+ if (directory == nullptr) {
+ /* we have moved outside the music
+ directory - skip this symlink
+ if such symlinks are not allowed */
+ return !follow_outside_symlinks;
+ }
+ p += 3;
+ } else if (PathTraitsFS::IsSeparator(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
+}
+
+bool
+UpdateWalk::UpdateDirectory(Directory &directory, const FileInfo &info)
+{
+ assert(info.IsDirectory());
+
+ directory_set_stat(directory, info);
+
+ Error error;
+ const std::auto_ptr<StorageDirectoryReader> reader(storage.OpenDirectory(directory.GetPath(), error));
+ if (reader.get() == nullptr) {
+ LogError(error);
+ return false;
+ }
+
+ ExcludeList exclude_list;
+
+ {
+ const auto exclude_path_fs =
+ storage.MapChildFS(directory.GetPath(), ".mpdignore");
+ if (!exclude_path_fs.IsNull())
+ exclude_list.LoadFile(exclude_path_fs);
+ }
+
+ if (!exclude_list.IsEmpty())
+ RemoveExcludedFromDirectory(directory, exclude_list);
+
+ PurgeDeletedFromDirectory(directory);
+
+ const char *name_utf8;
+ while (!cancel && (name_utf8 = reader->Read()) != nullptr) {
+ if (skip_path(name_utf8))
+ continue;
+
+ {
+ const auto name_fs = AllocatedPath::FromUTF8(name_utf8);
+ if (name_fs.IsNull() || exclude_list.Check(name_fs))
+ continue;
+ }
+
+ if (SkipSymlink(&directory, name_utf8)) {
+ modified |= editor.DeleteNameIn(directory, name_utf8);
+ continue;
+ }
+
+ FileInfo info2;
+ if (!GetInfo(*reader, info2)) {
+ modified |= editor.DeleteNameIn(directory, name_utf8);
+ continue;
+ }
+
+ UpdateDirectoryChild(directory, name_utf8, info2);
+ }
+
+ directory.mtime = info.mtime;
+
+ return true;
+}
+
+inline Directory *
+UpdateWalk::DirectoryMakeChildChecked(Directory &parent,
+ const char *uri_utf8,
+ const char *name_utf8)
+{
+ db_lock();
+ Directory *directory = parent.FindChild(name_utf8);
+ db_unlock();
+
+ if (directory != nullptr) {
+ if (directory->IsMount())
+ directory = nullptr;
+
+ return directory;
+ }
+
+ FileInfo info;
+ if (!GetInfo(storage, uri_utf8, info) ||
+ FindAncestorLoop(storage, &parent, info.inode, info.device))
+ return nullptr;
+
+ if (SkipSymlink(&parent, name_utf8))
+ return nullptr;
+
+ /* if we're adding directory paths, make sure to delete filenames
+ with potentially the same name */
+ db_lock();
+ Song *conflicting = parent.FindSong(name_utf8);
+ if (conflicting)
+ editor.DeleteSong(parent, conflicting);
+
+ directory = parent.CreateChild(name_utf8);
+ db_unlock();
+
+ directory_set_stat(*directory, info);
+ return directory;
+}
+
+inline Directory *
+UpdateWalk::DirectoryMakeUriParentChecked(Directory &root, const char *uri)
+{
+ Directory *directory = &root;
+ char *duplicated = xstrdup(uri);
+ char *name_utf8 = duplicated, *slash;
+
+ while ((slash = strchr(name_utf8, '/')) != nullptr) {
+ *slash = 0;
+
+ if (*name_utf8 == 0)
+ continue;
+
+ directory = DirectoryMakeChildChecked(*directory,
+ duplicated,
+ name_utf8);
+ if (directory == nullptr)
+ break;
+
+ name_utf8 = slash + 1;
+ }
+
+ free(duplicated);
+ return directory;
+}
+
+inline void
+UpdateWalk::UpdateUri(Directory &root, const char *uri)
+{
+ Directory *parent = DirectoryMakeUriParentChecked(root, uri);
+ if (parent == nullptr)
+ return;
+
+ const char *name = PathTraitsUTF8::GetBase(uri);
+
+ if (SkipSymlink(parent, name)) {
+ modified |= editor.DeleteNameIn(*parent, name);
+ return;
+ }
+
+ FileInfo info;
+ if (!GetInfo(storage, uri, info)) {
+ modified |= editor.DeleteNameIn(*parent, name);
+ return;
+ }
+
+ UpdateDirectoryChild(*parent, name, info);
+}
+
+bool
+UpdateWalk::Walk(Directory &root, const char *path, bool discard)
+{
+ walk_discard = discard;
+ modified = false;
+
+ if (path != nullptr && !isRootDirectory(path)) {
+ UpdateUri(root, path);
+ } else {
+ FileInfo info;
+ if (!GetInfo(storage, "", info))
+ return false;
+
+ UpdateDirectory(root, info);
+ }
+
+ return modified;
+}
diff --git a/src/db/update/Walk.hxx b/src/db/update/Walk.hxx
new file mode 100644
index 000000000..cce276ab0
--- /dev/null
+++ b/src/db/update/Walk.hxx
@@ -0,0 +1,156 @@
+/*
+ * Copyright (C) 2003-2014 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 "Editor.hxx"
+
+#include <sys/stat.h>
+
+struct stat;
+struct FileInfo;
+struct Directory;
+struct ArchivePlugin;
+class Storage;
+class ExcludeList;
+
+class UpdateWalk final {
+#ifdef ENABLE_ARCHIVE
+ friend class UpdateArchiveVisitor;
+#endif
+
+#ifndef WIN32
+ static constexpr bool DEFAULT_FOLLOW_INSIDE_SYMLINKS = true;
+ static constexpr bool DEFAULT_FOLLOW_OUTSIDE_SYMLINKS = true;
+
+ bool follow_inside_symlinks;
+ bool follow_outside_symlinks;
+#endif
+
+ bool walk_discard;
+ bool modified;
+
+ /**
+ * Set to true by the main thread when the update thread shall
+ * cancel as quickly as possible. Access to this flag is
+ * unprotected.
+ */
+ volatile bool cancel;
+
+ Storage &storage;
+
+ DatabaseEditor editor;
+
+public:
+ UpdateWalk(EventLoop &_loop, DatabaseListener &_listener,
+ Storage &_storage);
+
+ /**
+ * Cancel the current update and quit the Walk() method as
+ * soon as possible.
+ */
+ void Cancel() {
+ cancel = true;
+ }
+
+ /**
+ * Returns true if the database was modified.
+ */
+ bool Walk(Directory &root, const char *path, bool discard);
+
+private:
+ gcc_pure
+ bool SkipSymlink(const Directory *directory,
+ const char *utf8_name) const;
+
+ void RemoveExcludedFromDirectory(Directory &directory,
+ const ExcludeList &exclude_list);
+
+ void PurgeDeletedFromDirectory(Directory &directory);
+
+ void UpdateSongFile2(Directory &directory,
+ const char *name, const char *suffix,
+ const FileInfo &info);
+
+ bool UpdateSongFile(Directory &directory,
+ const char *name, const char *suffix,
+ const FileInfo &info);
+
+ bool UpdateContainerFile(Directory &directory,
+ const char *name, const char *suffix,
+ const FileInfo &info);
+
+
+#ifdef ENABLE_ARCHIVE
+ void UpdateArchiveTree(Directory &parent, const char *name);
+
+ bool UpdateArchiveFile(Directory &directory,
+ const char *name, const char *suffix,
+ const FileInfo &info);
+
+ void UpdateArchiveFile(Directory &directory, const char *name,
+ const FileInfo &info,
+ const ArchivePlugin &plugin);
+
+
+#else
+ bool UpdateArchiveFile(gcc_unused Directory &directory,
+ gcc_unused const char *name,
+ gcc_unused const char *suffix,
+ gcc_unused const FileInfo &info) {
+ return false;
+ }
+#endif
+
+ bool UpdatePlaylistFile(Directory &directory,
+ const char *name, const char *suffix,
+ const FileInfo &info);
+
+ bool UpdateRegularFile(Directory &directory,
+ const char *name, const FileInfo &info);
+
+ void UpdateDirectoryChild(Directory &directory,
+ const char *name, const FileInfo &info);
+
+ bool UpdateDirectory(Directory &directory, const FileInfo &info);
+
+ /**
+ * 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 nullptr when it
+ * exists already and is unmodified.
+ *
+ * The caller must lock the database.
+ */
+ Directory *MakeDirectoryIfModified(Directory &parent, const char *name,
+ const FileInfo &info);
+
+ Directory *DirectoryMakeChildChecked(Directory &parent,
+ const char *uri_utf8,
+ const char *name_utf8);
+
+ Directory *DirectoryMakeUriParentChecked(Directory &root,
+ const char *uri);
+
+ void UpdateUri(Directory &root, const char *uri);
+};
+
+#endif