2008-10-08 10:48:48 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
|
|
|
* Copyright (C) 2008 Max Kellermann <max@duempel.org>
|
|
|
|
* This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "update.h"
|
2008-10-08 11:07:35 +02:00
|
|
|
#include "database.h"
|
2008-10-08 10:49:05 +02:00
|
|
|
#include "directory.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2008-10-08 10:48:48 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "ls.h"
|
2008-10-14 11:10:49 +02:00
|
|
|
#include "mapper.h"
|
2008-10-08 10:48:48 +02:00
|
|
|
#include "path.h"
|
|
|
|
#include "playlist.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "main_notify.h"
|
|
|
|
#include "condition.h"
|
|
|
|
#include "update.h"
|
2008-10-14 22:38:14 +02:00
|
|
|
#include "idle.h"
|
2008-10-08 10:48:48 +02: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;
|
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
/* make this dynamic?, or maybe this is big enough... */
|
|
|
|
static char *update_paths[32];
|
|
|
|
static size_t update_paths_nr;
|
|
|
|
|
|
|
|
static pthread_t update_thr;
|
|
|
|
|
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
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static struct song *delete;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
|
|
|
static struct condition delete_cond;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static void
|
|
|
|
directory_set_stat(struct directory *dir, const struct stat *st)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
|
|
|
dir->inode = st->st_ino;
|
|
|
|
dir->device = st->st_dev;
|
|
|
|
dir->stat = 1;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static void
|
2008-10-08 10:49:11 +02:00
|
|
|
delete_song(struct directory *dir, struct song *del)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
|
|
|
/* first, prevent traversers in main task from getting this */
|
|
|
|
songvec_delete(&dir->songs, del);
|
|
|
|
|
|
|
|
/* now take it out of the playlist (in the main_task) */
|
|
|
|
cond_enter(&delete_cond);
|
|
|
|
assert(!delete);
|
|
|
|
delete = del;
|
|
|
|
wakeup_main_task();
|
|
|
|
do { cond_wait(&delete_cond); } while (delete);
|
|
|
|
cond_leave(&delete_cond);
|
|
|
|
|
|
|
|
/* finally, all possible references gone, free it */
|
2008-10-08 11:05:34 +02:00
|
|
|
song_free(del);
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2008-10-09 15:41:02 +02:00
|
|
|
static int
|
|
|
|
delete_each_song(struct song *song, mpd_unused void *data)
|
|
|
|
{
|
|
|
|
struct directory *directory = data;
|
|
|
|
assert(song->parent == directory);
|
|
|
|
delete_song(directory, song);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively remove all sub directories and songs from a directory,
|
|
|
|
* leaving an empty directory.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
clear_directory(struct directory *directory)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = directory->children.nr; --i >= 0;)
|
|
|
|
clear_directory(directory->children.base[i]);
|
|
|
|
dirvec_clear(&directory->children);
|
|
|
|
|
|
|
|
songvec_for_each(&directory->songs, delete_each_song, directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively free a directory and all its contents.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
delete_directory(struct directory *directory)
|
|
|
|
{
|
|
|
|
assert(directory->parent != NULL);
|
|
|
|
|
|
|
|
clear_directory(directory);
|
|
|
|
|
|
|
|
dirvec_delete(&directory->parent->children, directory);
|
|
|
|
directory_free(directory);
|
|
|
|
}
|
|
|
|
|
2008-10-13 16:46:32 +02:00
|
|
|
static void
|
|
|
|
delete_name_in(struct directory *parent, const char *name)
|
|
|
|
{
|
|
|
|
struct directory *directory = directory_get_child(parent, name);
|
|
|
|
struct song *song = songvec_find(&parent->songs, name);
|
|
|
|
|
|
|
|
if (directory != NULL) {
|
|
|
|
delete_directory(directory);
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (song != NULL) {
|
|
|
|
delete_song(parent, song);
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
struct delete_data {
|
|
|
|
char *tmp;
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *dir;
|
2008-10-08 10:48:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* passed to songvec_for_each */
|
2008-10-08 10:49:11 +02:00
|
|
|
static int
|
|
|
|
delete_song_if_removed(struct song *song, void *_data)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
|
|
|
struct delete_data *data = _data;
|
2008-10-14 11:10:49 +02:00
|
|
|
const char *path;
|
|
|
|
struct stat st;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-14 11:10:49 +02:00
|
|
|
if ((path = map_song_fs(song, data->tmp)) == NULL ||
|
|
|
|
stat(data->tmp, &st) < 0 || !S_ISREG(st.st_mode)) {
|
2008-10-08 10:48:48 +02:00
|
|
|
delete_song(data->dir, song);
|
2008-10-09 19:41:58 +02:00
|
|
|
modified = true;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-09 19:41:58 +02:00
|
|
|
static void
|
2008-10-08 10:49:05 +02:00
|
|
|
removeDeletedFromDirectory(char *path_max_tmp, struct directory *directory)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dirvec *dv = &directory->children;
|
|
|
|
struct delete_data data;
|
|
|
|
|
|
|
|
for (i = dv->nr; --i >= 0; ) {
|
2008-10-14 11:10:49 +02:00
|
|
|
const char *path_fs;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
path_fs = map_directory_fs(dv->base[i], path_max_tmp);
|
|
|
|
if (path_fs == NULL || (stat(path_fs, &st) == 0 &&
|
|
|
|
S_ISDIR(st.st_mode)))
|
2008-10-08 10:48:48 +02:00
|
|
|
continue;
|
|
|
|
LOG("removing directory: %s\n", dv->base[i]->path);
|
|
|
|
dirvec_delete(dv, dv->base[i]);
|
2008-10-09 19:41:58 +02:00
|
|
|
modified = true;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data.dir = directory;
|
|
|
|
data.tmp = path_max_tmp;
|
|
|
|
songvec_for_each(&directory->songs, delete_song_if_removed, &data);
|
|
|
|
}
|
|
|
|
|
2008-10-14 11:10:49 +02:00
|
|
|
static int
|
|
|
|
stat_directory(const struct directory *directory, struct stat *st)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2008-10-14 11:10:49 +02:00
|
|
|
char buffer[MPD_PATH_MAX];
|
|
|
|
const char *path_fs;
|
|
|
|
|
|
|
|
path_fs = map_directory_fs(directory, buffer);
|
|
|
|
if (path_fs == NULL)
|
|
|
|
return -1;
|
|
|
|
return stat(path_fs, st);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
stat_directory_child(const struct directory *parent, const char *name,
|
|
|
|
struct stat *st)
|
|
|
|
{
|
|
|
|
char path_fs[MPD_PATH_MAX];
|
|
|
|
|
|
|
|
map_directory_child_fs(parent, name, path_fs);
|
|
|
|
return stat(path_fs, st);
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static int
|
|
|
|
statDirectory(struct directory *dir)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2008-10-14 11:10:49 +02:00
|
|
|
if (stat_directory(dir, &st) < 0)
|
2008-10-08 10:48:48 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
directory_set_stat(dir, &st);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static int
|
|
|
|
inodeFoundInParent(struct directory *parent, ino_t inode, dev_t device)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
|
|
|
while (parent) {
|
|
|
|
if (!parent->stat && statDirectory(parent) < 0)
|
|
|
|
return -1;
|
|
|
|
if (parent->inode == inode && parent->device == device) {
|
|
|
|
DEBUG("recursive directory found\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
parent = parent->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-13 16:46:51 +02:00
|
|
|
static struct directory *
|
|
|
|
make_subdir(struct directory *parent, const char *name)
|
|
|
|
{
|
|
|
|
struct directory *directory;
|
|
|
|
|
|
|
|
directory = directory_get_child(parent, name);
|
2008-10-13 16:55:54 +02:00
|
|
|
if (directory == NULL) {
|
|
|
|
char path[MPD_PATH_MAX];
|
|
|
|
|
|
|
|
if (isRootDirectory(directory_get_path(parent)))
|
|
|
|
strcpy(path, name);
|
|
|
|
else
|
|
|
|
pfx_dir(path, name, strlen(name),
|
|
|
|
directory_get_path(parent),
|
|
|
|
strlen(directory_get_path(parent)));
|
|
|
|
|
|
|
|
directory = directory_new_child(parent, path);
|
|
|
|
}
|
2008-10-13 16:46:51 +02:00
|
|
|
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
2008-10-09 19:41:58 +02:00
|
|
|
static bool
|
2008-10-09 19:13:03 +02:00
|
|
|
updateDirectory(struct directory *directory, const struct stat *st);
|
2008-10-09 19:11:54 +02:00
|
|
|
|
2008-10-09 19:41:58 +02:00
|
|
|
static void
|
2008-10-09 19:13:03 +02:00
|
|
|
updateInDirectory(struct directory *directory,
|
|
|
|
const char *name, const struct stat *st)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2008-10-13 16:55:54 +02:00
|
|
|
assert(strchr(name, '/') == NULL);
|
|
|
|
|
2008-10-09 19:13:03 +02:00
|
|
|
if (S_ISREG(st->st_mode) && hasMusicSuffix(name, 0)) {
|
2008-10-13 16:55:54 +02:00
|
|
|
struct song *song = songvec_find(&directory->songs, name);
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-09 19:16:21 +02:00
|
|
|
if (song == NULL) {
|
2008-10-13 16:55:54 +02:00
|
|
|
song = song_file_load(name, directory);
|
2008-10-09 15:37:21 +02:00
|
|
|
if (song == NULL)
|
2008-10-09 19:41:58 +02:00
|
|
|
return;
|
2008-10-09 15:37:21 +02:00
|
|
|
|
|
|
|
songvec_add(&directory->songs, song);
|
2008-10-09 19:41:58 +02:00
|
|
|
modified = true;
|
2008-10-13 16:55:54 +02:00
|
|
|
LOG("added %s/%s\n",
|
|
|
|
directory_get_path(directory), name);
|
2008-10-09 19:13:03 +02:00
|
|
|
} else if (st->st_mtime != song->mtime) {
|
2008-10-13 16:55:54 +02:00
|
|
|
LOG("updating %s/%s\n",
|
|
|
|
directory_get_path(directory), name);
|
2008-10-08 11:06:26 +02:00
|
|
|
if (!song_file_update(song))
|
2008-10-08 10:48:48 +02:00
|
|
|
delete_song(directory, song);
|
2008-10-09 19:41:58 +02:00
|
|
|
modified = true;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
2008-10-09 19:13:03 +02:00
|
|
|
} else if (S_ISDIR(st->st_mode)) {
|
2008-10-09 19:15:56 +02:00
|
|
|
struct directory *subdir;
|
2008-10-09 19:41:58 +02:00
|
|
|
bool ret;
|
2008-10-09 19:15:56 +02:00
|
|
|
|
|
|
|
if (inodeFoundInParent(directory, st->st_ino, st->st_dev))
|
2008-10-09 19:41:58 +02:00
|
|
|
return;
|
2008-10-09 19:15:56 +02:00
|
|
|
|
2008-10-13 16:46:51 +02:00
|
|
|
subdir = make_subdir(directory, name);
|
2008-10-09 19:17:25 +02:00
|
|
|
assert(directory == subdir->parent);
|
2008-10-09 15:47:59 +02:00
|
|
|
|
2008-10-09 19:17:25 +02:00
|
|
|
ret = updateDirectory(subdir, st);
|
2008-10-09 19:41:58 +02:00
|
|
|
if (!ret)
|
2008-10-09 19:17:25 +02:00
|
|
|
delete_directory(subdir);
|
2008-10-09 19:20:59 +02:00
|
|
|
} else {
|
|
|
|
DEBUG("update: %s is not a directory or music\n", name);
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-15 20:51:01 +02:00
|
|
|
/* we don't look at "." / ".." nor files with newlines in their name */
|
|
|
|
static bool skip_path(const char *path)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2008-10-15 20:51:01 +02:00
|
|
|
return (path[0] == '.' && path[1] == 0) ||
|
|
|
|
(path[0] == '.' && path[1] == '.' && path[2] == 0) ||
|
|
|
|
strchr(path, '\n') != NULL;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2008-10-14 11:10:54 +02:00
|
|
|
static bool
|
|
|
|
skip_symlink(const struct directory *directory, const char *utf8_name)
|
|
|
|
{
|
|
|
|
char buffer[MPD_PATH_MAX];
|
|
|
|
const char *p;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
p = map_directory_child_fs(directory, utf8_name, buffer);
|
|
|
|
if (p == NULL)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ret = readlink(p, buffer, sizeof(buffer));
|
|
|
|
if (ret < 0)
|
|
|
|
/* don't skip if this is not a symlink */
|
|
|
|
return errno != EINVAL;
|
|
|
|
|
|
|
|
if (buffer[0] == '/')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
while (*p == '.') {
|
|
|
|
if (p[1] == '.' && p[2] == '/') {
|
|
|
|
/* "../" moves to parent directory */
|
|
|
|
directory = directory->parent;
|
|
|
|
if (directory == NULL)
|
|
|
|
/* we have moved outside the music
|
|
|
|
directory - don't skip this
|
|
|
|
symlink */
|
|
|
|
return false;
|
|
|
|
p += 3;
|
|
|
|
} else if (p[1] == '/')
|
|
|
|
/* eliminate "./" */
|
|
|
|
p += 2;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we are still in the music directory, so this symlink points
|
|
|
|
to a song which is already in the database - skip it */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-09 19:41:58 +02:00
|
|
|
static bool
|
2008-10-09 19:13:03 +02:00
|
|
|
updateDirectory(struct directory *directory, const struct stat *st)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
2008-10-14 11:10:49 +02:00
|
|
|
const char *path_fs;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-09 19:13:03 +02:00
|
|
|
assert(S_ISDIR(st->st_mode));
|
|
|
|
|
|
|
|
directory_set_stat(directory, st);
|
|
|
|
|
2008-10-14 11:10:49 +02:00
|
|
|
path_fs = map_directory_fs(directory, path_max_tmp);
|
|
|
|
if (path_fs == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
dir = opendir(path_fs);
|
2008-10-08 10:48:48 +02:00
|
|
|
if (!dir)
|
2008-10-09 19:41:58 +02:00
|
|
|
return false;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-09 19:41:58 +02:00
|
|
|
removeDeletedFromDirectory(path_max_tmp, directory);
|
2008-10-08 10:48:48 +02:00
|
|
|
|
|
|
|
while ((ent = readdir(dir))) {
|
|
|
|
char *utf8;
|
2008-10-09 19:13:03 +02:00
|
|
|
struct stat st2;
|
|
|
|
|
2008-10-14 11:10:54 +02:00
|
|
|
if (skip_path(ent->d_name) ||
|
|
|
|
skip_symlink(directory, ent->d_name))
|
2008-10-08 10:48:48 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
utf8 = fs_charset_to_utf8(path_max_tmp, ent->d_name);
|
|
|
|
if (!utf8)
|
|
|
|
continue;
|
|
|
|
|
2008-10-14 11:10:49 +02:00
|
|
|
if (stat_directory_child(directory, utf8, &st2) == 0)
|
2008-10-13 16:55:54 +02:00
|
|
|
updateInDirectory(directory,
|
2008-10-14 11:10:49 +02:00
|
|
|
path_max_tmp, &st2);
|
2008-10-09 19:13:03 +02:00
|
|
|
else
|
2008-10-14 11:10:49 +02:00
|
|
|
delete_name_in(directory, path_max_tmp);
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
2008-10-09 19:41:58 +02:00
|
|
|
return true;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2008-10-09 15:47:22 +02:00
|
|
|
static struct directory *
|
|
|
|
directory_make_child_checked(struct directory *parent, const char *path)
|
|
|
|
{
|
|
|
|
struct directory *directory;
|
|
|
|
struct stat st;
|
|
|
|
struct song *conflicting;
|
|
|
|
|
|
|
|
directory = directory_get_child(parent, path);
|
|
|
|
if (directory != NULL)
|
|
|
|
return directory;
|
|
|
|
|
2008-10-14 11:10:49 +02:00
|
|
|
if (stat_directory_child(parent, mpd_basename(path), &st) < 0 ||
|
2008-10-09 15:47:22 +02:00
|
|
|
inodeFoundInParent(parent, st.st_ino, st.st_dev))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* if we're adding directory paths, make sure to delete filenames
|
|
|
|
with potentially the same name */
|
|
|
|
conflicting = songvec_find(&parent->songs, mpd_basename(path));
|
|
|
|
if (conflicting)
|
|
|
|
delete_song(parent, conflicting);
|
|
|
|
|
2008-10-09 19:14:04 +02:00
|
|
|
directory = directory_new_child(parent, path);
|
|
|
|
directory_set_stat(directory, &st);
|
|
|
|
return directory;
|
2008-10-09 15:47:22 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static struct directory *
|
2008-10-09 15:48:39 +02:00
|
|
|
addParentPathToDB(const char *utf8path)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2008-10-09 15:48:07 +02:00
|
|
|
struct directory *directory = db_get_root();
|
|
|
|
char *duplicated = xstrdup(utf8path);
|
|
|
|
char *slash = duplicated;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-09 15:48:39 +02:00
|
|
|
while ((slash = strchr(slash, '/')) != NULL) {
|
|
|
|
*slash = 0;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-09 15:48:07 +02:00
|
|
|
directory = directory_make_child_checked(directory,
|
|
|
|
duplicated);
|
|
|
|
if (directory == NULL || slash == NULL)
|
|
|
|
break;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-09 15:48:07 +02:00
|
|
|
*slash++ = '/';
|
|
|
|
}
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-09 15:48:07 +02:00
|
|
|
free(duplicated);
|
|
|
|
return directory;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2008-10-09 19:41:58 +02:00
|
|
|
static void
|
|
|
|
updatePath(const char *path)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2008-10-13 16:55:55 +02:00
|
|
|
struct directory *parent;
|
|
|
|
const char *name;
|
2008-10-09 19:13:02 +02:00
|
|
|
struct stat st;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-13 16:55:55 +02:00
|
|
|
parent = addParentPathToDB(path);
|
|
|
|
if (parent == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
name = mpd_basename(path);
|
|
|
|
|
2008-10-14 11:10:49 +02:00
|
|
|
if (stat_directory_child(parent, name, &st) == 0)
|
2008-10-13 16:55:55 +02:00
|
|
|
updateInDirectory(parent, name, &st);
|
2008-10-09 19:41:58 +02:00
|
|
|
else
|
2008-10-13 16:55:55 +02:00
|
|
|
delete_name_in(parent, name);
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void * update_task(void *_path)
|
|
|
|
{
|
2008-10-09 19:11:49 +02:00
|
|
|
if (_path != NULL && !isRootDirectory(_path)) {
|
2008-10-09 19:41:58 +02:00
|
|
|
updatePath((char *)_path);
|
2008-10-08 10:48:48 +02:00
|
|
|
free(_path);
|
|
|
|
} else {
|
2008-10-09 19:13:03 +02:00
|
|
|
struct directory *directory = db_get_root();
|
|
|
|
struct stat st;
|
|
|
|
|
2008-10-14 11:10:49 +02:00
|
|
|
if (stat_directory(directory, &st) == 0)
|
2008-10-09 19:41:58 +02:00
|
|
|
updateDirectory(directory, &st);
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2008-10-09 19:41:58 +02:00
|
|
|
if (modified)
|
|
|
|
db_save();
|
2008-10-08 10:48:48 +02:00
|
|
|
progress = UPDATE_PROGRESS_DONE;
|
|
|
|
wakeup_main_task();
|
2008-10-09 19:41:58 +02:00
|
|
|
return NULL;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void spawn_update_task(char *path)
|
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
|
|
|
|
assert(pthread_equal(pthread_self(), main_task));
|
|
|
|
|
|
|
|
progress = UPDATE_PROGRESS_RUNNING;
|
2008-10-09 19:41:58 +02:00
|
|
|
modified = false;
|
2008-10-08 10:48:48 +02:00
|
|
|
pthread_attr_init(&attr);
|
|
|
|
if (pthread_create(&update_thr, &attr, update_task, path))
|
|
|
|
FATAL("Failed to spawn update task: %s\n", strerror(errno));
|
|
|
|
if (++update_task_id > update_task_id_max)
|
|
|
|
update_task_id = 1;
|
|
|
|
DEBUG("spawned thread for update job id %i\n", update_task_id);
|
|
|
|
}
|
|
|
|
|
2008-10-09 19:20:05 +02:00
|
|
|
unsigned
|
|
|
|
directory_update_init(char *path)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
|
|
|
assert(pthread_equal(pthread_self(), main_task));
|
|
|
|
|
|
|
|
if (progress != UPDATE_PROGRESS_IDLE) {
|
2008-10-09 19:20:05 +02:00
|
|
|
unsigned next_task_id;
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-10-09 19:20:54 +02:00
|
|
|
if (update_paths_nr == ARRAY_SIZE(update_paths)) {
|
2008-10-13 09:55:03 +02:00
|
|
|
if (path)
|
|
|
|
free(path);
|
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
|
|
|
assert(update_paths_nr < ARRAY_SIZE(update_paths));
|
|
|
|
update_paths[update_paths_nr++] = path;
|
|
|
|
next_task_id = update_task_id + update_paths_nr;
|
|
|
|
|
|
|
|
return next_task_id > update_task_id_max ? 1 : next_task_id;
|
|
|
|
}
|
|
|
|
spawn_update_task(path);
|
|
|
|
return update_task_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reap_update_task(void)
|
|
|
|
{
|
|
|
|
assert(pthread_equal(pthread_self(), main_task));
|
|
|
|
|
2008-10-09 19:17:38 +02:00
|
|
|
if (progress == UPDATE_PROGRESS_IDLE)
|
|
|
|
return;
|
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
cond_enter(&delete_cond);
|
|
|
|
if (delete) {
|
|
|
|
char tmp[MPD_PATH_MAX];
|
2008-10-08 11:05:34 +02:00
|
|
|
LOG("removing: %s\n", song_get_url(delete, tmp));
|
2008-10-08 10:48:48 +02:00
|
|
|
deleteASongFromPlaylist(delete);
|
|
|
|
delete = NULL;
|
2008-10-08 11:36:38 +02:00
|
|
|
cond_signal_sync(&delete_cond);
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
cond_leave(&delete_cond);
|
|
|
|
|
|
|
|
if (progress != UPDATE_PROGRESS_DONE)
|
|
|
|
return;
|
2008-10-09 19:41:58 +02:00
|
|
|
if (pthread_join(update_thr, NULL))
|
2008-10-08 10:48:48 +02:00
|
|
|
FATAL("error joining update thread: %s\n", strerror(errno));
|
2008-10-09 19:41:58 +02:00
|
|
|
|
2008-10-14 22:38:14 +02:00
|
|
|
if (modified) {
|
2008-10-08 10:48:48 +02:00
|
|
|
playlistVersionChange();
|
2008-10-14 22:38:14 +02:00
|
|
|
idle_add(IDLE_DATABASE);
|
|
|
|
}
|
2008-10-08 10:48:48 +02:00
|
|
|
|
|
|
|
if (update_paths_nr) {
|
|
|
|
char *path = update_paths[0];
|
|
|
|
memmove(&update_paths[0], &update_paths[1],
|
|
|
|
--update_paths_nr * sizeof(char *));
|
|
|
|
spawn_update_task(path);
|
|
|
|
} else {
|
|
|
|
progress = UPDATE_PROGRESS_IDLE;
|
|
|
|
}
|
|
|
|
}
|