~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/update.c

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 
 * http://www.musicpd.org
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License along
16
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
#include "update.h"
22
 
#include "update_queue.h"
23
 
#include "update_walk.h"
24
 
#include "update_remove.h"
25
 
#include "update.h"
26
 
#include "database.h"
27
 
#include "mapper.h"
28
 
#include "playlist.h"
29
 
#include "event_pipe.h"
30
 
#include "update.h"
31
 
#include "idle.h"
32
 
#include "stats.h"
33
 
#include "main.h"
34
 
#include "mpd_error.h"
35
 
 
36
 
#include <glib.h>
37
 
 
38
 
#include <assert.h>
39
 
 
40
 
#undef G_LOG_DOMAIN
41
 
#define G_LOG_DOMAIN "update"
42
 
 
43
 
static enum update_progress {
44
 
        UPDATE_PROGRESS_IDLE = 0,
45
 
        UPDATE_PROGRESS_RUNNING = 1,
46
 
        UPDATE_PROGRESS_DONE = 2
47
 
} progress;
48
 
 
49
 
static bool modified;
50
 
 
51
 
static GThread *update_thr;
52
 
 
53
 
static const unsigned update_task_id_max = 1 << 15;
54
 
 
55
 
static unsigned update_task_id;
56
 
 
57
 
/* XXX this flag is passed to update_task() */
58
 
static bool discard;
59
 
 
60
 
unsigned
61
 
isUpdatingDB(void)
62
 
{
63
 
        return (progress != UPDATE_PROGRESS_IDLE) ? update_task_id : 0;
64
 
}
65
 
 
66
 
static void * update_task(void *_path)
67
 
{
68
 
        const char *path = _path;
69
 
 
70
 
        if (path != NULL && *path != 0)
71
 
                g_debug("starting: %s", path);
72
 
        else
73
 
                g_debug("starting");
74
 
 
75
 
        modified = update_walk(path, discard);
76
 
 
77
 
        if (modified || !db_exists()) {
78
 
                GError *error = NULL;
79
 
                if (!db_save(&error)) {
80
 
                        g_warning("Failed to save database: %s",
81
 
                                  error->message);
82
 
                        g_error_free(error);
83
 
                }
84
 
        }
85
 
 
86
 
        if (path != NULL && *path != 0)
87
 
                g_debug("finished: %s", path);
88
 
        else
89
 
                g_debug("finished");
90
 
        g_free(_path);
91
 
 
92
 
        progress = UPDATE_PROGRESS_DONE;
93
 
        event_pipe_emit(PIPE_EVENT_UPDATE);
94
 
        return NULL;
95
 
}
96
 
 
97
 
static void
98
 
spawn_update_task(const char *path)
99
 
{
100
 
        GError *e = NULL;
101
 
 
102
 
        assert(g_thread_self() == main_task);
103
 
 
104
 
        progress = UPDATE_PROGRESS_RUNNING;
105
 
        modified = false;
106
 
 
107
 
        update_thr = g_thread_create(update_task, g_strdup(path), TRUE, &e);
108
 
        if (update_thr == NULL)
109
 
                MPD_ERROR("Failed to spawn update task: %s", e->message);
110
 
 
111
 
        if (++update_task_id > update_task_id_max)
112
 
                update_task_id = 1;
113
 
        g_debug("spawned thread for update job id %i", update_task_id);
114
 
}
115
 
 
116
 
unsigned
117
 
update_enqueue(const char *path, bool _discard)
118
 
{
119
 
        assert(g_thread_self() == main_task);
120
 
 
121
 
        if (!mapper_has_music_directory())
122
 
                return 0;
123
 
 
124
 
        if (progress != UPDATE_PROGRESS_IDLE) {
125
 
                unsigned next_task_id =
126
 
                        update_queue_push(path, discard, update_task_id);
127
 
                if (next_task_id == 0)
128
 
                        return 0;
129
 
 
130
 
                return next_task_id > update_task_id_max ?  1 : next_task_id;
131
 
        }
132
 
 
133
 
        discard = _discard;
134
 
        spawn_update_task(path);
135
 
 
136
 
        idle_add(IDLE_UPDATE);
137
 
 
138
 
        return update_task_id;
139
 
}
140
 
 
141
 
/**
142
 
 * Called in the main thread after the database update is finished.
143
 
 */
144
 
static void update_finished_event(void)
145
 
{
146
 
        char *path;
147
 
 
148
 
        assert(progress == UPDATE_PROGRESS_DONE);
149
 
 
150
 
        g_thread_join(update_thr);
151
 
 
152
 
        idle_add(IDLE_UPDATE);
153
 
 
154
 
        if (modified) {
155
 
                /* send "idle" events */
156
 
                playlist_increment_version_all(&g_playlist);
157
 
                idle_add(IDLE_DATABASE);
158
 
        }
159
 
 
160
 
        path = update_queue_shift(&discard);
161
 
        if (path != NULL) {
162
 
                /* schedule the next path */
163
 
                spawn_update_task(path);
164
 
                g_free(path);
165
 
        } else {
166
 
                progress = UPDATE_PROGRESS_IDLE;
167
 
 
168
 
                stats_update();
169
 
        }
170
 
}
171
 
 
172
 
void update_global_init(void)
173
 
{
174
 
        event_pipe_register(PIPE_EVENT_UPDATE, update_finished_event);
175
 
 
176
 
        update_remove_global_init();
177
 
        update_walk_global_init();
178
 
}
179
 
 
180
 
void update_global_finish(void)
181
 
{
182
 
        update_walk_global_finish();
183
 
        update_remove_global_finish();
184
 
}