2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-10-08 11:07:35 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-10-08 11:07:35 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2008-10-08 11:07:35 +02:00
|
|
|
#include "database.h"
|
2011-09-10 19:24:30 +02:00
|
|
|
#include "db_error.h"
|
2011-09-10 07:33:13 +02:00
|
|
|
#include "db_save.h"
|
2011-09-10 19:24:30 +02:00
|
|
|
#include "db_visitor.h"
|
2011-09-05 23:03:05 +02:00
|
|
|
#include "db_plugin.h"
|
|
|
|
#include "db/simple_db_plugin.h"
|
2008-10-08 11:07:35 +02:00
|
|
|
#include "directory.h"
|
|
|
|
#include "stats.h"
|
2011-09-05 23:03:05 +02:00
|
|
|
#include "conf.h"
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2008-10-08 11:07:35 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2009-01-02 17:22:54 +01:00
|
|
|
#include <errno.h>
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2008-12-29 17:28:37 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "database"
|
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
static struct db *db;
|
|
|
|
static bool db_is_open;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
bool
|
|
|
|
db_init(const struct config_param *path, GError **error_r)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db == NULL);
|
|
|
|
assert(!db_is_open);
|
2009-01-18 16:09:01 +01:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
if (path == NULL)
|
|
|
|
return true;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
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);
|
2009-01-18 16:56:07 +01:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
config_param_free(param);
|
2009-01-18 16:09:01 +01:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
return db != NULL;
|
2009-01-18 16:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-05 23:03:05 +02:00
|
|
|
db_finish(void)
|
2009-01-18 16:09:01 +01:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
if (db_is_open)
|
|
|
|
db_plugin_close(db);
|
2009-01-18 16:56:07 +01:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
if (db != NULL)
|
|
|
|
db_plugin_free(db);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct directory *
|
2008-10-08 11:07:55 +02:00
|
|
|
db_get_root(void)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db != NULL);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
return simple_db_get_root(db);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct directory *
|
2008-10-08 11:07:55 +02:00
|
|
|
db_get_directory(const char *name)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
if (db == NULL)
|
2009-01-18 16:56:07 +01:00
|
|
|
return NULL;
|
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
struct directory *music_root = db_get_root();
|
2008-10-08 11:07:35 +02:00
|
|
|
if (name == NULL)
|
|
|
|
return music_root;
|
|
|
|
|
2009-04-01 18:41:33 +02:00
|
|
|
return directory_lookup_directory(music_root, name);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct song *
|
2008-10-09 16:26:09 +02:00
|
|
|
db_get_song(const char *file)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2009-01-18 16:56:07 +01:00
|
|
|
assert(file != NULL);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2008-12-29 17:28:37 +01:00
|
|
|
g_debug("get song: %s", file);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
if (db == NULL)
|
2009-01-18 16:56:07 +01:00
|
|
|
return NULL;
|
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
struct directory *music_root = db_get_root();
|
2009-04-01 18:41:37 +02:00
|
|
|
return directory_lookup_song(music_root, file);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2011-09-10 19:24:30 +02:00
|
|
|
bool
|
|
|
|
db_walk(const char *uri,
|
|
|
|
const struct db_visitor *visitor, void *ctx,
|
|
|
|
GError **error_r)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
|
|
|
struct directory *directory;
|
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
if (db == NULL)
|
2009-01-18 16:56:07 +01:00
|
|
|
return -1;
|
|
|
|
|
2011-09-10 19:24:30 +02:00
|
|
|
if ((directory = db_get_directory(uri)) == NULL) {
|
2008-10-08 11:07:35 +02:00
|
|
|
struct song *song;
|
2011-09-10 19:24:30 +02:00
|
|
|
if (visitor->song != NULL &&
|
|
|
|
(song = db_get_song(uri)) != NULL)
|
|
|
|
return visitor->song(song, ctx, error_r);
|
|
|
|
|
|
|
|
g_set_error(error_r, db_quark(), DB_NOT_FOUND,
|
|
|
|
"No such directory: %s", uri);
|
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2011-09-13 20:38:12 +02:00
|
|
|
if (visitor->directory != NULL &&
|
|
|
|
!visitor->directory(directory, ctx, error_r))
|
|
|
|
return false;
|
|
|
|
|
2011-09-10 19:24:30 +02:00
|
|
|
return directory_walk(directory, visitor, ctx, error_r);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
bool
|
2011-09-09 23:28:27 +02:00
|
|
|
db_save(GError **error_r)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db != NULL);
|
|
|
|
assert(db_is_open);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
return simple_db_save(db, error_r);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
bool
|
2009-03-02 15:42:21 +01:00
|
|
|
db_load(GError **error)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db != NULL);
|
|
|
|
assert(!db_is_open);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
if (!db_plugin_open(db, error))
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
db_is_open = true;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2009-01-04 20:57:06 +01:00
|
|
|
stats_update();
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
return true;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:07:55 +02:00
|
|
|
time_t
|
|
|
|
db_get_mtime(void)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2011-09-05 23:03:05 +02:00
|
|
|
assert(db != NULL);
|
|
|
|
assert(db_is_open);
|
|
|
|
|
|
|
|
return simple_db_get_mtime(db);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|