aboutsummaryrefslogtreecommitdiffstats
path: root/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/db')
-rw-r--r--src/db/DatabaseError.cxx24
-rw-r--r--src/db/DatabaseError.hxx37
-rw-r--r--src/db/DatabaseGlue.cxx152
-rw-r--r--src/db/DatabaseGlue.hxx62
-rw-r--r--src/db/DatabaseListener.hxx38
-rw-r--r--src/db/DatabaseLock.cxx27
-rw-r--r--src/db/DatabaseLock.hxx97
-rw-r--r--src/db/DatabasePlaylist.cxx53
-rw-r--r--src/db/DatabasePlaylist.hxx34
-rw-r--r--src/db/DatabasePlugin.hxx159
-rw-r--r--src/db/DatabasePrint.cxx250
-rw-r--r--src/db/DatabasePrint.hxx54
-rw-r--r--src/db/DatabaseQueue.cxx57
-rw-r--r--src/db/DatabaseQueue.hxx31
-rw-r--r--src/db/DatabaseSave.cxx154
-rw-r--r--src/db/DatabaseSave.hxx35
-rw-r--r--src/db/DatabaseSimple.hxx74
-rw-r--r--src/db/DatabaseSong.cxx41
-rw-r--r--src/db/DatabaseSong.hxx38
-rw-r--r--src/db/Directory.cxx300
-rw-r--r--src/db/Directory.hxx248
-rw-r--r--src/db/DirectorySave.cxx163
-rw-r--r--src/db/DirectorySave.hxx35
-rw-r--r--src/db/Helpers.cxx131
-rw-r--r--src/db/Helpers.hxx41
-rw-r--r--src/db/LightDirectory.hxx61
-rw-r--r--src/db/LightSong.cxx33
-rw-r--r--src/db/LightSong.hxx92
-rw-r--r--src/db/Registry.cxx47
-rw-r--r--src/db/Registry.hxx37
-rw-r--r--src/db/Selection.cxx37
-rw-r--r--src/db/Selection.hxx51
-rw-r--r--src/db/Song.cxx110
-rw-r--r--src/db/Song.hxx112
-rw-r--r--src/db/SongSort.cxx114
-rw-r--r--src/db/SongSort.hxx28
-rw-r--r--src/db/Visitor.hxx37
-rw-r--r--src/db/plugins/LazyDatabase.cxx (renamed from src/db/LazyDatabase.cxx)0
-rw-r--r--src/db/plugins/LazyDatabase.hxx (renamed from src/db/LazyDatabase.hxx)2
-rw-r--r--src/db/plugins/ProxyDatabasePlugin.cxx (renamed from src/db/ProxyDatabasePlugin.cxx)12
-rw-r--r--src/db/plugins/ProxyDatabasePlugin.hxx (renamed from src/db/ProxyDatabasePlugin.hxx)0
-rw-r--r--src/db/plugins/SimpleDatabasePlugin.cxx (renamed from src/db/SimpleDatabasePlugin.cxx)16
-rw-r--r--src/db/plugins/SimpleDatabasePlugin.hxx (renamed from src/db/SimpleDatabasePlugin.hxx)4
-rw-r--r--src/db/plugins/UpnpDatabasePlugin.cxx (renamed from src/db/UpnpDatabasePlugin.cxx)10
-rw-r--r--src/db/plugins/UpnpDatabasePlugin.hxx (renamed from src/db/UpnpDatabasePlugin.hxx)0
-rw-r--r--src/db/plugins/upnp/Action.hxx (renamed from src/db/upnp/Action.hxx)0
-rw-r--r--src/db/plugins/upnp/ContentDirectoryService.cxx (renamed from src/db/upnp/ContentDirectoryService.cxx)0
-rw-r--r--src/db/plugins/upnp/ContentDirectoryService.hxx (renamed from src/db/upnp/ContentDirectoryService.hxx)0
-rw-r--r--src/db/plugins/upnp/Device.cxx (renamed from src/db/upnp/Device.cxx)0
-rw-r--r--src/db/plugins/upnp/Device.hxx (renamed from src/db/upnp/Device.hxx)0
-rw-r--r--src/db/plugins/upnp/Directory.cxx (renamed from src/db/upnp/Directory.cxx)0
-rw-r--r--src/db/plugins/upnp/Directory.hxx (renamed from src/db/upnp/Directory.hxx)0
-rw-r--r--src/db/plugins/upnp/Discovery.cxx (renamed from src/db/upnp/Discovery.cxx)0
-rw-r--r--src/db/plugins/upnp/Discovery.hxx (renamed from src/db/upnp/Discovery.hxx)0
-rw-r--r--src/db/plugins/upnp/Domain.cxx (renamed from src/db/upnp/Domain.cxx)0
-rw-r--r--src/db/plugins/upnp/Domain.hxx (renamed from src/db/upnp/Domain.hxx)0
-rw-r--r--src/db/plugins/upnp/Object.cxx (renamed from src/db/upnp/Object.cxx)0
-rw-r--r--src/db/plugins/upnp/Object.hxx (renamed from src/db/upnp/Object.hxx)0
-rw-r--r--src/db/plugins/upnp/Tags.cxx (renamed from src/db/upnp/Tags.cxx)0
-rw-r--r--src/db/plugins/upnp/Tags.hxx (renamed from src/db/upnp/Tags.hxx)0
-rw-r--r--src/db/plugins/upnp/Util.cxx (renamed from src/db/upnp/Util.cxx)0
-rw-r--r--src/db/plugins/upnp/Util.hxx (renamed from src/db/upnp/Util.hxx)0
-rw-r--r--src/db/plugins/upnp/WorkQueue.hxx (renamed from src/db/upnp/WorkQueue.hxx)0
-rw-r--r--src/db/plugins/upnp/ixmlwrap.cxx (renamed from src/db/upnp/ixmlwrap.cxx)0
-rw-r--r--src/db/plugins/upnp/ixmlwrap.hxx (renamed from src/db/upnp/ixmlwrap.hxx)0
-rw-r--r--src/db/plugins/upnp/upnpplib.cxx (renamed from src/db/upnp/upnpplib.cxx)0
-rw-r--r--src/db/plugins/upnp/upnpplib.hxx (renamed from src/db/upnp/upnpplib.hxx)0
-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.hxx41
-rw-r--r--src/db/update/InotifySource.cxx114
-rw-r--r--src/db/update/InotifySource.hxx74
-rw-r--r--src/db/update/InotifyUpdate.cxx339
-rw-r--r--src/db/update/InotifyUpdate.hxx47
-rw-r--r--src/db/update/UpdateArchive.cxx169
-rw-r--r--src/db/update/UpdateArchive.hxx50
-rw-r--r--src/db/update/UpdateContainer.cxx136
-rw-r--r--src/db/update/UpdateContainer.hxx36
-rw-r--r--src/db/update/UpdateDatabase.cxx104
-rw-r--r--src/db/update/UpdateDatabase.hxx50
-rw-r--r--src/db/update/UpdateDomain.cxx23
-rw-r--r--src/db/update/UpdateDomain.hxx25
-rw-r--r--src/db/update/UpdateGlue.cxx181
-rw-r--r--src/db/update/UpdateGlue.hxx43
-rw-r--r--src/db/update/UpdateIO.cxx113
-rw-r--r--src/db/update/UpdateIO.hxx50
-rw-r--r--src/db/update/UpdateInternal.hxx28
-rw-r--r--src/db/update/UpdateQueue.cxx49
-rw-r--r--src/db/update/UpdateQueue.hxx48
-rw-r--r--src/db/update/UpdateRemove.cxx97
-rw-r--r--src/db/update/UpdateRemove.hxx38
-rw-r--r--src/db/update/UpdateSong.cxx113
-rw-r--r--src/db/update/UpdateSong.hxx34
-rw-r--r--src/db/update/UpdateWalk.cxx484
-rw-r--r--src/db/update/UpdateWalk.hxx37
96 files changed, 5776 insertions, 22 deletions
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..1485a21b6
--- /dev/null
+++ b/src/db/DatabaseError.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_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,
+};
+
+extern const Domain db_domain;
+
+#endif
diff --git a/src/db/DatabaseGlue.cxx b/src/db/DatabaseGlue.cxx
new file mode 100644
index 000000000..3734e156c
--- /dev/null
+++ b/src/db/DatabaseGlue.cxx
@@ -0,0 +1,152 @@
+/*
+ * 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 "DatabaseSimple.hxx"
+#include "Registry.hxx"
+#include "DatabaseError.hxx"
+#include "Directory.hxx"
+#include "util/Error.hxx"
+#include "config/ConfigData.hxx"
+#include "Stats.hxx"
+#include "DatabasePlugin.hxx"
+#include "plugins/SimpleDatabasePlugin.hxx"
+
+#include <assert.h>
+#include <string.h>
+
+static Database *db;
+static bool db_is_open;
+static bool is_simple;
+
+bool
+DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener,
+ const config_param &param, Error &error)
+{
+ assert(db == nullptr);
+ assert(!db_is_open);
+
+ const char *plugin_name =
+ param.GetBlockValue("plugin", "simple");
+ is_simple = strcmp(plugin_name, "simple") == 0;
+
+ const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name);
+ if (plugin == nullptr) {
+ error.Format(db_domain,
+ "No such database plugin: %s", plugin_name);
+ return false;
+ }
+
+ db = plugin->create(loop, listener, param, error);
+ return db != nullptr;
+}
+
+void
+DatabaseGlobalDeinit(void)
+{
+ if (db_is_open)
+ db->Close();
+
+ if (db != nullptr)
+ delete db;
+}
+
+const Database *
+GetDatabase()
+{
+ assert(db == nullptr || db_is_open);
+
+ return db;
+}
+
+const Database *
+GetDatabase(Error &error)
+{
+ assert(db == nullptr || db_is_open);
+
+ if (db == nullptr)
+ error.Set(db_domain, DB_DISABLED, "No database");
+
+ return db;
+}
+
+bool
+db_is_simple(void)
+{
+ assert(db == nullptr || db_is_open);
+
+ return is_simple;
+}
+
+Directory *
+db_get_root(void)
+{
+ assert(db != nullptr);
+ assert(db_is_simple());
+
+ return ((SimpleDatabase *)db)->GetRoot();
+}
+
+Directory *
+db_get_directory(const char *name)
+{
+ if (db == nullptr)
+ return nullptr;
+
+ Directory *music_root = db_get_root();
+ if (name == nullptr)
+ return music_root;
+
+ return music_root->LookupDirectory(name);
+}
+
+bool
+db_save(Error &error)
+{
+ assert(db != nullptr);
+ assert(db_is_open);
+ assert(db_is_simple());
+
+ return ((SimpleDatabase *)db)->Save(error);
+}
+
+bool
+DatabaseGlobalOpen(Error &error)
+{
+ assert(db != nullptr);
+ assert(!db_is_open);
+
+ if (!db->Open(error))
+ return false;
+
+ db_is_open = true;
+
+ return true;
+}
+
+bool
+db_exists()
+{
+ assert(db != nullptr);
+ assert(db_is_open);
+ assert(db_is_simple());
+
+ return ((SimpleDatabase *)db)->GetUpdateStamp() > 0;
+}
diff --git a/src/db/DatabaseGlue.hxx b/src/db/DatabaseGlue.hxx
new file mode 100644
index 000000000..78032edb2
--- /dev/null
+++ b/src/db/DatabaseGlue.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_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
+ */
+bool
+DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener,
+ const config_param &param, Error &error);
+
+void
+DatabaseGlobalDeinit(void);
+
+bool
+DatabaseGlobalOpen(Error &error);
+
+/**
+ * Returns the global #Database instance. May return nullptr if this MPD
+ * configuration has no database (no music_directory was configured).
+ */
+gcc_const
+const Database *
+GetDatabase();
+
+/**
+ * Returns the global #Database instance. May return nullptr if this MPD
+ * configuration has no database (no music_directory was configured).
+ */
+gcc_pure
+const Database *
+GetDatabase(Error &error);
+
+#endif
diff --git a/src/db/DatabaseListener.hxx b/src/db/DatabaseListener.hxx
new file mode 100644
index 000000000..4da458866
--- /dev/null
+++ b/src/db/DatabaseListener.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_CLIENT_HXX
+#define MPD_DATABASE_CLIENT_HXX
+
+/**
+ * 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;
+};
+
+#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..64b365d2a
--- /dev/null
+++ b/src/db/DatabasePlaylist.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 "DatabasePlaylist.hxx"
+#include "Selection.hxx"
+#include "PlaylistFile.hxx"
+#include "DatabaseGlue.hxx"
+#include "DatabasePlugin.hxx"
+#include "DetachedSong.hxx"
+#include "Mapper.hxx"
+
+#include <functional>
+
+static bool
+AddSong(const char *playlist_path_utf8,
+ const LightSong &song, Error &error)
+{
+ return spl_append_song(playlist_path_utf8, map_song_detach(song),
+ error);
+}
+
+bool
+search_add_to_playlist(const char *uri, const char *playlist_path_utf8,
+ const SongFilter *filter,
+ Error &error)
+{
+ const Database *db = GetDatabase(error);
+ if (db == nullptr)
+ return false;
+
+ const DatabaseSelection selection(uri, true, filter);
+
+ using namespace std::placeholders;
+ const auto f = std::bind(AddSong, 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..1ee7584d3
--- /dev/null
+++ b/src/db/DatabasePlaylist.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_PLAYLIST_HXX
+#define MPD_DATABASE_PLAYLIST_HXX
+
+#include "Compiler.h"
+
+class SongFilter;
+class Error;
+
+gcc_nonnull(1,2)
+bool
+search_add_to_playlist(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..b0cb41502
--- /dev/null
+++ b/src/db/DatabasePlugin.hxx
@@ -0,0 +1,159 @@
+/*
+ * 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
+
+#include "Visitor.hxx"
+#include "tag/TagType.h"
+#include "Compiler.h"
+
+#include <time.h>
+
+struct config_param;
+struct DatabaseSelection;
+struct db_visitor;
+struct LightSong;
+class Error;
+class EventLoop;
+class DatabaseListener;
+
+struct DatabaseStats {
+ /**
+ * Number of songs.
+ */
+ unsigned song_count;
+
+ /**
+ * Total duration of all songs (in seconds).
+ */
+ unsigned long 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 = 0;
+ artist_count = album_count = 0;
+ }
+};
+
+class Database {
+public:
+ /**
+ * Free instance data.
+ */
+ virtual ~Database() {}
+
+ /**
+ * 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,
+ VisitString visit_string,
+ Error &error) const = 0;
+
+ virtual bool GetStats(const DatabaseSelection &selection,
+ DatabaseStats &stats,
+ Error &error) const = 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;
+};
+
+struct DatabasePlugin {
+ const char *name;
+
+ /**
+ * Allocates and configures a database.
+ */
+ Database *(*create)(EventLoop &loop, DatabaseListener &listener,
+ const config_param &param,
+ Error &error);
+};
+
+#endif
diff --git a/src/db/DatabasePrint.cxx b/src/db/DatabasePrint.cxx
new file mode 100644
index 000000000..9ed0b0826
--- /dev/null
+++ b/src/db/DatabasePrint.cxx
@@ -0,0 +1,250 @@
+/*
+ * 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 "DatabaseGlue.hxx"
+#include "DatabasePlugin.hxx"
+
+#include <functional>
+
+static bool
+PrintDirectoryBrief(Client &client, const LightDirectory &directory)
+{
+ if (!directory.IsRoot())
+ client_printf(client, "directory: %s\n", directory.GetPath());
+
+ return true;
+}
+
+static bool
+PrintDirectoryFull(Client &client, const LightDirectory &directory)
+{
+ if (!directory.IsRoot()) {
+ client_printf(client, "directory: %s\n", directory.GetPath());
+
+ if (directory.mtime > 0)
+ time_print(client, "Last-Modified", directory.mtime);
+ }
+
+ return true;
+}
+
+static void
+print_playlist_in_directory(Client &client,
+ const char *directory,
+ const char *name_utf8)
+{
+ if (directory == nullptr)
+ client_printf(client, "playlist: %s\n", name_utf8);
+ else
+ client_printf(client, "playlist: %s/%s\n",
+ directory, name_utf8);
+}
+
+static void
+print_playlist_in_directory(Client &client,
+ const LightDirectory *directory,
+ const char *name_utf8)
+{
+ if (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, const LightSong &song)
+{
+ song_print_uri(client, song);
+
+ if (song.tag->has_playlist)
+ /* this song file has an embedded CUE sheet */
+ print_playlist_in_directory(client, song.directory, song.uri);
+
+ return true;
+}
+
+static bool
+PrintSongFull(Client &client, const LightSong &song)
+{
+ song_print_info(client, song);
+
+ if (song.tag->has_playlist)
+ /* this song file has an embedded CUE sheet */
+ print_playlist_in_directory(client, song.directory, song.uri);
+
+ return true;
+}
+
+static bool
+PrintPlaylistBrief(Client &client,
+ const PlaylistInfo &playlist,
+ const LightDirectory &directory)
+{
+ print_playlist_in_directory(client, &directory, playlist.name.c_str());
+ return true;
+}
+
+static bool
+PrintPlaylistFull(Client &client,
+ const PlaylistInfo &playlist,
+ const LightDirectory &directory)
+{
+ print_playlist_in_directory(client, &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, Error &error)
+{
+ const Database *db = 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), _1)
+ : VisitDirectory();
+ const auto s = std::bind(full ? PrintSongFull : PrintSongBrief,
+ std::ref(client), _1);
+ const auto p = selection.filter == nullptr
+ ? std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief,
+ std::ref(client), _1, _2)
+ : VisitPlaylist();
+
+ return db->Visit(selection, d, s, p, error);
+}
+
+struct SearchStats {
+ int numberOfSongs;
+ unsigned long playTime;
+};
+
+static void printSearchStats(Client &client, SearchStats *stats)
+{
+ client_printf(client, "songs: %i\n", stats->numberOfSongs);
+ client_printf(client, "playtime: %li\n", stats->playTime);
+}
+
+static bool
+stats_visitor_song(SearchStats &stats, const LightSong &song)
+{
+ stats.numberOfSongs++;
+ stats.playTime += song.GetDuration();
+
+ return true;
+}
+
+bool
+searchStatsForSongsIn(Client &client, const char *name,
+ const SongFilter *filter,
+ Error &error)
+{
+ const Database *db = GetDatabase(error);
+ if (db == nullptr)
+ return false;
+
+ const DatabaseSelection selection(name, true, filter);
+
+ SearchStats stats;
+ stats.numberOfSongs = 0;
+ stats.playTime = 0;
+
+ 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);
+ return true;
+}
+
+bool
+printAllIn(Client &client, const char *uri_utf8, Error &error)
+{
+ const DatabaseSelection selection(uri_utf8, true);
+ return db_selection_print(client, selection, false, error);
+}
+
+bool
+printInfoForAllIn(Client &client, const char *uri_utf8,
+ Error &error)
+{
+ const DatabaseSelection selection(uri_utf8, true);
+ return db_selection_print(client, selection, true, 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 char *value)
+{
+ client_printf(client, "%s: %s\n", tag_item_names[tag_type], value);
+ return true;
+}
+
+bool
+listAllUniqueTags(Client &client, int type,
+ const SongFilter *filter,
+ Error &error)
+{
+ const Database *db = 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 {
+ using namespace std::placeholders;
+ const auto f = std::bind(PrintUniqueTag, std::ref(client),
+ (TagType)type, _1);
+ return db->VisitUniqueTags(selection, (TagType)type,
+ f, error);
+ }
+}
diff --git a/src/db/DatabasePrint.hxx b/src/db/DatabasePrint.hxx
new file mode 100644
index 000000000..2007e256b
--- /dev/null
+++ b/src/db/DatabasePrint.hxx
@@ -0,0 +1,54 @@
+/*
+ * 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"
+
+class SongFilter;
+struct DatabaseSelection;
+class Client;
+class Error;
+
+bool
+db_selection_print(Client &client, const DatabaseSelection &selection,
+ bool full, Error &error);
+
+gcc_nonnull(2)
+bool
+printAllIn(Client &client, const char *uri_utf8, Error &error);
+
+gcc_nonnull(2)
+bool
+printInfoForAllIn(Client &client, const char *uri_utf8,
+ Error &error);
+
+gcc_nonnull(2)
+bool
+searchStatsForSongsIn(Client &client, const char *name,
+ const SongFilter *filter,
+ Error &error);
+
+bool
+listAllUniqueTags(Client &client, int type,
+ const SongFilter *filter,
+ Error &error);
+
+#endif
diff --git a/src/db/DatabaseQueue.cxx b/src/db/DatabaseQueue.cxx
new file mode 100644
index 000000000..ee1dbd57c
--- /dev/null
+++ b/src/db/DatabaseQueue.cxx
@@ -0,0 +1,57 @@
+/*
+ * 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 "DatabaseGlue.hxx"
+#include "DatabasePlugin.hxx"
+#include "Partition.hxx"
+#include "util/Error.hxx"
+#include "DetachedSong.hxx"
+#include "Mapper.hxx"
+
+#include <functional>
+
+static bool
+AddToQueue(Partition &partition, const LightSong &song, Error &error)
+{
+ PlaylistResult result =
+ partition.playlist.AppendSong(partition.pc,
+ map_song_detach(song),
+ nullptr);
+ if (result != PlaylistResult::SUCCESS) {
+ error.Set(playlist_domain, int(result), "Playlist error");
+ return false;
+ }
+
+ return true;
+}
+
+bool
+AddFromDatabase(Partition &partition, const DatabaseSelection &selection,
+ Error &error)
+{
+ const Database *db = 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/DatabaseSave.cxx b/src/db/DatabaseSave.cxx
new file mode 100644
index 000000000..e9c81442b
--- /dev/null
+++ b/src/db/DatabaseSave.cxx
@@ -0,0 +1,154 @@
+/*
+ * 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 "DatabaseLock.hxx"
+#include "DatabaseError.hxx"
+#include "Directory.hxx"
+#include "DirectorySave.hxx"
+#include "fs/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 = 1;
+
+void
+db_save_internal(FILE *fp, const Directory &music_root)
+{
+ fprintf(fp, "%s\n", DIRECTORY_INFO_BEGIN);
+ fprintf(fp, DB_FORMAT_PREFIX "%u\n", DB_FORMAT);
+ fprintf(fp, "%s%s\n", DIRECTORY_MPD_VERSION, VERSION);
+ fprintf(fp, "%s%s\n", DIRECTORY_FS_CHARSET, GetFSCharset());
+
+ for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
+ if (!ignore_tag_items[i])
+ fprintf(fp, DB_TAG_PREFIX "%s\n", tag_item_names[i]);
+
+ fprintf(fp, "%s\n", DIRECTORY_INFO_END);
+
+ directory_save(fp, 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 != 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/DatabaseSave.hxx b/src/db/DatabaseSave.hxx
new file mode 100644
index 000000000..3bd3377ae
--- /dev/null
+++ b/src/db/DatabaseSave.hxx
@@ -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.
+ */
+
+#ifndef MPD_DATABASE_SAVE_HXX
+#define MPD_DATABASE_SAVE_HXX
+
+#include <stdio.h>
+
+struct Directory;
+class TextFile;
+class Error;
+
+void
+db_save_internal(FILE *file, const Directory &root);
+
+bool
+db_load_internal(TextFile &file, Directory &root, Error &error);
+
+#endif
diff --git a/src/db/DatabaseSimple.hxx b/src/db/DatabaseSimple.hxx
new file mode 100644
index 000000000..b99b3bfa5
--- /dev/null
+++ b/src/db/DatabaseSimple.hxx
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2003-2014 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPD_DATABASE_SIMPLE_HXX
+#define MPD_DATABASE_SIMPLE_HXX
+
+#include "Compiler.h"
+
+#include <sys/time.h>
+
+struct config_param;
+struct Directory;
+struct db_selection;
+struct db_visitor;
+class Error;
+
+/**
+ * Check whether the default #SimpleDatabasePlugin is used. This
+ * allows using db_get_root(), db_save(), db_get_mtime() and
+ * db_exists().
+ */
+bool
+db_is_simple(void);
+
+/**
+ * Returns the root directory object. Returns NULL if there is no
+ * configured music directory.
+ *
+ * May only be used if db_is_simple() returns true.
+ */
+gcc_pure
+Directory *
+db_get_root(void);
+
+/**
+ * Caller must lock the #db_mutex.
+ */
+gcc_nonnull(1)
+gcc_pure
+Directory *
+db_get_directory(const char *name);
+
+/**
+ * May only be used if db_is_simple() returns true.
+ */
+bool
+db_save(Error &error);
+
+/**
+ * Returns true if there is a valid database file on the disk.
+ *
+ * May only be used if db_is_simple() returns true.
+ */
+gcc_pure
+bool
+db_exists();
+
+#endif
diff --git a/src/db/DatabaseSong.cxx b/src/db/DatabaseSong.cxx
new file mode 100644
index 000000000..592d38b85
--- /dev/null
+++ b/src/db/DatabaseSong.cxx
@@ -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.
+ */
+
+#include "config.h"
+#include "DatabaseSong.hxx"
+#include "DatabaseGlue.hxx"
+#include "DatabasePlugin.hxx"
+#include "DetachedSong.hxx"
+#include "Mapper.hxx"
+
+DetachedSong *
+DatabaseDetachSong(const char *uri, Error &error)
+{
+ const Database *db = GetDatabase(error);
+ if (db == nullptr)
+ return nullptr;
+
+ const LightSong *tmp = db->GetSong(uri, error);
+ if (tmp == nullptr)
+ return nullptr;
+
+ DetachedSong *song = new DetachedSong(map_song_detach(*tmp));
+ db->ReturnSong(tmp);
+ return song;
+}
diff --git a/src/db/DatabaseSong.hxx b/src/db/DatabaseSong.hxx
new file mode 100644
index 000000000..0200af6b8
--- /dev/null
+++ b/src/db/DatabaseSong.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_SONG_HXX
+#define MPD_DATABASE_SONG_HXX
+
+#include "Compiler.h"
+
+class DetachedSong;
+class Error;
+
+/**
+ * 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 char *uri, Error &error);
+
+#endif
diff --git a/src/db/Directory.cxx b/src/db/Directory.cxx
new file mode 100644
index 000000000..e74eabd19
--- /dev/null
+++ b/src/db/Directory.cxx
@@ -0,0 +1,300 @@
+/*
+ * 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 "LightDirectory.hxx"
+#include "SongFilter.hxx"
+#include "PlaylistVector.hxx"
+#include "db/DatabaseLock.hxx"
+#include "SongSort.hxx"
+#include "Song.hxx"
+#include "LightSong.hxx"
+#include "fs/Traits.hxx"
+#include "util/Alloc.hxx"
+#include "util/Error.hxx"
+
+extern "C" {
+#include "util/list_sort.h"
+}
+
+#include <glib.h>
+
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+
+Directory::Directory(const char *_path_utf8, Directory *_parent)
+ :parent(_parent),
+ mtime(0), have_stat(false),
+ path(_path_utf8)
+{
+ INIT_LIST_HEAD(&children);
+ INIT_LIST_HEAD(&songs);
+}
+
+Directory::~Directory()
+{
+ Song *song, *ns;
+ directory_for_each_song_safe(song, ns, *this)
+ song->Free();
+
+ Directory *child, *n;
+ directory_for_each_child_safe(child, n, *this)
+ delete child;
+}
+
+void
+Directory::Delete()
+{
+ assert(holding_db_lock());
+ assert(parent != nullptr);
+
+ list_del(&siblings);
+ delete this;
+}
+
+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);
+
+ char *allocated;
+ const char *path_utf8;
+ if (IsRoot()) {
+ allocated = nullptr;
+ path_utf8 = name_utf8;
+ } else {
+ allocated = g_strconcat(GetPath(),
+ "/", name_utf8, nullptr);
+ path_utf8 = allocated;
+ }
+
+ Directory *child = new Directory(path_utf8, this);
+ g_free(allocated);
+
+ list_add_tail(&child->siblings, &children);
+ return child;
+}
+
+const Directory *
+Directory::FindChild(const char *name) const
+{
+ assert(holding_db_lock());
+
+ const Directory *child;
+ directory_for_each_child(child, *this)
+ if (strcmp(child->GetName(), name) == 0)
+ return child;
+
+ return nullptr;
+}
+
+void
+Directory::PruneEmpty()
+{
+ assert(holding_db_lock());
+
+ Directory *child, *n;
+ directory_for_each_child_safe(child, n, *this) {
+ child->PruneEmpty();
+
+ if (child->IsEmpty())
+ child->Delete();
+ }
+}
+
+Directory *
+Directory::LookupDirectory(const char *uri)
+{
+ assert(holding_db_lock());
+ assert(uri != nullptr);
+
+ if (isRootDirectory(uri))
+ return this;
+
+ char *duplicated = xstrdup(uri), *name = duplicated;
+
+ Directory *d = this;
+ while (1) {
+ char *slash = strchr(name, '/');
+ if (slash == name) {
+ d = nullptr;
+ break;
+ }
+
+ if (slash != nullptr)
+ *slash = '\0';
+
+ d = d->FindChild(name);
+ if (d == nullptr || slash == nullptr)
+ break;
+
+ name = slash + 1;
+ }
+
+ free(duplicated);
+
+ return d;
+}
+
+void
+Directory::AddSong(Song *song)
+{
+ assert(holding_db_lock());
+ assert(song != nullptr);
+ assert(song->parent == this);
+
+ list_add_tail(&song->siblings, &songs);
+}
+
+void
+Directory::RemoveSong(Song *song)
+{
+ assert(holding_db_lock());
+ assert(song != nullptr);
+ assert(song->parent == this);
+
+ list_del(&song->siblings);
+}
+
+const Song *
+Directory::FindSong(const char *name_utf8) const
+{
+ assert(holding_db_lock());
+ assert(name_utf8 != nullptr);
+
+ Song *song;
+ directory_for_each_song(song, *this) {
+ assert(song->parent == this);
+
+ if (strcmp(song->uri, name_utf8) == 0)
+ return song;
+ }
+
+ return nullptr;
+}
+
+Song *
+Directory::LookupSong(const char *uri)
+{
+ char *duplicated, *base;
+
+ assert(holding_db_lock());
+ assert(uri != nullptr);
+
+ duplicated = xstrdup(uri);
+ base = strrchr(duplicated, '/');
+
+ Directory *d = this;
+ if (base != nullptr) {
+ *base++ = 0;
+ d = d->LookupDirectory(duplicated);
+ if (d == nullptr) {
+ free(duplicated);
+ return nullptr;
+ }
+ } else
+ base = duplicated;
+
+ Song *song = d->FindSong(base);
+ assert(song == nullptr || song->parent == d);
+
+ free(duplicated);
+ return song;
+
+}
+
+static int
+directory_cmp(gcc_unused void *priv,
+ struct list_head *_a, struct list_head *_b)
+{
+ const Directory *a = (const Directory *)_a;
+ const Directory *b = (const Directory *)_b;
+ return g_utf8_collate(a->path.c_str(), b->path.c_str());
+}
+
+void
+Directory::Sort()
+{
+ assert(holding_db_lock());
+
+ list_sort(nullptr, &children, directory_cmp);
+ song_list_sort(&songs);
+
+ Directory *child;
+ directory_for_each_child(child, *this)
+ 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 (visit_song) {
+ Song *song;
+ directory_for_each_song(song, *this) {
+ 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;
+ }
+
+ Directory *child;
+ directory_for_each_child(child, *this) {
+ 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/Directory.hxx b/src/db/Directory.hxx
new file mode 100644
index 000000000..e114b27f4
--- /dev/null
+++ b/src/db/Directory.hxx
@@ -0,0 +1,248 @@
+/*
+ * 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 "util/list.h"
+#include "Compiler.h"
+#include "db/Visitor.hxx"
+#include "PlaylistVector.hxx"
+
+#include <string>
+
+#include <sys/types.h>
+
+#define DEVICE_INARCHIVE (dev_t)(-1)
+#define DEVICE_CONTAINER (dev_t)(-2)
+
+#define directory_for_each_child(pos, directory) \
+ list_for_each_entry(pos, &(directory).children, siblings)
+
+#define directory_for_each_child_safe(pos, n, directory) \
+ list_for_each_entry_safe(pos, n, &(directory).children, siblings)
+
+#define directory_for_each_song(pos, directory) \
+ list_for_each_entry(pos, &(directory).songs, siblings)
+
+#define directory_for_each_song_safe(pos, n, directory) \
+ list_for_each_entry_safe(pos, n, &(directory).songs, siblings)
+
+struct Song;
+struct db_visitor;
+class SongFilter;
+class Error;
+
+struct 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.
+ */
+ struct list_head siblings;
+
+ /**
+ * 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.
+ */
+ struct list_head 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.
+ */
+ struct list_head songs;
+
+ PlaylistVector playlists;
+
+ Directory *parent;
+ time_t mtime;
+ ino_t inode;
+ dev_t device;
+ bool have_stat; /* not needed if ino_t == dev_t == 0 is impossible */
+
+ std::string path;
+
+public:
+ Directory(const char *_path_utf8, Directory *_parent);
+ ~Directory();
+
+ /**
+ * Create a new root #Directory object.
+ */
+ gcc_malloc
+ static Directory *NewRoot() {
+ return new Directory("", 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;
+ }
+
+ /**
+ * Looks up a directory by its relative URI.
+ *
+ * @param uri the relative URI
+ * @return the Directory, or nullptr if none was found
+ */
+ gcc_pure
+ Directory *LookupDirectory(const char *uri);
+
+ gcc_pure
+ bool IsEmpty() const {
+ return list_empty(&children) &&
+ list_empty(&songs) &&
+ 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;
+ }
+
+ /**
+ * 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));
+ }
+
+ /**
+ * Looks up a song by its relative URI.
+ *
+ * Caller must lock the #db_mutex.
+ *
+ * @param uri the relative URI
+ * @return the song, or nullptr if none was found
+ */
+ gcc_pure
+ Song *LookupSong(const char *uri);
+
+ /**
+ * 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;
+};
+
+static inline bool
+isRootDirectory(const char *name)
+{
+ return name[0] == 0 || (name[0] == '/' && name[1] == 0);
+}
+
+#endif
diff --git a/src/db/DirectorySave.cxx b/src/db/DirectorySave.cxx
new file mode 100644
index 000000000..499f84734
--- /dev/null
+++ b/src/db/DirectorySave.cxx
@@ -0,0 +1,163 @@
+/*
+ * 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/TextFile.hxx"
+#include "util/StringUtil.hxx"
+#include "util/NumberParser.hxx"
+#include "util/Error.hxx"
+#include "util/Domain.hxx"
+
+#include <stddef.h>
+
+#define DIRECTORY_DIR "directory: "
+#define DIRECTORY_MTIME "mtime: "
+#define DIRECTORY_BEGIN "begin: "
+#define DIRECTORY_END "end: "
+
+static constexpr Domain directory_domain("directory");
+
+void
+directory_save(FILE *fp, const Directory &directory)
+{
+ if (!directory.IsRoot()) {
+ fprintf(fp, DIRECTORY_MTIME "%lu\n",
+ (unsigned long)directory.mtime);
+
+ fprintf(fp, "%s%s\n", DIRECTORY_BEGIN, directory.GetPath());
+ }
+
+ Directory *cur;
+ directory_for_each_child(cur, directory) {
+ fprintf(fp, DIRECTORY_DIR "%s\n", cur->GetName());
+
+ directory_save(fp, *cur);
+
+ if (ferror(fp))
+ return;
+ }
+
+ Song *song;
+ directory_for_each_song(song, directory)
+ song_save(fp, *song);
+
+ playlist_vector_save(fp, directory.playlists);
+
+ if (!directory.IsRoot())
+ fprintf(fp, DIRECTORY_END "%s\n", directory.GetPath());
+}
+
+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);
+
+ const char *line = file.ReadLine();
+ if (line == nullptr) {
+ error.Set(directory_domain, "Unexpected end of file");
+ directory->Delete();
+ return nullptr;
+ }
+
+ if (StringStartsWith(line, DIRECTORY_MTIME)) {
+ directory->mtime =
+ ParseUint64(line + sizeof(DIRECTORY_MTIME) - 1);
+
+ line = file.ReadLine();
+ if (line == nullptr) {
+ error.Set(directory_domain, "Unexpected end of file");
+ directory->Delete();
+ return nullptr;
+ }
+ }
+
+ if (!StringStartsWith(line, DIRECTORY_BEGIN)) {
+ 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/DirectorySave.hxx b/src/db/DirectorySave.hxx
new file mode 100644
index 000000000..07e9e158b
--- /dev/null
+++ b/src/db/DirectorySave.hxx
@@ -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.
+ */
+
+#ifndef MPD_DIRECTORY_SAVE_HXX
+#define MPD_DIRECTORY_SAVE_HXX
+
+#include <stdio.h>
+
+struct Directory;
+class TextFile;
+class Error;
+
+void
+directory_save(FILE *fp, const Directory &directory);
+
+bool
+directory_load(TextFile &file, Directory &directory, Error &error);
+
+#endif
diff --git a/src/db/Helpers.cxx b/src/db/Helpers.cxx
new file mode 100644
index 000000000..579b83e15
--- /dev/null
+++ b/src/db/Helpers.cxx
@@ -0,0 +1,131 @@
+/*
+ * 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 "DatabasePlugin.hxx"
+#include "LightSong.hxx"
+#include "tag/Tag.hxx"
+
+#include <functional>
+#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 bool
+CollectTags(StringSet &set, TagType tag_type, const LightSong &song)
+{
+ const Tag *tag = song.tag;
+
+ bool found = false;
+ for (unsigned i = 0; i < tag->num_items; ++i) {
+ if (tag->items[i]->type == tag_type) {
+ set.insert(tag->items[i]->value);
+ found = true;
+ }
+ }
+
+ if (!found)
+ set.insert("");
+
+ return true;
+}
+
+bool
+VisitUniqueTags(const Database &db, const DatabaseSelection &selection,
+ TagType tag_type,
+ VisitString visit_string,
+ Error &error)
+{
+ StringSet set;
+
+ using namespace std::placeholders;
+ const auto f = std::bind(CollectTags, std::ref(set), tag_type, _1);
+ if (!db.Visit(selection, f, error))
+ return false;
+
+ for (auto value : set)
+ if (!visit_string(value, error))
+ return false;
+
+ return true;
+}
+
+static void
+StatsVisitTag(DatabaseStats &stats, StringSet &artists, StringSet &albums,
+ const Tag &tag)
+{
+ if (tag.time > 0)
+ stats.total_duration += tag.time;
+
+ for (unsigned i = 0; i < tag.num_items; ++i) {
+ const TagItem &item = *tag.items[i];
+
+ switch (item.type) {
+ case TAG_ARTIST:
+ artists.insert(item.value);
+ break;
+
+ case TAG_ALBUM:
+ albums.insert(item.value);
+ 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..24db260c0
--- /dev/null
+++ b/src/db/Helpers.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_MEMORY_DATABASE_PLUGIN_HXX
+#define MPD_MEMORY_DATABASE_PLUGIN_HXX
+
+#include "Visitor.hxx"
+#include "tag/TagType.h"
+
+class Error;
+class Database;
+struct DatabaseSelection;
+struct DatabaseStats;
+
+bool
+VisitUniqueTags(const Database &db, const DatabaseSelection &selection,
+ TagType tag_type,
+ VisitString visit_string,
+ Error &error);
+
+bool
+GetStats(const Database &db, const DatabaseSelection &selection,
+ DatabaseStats &stats, Error &error);
+
+#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..af1e801f8
--- /dev/null
+++ b/src/db/LightSong.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 "LightSong.hxx"
+#include "tag/Tag.hxx"
+
+double
+LightSong::GetDuration() const
+{
+ if (end_ms > 0)
+ return (end_ms - start_ms) / 1000.0;
+
+ if (tag->time <= 0)
+ return 0;
+
+ return tag->time - start_ms / 1000.0;
+}
diff --git a/src/db/LightSong.hxx b/src/db/LightSong.hxx
new file mode 100644
index 000000000..c0cd47749
--- /dev/null
+++ b/src/db/LightSong.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.
+ */
+
+#ifndef MPD_LIGHT_SONG_HXX
+#define MPD_LIGHT_SONG_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 empty, then #uri (and
+ * #directory) shall be used.
+ *
+ * This attribute is used for songs from the database which
+ * have a relative URI.
+ */
+ std::string real_uri;
+
+ /**
+ * Must not be nullptr.
+ */
+ const Tag *tag;
+
+ time_t mtime;
+
+ /**
+ * Start of this sub-song within the file in milliseconds.
+ */
+ unsigned start_ms;
+
+ /**
+ * End of this sub-song within the file in milliseconds.
+ * Unused if zero.
+ */
+ unsigned end_ms;
+
+ 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
+ double GetDuration() const;
+};
+
+#endif
diff --git a/src/db/Registry.cxx b/src/db/Registry.cxx
new file mode 100644
index 000000000..295d3cf2a
--- /dev/null
+++ b/src/db/Registry.cxx
@@ -0,0 +1,47 @@
+/*
+ * 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 "plugins/SimpleDatabasePlugin.hxx"
+#include "plugins/ProxyDatabasePlugin.hxx"
+#include "plugins/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..96382eed7
--- /dev/null
+++ b/src/db/Selection.cxx
@@ -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.
+ */
+
+#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::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..a39ce7afe
--- /dev/null
+++ b/src/db/Selection.hxx
@@ -0,0 +1,51 @@
+/*
+ * 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 Match(const LightSong &song) const;
+};
+
+#endif
diff --git a/src/db/Song.cxx b/src/db/Song.cxx
new file mode 100644
index 000000000..15924a40a
--- /dev/null
+++ b/src/db/Song.cxx
@@ -0,0 +1,110 @@
+/*
+ * 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 "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_ms(0), end_ms(0)
+{
+ 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_ms = other.GetStartMS();
+ song->end_ms = other.GetEndMS();
+ 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.tag = &tag;
+ dest.mtime = mtime;
+ dest.start_ms = start_ms;
+ dest.end_ms = end_ms;
+ return dest;
+}
diff --git a/src/db/Song.hxx b/src/db/Song.hxx
new file mode 100644
index 000000000..0b94fe6d0
--- /dev/null
+++ b/src/db/Song.hxx
@@ -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.
+ */
+
+#ifndef MPD_SONG_HXX
+#define MPD_SONG_HXX
+
+#include "util/list.h"
+#include "tag/Tag.hxx"
+#include "Compiler.h"
+
+#include <string>
+
+#include <assert.h>
+#include <time.h>
+
+struct LightSong;
+struct Directory;
+class DetachedSong;
+
+/**
+ * A song file inside the configured music directory.
+ */
+struct Song {
+ /**
+ * 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.
+ */
+ struct list_head siblings;
+
+ Tag tag;
+
+ /**
+ * The #Directory that contains this song. May be nullptr if
+ * the current database plugin does not manage the parent
+ * directory this way.
+ */
+ Directory *const parent;
+
+ time_t mtime;
+
+ /**
+ * Start of this sub-song within the file in milliseconds.
+ */
+ unsigned start_ms;
+
+ /**
+ * End of this sub-song within the file in milliseconds.
+ * Unused if zero.
+ */
+ unsigned end_ms;
+
+ /**
+ * The file name. If #parent is nullptr, then this is the URI
+ * relative to the music directory.
+ */
+ 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(const char *path_utf8, Directory &parent);
+
+ void Free();
+
+ bool UpdateFile();
+ bool UpdateFileInArchive();
+
+ /**
+ * 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;
+};
+
+#endif
diff --git a/src/db/SongSort.cxx b/src/db/SongSort.cxx
new file mode 100644
index 000000000..dcea033b6
--- /dev/null
+++ b/src/db/SongSort.cxx
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2003-2014 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+#include "SongSort.hxx"
+#include "Song.hxx"
+#include "tag/Tag.hxx"
+
+extern "C" {
+#include "util/list_sort.h"
+}
+
+#include <glib.h>
+
+#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 g_utf8_collate(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 */
+static int
+song_cmp(gcc_unused void *priv, struct list_head *_a, struct list_head *_b)
+{
+ const Song *a = (const Song *)_a;
+ const Song *b = (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;
+
+ /* then sort by disc */
+ ret = compare_tag_item(a->tag, b->tag, TAG_DISC);
+ if (ret != 0)
+ return ret;
+
+ /* then by track number */
+ ret = compare_tag_item(a->tag, b->tag, TAG_TRACK);
+ if (ret != 0)
+ return ret;
+
+ /* still no difference? compare file name */
+ return g_utf8_collate(a->uri, b->uri);
+}
+
+void
+song_list_sort(struct list_head *songs)
+{
+ list_sort(nullptr, songs, song_cmp);
+}
diff --git a/src/db/SongSort.hxx b/src/db/SongSort.hxx
new file mode 100644
index 000000000..28b903532
--- /dev/null
+++ b/src/db/SongSort.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_SONG_SORT_HXX
+#define MPD_SONG_SORT_HXX
+
+struct list_head;
+
+void
+song_list_sort(list_head *songs);
+
+#endif
diff --git a/src/db/Visitor.hxx b/src/db/Visitor.hxx
new file mode 100644
index 000000000..0ec29bf49
--- /dev/null
+++ b/src/db/Visitor.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_VISITOR_HXX
+#define MPD_DATABASE_VISITOR_HXX
+
+#include <functional>
+
+struct LightDirectory;
+struct LightSong;
+struct PlaylistInfo;
+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 char *, Error &)> VisitString;
+
+#endif
diff --git a/src/db/LazyDatabase.cxx b/src/db/plugins/LazyDatabase.cxx
index 6a01ffb82..6a01ffb82 100644
--- a/src/db/LazyDatabase.cxx
+++ b/src/db/plugins/LazyDatabase.cxx
diff --git a/src/db/LazyDatabase.hxx b/src/db/plugins/LazyDatabase.hxx
index f718ecb3f..336b8558f 100644
--- a/src/db/LazyDatabase.hxx
+++ b/src/db/plugins/LazyDatabase.hxx
@@ -20,7 +20,7 @@
#ifndef MPD_LAZY_DATABASE_PLUGIN_HXX
#define MPD_LAZY_DATABASE_PLUGIN_HXX
-#include "DatabasePlugin.hxx"
+#include "db/DatabasePlugin.hxx"
/**
* A wrapper for a #Database object that gets opened on the first
diff --git a/src/db/ProxyDatabasePlugin.cxx b/src/db/plugins/ProxyDatabasePlugin.cxx
index 7253ba0d0..daa963c7d 100644
--- a/src/db/ProxyDatabasePlugin.cxx
+++ b/src/db/plugins/ProxyDatabasePlugin.cxx
@@ -19,13 +19,13 @@
#include "config.h"
#include "ProxyDatabasePlugin.hxx"
-#include "DatabasePlugin.hxx"
-#include "DatabaseListener.hxx"
-#include "DatabaseSelection.hxx"
-#include "DatabaseError.hxx"
+#include "db/DatabasePlugin.hxx"
+#include "db/DatabaseListener.hxx"
+#include "db/Selection.hxx"
+#include "db/DatabaseError.hxx"
#include "PlaylistInfo.hxx"
-#include "LightDirectory.hxx"
-#include "LightSong.hxx"
+#include "db/LightDirectory.hxx"
+#include "db/LightSong.hxx"
#include "SongFilter.hxx"
#include "Compiler.h"
#include "config/ConfigData.hxx"
diff --git a/src/db/ProxyDatabasePlugin.hxx b/src/db/plugins/ProxyDatabasePlugin.hxx
index 699d374b5..699d374b5 100644
--- a/src/db/ProxyDatabasePlugin.hxx
+++ b/src/db/plugins/ProxyDatabasePlugin.hxx
diff --git a/src/db/SimpleDatabasePlugin.cxx b/src/db/plugins/SimpleDatabasePlugin.cxx
index 73e080b41..55e08b6d7 100644
--- a/src/db/SimpleDatabasePlugin.cxx
+++ b/src/db/plugins/SimpleDatabasePlugin.cxx
@@ -19,15 +19,15 @@
#include "config.h"
#include "SimpleDatabasePlugin.hxx"
-#include "DatabaseSelection.hxx"
-#include "DatabaseHelpers.hxx"
-#include "LightDirectory.hxx"
-#include "Directory.hxx"
-#include "Song.hxx"
+#include "db/Selection.hxx"
+#include "db/Helpers.hxx"
+#include "db/LightDirectory.hxx"
+#include "db/Directory.hxx"
+#include "db/Song.hxx"
#include "SongFilter.hxx"
-#include "DatabaseSave.hxx"
-#include "DatabaseLock.hxx"
-#include "DatabaseError.hxx"
+#include "db/DatabaseSave.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/DatabaseError.hxx"
#include "fs/TextFile.hxx"
#include "config/ConfigData.hxx"
#include "fs/FileSystem.hxx"
diff --git a/src/db/SimpleDatabasePlugin.hxx b/src/db/plugins/SimpleDatabasePlugin.hxx
index 509b91e4e..137a60884 100644
--- a/src/db/SimpleDatabasePlugin.hxx
+++ b/src/db/plugins/SimpleDatabasePlugin.hxx
@@ -20,9 +20,9 @@
#ifndef MPD_SIMPLE_DATABASE_PLUGIN_HXX
#define MPD_SIMPLE_DATABASE_PLUGIN_HXX
-#include "DatabasePlugin.hxx"
+#include "db/DatabasePlugin.hxx"
#include "fs/AllocatedPath.hxx"
-#include "LightSong.hxx"
+#include "db/LightSong.hxx"
#include "Compiler.h"
#include <cassert>
diff --git a/src/db/UpnpDatabasePlugin.cxx b/src/db/plugins/UpnpDatabasePlugin.cxx
index 46084061f..10575fc94 100644
--- a/src/db/UpnpDatabasePlugin.cxx
+++ b/src/db/plugins/UpnpDatabasePlugin.cxx
@@ -26,11 +26,11 @@
#include "upnp/Directory.hxx"
#include "upnp/Tags.hxx"
#include "upnp/Util.hxx"
-#include "DatabasePlugin.hxx"
-#include "DatabaseSelection.hxx"
-#include "DatabaseError.hxx"
-#include "LightDirectory.hxx"
-#include "LightSong.hxx"
+#include "db/DatabasePlugin.hxx"
+#include "db/Selection.hxx"
+#include "db/DatabaseError.hxx"
+#include "db/LightDirectory.hxx"
+#include "db/LightSong.hxx"
#include "config/ConfigData.hxx"
#include "tag/TagBuilder.hxx"
#include "tag/TagTable.hxx"
diff --git a/src/db/UpnpDatabasePlugin.hxx b/src/db/plugins/UpnpDatabasePlugin.hxx
index 0228405cd..0228405cd 100644
--- a/src/db/UpnpDatabasePlugin.hxx
+++ b/src/db/plugins/UpnpDatabasePlugin.hxx
diff --git a/src/db/upnp/Action.hxx b/src/db/plugins/upnp/Action.hxx
index 28c88be92..28c88be92 100644
--- a/src/db/upnp/Action.hxx
+++ b/src/db/plugins/upnp/Action.hxx
diff --git a/src/db/upnp/ContentDirectoryService.cxx b/src/db/plugins/upnp/ContentDirectoryService.cxx
index 35445e09d..35445e09d 100644
--- a/src/db/upnp/ContentDirectoryService.cxx
+++ b/src/db/plugins/upnp/ContentDirectoryService.cxx
diff --git a/src/db/upnp/ContentDirectoryService.hxx b/src/db/plugins/upnp/ContentDirectoryService.hxx
index 24be5dfbf..24be5dfbf 100644
--- a/src/db/upnp/ContentDirectoryService.hxx
+++ b/src/db/plugins/upnp/ContentDirectoryService.hxx
diff --git a/src/db/upnp/Device.cxx b/src/db/plugins/upnp/Device.cxx
index 7bec1cccd..7bec1cccd 100644
--- a/src/db/upnp/Device.cxx
+++ b/src/db/plugins/upnp/Device.cxx
diff --git a/src/db/upnp/Device.hxx b/src/db/plugins/upnp/Device.hxx
index dd7ecac2d..dd7ecac2d 100644
--- a/src/db/upnp/Device.hxx
+++ b/src/db/plugins/upnp/Device.hxx
diff --git a/src/db/upnp/Directory.cxx b/src/db/plugins/upnp/Directory.cxx
index adb8b213a..adb8b213a 100644
--- a/src/db/upnp/Directory.cxx
+++ b/src/db/plugins/upnp/Directory.cxx
diff --git a/src/db/upnp/Directory.hxx b/src/db/plugins/upnp/Directory.hxx
index 433979900..433979900 100644
--- a/src/db/upnp/Directory.hxx
+++ b/src/db/plugins/upnp/Directory.hxx
diff --git a/src/db/upnp/Discovery.cxx b/src/db/plugins/upnp/Discovery.cxx
index 5203dba83..5203dba83 100644
--- a/src/db/upnp/Discovery.cxx
+++ b/src/db/plugins/upnp/Discovery.cxx
diff --git a/src/db/upnp/Discovery.hxx b/src/db/plugins/upnp/Discovery.hxx
index 4c64fe420..4c64fe420 100644
--- a/src/db/upnp/Discovery.hxx
+++ b/src/db/plugins/upnp/Discovery.hxx
diff --git a/src/db/upnp/Domain.cxx b/src/db/plugins/upnp/Domain.cxx
index 010d4c7c2..010d4c7c2 100644
--- a/src/db/upnp/Domain.cxx
+++ b/src/db/plugins/upnp/Domain.cxx
diff --git a/src/db/upnp/Domain.hxx b/src/db/plugins/upnp/Domain.hxx
index ec01ef735..ec01ef735 100644
--- a/src/db/upnp/Domain.hxx
+++ b/src/db/plugins/upnp/Domain.hxx
diff --git a/src/db/upnp/Object.cxx b/src/db/plugins/upnp/Object.cxx
index 703fb0be4..703fb0be4 100644
--- a/src/db/upnp/Object.cxx
+++ b/src/db/plugins/upnp/Object.cxx
diff --git a/src/db/upnp/Object.hxx b/src/db/plugins/upnp/Object.hxx
index 16a66c774..16a66c774 100644
--- a/src/db/upnp/Object.hxx
+++ b/src/db/plugins/upnp/Object.hxx
diff --git a/src/db/upnp/Tags.cxx b/src/db/plugins/upnp/Tags.cxx
index fd65df4d0..fd65df4d0 100644
--- a/src/db/upnp/Tags.cxx
+++ b/src/db/plugins/upnp/Tags.cxx
diff --git a/src/db/upnp/Tags.hxx b/src/db/plugins/upnp/Tags.hxx
index ec6d18478..ec6d18478 100644
--- a/src/db/upnp/Tags.hxx
+++ b/src/db/plugins/upnp/Tags.hxx
diff --git a/src/db/upnp/Util.cxx b/src/db/plugins/upnp/Util.cxx
index cf34a47d3..cf34a47d3 100644
--- a/src/db/upnp/Util.cxx
+++ b/src/db/plugins/upnp/Util.cxx
diff --git a/src/db/upnp/Util.hxx b/src/db/plugins/upnp/Util.hxx
index 58e382faa..58e382faa 100644
--- a/src/db/upnp/Util.hxx
+++ b/src/db/plugins/upnp/Util.hxx
diff --git a/src/db/upnp/WorkQueue.hxx b/src/db/plugins/upnp/WorkQueue.hxx
index fe8ce53f9..fe8ce53f9 100644
--- a/src/db/upnp/WorkQueue.hxx
+++ b/src/db/plugins/upnp/WorkQueue.hxx
diff --git a/src/db/upnp/ixmlwrap.cxx b/src/db/plugins/upnp/ixmlwrap.cxx
index 6a2829cf9..6a2829cf9 100644
--- a/src/db/upnp/ixmlwrap.cxx
+++ b/src/db/plugins/upnp/ixmlwrap.cxx
diff --git a/src/db/upnp/ixmlwrap.hxx b/src/db/plugins/upnp/ixmlwrap.hxx
index 0d519a323..0d519a323 100644
--- a/src/db/upnp/ixmlwrap.hxx
+++ b/src/db/plugins/upnp/ixmlwrap.hxx
diff --git a/src/db/upnp/upnpplib.cxx b/src/db/plugins/upnp/upnpplib.cxx
index 27b4e0564..27b4e0564 100644
--- a/src/db/upnp/upnpplib.cxx
+++ b/src/db/plugins/upnp/upnpplib.cxx
diff --git a/src/db/upnp/upnpplib.hxx b/src/db/plugins/upnp/upnpplib.hxx
index 6759aa16d..6759aa16d 100644
--- a/src/db/upnp/upnpplib.hxx
+++ b/src/db/plugins/upnp/upnpplib.hxx
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..f4bccf7ae
--- /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 "UpdateGlue.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..99e2635b1
--- /dev/null
+++ b/src/db/update/InotifyQueue.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_INOTIFY_QUEUE_HXX
+#define MPD_INOTIFY_QUEUE_HXX
+
+#include "event/TimeoutMonitor.hxx"
+#include "Compiler.h"
+
+#include <list>
+#include <string>
+
+class InotifyQueue final : private TimeoutMonitor {
+ std::list<std::string> queue;
+
+public:
+ InotifyQueue(EventLoop &_loop):TimeoutMonitor(_loop) {}
+
+ void Enqueue(const char *uri_utf8);
+
+private:
+ virtual void OnTimeout() override;
+};
+
+#endif
diff --git a/src/db/update/InotifySource.cxx b/src/db/update/InotifySource.cxx
new file mode 100644
index 000000000..c2783690e
--- /dev/null
+++ b/src/db/update/InotifySource.cxx
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2003-2014 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+#include "InotifySource.hxx"
+#include "InotifyDomain.hxx"
+#include "util/Error.hxx"
+#include "system/fd_util.h"
+#include "system/FatalError.hxx"
+#include "Log.hxx"
+
+#include <sys/inotify.h>
+#include <unistd.h>
+#include <errno.h>
+
+bool
+InotifySource::OnSocketReady(gcc_unused unsigned flags)
+{
+ const auto dest = buffer.Write();
+ if (dest.IsEmpty())
+ FatalError("buffer full");
+
+ ssize_t nbytes = read(Get(), dest.data, dest.size);
+ if (nbytes < 0)
+ FatalSystemError("Failed to read from inotify");
+ if (nbytes == 0)
+ FatalError("end of file from inotify");
+
+ buffer.Append(nbytes);
+
+ while (true) {
+ const char *name;
+
+ auto range = buffer.Read();
+ const struct inotify_event *event =
+ (const struct inotify_event *)
+ range.data;
+ if (range.size < sizeof(*event) ||
+ range.size < sizeof(*event) + event->len)
+ break;
+
+ if (event->len > 0 && event->name[event->len - 1] == 0)
+ name = event->name;
+ else
+ name = nullptr;
+
+ callback(event->wd, event->mask, name, callback_ctx);
+ buffer.Consume(sizeof(*event) + event->len);
+ }
+
+ return true;
+}
+
+inline
+InotifySource::InotifySource(EventLoop &_loop,
+ mpd_inotify_callback_t _callback, void *_ctx,
+ int _fd)
+ :SocketMonitor(_fd, _loop),
+ callback(_callback), callback_ctx(_ctx)
+{
+ ScheduleRead();
+
+}
+
+InotifySource *
+InotifySource::Create(EventLoop &loop,
+ mpd_inotify_callback_t callback, void *callback_ctx,
+ Error &error)
+{
+ int fd = inotify_init_cloexec();
+ if (fd < 0) {
+ error.SetErrno("inotify_init() has failed");
+ return nullptr;
+ }
+
+ return new InotifySource(loop, callback, callback_ctx, fd);
+}
+
+int
+InotifySource::Add(const char *path_fs, unsigned mask, Error &error)
+{
+ int wd = inotify_add_watch(Get(), path_fs, mask);
+ if (wd < 0)
+ error.SetErrno("inotify_add_watch() has failed");
+
+ return wd;
+}
+
+void
+InotifySource::Remove(unsigned wd)
+{
+ int ret = inotify_rm_watch(Get(), wd);
+ if (ret < 0 && errno != EINVAL)
+ LogErrno(inotify_domain, "inotify_rm_watch() has failed");
+
+ /* EINVAL may happen here when the file has been deleted; the
+ kernel seems to auto-unregister deleted files */
+}
diff --git a/src/db/update/InotifySource.hxx b/src/db/update/InotifySource.hxx
new file mode 100644
index 000000000..77c11093c
--- /dev/null
+++ b/src/db/update/InotifySource.hxx
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2003-2014 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPD_INOTIFY_SOURCE_HXX
+#define MPD_INOTIFY_SOURCE_HXX
+
+#include "event/SocketMonitor.hxx"
+#include "util/FifoBuffer.hxx"
+
+class Error;
+
+typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask,
+ const char *name, void *ctx);
+
+class InotifySource final : private SocketMonitor {
+ mpd_inotify_callback_t callback;
+ void *callback_ctx;
+
+ FifoBuffer<uint8_t, 4096> buffer;
+
+ InotifySource(EventLoop &_loop,
+ mpd_inotify_callback_t callback, void *ctx, int fd);
+
+public:
+ ~InotifySource() {
+ Close();
+ }
+
+ /**
+ * Creates a new inotify source and registers it in the GLib main
+ * loop.
+ *
+ * @param a callback invoked for events received from the kernel
+ */
+ static InotifySource *Create(EventLoop &_loop,
+ mpd_inotify_callback_t callback,
+ void *ctx,
+ Error &error);
+
+ /**
+ * Adds a path to the notify list.
+ *
+ * @return a watch descriptor or -1 on error
+ */
+ int Add(const char *path_fs, unsigned mask, Error &error);
+
+ /**
+ * Removes a path from the notify list.
+ *
+ * @param wd the watch descriptor returned by mpd_inotify_source_add()
+ */
+ void Remove(unsigned wd);
+
+private:
+ virtual bool OnSocketReady(unsigned flags) override;
+};
+
+#endif
diff --git a/src/db/update/InotifyUpdate.cxx b/src/db/update/InotifyUpdate.cxx
new file mode 100644
index 000000000..7515990d7
--- /dev/null
+++ b/src/db/update/InotifyUpdate.cxx
@@ -0,0 +1,339 @@
+/*
+ * 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 "Mapper.hxx"
+#include "Main.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;
+};
+
+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;
+ });
+}
+
+static AllocatedPath
+watch_directory_get_uri_fs(const WatchDirectory *directory)
+{
+ if (directory->parent == nullptr)
+ return AllocatedPath::Null();
+
+ const auto uri = watch_directory_get_uri_fs(directory->parent);
+ if (uri.IsNull())
+ return directory->name;
+
+ return AllocatedPath::Build(uri, directory->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
+static unsigned
+watch_directory_depth(const WatchDirectory *d)
+{
+ assert(d != nullptr);
+
+ 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 = watch_directory_get_uri_fs(directory);
+
+ 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 = mapper_get_music_directory_fs();
+
+ const auto path_fs = uri_fs.IsNull()
+ ? root
+ : AllocatedPath::Build(root, uri_fs.c_str());
+
+ recursive_watch_subdirectories(directory, path_fs,
+ watch_directory_depth(directory));
+ }
+
+ if ((mask & (IN_CLOSE_WRITE|IN_MOVE|IN_DELETE)) != 0 ||
+ /* at the maximum depth, we watch out for newly created
+ directories */
+ (watch_directory_depth(directory) == inotify_max_depth &&
+ (mask & (IN_CREATE|IN_ISDIR)) == (IN_CREATE|IN_ISDIR))) {
+ /* a file was changed, or a directory was
+ moved/deleted: queue a database update */
+
+ 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(unsigned max_depth)
+{
+ LogDebug(inotify_domain, "initializing inotify");
+
+ const auto &path = mapper_get_music_directory_fs();
+ if (path.IsNull()) {
+ LogDebug(inotify_domain, "no music directory configured");
+ return;
+ }
+
+ Error error;
+ inotify_source = InotifySource::Create(*main_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(*main_loop);
+
+ 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..2d7d4e3b4
--- /dev/null
+++ b/src/db/update/InotifyUpdate.hxx
@@ -0,0 +1,47 @@
+/*
+ * 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"
+
+#ifdef HAVE_INOTIFY_INIT
+
+void
+mpd_inotify_init(unsigned max_depth);
+
+void
+mpd_inotify_finish(void);
+
+#else /* !HAVE_INOTIFY_INIT */
+
+static inline void
+mpd_inotify_init(gcc_unused unsigned max_depth)
+{
+}
+
+static inline void
+mpd_inotify_finish(void)
+{
+}
+
+#endif /* !HAVE_INOTIFY_INIT */
+
+#endif
diff --git a/src/db/update/UpdateArchive.cxx b/src/db/update/UpdateArchive.cxx
new file mode 100644
index 000000000..5e733202d
--- /dev/null
+++ b/src/db/update/UpdateArchive.cxx
@@ -0,0 +1,169 @@
+/*
+ * 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 "UpdateArchive.hxx"
+#include "UpdateInternal.hxx"
+#include "UpdateDomain.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/Directory.hxx"
+#include "db/Song.hxx"
+#include "Mapper.hxx"
+#include "fs/AllocatedPath.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 <string.h>
+
+static void
+update_archive_tree(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
+ update_archive_tree(*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(name, directory);
+ if (song != nullptr) {
+ db_lock();
+ directory.AddSong(song);
+ db_unlock();
+
+ modified = true;
+ FormatDefault(update_domain, "added %s/%s",
+ directory.GetPath(), name);
+ }
+ }
+ }
+}
+
+/**
+ * Updates the file listing from an archive file.
+ *
+ * @param parent the parent directory the archive file resides in
+ * @param name the UTF-8 encoded base name of the archive file
+ * @param st stat() information on the archive file
+ * @param plugin the archive plugin which fits this archive type
+ */
+static void
+update_archive_file2(Directory &parent, const char *name,
+ const struct stat *st,
+ const struct archive_plugin *plugin)
+{
+ db_lock();
+ Directory *directory = parent.FindChild(name);
+ db_unlock();
+
+ if (directory != nullptr && directory->mtime == st->st_mtime &&
+ !walk_discard)
+ /* MPD has already scanned the archive, and it hasn't
+ changed since - don't consider updating it */
+ return;
+
+ const auto path_fs = map_directory_child_fs(parent, name);
+
+ /* open archive */
+ Error error;
+ ArchiveFile *file = archive_file_open(plugin, path_fs.c_str(), error);
+ if (file == nullptr) {
+ LogError(error);
+ 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 = st->st_mtime;
+
+ class UpdateArchiveVisitor final : public ArchiveVisitor {
+ Directory *directory;
+
+ public:
+ UpdateArchiveVisitor(Directory *_directory)
+ :directory(_directory) {}
+
+ virtual void VisitArchiveEntry(const char *path_utf8) override {
+ FormatDebug(update_domain,
+ "adding archive file: %s", path_utf8);
+ update_archive_tree(*directory, path_utf8);
+ }
+ };
+
+ UpdateArchiveVisitor visitor(directory);
+ file->Visit(visitor);
+ file->Close();
+}
+
+bool
+update_archive_file(Directory &directory,
+ const char *name, const char *suffix,
+ const struct stat *st)
+{
+#ifdef ENABLE_ARCHIVE
+ const struct archive_plugin *plugin =
+ archive_plugin_from_suffix(suffix);
+ if (plugin == nullptr)
+ return false;
+
+ update_archive_file2(directory, name, st, plugin);
+ return true;
+#else
+ (void)directory;
+ (void)name;
+ (void)suffix;
+ (void)st;
+
+ return false;
+#endif
+}
diff --git a/src/db/update/UpdateArchive.hxx b/src/db/update/UpdateArchive.hxx
new file mode 100644
index 000000000..1fc9af349
--- /dev/null
+++ b/src/db/update/UpdateArchive.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_UPDATE_ARCHIVE_HXX
+#define MPD_UPDATE_ARCHIVE_HXX
+
+#include "check.h"
+#include "Compiler.h"
+
+#include <sys/stat.h>
+
+struct Directory;
+
+#ifdef ENABLE_ARCHIVE
+
+bool
+update_archive_file(Directory &directory,
+ const char *name, const char *suffix,
+ const struct stat *st);
+
+#else
+
+static inline bool
+update_archive_file(gcc_unused Directory &directory,
+ gcc_unused const char *name,
+ gcc_unused const char *suffix,
+ gcc_unused const struct stat *st)
+{
+ return false;
+}
+
+#endif
+
+#endif
diff --git a/src/db/update/UpdateContainer.cxx b/src/db/update/UpdateContainer.cxx
new file mode 100644
index 000000000..c03d88748
--- /dev/null
+++ b/src/db/update/UpdateContainer.cxx
@@ -0,0 +1,136 @@
+/*
+ * 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 "UpdateContainer.hxx"
+#include "UpdateInternal.hxx"
+#include "UpdateDatabase.hxx"
+#include "UpdateDomain.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/Directory.hxx"
+#include "db/Song.hxx"
+#include "decoder/DecoderPlugin.hxx"
+#include "decoder/DecoderList.hxx"
+#include "Mapper.hxx"
+#include "fs/AllocatedPath.hxx"
+#include "tag/TagHandler.hxx"
+#include "tag/TagBuilder.hxx"
+#include "Log.hxx"
+
+#include <glib.h>
+
+/**
+ * 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.
+ */
+static Directory *
+make_directory_if_modified(Directory &parent, const char *name,
+ const struct stat *st)
+{
+ Directory *directory = parent.FindChild(name);
+
+ // directory exists already
+ if (directory != nullptr) {
+ if (directory->mtime == st->st_mtime && !walk_discard) {
+ /* not modified */
+ return nullptr;
+ }
+
+ delete_directory(directory);
+ modified = true;
+ }
+
+ directory = parent.MakeChild(name);
+ directory->mtime = st->st_mtime;
+ return directory;
+}
+
+static bool
+SupportsContainerSuffix(const DecoderPlugin &plugin, const char *suffix)
+{
+ return plugin.container_scan != nullptr &&
+ plugin.SupportsSuffix(suffix);
+}
+
+bool
+update_container_file(Directory &directory,
+ const char *name,
+ const struct stat *st,
+ const char *suffix)
+{
+ 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 = make_directory_if_modified(directory, name, st);
+ if (contdir == nullptr) {
+ /* not modified */
+ db_unlock();
+ return true;
+ }
+
+ contdir->device = DEVICE_CONTAINER;
+ db_unlock();
+
+ const auto pathname = map_directory_child_fs(directory, name);
+
+ char *vtrack;
+ unsigned int tnum = 0;
+ TagBuilder tag_builder;
+ while ((vtrack = plugin.container_scan(pathname.c_str(), ++tnum)) != nullptr) {
+ Song *song = Song::NewFile(vtrack, *contdir);
+
+ // shouldn't be necessary but it's there..
+ song->mtime = st->st_mtime;
+
+ const auto child_path_fs =
+ map_directory_child_fs(*contdir, vtrack);
+
+ plugin.ScanFile(child_path_fs.c_str(),
+ 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);
+ g_free(vtrack);
+ }
+
+ if (tnum == 1) {
+ db_lock();
+ delete_directory(contdir);
+ db_unlock();
+ return false;
+ } else
+ return true;
+}
diff --git a/src/db/update/UpdateContainer.hxx b/src/db/update/UpdateContainer.hxx
new file mode 100644
index 000000000..8125f71ee
--- /dev/null
+++ b/src/db/update/UpdateContainer.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_UPDATE_CONTAINER_HXX
+#define MPD_UPDATE_CONTAINER_HXX
+
+#include "check.h"
+
+#include <sys/stat.h>
+
+struct Directory;
+struct DecoderPlugin;
+
+bool
+update_container_file(Directory &directory,
+ const char *name,
+ const struct stat *st,
+ const char *suffix);
+
+#endif
diff --git a/src/db/update/UpdateDatabase.cxx b/src/db/update/UpdateDatabase.cxx
new file mode 100644
index 000000000..8ef0b6d82
--- /dev/null
+++ b/src/db/update/UpdateDatabase.cxx
@@ -0,0 +1,104 @@
+/*
+ * 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 "UpdateDatabase.hxx"
+#include "UpdateRemove.hxx"
+#include "PlaylistVector.hxx"
+#include "db/Directory.hxx"
+#include "db/Song.hxx"
+#include "db/DatabaseLock.hxx"
+
+#include <assert.h>
+#include <stddef.h>
+
+void
+delete_song(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) */
+ update_remove_song(del);
+
+ /* finally, all possible references gone, free it */
+ del->Free();
+
+ db_lock();
+}
+
+/**
+ * Recursively remove all sub directories and songs from a directory,
+ * leaving an empty directory.
+ *
+ * Caller must lock the #db_mutex.
+ */
+static void
+clear_directory(Directory &directory)
+{
+ Directory *child, *n;
+ directory_for_each_child_safe(child, n, directory)
+ delete_directory(child);
+
+ Song *song, *ns;
+ directory_for_each_song_safe(song, ns, directory) {
+ assert(song->parent == &directory);
+ delete_song(directory, song);
+ }
+}
+
+void
+delete_directory(Directory *directory)
+{
+ assert(directory->parent != nullptr);
+
+ clear_directory(*directory);
+
+ directory->Delete();
+}
+
+bool
+delete_name_in(Directory &parent, const char *name)
+{
+ bool modified = false;
+
+ db_lock();
+ Directory *directory = parent.FindChild(name);
+
+ if (directory != nullptr) {
+ delete_directory(directory);
+ modified = true;
+ }
+
+ Song *song = parent.FindSong(name);
+ if (song != nullptr) {
+ delete_song(parent, song);
+ modified = true;
+ }
+
+ parent.playlists.erase(name);
+
+ db_unlock();
+
+ return modified;
+}
diff --git a/src/db/update/UpdateDatabase.hxx b/src/db/update/UpdateDatabase.hxx
new file mode 100644
index 000000000..bd7c395f2
--- /dev/null
+++ b/src/db/update/UpdateDatabase.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_UPDATE_DATABASE_HXX
+#define MPD_UPDATE_DATABASE_HXX
+
+#include "check.h"
+
+struct Directory;
+struct Song;
+
+/**
+ * Caller must lock the #db_mutex.
+ */
+void
+delete_song(Directory &parent, Song *song);
+
+/**
+ * Recursively free a directory and all its contents.
+ *
+ * Caller must lock the #db_mutex.
+ */
+void
+delete_directory(Directory *directory);
+
+/**
+ * Caller must NOT lock the #db_mutex.
+ *
+ * @return true if the database was modified
+ */
+bool
+delete_name_in(Directory &parent, const char *name);
+
+#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/UpdateGlue.cxx b/src/db/update/UpdateGlue.cxx
new file mode 100644
index 000000000..d18747ba1
--- /dev/null
+++ b/src/db/update/UpdateGlue.cxx
@@ -0,0 +1,181 @@
+/*
+ * 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 "UpdateGlue.hxx"
+#include "UpdateQueue.hxx"
+#include "UpdateWalk.hxx"
+#include "UpdateRemove.hxx"
+#include "UpdateDomain.hxx"
+#include "Mapper.hxx"
+#include "db/DatabaseSimple.hxx"
+#include "Idle.hxx"
+#include "GlobalEvents.hxx"
+#include "util/Error.hxx"
+#include "Log.hxx"
+#include "Main.hxx"
+#include "Instance.hxx"
+#include "system/FatalError.hxx"
+#include "thread/Id.hxx"
+#include "thread/Thread.hxx"
+#include "thread/Util.hxx"
+
+#include <assert.h>
+
+static enum update_progress {
+ UPDATE_PROGRESS_IDLE = 0,
+ UPDATE_PROGRESS_RUNNING = 1,
+ UPDATE_PROGRESS_DONE = 2
+} progress;
+
+static bool modified;
+
+static Thread update_thread;
+
+static const unsigned update_task_id_max = 1 << 15;
+
+static unsigned update_task_id;
+
+static UpdateQueueItem next;
+
+unsigned
+isUpdatingDB(void)
+{
+ return next.id;
+}
+
+static void
+update_task(gcc_unused void *ctx)
+{
+ if (!next.path_utf8.empty())
+ FormatDebug(update_domain, "starting: %s",
+ next.path_utf8.c_str());
+ else
+ LogDebug(update_domain, "starting");
+
+ SetThreadIdlePriority();
+
+ modified = update_walk(next.path_utf8.c_str(), next.discard);
+
+ if (modified || !db_exists()) {
+ Error error;
+ if (!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;
+ GlobalEvents::Emit(GlobalEvents::UPDATE);
+}
+
+static void
+spawn_update_task(UpdateQueueItem &&i)
+{
+ assert(main_thread.IsInside());
+
+ progress = UPDATE_PROGRESS_RUNNING;
+ modified = false;
+
+ next = std::move(i);
+
+ Error error;
+ if (!update_thread.Start(update_task, nullptr, error))
+ FatalError(error);
+
+ FormatDebug(update_domain,
+ "spawned thread for update job id %i", next.id);
+}
+
+static unsigned
+generate_update_id()
+{
+ unsigned id = update_task_id + 1;
+ if (id > update_task_id_max)
+ id = 1;
+ return id;
+}
+
+unsigned
+update_enqueue(const char *path, bool discard)
+{
+ assert(main_thread.IsInside());
+
+ if (!db_is_simple() || !mapper_has_music_directory())
+ return 0;
+
+ if (progress != UPDATE_PROGRESS_IDLE) {
+ const unsigned id = generate_update_id();
+ if (!update_queue_push(path, discard, id))
+ return 0;
+
+ update_task_id = id;
+ return id;
+ }
+
+ const unsigned id = update_task_id = generate_update_id();
+ spawn_update_task(UpdateQueueItem(path, discard, id));
+
+ idle_add(IDLE_UPDATE);
+
+ return id;
+}
+
+/**
+ * Called in the main thread after the database update is finished.
+ */
+static void update_finished_event(void)
+{
+ assert(progress == UPDATE_PROGRESS_DONE);
+ assert(next.IsDefined());
+
+ update_thread.Join();
+ next = UpdateQueueItem();
+
+ idle_add(IDLE_UPDATE);
+
+ if (modified)
+ /* send "idle" events */
+ instance->DatabaseModified();
+
+ auto i = update_queue_shift();
+ if (i.IsDefined()) {
+ /* schedule the next path */
+ spawn_update_task(std::move(i));
+ } else {
+ progress = UPDATE_PROGRESS_IDLE;
+ }
+}
+
+void update_global_init(void)
+{
+ GlobalEvents::Register(GlobalEvents::UPDATE, update_finished_event);
+
+ update_remove_global_init();
+ update_walk_global_init();
+}
+
+void update_global_finish(void)
+{
+ update_walk_global_finish();
+}
diff --git a/src/db/update/UpdateGlue.hxx b/src/db/update/UpdateGlue.hxx
new file mode 100644
index 000000000..6e247414e
--- /dev/null
+++ b/src/db/update/UpdateGlue.hxx
@@ -0,0 +1,43 @@
+/*
+ * 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_GLUE_HXX
+#define MPD_UPDATE_GLUE_HXX
+
+#include "Compiler.h"
+
+void update_global_init(void);
+
+void update_global_finish(void);
+
+unsigned
+isUpdatingDB(void);
+
+/**
+ * Add this path to the database update queue.
+ *
+ * @param path a path to update; if an empty string,
+ * the whole music directory is updated
+ * @return the job id, or 0 on error
+ */
+gcc_nonnull_all
+unsigned
+update_enqueue(const char *path, bool discard);
+
+#endif
diff --git a/src/db/update/UpdateIO.cxx b/src/db/update/UpdateIO.cxx
new file mode 100644
index 000000000..f91caf359
--- /dev/null
+++ b/src/db/update/UpdateIO.cxx
@@ -0,0 +1,113 @@
+/*
+ * 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/Directory.hxx"
+#include "Mapper.hxx"
+#include "fs/AllocatedPath.hxx"
+#include "fs/FileSystem.hxx"
+#include "Log.hxx"
+
+#include <errno.h>
+#include <unistd.h>
+
+int
+stat_directory(const Directory &directory, struct stat *st)
+{
+ const auto path_fs = map_directory_fs(directory);
+ if (path_fs.IsNull())
+ return -1;
+
+ if (!StatFile(path_fs, *st)) {
+ int error = errno;
+ const std::string path_utf8 = path_fs.ToUTF8();
+ FormatErrno(update_domain, error,
+ "Failed to stat %s", path_utf8.c_str());
+ return -1;
+ }
+
+ return 0;
+}
+
+int
+stat_directory_child(const Directory &parent, const char *name,
+ struct stat *st)
+{
+ const auto path_fs = map_directory_child_fs(parent, name);
+ if (path_fs.IsNull())
+ return -1;
+
+ if (!StatFile(path_fs, *st)) {
+ int error = errno;
+ const std::string path_utf8 = path_fs.ToUTF8();
+ FormatErrno(update_domain, error,
+ "Failed to stat %s", path_utf8.c_str());
+ return -1;
+ }
+
+ return 0;
+}
+
+bool
+directory_exists(const Directory &directory)
+{
+ const auto path_fs = map_directory_fs(directory);
+ if (path_fs.IsNull())
+ /* invalid path: cannot exist */
+ return false;
+
+ return directory.device == DEVICE_INARCHIVE ||
+ directory.device == DEVICE_CONTAINER
+ ? FileExists(path_fs)
+ : DirectoryExists(path_fs);
+}
+
+bool
+directory_child_is_regular(const Directory &directory,
+ const char *name_utf8)
+{
+ const auto path_fs = map_directory_child_fs(directory, name_utf8);
+ if (path_fs.IsNull())
+ return false;
+
+ return FileExists(path_fs);
+}
+
+bool
+directory_child_access(const Directory &directory,
+ const char *name, int mode)
+{
+#ifdef WIN32
+ /* CheckAccess() is useless on WIN32 */
+ (void)directory;
+ (void)name;
+ (void)mode;
+ return true;
+#else
+ const auto path = map_directory_child_fs(directory, name);
+ if (path.IsNull())
+ /* something went wrong, but that isn't a permission
+ problem */
+ 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..819879422
--- /dev/null
+++ b/src/db/update/UpdateIO.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_UPDATE_IO_HXX
+#define MPD_UPDATE_IO_HXX
+
+#include "check.h"
+
+#include <sys/stat.h>
+
+struct Directory;
+
+int
+stat_directory(const Directory &directory, struct stat *st);
+
+int
+stat_directory_child(const Directory &parent, const char *name,
+ struct stat *st);
+
+bool
+directory_exists(const Directory &directory);
+
+bool
+directory_child_is_regular(const Directory &directory,
+ const char *name_utf8);
+
+/**
+ * Checks if the given permissions on the mapped file are given.
+ */
+bool
+directory_child_access(const Directory &directory,
+ const char *name, int mode);
+
+#endif
diff --git a/src/db/update/UpdateInternal.hxx b/src/db/update/UpdateInternal.hxx
new file mode 100644
index 000000000..2e373bd06
--- /dev/null
+++ b/src/db/update/UpdateInternal.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_UPDATE_INTERNAL_H
+#define MPD_UPDATE_INTERNAL_H
+
+#include "check.h"
+
+extern bool walk_discard;
+extern bool modified;
+
+#endif
diff --git a/src/db/update/UpdateQueue.cxx b/src/db/update/UpdateQueue.cxx
new file mode 100644
index 000000000..a6002f854
--- /dev/null
+++ b/src/db/update/UpdateQueue.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 "config.h"
+#include "UpdateQueue.hxx"
+
+#include <queue>
+#include <list>
+
+static constexpr unsigned MAX_UPDATE_QUEUE_SIZE = 32;
+
+static std::queue<UpdateQueueItem, std::list<UpdateQueueItem>> update_queue;
+
+bool
+update_queue_push(const char *path, bool discard, unsigned id)
+{
+ if (update_queue.size() >= MAX_UPDATE_QUEUE_SIZE)
+ return false;
+
+ update_queue.emplace(path, discard, id);
+ return true;
+}
+
+UpdateQueueItem
+update_queue_shift()
+{
+ if (update_queue.empty())
+ return UpdateQueueItem();
+
+ auto i = std::move(update_queue.front());
+ update_queue.pop();
+ return i;
+}
diff --git a/src/db/update/UpdateQueue.hxx b/src/db/update/UpdateQueue.hxx
new file mode 100644
index 000000000..e4228f5ed
--- /dev/null
+++ b/src/db/update/UpdateQueue.hxx
@@ -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.
+ */
+
+#ifndef MPD_UPDATE_QUEUE_HXX
+#define MPD_UPDATE_QUEUE_HXX
+
+#include "check.h"
+
+#include <string>
+
+struct UpdateQueueItem {
+ std::string path_utf8;
+ unsigned id;
+ bool discard;
+
+ UpdateQueueItem():id(0) {}
+ UpdateQueueItem(const char *_path, bool _discard,
+ unsigned _id)
+ :path_utf8(_path), id(_id), discard(_discard) {}
+
+ bool IsDefined() const {
+ return id != 0;
+ }
+};
+
+bool
+update_queue_push(const char *path, bool discard, unsigned id);
+
+UpdateQueueItem
+update_queue_shift();
+
+#endif
diff --git a/src/db/update/UpdateRemove.cxx b/src/db/update/UpdateRemove.cxx
new file mode 100644
index 000000000..c57758aef
--- /dev/null
+++ b/src/db/update/UpdateRemove.cxx
@@ -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.
+ */
+
+#include "config.h" /* must be first for large file support */
+#include "UpdateRemove.hxx"
+#include "UpdateDomain.hxx"
+#include "GlobalEvents.hxx"
+#include "thread/Mutex.hxx"
+#include "thread/Cond.hxx"
+#include "db/Song.hxx"
+#include "db/LightSong.hxx"
+#include "Main.hxx"
+#include "Instance.hxx"
+#include "Log.hxx"
+
+#ifdef ENABLE_SQLITE
+#include "sticker/StickerDatabase.hxx"
+#include "sticker/SongSticker.hxx"
+#endif
+
+#include <assert.h>
+
+static const Song *removed_song;
+
+static Mutex remove_mutex;
+static Cond remove_cond;
+
+/**
+ * Safely remove a song from the database. This must be done in the
+ * main task, to be sure that there is no pointer left to it.
+ */
+static void
+song_remove_event(void)
+{
+ assert(removed_song != nullptr);
+
+ {
+ const auto uri = removed_song->GetURI();
+ FormatDefault(update_domain, "removing %s", uri.c_str());
+ }
+
+#ifdef ENABLE_SQLITE
+ /* if the song has a sticker, remove it */
+ if (sticker_enabled())
+ sticker_song_delete(removed_song->Export());
+#endif
+
+ {
+ const auto uri = removed_song->GetURI();
+ instance->DeleteSong(uri.c_str());
+ }
+
+ /* clear "removed_song" and send signal to update thread */
+ remove_mutex.lock();
+ removed_song = nullptr;
+ remove_cond.signal();
+ remove_mutex.unlock();
+}
+
+void
+update_remove_global_init(void)
+{
+ GlobalEvents::Register(GlobalEvents::DELETE, song_remove_event);
+}
+
+void
+update_remove_song(const Song *song)
+{
+ assert(removed_song == nullptr);
+
+ removed_song = song;
+
+ GlobalEvents::Emit(GlobalEvents::DELETE);
+
+ remove_mutex.lock();
+
+ while (removed_song != nullptr)
+ remove_cond.wait(remove_mutex);
+
+ remove_mutex.unlock();
+}
diff --git a/src/db/update/UpdateRemove.hxx b/src/db/update/UpdateRemove.hxx
new file mode 100644
index 000000000..d54e3aa80
--- /dev/null
+++ b/src/db/update/UpdateRemove.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_UPDATE_REMOVE_HXX
+#define MPD_UPDATE_REMOVE_HXX
+
+#include "check.h"
+
+struct Song;
+
+void
+update_remove_global_init(void);
+
+/**
+ * Sends a signal to the main thread which will in turn remove the
+ * song: from the sticker database and from the playlist. This
+ * serialized access is implemented to avoid excessive locking.
+ */
+void
+update_remove_song(const Song *song);
+
+#endif
diff --git a/src/db/update/UpdateSong.cxx b/src/db/update/UpdateSong.cxx
new file mode 100644
index 000000000..ac2d01cd2
--- /dev/null
+++ b/src/db/update/UpdateSong.cxx
@@ -0,0 +1,113 @@
+/*
+ * 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 "UpdateSong.hxx"
+#include "UpdateInternal.hxx"
+#include "UpdateIO.hxx"
+#include "UpdateDatabase.hxx"
+#include "UpdateContainer.hxx"
+#include "UpdateDomain.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/Directory.hxx"
+#include "db/Song.hxx"
+#include "decoder/DecoderList.hxx"
+#include "Log.hxx"
+
+#include <unistd.h>
+
+static void
+update_song_file2(Directory &directory,
+ const char *name, const struct stat *st,
+ const char *suffix)
+{
+ db_lock();
+ Song *song = directory.FindSong(name);
+ db_unlock();
+
+ if (!directory_child_access(directory, name, R_OK)) {
+ FormatError(update_domain,
+ "no read permissions on %s/%s",
+ directory.GetPath(), name);
+ if (song != nullptr) {
+ db_lock();
+ delete_song(directory, song);
+ db_unlock();
+ }
+
+ return;
+ }
+
+ if (!(song != nullptr && st->st_mtime == song->mtime &&
+ !walk_discard) &&
+ update_container_file(directory, name, st, suffix)) {
+ if (song != nullptr) {
+ db_lock();
+ delete_song(directory, song);
+ db_unlock();
+ }
+
+ return;
+ }
+
+ if (song == nullptr) {
+ FormatDebug(update_domain, "reading %s/%s",
+ directory.GetPath(), name);
+ song = Song::LoadFile(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 (st->st_mtime != song->mtime || walk_discard) {
+ FormatDefault(update_domain, "updating %s/%s",
+ directory.GetPath(), name);
+ if (!song->UpdateFile()) {
+ FormatDebug(update_domain,
+ "deleting unrecognized file %s/%s",
+ directory.GetPath(), name);
+ db_lock();
+ delete_song(directory, song);
+ db_unlock();
+ }
+
+ modified = true;
+ }
+}
+
+bool
+update_song_file(Directory &directory,
+ const char *name, const char *suffix,
+ const struct stat *st)
+{
+ if (!decoder_plugins_supports_suffix(suffix))
+ return false;
+
+ update_song_file2(directory, name, st, suffix);
+ return true;
+}
diff --git a/src/db/update/UpdateSong.hxx b/src/db/update/UpdateSong.hxx
new file mode 100644
index 000000000..5feb01928
--- /dev/null
+++ b/src/db/update/UpdateSong.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_UPDATE_SONG_HXX
+#define MPD_UPDATE_SONG_HXX
+
+#include "check.h"
+
+#include <sys/stat.h>
+
+struct Directory;
+
+bool
+update_song_file(Directory &directory,
+ const char *name, const char *suffix,
+ const struct stat *st);
+
+#endif
diff --git a/src/db/update/UpdateWalk.cxx b/src/db/update/UpdateWalk.cxx
new file mode 100644
index 000000000..c5a9936e9
--- /dev/null
+++ b/src/db/update/UpdateWalk.cxx
@@ -0,0 +1,484 @@
+/*
+ * 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 "UpdateWalk.hxx"
+#include "UpdateIO.hxx"
+#include "UpdateDatabase.hxx"
+#include "UpdateSong.hxx"
+#include "UpdateArchive.hxx"
+#include "UpdateDomain.hxx"
+#include "db/DatabaseLock.hxx"
+#include "db/DatabaseSimple.hxx"
+#include "db/Directory.hxx"
+#include "db/Song.hxx"
+#include "PlaylistVector.hxx"
+#include "playlist/PlaylistRegistry.hxx"
+#include "Mapper.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/DirectoryReader.hxx"
+#include "util/Alloc.hxx"
+#include "util/UriUtil.hxx"
+#include "Log.hxx"
+
+#include <assert.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
+bool walk_discard;
+bool modified;
+
+#ifndef WIN32
+
+static constexpr bool DEFAULT_FOLLOW_INSIDE_SYMLINKS = true;
+static constexpr bool DEFAULT_FOLLOW_OUTSIDE_SYMLINKS = true;
+
+static bool follow_inside_symlinks;
+static bool follow_outside_symlinks;
+
+#endif
+
+void
+update_walk_global_init(void)
+{
+#ifndef WIN32
+ follow_inside_symlinks =
+ config_get_bool(CONF_FOLLOW_INSIDE_SYMLINKS,
+ DEFAULT_FOLLOW_INSIDE_SYMLINKS);
+
+ follow_outside_symlinks =
+ config_get_bool(CONF_FOLLOW_OUTSIDE_SYMLINKS,
+ DEFAULT_FOLLOW_OUTSIDE_SYMLINKS);
+#endif
+}
+
+void
+update_walk_global_finish(void)
+{
+}
+
+static void
+directory_set_stat(Directory &dir, const struct stat *st)
+{
+ dir.inode = st->st_ino;
+ dir.device = st->st_dev;
+ dir.have_stat = true;
+}
+
+static void
+remove_excluded_from_directory(Directory &directory,
+ const ExcludeList &exclude_list)
+{
+ db_lock();
+
+ Directory *child, *n;
+ directory_for_each_child_safe(child, n, directory) {
+ const auto name_fs = AllocatedPath::FromUTF8(child->GetName());
+
+ if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
+ delete_directory(child);
+ modified = true;
+ }
+ }
+
+ Song *song, *ns;
+ directory_for_each_song_safe(song, ns, directory) {
+ assert(song->parent == &directory);
+
+ const auto name_fs = AllocatedPath::FromUTF8(song->uri);
+ if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
+ delete_song(directory, song);
+ modified = true;
+ }
+ }
+
+ db_unlock();
+}
+
+static void
+purge_deleted_from_directory(Directory &directory)
+{
+ Directory *child, *n;
+ directory_for_each_child_safe(child, n, directory) {
+ if (directory_exists(*child))
+ continue;
+
+ db_lock();
+ delete_directory(child);
+ db_unlock();
+
+ modified = true;
+ }
+
+ Song *song, *ns;
+ directory_for_each_song_safe(song, ns, directory) {
+ const auto path = map_song_fs(*song);
+ if (path.IsNull() || !FileExists(path)) {
+ db_lock();
+ delete_song(directory, song);
+ db_unlock();
+
+ modified = true;
+ }
+ }
+
+ for (auto i = directory.playlists.begin(),
+ end = directory.playlists.end();
+ i != end;) {
+ if (!directory_child_is_regular(directory, i->name.c_str())) {
+ db_lock();
+ i = directory.playlists.erase(i);
+ db_unlock();
+ } else
+ ++i;
+ }
+}
+
+#ifndef WIN32
+static int
+update_directory_stat(Directory &directory)
+{
+ struct stat st;
+ if (stat_directory(directory, &st) < 0)
+ return -1;
+
+ directory_set_stat(directory, &st);
+ return 0;
+}
+#endif
+
+static int
+find_inode_ancestor(Directory *parent, ino_t inode, dev_t device)
+{
+#ifndef WIN32
+ while (parent) {
+ if (!parent->have_stat && update_directory_stat(*parent) < 0)
+ return -1;
+
+ if (parent->inode == inode && parent->device == device) {
+ LogDebug(update_domain, "recursive directory found");
+ return 1;
+ }
+
+ parent = parent->parent;
+ }
+#else
+ (void)parent;
+ (void)inode;
+ (void)device;
+#endif
+
+ return 0;
+}
+
+static bool
+update_playlist_file2(Directory &directory,
+ const char *name, const char *suffix,
+ const struct stat *st)
+{
+ if (!playlist_suffix_supported(suffix))
+ return false;
+
+ PlaylistInfo pi(name, st->st_mtime);
+
+ db_lock();
+ if (directory.playlists.UpdateOrInsert(std::move(pi)))
+ modified = true;
+ db_unlock();
+ return true;
+}
+
+static bool
+update_regular_file(Directory &directory,
+ const char *name, const struct stat *st)
+{
+ const char *suffix = uri_get_suffix(name);
+ if (suffix == nullptr)
+ return false;
+
+ return update_song_file(directory, name, suffix, st) ||
+ update_archive_file(directory, name, suffix, st) ||
+ update_playlist_file2(directory, name, suffix, st);
+}
+
+static bool
+update_directory(Directory &directory, const struct stat *st);
+
+static void
+update_directory_child(Directory &directory,
+ const char *name, const struct stat *st)
+{
+ assert(strchr(name, '/') == nullptr);
+
+ if (S_ISREG(st->st_mode)) {
+ update_regular_file(directory, name, st);
+ } else if (S_ISDIR(st->st_mode)) {
+ if (find_inode_ancestor(&directory, st->st_ino, st->st_dev))
+ return;
+
+ db_lock();
+ Directory *subdir = directory.MakeChild(name);
+ db_unlock();
+
+ assert(&directory == subdir->parent);
+
+ if (!update_directory(*subdir, st)) {
+ db_lock();
+ delete_directory(subdir);
+ db_unlock();
+ }
+ } 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(Path path_fs)
+{
+ const char *path = path_fs.c_str();
+ return (path[0] == '.' && path[1] == 0) ||
+ (path[0] == '.' && path[1] == '.' && path[2] == 0) ||
+ strchr(path, '\n') != nullptr;
+}
+
+gcc_pure
+static bool
+skip_symlink(const Directory *directory, const char *utf8_name)
+{
+#ifndef WIN32
+ const auto path_fs = map_directory_child_fs(*directory, utf8_name);
+ if (path_fs.IsNull())
+ return true;
+
+ 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 char *relative = map_to_relative_path(target_str);
+ return relative > target_str
+ ? !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
+}
+
+static bool
+update_directory(Directory &directory, const struct stat *st)
+{
+ assert(S_ISDIR(st->st_mode));
+
+ directory_set_stat(directory, st);
+
+ const auto path_fs = map_directory_fs(directory);
+ if (path_fs.IsNull())
+ return false;
+
+ DirectoryReader reader(path_fs);
+ if (reader.HasFailed()) {
+ int error = errno;
+ const auto path_utf8 = path_fs.ToUTF8();
+ FormatErrno(update_domain, error,
+ "Failed to open directory %s",
+ path_utf8.c_str());
+ return false;
+ }
+
+ ExcludeList exclude_list;
+ exclude_list.LoadFile(AllocatedPath::Build(path_fs, ".mpdignore"));
+
+ if (!exclude_list.IsEmpty())
+ remove_excluded_from_directory(directory, exclude_list);
+
+ purge_deleted_from_directory(directory);
+
+ while (reader.ReadEntry()) {
+ std::string utf8;
+ struct stat st2;
+
+ const auto entry = reader.GetEntry();
+
+ if (skip_path(entry) || exclude_list.Check(entry))
+ continue;
+
+ utf8 = entry.ToUTF8();
+ if (utf8.empty())
+ continue;
+
+ if (skip_symlink(&directory, utf8.c_str())) {
+ modified |= delete_name_in(directory, utf8.c_str());
+ continue;
+ }
+
+ if (stat_directory_child(directory, utf8.c_str(), &st2) == 0)
+ update_directory_child(directory, utf8.c_str(), &st2);
+ else
+ modified |= delete_name_in(directory, utf8.c_str());
+ }
+
+ directory.mtime = st->st_mtime;
+
+ return true;
+}
+
+static Directory *
+directory_make_child_checked(Directory &parent, const char *name_utf8)
+{
+ db_lock();
+ Directory *directory = parent.FindChild(name_utf8);
+ db_unlock();
+
+ if (directory != nullptr)
+ return directory;
+
+ struct stat st;
+ if (stat_directory_child(parent, name_utf8, &st) < 0 ||
+ find_inode_ancestor(&parent, st.st_ino, st.st_dev))
+ return nullptr;
+
+ if (skip_symlink(&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)
+ delete_song(parent, conflicting);
+
+ directory = parent.CreateChild(name_utf8);
+ db_unlock();
+
+ directory_set_stat(*directory, &st);
+ return directory;
+}
+
+static Directory *
+directory_make_uri_parent_checked(const char *uri)
+{
+ Directory *directory = db_get_root();
+ char *duplicated = xstrdup(uri);
+ char *name_utf8 = duplicated, *slash;
+
+ while ((slash = strchr(name_utf8, '/')) != nullptr) {
+ *slash = 0;
+
+ if (*name_utf8 == 0)
+ continue;
+
+ directory = directory_make_child_checked(*directory,
+ name_utf8);
+ if (directory == nullptr)
+ break;
+
+ name_utf8 = slash + 1;
+ }
+
+ free(duplicated);
+ return directory;
+}
+
+static void
+update_uri(const char *uri)
+{
+ Directory *parent = directory_make_uri_parent_checked(uri);
+ if (parent == nullptr)
+ return;
+
+ const char *name = PathTraitsUTF8::GetBase(uri);
+
+ struct stat st;
+ if (!skip_symlink(parent, name) &&
+ stat_directory_child(*parent, name, &st) == 0)
+ update_directory_child(*parent, name, &st);
+ else
+ modified |= delete_name_in(*parent, name);
+}
+
+bool
+update_walk(const char *path, bool discard)
+{
+ walk_discard = discard;
+ modified = false;
+
+ if (path != nullptr && !isRootDirectory(path)) {
+ update_uri(path);
+ } else {
+ Directory *directory = db_get_root();
+ struct stat st;
+
+ if (stat_directory(*directory, &st) == 0)
+ update_directory(*directory, &st);
+ }
+
+ return modified;
+}
diff --git a/src/db/update/UpdateWalk.hxx b/src/db/update/UpdateWalk.hxx
new file mode 100644
index 000000000..e908829e3
--- /dev/null
+++ b/src/db/update/UpdateWalk.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_UPDATE_WALK_HXX
+#define MPD_UPDATE_WALK_HXX
+
+#include "check.h"
+
+void
+update_walk_global_init(void);
+
+void
+update_walk_global_finish(void);
+
+/**
+ * Returns true if the database was modified.
+ */
+bool
+update_walk(const char *path, bool discard);
+
+#endif