diff options
author | Max Kellermann <max@duempel.org> | 2011-09-05 23:03:05 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2011-09-10 07:58:43 +0200 |
commit | 7819aa6b2060b600b9ec2471f250038eeebae523 (patch) | |
tree | a1523f90ee693f5adba207c208bea81b8466e3b9 /src/database.c | |
parent | 7cc6b63aac5a355bc2dd216d41ce4dc0e0fc3066 (diff) | |
download | mpd-7819aa6b2060b600b9ec2471f250038eeebae523.tar.gz mpd-7819aa6b2060b600b9ec2471f250038eeebae523.tar.xz mpd-7819aa6b2060b600b9ec2471f250038eeebae523.zip |
db_plugin: introducing a plugin API for the song database
First draft, not really pluggable currently - hard-coded to use the
"simple" plugin, and calls several of its internal functions.
The API is very simple currently, all searches are still performed
over the root "directory" object. Future changes to the API will move
those search implementations into the plugin, to allow more efficient
implementations, or implementations that don't have the whole tree in
memory all the time.
Diffstat (limited to '')
-rw-r--r-- | src/database.c | 199 |
1 files changed, 40 insertions, 159 deletions
diff --git a/src/database.c b/src/database.c index a892452ed..ea7f01eb1 100644 --- a/src/database.c +++ b/src/database.c @@ -20,8 +20,11 @@ #include "config.h" #include "database.h" #include "db_save.h" +#include "db_plugin.h" +#include "db/simple_db_plugin.h" #include "directory.h" #include "stats.h" +#include "conf.h" #include <glib.h> @@ -35,11 +38,8 @@ #undef G_LOG_DOMAIN #define G_LOG_DOMAIN "database" -static char *database_path; - -static struct directory *music_root; - -static time_t database_mtime; +static struct db *db; +static bool db_is_open; /** * The quark used for GError.domain. @@ -50,49 +50,50 @@ db_quark(void) return g_quark_from_static_string("database"); } -void -db_init(const char *path) +bool +db_init(const struct config_param *path, GError **error_r) { - database_path = g_strdup(path); + assert(db == NULL); + assert(!db_is_open); - if (path != NULL) - music_root = directory_new("", NULL); -} + if (path == NULL) + return true; -void -db_finish(void) -{ - assert((database_path == NULL) == (music_root == NULL)); + struct config_param *param = config_new_param("database", path->line); + config_add_block_param(param, "path", path->value, path->line); + + db = db_plugin_new(&simple_db_plugin, param, error_r); - if (music_root != NULL) - directory_free(music_root); + config_param_free(param); - g_free(database_path); + return db != NULL; } void -db_clear(void) +db_finish(void) { - assert(music_root != NULL); + if (db_is_open) + db_plugin_close(db); - directory_free(music_root); - music_root = directory_new("", NULL); + if (db != NULL) + db_plugin_free(db); } struct directory * db_get_root(void) { - assert(music_root != NULL); + assert(db != NULL); - return music_root; + return simple_db_get_root(db); } struct directory * db_get_directory(const char *name) { - if (music_root == NULL) + if (db == NULL) return NULL; + struct directory *music_root = db_get_root(); if (name == NULL) return music_root; @@ -106,9 +107,10 @@ db_get_song(const char *file) g_debug("get song: %s", file); - if (music_root == NULL) + if (db == NULL) return NULL; + struct directory *music_root = db_get_root(); return directory_lookup_song(music_root, file); } @@ -119,7 +121,7 @@ db_walk(const char *name, { struct directory *directory; - if (music_root == NULL) + if (db == NULL) return -1; if ((directory = db_get_directory(name)) == NULL) { @@ -134,156 +136,35 @@ db_walk(const char *name, } bool -db_check(GError **error_r) -{ - struct stat st; - - assert(database_path != NULL); - - /* Check if the file exists */ - if (access(database_path, F_OK)) { - /* If the file doesn't exist, we can't check if we can write - * it, so we are going to try to get the directory path, and - * see if we can write a file in that */ - char *dirPath = g_path_get_dirname(database_path); - - /* Check that the parent part of the path is a directory */ - if (stat(dirPath, &st) < 0) { - g_free(dirPath); - g_set_error(error_r, db_quark(), errno, - "Couldn't stat parent directory of db file " - "\"%s\": %s", - database_path, g_strerror(errno)); - return false; - } - - if (!S_ISDIR(st.st_mode)) { - g_free(dirPath); - g_set_error(error_r, db_quark(), 0, - "Couldn't create db file \"%s\" because the " - "parent path is not a directory", - database_path); - return false; - } - - /* Check if we can write to the directory */ - if (access(dirPath, X_OK | W_OK)) { - g_set_error(error_r, db_quark(), errno, - "Can't create db file in \"%s\": %s", - dirPath, g_strerror(errno)); - g_free(dirPath); - return false; - } - - g_free(dirPath); - - return true; - } - - /* Path exists, now check if it's a regular file */ - if (stat(database_path, &st) < 0) { - g_set_error(error_r, db_quark(), errno, - "Couldn't stat db file \"%s\": %s", - database_path, g_strerror(errno)); - return false; - } - - if (!S_ISREG(st.st_mode)) { - g_set_error(error_r, db_quark(), 0, - "db file \"%s\" is not a regular file", - database_path); - return false; - } - - /* And check that we can write to it */ - if (access(database_path, R_OK | W_OK)) { - g_set_error(error_r, db_quark(), errno, - "Can't open db file \"%s\" for reading/writing: %s", - database_path, g_strerror(errno)); - return false; - } - - return true; -} - -bool db_save(GError **error_r) { - FILE *fp; - struct stat st; - - assert(database_path != NULL); - assert(music_root != NULL); - - g_debug("removing empty directories from DB"); - directory_prune_empty(music_root); - - g_debug("sorting DB"); - - directory_sort(music_root); - - g_debug("writing DB"); - - fp = fopen(database_path, "w"); - if (!fp) { - g_set_error(error_r, db_quark(), errno, - "unable to write to db file \"%s\": %s", - database_path, g_strerror(errno)); - return false; - } + assert(db != NULL); + assert(db_is_open); - db_save_internal(fp, music_root); - - if (ferror(fp)) { - g_set_error(error_r, db_quark(), errno, - "Failed to write to database file: %s", - g_strerror(errno)); - fclose(fp); - return false; - } - - fclose(fp); - - if (stat(database_path, &st) == 0) - database_mtime = st.st_mtime; - - return true; + return simple_db_save(db, error_r); } bool db_load(GError **error) { - FILE *fp = NULL; - struct stat st; + assert(db != NULL); + assert(!db_is_open); - assert(database_path != NULL); - assert(music_root != NULL); - - fp = fopen(database_path, "r"); - if (fp == NULL) { - g_set_error(error, db_quark(), errno, - "Failed to open database file \"%s\": %s", - database_path, strerror(errno)); + if (!db_plugin_open(db, error)) return false; - } - if (!db_load_internal(fp, music_root, error)) { - fclose(fp); - return false; - } - - fclose(fp); + db_is_open = true; stats_update(); - if (stat(database_path, &st) == 0) - database_mtime = st.st_mtime; - return true; } time_t db_get_mtime(void) { - return database_mtime; + assert(db != NULL); + assert(db_is_open); + + return simple_db_get_mtime(db); } |