2011-09-05 23:03:05 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
|
|
|
* 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"
|
2012-07-30 07:26:08 +02:00
|
|
|
#include "SimpleDatabasePlugin.hxx"
|
2012-08-07 23:06:41 +02:00
|
|
|
#include "DatabaseSelection.hxx"
|
2012-08-15 21:32:34 +02:00
|
|
|
#include "DatabaseHelpers.hxx"
|
2013-01-02 22:52:08 +01:00
|
|
|
#include "Directory.hxx"
|
2012-08-29 19:12:26 +02:00
|
|
|
#include "SongFilter.hxx"
|
2013-01-02 19:52:57 +01:00
|
|
|
#include "DatabaseSave.hxx"
|
2013-01-02 20:56:21 +01:00
|
|
|
#include "DatabaseLock.hxx"
|
|
|
|
#include "db_error.h"
|
2013-01-03 10:16:05 +01:00
|
|
|
#include "TextFile.hxx"
|
2011-09-05 23:03:05 +02:00
|
|
|
#include "conf.h"
|
2012-07-30 07:26:08 +02:00
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
G_GNUC_CONST
|
|
|
|
static inline GQuark
|
|
|
|
simple_db_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("simple_db");
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
Database *
|
|
|
|
SimpleDatabase::Create(const struct config_param *param, GError **error_r)
|
2011-09-10 18:48:10 +02:00
|
|
|
{
|
2012-07-30 07:26:08 +02:00
|
|
|
SimpleDatabase *db = new SimpleDatabase();
|
|
|
|
if (!db->Configure(param, error_r)) {
|
|
|
|
delete db;
|
|
|
|
db = NULL;
|
|
|
|
}
|
2011-09-10 18:48:10 +02:00
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
return db;
|
2011-09-10 18:48:10 +02:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
bool
|
|
|
|
SimpleDatabase::Configure(const struct config_param *param, GError **error_r)
|
2011-09-05 23:03:05 +02:00
|
|
|
{
|
|
|
|
GError *error = NULL;
|
2012-07-30 07:26:08 +02:00
|
|
|
|
|
|
|
char *_path = config_dup_block_path(param, "path", &error);
|
|
|
|
if (_path == NULL) {
|
2011-09-05 23:03:05 +02:00
|
|
|
if (error != NULL)
|
|
|
|
g_propagate_error(error_r, error);
|
|
|
|
else
|
|
|
|
g_set_error(error_r, simple_db_quark(), 0,
|
|
|
|
"No \"path\" parameter specified");
|
2012-07-30 07:26:08 +02:00
|
|
|
return false;
|
2011-09-05 23:03:05 +02:00
|
|
|
}
|
|
|
|
|
2013-01-17 00:56:57 +01:00
|
|
|
path = Path::FromUTF8(_path);
|
2012-07-30 07:26:08 +02:00
|
|
|
free(_path);
|
2011-09-05 23:03:05 +02:00
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
return true;
|
2011-09-05 23:03:05 +02:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
bool
|
|
|
|
SimpleDatabase::Check(GError **error_r) const
|
2011-09-05 23:03:05 +02:00
|
|
|
{
|
2013-01-17 00:56:57 +01:00
|
|
|
assert(!path.IsNull());
|
2012-07-30 07:26:08 +02:00
|
|
|
assert(!path.empty());
|
2011-09-05 23:03:05 +02:00
|
|
|
|
|
|
|
/* Check if the file exists */
|
2012-07-30 07:26:08 +02:00
|
|
|
if (access(path.c_str(), F_OK)) {
|
2011-09-05 23:03:05 +02:00
|
|
|
/* 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 */
|
2012-07-30 07:26:08 +02:00
|
|
|
char *dirPath = g_path_get_dirname(path.c_str());
|
2011-09-05 23:03:05 +02:00
|
|
|
|
|
|
|
/* Check that the parent part of the path is a directory */
|
|
|
|
struct stat st;
|
|
|
|
if (stat(dirPath, &st) < 0) {
|
|
|
|
g_free(dirPath);
|
|
|
|
g_set_error(error_r, simple_db_quark(), errno,
|
|
|
|
"Couldn't stat parent directory of db file "
|
|
|
|
"\"%s\": %s",
|
2012-07-30 07:26:08 +02:00
|
|
|
path.c_str(), g_strerror(errno));
|
2011-09-05 23:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
|
|
|
g_free(dirPath);
|
|
|
|
g_set_error(error_r, simple_db_quark(), 0,
|
|
|
|
"Couldn't create db file \"%s\" because the "
|
|
|
|
"parent path is not a directory",
|
2012-07-30 07:26:08 +02:00
|
|
|
path.c_str());
|
2011-09-05 23:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we can write to the directory */
|
|
|
|
if (access(dirPath, X_OK | W_OK)) {
|
|
|
|
g_set_error(error_r, simple_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 */
|
|
|
|
struct stat st;
|
2012-07-30 07:26:08 +02:00
|
|
|
if (stat(path.c_str(), &st) < 0) {
|
2011-09-05 23:03:05 +02:00
|
|
|
g_set_error(error_r, simple_db_quark(), errno,
|
|
|
|
"Couldn't stat db file \"%s\": %s",
|
2012-07-30 07:26:08 +02:00
|
|
|
path.c_str(), g_strerror(errno));
|
2011-09-05 23:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISREG(st.st_mode)) {
|
|
|
|
g_set_error(error_r, simple_db_quark(), 0,
|
|
|
|
"db file \"%s\" is not a regular file",
|
2012-07-30 07:26:08 +02:00
|
|
|
path.c_str());
|
2011-09-05 23:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And check that we can write to it */
|
2012-07-30 07:26:08 +02:00
|
|
|
if (access(path.c_str(), R_OK | W_OK)) {
|
2011-09-05 23:03:05 +02:00
|
|
|
g_set_error(error_r, simple_db_quark(), errno,
|
|
|
|
"Can't open db file \"%s\" for reading/writing: %s",
|
2012-07-30 07:26:08 +02:00
|
|
|
path.c_str(), g_strerror(errno));
|
2011-09-05 23:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
bool
|
|
|
|
SimpleDatabase::Load(GError **error_r)
|
2011-09-05 23:03:05 +02:00
|
|
|
{
|
2012-07-30 07:26:08 +02:00
|
|
|
assert(!path.empty());
|
|
|
|
assert(root != NULL);
|
2011-09-05 23:03:05 +02:00
|
|
|
|
2013-01-17 00:56:57 +01:00
|
|
|
TextFile file(path);
|
2013-01-03 10:16:05 +01:00
|
|
|
if (file.HasFailed()) {
|
2011-09-05 23:03:05 +02:00
|
|
|
g_set_error(error_r, simple_db_quark(), errno,
|
|
|
|
"Failed to open database file \"%s\": %s",
|
2012-07-30 07:26:08 +02:00
|
|
|
path.c_str(), g_strerror(errno));
|
2011-09-05 23:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-03 10:16:05 +01:00
|
|
|
if (!db_load_internal(file, root, error_r))
|
2011-09-05 23:03:05 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
struct stat st;
|
2012-07-30 07:26:08 +02:00
|
|
|
if (stat(path.c_str(), &st) == 0)
|
|
|
|
mtime = st.st_mtime;
|
2011-09-05 23:03:05 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
bool
|
|
|
|
SimpleDatabase::Open(GError **error_r)
|
2011-09-05 23:03:05 +02:00
|
|
|
{
|
2013-01-02 23:06:20 +01:00
|
|
|
root = Directory::NewRoot();
|
2012-07-30 07:26:08 +02:00
|
|
|
mtime = 0;
|
2011-09-05 23:03:05 +02:00
|
|
|
|
2012-08-15 23:28:19 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
borrowed_song_count = 0;
|
|
|
|
#endif
|
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
GError *error = NULL;
|
2012-07-30 07:26:08 +02:00
|
|
|
if (!Load(&error)) {
|
2013-01-02 23:06:10 +01:00
|
|
|
root->Free();
|
2011-09-05 23:03:05 +02:00
|
|
|
|
|
|
|
g_warning("Failed to load database: %s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
if (!Check(error_r))
|
2011-09-05 23:03:05 +02:00
|
|
|
return false;
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
root = Directory::NewRoot();
|
2011-09-05 23:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
void
|
|
|
|
SimpleDatabase::Close()
|
2011-09-05 23:03:05 +02:00
|
|
|
{
|
2012-07-30 07:26:08 +02:00
|
|
|
assert(root != NULL);
|
2012-08-15 23:28:19 +02:00
|
|
|
assert(borrowed_song_count == 0);
|
2011-09-05 23:03:05 +02:00
|
|
|
|
2013-01-02 23:06:10 +01:00
|
|
|
root->Free();
|
2011-09-05 23:03:05 +02:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
struct song *
|
2012-08-02 18:20:43 +02:00
|
|
|
SimpleDatabase::GetSong(const char *uri, GError **error_r) const
|
2011-09-13 21:01:35 +02:00
|
|
|
{
|
2012-07-30 07:26:08 +02:00
|
|
|
assert(root != NULL);
|
2011-09-13 21:01:35 +02:00
|
|
|
|
2012-01-31 22:12:14 +01:00
|
|
|
db_lock();
|
2013-01-02 23:06:10 +01:00
|
|
|
song *song = root->LookupSong(uri);
|
2012-01-31 22:12:14 +01:00
|
|
|
db_unlock();
|
2011-09-13 21:01:35 +02:00
|
|
|
if (song == NULL)
|
|
|
|
g_set_error(error_r, db_quark(), DB_NOT_FOUND,
|
|
|
|
"No such song: %s", uri);
|
2012-08-15 23:28:19 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
else
|
|
|
|
++const_cast<unsigned &>(borrowed_song_count);
|
|
|
|
#endif
|
2011-09-13 21:01:35 +02:00
|
|
|
|
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
2012-08-15 23:28:19 +02:00
|
|
|
void
|
|
|
|
SimpleDatabase::ReturnSong(gcc_unused struct song *song) const
|
|
|
|
{
|
|
|
|
assert(song != nullptr);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(borrowed_song_count > 0);
|
|
|
|
--const_cast<unsigned &>(borrowed_song_count);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
G_GNUC_PURE
|
2013-01-02 23:06:20 +01:00
|
|
|
const Directory *
|
2012-07-30 07:26:08 +02:00
|
|
|
SimpleDatabase::LookupDirectory(const char *uri) const
|
|
|
|
{
|
|
|
|
assert(root != NULL);
|
|
|
|
assert(uri != NULL);
|
|
|
|
|
2012-09-05 20:50:15 +02:00
|
|
|
ScopeDatabaseLock protect;
|
2013-01-02 23:06:10 +01:00
|
|
|
return root->LookupDirectory(uri);
|
2012-07-30 07:26:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-07 23:06:41 +02:00
|
|
|
SimpleDatabase::Visit(const DatabaseSelection &selection,
|
2012-07-30 07:26:08 +02:00
|
|
|
VisitDirectory visit_directory,
|
|
|
|
VisitSong visit_song,
|
|
|
|
VisitPlaylist visit_playlist,
|
2012-08-02 18:20:43 +02:00
|
|
|
GError **error_r) const
|
2011-09-10 18:48:10 +02:00
|
|
|
{
|
2012-09-05 20:49:04 +02:00
|
|
|
ScopeDatabaseLock protect;
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
const Directory *directory = root->LookupDirectory(selection.uri);
|
2011-09-10 18:48:10 +02:00
|
|
|
if (directory == NULL) {
|
2012-09-05 20:49:04 +02:00
|
|
|
if (visit_song) {
|
2013-01-02 23:06:10 +01:00
|
|
|
song *song = root->LookupSong(selection.uri);
|
2012-09-05 20:49:04 +02:00
|
|
|
if (song != nullptr)
|
|
|
|
return !selection.Match(*song) ||
|
|
|
|
visit_song(*song, error_r);
|
|
|
|
}
|
2011-09-10 18:48:10 +02:00
|
|
|
|
|
|
|
g_set_error(error_r, db_quark(), DB_NOT_FOUND,
|
|
|
|
"No such directory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-07 21:32:08 +02:00
|
|
|
if (selection.recursive && visit_directory &&
|
|
|
|
!visit_directory(*directory, error_r))
|
2011-09-10 18:48:10 +02:00
|
|
|
return false;
|
|
|
|
|
2012-09-05 20:50:15 +02:00
|
|
|
return directory->Walk(selection.recursive, selection.filter,
|
|
|
|
visit_directory, visit_song, visit_playlist,
|
|
|
|
error_r);
|
2011-09-10 18:48:10 +02:00
|
|
|
}
|
|
|
|
|
2012-08-15 21:32:34 +02:00
|
|
|
bool
|
|
|
|
SimpleDatabase::VisitUniqueTags(const DatabaseSelection &selection,
|
|
|
|
enum tag_type tag_type,
|
|
|
|
VisitString visit_string,
|
|
|
|
GError **error_r) const
|
|
|
|
{
|
|
|
|
return ::VisitUniqueTags(*this, selection, tag_type, visit_string,
|
|
|
|
error_r);
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:20:28 +02:00
|
|
|
bool
|
|
|
|
SimpleDatabase::GetStats(const DatabaseSelection &selection,
|
|
|
|
DatabaseStats &stats, GError **error_r) const
|
|
|
|
{
|
|
|
|
return ::GetStats(*this, selection, stats, error_r);
|
|
|
|
}
|
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
bool
|
2012-07-30 07:26:08 +02:00
|
|
|
SimpleDatabase::Save(GError **error_r)
|
2011-09-05 23:03:05 +02:00
|
|
|
{
|
2012-01-31 22:12:14 +01:00
|
|
|
db_lock();
|
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
g_debug("removing empty directories from DB");
|
2013-01-02 23:06:10 +01:00
|
|
|
root->PruneEmpty();
|
2011-09-05 23:03:05 +02:00
|
|
|
|
|
|
|
g_debug("sorting DB");
|
2013-01-02 23:06:10 +01:00
|
|
|
root->Sort();
|
2011-09-05 23:03:05 +02:00
|
|
|
|
2012-01-31 22:12:14 +01:00
|
|
|
db_unlock();
|
|
|
|
|
2011-09-05 23:03:05 +02:00
|
|
|
g_debug("writing DB");
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
FILE *fp = fopen(path.c_str(), "w");
|
2011-09-05 23:03:05 +02:00
|
|
|
if (!fp) {
|
|
|
|
g_set_error(error_r, simple_db_quark(), errno,
|
|
|
|
"unable to write to db file \"%s\": %s",
|
2012-07-30 07:26:08 +02:00
|
|
|
path.c_str(), g_strerror(errno));
|
2011-09-05 23:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
db_save_internal(fp, root);
|
2011-09-05 23:03:05 +02:00
|
|
|
|
|
|
|
if (ferror(fp)) {
|
|
|
|
g_set_error(error_r, simple_db_quark(), errno,
|
|
|
|
"Failed to write to database file: %s",
|
|
|
|
g_strerror(errno));
|
|
|
|
fclose(fp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
struct stat st;
|
2012-07-30 07:26:08 +02:00
|
|
|
if (stat(path.c_str(), &st) == 0)
|
|
|
|
mtime = st.st_mtime;
|
2011-09-05 23:03:05 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
const DatabasePlugin simple_db_plugin = {
|
|
|
|
"simple",
|
|
|
|
SimpleDatabase::Create,
|
|
|
|
};
|