diff options
Diffstat (limited to '')
-rw-r--r-- | src/database.c | 28 | ||||
-rw-r--r-- | src/directory.c | 15 | ||||
-rw-r--r-- | src/directory.h | 2 | ||||
-rw-r--r-- | src/update.c | 10 |
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) |