aboutsummaryrefslogtreecommitdiffstats
path: root/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/db')
-rw-r--r--src/db/Configured.cxx72
-rw-r--r--src/db/Configured.hxx39
-rw-r--r--src/db/Count.cxx151
-rw-r--r--src/db/Count.hxx39
-rw-r--r--src/db/DatabaseError.cxx24
-rw-r--r--src/db/DatabaseError.hxx39
-rw-r--r--src/db/DatabaseGlue.cxx45
-rw-r--r--src/db/DatabaseGlue.hxx40
-rw-r--r--src/db/DatabaseListener.hxx46
-rw-r--r--src/db/DatabaseLock.cxx27
-rw-r--r--src/db/DatabaseLock.hxx97
-rw-r--r--src/db/DatabasePlaylist.cxx52
-rw-r--r--src/db/DatabasePlaylist.hxx37
-rw-r--r--src/db/DatabasePlugin.hxx58
-rw-r--r--src/db/DatabasePrint.cxx221
-rw-r--r--src/db/DatabasePrint.hxx45
-rw-r--r--src/db/DatabaseQueue.cxx53
-rw-r--r--src/db/DatabaseQueue.hxx31
-rw-r--r--src/db/DatabaseSong.cxx53
-rw-r--r--src/db/DatabaseSong.hxx50
-rw-r--r--src/db/Helpers.cxx98
-rw-r--r--src/db/Helpers.hxx32
-rw-r--r--src/db/Interface.hxx138
-rw-r--r--src/db/LightDirectory.hxx61
-rw-r--r--src/db/LightSong.cxx35
-rw-r--r--src/db/LightSong.hxx93
-rw-r--r--src/db/PlaylistInfo.hxx63
-rw-r--r--src/db/PlaylistVector.cxx66
-rw-r--r--src/db/PlaylistVector.hxx56
-rw-r--r--src/db/Registry.cxx48
-rw-r--r--src/db/Registry.hxx37
-rw-r--r--src/db/Selection.cxx49
-rw-r--r--src/db/Selection.hxx60
-rw-r--r--src/db/SimpleDatabasePlugin.cxx323
-rw-r--r--src/db/Stats.hxx53
-rw-r--r--src/db/UniqueTags.cxx59
-rw-r--r--src/db/UniqueTags.hxx38
-rw-r--r--src/db/Uri.hxx29
-rw-r--r--src/db/Visitor.hxx38
-rw-r--r--src/db/plugins/LazyDatabase.cxx108
-rw-r--r--src/db/plugins/LazyDatabase.hxx69
-rw-r--r--src/db/plugins/ProxyDatabasePlugin.cxx (renamed from src/db/ProxyDatabasePlugin.cxx)382
-rw-r--r--src/db/plugins/ProxyDatabasePlugin.hxx (renamed from src/db/ProxyDatabasePlugin.hxx)2
-rw-r--r--src/db/plugins/simple/DatabaseSave.cxx160
-rw-r--r--src/db/plugins/simple/DatabaseSave.hxx34
-rw-r--r--src/db/plugins/simple/Directory.cxx277
-rw-r--r--src/db/plugins/simple/Directory.hxx285
-rw-r--r--src/db/plugins/simple/DirectorySave.cxx207
-rw-r--r--src/db/plugins/simple/DirectorySave.hxx34
-rw-r--r--src/db/plugins/simple/Mount.cxx96
-rw-r--r--src/db/plugins/simple/Mount.hxx36
-rw-r--r--src/db/plugins/simple/PrefixedLightSong.hxx41
-rw-r--r--src/db/plugins/simple/SimpleDatabasePlugin.cxx541
-rw-r--r--src/db/plugins/simple/SimpleDatabasePlugin.hxx (renamed from src/db/SimpleDatabasePlugin.hxx)84
-rw-r--r--src/db/plugins/simple/Song.cxx112
-rw-r--r--src/db/plugins/simple/Song.hxx130
-rw-r--r--src/db/plugins/simple/SongSort.cxx108
-rw-r--r--src/db/plugins/simple/SongSort.hxx30
-rw-r--r--src/db/plugins/upnp/ContentDirectoryService.cxx204
-rw-r--r--src/db/plugins/upnp/Directory.cxx262
-rw-r--r--src/db/plugins/upnp/Directory.hxx66
-rw-r--r--src/db/plugins/upnp/Object.cxx25
-rw-r--r--src/db/plugins/upnp/Object.hxx85
-rw-r--r--src/db/plugins/upnp/Tags.cxx33
-rw-r--r--src/db/plugins/upnp/Tags.hxx28
-rw-r--r--src/db/plugins/upnp/UpnpDatabasePlugin.cxx783
-rw-r--r--src/db/plugins/upnp/UpnpDatabasePlugin.hxx27
-rw-r--r--src/db/update/Archive.cxx167
-rw-r--r--src/db/update/Container.cxx132
-rw-r--r--src/db/update/Editor.cxx119
-rw-r--r--src/db/update/Editor.hxx71
-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.cxx116
-rw-r--r--src/db/update/InotifySource.hxx72
-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.hxx62
-rw-r--r--src/db/update/Service.cxx279
-rw-r--r--src/db/update/Service.hxx116
-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.cxx491
-rw-r--r--src/db/update/Walk.hxx157
94 files changed, 9457 insertions, 449 deletions
diff --git a/src/db/Configured.cxx b/src/db/Configured.cxx
new file mode 100644
index 000000000..625300d75
--- /dev/null
+++ b/src/db/Configured.cxx
@@ -0,0 +1,72 @@
+/*
+ * 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 "Configured.hxx"
+#include "DatabaseGlue.hxx"
+#include "config/ConfigGlobal.hxx"
+#include "config/ConfigData.hxx"
+#include "config/ConfigError.hxx"
+#include "fs/AllocatedPath.hxx"
+#include "fs/StandardDirectory.hxx"
+#include "util/Error.hxx"
+#include "Log.hxx"
+
+Database *
+CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener,
+ Error &error)
+{
+ const struct config_param *param = config_get_param(CONF_DATABASE);
+ const struct config_param *path = config_get_param(CONF_DB_FILE);
+
+ if (param != nullptr && path != nullptr) {
+ error.Format(config_domain,
+ "Found both 'database' (line %d) and 'db_file' (line %d) setting",
+ param->line, path->line);
+ return nullptr;
+ }
+
+ struct config_param *allocated = nullptr;
+
+ if (param == nullptr && path != nullptr) {
+ allocated = new config_param("database", path->line);
+ allocated->AddBlockParam("path", path->value.c_str(),
+ path->line);
+ param = allocated;
+ }
+
+ if (param == nullptr) {
+ /* if there is no override, use the cache directory */
+
+ const AllocatedPath cache_dir = GetUserCacheDir();
+ if (cache_dir.IsNull())
+ return nullptr;
+
+ const auto db_file = AllocatedPath::Build(cache_dir, "mpd.db");
+
+ allocated = new config_param("database");
+ allocated->AddBlockParam("path", db_file.c_str(), -1);
+ param = allocated;
+ }
+
+ Database *db = DatabaseGlobalInit(loop, listener, *param,
+ error);
+ delete allocated;
+ return db;
+}
diff --git a/src/db/Configured.hxx b/src/db/Configured.hxx
new file mode 100644
index 000000000..5d25b701c
--- /dev/null
+++ b/src/db/Configured.hxx
@@ -0,0 +1,39 @@
+/*
+ * 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_DB_CONFIG_HXX
+#define MPD_DB_CONFIG_HXX
+
+#include "check.h"
+
+class EventLoop;
+class DatabaseListener;
+class Database;
+class Error;
+
+/**
+ * Read database configuration settings and create a #Database
+ * instance from it, but do not open it. Returns nullptr on error or
+ * if no database is configured (no #Error set in that case).
+ */
+Database *
+CreateConfiguredDatabase(EventLoop &loop, DatabaseListener &listener,
+ Error &error);
+
+#endif
diff --git a/src/db/Count.cxx b/src/db/Count.cxx
new file mode 100644
index 000000000..b58ac53e9
--- /dev/null
+++ b/src/db/Count.cxx
@@ -0,0 +1,151 @@
+/*
+ * 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 "Count.hxx"
+#include "Selection.hxx"
+#include "Interface.hxx"
+#include "client/Client.hxx"
+#include "LightSong.hxx"
+#include "tag/Set.hxx"
+
+#include <functional>
+#include <map>
+
+struct SearchStats {
+ unsigned n_songs;
+ std::chrono::duration<std::uint64_t, SongTime::period> total_duration;
+
+ constexpr SearchStats()
+ :n_songs(0), total_duration(0) {}
+};
+
+class TagCountMap : public std::map<std::string, SearchStats> {
+};
+
+static void
+PrintSearchStats(Client &client, const SearchStats &stats)
+{
+ unsigned total_duration_s =
+ std::chrono::duration_cast<std::chrono::seconds>(stats.total_duration).count();
+
+ client_printf(client,
+ "songs: %u\n"
+ "playtime: %u\n",
+ stats.n_songs, total_duration_s);
+}
+
+static void
+Print(Client &client, TagType group, const TagCountMap &m)
+{
+ assert(unsigned(group) < TAG_NUM_OF_ITEM_TYPES);
+
+ for (const auto &i : m) {
+ client_printf(client, "%s: %s\n",
+ tag_item_names[group], i.first.c_str());
+ PrintSearchStats(client, i.second);
+ }
+}
+
+static bool
+stats_visitor_song(SearchStats &stats, const LightSong &song)
+{
+ stats.n_songs++;
+
+ const auto duration = song.GetDuration();
+ if (!duration.IsNegative())
+ stats.total_duration += duration;
+
+ return true;
+}
+
+static bool
+CollectGroupCounts(TagCountMap &map, TagType group, const Tag &tag)
+{
+ bool found = false;
+ for (const auto &item : tag) {
+ if (item.type == group) {
+ auto r = map.insert(std::make_pair(item.value,
+ SearchStats()));
+ SearchStats &s = r.first->second;
+ ++s.n_songs;
+ if (!tag.duration.IsNegative())
+ s.total_duration += tag.duration;
+
+ found = true;
+ }
+ }
+
+ return found;
+}
+
+static bool
+GroupCountVisitor(TagCountMap &map, TagType group, const LightSong &song)
+{
+ assert(song.tag != nullptr);
+
+ const Tag &tag = *song.tag;
+ if (!CollectGroupCounts(map, group, tag) && group == TAG_ALBUM_ARTIST)
+ /* fall back to "Artist" if no "AlbumArtist" was found */
+ CollectGroupCounts(map, TAG_ARTIST, tag);
+
+ return true;
+}
+
+bool
+PrintSongCount(Client &client, const char *name,
+ const SongFilter *filter,
+ TagType group,
+ Error &error)
+{
+ const Database *db = client.GetDatabase(error);
+ if (db == nullptr)
+ return false;
+
+ const DatabaseSelection selection(name, true, filter);
+
+ if (group == TAG_NUM_OF_ITEM_TYPES) {
+ /* no grouping */
+
+ SearchStats stats;
+
+ using namespace std::placeholders;
+ const auto f = std::bind(stats_visitor_song, std::ref(stats),
+ _1);
+ if (!db->Visit(selection, f, error))
+ return false;
+
+ PrintSearchStats(client, stats);
+ } else {
+ /* group by the specified tag: store counts in a
+ std::map */
+
+ TagCountMap map;
+
+ using namespace std::placeholders;
+ const auto f = std::bind(GroupCountVisitor, std::ref(map),
+ group, _1);
+ if (!db->Visit(selection, f, error))
+ return false;
+
+ Print(client, group, map);
+ }
+
+ return true;
+}
diff --git a/src/db/Count.hxx b/src/db/Count.hxx
new file mode 100644
index 000000000..d22a3210d
--- /dev/null
+++ b/src/db/Count.hxx
@@ -0,0 +1,39 @@
+/*
+ * 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_DB_COUNT_HXX
+#define MPD_DB_COUNT_HXX
+
+#include "Compiler.h"
+
+#include <stdint.h>
+
+enum TagType : uint8_t;
+class Client;
+class SongFilter;
+class Error;
+
+gcc_nonnull(2)
+bool
+PrintSongCount(Client &client, const char *name,
+ const SongFilter *filter,
+ TagType group,
+ Error &error);
+
+#endif
diff --git a/src/db/DatabaseError.cxx b/src/db/DatabaseError.cxx
new file mode 100644
index 000000000..e0cbdd6a3
--- /dev/null
+++ b/src/db/DatabaseError.cxx
@@ -0,0 +1,24 @@
+/*
+ * 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 "DatabaseError.hxx"
+#include "util/Domain.hxx"
+
+const Domain db_domain("db");
diff --git a/src/db/DatabaseError.hxx b/src/db/DatabaseError.hxx
new file mode 100644
index 000000000..c71bbdfff
--- /dev/null
+++ b/src/db/DatabaseError.hxx
@@ -0,0 +1,39 @@
+/*
+ * 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_DB_ERROR_HXX
+#define MPD_DB_ERROR_HXX
+
+class Domain;
+
+enum db_error {
+ /**
+ * The database is disabled, i.e. none is configured in this
+ * MPD instance.
+ */
+ DB_DISABLED,
+
+ DB_NOT_FOUND,
+
+ DB_CONFLICT,
+};
+
+extern const Domain db_domain;
+
+#endif
diff --git a/src/db/DatabaseGlue.cxx b/src/db/DatabaseGlue.cxx
new file mode 100644
index 000000000..ade5c95f3
--- /dev/null
+++ b/src/db/DatabaseGlue.cxx
@@ -0,0 +1,45 @@
+/*
+ * 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 "DatabaseGlue.hxx"
+#include "Registry.hxx"
+#include "DatabaseError.hxx"
+#include "util/Error.hxx"
+#include "config/ConfigData.hxx"
+#include "DatabasePlugin.hxx"
+
+#include <string.h>
+
+Database *
+DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener,
+ const config_param &param, Error &error)
+{
+ const char *plugin_name =
+ param.GetBlockValue("plugin", "simple");
+
+ const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name);
+ if (plugin == nullptr) {
+ error.Format(db_domain,
+ "No such database plugin: %s", plugin_name);
+ return nullptr;
+ }
+
+ return plugin->create(loop, listener, param, error);
+}
diff --git a/src/db/DatabaseGlue.hxx b/src/db/DatabaseGlue.hxx
new file mode 100644
index 000000000..70b50def3
--- /dev/null
+++ b/src/db/DatabaseGlue.hxx
@@ -0,0 +1,40 @@
+/*
+ * 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_DATABASE_GLUE_HXX
+#define MPD_DATABASE_GLUE_HXX
+
+#include "Compiler.h"
+
+struct config_param;
+class EventLoop;
+class DatabaseListener;
+class Database;
+class Error;
+
+/**
+ * Initialize the database library.
+ *
+ * @param param the database configuration block
+ */
+Database *
+DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener,
+ const config_param &param, Error &error);
+
+#endif
diff --git a/src/db/DatabaseListener.hxx b/src/db/DatabaseListener.hxx
new file mode 100644
index 000000000..8b410c2f5
--- /dev/null
+++ b/src/db/DatabaseListener.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_DATABASE_CLIENT_HXX
+#define MPD_DATABASE_CLIENT_HXX
+
+struct LightSong;
+
+/**
+ * An object that listens to events from the #Database.
+ *
+ * @see #Instance
+ */
+class DatabaseListener {
+public:
+ /**
+ * The database has been modified. This must be called in the
+ * thread that has created the #Database instance and that
+ * runs the #EventLoop.
+ */
+ virtual void OnDatabaseModified() = 0;
+
+ /**
+ * During database update, a song is about to be removed from
+ * the database because the file has disappeared.
+ */
+ virtual void OnDatabaseSongRemoved(const LightSong &song) = 0;
+};
+
+#endif
diff --git a/src/db/DatabaseLock.cxx b/src/db/DatabaseLock.cxx
new file mode 100644
index 000000000..c0b5e4844
--- /dev/null
+++ b/src/db/DatabaseLock.cxx
@@ -0,0 +1,27 @@
+/*
+ * 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 "DatabaseLock.hxx"
+
+Mutex db_mutex;
+
+#ifndef NDEBUG
+ThreadId db_mutex_holder;
+#endif
diff --git a/src/db/DatabaseLock.hxx b/src/db/DatabaseLock.hxx
new file mode 100644
index 000000000..9d0b0c152
--- /dev/null
+++ b/src/db/DatabaseLock.hxx
@@ -0,0 +1,97 @@
+/*
+ * 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.
+ */
+
+/** \file
+ *
+ * Support for locking data structures from the database, for safe
+ * multi-threading.
+ */
+
+#ifndef MPD_DB_LOCK_HXX
+#define MPD_DB_LOCK_HXX
+
+#include "check.h"
+#include "thread/Mutex.hxx"
+#include "Compiler.h"
+
+#include <assert.h>
+
+extern Mutex db_mutex;
+
+#ifndef NDEBUG
+
+#include "thread/Id.hxx"
+
+extern ThreadId db_mutex_holder;
+
+/**
+ * Does the current thread hold the database lock?
+ */
+gcc_pure
+static inline bool
+holding_db_lock(void)
+{
+ return db_mutex_holder.IsInside();
+}
+
+#endif
+
+/**
+ * Obtain the global database lock. This is needed before
+ * dereferencing a #song or #directory. It is not recursive.
+ */
+static inline void
+db_lock(void)
+{
+ assert(!holding_db_lock());
+
+ db_mutex.lock();
+
+ assert(db_mutex_holder.IsNull());
+#ifndef NDEBUG
+ db_mutex_holder = ThreadId::GetCurrent();
+#endif
+}
+
+/**
+ * Release the global database lock.
+ */
+static inline void
+db_unlock(void)
+{
+ assert(holding_db_lock());
+#ifndef NDEBUG
+ db_mutex_holder = ThreadId::Null();
+#endif
+
+ db_mutex.unlock();
+}
+
+class ScopeDatabaseLock {
+public:
+ ScopeDatabaseLock() {
+ db_lock();
+ }
+
+ ~ScopeDatabaseLock() {
+ db_unlock();
+ }
+};
+
+#endif
diff --git a/src/db/DatabasePlaylist.cxx b/src/db/DatabasePlaylist.cxx
new file mode 100644
index 000000000..f1cfdc874
--- /dev/null
+++ b/src/db/DatabasePlaylist.cxx
@@ -0,0 +1,52 @@
+/*
+ * 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 "DatabasePlaylist.hxx"
+#include "DatabaseSong.hxx"
+#include "Selection.hxx"
+#include "PlaylistFile.hxx"
+#include "Interface.hxx"
+#include "DetachedSong.hxx"
+#include "storage/StorageInterface.hxx"
+
+#include <functional>
+
+static bool
+AddSong(const Storage &storage, const char *playlist_path_utf8,
+ const LightSong &song, Error &error)
+{
+ return spl_append_song(playlist_path_utf8,
+ DatabaseDetachSong(storage, song),
+ error);
+}
+
+bool
+search_add_to_playlist(const Database &db, const Storage &storage,
+ const char *uri, const char *playlist_path_utf8,
+ const SongFilter *filter,
+ Error &error)
+{
+ const DatabaseSelection selection(uri, true, filter);
+
+ using namespace std::placeholders;
+ const auto f = std::bind(AddSong, std::ref(storage),
+ playlist_path_utf8, _1, _2);
+ return db.Visit(selection, f, error);
+}
diff --git a/src/db/DatabasePlaylist.hxx b/src/db/DatabasePlaylist.hxx
new file mode 100644
index 000000000..9dc3526bb
--- /dev/null
+++ b/src/db/DatabasePlaylist.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_DATABASE_PLAYLIST_HXX
+#define MPD_DATABASE_PLAYLIST_HXX
+
+#include "Compiler.h"
+
+class Database;
+class Storage;
+class SongFilter;
+class Error;
+
+gcc_nonnull(3,4)
+bool
+search_add_to_playlist(const Database &db, const Storage &storage,
+ const char *uri, const char *path_utf8,
+ const SongFilter *filter,
+ Error &error);
+
+#endif
diff --git a/src/db/DatabasePlugin.hxx b/src/db/DatabasePlugin.hxx
new file mode 100644
index 000000000..831101786
--- /dev/null
+++ b/src/db/DatabasePlugin.hxx
@@ -0,0 +1,58 @@
+/*
+ * 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.
+ */
+
+/** \file
+ *
+ * This header declares the db_plugin class. It describes a
+ * plugin API for databases of song metadata.
+ */
+
+#ifndef MPD_DATABASE_PLUGIN_HXX
+#define MPD_DATABASE_PLUGIN_HXX
+
+struct config_param;
+class Error;
+class EventLoop;
+class DatabaseListener;
+class Database;
+
+struct DatabasePlugin {
+ /**
+ * This plugin requires a #Storage instance. It contains only
+ * cached metadata from files in the #Storage.
+ */
+ static constexpr unsigned FLAG_REQUIRE_STORAGE = 0x1;
+
+ const char *name;
+
+ unsigned flags;
+
+ /**
+ * Allocates and configures a database.
+ */
+ Database *(*create)(EventLoop &loop, DatabaseListener &listener,
+ const config_param &param,
+ Error &error);
+
+ constexpr bool RequireStorage() const {
+ return flags & FLAG_REQUIRE_STORAGE;
+ }
+};
+
+#endif
diff --git a/src/db/DatabasePrint.cxx b/src/db/DatabasePrint.cxx
new file mode 100644
index 000000000..498aedf97
--- /dev/null
+++ b/src/db/DatabasePrint.cxx
@@ -0,0 +1,221 @@
+/*
+ * 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 "DatabasePrint.hxx"
+#include "Selection.hxx"
+#include "SongFilter.hxx"
+#include "SongPrint.hxx"
+#include "TimePrint.hxx"
+#include "client/Client.hxx"
+#include "tag/Tag.hxx"
+#include "LightSong.hxx"
+#include "LightDirectory.hxx"
+#include "PlaylistInfo.hxx"
+#include "Interface.hxx"
+#include "fs/Traits.hxx"
+
+#include <functional>
+
+static const char *
+ApplyBaseFlag(const char *uri, bool base)
+{
+ if (base)
+ uri = PathTraitsUTF8::GetBase(uri);
+ return uri;
+}
+
+static void
+PrintDirectoryURI(Client &client, bool base, const LightDirectory &directory)
+{
+ client_printf(client, "directory: %s\n",
+ ApplyBaseFlag(directory.GetPath(), base));
+}
+
+static bool
+PrintDirectoryBrief(Client &client, bool base, const LightDirectory &directory)
+{
+ if (!directory.IsRoot())
+ PrintDirectoryURI(client, base, directory);
+
+ return true;
+}
+
+static bool
+PrintDirectoryFull(Client &client, bool base, const LightDirectory &directory)
+{
+ if (!directory.IsRoot()) {
+ PrintDirectoryURI(client, base, directory);
+
+ if (directory.mtime > 0)
+ time_print(client, "Last-Modified", directory.mtime);
+ }
+
+ return true;
+}
+
+static void
+print_playlist_in_directory(Client &client, bool base,
+ const char *directory,
+ const char *name_utf8)
+{
+ if (base || directory == nullptr)
+ client_printf(client, "playlist: %s\n",
+ ApplyBaseFlag(name_utf8, base));
+ else
+ client_printf(client, "playlist: %s/%s\n",
+ directory, name_utf8);
+}
+
+static void
+print_playlist_in_directory(Client &client, bool base,
+ const LightDirectory *directory,
+ const char *name_utf8)
+{
+ if (base || directory == nullptr || directory->IsRoot())
+ client_printf(client, "playlist: %s\n", name_utf8);
+ else
+ client_printf(client, "playlist: %s/%s\n",
+ directory->GetPath(), name_utf8);
+}
+
+static bool
+PrintSongBrief(Client &client, bool base, const LightSong &song)
+{
+ song_print_uri(client, song, base);
+
+ if (song.tag->has_playlist)
+ /* this song file has an embedded CUE sheet */
+ print_playlist_in_directory(client, base,
+ song.directory, song.uri);
+
+ return true;
+}
+
+static bool
+PrintSongFull(Client &client, bool base, const LightSong &song)
+{
+ song_print_info(client, song, base);
+
+ if (song.tag->has_playlist)
+ /* this song file has an embedded CUE sheet */
+ print_playlist_in_directory(client, base,
+ song.directory, song.uri);
+
+ return true;
+}
+
+static bool
+PrintPlaylistBrief(Client &client, bool base,
+ const PlaylistInfo &playlist,
+ const LightDirectory &directory)
+{
+ print_playlist_in_directory(client, base,
+ &directory, playlist.name.c_str());
+ return true;
+}
+
+static bool
+PrintPlaylistFull(Client &client, bool base,
+ const PlaylistInfo &playlist,
+ const LightDirectory &directory)
+{
+ print_playlist_in_directory(client, base,
+ &directory, playlist.name.c_str());
+
+ if (playlist.mtime > 0)
+ time_print(client, "Last-Modified", playlist.mtime);
+
+ return true;
+}
+
+bool
+db_selection_print(Client &client, const DatabaseSelection &selection,
+ bool full, bool base, Error &error)
+{
+ const Database *db = client.GetDatabase(error);
+ if (db == nullptr)
+ return false;
+
+ using namespace std::placeholders;
+ const auto d = selection.filter == nullptr
+ ? std::bind(full ? PrintDirectoryFull : PrintDirectoryBrief,
+ std::ref(client), base, _1)
+ : VisitDirectory();
+ const auto s = std::bind(full ? PrintSongFull : PrintSongBrief,
+ std::ref(client), base, _1);
+ const auto p = selection.filter == nullptr
+ ? std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief,
+ std::ref(client), base, _1, _2)
+ : VisitPlaylist();
+
+ return db->Visit(selection, d, s, p, error);
+}
+
+static bool
+PrintSongURIVisitor(Client &client, const LightSong &song)
+{
+ song_print_uri(client, song);
+
+ return true;
+}
+
+static bool
+PrintUniqueTag(Client &client, TagType tag_type,
+ const Tag &tag)
+{
+ const char *value = tag.GetValue(tag_type);
+ assert(value != nullptr);
+ client_printf(client, "%s: %s\n", tag_item_names[tag_type], value);
+
+ for (const auto &item : tag)
+ if (item.type != tag_type)
+ client_printf(client, "%s: %s\n",
+ tag_item_names[item.type], item.value);
+
+ return true;
+}
+
+bool
+PrintUniqueTags(Client &client, unsigned type, uint32_t group_mask,
+ const SongFilter *filter,
+ Error &error)
+{
+ const Database *db = client.GetDatabase(error);
+ if (db == nullptr)
+ return false;
+
+ const DatabaseSelection selection("", true, filter);
+
+ if (type == LOCATE_TAG_FILE_TYPE) {
+ using namespace std::placeholders;
+ const auto f = std::bind(PrintSongURIVisitor,
+ std::ref(client), _1);
+ return db->Visit(selection, f, error);
+ } else {
+ assert(type < TAG_NUM_OF_ITEM_TYPES);
+
+ using namespace std::placeholders;
+ const auto f = std::bind(PrintUniqueTag, std::ref(client),
+ (TagType)type, _1);
+ return db->VisitUniqueTags(selection, (TagType)type,
+ group_mask,
+ f, error);
+ }
+}
diff --git a/src/db/DatabasePrint.hxx b/src/db/DatabasePrint.hxx
new file mode 100644
index 000000000..2ab5e703d
--- /dev/null
+++ b/src/db/DatabasePrint.hxx
@@ -0,0 +1,45 @@
+/*
+ * 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_DB_PRINT_H
+#define MPD_DB_PRINT_H
+
+#include "Compiler.h"
+
+#include <stdint.h>
+
+class SongFilter;
+struct DatabaseSelection;
+class Client;
+class Error;
+
+/**
+ * @param full print attributes/tags
+ * @param base print only base name of songs/directories?
+ */
+bool
+db_selection_print(Client &client, const DatabaseSelection &selection,
+ bool full, bool base, Error &error);
+
+bool
+PrintUniqueTags(Client &client, unsigned type, uint32_t group_mask,
+ const SongFilter *filter,
+ Error &error);
+
+#endif
diff --git a/src/db/DatabaseQueue.cxx b/src/db/DatabaseQueue.cxx
new file mode 100644
index 000000000..490678188
--- /dev/null
+++ b/src/db/DatabaseQueue.cxx
@@ -0,0 +1,53 @@
+/*
+ * 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 "DatabaseQueue.hxx"
+#include "DatabaseSong.hxx"
+#include "Interface.hxx"
+#include "Partition.hxx"
+#include "Instance.hxx"
+#include "DetachedSong.hxx"
+
+#include <functional>
+
+static bool
+AddToQueue(Partition &partition, const LightSong &song, Error &error)
+{
+ const Storage &storage = *partition.instance.storage;
+ unsigned id =
+ partition.playlist.AppendSong(partition.pc,
+ DatabaseDetachSong(storage,
+ song),
+ error);
+ return id != 0;
+}
+
+bool
+AddFromDatabase(Partition &partition, const DatabaseSelection &selection,
+ Error &error)
+{
+ const Database *db = partition.instance.GetDatabase(error);
+ if (db == nullptr)
+ return false;
+
+ using namespace std::placeholders;
+ const auto f = std::bind(AddToQueue, std::ref(partition), _1, _2);
+ return db->Visit(selection, f, error);
+}
diff --git a/src/db/DatabaseQueue.hxx b/src/db/DatabaseQueue.hxx
new file mode 100644
index 000000000..e653f973c
--- /dev/null
+++ b/src/db/DatabaseQueue.hxx
@@ -0,0 +1,31 @@
+/*
+ * 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_DATABASE_QUEUE_HXX
+#define MPD_DATABASE_QUEUE_HXX
+
+struct Partition;
+struct DatabaseSelection;
+class Error;
+
+bool
+AddFromDatabase(Partition &partition, const DatabaseSelection &selection,
+ Error &error);
+
+#endif
diff --git a/src/db/DatabaseSong.cxx b/src/db/DatabaseSong.cxx
new file mode 100644
index 000000000..699213835
--- /dev/null
+++ b/src/db/DatabaseSong.cxx
@@ -0,0 +1,53 @@
+/*
+ * 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 "DatabaseSong.hxx"
+#include "LightSong.hxx"
+#include "Interface.hxx"
+#include "DetachedSong.hxx"
+#include "storage/StorageInterface.hxx"
+
+DetachedSong
+DatabaseDetachSong(const Storage &storage, const LightSong &song)
+{
+ DetachedSong detached(song);
+ assert(detached.IsInDatabase());
+
+ if (!detached.HasRealURI()) {
+ const auto uri = song.GetURI();
+ detached.SetRealURI(storage.MapUTF8(uri.c_str()));
+ }
+
+ return detached;
+}
+
+DetachedSong *
+DatabaseDetachSong(const Database &db, const Storage &storage, const char *uri,
+ Error &error)
+{
+ const LightSong *tmp = db.GetSong(uri, error);
+ if (tmp == nullptr)
+ return nullptr;
+
+ DetachedSong *song = new DetachedSong(DatabaseDetachSong(storage,
+ *tmp));
+ db.ReturnSong(tmp);
+ return song;
+}
diff --git a/src/db/DatabaseSong.hxx b/src/db/DatabaseSong.hxx
new file mode 100644
index 000000000..4daaf4047
--- /dev/null
+++ b/src/db/DatabaseSong.hxx
@@ -0,0 +1,50 @@
+/*
+ * 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_DATABASE_SONG_HXX
+#define MPD_DATABASE_SONG_HXX
+
+#include "Compiler.h"
+
+struct LightSong;
+class Database;
+class Storage;
+class DetachedSong;
+class Error;
+
+/**
+ * "Detach" the #Song object, i.e. convert it to a #DetachedSong
+ * instance.
+ */
+gcc_pure
+DetachedSong
+DatabaseDetachSong(const Storage &storage, const LightSong &song);
+
+/**
+ * Look up a song in the database and convert it to a #DetachedSong
+ * instance. The caller is responsible for freeing it.
+ *
+ * @return nullptr on error
+ */
+gcc_malloc gcc_nonnull_all
+DetachedSong *
+DatabaseDetachSong(const Database &db, const Storage &storage, const char *uri,
+ Error &error);
+
+#endif
diff --git a/src/db/Helpers.cxx b/src/db/Helpers.cxx
new file mode 100644
index 000000000..add4bb98e
--- /dev/null
+++ b/src/db/Helpers.cxx
@@ -0,0 +1,98 @@
+/*
+ * 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 "Helpers.hxx"
+#include "Stats.hxx"
+#include "Interface.hxx"
+#include "LightSong.hxx"
+#include "tag/Tag.hxx"
+
+#include <set>
+
+#include <string.h>
+
+struct StringLess {
+ gcc_pure
+ bool operator()(const char *a, const char *b) const {
+ return strcmp(a, b) < 0;
+ }
+};
+
+typedef std::set<const char *, StringLess> StringSet;
+
+static void
+StatsVisitTag(DatabaseStats &stats, StringSet &artists, StringSet &albums,
+ const Tag &tag)
+{
+ if (!tag.duration.IsNegative())
+ stats.total_duration += tag.duration;
+
+ for (const auto &item : tag) {
+ switch (item.type) {
+ case TAG_ARTIST:
+#if defined(__clang__) || GCC_CHECK_VERSION(4,8)
+ artists.emplace(item.value);
+#else
+ artists.insert(item.value);
+#endif
+ break;
+
+ case TAG_ALBUM:
+#if defined(__clang__) || GCC_CHECK_VERSION(4,8)
+ albums.emplace(item.value);
+#else
+ albums.insert(item.value);
+#endif
+ break;
+
+ default:
+ break;
+ }
+ }
+}
+
+static bool
+StatsVisitSong(DatabaseStats &stats, StringSet &artists, StringSet &albums,
+ const LightSong &song)
+{
+ ++stats.song_count;
+
+ StatsVisitTag(stats, artists, albums, *song.tag);
+
+ return true;
+}
+
+bool
+GetStats(const Database &db, const DatabaseSelection &selection,
+ DatabaseStats &stats, Error &error)
+{
+ stats.Clear();
+
+ StringSet artists, albums;
+ using namespace std::placeholders;
+ const auto f = std::bind(StatsVisitSong,
+ std::ref(stats), std::ref(artists),
+ std::ref(albums), _1);
+ if (!db.Visit(selection, f, error))
+ return false;
+
+ stats.artist_count = artists.size();
+ stats.album_count = albums.size();
+ return true;
+}
diff --git a/src/db/Helpers.hxx b/src/db/Helpers.hxx
new file mode 100644
index 000000000..651bac0e0
--- /dev/null
+++ b/src/db/Helpers.hxx
@@ -0,0 +1,32 @@
+/*
+ * 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_MEMORY_DATABASE_PLUGIN_HXX
+#define MPD_MEMORY_DATABASE_PLUGIN_HXX
+
+class Error;
+class Database;
+struct DatabaseSelection;
+struct DatabaseStats;
+
+bool
+GetStats(const Database &db, const DatabaseSelection &selection,
+ DatabaseStats &stats, Error &error);
+
+#endif
diff --git a/src/db/Interface.hxx b/src/db/Interface.hxx
new file mode 100644
index 000000000..152928c79
--- /dev/null
+++ b/src/db/Interface.hxx
@@ -0,0 +1,138 @@
+/*
+ * 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_DATABASE_INTERFACE_HXX
+#define MPD_DATABASE_INTERFACE_HXX
+
+#include "Visitor.hxx"
+#include "tag/TagType.h"
+#include "Compiler.h"
+
+#include <time.h>
+#include <stdint.h>
+
+struct DatabasePlugin;
+struct DatabaseStats;
+struct DatabaseSelection;
+struct LightSong;
+class Error;
+
+class Database {
+ const DatabasePlugin &plugin;
+
+public:
+ Database(const DatabasePlugin &_plugin)
+ :plugin(_plugin) {}
+
+ /**
+ * Free instance data.
+ */
+ virtual ~Database() {}
+
+ const DatabasePlugin &GetPlugin() const {
+ return plugin;
+ }
+
+ bool IsPlugin(const DatabasePlugin &other) const {
+ return &plugin == &other;
+ }
+
+ /**
+ * Open the database. Read it into memory if applicable.
+ */
+ virtual bool Open(gcc_unused Error &error) {
+ return true;
+ }
+
+ /**
+ * Close the database, free allocated memory.
+ */
+ virtual void Close() {}
+
+ /**
+ * Look up a song (including tag data) in the database. When
+ * you don't need this anymore, call ReturnSong().
+ *
+ * @param uri_utf8 the URI of the song within the music
+ * directory (UTF-8)
+ */
+ virtual const LightSong *GetSong(const char *uri_utf8,
+ Error &error) const = 0;
+
+ /**
+ * Mark the song object as "unused". Call this on objects
+ * returned by GetSong().
+ */
+ virtual void ReturnSong(const LightSong *song) const = 0;
+
+ /**
+ * Visit the selected entities.
+ */
+ virtual bool Visit(const DatabaseSelection &selection,
+ VisitDirectory visit_directory,
+ VisitSong visit_song,
+ VisitPlaylist visit_playlist,
+ Error &error) const = 0;
+
+ bool Visit(const DatabaseSelection &selection,
+ VisitDirectory visit_directory,
+ VisitSong visit_song,
+ Error &error) const {
+ return Visit(selection, visit_directory, visit_song,
+ VisitPlaylist(), error);
+ }
+
+ bool Visit(const DatabaseSelection &selection, VisitSong visit_song,
+ Error &error) const {
+ return Visit(selection, VisitDirectory(), visit_song, error);
+ }
+
+ /**
+ * Visit all unique tag values.
+ */
+ virtual bool VisitUniqueTags(const DatabaseSelection &selection,
+ TagType tag_type, uint32_t group_mask,
+ VisitTag visit_tag,
+ Error &error) const = 0;
+
+ virtual bool GetStats(const DatabaseSelection &selection,
+ DatabaseStats &stats,
+ Error &error) const = 0;
+
+ /**
+ * Update the database. Returns the job id on success, 0 on
+ * error (with #Error set) and 0 if not implemented (#Error
+ * not set).
+ */
+ virtual unsigned Update(gcc_unused const char *uri_utf8,
+ gcc_unused bool discard,
+ gcc_unused Error &error) {
+ /* not implemented: return 0 and don't set an Error */
+ return 0;
+ }
+
+ /**
+ * Returns the time stamp of the last database update.
+ * Returns 0 if that is not not known/available.
+ */
+ gcc_pure
+ virtual time_t GetUpdateStamp() const = 0;
+};
+
+#endif
diff --git a/src/db/LightDirectory.hxx b/src/db/LightDirectory.hxx
new file mode 100644
index 000000000..d134151a4
--- /dev/null
+++ b/src/db/LightDirectory.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_LIGHT_DIRECTORY_HXX
+#define MPD_LIGHT_DIRECTORY_HXX
+
+#include "Compiler.h"
+
+#include <string>
+
+#include <time.h>
+
+struct Tag;
+
+/**
+ * A reference to a directory. Unlike the #Directory class, this one
+ * consists only of pointers. It is supposed to be as light as
+ * possible while still providing all the information MPD has about a
+ * directory. This class does not manage any memory, and the pointers
+ * become invalid quickly. Only to be used to pass around during
+ * well-defined situations.
+ */
+struct LightDirectory {
+ const char *uri;
+
+ time_t mtime;
+
+ constexpr LightDirectory(const char *_uri, time_t _mtime)
+ :uri(_uri), mtime(_mtime) {}
+
+ static constexpr LightDirectory Root() {
+ return LightDirectory("", 0);
+ }
+
+ bool IsRoot() const {
+ return *uri == 0;
+ }
+
+ gcc_pure
+ const char *GetPath() const {
+ return uri;
+ }
+};
+
+#endif
diff --git a/src/db/LightSong.cxx b/src/db/LightSong.cxx
new file mode 100644
index 000000000..5cdebc133
--- /dev/null
+++ b/src/db/LightSong.cxx
@@ -0,0 +1,35 @@
+/*
+ * 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 "LightSong.hxx"
+#include "tag/Tag.hxx"
+
+SignedSongTime
+LightSong::GetDuration() const
+{
+ SongTime a = start_time, b = end_time;
+ if (!b.IsPositive()) {
+ if (tag->duration.IsNegative())
+ return tag->duration;
+
+ b = SongTime(tag->duration);
+ }
+
+ return SignedSongTime(b - a);
+}
diff --git a/src/db/LightSong.hxx b/src/db/LightSong.hxx
new file mode 100644
index 000000000..bbd449fbe
--- /dev/null
+++ b/src/db/LightSong.hxx
@@ -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.
+ */
+
+#ifndef MPD_LIGHT_SONG_HXX
+#define MPD_LIGHT_SONG_HXX
+
+#include "Chrono.hxx"
+#include "Compiler.h"
+
+#include <string>
+
+#include <time.h>
+
+struct Tag;
+
+/**
+ * A reference to a song file. Unlike the other "Song" classes in the
+ * MPD code base, this one consists only of pointers. It is supposed
+ * to be as light as possible while still providing all the
+ * information MPD has about a song file. This class does not manage
+ * any memory, and the pointers become invalid quickly. Only to be
+ * used to pass around during well-defined situations.
+ */
+struct LightSong {
+ /**
+ * If this is not nullptr, then it denotes a prefix for the
+ * #uri. To build the full URI, join directory and uri with a
+ * slash.
+ */
+ const char *directory;
+
+ const char *uri;
+
+ /**
+ * The "real" URI, the one to be used for opening the
+ * resource. If this attribute is nullptr, then #uri (and
+ * #directory) shall be used.
+ *
+ * This attribute is used for songs from the database which
+ * have a relative URI.
+ */
+ const char *real_uri;
+
+ /**
+ * Must not be nullptr.
+ */
+ const Tag *tag;
+
+ time_t mtime;
+
+ /**
+ * Start of this sub-song within the file.
+ */
+ SongTime start_time;
+
+ /**
+ * End of this sub-song within the file.
+ * Unused if zero.
+ */
+ SongTime end_time;
+
+ gcc_pure
+ std::string GetURI() const {
+ if (directory == nullptr)
+ return std::string(uri);
+
+ std::string result(directory);
+ result.push_back('/');
+ result.append(uri);
+ return result;
+ }
+
+ gcc_pure
+ SignedSongTime GetDuration() const;
+};
+
+#endif
diff --git a/src/db/PlaylistInfo.hxx b/src/db/PlaylistInfo.hxx
new file mode 100644
index 000000000..baa6cc361
--- /dev/null
+++ b/src/db/PlaylistInfo.hxx
@@ -0,0 +1,63 @@
+/*
+ * 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_PLAYLIST_INFO_HXX
+#define MPD_PLAYLIST_INFO_HXX
+
+#include "check.h"
+#include "Compiler.h"
+
+#include <string>
+
+#include <sys/time.h>
+
+/**
+ * A directory entry pointing to a playlist file.
+ */
+struct PlaylistInfo {
+ /**
+ * The UTF-8 encoded name of the playlist file.
+ */
+ std::string name;
+
+ time_t mtime;
+
+ class CompareName {
+ const char *const name;
+
+ public:
+ constexpr CompareName(const char *_name):name(_name) {}
+
+ gcc_pure
+ bool operator()(const PlaylistInfo &pi) const {
+ return pi.name.compare(name) == 0;
+ }
+ };
+
+ PlaylistInfo() = default;
+
+ template<typename N>
+ PlaylistInfo(N &&_name, time_t _mtime)
+ :name(std::forward<N>(_name)), mtime(_mtime) {}
+
+ PlaylistInfo(const PlaylistInfo &other) = delete;
+ PlaylistInfo(PlaylistInfo &&) = default;
+};
+
+#endif
diff --git a/src/db/PlaylistVector.cxx b/src/db/PlaylistVector.cxx
new file mode 100644
index 000000000..82a3519d9
--- /dev/null
+++ b/src/db/PlaylistVector.cxx
@@ -0,0 +1,66 @@
+/*
+ * 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 "PlaylistVector.hxx"
+#include "db/DatabaseLock.hxx"
+
+#include <algorithm>
+
+#include <assert.h>
+
+PlaylistVector::iterator
+PlaylistVector::find(const char *name)
+{
+ assert(holding_db_lock());
+ assert(name != nullptr);
+
+ return std::find_if(begin(), end(),
+ PlaylistInfo::CompareName(name));
+}
+
+bool
+PlaylistVector::UpdateOrInsert(PlaylistInfo &&pi)
+{
+ assert(holding_db_lock());
+
+ auto i = find(pi.name.c_str());
+ if (i != end()) {
+ if (pi.mtime == i->mtime)
+ return false;
+
+ i->mtime = pi.mtime;
+ } else
+ push_back(std::move(pi));
+
+ return true;
+}
+
+bool
+PlaylistVector::erase(const char *name)
+{
+ assert(holding_db_lock());
+
+ auto i = find(name);
+ if (i == end())
+ return false;
+
+ erase(i);
+ return true;
+}
diff --git a/src/db/PlaylistVector.hxx b/src/db/PlaylistVector.hxx
new file mode 100644
index 000000000..accd4fd42
--- /dev/null
+++ b/src/db/PlaylistVector.hxx
@@ -0,0 +1,56 @@
+/*
+ * 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_PLAYLIST_VECTOR_HXX
+#define MPD_PLAYLIST_VECTOR_HXX
+
+#include "db/PlaylistInfo.hxx"
+#include "Compiler.h"
+
+#include <list>
+
+class PlaylistVector : protected std::list<PlaylistInfo> {
+protected:
+ /**
+ * Caller must lock the #db_mutex.
+ */
+ gcc_pure
+ iterator find(const char *name);
+
+public:
+ using std::list<PlaylistInfo>::empty;
+ using std::list<PlaylistInfo>::begin;
+ using std::list<PlaylistInfo>::end;
+ using std::list<PlaylistInfo>::push_back;
+ using std::list<PlaylistInfo>::erase;
+
+ /**
+ * Caller must lock the #db_mutex.
+ *
+ * @return true if the vector or one of its items was modified
+ */
+ bool UpdateOrInsert(PlaylistInfo &&pi);
+
+ /**
+ * Caller must lock the #db_mutex.
+ */
+ bool erase(const char *name);
+};
+
+#endif /* SONGVEC_H */
diff --git a/src/db/Registry.cxx b/src/db/Registry.cxx
new file mode 100644
index 000000000..5681a9b82
--- /dev/null
+++ b/src/db/Registry.cxx
@@ -0,0 +1,48 @@
+/*
+ * 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 "Registry.hxx"
+#include "DatabasePlugin.hxx"
+#include "plugins/simple/SimpleDatabasePlugin.hxx"
+#include "plugins/ProxyDatabasePlugin.hxx"
+#include "plugins/upnp/UpnpDatabasePlugin.hxx"
+
+#include <string.h>
+
+const DatabasePlugin *const database_plugins[] = {
+ &simple_db_plugin,
+#ifdef HAVE_LIBMPDCLIENT
+ &proxy_db_plugin,
+#endif
+#ifdef HAVE_LIBUPNP
+ &upnp_db_plugin,
+#endif
+ nullptr
+};
+
+const DatabasePlugin *
+GetDatabasePluginByName(const char *name)
+{
+ for (auto i = database_plugins; *i != nullptr; ++i)
+ if (strcmp((*i)->name, name) == 0)
+ return *i;
+
+ return nullptr;
+}
diff --git a/src/db/Registry.hxx b/src/db/Registry.hxx
new file mode 100644
index 000000000..050842e21
--- /dev/null
+++ b/src/db/Registry.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_DATABASE_REGISTRY_HXX
+#define MPD_DATABASE_REGISTRY_HXX
+
+#include "Compiler.h"
+
+struct DatabasePlugin;
+
+/**
+ * nullptr terminated list of all database plugins which were enabled at
+ * compile time.
+ */
+extern const DatabasePlugin *const database_plugins[];
+
+gcc_pure
+const DatabasePlugin *
+GetDatabasePluginByName(const char *name);
+
+#endif
diff --git a/src/db/Selection.cxx b/src/db/Selection.cxx
new file mode 100644
index 000000000..a886916cb
--- /dev/null
+++ b/src/db/Selection.cxx
@@ -0,0 +1,49 @@
+/*
+ * 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 "Selection.hxx"
+#include "SongFilter.hxx"
+
+DatabaseSelection::DatabaseSelection(const char *_uri, bool _recursive,
+ const SongFilter *_filter)
+ :uri(_uri), recursive(_recursive), filter(_filter)
+{
+ /* optimization: if the caller didn't specify a base URI, pick
+ the one from SongFilter */
+ if (uri.empty() && filter != nullptr)
+ uri = filter->GetBase();
+}
+
+bool
+DatabaseSelection::IsEmpty() const
+{
+ return uri.empty() && (filter == nullptr || filter->IsEmpty());
+}
+
+bool
+DatabaseSelection::HasOtherThanBase() const
+{
+ return filter != nullptr && filter->HasOtherThanBase();
+}
+
+bool
+DatabaseSelection::Match(const LightSong &song) const
+{
+ return filter == nullptr || filter->Match(song);
+}
diff --git a/src/db/Selection.hxx b/src/db/Selection.hxx
new file mode 100644
index 000000000..9802603fc
--- /dev/null
+++ b/src/db/Selection.hxx
@@ -0,0 +1,60 @@
+/*
+ * 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_DATABASE_SELECTION_HXX
+#define MPD_DATABASE_SELECTION_HXX
+
+#include "Compiler.h"
+
+#include <string>
+
+class SongFilter;
+struct LightSong;
+
+struct DatabaseSelection {
+ /**
+ * The base URI of the search (UTF-8). Must not begin or end
+ * with a slash. An empty string searches the whole database.
+ */
+ std::string uri;
+
+ /**
+ * Recursively search all sub directories?
+ */
+ bool recursive;
+
+ const SongFilter *filter;
+
+ DatabaseSelection(const char *_uri, bool _recursive,
+ const SongFilter *_filter=nullptr);
+
+ gcc_pure
+ bool IsEmpty() const;
+
+ /**
+ * Does this selection contain constraints other than "base"?
+ */
+ gcc_pure
+ bool HasOtherThanBase() const;
+
+ gcc_pure
+ bool Match(const LightSong &song) const;
+};
+
+#endif
diff --git a/src/db/SimpleDatabasePlugin.cxx b/src/db/SimpleDatabasePlugin.cxx
deleted file mode 100644
index e7ea7a62d..000000000
--- a/src/db/SimpleDatabasePlugin.cxx
+++ /dev/null
@@ -1,323 +0,0 @@
-/*
- * 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 "SimpleDatabasePlugin.hxx"
-#include "DatabaseSelection.hxx"
-#include "DatabaseHelpers.hxx"
-#include "Directory.hxx"
-#include "SongFilter.hxx"
-#include "DatabaseSave.hxx"
-#include "DatabaseLock.hxx"
-#include "DatabaseError.hxx"
-#include "TextFile.hxx"
-#include "ConfigData.hxx"
-#include "fs/FileSystem.hxx"
-#include "util/Error.hxx"
-#include "util/Domain.hxx"
-#include "Log.hxx"
-
-#include <sys/types.h>
-#include <errno.h>
-
-static constexpr Domain simple_db_domain("simple_db");
-
-Database *
-SimpleDatabase::Create(const config_param &param, Error &error)
-{
- SimpleDatabase *db = new SimpleDatabase();
- if (!db->Configure(param, error)) {
- delete db;
- db = nullptr;
- }
-
- return db;
-}
-
-bool
-SimpleDatabase::Configure(const config_param &param, Error &error)
-{
- path = param.GetBlockPath("path", error);
- if (path.IsNull()) {
- if (!error.IsDefined())
- error.Set(simple_db_domain,
- "No \"path\" parameter specified");
- return false;
- }
-
- path_utf8 = path.ToUTF8();
-
- return true;
-}
-
-bool
-SimpleDatabase::Check(Error &error) const
-{
- assert(!path.IsNull());
-
- /* Check if the file exists */
- if (!CheckAccess(path, F_OK)) {
- /* If the file doesn't exist, we can't check if we can write
- * it, so we are going to try to get the directory path, and
- * see if we can write a file in that */
- const auto dirPath = path.GetDirectoryName();
-
- /* Check that the parent part of the path is a directory */
- struct stat st;
- if (!StatFile(dirPath, st)) {
- error.FormatErrno("Couldn't stat parent directory of db file "
- "\"%s\"",
- path_utf8.c_str());
- return false;
- }
-
- if (!S_ISDIR(st.st_mode)) {
- error.Format(simple_db_domain,
- "Couldn't create db file \"%s\" because the "
- "parent path is not a directory",
- path_utf8.c_str());
- return false;
- }
-
- /* Check if we can write to the directory */
- if (!CheckAccess(dirPath, X_OK | W_OK)) {
- const int e = errno;
- const std::string dirPath_utf8 = dirPath.ToUTF8();
- error.FormatErrno(e, "Can't create db file in \"%s\"",
- dirPath_utf8.c_str());
- return false;
- }
-
- return true;
- }
-
- /* Path exists, now check if it's a regular file */
- struct stat st;
- if (!StatFile(path, st)) {
- error.FormatErrno("Couldn't stat db file \"%s\"",
- path_utf8.c_str());
- return false;
- }
-
- if (!S_ISREG(st.st_mode)) {
- error.Format(simple_db_domain,
- "db file \"%s\" is not a regular file",
- path_utf8.c_str());
- return false;
- }
-
- /* And check that we can write to it */
- if (!CheckAccess(path, R_OK | W_OK)) {
- error.FormatErrno("Can't open db file \"%s\" for reading/writing",
- path_utf8.c_str());
- return false;
- }
-
- return true;
-}
-
-bool
-SimpleDatabase::Load(Error &error)
-{
- assert(!path.IsNull());
- assert(root != nullptr);
-
- TextFile file(path);
- if (file.HasFailed()) {
- error.FormatErrno("Failed to open database file \"%s\"",
- path_utf8.c_str());
- return false;
- }
-
- if (!db_load_internal(file, *root, error))
- return false;
-
- struct stat st;
- if (StatFile(path, st))
- mtime = st.st_mtime;
-
- return true;
-}
-
-bool
-SimpleDatabase::Open(Error &error)
-{
- root = Directory::NewRoot();
- mtime = 0;
-
-#ifndef NDEBUG
- borrowed_song_count = 0;
-#endif
-
- if (!Load(error)) {
- root->Free();
-
- LogError(error);
- error.Clear();
-
- if (!Check(error))
- return false;
-
- root = Directory::NewRoot();
- }
-
- return true;
-}
-
-void
-SimpleDatabase::Close()
-{
- assert(root != nullptr);
- assert(borrowed_song_count == 0);
-
- root->Free();
-}
-
-Song *
-SimpleDatabase::GetSong(const char *uri, Error &error) const
-{
- assert(root != nullptr);
-
- db_lock();
- Song *song = root->LookupSong(uri);
- db_unlock();
- if (song == nullptr)
- error.Format(db_domain, DB_NOT_FOUND,
- "No such song: %s", uri);
-#ifndef NDEBUG
- else
- ++const_cast<unsigned &>(borrowed_song_count);
-#endif
-
- return song;
-}
-
-void
-SimpleDatabase::ReturnSong(gcc_unused Song *song) const
-{
- assert(song != nullptr);
-
-#ifndef NDEBUG
- assert(borrowed_song_count > 0);
- --const_cast<unsigned &>(borrowed_song_count);
-#endif
-}
-
-gcc_pure
-const Directory *
-SimpleDatabase::LookupDirectory(const char *uri) const
-{
- assert(root != nullptr);
- assert(uri != nullptr);
-
- ScopeDatabaseLock protect;
- return root->LookupDirectory(uri);
-}
-
-bool
-SimpleDatabase::Visit(const DatabaseSelection &selection,
- VisitDirectory visit_directory,
- VisitSong visit_song,
- VisitPlaylist visit_playlist,
- Error &error) const
-{
- ScopeDatabaseLock protect;
-
- const Directory *directory = root->LookupDirectory(selection.uri.c_str());
- if (directory == nullptr) {
- if (visit_song) {
- Song *song = root->LookupSong(selection.uri.c_str());
- if (song != nullptr)
- return !selection.Match(*song) ||
- visit_song(*song, error);
- }
-
- error.Set(db_domain, DB_NOT_FOUND, "No such directory");
- return false;
- }
-
- if (selection.recursive && visit_directory &&
- !visit_directory(*directory, error))
- return false;
-
- return directory->Walk(selection.recursive, selection.filter,
- visit_directory, visit_song, visit_playlist,
- error);
-}
-
-bool
-SimpleDatabase::VisitUniqueTags(const DatabaseSelection &selection,
- TagType tag_type,
- VisitString visit_string,
- Error &error) const
-{
- return ::VisitUniqueTags(*this, selection, tag_type, visit_string,
- error);
-}
-
-bool
-SimpleDatabase::GetStats(const DatabaseSelection &selection,
- DatabaseStats &stats, Error &error) const
-{
- return ::GetStats(*this, selection, stats, error);
-}
-
-bool
-SimpleDatabase::Save(Error &error)
-{
- db_lock();
-
- LogDebug(simple_db_domain, "removing empty directories from DB");
- root->PruneEmpty();
-
- LogDebug(simple_db_domain, "sorting DB");
- root->Sort();
-
- db_unlock();
-
- LogDebug(simple_db_domain, "writing DB");
-
- FILE *fp = FOpen(path, FOpenMode::WriteText);
- if (!fp) {
- error.FormatErrno("unable to write to db file \"%s\"",
- path_utf8.c_str());
- return false;
- }
-
- db_save_internal(fp, *root);
-
- if (ferror(fp)) {
- error.SetErrno("Failed to write to database file");
- fclose(fp);
- return false;
- }
-
- fclose(fp);
-
- struct stat st;
- if (StatFile(path, st))
- mtime = st.st_mtime;
-
- return true;
-}
-
-const DatabasePlugin simple_db_plugin = {
- "simple",
- SimpleDatabase::Create,
-};
diff --git a/src/db/Stats.hxx b/src/db/Stats.hxx
new file mode 100644
index 000000000..131a5dc47
--- /dev/null
+++ b/src/db/Stats.hxx
@@ -0,0 +1,53 @@
+/*
+ * 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_DATABASE_STATS_HXX
+#define MPD_DATABASE_STATS_HXX
+
+#include "Chrono.hxx"
+
+struct DatabaseStats {
+ /**
+ * Number of songs.
+ */
+ unsigned song_count;
+
+ /**
+ * Total duration of all songs (in seconds).
+ */
+ std::chrono::duration<std::uint64_t, SongTime::period> total_duration;
+
+ /**
+ * Number of distinct artist names.
+ */
+ unsigned artist_count;
+
+ /**
+ * Number of distinct album names.
+ */
+ unsigned album_count;
+
+ void Clear() {
+ song_count = 0;
+ total_duration = total_duration.zero();
+ artist_count = album_count = 0;
+ }
+};
+
+#endif
diff --git a/src/db/UniqueTags.cxx b/src/db/UniqueTags.cxx
new file mode 100644
index 000000000..589dc936d
--- /dev/null
+++ b/src/db/UniqueTags.cxx
@@ -0,0 +1,59 @@
+/*
+ * 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 "UniqueTags.hxx"
+#include "Interface.hxx"
+#include "LightSong.hxx"
+#include "tag/Set.hxx"
+
+#include <functional>
+
+#include <assert.h>
+
+static bool
+CollectTags(TagSet &set, TagType tag_type, uint32_t group_mask,
+ const LightSong &song)
+{
+ assert(song.tag != nullptr);
+ const Tag &tag = *song.tag;
+
+ set.InsertUnique(tag, tag_type, group_mask);
+ return true;
+}
+
+bool
+VisitUniqueTags(const Database &db, const DatabaseSelection &selection,
+ TagType tag_type, uint32_t group_mask,
+ VisitTag visit_tag,
+ Error &error)
+{
+ TagSet set;
+
+ using namespace std::placeholders;
+ const auto f = std::bind(CollectTags, std::ref(set),
+ tag_type, group_mask, _1);
+ if (!db.Visit(selection, f, error))
+ return false;
+
+ for (const auto &value : set)
+ if (!visit_tag(value, error))
+ return false;
+
+ return true;
+}
diff --git a/src/db/UniqueTags.hxx b/src/db/UniqueTags.hxx
new file mode 100644
index 000000000..61004fc56
--- /dev/null
+++ b/src/db/UniqueTags.hxx
@@ -0,0 +1,38 @@
+/*
+ * 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_DB_UNIQUE_TAGS_HXX
+#define MPD_DB_UNIQUE_TAGS_HXX
+
+#include "Visitor.hxx"
+#include "tag/TagType.h"
+
+#include <stdint.h>
+
+class Error;
+class Database;
+struct DatabaseSelection;
+
+bool
+VisitUniqueTags(const Database &db, const DatabaseSelection &selection,
+ TagType tag_type, uint32_t group_mask,
+ VisitTag visit_tag,
+ Error &error);
+
+#endif
diff --git a/src/db/Uri.hxx b/src/db/Uri.hxx
new file mode 100644
index 000000000..04960ba80
--- /dev/null
+++ b/src/db/Uri.hxx
@@ -0,0 +1,29 @@
+/*
+ * 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_DB_URI_HXX
+#define MPD_DB_URI_HXX
+
+static inline bool
+isRootDirectory(const char *name)
+{
+ return name[0] == 0 || (name[0] == '/' && name[1] == 0);
+}
+
+#endif
diff --git a/src/db/Visitor.hxx b/src/db/Visitor.hxx
new file mode 100644
index 000000000..c524f1722
--- /dev/null
+++ b/src/db/Visitor.hxx
@@ -0,0 +1,38 @@
+/*
+ * 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_DATABASE_VISITOR_HXX
+#define MPD_DATABASE_VISITOR_HXX
+
+#include <functional>
+
+struct LightDirectory;
+struct LightSong;
+struct PlaylistInfo;
+struct Tag;
+class Error;
+
+typedef std::function<bool(const LightDirectory &, Error &)> VisitDirectory;
+typedef std::function<bool(const LightSong &, Error &)> VisitSong;
+typedef std::function<bool(const PlaylistInfo &, const LightDirectory &,
+ Error &)> VisitPlaylist;
+
+typedef std::function<bool(const Tag &, Error &)> VisitTag;
+
+#endif
diff --git a/src/db/plugins/LazyDatabase.cxx b/src/db/plugins/LazyDatabase.cxx
new file mode 100644
index 000000000..bc52395c5
--- /dev/null
+++ b/src/db/plugins/LazyDatabase.cxx
@@ -0,0 +1,108 @@
+/*
+ * 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 "LazyDatabase.hxx"
+#include "db/Interface.hxx"
+
+#include <assert.h>
+
+LazyDatabase::LazyDatabase(Database *_db)
+ :Database(_db->GetPlugin()), db(_db), open(false) {}
+
+LazyDatabase::~LazyDatabase()
+{
+ assert(!open);
+
+ delete db;
+}
+
+bool
+LazyDatabase::EnsureOpen(Error &error) const
+{
+ if (open)
+ return true;
+
+ if (!db->Open(error))
+ return false;
+
+ open = true;
+ return true;
+}
+
+void
+LazyDatabase::Close()
+{
+ if (open) {
+ open = false;
+ db->Close();
+ }
+}
+
+const LightSong *
+LazyDatabase::GetSong(const char *uri, Error &error) const
+{
+ return EnsureOpen(error)
+ ? db->GetSong(uri, error)
+ : nullptr;
+}
+
+void
+LazyDatabase::ReturnSong(const LightSong *song) const
+{
+ assert(open);
+
+ db->ReturnSong(song);
+}
+
+bool
+LazyDatabase::Visit(const DatabaseSelection &selection,
+ VisitDirectory visit_directory,
+ VisitSong visit_song,
+ VisitPlaylist visit_playlist,
+ Error &error) const
+{
+ return EnsureOpen(error) &&
+ db->Visit(selection, visit_directory, visit_song,
+ visit_playlist, error);
+}
+
+bool
+LazyDatabase::VisitUniqueTags(const DatabaseSelection &selection,
+ TagType tag_type, uint32_t group_mask,
+ VisitTag visit_tag,
+ Error &error) const
+{
+ return EnsureOpen(error) &&
+ db->VisitUniqueTags(selection, tag_type, group_mask, visit_tag,
+ error);
+}
+
+bool
+LazyDatabase::GetStats(const DatabaseSelection &selection,
+ DatabaseStats &stats, Error &error) const
+{
+ return EnsureOpen(error) && db->GetStats(selection, stats, error);
+}
+
+time_t
+LazyDatabase::GetUpdateStamp() const
+{
+ return open ? db->GetUpdateStamp() : 0;
+}
diff --git a/src/db/plugins/LazyDatabase.hxx b/src/db/plugins/LazyDatabase.hxx
new file mode 100644
index 000000000..ae1b961d0
--- /dev/null
+++ b/src/db/plugins/LazyDatabase.hxx
@@ -0,0 +1,69 @@
+/*
+ * 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_LAZY_DATABASE_PLUGIN_HXX
+#define MPD_LAZY_DATABASE_PLUGIN_HXX
+
+#include "db/Interface.hxx"
+#include "Compiler.h"
+
+/**
+ * A wrapper for a #Database object that gets opened on the first
+ * database access. This works around daemonization problems with
+ * some plugins.
+ */
+class LazyDatabase final : public Database {
+ Database *const db;
+
+ mutable bool open;
+
+public:
+ gcc_nonnull_all
+ LazyDatabase(Database *_db);
+
+ virtual ~LazyDatabase();
+
+ virtual void Close() override;
+
+ virtual const LightSong *GetSong(const char *uri_utf8,
+ Error &error) const override;
+ virtual void ReturnSong(const LightSong *song) const;
+
+ virtual bool Visit(const DatabaseSelection &selection,
+ VisitDirectory visit_directory,
+ VisitSong visit_song,
+ VisitPlaylist visit_playlist,
+ Error &error) const override;
+
+ virtual bool VisitUniqueTags(const DatabaseSelection &selection,
+ TagType tag_type, uint32_t group_mask,
+ VisitTag visit_tag,
+ Error &error) const override;
+
+ virtual bool GetStats(const DatabaseSelection &selection,
+ DatabaseStats &stats,
+ Error &error) const override;
+
+ virtual time_t GetUpdateStamp() const override;
+
+private:
+ bool EnsureOpen(Error &error) const;
+};
+
+#endif
diff --git a/src/db/ProxyDatabasePlugin.cxx b/src/db/plugins/ProxyDatabasePlugin.cxx
index cb1bcdc6b..2bd04e3ad 100644
--- a/src/db/ProxyDatabasePlugin.cxx
+++ b/src/db/plugins/ProxyDatabasePlugin.cxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * 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
@@ -19,45 +19,91 @@
#include "config.h"
#include "ProxyDatabasePlugin.hxx"
-#include "DatabasePlugin.hxx"
-#include "DatabaseSelection.hxx"
-#include "DatabaseError.hxx"
-#include "PlaylistVector.hxx"
-#include "Directory.hxx"
-#include "Song.hxx"
+#include "db/Interface.hxx"
+#include "db/DatabasePlugin.hxx"
+#include "db/DatabaseListener.hxx"
+#include "db/Selection.hxx"
+#include "db/DatabaseError.hxx"
+#include "db/PlaylistInfo.hxx"
+#include "db/LightDirectory.hxx"
+#include "db/LightSong.hxx"
+#include "db/Stats.hxx"
#include "SongFilter.hxx"
#include "Compiler.h"
-#include "ConfigData.hxx"
+#include "config/ConfigData.hxx"
#include "tag/TagBuilder.hxx"
+#include "tag/Tag.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "protocol/Ack.hxx"
+#include "event/SocketMonitor.hxx"
+#include "event/IdleMonitor.hxx"
+#include "Log.hxx"
#include <mpd/client.h>
+#include <mpd/async.h>
#include <cassert>
#include <string>
#include <list>
-class ProxyDatabase : public Database {
+class ProxySong : public LightSong {
+ Tag tag2;
+
+public:
+ explicit ProxySong(const mpd_song *song);
+};
+
+class AllocatedProxySong : public ProxySong {
+ mpd_song *const song;
+
+public:
+ explicit AllocatedProxySong(mpd_song *_song)
+ :ProxySong(_song), song(_song) {}
+
+ ~AllocatedProxySong() {
+ mpd_song_free(song);
+ }
+};
+
+class ProxyDatabase final : public Database, SocketMonitor, IdleMonitor {
+ DatabaseListener &listener;
+
std::string host;
unsigned port;
struct mpd_connection *connection;
- Directory *root;
/* this is mutable because GetStats() must be "const" */
mutable time_t update_stamp;
+ /**
+ * The libmpdclient idle mask that was removed from the other
+ * MPD. This will be handled by the next OnIdle() call.
+ */
+ unsigned idle_received;
+
+ /**
+ * Is the #connection currently "idle"? That is, did we send
+ * the "idle" command to it?
+ */
+ bool is_idle;
+
public:
- static Database *Create(const config_param &param,
+ ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener)
+ :Database(proxy_db_plugin),
+ SocketMonitor(_loop), IdleMonitor(_loop),
+ listener(_listener) {}
+
+ static Database *Create(EventLoop &loop, DatabaseListener &listener,
+ const config_param &param,
Error &error);
virtual bool Open(Error &error) override;
virtual void Close() override;
- virtual Song *GetSong(const char *uri_utf8,
+ virtual const LightSong *GetSong(const char *uri_utf8,
Error &error) const override;
- virtual void ReturnSong(Song *song) const;
+ virtual void ReturnSong(const LightSong *song) const;
virtual bool Visit(const DatabaseSelection &selection,
VisitDirectory visit_directory,
@@ -66,14 +112,17 @@ public:
Error &error) const override;
virtual bool VisitUniqueTags(const DatabaseSelection &selection,
- TagType tag_type,
- VisitString visit_string,
+ TagType tag_type, uint32_t group_mask,
+ VisitTag visit_tag,
Error &error) const override;
virtual bool GetStats(const DatabaseSelection &selection,
DatabaseStats &stats,
Error &error) const override;
+ virtual unsigned Update(const char *uri_utf8, bool discard,
+ Error &error) override;
+
virtual time_t GetUpdateStamp() const override {
return update_stamp;
}
@@ -84,6 +133,14 @@ private:
bool Connect(Error &error);
bool CheckConnection(Error &error);
bool EnsureConnected(Error &error);
+
+ void Disconnect();
+
+ /* virtual methods from SocketMonitor */
+ virtual bool OnSocketReady(unsigned flags) override;
+
+ /* virtual methods from IdleMonitor */
+ virtual void OnIdle() override;
};
static constexpr Domain libmpdclient_domain("libmpdclient");
@@ -112,6 +169,47 @@ static constexpr struct {
{ TAG_NUM_OF_ITEM_TYPES, MPD_TAG_COUNT }
};
+static void
+Copy(TagBuilder &tag, TagType d_tag,
+ const struct mpd_song *song, enum mpd_tag_type s_tag)
+{
+
+ for (unsigned i = 0;; ++i) {
+ const char *value = mpd_song_get_tag(song, s_tag, i);
+ if (value == nullptr)
+ break;
+
+ tag.AddItem(d_tag, value);
+ }
+}
+
+ProxySong::ProxySong(const mpd_song *song)
+{
+ directory = nullptr;
+ uri = mpd_song_get_uri(song);
+ real_uri = nullptr;
+ tag = &tag2;
+ mtime = mpd_song_get_last_modified(song);
+
+#if LIBMPDCLIENT_CHECK_VERSION(2,3,0)
+ start_time = SongTime::FromS(mpd_song_get_start(song));
+ end_time = SongTime::FromS(mpd_song_get_end(song));
+#else
+ start_time = end_time = SongTime::zero();
+#endif
+
+ TagBuilder tag_builder;
+
+ const unsigned duration = mpd_song_get_duration(song);
+ if (duration > 0)
+ tag_builder.SetDuration(SignedSongTime::FromS(duration));
+
+ for (const auto *i = &tag_table[0]; i->d != TAG_NUM_OF_ITEM_TYPES; ++i)
+ Copy(tag_builder, i->d, song, i->s);
+
+ tag_builder.Commit(tag2);
+}
+
gcc_const
static enum mpd_tag_type
Convert(TagType tag_type)
@@ -217,9 +315,10 @@ SendConstraints(mpd_connection *connection, const DatabaseSelection &selection)
}
Database *
-ProxyDatabase::Create(const config_param &param, Error &error)
+ProxyDatabase::Create(EventLoop &loop, DatabaseListener &listener,
+ const config_param &param, Error &error)
{
- ProxyDatabase *db = new ProxyDatabase();
+ ProxyDatabase *db = new ProxyDatabase(loop, listener);
if (!db->Configure(param, error)) {
delete db;
db = nullptr;
@@ -243,7 +342,6 @@ ProxyDatabase::Open(Error &error)
if (!Connect(error))
return false;
- root = Directory::NewRoot();
update_stamp = 0;
return true;
@@ -252,10 +350,8 @@ ProxyDatabase::Open(Error &error)
void
ProxyDatabase::Close()
{
- root->Free();
-
if (connection != nullptr)
- mpd_connection_free(connection);
+ Disconnect();
}
bool
@@ -270,14 +366,18 @@ ProxyDatabase::Connect(Error &error)
}
if (!CheckError(connection, error)) {
- if (connection != nullptr) {
- mpd_connection_free(connection);
- connection = nullptr;
- }
+ mpd_connection_free(connection);
+ connection = nullptr;
return false;
}
+ idle_received = unsigned(-1);
+ is_idle = false;
+
+ SocketMonitor::Open(mpd_async_get_fd(mpd_connection_get_async(connection)));
+ IdleMonitor::Schedule();
+
return true;
}
@@ -287,10 +387,22 @@ ProxyDatabase::CheckConnection(Error &error)
assert(connection != nullptr);
if (!mpd_connection_clear_error(connection)) {
- mpd_connection_free(connection);
+ Disconnect();
return Connect(error);
}
+ if (is_idle) {
+ unsigned idle = mpd_run_noidle(connection);
+ if (idle == 0 && !CheckError(connection, error)) {
+ Disconnect();
+ return false;
+ }
+
+ idle_received |= idle;
+ is_idle = false;
+ IdleMonitor::Schedule();
+ }
+
return true;
}
@@ -302,10 +414,80 @@ ProxyDatabase::EnsureConnected(Error &error)
: Connect(error);
}
-static Song *
-Convert(const struct mpd_song *song);
+void
+ProxyDatabase::Disconnect()
+{
+ assert(connection != nullptr);
+
+ IdleMonitor::Cancel();
+ SocketMonitor::Steal();
+
+ mpd_connection_free(connection);
+ connection = nullptr;
+}
+
+bool
+ProxyDatabase::OnSocketReady(gcc_unused unsigned flags)
+{
+ assert(connection != nullptr);
+
+ if (!is_idle) {
+ // TODO: can this happen?
+ IdleMonitor::Schedule();
+ return false;
+ }
-Song *
+ unsigned idle = (unsigned)mpd_recv_idle(connection, false);
+ if (idle == 0) {
+ Error error;
+ if (!CheckError(connection, error)) {
+ LogError(error);
+ Disconnect();
+ return false;
+ }
+ }
+
+ /* let OnIdle() handle this */
+ idle_received |= idle;
+ is_idle = false;
+ IdleMonitor::Schedule();
+ return false;
+}
+
+void
+ProxyDatabase::OnIdle()
+{
+ assert(connection != nullptr);
+
+ /* handle previous idle events */
+
+ if (idle_received & MPD_IDLE_DATABASE)
+ listener.OnDatabaseModified();
+
+ idle_received = 0;
+
+ /* send a new idle command to the other MPD */
+
+ if (is_idle)
+ // TODO: can this happen?
+ return;
+
+ if (!mpd_send_idle_mask(connection, MPD_IDLE_DATABASE)) {
+ Error error;
+ if (!CheckError(connection, error))
+ LogError(error);
+
+ SocketMonitor::Steal();
+ mpd_connection_free(connection);
+ connection = nullptr;
+ return;
+ }
+
+ is_idle = true;
+ SocketMonitor::ScheduleRead();
+}
+
+const LightSong *
ProxyDatabase::GetSong(const char *uri, Error &error) const
{
// TODO: eliminate the const_cast
@@ -318,33 +500,29 @@ ProxyDatabase::GetSong(const char *uri, Error &error) const
}
struct mpd_song *song = mpd_recv_song(connection);
- Song *song2 = song != nullptr
- ? Convert(song)
- : nullptr;
- if (song != nullptr)
- mpd_song_free(song);
- if (!mpd_response_finish(connection)) {
- if (song2 != nullptr)
- song2->Free();
-
- CheckError(connection, error);
+ if (!mpd_response_finish(connection) &&
+ !CheckError(connection, error)) {
+ if (song != nullptr)
+ mpd_song_free(song);
return nullptr;
}
- if (song2 == nullptr)
+ if (song == nullptr) {
error.Format(db_domain, DB_NOT_FOUND, "No such song: %s", uri);
+ return nullptr;
+ }
- return song2;
+ return new AllocatedProxySong(song);
}
void
-ProxyDatabase::ReturnSong(Song *song) const
+ProxyDatabase::ReturnSong(const LightSong *_song) const
{
- assert(song != nullptr);
- assert(song->IsInDatabase());
- assert(song->IsDetached());
+ assert(_song != nullptr);
- song->Free();
+ AllocatedProxySong *song = (AllocatedProxySong *)
+ const_cast<LightSong *>(_song);
+ delete song;
}
static bool
@@ -361,14 +539,15 @@ Visit(struct mpd_connection *connection,
VisitPlaylist visit_playlist, Error &error)
{
const char *path = mpd_directory_get_path(directory);
+#if LIBMPDCLIENT_CHECK_VERSION(2,9,0)
+ time_t mtime = mpd_directory_get_last_modified(directory);
+#else
+ time_t mtime = 0;
+#endif
- if (visit_directory) {
- Directory *d = Directory::NewGeneric(path, &detached_root);
- bool success = visit_directory(*d, error);
- d->Free();
- if (!success)
- return false;
- }
+ if (visit_directory &&
+ !visit_directory(LightDirectory(path, mtime), error))
+ return false;
if (recursive &&
!Visit(connection, path, recursive, filter,
@@ -378,65 +557,23 @@ Visit(struct mpd_connection *connection,
return true;
}
-static void
-Copy(TagBuilder &tag, TagType d_tag,
- const struct mpd_song *song, enum mpd_tag_type s_tag)
-{
-
- for (unsigned i = 0;; ++i) {
- const char *value = mpd_song_get_tag(song, s_tag, i);
- if (value == nullptr)
- break;
-
- tag.AddItem(d_tag, value);
- }
-}
-
-static Song *
-Convert(const struct mpd_song *song)
-{
- Song *s = Song::NewDetached(mpd_song_get_uri(song));
-
- s->mtime = mpd_song_get_last_modified(song);
-
-#if LIBMPDCLIENT_CHECK_VERSION(2,3,0)
- s->start_ms = mpd_song_get_start(song) * 1000;
- s->end_ms = mpd_song_get_end(song) * 1000;
-#else
- s->start_ms = s->end_ms = 0;
-#endif
-
- TagBuilder tag;
- tag.SetTime(mpd_song_get_duration(song));
-
- for (const auto *i = &tag_table[0]; i->d != TAG_NUM_OF_ITEM_TYPES; ++i)
- Copy(tag, i->d, song, i->s);
-
- s->tag = tag.Commit();
-
- return s;
-}
-
gcc_pure
static bool
-Match(const SongFilter *filter, const Song &song)
+Match(const SongFilter *filter, const LightSong &song)
{
return filter == nullptr || filter->Match(song);
}
static bool
Visit(const SongFilter *filter,
- const struct mpd_song *song,
+ const mpd_song *_song,
VisitSong visit_song, Error &error)
{
if (!visit_song)
return true;
- Song *s = Convert(song);
- bool success = !Match(filter, *s) || visit_song(*s, error);
- s->Free();
-
- return success;
+ const ProxySong song(_song);
+ return !Match(filter, song) || visit_song(song, error);
}
static bool
@@ -449,7 +586,7 @@ Visit(const struct mpd_playlist *playlist,
PlaylistInfo p(mpd_playlist_get_path(playlist),
mpd_playlist_get_last_modified(playlist));
- return visit_playlist(p, detached_root, error);
+ return visit_playlist(p, LightDirectory::Root(), error);
}
class ProxyEntity {
@@ -554,12 +691,10 @@ SearchSongs(struct mpd_connection *connection,
bool result = true;
struct mpd_song *song;
while (result && (song = mpd_recv_song(connection)) != nullptr) {
- Song *song2 = Convert(song);
- mpd_song_free(song);
+ AllocatedProxySong song2(song);
- result = !Match(selection.filter, *song2) ||
- visit_song(*song2, error);
- song2->Free();
+ result = !Match(selection.filter, song2) ||
+ visit_song(song2, error);
}
mpd_response_finish(connection);
@@ -612,7 +747,8 @@ ProxyDatabase::Visit(const DatabaseSelection &selection,
bool
ProxyDatabase::VisitUniqueTags(const DatabaseSelection &selection,
TagType tag_type,
- VisitString visit_string,
+ gcc_unused uint32_t group_mask,
+ VisitTag visit_tag,
Error &error) const
{
// TODO: eliminate the const_cast
@@ -631,6 +767,8 @@ ProxyDatabase::VisitUniqueTags(const DatabaseSelection &selection,
if (!SendConstraints(connection, selection))
return CheckError(connection, error);
+ // TODO: use group_mask
+
if (!mpd_search_commit(connection))
return CheckError(connection, error);
@@ -639,7 +777,18 @@ ProxyDatabase::VisitUniqueTags(const DatabaseSelection &selection,
struct mpd_pair *pair;
while (result &&
(pair = mpd_recv_pair_tag(connection, tag_type2)) != nullptr) {
- result = visit_string(pair->value, error);
+ TagBuilder tag;
+ tag.AddItem(tag_type, pair->value);
+
+ if (tag.IsEmpty())
+ /* if no tag item has been added, then the
+ given value was not acceptable
+ (e.g. empty); forcefully insert an empty
+ tag in this case, as the caller expects the
+ given tag type to be present */
+ tag.AddEmptyItem(tag_type);
+
+ result = visit_tag(tag.Commit(), error);
mpd_return_pair(connection, pair);
}
@@ -667,7 +816,7 @@ ProxyDatabase::GetStats(const DatabaseSelection &selection,
update_stamp = (time_t)mpd_stats_get_db_update_time(stats2);
stats.song_count = mpd_stats_get_number_of_songs(stats2);
- stats.total_duration = mpd_stats_get_db_play_time(stats2);
+ stats.total_duration = std::chrono::seconds(mpd_stats_get_db_play_time(stats2));
stats.artist_count = mpd_stats_get_number_of_artists(stats2);
stats.album_count = mpd_stats_get_number_of_albums(stats2);
mpd_stats_free(stats2);
@@ -675,7 +824,24 @@ ProxyDatabase::GetStats(const DatabaseSelection &selection,
return true;
}
+unsigned
+ProxyDatabase::Update(const char *uri_utf8, bool discard,
+ Error &error)
+{
+ if (!EnsureConnected(error))
+ return 0;
+
+ unsigned id = discard
+ ? mpd_run_rescan(connection, uri_utf8)
+ : mpd_run_update(connection, uri_utf8);
+ if (id == 0)
+ CheckError(connection, error);
+
+ return id;
+}
+
const DatabasePlugin proxy_db_plugin = {
"proxy",
+ DatabasePlugin::FLAG_REQUIRE_STORAGE,
ProxyDatabase::Create,
};
diff --git a/src/db/ProxyDatabasePlugin.hxx b/src/db/plugins/ProxyDatabasePlugin.hxx
index 576c01c69..699d374b5 100644
--- a/src/db/ProxyDatabasePlugin.hxx
+++ b/src/db/plugins/ProxyDatabasePlugin.hxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * 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
diff --git a/src/db/plugins/simple/DatabaseSave.cxx b/src/db/plugins/simple/DatabaseSave.cxx
new file mode 100644
index 000000000..c766843b6
--- /dev/null
+++ b/src/db/plugins/simple/DatabaseSave.cxx
@@ -0,0 +1,160 @@
+/*
+ * 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 "DatabaseSave.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/DatabaseError.hxx"
+#include "Directory.hxx"
+#include "DirectorySave.hxx"
+#include "fs/io/BufferedOutputStream.hxx"
+#include "fs/io/TextFile.hxx"
+#include "tag/Tag.hxx"
+#include "tag/TagSettings.h"
+#include "fs/Charset.hxx"
+#include "util/StringUtil.hxx"
+#include "util/Error.hxx"
+#include "Log.hxx"
+
+#include <string.h>
+#include <stdlib.h>
+
+#define DIRECTORY_INFO_BEGIN "info_begin"
+#define DIRECTORY_INFO_END "info_end"
+#define DB_FORMAT_PREFIX "format: "
+#define DIRECTORY_MPD_VERSION "mpd_version: "
+#define DIRECTORY_FS_CHARSET "fs_charset: "
+#define DB_TAG_PREFIX "tag: "
+
+static constexpr unsigned DB_FORMAT = 2;
+
+/**
+ * The oldest database format understood by this MPD version.
+ */
+static constexpr unsigned OLDEST_DB_FORMAT = 1;
+
+void
+db_save_internal(BufferedOutputStream &os, const Directory &music_root)
+{
+ os.Format("%s\n", DIRECTORY_INFO_BEGIN);
+ os.Format(DB_FORMAT_PREFIX "%u\n", DB_FORMAT);
+ os.Format("%s%s\n", DIRECTORY_MPD_VERSION, VERSION);
+ os.Format("%s%s\n", DIRECTORY_FS_CHARSET, GetFSCharset());
+
+ for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
+ if (!ignore_tag_items[i])
+ os.Format(DB_TAG_PREFIX "%s\n", tag_item_names[i]);
+
+ os.Format("%s\n", DIRECTORY_INFO_END);
+
+ directory_save(os, music_root);
+}
+
+bool
+db_load_internal(TextFile &file, Directory &music_root, Error &error)
+{
+ char *line;
+ unsigned format = 0;
+ bool found_charset = false, found_version = false;
+ bool success;
+ bool tags[TAG_NUM_OF_ITEM_TYPES];
+
+ /* get initial info */
+ line = file.ReadLine();
+ if (line == nullptr || strcmp(DIRECTORY_INFO_BEGIN, line) != 0) {
+ error.Set(db_domain, "Database corrupted");
+ return false;
+ }
+
+ memset(tags, false, sizeof(tags));
+
+ while ((line = file.ReadLine()) != nullptr &&
+ strcmp(line, DIRECTORY_INFO_END) != 0) {
+ if (StringStartsWith(line, DB_FORMAT_PREFIX)) {
+ format = atoi(line + sizeof(DB_FORMAT_PREFIX) - 1);
+ } else if (StringStartsWith(line, DIRECTORY_MPD_VERSION)) {
+ if (found_version) {
+ error.Set(db_domain, "Duplicate version line");
+ return false;
+ }
+
+ found_version = true;
+ } else if (StringStartsWith(line, DIRECTORY_FS_CHARSET)) {
+ const char *new_charset;
+
+ if (found_charset) {
+ error.Set(db_domain, "Duplicate charset line");
+ return false;
+ }
+
+ found_charset = true;
+
+ new_charset = line + sizeof(DIRECTORY_FS_CHARSET) - 1;
+ const char *const old_charset = GetFSCharset();
+ if (*old_charset != 0
+ && strcmp(new_charset, old_charset) != 0) {
+ error.Format(db_domain,
+ "Existing database has charset "
+ "\"%s\" instead of \"%s\"; "
+ "discarding database file",
+ new_charset, old_charset);
+ return false;
+ }
+ } else if (StringStartsWith(line, DB_TAG_PREFIX)) {
+ const char *name = line + sizeof(DB_TAG_PREFIX) - 1;
+ TagType tag = tag_name_parse(name);
+ if (tag == TAG_NUM_OF_ITEM_TYPES) {
+ error.Format(db_domain,
+ "Unrecognized tag '%s', "
+ "discarding database file",
+ name);
+ return false;
+ }
+
+ tags[tag] = true;
+ } else {
+ error.Format(db_domain, "Malformed line: %s", line);
+ return false;
+ }
+ }
+
+ if (format < OLDEST_DB_FORMAT || format > DB_FORMAT) {
+ error.Set(db_domain,
+ "Database format mismatch, "
+ "discarding database file");
+ return false;
+ }
+
+ for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) {
+ if (!ignore_tag_items[i] && !tags[i]) {
+ error.Set(db_domain,
+ "Tag list mismatch, "
+ "discarding database file");
+ return false;
+ }
+ }
+
+ LogDebug(db_domain, "reading DB");
+
+ db_lock();
+ success = directory_load(file, music_root, error);
+ db_unlock();
+
+ return success;
+}
diff --git a/src/db/plugins/simple/DatabaseSave.hxx b/src/db/plugins/simple/DatabaseSave.hxx
new file mode 100644
index 000000000..bb7f57115
--- /dev/null
+++ b/src/db/plugins/simple/DatabaseSave.hxx
@@ -0,0 +1,34 @@
+/*
+ * 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_DATABASE_SAVE_HXX
+#define MPD_DATABASE_SAVE_HXX
+
+struct Directory;
+class BufferedOutputStream;
+class TextFile;
+class Error;
+
+void
+db_save_internal(BufferedOutputStream &os, const Directory &root);
+
+bool
+db_load_internal(TextFile &file, Directory &root, Error &error);
+
+#endif
diff --git a/src/db/plugins/simple/Directory.cxx b/src/db/plugins/simple/Directory.cxx
new file mode 100644
index 000000000..218652b03
--- /dev/null
+++ b/src/db/plugins/simple/Directory.cxx
@@ -0,0 +1,277 @@
+/*
+ * 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 "Directory.hxx"
+#include "SongSort.hxx"
+#include "Song.hxx"
+#include "Mount.hxx"
+#include "db/LightDirectory.hxx"
+#include "db/LightSong.hxx"
+#include "db/Uri.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/Interface.hxx"
+#include "SongFilter.hxx"
+#include "lib/icu/Collate.hxx"
+#include "fs/Traits.hxx"
+#include "util/Alloc.hxx"
+#include "util/Error.hxx"
+
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+
+Directory::Directory(std::string &&_path_utf8, Directory *_parent)
+ :parent(_parent),
+ mtime(0),
+ inode(0), device(0),
+ path(std::move(_path_utf8)),
+ mounted_database(nullptr)
+{
+}
+
+Directory::~Directory()
+{
+ delete mounted_database;
+
+ songs.clear_and_dispose(Song::Disposer());
+ children.clear_and_dispose(Disposer());
+}
+
+void
+Directory::Delete()
+{
+ assert(holding_db_lock());
+ assert(parent != nullptr);
+
+ parent->children.erase_and_dispose(parent->children.iterator_to(*this),
+ Disposer());
+}
+
+const char *
+Directory::GetName() const
+{
+ assert(!IsRoot());
+
+ return PathTraitsUTF8::GetBase(path.c_str());
+}
+
+Directory *
+Directory::CreateChild(const char *name_utf8)
+{
+ assert(holding_db_lock());
+ assert(name_utf8 != nullptr);
+ assert(*name_utf8 != 0);
+
+ std::string path_utf8 = IsRoot()
+ ? std::string(name_utf8)
+ : PathTraitsUTF8::Build(GetPath(), name_utf8);
+
+ Directory *child = new Directory(std::move(path_utf8), this);
+ children.push_back(*child);
+ return child;
+}
+
+const Directory *
+Directory::FindChild(const char *name) const
+{
+ assert(holding_db_lock());
+
+ for (const auto &child : children)
+ if (strcmp(child.GetName(), name) == 0)
+ return &child;
+
+ return nullptr;
+}
+
+void
+Directory::PruneEmpty()
+{
+ assert(holding_db_lock());
+
+ for (auto child = children.begin(), end = children.end();
+ child != end;) {
+ child->PruneEmpty();
+
+ if (child->IsEmpty())
+ child = children.erase_and_dispose(child, Disposer());
+ else
+ ++child;
+ }
+}
+
+Directory::LookupResult
+Directory::LookupDirectory(const char *uri)
+{
+ assert(holding_db_lock());
+ assert(uri != nullptr);
+
+ if (isRootDirectory(uri))
+ return { this, nullptr };
+
+ char *duplicated = xstrdup(uri), *name = duplicated;
+
+ Directory *d = this;
+ while (true) {
+ char *slash = strchr(name, '/');
+ if (slash == name)
+ break;
+
+ if (slash != nullptr)
+ *slash = '\0';
+
+ Directory *tmp = d->FindChild(name);
+ if (tmp == nullptr)
+ /* not found */
+ break;
+
+ d = tmp;
+
+ if (slash == nullptr) {
+ /* found everything */
+ name = nullptr;
+ break;
+ }
+
+ name = slash + 1;
+ }
+
+ free(duplicated);
+
+ const char *rest = name == nullptr
+ ? nullptr
+ : uri + (name - duplicated);
+
+ return { d, rest };
+}
+
+void
+Directory::AddSong(Song *song)
+{
+ assert(holding_db_lock());
+ assert(song != nullptr);
+ assert(song->parent == this);
+
+ songs.push_back(*song);
+}
+
+void
+Directory::RemoveSong(Song *song)
+{
+ assert(holding_db_lock());
+ assert(song != nullptr);
+ assert(song->parent == this);
+
+ songs.erase(songs.iterator_to(*song));
+}
+
+const Song *
+Directory::FindSong(const char *name_utf8) const
+{
+ assert(holding_db_lock());
+ assert(name_utf8 != nullptr);
+
+ for (auto &song : songs) {
+ assert(song.parent == this);
+
+ if (strcmp(song.uri, name_utf8) == 0)
+ return &song;
+ }
+
+ return nullptr;
+}
+
+gcc_pure
+static bool
+directory_cmp(const Directory &a, const Directory &b)
+{
+ return IcuCollate(a.path.c_str(), b.path.c_str()) < 0;
+}
+
+void
+Directory::Sort()
+{
+ assert(holding_db_lock());
+
+ children.sort(directory_cmp);
+ song_list_sort(songs);
+
+ for (auto &child : children)
+ child.Sort();
+}
+
+bool
+Directory::Walk(bool recursive, const SongFilter *filter,
+ VisitDirectory visit_directory, VisitSong visit_song,
+ VisitPlaylist visit_playlist,
+ Error &error) const
+{
+ assert(!error.IsDefined());
+
+ if (IsMount()) {
+ assert(IsEmpty());
+
+ /* TODO: eliminate this unlock/lock; it is necessary
+ because the child's SimpleDatabasePlugin::Visit()
+ call will lock it again */
+ db_unlock();
+ bool result = WalkMount(GetPath(), *mounted_database,
+ recursive, filter,
+ visit_directory, visit_song,
+ visit_playlist,
+ error);
+ db_lock();
+ return result;
+ }
+
+ if (visit_song) {
+ for (auto &song : songs){
+ const LightSong song2 = song.Export();
+ if ((filter == nullptr || filter->Match(song2)) &&
+ !visit_song(song2, error))
+ return false;
+ }
+ }
+
+ if (visit_playlist) {
+ for (const PlaylistInfo &p : playlists)
+ if (!visit_playlist(p, Export(), error))
+ return false;
+ }
+
+ for (auto &child : children) {
+ if (visit_directory &&
+ !visit_directory(child.Export(), error))
+ return false;
+
+ if (recursive &&
+ !child.Walk(recursive, filter,
+ visit_directory, visit_song, visit_playlist,
+ error))
+ return false;
+ }
+
+ return true;
+}
+
+LightDirectory
+Directory::Export() const
+{
+ return LightDirectory(GetPath(), mtime);
+}
diff --git a/src/db/plugins/simple/Directory.hxx b/src/db/plugins/simple/Directory.hxx
new file mode 100644
index 000000000..acef62143
--- /dev/null
+++ b/src/db/plugins/simple/Directory.hxx
@@ -0,0 +1,285 @@
+/*
+ * 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_DIRECTORY_HXX
+#define MPD_DIRECTORY_HXX
+
+#include "check.h"
+#include "Compiler.h"
+#include "db/Visitor.hxx"
+#include "db/PlaylistVector.hxx"
+#include "Song.hxx"
+
+#include <boost/intrusive/list.hpp>
+
+#include <string>
+
+/**
+ * Virtual directory that is really an archive file or a folder inside
+ * the archive (special value for Directory::device).
+ */
+static constexpr unsigned DEVICE_INARCHIVE = -1;
+
+/**
+ * Virtual directory that is really a song file with one or more "sub"
+ * songs as specified by DecoderPlugin::container_scan() (special
+ * value for Directory::device).
+ */
+static constexpr unsigned DEVICE_CONTAINER = -2;
+
+struct db_visitor;
+class SongFilter;
+class Error;
+class Database;
+
+struct Directory {
+ static constexpr auto link_mode = boost::intrusive::normal_link;
+ typedef boost::intrusive::link_mode<link_mode> LinkMode;
+ typedef boost::intrusive::list_member_hook<LinkMode> Hook;
+
+ struct Disposer {
+ void operator()(Directory *directory) const {
+ delete directory;
+ }
+ };
+
+ /**
+ * Pointers to the siblings of this directory within the
+ * parent directory. It is unused (undefined) in the root
+ * directory.
+ *
+ * This attribute is protected with the global #db_mutex.
+ * Read access in the update thread does not need protection.
+ */
+ Hook siblings;
+
+ typedef boost::intrusive::member_hook<Directory, Hook,
+ &Directory::siblings> SiblingsHook;
+ typedef boost::intrusive::list<Directory, SiblingsHook,
+ boost::intrusive::constant_time_size<false>> List;
+
+ /**
+ * A doubly linked list of child directories.
+ *
+ * This attribute is protected with the global #db_mutex.
+ * Read access in the update thread does not need protection.
+ */
+ List children;
+
+ /**
+ * A doubly linked list of songs within this directory.
+ *
+ * This attribute is protected with the global #db_mutex.
+ * Read access in the update thread does not need protection.
+ */
+ SongList songs;
+
+ PlaylistVector playlists;
+
+ Directory *parent;
+ time_t mtime;
+ unsigned inode, device;
+
+ std::string path;
+
+ /**
+ * If this is not nullptr, then this directory does not really
+ * exist, but is a mount point for another #Database.
+ */
+ Database *mounted_database;
+
+public:
+ Directory(std::string &&_path_utf8, Directory *_parent);
+ ~Directory();
+
+ /**
+ * Create a new root #Directory object.
+ */
+ gcc_malloc
+ static Directory *NewRoot() {
+ return new Directory(std::string(), nullptr);
+ }
+
+ bool IsMount() const {
+ return mounted_database != nullptr;
+ }
+
+ /**
+ * Remove this #Directory object from its parent and free it. This
+ * must not be called with the root Directory.
+ *
+ * Caller must lock the #db_mutex.
+ */
+ void Delete();
+
+ /**
+ * Create a new #Directory object as a child of the given one.
+ *
+ * Caller must lock the #db_mutex.
+ *
+ * @param name_utf8 the UTF-8 encoded name of the new sub directory
+ */
+ gcc_malloc
+ Directory *CreateChild(const char *name_utf8);
+
+ /**
+ * Caller must lock the #db_mutex.
+ */
+ gcc_pure
+ const Directory *FindChild(const char *name) const;
+
+ gcc_pure
+ Directory *FindChild(const char *name) {
+ const Directory *cthis = this;
+ return const_cast<Directory *>(cthis->FindChild(name));
+ }
+
+ /**
+ * Look up a sub directory, and create the object if it does not
+ * exist.
+ *
+ * Caller must lock the #db_mutex.
+ */
+ Directory *MakeChild(const char *name_utf8) {
+ Directory *child = FindChild(name_utf8);
+ if (child == nullptr)
+ child = CreateChild(name_utf8);
+ return child;
+ }
+
+ struct LookupResult {
+ /**
+ * The last directory that was found. If the given
+ * URI could not be resolved at all, then this is the
+ * root directory.
+ */
+ Directory *directory;
+
+ /**
+ * The remaining URI part (without leading slash) or
+ * nullptr if the given URI was consumed completely.
+ */
+ const char *uri;
+ };
+
+ /**
+ * Looks up a directory by its relative URI.
+ *
+ * @param uri the relative URI
+ * @return the Directory, or nullptr if none was found
+ */
+ gcc_pure
+ LookupResult LookupDirectory(const char *uri);
+
+ gcc_pure
+ bool IsEmpty() const {
+ return children.empty() &&
+ songs.empty() &&
+ playlists.empty();
+ }
+
+ gcc_pure
+ const char *GetPath() const {
+ return path.c_str();
+ }
+
+ /**
+ * Returns the base name of the directory.
+ */
+ gcc_pure
+ const char *GetName() const;
+
+ /**
+ * Is this the root directory of the music database?
+ */
+ gcc_pure
+ bool IsRoot() const {
+ return parent == nullptr;
+ }
+
+ template<typename T>
+ void ForEachChildSafe(T &&t) {
+ const auto end = children.end();
+ for (auto i = children.begin(), next = i; i != end; i = next) {
+ next = std::next(i);
+ t(*i);
+ }
+ }
+
+ template<typename T>
+ void ForEachSongSafe(T &&t) {
+ const auto end = songs.end();
+ for (auto i = songs.begin(), next = i; i != end; i = next) {
+ next = std::next(i);
+ t(*i);
+ }
+ }
+
+ /**
+ * Look up a song in this directory by its name.
+ *
+ * Caller must lock the #db_mutex.
+ */
+ gcc_pure
+ const Song *FindSong(const char *name_utf8) const;
+
+ gcc_pure
+ Song *FindSong(const char *name_utf8) {
+ const Directory *cthis = this;
+ return const_cast<Song *>(cthis->FindSong(name_utf8));
+ }
+
+ /**
+ * Add a song object to this directory. Its "parent" attribute must
+ * be set already.
+ */
+ void AddSong(Song *song);
+
+ /**
+ * Remove a song object from this directory (which effectively
+ * invalidates the song object, because the "parent" attribute becomes
+ * stale), but does not free it.
+ */
+ void RemoveSong(Song *song);
+
+ /**
+ * Caller must lock the #db_mutex.
+ */
+ void PruneEmpty();
+
+ /**
+ * Sort all directory entries recursively.
+ *
+ * Caller must lock the #db_mutex.
+ */
+ void Sort();
+
+ /**
+ * Caller must lock #db_mutex.
+ */
+ bool Walk(bool recursive, const SongFilter *match,
+ VisitDirectory visit_directory, VisitSong visit_song,
+ VisitPlaylist visit_playlist,
+ Error &error) const;
+
+ gcc_pure
+ LightDirectory Export() const;
+};
+
+#endif
diff --git a/src/db/plugins/simple/DirectorySave.cxx b/src/db/plugins/simple/DirectorySave.cxx
new file mode 100644
index 000000000..e1650cbe8
--- /dev/null
+++ b/src/db/plugins/simple/DirectorySave.cxx
@@ -0,0 +1,207 @@
+/*
+ * 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 "DirectorySave.hxx"
+#include "Directory.hxx"
+#include "Song.hxx"
+#include "SongSave.hxx"
+#include "DetachedSong.hxx"
+#include "PlaylistDatabase.hxx"
+#include "fs/io/TextFile.hxx"
+#include "fs/io/BufferedOutputStream.hxx"
+#include "util/StringUtil.hxx"
+#include "util/NumberParser.hxx"
+#include "util/Error.hxx"
+#include "util/Domain.hxx"
+
+#include <stddef.h>
+#include <string.h>
+
+#define DIRECTORY_DIR "directory: "
+#define DIRECTORY_TYPE "type: "
+#define DIRECTORY_MTIME "mtime: "
+#define DIRECTORY_BEGIN "begin: "
+#define DIRECTORY_END "end: "
+
+static constexpr Domain directory_domain("directory");
+
+gcc_const
+static const char *
+DeviceToTypeString(unsigned device)
+{
+ switch (device) {
+ case DEVICE_INARCHIVE:
+ return "archive";
+
+ case DEVICE_CONTAINER:
+ return "container";
+
+ default:
+ return nullptr;
+ }
+}
+
+gcc_pure
+static unsigned
+ParseTypeString(const char *type)
+{
+ if (strcmp(type, "archive") == 0)
+ return DEVICE_INARCHIVE;
+ else if (strcmp(type, "container") == 0)
+ return DEVICE_CONTAINER;
+ else
+ return 0;
+}
+
+void
+directory_save(BufferedOutputStream &os, const Directory &directory)
+{
+ if (!directory.IsRoot()) {
+ const char *type = DeviceToTypeString(directory.device);
+ if (type != nullptr)
+ os.Format(DIRECTORY_TYPE "%s\n", type);
+
+ if (directory.mtime != 0)
+ os.Format(DIRECTORY_MTIME "%lu\n",
+ (unsigned long)directory.mtime);
+
+ os.Format("%s%s\n", DIRECTORY_BEGIN, directory.GetPath());
+ }
+
+ for (const auto &child : directory.children) {
+ os.Format(DIRECTORY_DIR "%s\n", child.GetName());
+
+ if (!child.IsMount())
+ directory_save(os, child);
+
+ if (!os.Check())
+ return;
+ }
+
+ for (const auto &song : directory.songs)
+ song_save(os, song);
+
+ playlist_vector_save(os, directory.playlists);
+
+ if (!directory.IsRoot())
+ os.Format(DIRECTORY_END "%s\n", directory.GetPath());
+}
+
+static bool
+ParseLine(Directory &directory, const char *line)
+{
+ if (StringStartsWith(line, DIRECTORY_MTIME)) {
+ directory.mtime =
+ ParseUint64(line + sizeof(DIRECTORY_MTIME) - 1);
+ } else if (StringStartsWith(line, DIRECTORY_TYPE)) {
+ directory.device =
+ ParseTypeString(line + sizeof(DIRECTORY_TYPE) - 1);
+ } else
+ return false;
+
+ return true;
+}
+
+static Directory *
+directory_load_subdir(TextFile &file, Directory &parent, const char *name,
+ Error &error)
+{
+ bool success;
+
+ if (parent.FindChild(name) != nullptr) {
+ error.Format(directory_domain,
+ "Duplicate subdirectory '%s'", name);
+ return nullptr;
+ }
+
+ Directory *directory = parent.CreateChild(name);
+
+ while (true) {
+ const char *line = file.ReadLine();
+ if (line == nullptr) {
+ error.Set(directory_domain, "Unexpected end of file");
+ directory->Delete();
+ return nullptr;
+ }
+
+ if (StringStartsWith(line, DIRECTORY_BEGIN))
+ break;
+
+ if (!ParseLine(*directory, line)) {
+ error.Format(directory_domain,
+ "Malformed line: %s", line);
+ directory->Delete();
+ return nullptr;
+ }
+ }
+
+ success = directory_load(file, *directory, error);
+ if (!success) {
+ directory->Delete();
+ return nullptr;
+ }
+
+ return directory;
+}
+
+bool
+directory_load(TextFile &file, Directory &directory, Error &error)
+{
+ const char *line;
+
+ while ((line = file.ReadLine()) != nullptr &&
+ !StringStartsWith(line, DIRECTORY_END)) {
+ if (StringStartsWith(line, DIRECTORY_DIR)) {
+ Directory *subdir =
+ directory_load_subdir(file, directory,
+ line + sizeof(DIRECTORY_DIR) - 1,
+ error);
+ if (subdir == nullptr)
+ return false;
+ } else if (StringStartsWith(line, SONG_BEGIN)) {
+ const char *name = line + sizeof(SONG_BEGIN) - 1;
+
+ if (directory.FindSong(name) != nullptr) {
+ error.Format(directory_domain,
+ "Duplicate song '%s'", name);
+ return false;
+ }
+
+ DetachedSong *song = song_load(file, name, error);
+ if (song == nullptr)
+ return false;
+
+ directory.AddSong(Song::NewFrom(std::move(*song),
+ directory));
+ delete song;
+ } else if (StringStartsWith(line, PLAYLIST_META_BEGIN)) {
+ const char *name = line + sizeof(PLAYLIST_META_BEGIN) - 1;
+ if (!playlist_metadata_load(file, directory.playlists,
+ name, error))
+ return false;
+ } else {
+ error.Format(directory_domain,
+ "Malformed line: %s", line);
+ return false;
+ }
+ }
+
+ return true;
+}
diff --git a/src/db/plugins/simple/DirectorySave.hxx b/src/db/plugins/simple/DirectorySave.hxx
new file mode 100644
index 000000000..f464f9946
--- /dev/null
+++ b/src/db/plugins/simple/DirectorySave.hxx
@@ -0,0 +1,34 @@
+/*
+ * 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_DIRECTORY_SAVE_HXX
+#define MPD_DIRECTORY_SAVE_HXX
+
+struct Directory;
+class TextFile;
+class BufferedOutputStream;
+class Error;
+
+void
+directory_save(BufferedOutputStream &os, const Directory &directory);
+
+bool
+directory_load(TextFile &file, Directory &directory, Error &error);
+
+#endif
diff --git a/src/db/plugins/simple/Mount.cxx b/src/db/plugins/simple/Mount.cxx
new file mode 100644
index 000000000..96c7bbb5c
--- /dev/null
+++ b/src/db/plugins/simple/Mount.cxx
@@ -0,0 +1,96 @@
+/*
+ * 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 "Mount.hxx"
+#include "PrefixedLightSong.hxx"
+#include "db/Selection.hxx"
+#include "db/LightDirectory.hxx"
+#include "db/LightSong.hxx"
+#include "db/Interface.hxx"
+#include "fs/Traits.hxx"
+#include "util/Error.hxx"
+
+#include <string>
+
+struct PrefixedLightDirectory : LightDirectory {
+ std::string buffer;
+
+ PrefixedLightDirectory(const LightDirectory &directory,
+ const char *base)
+ :LightDirectory(directory),
+ buffer(IsRoot()
+ ? std::string(base)
+ : PathTraitsUTF8::Build(base, uri)) {
+ uri = buffer.c_str();
+ }
+};
+
+static bool
+PrefixVisitDirectory(const char *base, const VisitDirectory &visit_directory,
+ const LightDirectory &directory, Error &error)
+{
+ return visit_directory(PrefixedLightDirectory(directory, base), error);
+}
+
+static bool
+PrefixVisitSong(const char *base, const VisitSong &visit_song,
+ const LightSong &song, Error &error)
+{
+ return visit_song(PrefixedLightSong(song, base), error);
+}
+
+static bool
+PrefixVisitPlaylist(const char *base, const VisitPlaylist &visit_playlist,
+ const PlaylistInfo &playlist,
+ const LightDirectory &directory,
+ Error &error)
+{
+ return visit_playlist(playlist,
+ PrefixedLightDirectory(directory, base),
+ error);
+}
+
+bool
+WalkMount(const char *base, const Database &db,
+ bool recursive, const SongFilter *filter,
+ const VisitDirectory &visit_directory, const VisitSong &visit_song,
+ const VisitPlaylist &visit_playlist,
+ Error &error)
+{
+ using namespace std::placeholders;
+
+ VisitDirectory vd;
+ if (visit_directory)
+ vd = std::bind(PrefixVisitDirectory,
+ base, std::ref(visit_directory), _1, _2);
+
+ VisitSong vs;
+ if (visit_song)
+ vs = std::bind(PrefixVisitSong,
+ base, std::ref(visit_song), _1, _2);
+
+ VisitPlaylist vp;
+ if (visit_playlist)
+ vp = std::bind(PrefixVisitPlaylist,
+ base, std::ref(visit_playlist), _1, _2, _3);
+
+ return db.Visit(DatabaseSelection("", recursive, filter),
+ vd, vs, vp, error);
+}
diff --git a/src/db/plugins/simple/Mount.hxx b/src/db/plugins/simple/Mount.hxx
new file mode 100644
index 000000000..a4690114c
--- /dev/null
+++ b/src/db/plugins/simple/Mount.hxx
@@ -0,0 +1,36 @@
+/*
+ * 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_DB_SIMPLE_MOUNT_HXX
+#define MPD_DB_SIMPLE_MOUNT_HXX
+
+#include "db/Visitor.hxx"
+
+class Database;
+class SongFilter;
+class Error;
+
+bool
+WalkMount(const char *base, const Database &db,
+ bool recursive, const SongFilter *filter,
+ const VisitDirectory &visit_directory, const VisitSong &visit_song,
+ const VisitPlaylist &visit_playlist,
+ Error &error);
+
+#endif
diff --git a/src/db/plugins/simple/PrefixedLightSong.hxx b/src/db/plugins/simple/PrefixedLightSong.hxx
new file mode 100644
index 000000000..3664de001
--- /dev/null
+++ b/src/db/plugins/simple/PrefixedLightSong.hxx
@@ -0,0 +1,41 @@
+/*
+ * 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_DB_SIMPLE_PREFIXED_LIGHT_SONG_HXX
+#define MPD_DB_SIMPLE_PREFIXED_LIGHT_SONG_HXX
+
+#include "check.h"
+#include "db/LightSong.hxx"
+#include "fs/Traits.hxx"
+
+#include <string>
+
+class PrefixedLightSong : public LightSong {
+ std::string buffer;
+
+public:
+ PrefixedLightSong(const LightSong &song, const char *base)
+ :LightSong(song),
+ buffer(PathTraitsUTF8::Build(base, GetURI().c_str())) {
+ uri = buffer.c_str();
+ directory = nullptr;
+ }
+};
+
+#endif
diff --git a/src/db/plugins/simple/SimpleDatabasePlugin.cxx b/src/db/plugins/simple/SimpleDatabasePlugin.cxx
new file mode 100644
index 000000000..7b1886f1c
--- /dev/null
+++ b/src/db/plugins/simple/SimpleDatabasePlugin.cxx
@@ -0,0 +1,541 @@
+/*
+ * 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 "SimpleDatabasePlugin.hxx"
+#include "PrefixedLightSong.hxx"
+#include "db/DatabasePlugin.hxx"
+#include "db/Selection.hxx"
+#include "db/Helpers.hxx"
+#include "db/UniqueTags.hxx"
+#include "db/LightDirectory.hxx"
+#include "Directory.hxx"
+#include "Song.hxx"
+#include "SongFilter.hxx"
+#include "DatabaseSave.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/DatabaseError.hxx"
+#include "fs/io/TextFile.hxx"
+#include "fs/io/BufferedOutputStream.hxx"
+#include "fs/io/FileOutputStream.hxx"
+#include "config/ConfigData.hxx"
+#include "fs/FileSystem.hxx"
+#include "util/CharUtil.hxx"
+#include "util/Error.hxx"
+#include "util/Domain.hxx"
+#include "Log.hxx"
+
+#ifdef HAVE_ZLIB
+#include "fs/io/GzipOutputStream.hxx"
+#endif
+
+#include <errno.h>
+
+static constexpr Domain simple_db_domain("simple_db");
+
+inline SimpleDatabase::SimpleDatabase()
+ :Database(simple_db_plugin),
+ path(AllocatedPath::Null()),
+#ifdef HAVE_ZLIB
+ compress(true),
+#endif
+ cache_path(AllocatedPath::Null()),
+ prefixed_light_song(nullptr) {}
+
+inline SimpleDatabase::SimpleDatabase(AllocatedPath &&_path,
+#ifndef HAVE_ZLIB
+ gcc_unused
+#endif
+ bool _compress)
+ :Database(simple_db_plugin),
+ path(std::move(_path)),
+ path_utf8(path.ToUTF8()),
+#ifdef HAVE_ZLIB
+ compress(_compress),
+#endif
+ cache_path(AllocatedPath::Null()),
+ prefixed_light_song(nullptr) {
+}
+
+Database *
+SimpleDatabase::Create(gcc_unused EventLoop &loop,
+ gcc_unused DatabaseListener &listener,
+ const config_param &param, Error &error)
+{
+ SimpleDatabase *db = new SimpleDatabase();
+ if (!db->Configure(param, error)) {
+ delete db;
+ db = nullptr;
+ }
+
+ return db;
+}
+
+bool
+SimpleDatabase::Configure(const config_param &param, Error &error)
+{
+ path = param.GetBlockPath("path", error);
+ if (path.IsNull()) {
+ if (!error.IsDefined())
+ error.Set(simple_db_domain,
+ "No \"path\" parameter specified");
+ return false;
+ }
+
+ path_utf8 = path.ToUTF8();
+
+ cache_path = param.GetBlockPath("cache_directory", error);
+ if (path.IsNull() && error.IsDefined())
+ return false;
+
+#ifdef HAVE_ZLIB
+ compress = param.GetBlockValue("compress", compress);
+#endif
+
+ return true;
+}
+
+bool
+SimpleDatabase::Check(Error &error) const
+{
+ assert(!path.IsNull());
+
+ /* Check if the file exists */
+ if (!CheckAccess(path)) {
+ /* If the file doesn't exist, we can't check if we can write
+ * it, so we are going to try to get the directory path, and
+ * see if we can write a file in that */
+ const auto dirPath = path.GetDirectoryName();
+
+ /* Check that the parent part of the path is a directory */
+ struct stat st;
+ if (!StatFile(dirPath, st)) {
+ error.FormatErrno("Couldn't stat parent directory of db file "
+ "\"%s\"",
+ path_utf8.c_str());
+ return false;
+ }
+
+ if (!S_ISDIR(st.st_mode)) {
+ error.Format(simple_db_domain,
+ "Couldn't create db file \"%s\" because the "
+ "parent path is not a directory",
+ path_utf8.c_str());
+ return false;
+ }
+
+#ifndef WIN32
+ /* Check if we can write to the directory */
+ if (!CheckAccess(dirPath, X_OK | W_OK)) {
+ const int e = errno;
+ const std::string dirPath_utf8 = dirPath.ToUTF8();
+ error.FormatErrno(e, "Can't create db file in \"%s\"",
+ dirPath_utf8.c_str());
+ return false;
+ }
+#endif
+ return true;
+ }
+
+ /* Path exists, now check if it's a regular file */
+ struct stat st;
+ if (!StatFile(path, st)) {
+ error.FormatErrno("Couldn't stat db file \"%s\"",
+ path_utf8.c_str());
+ return false;
+ }
+
+ if (!S_ISREG(st.st_mode)) {
+ error.Format(simple_db_domain,
+ "db file \"%s\" is not a regular file",
+ path_utf8.c_str());
+ return false;
+ }
+
+#ifndef WIN32
+ /* And check that we can write to it */
+ if (!CheckAccess(path, R_OK | W_OK)) {
+ error.FormatErrno("Can't open db file \"%s\" for reading/writing",
+ path_utf8.c_str());
+ return false;
+ }
+#endif
+
+ return true;
+}
+
+bool
+SimpleDatabase::Load(Error &error)
+{
+ assert(!path.IsNull());
+ assert(root != nullptr);
+
+ TextFile file(path, error);
+ if (file.HasFailed())
+ return false;
+
+ if (!db_load_internal(file, *root, error) || !file.Check(error))
+ return false;
+
+ struct stat st;
+ if (StatFile(path, st))
+ mtime = st.st_mtime;
+
+ return true;
+}
+
+bool
+SimpleDatabase::Open(Error &error)
+{
+ assert(prefixed_light_song == nullptr);
+
+ root = Directory::NewRoot();
+ mtime = 0;
+
+#ifndef NDEBUG
+ borrowed_song_count = 0;
+#endif
+
+ if (!Load(error)) {
+ delete root;
+
+ LogError(error);
+ error.Clear();
+
+ if (!Check(error))
+ return false;
+
+ root = Directory::NewRoot();
+ }
+
+ return true;
+}
+
+void
+SimpleDatabase::Close()
+{
+ assert(root != nullptr);
+ assert(prefixed_light_song == nullptr);
+ assert(borrowed_song_count == 0);
+
+ delete root;
+}
+
+const LightSong *
+SimpleDatabase::GetSong(const char *uri, Error &error) const
+{
+ assert(root != nullptr);
+ assert(prefixed_light_song == nullptr);
+ assert(borrowed_song_count == 0);
+
+ db_lock();
+
+ auto r = root->LookupDirectory(uri);
+
+ if (r.directory->IsMount()) {
+ /* pass the request to the mounted database */
+ db_unlock();
+
+ const LightSong *song =
+ r.directory->mounted_database->GetSong(r.uri, error);
+ if (song == nullptr)
+ return nullptr;
+
+ prefixed_light_song =
+ new PrefixedLightSong(*song, r.directory->GetPath());
+ return prefixed_light_song;
+ }
+
+ if (r.uri == nullptr) {
+ /* it's a directory */
+ db_unlock();
+ error.Format(db_domain, DB_NOT_FOUND,
+ "No such song: %s", uri);
+ return nullptr;
+ }
+
+ if (strchr(r.uri, '/') != nullptr) {
+ /* refers to a URI "below" the actual song */
+ db_unlock();
+ error.Format(db_domain, DB_NOT_FOUND,
+ "No such song: %s", uri);
+ return nullptr;
+ }
+
+ const Song *song = r.directory->FindSong(r.uri);
+ db_unlock();
+ if (song == nullptr) {
+ error.Format(db_domain, DB_NOT_FOUND,
+ "No such song: %s", uri);
+ return nullptr;
+ }
+
+ light_song = song->Export();
+
+#ifndef NDEBUG
+ ++borrowed_song_count;
+#endif
+
+ return &light_song;
+}
+
+void
+SimpleDatabase::ReturnSong(gcc_unused const LightSong *song) const
+{
+ assert(song != nullptr);
+ assert(song == &light_song || song == prefixed_light_song);
+
+ delete prefixed_light_song;
+ prefixed_light_song = nullptr;
+
+#ifndef NDEBUG
+ if (song == &light_song) {
+ assert(borrowed_song_count > 0);
+ --borrowed_song_count;
+ }
+#endif
+}
+
+bool
+SimpleDatabase::Visit(const DatabaseSelection &selection,
+ VisitDirectory visit_directory,
+ VisitSong visit_song,
+ VisitPlaylist visit_playlist,
+ Error &error) const
+{
+ ScopeDatabaseLock protect;
+
+ auto r = root->LookupDirectory(selection.uri.c_str());
+ if (r.uri == nullptr) {
+ /* it's a directory */
+
+ if (selection.recursive && visit_directory &&
+ !visit_directory(r.directory->Export(), error))
+ return false;
+
+ return r.directory->Walk(selection.recursive, selection.filter,
+ visit_directory, visit_song,
+ visit_playlist,
+ error);
+ }
+
+ if (strchr(r.uri, '/') == nullptr) {
+ if (visit_song) {
+ Song *song = r.directory->FindSong(r.uri);
+ if (song != nullptr) {
+ const LightSong song2 = song->Export();
+ return !selection.Match(song2) ||
+ visit_song(song2, error);
+ }
+ }
+ }
+
+ error.Set(db_domain, DB_NOT_FOUND, "No such directory");
+ return false;
+}
+
+bool
+SimpleDatabase::VisitUniqueTags(const DatabaseSelection &selection,
+ TagType tag_type, uint32_t group_mask,
+ VisitTag visit_tag,
+ Error &error) const
+{
+ return ::VisitUniqueTags(*this, selection, tag_type, group_mask,
+ visit_tag,
+ error);
+}
+
+bool
+SimpleDatabase::GetStats(const DatabaseSelection &selection,
+ DatabaseStats &stats, Error &error) const
+{
+ return ::GetStats(*this, selection, stats, error);
+}
+
+bool
+SimpleDatabase::Save(Error &error)
+{
+ db_lock();
+
+ LogDebug(simple_db_domain, "removing empty directories from DB");
+ root->PruneEmpty();
+
+ LogDebug(simple_db_domain, "sorting DB");
+ root->Sort();
+
+ db_unlock();
+
+ LogDebug(simple_db_domain, "writing DB");
+
+ FileOutputStream fos(path, error);
+ if (!fos.IsDefined())
+ return false;
+
+ OutputStream *os = &fos;
+
+#ifdef HAVE_ZLIB
+ GzipOutputStream *gzip = nullptr;
+ if (compress) {
+ gzip = new GzipOutputStream(*os, error);
+ if (!gzip->IsDefined()) {
+ delete gzip;
+ return false;
+ }
+
+ os = gzip;
+ }
+#endif
+
+ BufferedOutputStream bos(*os);
+
+ db_save_internal(bos, *root);
+
+ if (!bos.Flush(error)) {
+#ifdef HAVE_ZLIB
+ delete gzip;
+#endif
+ return false;
+ }
+
+#ifdef HAVE_ZLIB
+ if (gzip != nullptr) {
+ bool success = gzip->Flush(error);
+ delete gzip;
+ if (!success)
+ return false;
+ }
+#endif
+
+ if (!fos.Commit(error))
+ return false;
+
+ struct stat st;
+ if (StatFile(path, st))
+ mtime = st.st_mtime;
+
+ return true;
+}
+
+bool
+SimpleDatabase::Mount(const char *uri, Database *db, Error &error)
+{
+ assert(uri != nullptr);
+ assert(*uri != 0);
+ assert(db != nullptr);
+
+ ScopeDatabaseLock protect;
+
+ auto r = root->LookupDirectory(uri);
+ if (r.uri == nullptr) {
+ error.Format(db_domain, DB_CONFLICT,
+ "Already exists: %s", uri);
+ return nullptr;
+ }
+
+ if (strchr(r.uri, '/') != nullptr) {
+ error.Format(db_domain, DB_NOT_FOUND,
+ "Parent not found: %s", uri);
+ return nullptr;
+ }
+
+ Directory *mnt = r.directory->CreateChild(r.uri);
+ mnt->mounted_database = db;
+ return true;
+}
+
+static constexpr bool
+IsSafeChar(char ch)
+{
+ return IsAlphaNumericASCII(ch) || ch == '-' || ch == '_' || ch == '%';
+}
+
+static constexpr bool
+IsUnsafeChar(char ch)
+{
+ return !IsSafeChar(ch);
+}
+
+bool
+SimpleDatabase::Mount(const char *local_uri, const char *storage_uri,
+ Error &error)
+{
+ if (cache_path.IsNull()) {
+ error.Format(db_domain, DB_NOT_FOUND,
+ "No 'cache_directory' configured");
+ return nullptr;
+ }
+
+ std::string name(storage_uri);
+ std::replace_if(name.begin(), name.end(), IsUnsafeChar, '_');
+
+#ifndef HAVE_ZLIB
+ constexpr bool compress = false;
+#endif
+ auto db = new SimpleDatabase(AllocatedPath::Build(cache_path,
+ name.c_str()),
+ compress);
+ if (!db->Open(error)) {
+ delete db;
+ return false;
+ }
+
+ // TODO: update the new database instance?
+
+ if (!Mount(local_uri, db, error)) {
+ db->Close();
+ delete db;
+ return false;
+ }
+
+ return true;
+}
+
+Database *
+SimpleDatabase::LockUmountSteal(const char *uri)
+{
+ ScopeDatabaseLock protect;
+
+ auto r = root->LookupDirectory(uri);
+ if (r.uri != nullptr || !r.directory->IsMount())
+ return nullptr;
+
+ Database *db = r.directory->mounted_database;
+ r.directory->mounted_database = nullptr;
+ r.directory->Delete();
+
+ return db;
+}
+
+bool
+SimpleDatabase::Unmount(const char *uri)
+{
+ Database *db = LockUmountSteal(uri);
+ if (db == nullptr)
+ return false;
+
+ db->Close();
+ delete db;
+ return true;
+}
+
+const DatabasePlugin simple_db_plugin = {
+ "simple",
+ DatabasePlugin::FLAG_REQUIRE_STORAGE,
+ SimpleDatabase::Create,
+};
diff --git a/src/db/SimpleDatabasePlugin.hxx b/src/db/plugins/simple/SimpleDatabasePlugin.hxx
index dfe981dd8..7ba71e272 100644
--- a/src/db/SimpleDatabasePlugin.hxx
+++ b/src/db/plugins/simple/SimpleDatabasePlugin.hxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * 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
@@ -20,48 +20,99 @@
#ifndef MPD_SIMPLE_DATABASE_PLUGIN_HXX
#define MPD_SIMPLE_DATABASE_PLUGIN_HXX
-#include "DatabasePlugin.hxx"
+#include "check.h"
+#include "db/Interface.hxx"
#include "fs/AllocatedPath.hxx"
+#include "db/LightSong.hxx"
#include "Compiler.h"
#include <cassert>
+struct config_param;
struct Directory;
+struct DatabasePlugin;
+class EventLoop;
+class DatabaseListener;
+class PrefixedLightSong;
class SimpleDatabase : public Database {
AllocatedPath path;
std::string path_utf8;
+#ifdef HAVE_ZLIB
+ bool compress;
+#endif
+
+ /**
+ * The path where cache files for Mount() are located.
+ */
+ AllocatedPath cache_path;
+
Directory *root;
time_t mtime;
+ /**
+ * A buffer for GetSong() when prefixing the #LightSong
+ * instance from a mounted #Database.
+ */
+ mutable PrefixedLightSong *prefixed_light_song;
+
+ /**
+ * A buffer for GetSong().
+ */
+ mutable LightSong light_song;
+
#ifndef NDEBUG
- unsigned borrowed_song_count;
+ mutable unsigned borrowed_song_count;
#endif
- SimpleDatabase()
- :path(AllocatedPath::Null()) {}
+ SimpleDatabase();
+
+ SimpleDatabase(AllocatedPath &&_path, bool _compress);
public:
+ static Database *Create(EventLoop &loop, DatabaseListener &listener,
+ const config_param &param,
+ Error &error);
+
gcc_pure
- Directory *GetRoot() {
+ Directory &GetRoot() {
assert(root != NULL);
- return root;
+ return *root;
}
bool Save(Error &error);
- static Database *Create(const config_param &param,
- Error &error);
+ /**
+ * Returns true if there is a valid database file on the disk.
+ */
+ bool FileExists() const {
+ return mtime > 0;
+ }
+
+ /**
+ * @param db the #Database to be mounted; must be "open"; on
+ * success, this object gains ownership of the given #Database
+ */
+ gcc_nonnull_all
+ bool Mount(const char *uri, Database *db, Error &error);
+
+ gcc_nonnull_all
+ bool Mount(const char *local_uri, const char *storage_uri,
+ Error &error);
+ gcc_nonnull_all
+ bool Unmount(const char *uri);
+
+ /* virtual methods from class Database */
virtual bool Open(Error &error) override;
virtual void Close() override;
- virtual Song *GetSong(const char *uri_utf8,
- Error &error) const override;
- virtual void ReturnSong(Song *song) const;
+ virtual const LightSong *GetSong(const char *uri_utf8,
+ Error &error) const override;
+ virtual void ReturnSong(const LightSong *song) const;
virtual bool Visit(const DatabaseSelection &selection,
VisitDirectory visit_directory,
@@ -70,8 +121,8 @@ public:
Error &error) const override;
virtual bool VisitUniqueTags(const DatabaseSelection &selection,
- TagType tag_type,
- VisitString visit_string,
+ TagType tag_type, uint32_t group_mask,
+ VisitTag visit_tag,
Error &error) const override;
virtual bool GetStats(const DatabaseSelection &selection,
@@ -82,7 +133,7 @@ public:
return mtime;
}
-protected:
+private:
bool Configure(const config_param &param, Error &error);
gcc_pure
@@ -90,8 +141,7 @@ protected:
bool Load(Error &error);
- gcc_pure
- const Directory *LookupDirectory(const char *uri) const;
+ Database *LockUmountSteal(const char *uri);
};
extern const DatabasePlugin simple_db_plugin;
diff --git a/src/db/plugins/simple/Song.cxx b/src/db/plugins/simple/Song.cxx
new file mode 100644
index 000000000..fbfc2ec19
--- /dev/null
+++ b/src/db/plugins/simple/Song.cxx
@@ -0,0 +1,112 @@
+/*
+ * 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 "Song.hxx"
+#include "Directory.hxx"
+#include "tag/Tag.hxx"
+#include "util/VarSize.hxx"
+#include "DetachedSong.hxx"
+#include "db/LightSong.hxx"
+
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+
+inline Song::Song(const char *_uri, size_t uri_length, Directory &_parent)
+ :parent(&_parent), mtime(0),
+ start_time(SongTime::zero()), end_time(SongTime::zero())
+{
+ memcpy(uri, _uri, uri_length + 1);
+}
+
+inline Song::~Song()
+{
+}
+
+static Song *
+song_alloc(const char *uri, Directory &parent)
+{
+ size_t uri_length;
+
+ assert(uri);
+ uri_length = strlen(uri);
+ assert(uri_length);
+
+ return NewVarSize<Song>(sizeof(Song::uri),
+ uri_length + 1,
+ uri, uri_length, parent);
+}
+
+Song *
+Song::NewFrom(DetachedSong &&other, Directory &parent)
+{
+ Song *song = song_alloc(other.GetURI(), parent);
+ song->tag = std::move(other.WritableTag());
+ song->mtime = other.GetLastModified();
+ song->start_time = other.GetStartTime();
+ song->end_time = other.GetEndTime();
+ return song;
+}
+
+Song *
+Song::NewFile(const char *path, Directory &parent)
+{
+ return song_alloc(path, parent);
+}
+
+void
+Song::Free()
+{
+ DeleteVarSize(this);
+}
+
+std::string
+Song::GetURI() const
+{
+ assert(*uri);
+
+ if (parent->IsRoot())
+ return std::string(uri);
+ else {
+ const char *path = parent->GetPath();
+
+ std::string result;
+ result.reserve(strlen(path) + 1 + strlen(uri));
+ result.assign(path);
+ result.push_back('/');
+ result.append(uri);
+ return result;
+ }
+}
+
+LightSong
+Song::Export() const
+{
+ LightSong dest;
+ dest.directory = parent->IsRoot()
+ ? nullptr : parent->GetPath();
+ dest.uri = uri;
+ dest.real_uri = nullptr;
+ dest.tag = &tag;
+ dest.mtime = mtime;
+ dest.start_time = start_time;
+ dest.end_time = end_time;
+ return dest;
+}
diff --git a/src/db/plugins/simple/Song.hxx b/src/db/plugins/simple/Song.hxx
new file mode 100644
index 000000000..9f3a4a3ef
--- /dev/null
+++ b/src/db/plugins/simple/Song.hxx
@@ -0,0 +1,130 @@
+/*
+ * 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_SONG_HXX
+#define MPD_SONG_HXX
+
+#include "Chrono.hxx"
+#include "tag/Tag.hxx"
+#include "Compiler.h"
+
+#include <boost/intrusive/list.hpp>
+
+#include <string>
+
+#include <assert.h>
+#include <time.h>
+
+struct LightSong;
+struct Directory;
+class DetachedSong;
+class Storage;
+
+/**
+ * A song file inside the configured music directory. Internal
+ * #SimpleDatabase class.
+ */
+struct Song {
+ static constexpr auto link_mode = boost::intrusive::normal_link;
+ typedef boost::intrusive::link_mode<link_mode> LinkMode;
+ typedef boost::intrusive::list_member_hook<LinkMode> Hook;
+
+ struct Disposer {
+ void operator()(Song *song) const {
+ song->Free();
+ }
+ };
+
+ /**
+ * Pointers to the siblings of this directory within the
+ * parent directory. It is unused (undefined) if this song is
+ * not in the database.
+ *
+ * This attribute is protected with the global #db_mutex.
+ * Read access in the update thread does not need protection.
+ */
+ Hook siblings;
+
+ Tag tag;
+
+ /**
+ * The #Directory that contains this song. Must be
+ * non-nullptr. directory this way.
+ */
+ Directory *const parent;
+
+ time_t mtime;
+
+ /**
+ * Start of this sub-song within the file.
+ */
+ SongTime start_time;
+
+ /**
+ * End of this sub-song within the file.
+ * Unused if zero.
+ */
+ SongTime end_time;
+
+ /**
+ * The file name.
+ */
+ char uri[sizeof(int)];
+
+ Song(const char *_uri, size_t uri_length, Directory &parent);
+ ~Song();
+
+ gcc_malloc
+ static Song *NewFrom(DetachedSong &&other, Directory &parent);
+
+ /** allocate a new song with a local file name */
+ gcc_malloc
+ static Song *NewFile(const char *path_utf8, Directory &parent);
+
+ /**
+ * allocate a new song structure with a local file name and attempt to
+ * load its metadata. If all decoder plugin fail to read its meta
+ * data, nullptr is returned.
+ */
+ gcc_malloc
+ static Song *LoadFile(Storage &storage, const char *name_utf8,
+ Directory &parent);
+
+ void Free();
+
+ bool UpdateFile(Storage &storage);
+ bool UpdateFileInArchive(const Storage &storage);
+
+ /**
+ * Returns the URI of the song in UTF-8 encoding, including its
+ * location within the music directory.
+ */
+ gcc_pure
+ std::string GetURI() const;
+
+ gcc_pure
+ LightSong Export() const;
+};
+
+typedef boost::intrusive::list<Song,
+ boost::intrusive::member_hook<Song, Song::Hook,
+ &Song::siblings>,
+ boost::intrusive::constant_time_size<false>> SongList;
+
+#endif
diff --git a/src/db/plugins/simple/SongSort.cxx b/src/db/plugins/simple/SongSort.cxx
new file mode 100644
index 000000000..4b7144937
--- /dev/null
+++ b/src/db/plugins/simple/SongSort.cxx
@@ -0,0 +1,108 @@
+/*
+ * 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 "SongSort.hxx"
+#include "Song.hxx"
+#include "tag/Tag.hxx"
+#include "lib/icu/Collate.hxx"
+
+#include <stdlib.h>
+
+static int
+compare_utf8_string(const char *a, const char *b)
+{
+ if (a == nullptr)
+ return b == nullptr ? 0 : -1;
+
+ if (b == nullptr)
+ return 1;
+
+ return IcuCollate(a, b);
+}
+
+/**
+ * Compare two string tag values, ignoring case. Either one may be
+ * nullptr.
+ */
+static int
+compare_string_tag_item(const Tag &a, const Tag &b,
+ TagType type)
+{
+ return compare_utf8_string(a.GetValue(type),
+ b.GetValue(type));
+}
+
+/**
+ * Compare two tag values which should contain an integer value
+ * (e.g. disc or track number). Either one may be nullptr.
+ */
+static int
+compare_number_string(const char *a, const char *b)
+{
+ long ai = a == nullptr ? 0 : strtol(a, nullptr, 10);
+ long bi = b == nullptr ? 0 : strtol(b, nullptr, 10);
+
+ if (ai <= 0)
+ return bi <= 0 ? 0 : -1;
+
+ if (bi <= 0)
+ return 1;
+
+ return ai - bi;
+}
+
+static int
+compare_tag_item(const Tag &a, const Tag &b, TagType type)
+{
+ return compare_number_string(a.GetValue(type),
+ b.GetValue(type));
+}
+
+/* Only used for sorting/searchin a songvec, not general purpose compares */
+gcc_pure
+static bool
+song_cmp(const Song &a, const Song &b)
+{
+ int ret;
+
+ /* first sort by album */
+ ret = compare_string_tag_item(a.tag, b.tag, TAG_ALBUM);
+ if (ret != 0)
+ return ret < 0;
+
+ /* then sort by disc */
+ ret = compare_tag_item(a.tag, b.tag, TAG_DISC);
+ if (ret != 0)
+ return ret < 0;
+
+ /* then by track number */
+ ret = compare_tag_item(a.tag, b.tag, TAG_TRACK);
+ if (ret != 0)
+ return ret < 0;
+
+ /* still no difference? compare file name */
+ return IcuCollate(a.uri, b.uri) < 0;
+}
+
+void
+song_list_sort(SongList &songs)
+{
+ songs.sort(song_cmp);
+}
diff --git a/src/db/plugins/simple/SongSort.hxx b/src/db/plugins/simple/SongSort.hxx
new file mode 100644
index 000000000..2a0c4383b
--- /dev/null
+++ b/src/db/plugins/simple/SongSort.hxx
@@ -0,0 +1,30 @@
+/*
+ * 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_SONG_SORT_HXX
+#define MPD_SONG_SORT_HXX
+
+#include "Song.hxx"
+
+struct list_head;
+
+void
+song_list_sort(SongList &songs);
+
+#endif
diff --git a/src/db/plugins/upnp/ContentDirectoryService.cxx b/src/db/plugins/upnp/ContentDirectoryService.cxx
new file mode 100644
index 000000000..c097f7644
--- /dev/null
+++ b/src/db/plugins/upnp/ContentDirectoryService.cxx
@@ -0,0 +1,204 @@
+/*
+ * 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 "lib/upnp/ContentDirectoryService.hxx"
+#include "lib/upnp/Domain.hxx"
+#include "lib/upnp/ixmlwrap.hxx"
+#include "lib/upnp/Action.hxx"
+#include "Directory.hxx"
+#include "util/NumberParser.hxx"
+#include "util/Error.hxx"
+
+#include <stdio.h>
+
+static bool
+ReadResultTag(UPnPDirContent &dirbuf, IXML_Document *response, Error &error)
+{
+ const char *p = ixmlwrap::getFirstElementValue(response, "Result");
+ if (p == nullptr)
+ p = "";
+
+ return dirbuf.parse(p, error);
+}
+
+inline bool
+ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl,
+ const char *objectId, unsigned offset,
+ unsigned count, UPnPDirContent &dirbuf,
+ unsigned &didreadp, unsigned &totalp,
+ Error &error) const
+{
+ // Create request
+ char ofbuf[100], cntbuf[100];
+ sprintf(ofbuf, "%u", offset);
+ sprintf(cntbuf, "%u", count);
+ // Some devices require an empty SortCriteria, else bad params
+ IXML_Document *request =
+ MakeActionHelper("Browse", m_serviceType.c_str(),
+ "ObjectID", objectId,
+ "BrowseFlag", "BrowseDirectChildren",
+ "Filter", "*",
+ "SortCriteria", "",
+ "StartingIndex", ofbuf,
+ "RequestedCount", cntbuf);
+ if (request == nullptr) {
+ error.Set(upnp_domain, "UpnpMakeAction() failed");
+ return false;
+ }
+
+ IXML_Document *response;
+ int code = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
+ 0 /*devUDN*/, request, &response);
+ ixmlDocument_free(request);
+ if (code != UPNP_E_SUCCESS) {
+ error.Format(upnp_domain, code,
+ "UpnpSendAction() failed: %s",
+ UpnpGetErrorMessage(code));
+ return false;
+ }
+
+ const char *value = ixmlwrap::getFirstElementValue(response, "NumberReturned");
+ didreadp = value != nullptr
+ ? ParseUnsigned(value)
+ : 0;
+
+ value = ixmlwrap::getFirstElementValue(response, "TotalMatches");
+ if (value != nullptr)
+ totalp = ParseUnsigned(value);
+
+ bool success = ReadResultTag(dirbuf, response, error);
+ ixmlDocument_free(response);
+ return success;
+}
+
+bool
+ContentDirectoryService::readDir(UpnpClient_Handle handle,
+ const char *objectId,
+ UPnPDirContent &dirbuf,
+ Error &error) const
+{
+ unsigned offset = 0, total = -1, count;
+
+ do {
+ if (!readDirSlice(handle, objectId, offset, m_rdreqcnt, dirbuf,
+ count, total, error))
+ return false;
+
+ offset += count;
+ } while (count > 0 && offset < total);
+
+ return true;
+}
+
+bool
+ContentDirectoryService::search(UpnpClient_Handle hdl,
+ const char *objectId,
+ const char *ss,
+ UPnPDirContent &dirbuf,
+ Error &error) const
+{
+ unsigned offset = 0, total = -1, count;
+
+ do {
+ char ofbuf[100];
+ sprintf(ofbuf, "%d", offset);
+
+ IXML_Document *request =
+ MakeActionHelper("Search", m_serviceType.c_str(),
+ "ContainerID", objectId,
+ "SearchCriteria", ss,
+ "Filter", "*",
+ "SortCriteria", "",
+ "StartingIndex", ofbuf,
+ "RequestedCount", "0"); // Setting a value here gets twonky into fits
+ if (request == 0) {
+ error.Set(upnp_domain, "UpnpMakeAction() failed");
+ return false;
+ }
+
+ IXML_Document *response;
+ auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
+ m_serviceType.c_str(),
+ 0 /*devUDN*/, request, &response);
+ ixmlDocument_free(request);
+ if (code != UPNP_E_SUCCESS) {
+ error.Format(upnp_domain, code,
+ "UpnpSendAction() failed: %s",
+ UpnpGetErrorMessage(code));
+ return false;
+ }
+
+ const char *value =
+ ixmlwrap::getFirstElementValue(response, "NumberReturned");
+ count = value != nullptr
+ ? ParseUnsigned(value)
+ : 0;
+
+ offset += count;
+
+ value = ixmlwrap::getFirstElementValue(response, "TotalMatches");
+ if (value != nullptr)
+ total = ParseUnsigned(value);
+
+ bool success = ReadResultTag(dirbuf, response, error);
+ ixmlDocument_free(response);
+ if (!success)
+ return false;
+ } while (count > 0 && offset < total);
+
+ return true;
+}
+
+bool
+ContentDirectoryService::getMetadata(UpnpClient_Handle hdl,
+ const char *objectId,
+ UPnPDirContent &dirbuf,
+ Error &error) const
+{
+ // Create request
+ IXML_Document *request =
+ MakeActionHelper("Browse", m_serviceType.c_str(),
+ "ObjectID", objectId,
+ "BrowseFlag", "BrowseMetadata",
+ "Filter", "*",
+ "SortCriteria", "",
+ "StartingIndex", "0",
+ "RequestedCount", "1");
+ if (request == nullptr) {
+ error.Set(upnp_domain, "UpnpMakeAction() failed");
+ return false;
+ }
+
+ IXML_Document *response;
+ auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
+ m_serviceType.c_str(),
+ 0 /*devUDN*/, request, &response);
+ ixmlDocument_free(request);
+ if (code != UPNP_E_SUCCESS) {
+ error.Format(upnp_domain, code,
+ "UpnpSendAction() failed: %s",
+ UpnpGetErrorMessage(code));
+ return false;
+ }
+
+ bool success = ReadResultTag(dirbuf, response, error);
+ ixmlDocument_free(response);
+ return success;
+}
diff --git a/src/db/plugins/upnp/Directory.cxx b/src/db/plugins/upnp/Directory.cxx
new file mode 100644
index 000000000..683022a10
--- /dev/null
+++ b/src/db/plugins/upnp/Directory.cxx
@@ -0,0 +1,262 @@
+/*
+ * 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 "Directory.hxx"
+#include "lib/upnp/Util.hxx"
+#include "lib/expat/ExpatParser.hxx"
+#include "Tags.hxx"
+#include "tag/TagBuilder.hxx"
+#include "tag/TagTable.hxx"
+#include "util/NumberParser.hxx"
+
+#include <algorithm>
+#include <string>
+
+#include <string.h>
+
+UPnPDirContent::~UPnPDirContent()
+{
+ /* this destructor exists here just so it won't get inlined */
+}
+
+gcc_pure gcc_nonnull_all
+static bool
+CompareStringLiteral(const char *literal, const char *value, size_t length)
+{
+ return length == strlen(literal) &&
+ memcmp(literal, value, length) == 0;
+}
+
+gcc_pure
+static UPnPDirObject::ItemClass
+ParseItemClass(const char *name, size_t length)
+{
+ if (CompareStringLiteral("object.item.audioItem.musicTrack",
+ name, length))
+ return UPnPDirObject::ItemClass::MUSIC;
+ else if (CompareStringLiteral("object.item.playlistItem",
+ name, length))
+ return UPnPDirObject::ItemClass::PLAYLIST;
+ else
+ return UPnPDirObject::ItemClass::UNKNOWN;
+}
+
+gcc_pure
+static SignedSongTime
+ParseDuration(const char *duration)
+{
+ char *endptr;
+
+ unsigned result = ParseUnsigned(duration, &endptr);
+ if (endptr == duration || *endptr != ':')
+ return SignedSongTime::Negative();
+
+ result *= 60;
+ duration = endptr + 1;
+ result += ParseUnsigned(duration, &endptr);
+ if (endptr == duration || *endptr != ':')
+ return SignedSongTime::Negative();
+
+ result *= 60;
+ duration = endptr + 1;
+ result += ParseUnsigned(duration, &endptr);
+ if (endptr == duration || *endptr != 0)
+ return SignedSongTime::Negative();
+
+ return SignedSongTime::FromS(result);
+}
+
+/**
+ * Transform titles to turn '/' into '_' to make them acceptable path
+ * elements. There is a very slight risk of collision in doing
+ * this. Twonky returns directory names (titles) like 'Artist/Album'.
+ */
+gcc_pure
+static std::string
+titleToPathElt(std::string &&s)
+{
+ std::replace(s.begin(), s.end(), '/', '_');
+ return s;
+}
+
+/**
+ * An XML parser which builds directory contents from DIDL lite input.
+ */
+class UPnPDirParser final : public CommonExpatParser {
+ UPnPDirContent &m_dir;
+
+ enum {
+ NONE,
+ RES,
+ CLASS,
+ } state;
+
+ /**
+ * If not equal to #TAG_NUM_OF_ITEM_TYPES, then we're
+ * currently reading an element containing a tag value. The
+ * value is being constructed in #value.
+ */
+ TagType tag_type;
+
+ /**
+ * The text inside the current element.
+ */
+ std::string value;
+
+ UPnPDirObject m_tobj;
+ TagBuilder tag;
+
+public:
+ UPnPDirParser(UPnPDirContent& dir)
+ :m_dir(dir),
+ state(NONE),
+ tag_type(TAG_NUM_OF_ITEM_TYPES)
+ {
+ }
+
+protected:
+ virtual void StartElement(const XML_Char *name, const XML_Char **attrs)
+ {
+ if (m_tobj.type != UPnPDirObject::Type::UNKNOWN &&
+ tag_type == TAG_NUM_OF_ITEM_TYPES) {
+ tag_type = tag_table_lookup(upnp_tags, name);
+ if (tag_type != TAG_NUM_OF_ITEM_TYPES)
+ return;
+ } else {
+ assert(tag_type == TAG_NUM_OF_ITEM_TYPES);
+ }
+
+ switch (name[0]) {
+ case 'c':
+ if (!strcmp(name, "container")) {
+ m_tobj.clear();
+ m_tobj.type = UPnPDirObject::Type::CONTAINER;
+
+ const char *id = GetAttribute(attrs, "id");
+ if (id != nullptr)
+ m_tobj.m_id = id;
+
+ const char *pid = GetAttribute(attrs, "parentID");
+ if (pid != nullptr)
+ m_tobj.m_pid = pid;
+ }
+ break;
+
+ case 'i':
+ if (!strcmp(name, "item")) {
+ m_tobj.clear();
+ m_tobj.type = UPnPDirObject::Type::ITEM;
+
+ const char *id = GetAttribute(attrs, "id");
+ if (id != nullptr)
+ m_tobj.m_id = id;
+
+ const char *pid = GetAttribute(attrs, "parentID");
+ if (pid != nullptr)
+ m_tobj.m_pid = pid;
+ }
+ break;
+
+ case 'r':
+ if (!strcmp(name, "res")) {
+ // <res protocolInfo="http-get:*:audio/mpeg:*" size="5171496"
+ // bitrate="24576" duration="00:03:35" sampleFrequency="44100"
+ // nrAudioChannels="2">
+
+ const char *duration =
+ GetAttribute(attrs, "duration");
+ if (duration != nullptr)
+ tag.SetDuration(ParseDuration(duration));
+
+ state = RES;
+ }
+
+ break;
+
+ case 'u':
+ if (strcmp(name, "upnp:class") == 0)
+ state = CLASS;
+ }
+ }
+
+ bool checkobjok() {
+ if (m_tobj.m_id.empty() || m_tobj.m_pid.empty() ||
+ m_tobj.name.empty() ||
+ (m_tobj.type == UPnPDirObject::Type::ITEM &&
+ m_tobj.item_class == UPnPDirObject::ItemClass::UNKNOWN))
+ return false;
+
+ return true;
+ }
+
+ virtual void EndElement(const XML_Char *name)
+ {
+ if (tag_type != TAG_NUM_OF_ITEM_TYPES) {
+ assert(m_tobj.type != UPnPDirObject::Type::UNKNOWN);
+
+ tag.AddItem(tag_type, value.c_str());
+
+ if (tag_type == TAG_TITLE)
+ m_tobj.name = titleToPathElt(std::move(value));
+
+ value.clear();
+ tag_type = TAG_NUM_OF_ITEM_TYPES;
+ return;
+ }
+
+ if ((!strcmp(name, "container") || !strcmp(name, "item")) &&
+ checkobjok()) {
+ tag.Commit(m_tobj.tag);
+ m_dir.objects.emplace_back(std::move(m_tobj));
+ }
+
+ state = NONE;
+ }
+
+ virtual void CharacterData(const XML_Char *s, int len)
+ {
+ if (tag_type != TAG_NUM_OF_ITEM_TYPES) {
+ assert(m_tobj.type != UPnPDirObject::Type::UNKNOWN);
+
+ value.append(s, len);
+ return;
+ }
+
+ switch (state) {
+ case NONE:
+ break;
+
+ case RES:
+ m_tobj.url.assign(s, len);
+ break;
+
+ case CLASS:
+ m_tobj.item_class = ParseItemClass(s, len);
+ break;
+ }
+ }
+};
+
+bool
+UPnPDirContent::parse(const char *input, Error &error)
+{
+ UPnPDirParser parser(*this);
+ return parser.Parse(input, strlen(input), true, error);
+}
diff --git a/src/db/plugins/upnp/Directory.hxx b/src/db/plugins/upnp/Directory.hxx
new file mode 100644
index 000000000..433979900
--- /dev/null
+++ b/src/db/plugins/upnp/Directory.hxx
@@ -0,0 +1,66 @@
+/*
+ * 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_UPNP_DIRECTORY_HXX
+#define MPD_UPNP_DIRECTORY_HXX
+
+#include "Object.hxx"
+#include "Compiler.h"
+
+#include <string>
+#include <vector>
+
+class Error;
+
+/**
+ * Image of a MediaServer Directory Service container (directory),
+ * possibly containing items and subordinate containers.
+ */
+class UPnPDirContent {
+public:
+ std::vector<UPnPDirObject> objects;
+
+ ~UPnPDirContent();
+
+ gcc_pure
+ UPnPDirObject *FindObject(const char *name) {
+ for (auto &o : objects)
+ if (o.name == name)
+ return &o;
+
+ return nullptr;
+ }
+
+ /**
+ * Parse from DIDL-Lite XML data.
+ *
+ * Normally only used by ContentDirectoryService::readDir()
+ * This is cumulative: in general, the XML data is obtained in
+ * several documents corresponding to (offset,count) slices of the
+ * directory (container). parse() can be called repeatedly with
+ * the successive XML documents and will accumulate entries in the item
+ * and container vectors. This makes more sense if the different
+ * chunks are from the same container, but given that UPnP Ids are
+ * actually global, nothing really bad will happen if you mix
+ * up...
+ */
+ bool parse(const char *didltext, Error &error);
+};
+
+#endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */
diff --git a/src/db/plugins/upnp/Object.cxx b/src/db/plugins/upnp/Object.cxx
new file mode 100644
index 000000000..703fb0be4
--- /dev/null
+++ b/src/db/plugins/upnp/Object.cxx
@@ -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.
+ */
+
+#include "Object.hxx"
+
+UPnPDirObject::~UPnPDirObject()
+{
+ /* this destructor exists here just so it won't get inlined */
+}
diff --git a/src/db/plugins/upnp/Object.hxx b/src/db/plugins/upnp/Object.hxx
new file mode 100644
index 000000000..16a66c774
--- /dev/null
+++ b/src/db/plugins/upnp/Object.hxx
@@ -0,0 +1,85 @@
+/*
+ * 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_UPNP_OBJECT_HXX
+#define MPD_UPNP_OBJECT_HXX
+
+#include "tag/Tag.hxx"
+
+#include <string>
+
+/**
+ * UpnP Media Server directory entry, converted from XML data.
+ *
+ * This is a dumb data holder class, a struct with helpers.
+ */
+class UPnPDirObject {
+public:
+ enum class Type {
+ UNKNOWN,
+ ITEM,
+ CONTAINER,
+ };
+
+ // There are actually several kinds of containers:
+ // object.container.storageFolder, object.container.person,
+ // object.container.playlistContainer etc., but they all seem to
+ // behave the same as far as we're concerned. Otoh, musicTrack
+ // items are special to us, and so should playlists, but I've not
+ // seen one of the latter yet (servers seem to use containers for
+ // playlists).
+ enum class ItemClass {
+ UNKNOWN,
+ MUSIC,
+ PLAYLIST,
+ };
+
+ std::string m_id; // ObjectId
+ std::string m_pid; // Parent ObjectId
+ std::string url;
+
+ /**
+ * A copy of "dc:title" sanitized as a file name.
+ */
+ std::string name;
+
+ Type type;
+ ItemClass item_class;
+
+ Tag tag;
+
+ UPnPDirObject() = default;
+ UPnPDirObject(UPnPDirObject &&) = default;
+
+ ~UPnPDirObject();
+
+ UPnPDirObject &operator=(UPnPDirObject &&) = default;
+
+ void clear()
+ {
+ m_id.clear();
+ m_pid.clear();
+ url.clear();
+ type = Type::UNKNOWN;
+ item_class = ItemClass::UNKNOWN;
+ tag.Clear();
+ }
+};
+
+#endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */
diff --git a/src/db/plugins/upnp/Tags.cxx b/src/db/plugins/upnp/Tags.cxx
new file mode 100644
index 000000000..fd65df4d0
--- /dev/null
+++ b/src/db/plugins/upnp/Tags.cxx
@@ -0,0 +1,33 @@
+/*
+ * 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 "Tags.hxx"
+#include "tag/TagTable.hxx"
+
+const struct tag_table upnp_tags[] = {
+ { "upnp:artist", TAG_ARTIST },
+ { "upnp:album", TAG_ALBUM },
+ { "upnp:originalTrackNumber", TAG_TRACK },
+ { "upnp:genre", TAG_GENRE },
+ { "dc:title", TAG_TITLE },
+
+ /* sentinel */
+ { nullptr, TAG_NUM_OF_ITEM_TYPES }
+};
diff --git a/src/db/plugins/upnp/Tags.hxx b/src/db/plugins/upnp/Tags.hxx
new file mode 100644
index 000000000..ec6d18478
--- /dev/null
+++ b/src/db/plugins/upnp/Tags.hxx
@@ -0,0 +1,28 @@
+/*
+ * 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_UPNP_TAGS_HXX
+#define MPD_UPNP_TAGS_HXX
+
+/**
+ * Map UPnP property names to MPD tags.
+ */
+extern const struct tag_table upnp_tags[];
+
+#endif
diff --git a/src/db/plugins/upnp/UpnpDatabasePlugin.cxx b/src/db/plugins/upnp/UpnpDatabasePlugin.cxx
new file mode 100644
index 000000000..21ddb8790
--- /dev/null
+++ b/src/db/plugins/upnp/UpnpDatabasePlugin.cxx
@@ -0,0 +1,783 @@
+/*
+ * 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 "UpnpDatabasePlugin.hxx"
+#include "Directory.hxx"
+#include "Tags.hxx"
+#include "lib/upnp/Domain.hxx"
+#include "lib/upnp/ClientInit.hxx"
+#include "lib/upnp/Discovery.hxx"
+#include "lib/upnp/ContentDirectoryService.hxx"
+#include "lib/upnp/Util.hxx"
+#include "db/Interface.hxx"
+#include "db/DatabasePlugin.hxx"
+#include "db/Selection.hxx"
+#include "db/DatabaseError.hxx"
+#include "db/LightDirectory.hxx"
+#include "db/LightSong.hxx"
+#include "db/Stats.hxx"
+#include "config/ConfigData.hxx"
+#include "tag/TagBuilder.hxx"
+#include "tag/TagTable.hxx"
+#include "util/Error.hxx"
+#include "util/Domain.hxx"
+#include "fs/Traits.hxx"
+#include "Log.hxx"
+#include "SongFilter.hxx"
+
+#include <string>
+#include <vector>
+#include <set>
+
+#include <assert.h>
+#include <string.h>
+
+static const char *const rootid = "0";
+
+class UpnpSong : public LightSong {
+ std::string uri2, real_uri2;
+
+ Tag tag2;
+
+public:
+ UpnpSong(UPnPDirObject &&object, std::string &&_uri)
+ :uri2(std::move(_uri)),
+ real_uri2(std::move(object.url)),
+ tag2(std::move(object.tag)) {
+ directory = nullptr;
+ uri = uri2.c_str();
+ real_uri = real_uri2.c_str();
+ tag = &tag2;
+ mtime = 0;
+ start_time = end_time = SongTime::zero();
+ }
+};
+
+class UpnpDatabase : public Database {
+ UpnpClient_Handle handle;
+ UPnPDeviceDirectory *discovery;
+
+public:
+ UpnpDatabase():Database(upnp_db_plugin) {}
+
+ static Database *Create(EventLoop &loop, DatabaseListener &listener,
+ const config_param &param,
+ Error &error);
+
+ virtual bool Open(Error &error) override;
+ virtual void Close() override;
+ virtual const LightSong *GetSong(const char *uri_utf8,
+ Error &error) const override;
+ virtual void ReturnSong(const LightSong *song) const;
+
+ virtual bool Visit(const DatabaseSelection &selection,
+ VisitDirectory visit_directory,
+ VisitSong visit_song,
+ VisitPlaylist visit_playlist,
+ Error &error) const override;
+
+ virtual bool VisitUniqueTags(const DatabaseSelection &selection,
+ TagType tag_type, uint32_t group_mask,
+ VisitTag visit_tag,
+ Error &error) const override;
+
+ virtual bool GetStats(const DatabaseSelection &selection,
+ DatabaseStats &stats,
+ Error &error) const override;
+ virtual time_t GetUpdateStamp() const {return 0;}
+
+protected:
+ bool Configure(const config_param &param, Error &error);
+
+private:
+ bool VisitServer(const ContentDirectoryService &server,
+ const std::list<std::string> &vpath,
+ const DatabaseSelection &selection,
+ VisitDirectory visit_directory,
+ VisitSong visit_song,
+ VisitPlaylist visit_playlist,
+ Error &error) const;
+
+ /**
+ * Run an UPnP search according to MPD parameters, and
+ * visit_song the results.
+ */
+ bool SearchSongs(const ContentDirectoryService &server,
+ const char *objid,
+ const DatabaseSelection &selection,
+ VisitSong visit_song,
+ Error &error) const;
+
+ bool SearchSongs(const ContentDirectoryService &server,
+ const char *objid,
+ const DatabaseSelection &selection,
+ UPnPDirContent& dirbuf,
+ Error &error) const;
+
+ bool Namei(const ContentDirectoryService &server,
+ const std::list<std::string> &vpath,
+ UPnPDirObject &dirent,
+ Error &error) const;
+
+ /**
+ * Take server and objid, return metadata.
+ */
+ bool ReadNode(const ContentDirectoryService &server,
+ const char *objid, UPnPDirObject& dirent,
+ Error &error) const;
+
+ /**
+ * Get the path for an object Id. This works much like pwd,
+ * except easier cause our inodes have a parent id. Not used
+ * any more actually (see comments in SearchSongs).
+ */
+ bool BuildPath(const ContentDirectoryService &server,
+ const UPnPDirObject& dirent, std::string &idpath,
+ Error &error) const;
+};
+
+Database *
+UpnpDatabase::Create(gcc_unused EventLoop &loop,
+ gcc_unused DatabaseListener &listener,
+ const config_param &param, Error &error)
+{
+ UpnpDatabase *db = new UpnpDatabase();
+ if (!db->Configure(param, error)) {
+ delete db;
+ return nullptr;
+ }
+
+ /* libupnp loses its ability to receive multicast messages
+ apparently due to daemonization; using the LazyDatabase
+ wrapper works around this problem */
+ return db;
+}
+
+inline bool
+UpnpDatabase::Configure(const config_param &, Error &)
+{
+ return true;
+}
+
+bool
+UpnpDatabase::Open(Error &error)
+{
+ if (!UpnpClientGlobalInit(handle, error))
+ return false;
+
+ discovery = new UPnPDeviceDirectory(handle);
+ if (!discovery->Start(error)) {
+ delete discovery;
+ UpnpClientGlobalFinish();
+ return false;
+ }
+
+ return true;
+}
+
+void
+UpnpDatabase::Close()
+{
+ delete discovery;
+ UpnpClientGlobalFinish();
+}
+
+void
+UpnpDatabase::ReturnSong(const LightSong *_song) const
+{
+ assert(_song != nullptr);
+
+ UpnpSong *song = (UpnpSong *)const_cast<LightSong *>(_song);
+ delete song;
+}
+
+// Get song info by path. We can receive either the id path, or the titles
+// one
+const LightSong *
+UpnpDatabase::GetSong(const char *uri, Error &error) const
+{
+ auto vpath = stringToTokens(uri, "/", true);
+ if (vpath.size() < 2) {
+ error.Format(db_domain, DB_NOT_FOUND, "No such song: %s", uri);
+ return nullptr;
+ }
+
+ ContentDirectoryService server;
+ if (!discovery->getServer(vpath.front().c_str(), server, error))
+ return nullptr;
+
+ vpath.pop_front();
+
+ UPnPDirObject dirent;
+ if (vpath.front() != rootid) {
+ if (!Namei(server, vpath, dirent, error))
+ return nullptr;
+ } else {
+ if (!ReadNode(server, vpath.back().c_str(), dirent,
+ error))
+ return nullptr;
+ }
+
+ return new UpnpSong(std::move(dirent), uri);
+}
+
+/**
+ * Double-quote a string, adding internal backslash escaping.
+ */
+static void
+dquote(std::string &out, const char *in)
+{
+ out.push_back('"');
+
+ for (; *in != 0; ++in) {
+ switch(*in) {
+ case '\\':
+ case '"':
+ out.push_back('\\');
+ break;
+ }
+
+ out.push_back(*in);
+ }
+
+ out.push_back('"');
+}
+
+// Run an UPnP search, according to MPD parameters. Return results as
+// UPnP items
+bool
+UpnpDatabase::SearchSongs(const ContentDirectoryService &server,
+ const char *objid,
+ const DatabaseSelection &selection,
+ UPnPDirContent &dirbuf,
+ Error &error) const
+{
+ const SongFilter *filter = selection.filter;
+ if (selection.filter == nullptr)
+ return true;
+
+ std::list<std::string> searchcaps;
+ if (!server.getSearchCapabilities(handle, searchcaps, error))
+ return false;
+
+ if (searchcaps.empty())
+ return true;
+
+ std::string cond;
+ for (const auto &item : filter->GetItems()) {
+ switch (auto tag = item.GetTag()) {
+ case LOCATE_TAG_ANY_TYPE:
+ {
+ if (!cond.empty()) {
+ cond += " and ";
+ }
+ cond += '(';
+ bool first(true);
+ for (const auto& cap : searchcaps) {
+ if (first)
+ first = false;
+ else
+ cond += " or ";
+ cond += cap;
+ if (item.GetFoldCase()) {
+ cond += " contains ";
+ } else {
+ cond += " = ";
+ }
+ dquote(cond, item.GetValue().c_str());
+ }
+ cond += ')';
+ }
+ break;
+
+ default:
+ /* Unhandled conditions like
+ LOCATE_TAG_BASE_TYPE or
+ LOCATE_TAG_FILE_TYPE won't have a
+ corresponding upnp prop, so they will be
+ skipped */
+ if (tag == TAG_ALBUM_ARTIST)
+ tag = TAG_ARTIST;
+
+ // TODO: support LOCATE_TAG_ANY_TYPE etc.
+ const char *name = tag_table_lookup(upnp_tags,
+ TagType(tag));
+ if (name == nullptr)
+ continue;
+
+ if (!cond.empty()) {
+ cond += " and ";
+ }
+ cond += name;
+
+ /* FoldCase doubles up as contains/equal
+ switch. UpNP search is supposed to be
+ case-insensitive, but at least some servers
+ have the same convention as mpd (e.g.:
+ minidlna) */
+ if (item.GetFoldCase()) {
+ cond += " contains ";
+ } else {
+ cond += " = ";
+ }
+ dquote(cond, item.GetValue().c_str());
+ }
+ }
+
+ return server.search(handle,
+ objid, cond.c_str(), dirbuf,
+ error);
+}
+
+static bool
+visitSong(const UPnPDirObject &meta, const char *path,
+ const DatabaseSelection &selection,
+ VisitSong visit_song, Error& error)
+{
+ if (!visit_song)
+ return true;
+
+ LightSong song;
+ song.directory = nullptr;
+ song.uri = path;
+ song.real_uri = meta.url.c_str();
+ song.tag = &meta.tag;
+ song.mtime = 0;
+ song.start_time = song.end_time = SongTime::zero();
+
+ return !selection.Match(song) || visit_song(song, error);
+}
+
+/**
+ * Build synthetic path based on object id for search results. The use
+ * of "rootid" is arbitrary, any name that is not likely to be a top
+ * directory name would fit.
+ */
+static std::string
+songPath(const std::string &servername,
+ const std::string &objid)
+{
+ return servername + "/" + rootid + "/" + objid;
+}
+
+bool
+UpnpDatabase::SearchSongs(const ContentDirectoryService &server,
+ const char *objid,
+ const DatabaseSelection &selection,
+ VisitSong visit_song,
+ Error &error) const
+{
+ UPnPDirContent dirbuf;
+ if (!visit_song)
+ return true;
+ if (!SearchSongs(server, objid, selection, dirbuf, error))
+ return false;
+
+ for (auto &dirent : dirbuf.objects) {
+ if (dirent.type != UPnPDirObject::Type::ITEM ||
+ dirent.item_class != UPnPDirObject::ItemClass::MUSIC)
+ continue;
+
+ // We get song ids as the result of the UPnP search. But our
+ // client expects paths (e.g. we get 1$4$3788 from minidlna,
+ // but we need to translate to /Music/All_Music/Satisfaction).
+ // We can do this in two ways:
+ // - Rebuild a normal path using BuildPath() which is a kind of pwd
+ // - Build a bogus path based on the song id.
+ // The first method is nice because the returned paths are pretty, but
+ // it has two big problems:
+ // - The song paths are ambiguous: e.g. minidlna returns all search
+ // results as being from the "All Music" directory, which can
+ // contain several songs with the same title (but different objids)
+ // - The performance of BuildPath() is atrocious on very big
+ // directories, even causing timeouts in clients. And of
+ // course, 'All Music' is very big.
+ // So we return synthetic and ugly paths based on the object id,
+ // which we later have to detect.
+ const std::string path = songPath(server.getFriendlyName(),
+ dirent.m_id);
+ if (!visitSong(std::move(dirent), path.c_str(),
+ selection, visit_song,
+ error))
+ return false;
+ }
+
+ return true;
+}
+
+bool
+UpnpDatabase::ReadNode(const ContentDirectoryService &server,
+ const char *objid, UPnPDirObject &dirent,
+ Error &error) const
+{
+ UPnPDirContent dirbuf;
+ if (!server.getMetadata(handle, objid, dirbuf, error))
+ return false;
+
+ if (dirbuf.objects.size() == 1) {
+ dirent = std::move(dirbuf.objects.front());
+ } else {
+ error.Format(upnp_domain, "Bad resource");
+ return false;
+ }
+
+ return true;
+}
+
+bool
+UpnpDatabase::BuildPath(const ContentDirectoryService &server,
+ const UPnPDirObject& idirent,
+ std::string &path,
+ Error &error) const
+{
+ const char *pid = idirent.m_id.c_str();
+ path.clear();
+ UPnPDirObject dirent;
+ while (strcmp(pid, rootid) != 0) {
+ if (!ReadNode(server, pid, dirent, error))
+ return false;
+ pid = dirent.m_pid.c_str();
+
+ if (path.empty())
+ path = dirent.name;
+ else
+ path = PathTraitsUTF8::Build(dirent.name.c_str(),
+ path.c_str());
+ }
+
+ path = PathTraitsUTF8::Build(server.getFriendlyName(),
+ path.c_str());
+ return true;
+}
+
+// Take server and internal title pathname and return objid and metadata.
+bool
+UpnpDatabase::Namei(const ContentDirectoryService &server,
+ const std::list<std::string> &vpath,
+ UPnPDirObject &odirent,
+ Error &error) const
+{
+ if (vpath.empty()) {
+ // looking for root info
+ if (!ReadNode(server, rootid, odirent, error))
+ return false;
+
+ return true;
+ }
+
+ std::string objid(rootid);
+
+ // Walk the path elements, read each directory and try to find the next one
+ for (auto i = vpath.begin(), last = std::prev(vpath.end());; ++i) {
+ UPnPDirContent dirbuf;
+ if (!server.readDir(handle, objid.c_str(), dirbuf, error))
+ return false;
+
+ // Look for the name in the sub-container list
+ UPnPDirObject *child = dirbuf.FindObject(i->c_str());
+ if (child == nullptr) {
+ error.Format(db_domain, DB_NOT_FOUND,
+ "No such object");
+ return false;
+ }
+
+ if (i == last) {
+ odirent = std::move(*child);
+ return true;
+ }
+
+ if (child->type != UPnPDirObject::Type::CONTAINER) {
+ error.Format(db_domain, DB_NOT_FOUND,
+ "Not a container");
+ return false;
+ }
+
+ objid = std::move(child->m_id);
+ }
+}
+
+static bool
+VisitItem(const UPnPDirObject &object, const char *uri,
+ const DatabaseSelection &selection,
+ VisitSong visit_song, VisitPlaylist visit_playlist,
+ Error &error)
+{
+ assert(object.type == UPnPDirObject::Type::ITEM);
+
+ switch (object.item_class) {
+ case UPnPDirObject::ItemClass::MUSIC:
+ return !visit_song ||
+ visitSong(object, uri,
+ selection, visit_song, error);
+
+ case UPnPDirObject::ItemClass::PLAYLIST:
+ if (visit_playlist) {
+ /* Note: I've yet to see a
+ playlist item (playlists
+ seem to be usually handled
+ as containers, so I'll
+ decide what to do when I
+ see one... */
+ }
+
+ return true;
+
+ case UPnPDirObject::ItemClass::UNKNOWN:
+ return true;
+ }
+
+ assert(false);
+ gcc_unreachable();
+}
+
+static bool
+VisitObject(const UPnPDirObject &object, const char *uri,
+ const DatabaseSelection &selection,
+ VisitDirectory visit_directory,
+ VisitSong visit_song,
+ VisitPlaylist visit_playlist,
+ Error &error)
+{
+ switch (object.type) {
+ case UPnPDirObject::Type::UNKNOWN:
+ assert(false);
+ gcc_unreachable();
+
+ case UPnPDirObject::Type::CONTAINER:
+ return !visit_directory ||
+ visit_directory(LightDirectory(uri, 0), error);
+
+ case UPnPDirObject::Type::ITEM:
+ return VisitItem(object, uri, selection,
+ visit_song, visit_playlist,
+ error);
+ }
+
+ assert(false);
+ gcc_unreachable();
+}
+
+// vpath is a parsed and writeable version of selection.uri. There is
+// really just one path parameter.
+bool
+UpnpDatabase::VisitServer(const ContentDirectoryService &server,
+ const std::list<std::string> &vpath,
+ const DatabaseSelection &selection,
+ VisitDirectory visit_directory,
+ VisitSong visit_song,
+ VisitPlaylist visit_playlist,
+ Error &error) const
+{
+ /* If the path begins with rootid, we know that this is a
+ song, not a directory (because that's how we set things
+ up). Just visit it. Note that the choice of rootid is
+ arbitrary, any value not likely to be the name of a top
+ directory would be ok. */
+ /* !Note: this *can't* be handled by Namei further down,
+ because the path is not valid for traversal. Besides, it's
+ just faster to access the target node directly */
+ if (!vpath.empty() && vpath.front() == rootid) {
+ switch (vpath.size()) {
+ case 1:
+ return true;
+
+ case 2:
+ break;
+
+ default:
+ error.Format(db_domain, DB_NOT_FOUND,
+ "Not found");
+ return false;
+ }
+
+ if (visit_song) {
+ UPnPDirObject dirent;
+ if (!ReadNode(server, vpath.back().c_str(), dirent,
+ error))
+ return false;
+
+ if (dirent.type != UPnPDirObject::Type::ITEM ||
+ dirent.item_class != UPnPDirObject::ItemClass::MUSIC) {
+ error.Format(db_domain, DB_NOT_FOUND,
+ "Not found");
+ return false;
+ }
+
+ std::string path = songPath(server.getFriendlyName(),
+ dirent.m_id);
+ if (!visitSong(std::move(dirent), path.c_str(),
+ selection,
+ visit_song, error))
+ return false;
+ }
+ return true;
+ }
+
+ // Translate the target path into an object id and the associated metadata.
+ UPnPDirObject tdirent;
+ if (!Namei(server, vpath, tdirent, error))
+ return false;
+
+ /* If recursive is set, this is a search... No use sending it
+ if the filter is empty. In this case, we implement limited
+ recursion (1-deep) here, which will handle the "add dir"
+ case. */
+ if (selection.recursive && selection.filter)
+ return SearchSongs(server, tdirent.m_id.c_str(), selection,
+ visit_song, error);
+
+ const char *const base_uri = selection.uri.empty()
+ ? server.getFriendlyName()
+ : selection.uri.c_str();
+
+ if (tdirent.type == UPnPDirObject::Type::ITEM) {
+ return VisitItem(tdirent, base_uri,
+ selection,
+ visit_song, visit_playlist,
+ error);
+ }
+
+ /* Target was a a container. Visit it. We could read slices
+ and loop here, but it's not useful as mpd will only return
+ data to the client when we're done anyway. */
+ UPnPDirContent dirbuf;
+ if (!server.readDir(handle, tdirent.m_id.c_str(), dirbuf,
+ error))
+ return false;
+
+ for (auto &dirent : dirbuf.objects) {
+ const std::string uri = PathTraitsUTF8::Build(base_uri,
+ dirent.name.c_str());
+ if (!VisitObject(dirent, uri.c_str(),
+ selection,
+ visit_directory,
+ visit_song, visit_playlist,
+ error))
+ return false;
+ }
+
+ return true;
+}
+
+// Deal with the possibly multiple servers, call VisitServer if needed.
+bool
+UpnpDatabase::Visit(const DatabaseSelection &selection,
+ VisitDirectory visit_directory,
+ VisitSong visit_song,
+ VisitPlaylist visit_playlist,
+ Error &error) const
+{
+ auto vpath = stringToTokens(selection.uri, "/", true);
+ if (vpath.empty()) {
+ std::vector<ContentDirectoryService> servers;
+ if (!discovery->getDirServices(servers, error))
+ return false;
+
+ for (const auto &server : servers) {
+ if (visit_directory) {
+ const LightDirectory d(server.getFriendlyName(), 0);
+ if (!visit_directory(d, error))
+ return false;
+ }
+
+ if (selection.recursive &&
+ !VisitServer(server, vpath, selection,
+ visit_directory, visit_song, visit_playlist,
+ error))
+ return false;
+ }
+
+ return true;
+ }
+
+ // We do have a path: the first element selects the server
+ std::string servername(std::move(vpath.front()));
+ vpath.pop_front();
+
+ ContentDirectoryService server;
+ if (!discovery->getServer(servername.c_str(), server, error))
+ return false;
+
+ return VisitServer(server, vpath, selection,
+ visit_directory, visit_song, visit_playlist, error);
+}
+
+bool
+UpnpDatabase::VisitUniqueTags(const DatabaseSelection &selection,
+ TagType tag, gcc_unused uint32_t group_mask,
+ VisitTag visit_tag,
+ Error &error) const
+{
+ // TODO: use group_mask
+
+ if (!visit_tag)
+ return true;
+
+ std::vector<ContentDirectoryService> servers;
+ if (!discovery->getDirServices(servers, error))
+ return false;
+
+ std::set<std::string> values;
+ for (auto& server : servers) {
+ UPnPDirContent dirbuf;
+ if (!SearchSongs(server, rootid, selection, dirbuf, error))
+ return false;
+
+ for (const auto &dirent : dirbuf.objects) {
+ if (dirent.type != UPnPDirObject::Type::ITEM ||
+ dirent.item_class != UPnPDirObject::ItemClass::MUSIC)
+ continue;
+
+ const char *value = dirent.tag.GetValue(tag);
+ if (value != nullptr) {
+#if defined(__clang__) || GCC_CHECK_VERSION(4,8)
+ values.emplace(value);
+#else
+ values.insert(value);
+#endif
+ }
+ }
+ }
+
+ for (const auto& value : values) {
+ TagBuilder builder;
+ builder.AddItem(tag, value.c_str());
+ if (!visit_tag(builder.Commit(), error))
+ return false;
+ }
+
+ return true;
+}
+
+bool
+UpnpDatabase::GetStats(const DatabaseSelection &,
+ DatabaseStats &stats, Error &) const
+{
+ /* Note: this gets called before the daemonizing so we can't
+ reallyopen this would be a problem if we had real stats */
+ stats.Clear();
+ return true;
+}
+
+const DatabasePlugin upnp_db_plugin = {
+ "upnp",
+ 0,
+ UpnpDatabase::Create,
+};
diff --git a/src/db/plugins/upnp/UpnpDatabasePlugin.hxx b/src/db/plugins/upnp/UpnpDatabasePlugin.hxx
new file mode 100644
index 000000000..0228405cd
--- /dev/null
+++ b/src/db/plugins/upnp/UpnpDatabasePlugin.hxx
@@ -0,0 +1,27 @@
+/*
+ * 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_UPNP_DATABASE_PLUGIN_HXX
+#define MPD_UPNP_DATABASE_PLUGIN_HXX
+
+struct DatabasePlugin;
+
+extern const DatabasePlugin upnp_db_plugin;
+
+#endif
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..4136ccdad
--- /dev/null
+++ b/src/db/update/Editor.cxx
@@ -0,0 +1,119 @@
+/*
+ * 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.ForEachChildSafe([this](Directory &child){
+ DeleteDirectory(&child);
+ });
+
+ directory.ForEachSongSafe([this, &directory](Song &song){
+ 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..58e23ed7a
--- /dev/null
+++ b/src/db/update/Editor.hxx
@@ -0,0 +1,71 @@
+/*
+ * 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"
+#include "Compiler.h"
+
+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..233504ad6
--- /dev/null
+++ b/src/db/update/InotifySource.cxx
@@ -0,0 +1,116 @@
+/*
+ * 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 <algorithm>
+
+#include <sys/inotify.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdint.h>
+#include <limits.h>
+
+bool
+InotifySource::OnSocketReady(gcc_unused unsigned flags)
+{
+ uint8_t buffer[4096];
+ static_assert(sizeof(buffer) >= sizeof(struct inotify_event) + NAME_MAX + 1,
+ "inotify buffer too small");
+
+ ssize_t nbytes = read(Get(), buffer, sizeof(buffer));
+ if (nbytes < 0)
+ FatalSystemError("Failed to read from inotify");
+ if (nbytes == 0)
+ FatalError("end of file from inotify");
+
+ const uint8_t *p = buffer, *const end = p + nbytes;
+
+ while (true) {
+ const size_t remaining = end - p;
+ const struct inotify_event *event =
+ (const struct inotify_event *)p;
+ if (remaining < sizeof(*event) ||
+ remaining < sizeof(*event) + event->len)
+ break;
+
+ const char *name;
+ if (event->len > 0 && event->name[event->len - 1] == 0)
+ name = event->name;
+ else
+ name = nullptr;
+
+ callback(event->wd, event->mask, name, callback_ctx);
+ p += 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..2557680a0
--- /dev/null
+++ b/src/db/update/InotifySource.hxx
@@ -0,0 +1,72 @@
+/*
+ * 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 "Compiler.h"
+
+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;
+
+ 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..ce6d77d47
--- /dev/null
+++ b/src/db/update/Remove.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_REMOVE_HXX
+#define MPD_UPDATE_REMOVE_HXX
+
+#include "check.h"
+#include "event/DeferredMonitor.hxx"
+#include "thread/Mutex.hxx"
+#include "thread/Cond.hxx"
+#include "Compiler.h"
+
+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..feaeaebc5
--- /dev/null
+++ b/src/db/update/Service.hxx
@@ -0,0 +1,116 @@
+/*
+ * 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"
+#include "Compiler.h"
+
+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..f71faa86d
--- /dev/null
+++ b/src/db/update/Walk.cxx
@@ -0,0 +1,491 @@
+/*
+ * 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;
+}
+
+inline void
+UpdateWalk::RemoveExcludedFromDirectory(Directory &directory,
+ const ExcludeList &exclude_list)
+{
+ db_lock();
+
+ directory.ForEachChildSafe([&](Directory &child){
+ const auto name_fs =
+ AllocatedPath::FromUTF8(child.GetName());
+
+ if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
+ editor.DeleteDirectory(&child);
+ modified = true;
+ }
+ });
+
+ directory.ForEachSongSafe([&](Song &song){
+ 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.ForEachChildSafe([&](Directory &child){
+ if (DirectoryExists(storage, child))
+ return;
+
+ editor.LockDeleteDirectory(&child);
+
+ modified = true;
+ });
+
+ directory.ForEachSongSafe([&](Song &song){
+ 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->device == 0 && parent->inode == 0 &&
+ !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..a4c518813
--- /dev/null
+++ b/src/db/update/Walk.hxx
@@ -0,0 +1,157 @@
+/*
+ * 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 "Compiler.h"
+
+#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