2012-06-13 21:28:26 +02:00
|
|
|
/*
|
2019-06-17 11:17:30 +02:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2012-06-13 21:28:26 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
#include "Service.hxx"
|
2014-02-27 18:04:24 +01:00
|
|
|
#include "Walk.hxx"
|
2014-02-27 16:14:10 +01:00
|
|
|
#include "UpdateDomain.hxx"
|
|
|
|
#include "db/DatabaseListener.hxx"
|
2014-02-26 08:39:44 +01:00
|
|
|
#include "db/DatabaseLock.hxx"
|
2014-02-27 16:14:10 +01:00
|
|
|
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
|
2014-02-26 08:39:44 +01:00
|
|
|
#include "db/plugins/simple/Directory.hxx"
|
|
|
|
#include "storage/CompositeStorage.hxx"
|
2018-08-19 23:15:52 +02:00
|
|
|
#include "protocol/Ack.hxx"
|
2014-02-27 16:14:10 +01:00
|
|
|
#include "Idle.hxx"
|
|
|
|
#include "Log.hxx"
|
|
|
|
#include "thread/Thread.hxx"
|
2018-02-09 18:48:14 +01:00
|
|
|
#include "thread/Name.hxx"
|
2014-02-27 16:14:10 +01:00
|
|
|
#include "thread/Util.hxx"
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "event/Loop.hxx"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2018-07-17 22:41:28 +02:00
|
|
|
UpdateService::UpdateService(const ConfigData &_config,
|
|
|
|
EventLoop &_loop, SimpleDatabase &_db,
|
2014-02-26 08:39:44 +01:00
|
|
|
CompositeStorage &_storage,
|
2019-09-01 13:51:34 +02:00
|
|
|
DatabaseListener &_listener) noexcept
|
2018-07-17 22:41:28 +02:00
|
|
|
:config(_config),
|
|
|
|
defer(_loop, BIND_THIS_METHOD(RunDeferred)),
|
2014-02-27 18:04:24 +01:00
|
|
|
db(_db), storage(_storage),
|
|
|
|
listener(_listener),
|
2017-09-09 07:59:51 +02:00
|
|
|
update_thread(BIND_THIS_METHOD(Task))
|
2014-02-27 16:14:10 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-01 13:51:34 +02:00
|
|
|
UpdateService::~UpdateService() noexcept
|
2014-02-27 16:36:11 +01:00
|
|
|
{
|
|
|
|
CancelAllAsync();
|
|
|
|
|
|
|
|
if (update_thread.IsDefined())
|
|
|
|
update_thread.Join();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-01 13:51:34 +02:00
|
|
|
UpdateService::CancelAllAsync() noexcept
|
2014-02-27 16:36:11 +01:00
|
|
|
{
|
2017-08-18 17:57:59 +02:00
|
|
|
assert(GetEventLoop().IsInside());
|
2014-02-27 16:36:11 +01:00
|
|
|
|
|
|
|
queue.Clear();
|
2014-02-27 18:04:24 +01:00
|
|
|
|
|
|
|
if (walk != nullptr)
|
|
|
|
walk->Cancel();
|
2014-02-27 16:36:11 +01:00
|
|
|
}
|
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
void
|
2019-09-01 13:51:34 +02:00
|
|
|
UpdateService::CancelMount(const char *uri) noexcept
|
2014-02-26 08:39:44 +01:00
|
|
|
{
|
|
|
|
/* determine which (mounted) database will be updated and what
|
|
|
|
storage will be scanned */
|
|
|
|
|
2015-12-15 23:27:05 +01:00
|
|
|
Directory::LookupResult lr;
|
|
|
|
{
|
|
|
|
const ScopeDatabaseLock protect;
|
|
|
|
lr = db.GetRoot().LookupDirectory(uri);
|
|
|
|
}
|
2014-02-26 08:39:44 +01:00
|
|
|
|
|
|
|
if (!lr.directory->IsMount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool cancel_current = false;
|
|
|
|
|
|
|
|
Storage *storage2 = storage.GetMount(uri);
|
|
|
|
if (storage2 != nullptr) {
|
|
|
|
queue.Erase(*storage2);
|
|
|
|
cancel_current = next.IsDefined() && next.storage == storage2;
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:32:11 +01:00
|
|
|
if (auto *db2 = dynamic_cast<SimpleDatabase *>(lr.directory->mounted_database.get())) {
|
2018-11-19 19:19:20 +01:00
|
|
|
queue.Erase(*db2);
|
|
|
|
cancel_current |= next.IsDefined() && next.db == db2;
|
2014-02-26 08:39:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cancel_current && walk != nullptr) {
|
|
|
|
walk->Cancel();
|
|
|
|
|
|
|
|
if (update_thread.IsDefined())
|
|
|
|
update_thread.Join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-27 16:14:10 +01:00
|
|
|
inline void
|
2019-08-02 14:44:00 +02:00
|
|
|
UpdateService::Task() noexcept
|
2014-02-27 16:14:10 +01:00
|
|
|
{
|
2014-02-27 18:04:24 +01:00
|
|
|
assert(walk != nullptr);
|
|
|
|
|
2018-02-09 18:48:14 +01:00
|
|
|
SetThreadName("update");
|
|
|
|
|
2014-02-27 16:14:10 +01:00
|
|
|
if (!next.path_utf8.empty())
|
|
|
|
FormatDebug(update_domain, "starting: %s",
|
|
|
|
next.path_utf8.c_str());
|
|
|
|
else
|
|
|
|
LogDebug(update_domain, "starting");
|
|
|
|
|
|
|
|
SetThreadIdlePriority();
|
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
modified = walk->Walk(next.db->GetRoot(), next.path_utf8.c_str(),
|
2014-02-27 18:04:24 +01:00
|
|
|
next.discard);
|
2014-02-27 16:14:10 +01:00
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
if (modified || !next.db->FileExists()) {
|
2015-12-15 22:26:26 +01:00
|
|
|
try {
|
2015-12-16 10:24:43 +01:00
|
|
|
next.db->Save();
|
2019-09-01 12:57:24 +02:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception(),
|
|
|
|
"Failed to save database");
|
2015-12-15 22:26:26 +01:00
|
|
|
}
|
2014-02-27 16:14:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!next.path_utf8.empty())
|
|
|
|
FormatDebug(update_domain, "finished: %s",
|
|
|
|
next.path_utf8.c_str());
|
|
|
|
else
|
|
|
|
LogDebug(update_domain, "finished");
|
|
|
|
|
2017-11-10 20:58:25 +01:00
|
|
|
defer.Schedule();
|
2014-02-27 16:14:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateService::StartThread(UpdateQueueItem &&i)
|
|
|
|
{
|
2017-08-18 17:57:59 +02:00
|
|
|
assert(GetEventLoop().IsInside());
|
2014-02-27 18:04:24 +01:00
|
|
|
assert(walk == nullptr);
|
2014-02-27 16:14:10 +01:00
|
|
|
|
|
|
|
modified = false;
|
|
|
|
|
|
|
|
next = std::move(i);
|
2019-04-26 14:53:54 +02:00
|
|
|
walk = std::make_unique<UpdateWalk>(config, GetEventLoop(), listener,
|
|
|
|
*next.storage);
|
2014-02-27 16:36:11 +01:00
|
|
|
|
2017-02-10 22:41:11 +01:00
|
|
|
update_thread.Start();
|
2014-02-27 16:14:10 +01:00
|
|
|
|
|
|
|
FormatDebug(update_domain,
|
|
|
|
"spawned thread for update job id %i", next.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
2019-09-01 13:51:34 +02:00
|
|
|
UpdateService::GenerateId() noexcept
|
2014-02-27 16:14:10 +01:00
|
|
|
{
|
|
|
|
unsigned id = update_task_id + 1;
|
|
|
|
if (id > update_task_id_max)
|
|
|
|
id = 1;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
UpdateService::Enqueue(const char *path, bool discard)
|
|
|
|
{
|
2017-08-18 17:57:59 +02:00
|
|
|
assert(GetEventLoop().IsInside());
|
2014-02-27 16:14:10 +01:00
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
/* determine which (mounted) database will be updated and what
|
|
|
|
storage will be scanned */
|
|
|
|
SimpleDatabase *db2;
|
|
|
|
Storage *storage2;
|
|
|
|
|
2015-12-15 23:27:05 +01:00
|
|
|
Directory::LookupResult lr;
|
|
|
|
{
|
|
|
|
const ScopeDatabaseLock protect;
|
|
|
|
lr = db.GetRoot().LookupDirectory(path);
|
|
|
|
}
|
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
if (lr.directory->IsMount()) {
|
|
|
|
/* follow the mountpoint, update the mounted
|
|
|
|
database */
|
|
|
|
|
2019-02-20 20:32:11 +01:00
|
|
|
db2 = dynamic_cast<SimpleDatabase *>(lr.directory->mounted_database.get());
|
2018-11-19 19:19:20 +01:00
|
|
|
if (db2 == nullptr)
|
2018-08-19 23:15:52 +02:00
|
|
|
throw std::runtime_error("Cannot update this type of database");
|
2014-02-26 08:39:44 +01:00
|
|
|
|
|
|
|
if (lr.uri == nullptr) {
|
|
|
|
storage2 = storage.GetMount(path);
|
|
|
|
path = "";
|
|
|
|
} else {
|
|
|
|
assert(lr.uri > path);
|
|
|
|
assert(lr.uri < path + strlen(path));
|
|
|
|
assert(lr.uri[-1] == '/');
|
|
|
|
|
|
|
|
const std::string mountpoint(path, lr.uri - 1);
|
|
|
|
storage2 = storage.GetMount(mountpoint.c_str());
|
|
|
|
path = lr.uri;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* use the "root" database/storage */
|
|
|
|
|
|
|
|
db2 = &db;
|
|
|
|
storage2 = storage.GetMount("");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (storage2 == nullptr)
|
|
|
|
/* no storage found at this mount point - should not
|
|
|
|
happen */
|
2018-08-19 23:15:52 +02:00
|
|
|
throw std::runtime_error("No storage at this path");
|
2014-02-26 08:39:44 +01:00
|
|
|
|
2016-03-05 19:40:11 +01:00
|
|
|
if (walk != nullptr) {
|
2014-02-27 16:14:10 +01:00
|
|
|
const unsigned id = GenerateId();
|
2014-02-26 08:39:44 +01:00
|
|
|
if (!queue.Push(*db2, *storage2, path, discard, id))
|
2018-08-19 23:15:52 +02:00
|
|
|
throw ProtocolError(ACK_ERROR_UPDATE_ALREADY,
|
|
|
|
"Update queue is full");
|
2014-02-27 16:14:10 +01:00
|
|
|
|
|
|
|
update_task_id = id;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned id = update_task_id = GenerateId();
|
2014-02-26 08:39:44 +01:00
|
|
|
StartThread(UpdateQueueItem(*db2, *storage2, path, discard, id));
|
2014-02-27 16:14:10 +01:00
|
|
|
|
|
|
|
idle_add(IDLE_UPDATE);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called in the main thread after the database update is finished.
|
|
|
|
*/
|
|
|
|
void
|
2017-11-10 20:58:25 +01:00
|
|
|
UpdateService::RunDeferred() noexcept
|
2014-02-27 16:14:10 +01:00
|
|
|
{
|
|
|
|
assert(next.IsDefined());
|
2014-02-27 18:04:24 +01:00
|
|
|
assert(walk != nullptr);
|
2014-02-27 16:14:10 +01:00
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
/* wait for thread to finish only if it wasn't cancelled by
|
|
|
|
CancelMount() */
|
|
|
|
if (update_thread.IsDefined())
|
|
|
|
update_thread.Join();
|
2014-02-27 18:04:24 +01:00
|
|
|
|
2019-04-26 14:53:54 +02:00
|
|
|
walk.reset();
|
2014-02-27 18:04:24 +01:00
|
|
|
|
2018-02-01 19:53:42 +01:00
|
|
|
next.Clear();
|
2014-02-27 16:14:10 +01:00
|
|
|
|
|
|
|
idle_add(IDLE_UPDATE);
|
|
|
|
|
|
|
|
if (modified)
|
|
|
|
/* send "idle" events */
|
|
|
|
listener.OnDatabaseModified();
|
|
|
|
|
|
|
|
auto i = queue.Pop();
|
|
|
|
if (i.IsDefined()) {
|
|
|
|
/* schedule the next path */
|
|
|
|
StartThread(std::move(i));
|
|
|
|
}
|
|
|
|
}
|