aboutsummaryrefslogtreecommitdiffstats
path: root/src/update.c
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-10-09 19:41:58 +0200
committerEric Wong <normalperson@yhbt.net>2008-10-11 23:33:42 -0700
commite71207440ebdf492e00fd5ac73b0833c82366ac6 (patch)
tree08483df3b1f9f796d8888b3f04e1a49f514b4ce5 /src/update.c
parent9ae9c1f8cff904cdc2451ddbee88a30e0570d2f5 (diff)
downloadmpd-e71207440ebdf492e00fd5ac73b0833c82366ac6.tar.gz
mpd-e71207440ebdf492e00fd5ac73b0833c82366ac6.tar.xz
mpd-e71207440ebdf492e00fd5ac73b0833c82366ac6.zip
update: replaced update_return with global "modified" flag
There is only once update thread at a time. Make the "modified" flag global and remove the return values of most functions. Propagating an error is only useful for updateDirectory(), since updateInDirectory() will delete failed subdirectories.
Diffstat (limited to 'src/update.c')
-rw-r--r--src/update.c107
1 files changed, 39 insertions, 68 deletions
diff --git a/src/update.c b/src/update.c
index f0905b5db..928aac5c5 100644
--- a/src/update.c
+++ b/src/update.c
@@ -28,18 +28,14 @@
#include "condition.h"
#include "update.h"
-enum update_return {
- UPDATE_RETURN_ERROR = -1,
- UPDATE_RETURN_NOUPDATE = 0,
- UPDATE_RETURN_UPDATED = 1
-};
-
static enum update_progress {
UPDATE_PROGRESS_IDLE = 0,
UPDATE_PROGRESS_RUNNING = 1,
UPDATE_PROGRESS_DONE = 2
} progress;
+static int modified;
+
/* make this dynamic?, or maybe this is big enough... */
static char *update_paths[32];
static size_t update_paths_nr;
@@ -122,7 +118,6 @@ static void delete_directory(struct directory *directory)
struct delete_data {
char *tmp;
struct directory *dir;
- enum update_return ret;
};
/* passed to songvec_for_each */
@@ -135,30 +130,29 @@ static int delete_song_if_removed(struct mpd_song *song, void *_data)
if (!isFile(data->tmp, NULL)) {
delete_song(data->dir, song);
- data->ret = UPDATE_RETURN_UPDATED;
+ modified = 1;
}
return 0;
}
-static enum update_return delete_path(const char *path)
+static void delete_path(const char *path)
{
struct directory *directory = db_get_directory(path);
struct mpd_song *song = db_get_song(path);
- if (directory)
+ if (directory) {
delete_directory(directory);
- if (song)
+ modified = 1;
+ }
+ if (song) {
delete_song(song->parent, song);
-
- return directory == NULL && song == NULL
- ? UPDATE_RETURN_NOUPDATE
- : UPDATE_RETURN_UPDATED;
+ modified = 1;
+ }
}
-static enum update_return
+static void
removeDeletedFromDirectory(char *path_max_tmp, struct directory *directory)
{
- enum update_return ret = UPDATE_RETURN_NOUPDATE;
int i;
struct dirvec *dv = &directory->children;
struct delete_data data;
@@ -168,15 +162,12 @@ removeDeletedFromDirectory(char *path_max_tmp, struct directory *directory)
continue;
LOG("removing directory: %s\n", dv->base[i]->path);
dirvec_delete(dv, dv->base[i]);
- ret = UPDATE_RETURN_UPDATED;
+ modified = 1;
}
data.dir = directory;
data.tmp = path_max_tmp;
- data.ret = ret;
songvec_for_each(&directory->songs, delete_song_if_removed, &data);
-
- return data.ret;
}
static const char *opendir_path(char *path_max_tmp, const char *dirname)
@@ -215,10 +206,9 @@ inodeFoundInParent(struct directory *parent, ino_t inode, dev_t device)
return 0;
}
-static enum update_return
-updateDirectory(struct directory *directory, const struct stat *st);
+static int updateDirectory(struct directory *directory, const struct stat *st);
-static enum update_return
+static void
updateInDirectory(struct directory *directory,
const char *name, const struct stat *st)
{
@@ -228,38 +218,31 @@ updateInDirectory(struct directory *directory,
if (!(song = songvec_find(&directory->songs, shortname))) {
if (!(song = song_file_load(shortname, directory)))
- return -1;
+ return;
songvec_add(&directory->songs, song);
+ modified = 1;
LOG("added %s\n", name);
- return UPDATE_RETURN_UPDATED;
} else if (st->st_mtime != song->mtime) {
LOG("updating %s\n", name);
if (!song_file_update(song))
delete_song(directory, song);
- return UPDATE_RETURN_UPDATED;
+ modified = 1;
}
-
- return UPDATE_RETURN_NOUPDATE;
} else if (S_ISDIR(st->st_mode)) {
struct directory *subdir;
- enum update_return ret;
if (inodeFoundInParent(directory, st->st_ino, st->st_dev))
- return UPDATE_RETURN_ERROR;
+ return;
if (!(subdir = directory_get_child(directory, name)))
subdir = directory_new_child(directory, name);
assert(directory == subdir->parent);
- ret = updateDirectory(subdir, st);
- if (ret == UPDATE_RETURN_ERROR || directory_is_empty(subdir))
+ if (!updateDirectory(subdir, st))
delete_directory(subdir);
-
- return ret;
} else {
DEBUG("update: %s is not a directory or music\n", name);
- return UPDATE_RETURN_NOUPDATE;
}
}
@@ -269,26 +252,21 @@ static int skip_path(const char *path)
return (path[0] == '.' || strchr(path, '\n')) ? 1 : 0;
}
-static enum update_return
-updateDirectory(struct directory *directory, const struct stat *st)
+static int updateDirectory(struct directory *directory, const struct stat *st)
{
DIR *dir;
const char *dirname = directory_get_path(directory);
struct dirent *ent;
char path_max_tmp[MPD_PATH_MAX];
- enum update_return ret = UPDATE_RETURN_NOUPDATE;
- enum update_return ret2;
assert(S_ISDIR(st->st_mode));
directory_set_stat(directory, st);
- dir = opendir(opendir_path(path_max_tmp, dirname));
- if (!dir)
- return UPDATE_RETURN_ERROR;
+ if (!(dir = opendir(opendir_path(path_max_tmp, dirname))))
+ return 0;
- if (removeDeletedFromDirectory(path_max_tmp, directory) > 0)
- ret = UPDATE_RETURN_UPDATED;
+ removeDeletedFromDirectory(path_max_tmp, directory);
while ((ent = readdir(dir))) {
char *utf8;
@@ -306,16 +284,14 @@ updateDirectory(struct directory *directory, const struct stat *st)
dirname, strlen(dirname));
if (myStat(path_max_tmp, &st2) == 0)
- ret2 = updateInDirectory(directory, path_max_tmp, &st2);
+ updateInDirectory(directory, path_max_tmp, &st2);
else
- ret2 = delete_path(path_max_tmp);
- if (ret == UPDATE_RETURN_NOUPDATE)
- ret = ret2;
+ delete_path(path_max_tmp);
}
closedir(dir);
- return ret;
+ return 1;
}
static struct directory *
@@ -363,37 +339,34 @@ addParentPathToDB(const char *utf8path)
return directory;
}
-static enum update_return updatePath(const char *utf8path)
+static void updatePath(const char *utf8path)
{
struct stat st;
if (myStat(utf8path, &st) < 0)
- return delete_path(utf8path);
- return updateInDirectory(addParentPathToDB(utf8path), utf8path, &st);
+ delete_path(utf8path);
+ else
+ updateInDirectory(addParentPathToDB(utf8path), utf8path, &st);
}
static void * update_task(void *_path)
{
- enum update_return ret = UPDATE_RETURN_NOUPDATE;
-
if (_path != NULL && !isRootDirectory(_path)) {
- ret = updatePath((char *)_path);
+ updatePath((char *)_path);
free(_path);
} else {
struct directory *directory = db_get_root();
struct stat st;
if (myStat(directory_get_path(directory), &st) == 0)
- ret = updateDirectory(directory, &st);
- else
- ret = UPDATE_RETURN_ERROR;
+ updateDirectory(directory, &st);
}
- if (ret == UPDATE_RETURN_UPDATED && db_save() < 0)
- ret = UPDATE_RETURN_ERROR;
+ if (modified)
+ db_save();
progress = UPDATE_PROGRESS_DONE;
wakeup_main_task();
- return (void *)ret;
+ return NULL;
}
static void spawn_update_task(char *path)
@@ -403,6 +376,7 @@ static void spawn_update_task(char *path)
assert(pthread_equal(pthread_self(), main_task));
progress = UPDATE_PROGRESS_RUNNING;
+ modified = 0;
pthread_attr_init(&attr);
if (pthread_create(&update_thr, &attr, update_task, path))
FATAL("Failed to spawn update task: %s\n", strerror(errno));
@@ -437,9 +411,6 @@ unsigned directory_update_init(char *path)
void reap_update_task(void)
{
- void *thread_return;
- enum update_return ret;
-
assert(pthread_equal(pthread_self(), main_task));
if (progress == UPDATE_PROGRESS_IDLE)
@@ -457,10 +428,10 @@ void reap_update_task(void)
if (progress != UPDATE_PROGRESS_DONE)
return;
- if (pthread_join(update_thr, &thread_return))
+ if (pthread_join(update_thr, NULL))
FATAL("error joining update thread: %s\n", strerror(errno));
- ret = (enum update_return)(size_t)thread_return;
- if (ret == UPDATE_RETURN_UPDATED)
+
+ if (modified)
playlistVersionChange();
if (update_paths_nr) {