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-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-02 19:22:15 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2009-01-01 18:12:00 +01:00
|
|
|
#include "event_pipe.h"
|
2009-01-04 20:30:27 +01:00
|
|
|
#include "stats.h"
|
2013-01-02 19:22:15 +01:00
|
|
|
}
|
|
|
|
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "Main.hxx"
|
2013-01-04 22:42:05 +01:00
|
|
|
#include "Partition.hxx"
|
2010-09-25 15:00:43 +02:00
|
|
|
#include "mpd_error.h"
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-20 22:18:12 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
2008-12-29 17:29:49 +01:00
|
|
|
|
2012-06-13 21:38:28 +02:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "update"
|
|
|
|
|
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;
|
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
static GThread *update_thr;
|
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
|
|
|
|
2009-09-24 21:55:40 +02:00
|
|
|
/* XXX this flag is passed to update_task() */
|
|
|
|
static bool discard;
|
|
|
|
|
2008-10-09 19:20:05 +02:00
|
|
|
unsigned
|
|
|
|
isUpdatingDB(void)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
|
|
|
return (progress != UPDATE_PROGRESS_IDLE) ? update_task_id : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void * update_task(void *_path)
|
|
|
|
{
|
2013-01-02 19:22:15 +01:00
|
|
|
const char *path = (const char *)_path;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2010-02-27 16:41:54 +01:00
|
|
|
if (path != NULL && *path != 0)
|
|
|
|
g_debug("starting: %s", path);
|
|
|
|
else
|
|
|
|
g_debug("starting");
|
|
|
|
|
2009-09-24 21:55:40 +02:00
|
|
|
modified = update_walk(path, discard);
|
2009-08-14 11:51:35 +02:00
|
|
|
|
2011-09-09 23:28:27 +02:00
|
|
|
if (modified || !db_exists()) {
|
|
|
|
GError *error = NULL;
|
|
|
|
if (!db_save(&error)) {
|
|
|
|
g_warning("Failed to save database: %s",
|
|
|
|
error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
}
|
2009-01-08 07:36:40 +01:00
|
|
|
|
2010-02-27 16:41:54 +01:00
|
|
|
if (path != NULL && *path != 0)
|
|
|
|
g_debug("finished: %s", path);
|
|
|
|
else
|
|
|
|
g_debug("finished");
|
|
|
|
g_free(_path);
|
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
progress = UPDATE_PROGRESS_DONE;
|
2009-01-01 18:22:11 +01:00
|
|
|
event_pipe_emit(PIPE_EVENT_UPDATE);
|
2008-10-09 19:41:58 +02:00
|
|
|
return NULL;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2009-09-24 21:39:43 +02:00
|
|
|
static void
|
|
|
|
spawn_update_task(const char *path)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2009-01-04 19:51:54 +01:00
|
|
|
GError *e = NULL;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
assert(g_thread_self() == main_task);
|
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
|
|
|
|
|
|
|
update_thr = g_thread_create(update_task, g_strdup(path), TRUE, &e);
|
|
|
|
if (update_thr == NULL)
|
2010-09-25 15:00:43 +02:00
|
|
|
MPD_ERROR("Failed to spawn update task: %s", e->message);
|
2009-09-24 21:39:43 +02:00
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
if (++update_task_id > update_task_id_max)
|
|
|
|
update_task_id = 1;
|
2008-12-29 17:29:49 +01:00
|
|
|
g_debug("spawned thread for update job id %i", update_task_id);
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2008-10-09 19:20:05 +02:00
|
|
|
unsigned
|
2009-09-24 21:55:40 +02:00
|
|
|
update_enqueue(const char *path, bool _discard)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2008-12-28 22:09:42 +01:00
|
|
|
assert(g_thread_self() == main_task);
|
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) {
|
2009-09-24 21:39:46 +02:00
|
|
|
unsigned next_task_id =
|
2009-09-24 21:55:40 +02:00
|
|
|
update_queue_push(path, discard, update_task_id);
|
2009-09-24 21:39:46 +02:00
|
|
|
if (next_task_id == 0)
|
2008-10-09 19:17:44 +02:00
|
|
|
return 0;
|
2008-10-09 19:20:54 +02:00
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
return next_task_id > update_task_id_max ? 1 : next_task_id;
|
|
|
|
}
|
2009-09-24 21:39:43 +02:00
|
|
|
|
2009-09-24 21:55:40 +02:00
|
|
|
discard = _discard;
|
2008-10-08 10:48:48 +02:00
|
|
|
spawn_update_task(path);
|
2009-07-05 08:46:53 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_UPDATE);
|
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
return update_task_id;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2009-09-24 21:39:46 +02:00
|
|
|
char *path;
|
|
|
|
|
2009-01-04 20:30:23 +01:00
|
|
|
assert(progress == UPDATE_PROGRESS_DONE);
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_thread_join(update_thr);
|
2008-10-09 19:41:58 +02:00
|
|
|
|
2009-07-05 08:46:53 +02:00
|
|
|
idle_add(IDLE_UPDATE);
|
|
|
|
|
2008-10-14 22:38:14 +02:00
|
|
|
if (modified) {
|
2009-01-04 20:30:23 +01:00
|
|
|
/* send "idle" events */
|
2013-01-04 22:42:05 +01:00
|
|
|
playlist_increment_version_all(&global_partition->playlist);
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_DATABASE);
|
|
|
|
}
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2009-09-24 21:55:40 +02:00
|
|
|
path = update_queue_shift(&discard);
|
2009-09-24 21:39:46 +02:00
|
|
|
if (path != NULL) {
|
2009-01-04 20:30:23 +01:00
|
|
|
/* schedule the next path */
|
2008-10-08 10:48:48 +02:00
|
|
|
spawn_update_task(path);
|
2009-09-24 21:39:43 +02:00
|
|
|
g_free(path);
|
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)
|
|
|
|
{
|
2009-01-04 20:30:23 +01:00
|
|
|
event_pipe_register(PIPE_EVENT_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();
|
|
|
|
update_remove_global_finish();
|
2008-11-27 19:34:54 +01:00
|
|
|
}
|