Service.cxx 6.15 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20 21
#include "config.h"
#include "Service.hxx"
22
#include "Walk.hxx"
23 24
#include "UpdateDomain.hxx"
#include "db/DatabaseListener.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "db/DatabaseLock.hxx"
26
#include "db/plugins/simple/SimpleDatabasePlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
27 28
#include "db/plugins/simple/Directory.hxx"
#include "storage/CompositeStorage.hxx"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include "Idle.hxx"
#include "util/Error.hxx"
#include "Log.hxx"
#include "Instance.hxx"
#include "system/FatalError.hxx"
#include "thread/Id.hxx"
#include "thread/Thread.hxx"
#include "thread/Util.hxx"

#ifndef NDEBUG
#include "event/Loop.hxx"
#endif

#include <assert.h>

UpdateService::UpdateService(EventLoop &_loop, SimpleDatabase &_db,
Max Kellermann's avatar
Max Kellermann committed
45
			     CompositeStorage &_storage,
46
			     DatabaseListener &_listener)
47 48 49
	:DeferredMonitor(_loop),
	 db(_db), storage(_storage),
	 listener(_listener),
50 51
	 progress(UPDATE_PROGRESS_IDLE),
	 update_task_id(0),
52
	 walk(nullptr)
53 54 55
{
}

56 57 58 59 60 61
UpdateService::~UpdateService()
{
	CancelAllAsync();

	if (update_thread.IsDefined())
		update_thread.Join();
62 63

	delete walk;
64 65 66 67 68 69 70 71
}

void
UpdateService::CancelAllAsync()
{
	assert(GetEventLoop().IsInsideOrNull());

	queue.Clear();
72 73 74

	if (walk != nullptr)
		walk->Cancel();
75 76
}

Max Kellermann's avatar
Max Kellermann committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
void
UpdateService::CancelMount(const char *uri)
{
	/* determine which (mounted) database will be updated and what
	   storage will be scanned */

	db_lock();
	const auto lr = db.GetRoot().LookupDirectory(uri);
	db_unlock();

	if (!lr.directory->IsMount())
		return;

	bool cancel_current = false;

	Storage *storage2 = storage.GetMount(uri);
	if (storage2 != nullptr) {
		queue.Erase(*storage2);
		cancel_current = next.IsDefined() && next.storage == storage2;
	}

	Database &_db2 = *lr.directory->mounted_database;
	if (_db2.IsPlugin(simple_db_plugin)) {
		SimpleDatabase &db2 = static_cast<SimpleDatabase &>(_db2);
		queue.Erase(db2);
		cancel_current |= next.IsDefined() && next.db == &db2;
	}

	if (cancel_current && walk != nullptr) {
		walk->Cancel();

		if (update_thread.IsDefined())
			update_thread.Join();
	}
}

113 114 115
inline void
UpdateService::Task()
{
116 117
	assert(walk != nullptr);

118 119 120 121 122 123 124 125
	if (!next.path_utf8.empty())
		FormatDebug(update_domain, "starting: %s",
			    next.path_utf8.c_str());
	else
		LogDebug(update_domain, "starting");

	SetThreadIdlePriority();

Max Kellermann's avatar
Max Kellermann committed
126
	modified = walk->Walk(next.db->GetRoot(), next.path_utf8.c_str(),
127
			      next.discard);
128

Max Kellermann's avatar
Max Kellermann committed
129
	if (modified || !next.db->FileExists()) {
130
		Error error;
Max Kellermann's avatar
Max Kellermann committed
131
		if (!next.db->Save(error))
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
			LogError(error, "Failed to save database");
	}

	if (!next.path_utf8.empty())
		FormatDebug(update_domain, "finished: %s",
			    next.path_utf8.c_str());
	else
		LogDebug(update_domain, "finished");

	progress = UPDATE_PROGRESS_DONE;
	DeferredMonitor::Schedule();
}

void
UpdateService::Task(void *ctx)
{
	UpdateService &service = *(UpdateService *)ctx;
	return service.Task();
}

void
UpdateService::StartThread(UpdateQueueItem &&i)
{
	assert(GetEventLoop().IsInsideOrNull());
156
	assert(walk == nullptr);
157 158 159 160 161

	progress = UPDATE_PROGRESS_RUNNING;
	modified = false;

	next = std::move(i);
Max Kellermann's avatar
Max Kellermann committed
162
	walk = new UpdateWalk(GetEventLoop(), listener, *next.storage);
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	Error error;
	if (!update_thread.Start(Task, this, error))
		FatalError(error);

	FormatDebug(update_domain,
		    "spawned thread for update job id %i", next.id);
}

unsigned
UpdateService::GenerateId()
{
	unsigned id = update_task_id + 1;
	if (id > update_task_id_max)
		id = 1;
	return id;
}

unsigned
UpdateService::Enqueue(const char *path, bool discard)
{
	assert(GetEventLoop().IsInsideOrNull());

Max Kellermann's avatar
Max Kellermann committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	/* determine which (mounted) database will be updated and what
	   storage will be scanned */
	SimpleDatabase *db2;
	Storage *storage2;

	db_lock();
	const auto lr = db.GetRoot().LookupDirectory(path);
	db_unlock();
	if (lr.directory->IsMount()) {
		/* follow the mountpoint, update the mounted
		   database */

		Database &_db2 = *lr.directory->mounted_database;
		if (!_db2.IsPlugin(simple_db_plugin))
			/* cannot update this type of database */
			return 0;

		db2 = static_cast<SimpleDatabase *>(&_db2);

		if (lr.uri == nullptr) {
			storage2 = storage.GetMount(path);
			path = "";
		} else {
			assert(lr.uri > path);
			assert(lr.uri < path + strlen(path));
			assert(lr.uri[-1] == '/');

			const std::string mountpoint(path, lr.uri - 1);
			storage2 = storage.GetMount(mountpoint.c_str());
			path = lr.uri;
		}
	} else {
		/* use the "root" database/storage */

		db2 = &db;
		storage2 = storage.GetMount("");
	}

	if (storage2 == nullptr)
		/* no storage found at this mount point - should not
		   happen */
		return 0;

229 230
	if (progress != UPDATE_PROGRESS_IDLE) {
		const unsigned id = GenerateId();
Max Kellermann's avatar
Max Kellermann committed
231
		if (!queue.Push(*db2, *storage2, path, discard, id))
232 233 234 235 236 237 238
			return 0;

		update_task_id = id;
		return id;
	}

	const unsigned id = update_task_id = GenerateId();
Max Kellermann's avatar
Max Kellermann committed
239
	StartThread(UpdateQueueItem(*db2, *storage2, path, discard, id));
240 241 242 243 244 245 246 247 248 249 250 251 252 253

	idle_add(IDLE_UPDATE);

	return id;
}

/**
 * Called in the main thread after the database update is finished.
 */
void
UpdateService::RunDeferred()
{
	assert(progress == UPDATE_PROGRESS_DONE);
	assert(next.IsDefined());
254
	assert(walk != nullptr);
255

Max Kellermann's avatar
Max Kellermann committed
256 257 258 259
	/* wait for thread to finish only if it wasn't cancelled by
	   CancelMount() */
	if (update_thread.IsDefined())
		update_thread.Join();
260 261 262 263

	delete walk;
	walk = nullptr;

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	next = UpdateQueueItem();

	idle_add(IDLE_UPDATE);

	if (modified)
		/* send "idle" events */
		listener.OnDatabaseModified();

	auto i = queue.Pop();
	if (i.IsDefined()) {
		/* schedule the next path */
		StartThread(std::move(i));
	} else {
		progress = UPDATE_PROGRESS_IDLE;
	}
}