aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2008-10-11 20:41:55 -0700
committerEric Wong <normalperson@yhbt.net>2008-10-12 05:27:32 -0700
commite28fb29349a811f0f306e9ac4bedf6013417d043 (patch)
treec28a33e1361a4a507dc2c401f4cce26eda7cd9aa
parentc6ee14dca5d7336dad20a6057aa2a8006d4447b8 (diff)
downloadmpd-e28fb29349a811f0f306e9ac4bedf6013417d043.tar.gz
mpd-e28fb29349a811f0f306e9ac4bedf6013417d043.tar.xz
mpd-e28fb29349a811f0f306e9ac4bedf6013417d043.zip
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</highlander>.
-rw-r--r--src/database.c28
-rw-r--r--src/directory.c15
-rw-r--r--src/directory.h2
-rw-r--r--src/update.c10
4 files changed, 21 insertions, 34 deletions
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);
diff --git a/src/directory.c b/src/directory.c
index 699b6fbc1..846f2510a 100644
--- a/src/directory.c
+++ b/src/directory.c
@@ -23,14 +23,18 @@
#include "myfprintf.h"
#include "dirvec.h"
+struct directory music_root;
+
struct directory * directory_new(const char *path, struct directory * parent)
{
struct directory *dir;
- size_t pathlen = strlen(path);
+ size_t pathlen;
- assert(path != NULL);
- assert((*path == 0) == (parent == NULL));
+ assert(path);
+ assert(*path);
+ assert(parent);
+ pathlen = strlen(path);
dir = xcalloc(1, sizeof(*dir) - sizeof(dir->path) + pathlen + 1);
memcpy(dir->path, path, pathlen + 1);
dir->parent = parent;
@@ -42,9 +46,8 @@ void directory_free(struct directory *dir)
{
dirvec_destroy(&dir->children);
songvec_destroy(&dir->songs);
- free(dir);
- /* this resets last dir returned */
- /*directory_get_path(NULL); */
+ if (dir != &music_root)
+ free(dir);
}
void directory_prune_empty(struct directory *dir)
diff --git a/src/directory.h b/src/directory.h
index 546e55183..9c3063d75 100644
--- a/src/directory.h
+++ b/src/directory.h
@@ -43,6 +43,8 @@ struct directory {
char path[mpd_sizeof_str_flex_array];
} mpd_packed;
+extern struct directory music_root;
+
static inline int isRootDirectory(const char *name)
{
/* TODO: verify and remove !name check */
diff --git a/src/update.c b/src/update.c
index ca3640b73..3f6fa15ed 100644
--- a/src/update.c
+++ b/src/update.c
@@ -317,10 +317,9 @@ directory_make_child_checked(struct directory *parent, const char *path)
return dir;
}
-static struct directory *
-addParentPathToDB(const char *utf8path)
+static struct directory * addParentPathToDB(const char *utf8path)
{
- struct directory *dir = db_get_root();
+ struct directory *dir = &music_root;
char *duplicated = xstrdup(utf8path);
char *slash = duplicated;
@@ -354,11 +353,10 @@ static void * update_task(void *_path)
updatePath((char *)_path);
free(_path);
} else {
- struct directory *dir = db_get_root();
struct stat st;
- if (myStat(directory_get_path(dir), &st) == 0)
- updateDirectory(dir, &st);
+ if (myStat(directory_get_path(&music_root), &st) == 0)
+ updateDirectory(&music_root, &st);
}
if (modified)