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 "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"
|
2009-01-01 18:12:00 +01:00
|
|
|
#include "event_pipe.h"
|
2008-10-08 10:48:48 +02:00
|
|
|
#include "condition.h"
|
|
|
|
#include "update.h"
|
2008-10-14 22:38:14 +02:00
|
|
|
#include "idle.h"
|
2008-11-28 10:57:39 +01:00
|
|
|
#include "conf.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>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-12-29 17:29:49 +01: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-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;
|
|
|
|
|
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
|
|
|
|
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-11-28 10:57:39 +01:00
|
|
|
enum {
|
|
|
|
DEFAULT_FOLLOW_INSIDE_SYMLINKS = true,
|
2008-12-08 09:00:38 +01:00
|
|
|
DEFAULT_FOLLOW_OUTSIDE_SYMLINKS = true,
|
2008-11-28 10:57:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool follow_inside_symlinks;
|
|
|
|
static bool follow_outside_symlinks;
|
|
|
|
|
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;
|
2009-01-01 18:12:14 +01:00
|
|
|
event_pipe_signal();
|
2008-10-08 10:48:48 +02:00
|
|
|
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
|
2008-11-24 14:46:10 +01:00
|
|
|
delete_each_song(struct song *song, G_GNUC_UNUSED void *data)
|
2008-10-09 15:41:02 +02:00
|
|
|
{
|
|
|
|
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;
|
2008-12-29 17:29:49 +01:00
|
|
|
g_debug("removing directory: %s", dv->base[i]->path);
|
2008-10-08 10:48:48 +02:00
|
|
|
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)
|
|
|
|
{
|
2008-10-31 16:47:21 +01:00
|
|
|
char buffer[MPD_PATH_MAX];
|
|
|
|
const char *path_fs;
|
|
|
|
|
|
|
|
path_fs = map_directory_child_fs(parent, name, buffer);
|
|
|
|
if (path_fs == NULL)
|
|
|
|
return -1;
|
2008-10-14 11:10:49 +02:00
|
|
|
|
|
|
|
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) {
|
2008-12-29 17:29:49 +01:00
|
|
|
g_debug("recursive directory found");
|
2008-10-08 10:48:48 +02:00
|
|
|
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-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2008-12-16 21:42:42 +01:00
|
|
|
static void
|
|
|
|
update_archive_tree(struct directory *directory, char *name)
|
|
|
|
{
|
|
|
|
struct directory *subdir;
|
|
|
|
struct song *song;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
tmp = strchr(name, '/');
|
|
|
|
if (tmp) {
|
|
|
|
*tmp = 0;
|
|
|
|
//add dir is not there already
|
|
|
|
if ((subdir = dirvec_find(&directory->children, name)) == NULL) {
|
|
|
|
//create new directory
|
|
|
|
subdir = make_subdir(directory, name);
|
|
|
|
subdir->device = DEVICE_INARCHIVE;
|
|
|
|
}
|
|
|
|
//create directories first
|
|
|
|
update_archive_tree(subdir, tmp+1);
|
|
|
|
} else {
|
2008-12-28 19:03:00 +01:00
|
|
|
if (strlen(name) == 0) {
|
2008-12-29 17:29:49 +01:00
|
|
|
g_warning("archive returned directory only");
|
2008-12-28 19:03:00 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-12-16 21:42:42 +01:00
|
|
|
//add file
|
|
|
|
song = songvec_find(&directory->songs, name);
|
|
|
|
if (song == NULL) {
|
|
|
|
song = song_file_load(name, directory);
|
|
|
|
if (song != NULL) {
|
|
|
|
songvec_add(&directory->songs, song);
|
|
|
|
modified = true;
|
2008-12-29 17:29:49 +01:00
|
|
|
g_message("added %s/%s",
|
|
|
|
directory_get_path(directory), name);
|
2008-12-16 21:42:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-27 14:33:41 +01:00
|
|
|
#endif
|
2008-12-16 21:42:42 +01:00
|
|
|
|
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-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2008-12-16 21:42:42 +01:00
|
|
|
const struct archive_plugin *archive;
|
2008-12-27 14:33:41 +01:00
|
|
|
#endif
|
|
|
|
|
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-12-29 17:29:49 +01:00
|
|
|
g_message("added %s/%s",
|
|
|
|
directory_get_path(directory), name);
|
2008-10-09 19:13:03 +02:00
|
|
|
} else if (st->st_mtime != song->mtime) {
|
2008-12-29 17:29:49 +01:00
|
|
|
g_message("updating %s/%s",
|
|
|
|
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-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2008-12-16 21:42:42 +01:00
|
|
|
} else if (S_ISREG(st->st_mode) && (archive = get_archive_by_suffix(name))) {
|
|
|
|
struct archive_file *archfile;
|
|
|
|
char pathname[MPD_PATH_MAX];
|
|
|
|
|
|
|
|
map_directory_child_fs(directory, name, pathname);
|
|
|
|
//open archive
|
|
|
|
archfile = archive->open(pathname);
|
|
|
|
if (archfile) {
|
|
|
|
char *filepath;
|
|
|
|
struct directory *archdir;
|
|
|
|
|
2008-12-29 17:29:49 +01:00
|
|
|
g_debug("archive %s opened",pathname);
|
2008-12-16 21:42:42 +01:00
|
|
|
archdir = dirvec_find(&directory->children, name);
|
|
|
|
if (archdir == NULL) {
|
2008-12-29 17:29:49 +01:00
|
|
|
g_debug("creating archive directory (%s)",
|
|
|
|
name);
|
2008-12-16 21:42:42 +01:00
|
|
|
archdir = make_subdir(directory, name);
|
|
|
|
//mark this directory as archive (we use device for this)
|
|
|
|
archdir->device = DEVICE_INARCHIVE;
|
|
|
|
}
|
|
|
|
archive->scan_reset(archfile);
|
|
|
|
while ((filepath = archive->scan_next(archfile)) != NULL) {
|
|
|
|
//split name into directory and file
|
2008-12-29 17:29:49 +01:00
|
|
|
g_debug("adding archive file: %s", filepath);
|
2008-12-16 21:42:42 +01:00
|
|
|
update_archive_tree(archdir, filepath);
|
|
|
|
}
|
|
|
|
archive->close(archfile);
|
|
|
|
} else {
|
2008-12-29 17:29:49 +01:00
|
|
|
g_warning("unable to open archive %s", pathname);
|
2008-12-16 21:42:42 +01:00
|
|
|
}
|
2008-12-27 14:33:41 +01:00
|
|
|
#endif
|
2008-10-09 19:20:59 +02:00
|
|
|
} else {
|
2008-12-29 17:29:49 +01:00
|
|
|
g_debug("update: %s is not a directory, archive or music", 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;
|
|
|
|
|
2008-11-28 10:57:39 +01:00
|
|
|
if (!follow_inside_symlinks && !follow_outside_symlinks) {
|
|
|
|
/* ignore all symlinks */
|
|
|
|
return true;
|
|
|
|
} else if (follow_inside_symlinks && follow_outside_symlinks) {
|
|
|
|
/* consider all symlinks */
|
2008-10-14 11:10:54 +02:00
|
|
|
return false;
|
2008-11-28 10:57:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[0] == '/')
|
|
|
|
return !follow_outside_symlinks;
|
2008-10-14 11:10:54 +02:00
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
while (*p == '.') {
|
|
|
|
if (p[1] == '.' && p[2] == '/') {
|
|
|
|
/* "../" moves to parent directory */
|
|
|
|
directory = directory->parent;
|
2008-11-28 10:57:39 +01:00
|
|
|
if (directory == NULL) {
|
2008-10-14 11:10:54 +02:00
|
|
|
/* we have moved outside the music
|
2008-11-28 10:57:39 +01:00
|
|
|
directory - skip this symlink
|
|
|
|
if such symlinks are not allowed */
|
|
|
|
return !follow_outside_symlinks;
|
|
|
|
}
|
2008-10-14 11:10:54 +02:00
|
|
|
p += 3;
|
|
|
|
} else if (p[1] == '/')
|
|
|
|
/* eliminate "./" */
|
|
|
|
p += 2;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we are still in the music directory, so this symlink points
|
2008-11-28 10:57:39 +01:00
|
|
|
to a song which is already in the database - skip according
|
|
|
|
to the follow_inside_symlinks param*/
|
|
|
|
return !follow_inside_symlinks;
|
2008-10-14 11:10:54 +02:00
|
|
|
}
|
|
|
|
|
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-11-20 18:22:07 +01:00
|
|
|
if (skip_path(ent->d_name))
|
2008-10-08 10:48:48 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
utf8 = fs_charset_to_utf8(path_max_tmp, ent->d_name);
|
2008-11-20 18:22:07 +01:00
|
|
|
if (utf8 == NULL || skip_symlink(directory, utf8))
|
2008-10-08 10:48:48 +02:00
|
|
|
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;
|
2008-11-22 14:57:08 +01:00
|
|
|
char *base;
|
2008-10-09 15:47:22 +02:00
|
|
|
struct stat st;
|
|
|
|
struct song *conflicting;
|
|
|
|
|
|
|
|
directory = directory_get_child(parent, path);
|
|
|
|
if (directory != NULL)
|
|
|
|
return directory;
|
|
|
|
|
2008-11-22 14:57:08 +01:00
|
|
|
base = g_path_get_basename(path);
|
2008-10-20 22:18:12 +02:00
|
|
|
|
2008-11-22 14:57:08 +01:00
|
|
|
if (stat_directory_child(parent, base, &st) < 0 ||
|
2008-10-20 22:18:12 +02:00
|
|
|
inodeFoundInParent(parent, st.st_ino, st.st_dev)) {
|
2008-11-22 14:57:08 +01:00
|
|
|
g_free(base);
|
2008-10-09 15:47:22 +02:00
|
|
|
return NULL;
|
2008-10-20 22:18:12 +02:00
|
|
|
}
|
2008-10-09 15:47:22 +02:00
|
|
|
|
|
|
|
/* if we're adding directory paths, make sure to delete filenames
|
|
|
|
with potentially the same name */
|
2008-11-22 14:57:08 +01:00
|
|
|
conflicting = songvec_find(&parent->songs, base);
|
2008-10-09 15:47:22 +02:00
|
|
|
if (conflicting)
|
|
|
|
delete_song(parent, conflicting);
|
|
|
|
|
2008-11-22 14:57:08 +01:00
|
|
|
g_free(base);
|
2008-10-20 22:18:12 +02:00
|
|
|
|
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;
|
2008-10-20 22:18:12 +02:00
|
|
|
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;
|
|
|
|
|
2008-10-20 22:18:12 +02:00
|
|
|
name = g_path_get_basename(path);
|
2008-10-13 16:55:55 +02:00
|
|
|
|
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-20 22:18:12 +02:00
|
|
|
|
|
|
|
g_free(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;
|
2009-01-01 18:12:14 +01:00
|
|
|
event_pipe_signal();
|
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)
|
|
|
|
{
|
2008-12-28 22:09:42 +01:00
|
|
|
GError *e;
|
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;
|
2008-12-28 22:09:42 +01:00
|
|
|
if (!(update_thr = g_thread_create(update_task, path, TRUE, &e)))
|
2008-12-29 17:29:49 +01:00
|
|
|
g_error("Failed to spawn update task: %s", e->message);
|
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
|
|
|
|
directory_update_init(char *path)
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2008-12-28 22:09:42 +01:00
|
|
|
assert(g_thread_self() == main_task);
|
2008-10-08 10:48:48 +02:00
|
|
|
|
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-12-29 17:29:49 +01:00
|
|
|
g_debug("removing: %s", 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-12-28 22:09:42 +01:00
|
|
|
g_thread_join(update_thr);
|
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;
|
|
|
|
}
|
|
|
|
}
|
2008-11-27 19:34:54 +01:00
|
|
|
|
|
|
|
void update_global_init(void)
|
|
|
|
{
|
2008-11-28 10:57:39 +01:00
|
|
|
follow_inside_symlinks =
|
|
|
|
config_get_bool(CONF_FOLLOW_INSIDE_SYMLINKS,
|
|
|
|
DEFAULT_FOLLOW_INSIDE_SYMLINKS);
|
|
|
|
|
|
|
|
follow_outside_symlinks =
|
|
|
|
config_get_bool(CONF_FOLLOW_OUTSIDE_SYMLINKS,
|
|
|
|
DEFAULT_FOLLOW_OUTSIDE_SYMLINKS);
|
2008-12-28 22:09:42 +01:00
|
|
|
|
|
|
|
cond_init(&delete_cond);
|
2008-11-27 19:34:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void update_global_finish(void)
|
|
|
|
{
|
2008-12-28 22:09:42 +01:00
|
|
|
cond_destroy(&delete_cond);
|
2008-11-27 19:34:54 +01:00
|
|
|
}
|