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

#include "config.h"
21
#include "SimpleDatabasePlugin.hxx"
22
#include "DatabaseSelection.hxx"
23
#include "DatabaseHelpers.hxx"
24
#include "Directory.hxx"
25
#include "SongFilter.hxx"
26
#include "DatabaseSave.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "DatabaseLock.hxx"
28
#include "DatabaseError.hxx"
29
#include "TextFile.hxx"
30
#include "ConfigData.hxx"
31
#include "fs/FileSystem.hxx"
32 33
#include "util/Error.hxx"
#include "util/Domain.hxx"
34
#include "Log.hxx"
35

36 37
#include <errno.h>

38
static constexpr Domain simple_db_domain("simple_db");
39

40
Database *
41
SimpleDatabase::Create(const config_param &param, Error &error)
42
{
43
	SimpleDatabase *db = new SimpleDatabase();
44
	if (!db->Configure(param, error)) {
45
		delete db;
46
		db = nullptr;
47
	}
48

49
	return db;
50 51
}

52
bool
53
SimpleDatabase::Configure(const config_param &param, Error &error)
54
{
55
	path = param.GetBlockPath("path", error);
56
	if (path.IsNull()) {
57 58 59
		if (!error.IsDefined())
			error.Set(simple_db_domain,
				  "No \"path\" parameter specified");
60
		return false;
61 62
	}

63
	path_utf8 = path.ToUTF8();
64

65
	return true;
66 67
}

68
bool
69
SimpleDatabase::Check(Error &error) const
70
{
71
	assert(!path.IsNull());
72 73

	/* Check if the file exists */
74
	if (!CheckAccess(path, F_OK)) {
75 76 77
		/* 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 */
78
		const auto dirPath = path.GetDirectoryName();
79 80 81

		/* Check that the parent part of the path is a directory */
		struct stat st;
82
		if (!StatFile(dirPath, st)) {
83 84 85
			error.FormatErrno("Couldn't stat parent directory of db file "
					  "\"%s\"",
					  path_utf8.c_str());
86 87 88 89
			return false;
		}

		if (!S_ISDIR(st.st_mode)) {
90 91 92 93
			error.Format(simple_db_domain,
				     "Couldn't create db file \"%s\" because the "
				     "parent path is not a directory",
				     path_utf8.c_str());
94 95 96 97
			return false;
		}

		/* Check if we can write to the directory */
98
		if (!CheckAccess(dirPath, X_OK | W_OK)) {
99
			const int e = errno;
100
			const std::string dirPath_utf8 = dirPath.ToUTF8();
101 102
			error.FormatErrno(e, "Can't create db file in \"%s\"",
					  dirPath_utf8.c_str());
103 104 105 106 107 108 109 110
			return false;
		}

		return true;
	}

	/* Path exists, now check if it's a regular file */
	struct stat st;
111
	if (!StatFile(path, st)) {
112 113
		error.FormatErrno("Couldn't stat db file \"%s\"",
				  path_utf8.c_str());
114 115 116 117
		return false;
	}

	if (!S_ISREG(st.st_mode)) {
118 119 120
		error.Format(simple_db_domain,
			     "db file \"%s\" is not a regular file",
			     path_utf8.c_str());
121 122 123 124
		return false;
	}

	/* And check that we can write to it */
125
	if (!CheckAccess(path, R_OK | W_OK)) {
126 127
		error.FormatErrno("Can't open db file \"%s\" for reading/writing",
				  path_utf8.c_str());
128 129 130 131 132 133
		return false;
	}

	return true;
}

134
bool
135
SimpleDatabase::Load(Error &error)
136
{
137
	assert(!path.IsNull());
138
	assert(root != nullptr);
139

140
	TextFile file(path);
141
	if (file.HasFailed()) {
142 143
		error.FormatErrno("Failed to open database file \"%s\"",
				  path_utf8.c_str());
144 145 146
		return false;
	}

147
	if (!db_load_internal(file, *root, error))
148 149 150
		return false;

	struct stat st;
151
	if (StatFile(path, st))
152
		mtime = st.st_mtime;
153 154 155 156

	return true;
}

157
bool
158
SimpleDatabase::Open(Error &error)
159
{
160
	root = Directory::NewRoot();
161
	mtime = 0;
162

163 164 165 166
#ifndef NDEBUG
	borrowed_song_count = 0;
#endif

167
	if (!Load(error)) {
168
		root->Free();
169

170
		LogError(error);
171
		error.Clear();
172

173
		if (!Check(error))
174 175
			return false;

176
		root = Directory::NewRoot();
177 178 179 180 181
	}

	return true;
}

182 183
void
SimpleDatabase::Close()
184
{
185
	assert(root != nullptr);
186
	assert(borrowed_song_count == 0);
187

188
	root->Free();
189 190
}

191
Song *
192
SimpleDatabase::GetSong(const char *uri, Error &error) const
193
{
194
	assert(root != nullptr);
195

196
	db_lock();
197
	Song *song = root->LookupSong(uri);
198
	db_unlock();
199
	if (song == nullptr)
200 201
		error.Format(db_domain, DB_NOT_FOUND,
			     "No such song: %s", uri);
202 203 204 205
#ifndef NDEBUG
	else
		++const_cast<unsigned &>(borrowed_song_count);
#endif
206 207 208 209

	return song;
}

210
void
211
SimpleDatabase::ReturnSong(gcc_unused Song *song) const
212 213 214 215 216 217 218 219 220
{
	assert(song != nullptr);

#ifndef NDEBUG
	assert(borrowed_song_count > 0);
	--const_cast<unsigned &>(borrowed_song_count);
#endif
}

221
gcc_pure
222
const Directory *
223 224
SimpleDatabase::LookupDirectory(const char *uri) const
{
225 226
	assert(root != nullptr);
	assert(uri != nullptr);
227

228
	ScopeDatabaseLock protect;
229
	return root->LookupDirectory(uri);
230 231 232
}

bool
233
SimpleDatabase::Visit(const DatabaseSelection &selection,
234 235 236
		      VisitDirectory visit_directory,
		      VisitSong visit_song,
		      VisitPlaylist visit_playlist,
237
		      Error &error) const
238
{
239 240
	ScopeDatabaseLock protect;

241
	const Directory *directory = root->LookupDirectory(selection.uri.c_str());
242
	if (directory == nullptr) {
243
		if (visit_song) {
244
			Song *song = root->LookupSong(selection.uri.c_str());
245 246
			if (song != nullptr)
				return !selection.Match(*song) ||
247
					visit_song(*song, error);
248
		}
249

250
		error.Set(db_domain, DB_NOT_FOUND, "No such directory");
251 252 253
		return false;
	}

254
	if (selection.recursive && visit_directory &&
255
	    !visit_directory(*directory, error))
256 257
		return false;

258 259
	return directory->Walk(selection.recursive, selection.filter,
			       visit_directory, visit_song, visit_playlist,
260
			       error);
261 262
}

263 264
bool
SimpleDatabase::VisitUniqueTags(const DatabaseSelection &selection,
265
				TagType tag_type,
266
				VisitString visit_string,
267
				Error &error) const
268 269
{
	return ::VisitUniqueTags(*this, selection, tag_type, visit_string,
270
				 error);
271 272
}

273 274
bool
SimpleDatabase::GetStats(const DatabaseSelection &selection,
275
			 DatabaseStats &stats, Error &error) const
276
{
277
	return ::GetStats(*this, selection, stats, error);
278 279
}

280
bool
281
SimpleDatabase::Save(Error &error)
282
{
283 284
	db_lock();

285
	LogDebug(simple_db_domain, "removing empty directories from DB");
286
	root->PruneEmpty();
287

288
	LogDebug(simple_db_domain, "sorting DB");
289
	root->Sort();
290

291 292
	db_unlock();

293
	LogDebug(simple_db_domain, "writing DB");
294

295
	FILE *fp = FOpen(path, FOpenMode::WriteText);
296
	if (!fp) {
297 298
		error.FormatErrno("unable to write to db file \"%s\"",
				  path_utf8.c_str());
299 300 301
		return false;
	}

302
	db_save_internal(fp, *root);
303 304

	if (ferror(fp)) {
305
		error.SetErrno("Failed to write to database file");
306 307 308 309 310 311 312
		fclose(fp);
		return false;
	}

	fclose(fp);

	struct stat st;
313
	if (StatFile(path, st))
314
		mtime = st.st_mtime;
315 316 317 318

	return true;
}

319 320 321 322
const DatabasePlugin simple_db_plugin = {
	"simple",
	SimpleDatabase::Create,
};