DatabaseGlue.cxx 3.03 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "DatabaseGlue.hxx"
22
#include "DatabaseSimple.hxx"
23
#include "DatabaseRegistry.hxx"
24
#include "DatabaseSave.hxx"
25
#include "DatabaseError.hxx"
26
#include "Directory.hxx"
27
#include "util/Error.hxx"
28
#include "ConfigData.hxx"
29
#include "Stats.hxx"
30 31
#include "DatabasePlugin.hxx"
#include "db/SimpleDatabasePlugin.hxx"
32

Max Kellermann's avatar
Max Kellermann committed
33 34 35
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
36 37
#include <assert.h>
#include <string.h>
38
#include <errno.h>
39

40

41
static Database *db;
42
static bool db_is_open;
43
static bool is_simple;
44

45
bool
46
DatabaseGlobalInit(const config_param &param, Error &error)
47
{
48
	assert(db == nullptr);
49
	assert(!db_is_open);
50

51
	const char *plugin_name =
52
		param.GetBlockValue("plugin", "simple");
53 54
	is_simple = strcmp(plugin_name, "simple") == 0;

55
	const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name);
56
	if (plugin == nullptr) {
57 58
		error.Format(db_domain,
			     "No such database plugin: %s", plugin_name);
59 60 61
		return false;
	}

62
	db = plugin->create(param, error);
63
	return db != nullptr;
64 65 66
}

void
67
DatabaseGlobalDeinit(void)
68
{
69
	if (db_is_open)
70
		db->Close();
71

72
	if (db != nullptr)
73
		delete db;
74 75
}

76 77 78
const Database *
GetDatabase()
{
79
	assert(db == nullptr || db_is_open);
80 81 82 83

	return db;
}

84
const Database *
85
GetDatabase(Error &error)
86 87 88 89
{
	assert(db == nullptr || db_is_open);

	if (db == nullptr)
90
		error.Set(db_domain, DB_DISABLED, "No database");
91 92 93 94

	return db;
}

95 96 97
bool
db_is_simple(void)
{
98
	assert(db == nullptr || db_is_open);
99

100
	return is_simple;
101 102
}

103
Directory *
104
db_get_root(void)
105
{
106
	assert(db != nullptr);
107
	assert(db_is_simple());
108

109
	return ((SimpleDatabase *)db)->GetRoot();
110 111
}

112
Directory *
113
db_get_directory(const char *name)
114
{
115 116
	if (db == nullptr)
		return nullptr;
117

118
	Directory *music_root = db_get_root();
119
	if (name == nullptr)
120 121
		return music_root;

122
	return music_root->LookupDirectory(name);
123 124
}

Max Kellermann's avatar
Max Kellermann committed
125
bool
126
db_save(Error &error)
127
{
128
	assert(db != nullptr);
129
	assert(db_is_open);
130
	assert(db_is_simple());
131

132
	return ((SimpleDatabase *)db)->Save(error);
133 134
}

Max Kellermann's avatar
Max Kellermann committed
135
bool
136
DatabaseGlobalOpen(Error &error)
137
{
138
	assert(db != nullptr);
139
	assert(!db_is_open);
140

141
	if (!db->Open(error))
Max Kellermann's avatar
Max Kellermann committed
142
		return false;
143

144
	db_is_open = true;
145

146
	stats_update();
147

Max Kellermann's avatar
Max Kellermann committed
148
	return true;
149 150
}

151 152
bool
db_exists()
153
{
154
	assert(db != nullptr);
155
	assert(db_is_open);
156
	assert(db_is_simple());
157

158
	return ((SimpleDatabase *)db)->GetUpdateStamp() > 0;
159
}