From e28fb29349a811f0f306e9ac4bedf6013417d043 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 11 Oct 2008 20:41:55 -0700 Subject: directory: make music_root global and avoid runtime initialization mpd can't function without music_root; so don't bother allocating it on the heap nor checking to see if it's initialized. Don't allow directory_new() to create a directory w/o a parent or with an empty path, either: root is root and there can be only one. --- src/database.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) (limited to 'src/database.c') diff --git a/src/database.c b/src/database.c index dde57ce6a..a9e80f425 100644 --- a/src/database.c +++ b/src/database.c @@ -32,14 +32,10 @@ #include "os_compat.h" #include "myfprintf.h" -static struct directory *music_root; - static time_t directory_dbModTime; void db_init(void) { - music_root = directory_new("", NULL); - if (!directory_update_init(NULL)) FATAL("directory update failed\n"); @@ -54,22 +50,12 @@ void db_init(void) void db_finish(void) { - directory_free(music_root); -} - -struct directory * db_get_root(void) -{ - assert(music_root != NULL); - - return music_root; + directory_free(&music_root); } struct directory * db_get_directory(const char *name) { - if (name == NULL) - return music_root; - - return directory_get_subdir(music_root, name); + return name ? directory_get_subdir(&music_root, name) : &music_root; } struct mpd_song *db_get_song(const char *file) @@ -195,11 +181,11 @@ int db_save(void) struct stat st; DEBUG("removing empty directories from DB\n"); - directory_prune_empty(music_root); + directory_prune_empty(&music_root); DEBUG("sorting DB\n"); - directory_sort(music_root); + directory_sort(&music_root); DEBUG("writing DB\n"); @@ -220,7 +206,7 @@ int db_save(void) DIRECTORY_FS_CHARSET "%s\n" DIRECTORY_INFO_END "\n", getFsCharset()); - if (directory_save(fd, music_root) < 0) { + if (directory_save(fd, &music_root) < 0) { ERROR("Failed to write to database file: %s\n", strerror(errno)); xclose(fd); @@ -243,8 +229,6 @@ int db_load(void) int foundFsCharset = 0; int foundVersion = 0; - if (!music_root) - music_root = directory_new("", NULL); while (!(fp = fopen(dbFile, "r")) && errno == EINTR) ; if (fp == NULL) { ERROR("unable to open db file \"%s\": %s\n", @@ -296,7 +280,7 @@ int db_load(void) DEBUG("reading DB\n"); - directory_load(fp, music_root); + directory_load(fp, &music_root); while (fclose(fp) && errno == EINTR) ; stats.numberOfSongs = countSongsIn(NULL); -- cgit v1.2.3