2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-04 20:50:00 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-10-08 10:48:48 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-10-08 10:48:48 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-02 19:22:15 +01:00
|
|
|
#include "UpdateGlue.hxx"
|
|
|
|
#include "UpdateQueue.hxx"
|
|
|
|
#include "UpdateWalk.hxx"
|
|
|
|
#include "UpdateRemove.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "UpdateDomain.hxx"
|
2013-01-02 22:43:56 +01:00
|
|
|
#include "Mapper.hxx"
|
2013-01-03 00:25:15 +01:00
|
|
|
#include "DatabaseSimple.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2013-01-09 23:12:53 +01:00
|
|
|
#include "GlobalEvents.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2013-10-02 12:14:07 +02:00
|
|
|
#include "Stats.hxx"
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "Main.hxx"
|
2013-04-17 22:58:33 +02:00
|
|
|
#include "Instance.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/FatalError.hxx"
|
2013-10-17 19:27:02 +02:00
|
|
|
#include "thread/Id.hxx"
|
2013-10-17 18:42:14 +02:00
|
|
|
#include "thread/Thread.hxx"
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
2008-12-29 17:29:49 +01:00
|
|
|
|
2008-10-09 19:31:31 +02:00
|
|
|
static enum update_progress {
|
2008-10-08 10:48:48 +02:00
|
|
|
UPDATE_PROGRESS_IDLE = 0,
|
|
|
|
UPDATE_PROGRESS_RUNNING = 1,
|
|
|
|
UPDATE_PROGRESS_DONE = 2
|
|
|
|
} progress;
|
|
|
|
|
2008-10-09 19:41:58 +02:00
|
|
|
static bool modified;
|
|
|
|
|
2013-10-17 18:42:14 +02:00
|
|
|
static Thread update_thread;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-09 19:20:05 +02:00
|
|
|
static const unsigned update_task_id_max = 1 << 15;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-09 19:20:05 +02:00
|
|
|
static unsigned update_task_id;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2013-10-17 21:30:49 +02:00
|
|
|
static UpdateQueueItem next;
|
2009-09-24 21:55:40 +02:00
|
|
|
|
2008-10-09 19:20:05 +02:00
|
|
|
unsigned
|
|
|
|
isUpdatingDB(void)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2013-10-17 21:34:36 +02:00
|
|
|
return next.id;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2013-10-17 18:42:14 +02:00
|
|
|
static void
|
2013-10-17 21:30:49 +02:00
|
|
|
update_task(gcc_unused void *ctx)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2013-10-17 21:30:49 +02:00
|
|
|
if (!next.path_utf8.empty())
|
|
|
|
FormatDebug(update_domain, "starting: %s",
|
|
|
|
next.path_utf8.c_str());
|
2010-02-27 16:41:54 +01:00
|
|
|
else
|
2013-09-27 22:31:24 +02:00
|
|
|
LogDebug(update_domain, "starting");
|
2010-02-27 16:41:54 +01:00
|
|
|
|
2013-10-17 21:30:49 +02:00
|
|
|
modified = update_walk(next.path_utf8.c_str(), next.discard);
|
2009-08-14 11:51:35 +02:00
|
|
|
|
2011-09-09 23:28:27 +02:00
|
|
|
if (modified || !db_exists()) {
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
|
|
|
if (!db_save(error))
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error, "Failed to save database");
|
2011-09-09 23:28:27 +02:00
|
|
|
}
|
2009-01-08 07:36:40 +01:00
|
|
|
|
2013-10-17 21:30:49 +02:00
|
|
|
if (!next.path_utf8.empty())
|
|
|
|
FormatDebug(update_domain, "finished: %s",
|
|
|
|
next.path_utf8.c_str());
|
2010-02-27 16:41:54 +01:00
|
|
|
else
|
2013-09-27 22:31:24 +02:00
|
|
|
LogDebug(update_domain, "finished");
|
2010-02-27 16:41:54 +01:00
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
progress = UPDATE_PROGRESS_DONE;
|
2013-01-09 23:12:53 +01:00
|
|
|
GlobalEvents::Emit(GlobalEvents::UPDATE);
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2009-09-24 21:39:43 +02:00
|
|
|
static void
|
2013-10-17 21:30:49 +02:00
|
|
|
spawn_update_task(UpdateQueueItem &&i)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2013-10-17 19:27:02 +02:00
|
|
|
assert(main_thread.IsInside());
|
2008-10-08 10:48:48 +02:00
|
|
|
|
|
|
|
progress = UPDATE_PROGRESS_RUNNING;
|
2008-10-09 19:41:58 +02:00
|
|
|
modified = false;
|
2009-09-24 21:39:43 +02:00
|
|
|
|
2013-10-17 21:30:49 +02:00
|
|
|
next = std::move(i);
|
|
|
|
|
2013-10-17 18:42:14 +02:00
|
|
|
Error error;
|
2013-10-17 21:30:49 +02:00
|
|
|
if (!update_thread.Start(update_task, nullptr, error))
|
2013-10-17 18:42:14 +02:00
|
|
|
FatalError(error);
|
2009-09-24 21:39:43 +02:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(update_domain,
|
2013-10-17 21:34:36 +02:00
|
|
|
"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;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2008-10-09 19:20:05 +02:00
|
|
|
unsigned
|
2013-10-17 21:30:49 +02:00
|
|
|
update_enqueue(const char *path, bool discard)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2013-10-17 19:27:02 +02:00
|
|
|
assert(main_thread.IsInside());
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2012-08-08 08:19:30 +02:00
|
|
|
if (!db_is_simple() || !mapper_has_music_directory())
|
2009-01-18 16:56:07 +01:00
|
|
|
return 0;
|
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
if (progress != UPDATE_PROGRESS_IDLE) {
|
2013-10-17 21:34:36 +02:00
|
|
|
const unsigned id = generate_update_id();
|
|
|
|
if (!update_queue_push(path, discard, id))
|
2008-10-09 19:17:44 +02:00
|
|
|
return 0;
|
2008-10-09 19:20:54 +02:00
|
|
|
|
2013-10-17 21:34:36 +02:00
|
|
|
update_task_id = id;
|
|
|
|
return id;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
2009-09-24 21:39:43 +02:00
|
|
|
|
2013-10-17 21:34:36 +02:00
|
|
|
const unsigned id = update_task_id = generate_update_id();
|
|
|
|
spawn_update_task(UpdateQueueItem(path, discard, id));
|
2009-07-05 08:46:53 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_UPDATE);
|
|
|
|
|
2013-10-17 21:34:36 +02:00
|
|
|
return id;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2009-01-04 20:30:23 +01:00
|
|
|
/**
|
|
|
|
* Called in the main thread after the database update is finished.
|
|
|
|
*/
|
|
|
|
static void update_finished_event(void)
|
|
|
|
{
|
|
|
|
assert(progress == UPDATE_PROGRESS_DONE);
|
2013-10-17 21:34:36 +02:00
|
|
|
assert(next.IsDefined());
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2013-10-17 18:42:14 +02:00
|
|
|
update_thread.Join();
|
2013-10-17 21:34:36 +02:00
|
|
|
next = UpdateQueueItem();
|
2008-10-09 19:41:58 +02:00
|
|
|
|
2009-07-05 08:46:53 +02:00
|
|
|
idle_add(IDLE_UPDATE);
|
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
if (modified)
|
2009-01-04 20:30:23 +01:00
|
|
|
/* send "idle" events */
|
2013-04-17 22:58:33 +02:00
|
|
|
instance->DatabaseModified();
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2013-10-17 21:13:40 +02:00
|
|
|
auto i = update_queue_shift();
|
|
|
|
if (i.IsDefined()) {
|
2009-01-04 20:30:23 +01:00
|
|
|
/* schedule the next path */
|
2013-10-17 21:30:49 +02:00
|
|
|
spawn_update_task(std::move(i));
|
2008-10-08 10:48:48 +02:00
|
|
|
} else {
|
|
|
|
progress = UPDATE_PROGRESS_IDLE;
|
2009-01-04 20:30:27 +01:00
|
|
|
|
2009-01-04 20:57:06 +01:00
|
|
|
stats_update();
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-27 19:34:54 +01:00
|
|
|
|
|
|
|
void update_global_init(void)
|
|
|
|
{
|
2013-01-09 23:12:53 +01:00
|
|
|
GlobalEvents::Register(GlobalEvents::UPDATE, update_finished_event);
|
2009-09-24 21:39:46 +02:00
|
|
|
|
|
|
|
update_remove_global_init();
|
|
|
|
update_walk_global_init();
|
2008-11-27 19:34:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void update_global_finish(void)
|
|
|
|
{
|
2009-09-24 21:39:46 +02:00
|
|
|
update_walk_global_finish();
|
2008-11-27 19:34:54 +01:00
|
|
|
}
|