2008-10-08 11:07:35 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
|
|
|
* Copyright (C) 2008 Max Kellermann <max@duempel.org>
|
|
|
|
* This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "database.h"
|
|
|
|
#include "directory.h"
|
2008-10-09 15:23:37 +02:00
|
|
|
#include "directory_save.h"
|
2008-10-08 11:07:35 +02:00
|
|
|
#include "song.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "stats.h"
|
2008-12-29 17:28:32 +01:00
|
|
|
#include "config.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 <stdlib.h>
|
|
|
|
#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"
|
|
|
|
|
2009-02-27 19:30:16 +01:00
|
|
|
#define DIRECTORY_INFO_BEGIN "info_begin"
|
|
|
|
#define DIRECTORY_INFO_END "info_end"
|
|
|
|
#define DIRECTORY_MPD_VERSION "mpd_version: "
|
|
|
|
#define DIRECTORY_FS_CHARSET "fs_charset: "
|
|
|
|
|
2009-01-18 16:09:01 +01:00
|
|
|
static char *database_path;
|
|
|
|
|
2008-10-08 11:07:35 +02:00
|
|
|
static struct directory *music_root;
|
|
|
|
|
2009-03-02 15:41:44 +01:00
|
|
|
static time_t database_mtime;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2009-03-02 15:42:21 +01:00
|
|
|
/**
|
|
|
|
* The quark used for GError.domain.
|
|
|
|
*/
|
|
|
|
static inline GQuark
|
|
|
|
db_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("database");
|
|
|
|
}
|
|
|
|
|
2008-10-08 11:07:55 +02:00
|
|
|
void
|
2009-01-18 16:09:01 +01:00
|
|
|
db_init(const char *path)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2009-01-18 16:09:01 +01:00
|
|
|
database_path = g_strdup(path);
|
|
|
|
|
2009-01-18 16:56:07 +01:00
|
|
|
if (path != NULL)
|
|
|
|
music_root = directory_new("", NULL);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:07:55 +02:00
|
|
|
void
|
|
|
|
db_finish(void)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2009-01-18 16:56:07 +01:00
|
|
|
assert((database_path == NULL) == (music_root == NULL));
|
|
|
|
|
|
|
|
if (music_root != NULL)
|
|
|
|
directory_free(music_root);
|
2009-01-18 16:09:01 +01:00
|
|
|
|
|
|
|
g_free(database_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
db_clear(void)
|
|
|
|
{
|
2009-01-18 16:56:07 +01:00
|
|
|
assert(music_root != NULL);
|
|
|
|
|
2009-01-18 16:09:01 +01:00
|
|
|
directory_free(music_root);
|
|
|
|
music_root = directory_new("", NULL);
|
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
|
|
|
{
|
|
|
|
assert(music_root != NULL);
|
|
|
|
|
|
|
|
return music_root;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct directory *
|
2008-10-08 11:07:55 +02:00
|
|
|
db_get_directory(const char *name)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
2009-01-18 16:56:07 +01:00
|
|
|
if (music_root == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2008-10-08 11:07:35 +02:00
|
|
|
if (name == NULL)
|
|
|
|
return music_root;
|
|
|
|
|
2008-10-08 11:07:58 +02:00
|
|
|
return directory_get_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-25 18:47:23 +01:00
|
|
|
struct song *song;
|
2008-10-08 11:07:35 +02:00
|
|
|
struct directory *directory;
|
2009-01-18 16:56:07 +01:00
|
|
|
char *duplicated, *shortname, *dir;
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-01-18 16:56:07 +01:00
|
|
|
if (music_root == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
duplicated = g_strdup(file);
|
|
|
|
shortname = strrchr(duplicated, '/');
|
2008-10-08 11:07:35 +02:00
|
|
|
if (!shortname) {
|
|
|
|
shortname = duplicated;
|
2009-01-18 16:56:07 +01:00
|
|
|
dir = NULL;
|
2008-10-08 11:07:35 +02:00
|
|
|
} else {
|
|
|
|
*shortname = '\0';
|
|
|
|
++shortname;
|
|
|
|
dir = duplicated;
|
|
|
|
}
|
|
|
|
|
2009-01-25 18:47:23 +01:00
|
|
|
directory = db_get_directory(dir);
|
|
|
|
if (directory != NULL)
|
|
|
|
song = songvec_find(&directory->songs, shortname);
|
|
|
|
else
|
|
|
|
song = NULL;
|
|
|
|
|
|
|
|
assert(song == NULL || song->parent == directory);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2009-01-25 18:47:21 +01:00
|
|
|
g_free(duplicated);
|
2008-10-08 11:07:35 +02:00
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-10-08 11:07:55 +02:00
|
|
|
db_walk(const char *name,
|
|
|
|
int (*forEachSong)(struct song *, void *),
|
|
|
|
int (*forEachDir)(struct directory *, void *), void *data)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
|
|
|
struct directory *directory;
|
|
|
|
|
2009-01-18 16:56:07 +01:00
|
|
|
if (music_root == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2008-10-08 11:07:55 +02:00
|
|
|
if ((directory = db_get_directory(name)) == NULL) {
|
2008-10-08 11:07:35 +02:00
|
|
|
struct song *song;
|
2008-10-09 16:26:09 +02:00
|
|
|
if ((song = db_get_song(name)) && forEachSong) {
|
2008-10-08 11:07:35 +02:00
|
|
|
return forEachSong(song, data);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-08 11:07:58 +02:00
|
|
|
return directory_walk(directory, forEachSong, forEachDir, data);
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
bool
|
2008-10-08 11:07:55 +02:00
|
|
|
db_check(void)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
2009-01-18 16:09:01 +01:00
|
|
|
|
|
|
|
assert(database_path != NULL);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
|
|
|
/* Check if the file exists */
|
2009-01-18 16:09:01 +01:00
|
|
|
if (access(database_path, F_OK)) {
|
2008-10-08 11:07:35 +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 */
|
2009-01-18 16:09:01 +01:00
|
|
|
char *dirPath = g_path_get_dirname(database_path);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
|
|
|
/* Check that the parent part of the path is a directory */
|
|
|
|
if (stat(dirPath, &st) < 0) {
|
2008-10-20 22:18:40 +02:00
|
|
|
g_free(dirPath);
|
2008-12-29 17:28:37 +01:00
|
|
|
g_warning("Couldn't stat parent directory of db file "
|
2009-01-18 16:09:01 +01:00
|
|
|
"\"%s\": %s", database_path, strerror(errno));
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
2008-10-20 22:18:40 +02:00
|
|
|
g_free(dirPath);
|
2008-12-29 17:28:37 +01:00
|
|
|
g_warning("Couldn't create db file \"%s\" because the "
|
2009-01-18 16:09:01 +01:00
|
|
|
"parent path is not a directory", database_path);
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we can write to the directory */
|
|
|
|
if (access(dirPath, R_OK | W_OK)) {
|
2008-12-29 17:28:37 +01:00
|
|
|
g_warning("Can't create db file in \"%s\": %s",
|
|
|
|
dirPath, strerror(errno));
|
2008-10-20 22:18:40 +02:00
|
|
|
g_free(dirPath);
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2008-10-20 22:18:40 +02:00
|
|
|
g_free(dirPath);
|
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
return true;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Path exists, now check if it's a regular file */
|
2009-01-18 16:09:01 +01:00
|
|
|
if (stat(database_path, &st) < 0) {
|
2008-12-29 17:28:37 +01:00
|
|
|
g_warning("Couldn't stat db file \"%s\": %s",
|
2009-01-18 16:09:01 +01:00
|
|
|
database_path, strerror(errno));
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISREG(st.st_mode)) {
|
2009-01-18 16:09:01 +01:00
|
|
|
g_warning("db file \"%s\" is not a regular file", database_path);
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* And check that we can write to it */
|
2009-01-18 16:09:01 +01:00
|
|
|
if (access(database_path, R_OK | W_OK)) {
|
2008-12-29 17:28:37 +01:00
|
|
|
g_warning("Can't open db file \"%s\" for reading/writing: %s",
|
2009-01-18 16:09:01 +01:00
|
|
|
database_path, strerror(errno));
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
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
|
|
|
}
|
|
|
|
|
2009-01-04 21:18:40 +01:00
|
|
|
bool
|
2008-10-08 11:07:55 +02:00
|
|
|
db_save(void)
|
2008-10-08 11:07:35 +02:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
struct stat st;
|
|
|
|
|
2009-01-18 16:09:01 +01:00
|
|
|
assert(database_path != NULL);
|
|
|
|
assert(music_root != NULL);
|
|
|
|
|
2008-12-29 17:28:37 +01:00
|
|
|
g_debug("removing empty directories from DB");
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_prune_empty(music_root);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2008-12-29 17:28:37 +01:00
|
|
|
g_debug("sorting DB");
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_sort(music_root);
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2008-12-29 17:28:37 +01:00
|
|
|
g_debug("writing DB");
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2009-01-18 16:09:01 +01:00
|
|
|
fp = fopen(database_path, "w");
|
2008-10-08 11:07:35 +02:00
|
|
|
if (!fp) {
|
2008-12-29 17:28:37 +01:00
|
|
|
g_warning("unable to write to db file \"%s\": %s",
|
2009-01-18 16:09:01 +01:00
|
|
|
database_path, strerror(errno));
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* block signals when writing the db so we don't get a corrupted db */
|
|
|
|
fprintf(fp, "%s\n", DIRECTORY_INFO_BEGIN);
|
|
|
|
fprintf(fp, "%s%s\n", DIRECTORY_MPD_VERSION, VERSION);
|
2008-11-05 18:21:52 +01:00
|
|
|
fprintf(fp, "%s%s\n", DIRECTORY_FS_CHARSET, path_get_fs_charset());
|
2008-10-08 11:07:35 +02:00
|
|
|
fprintf(fp, "%s\n", DIRECTORY_INFO_END);
|
|
|
|
|
2008-10-08 11:07:58 +02:00
|
|
|
if (directory_save(fp, music_root) < 0) {
|
2008-12-29 17:28:37 +01:00
|
|
|
g_warning("Failed to write to database file: %s",
|
|
|
|
strerror(errno));
|
2008-10-08 11:07:35 +02:00
|
|
|
while (fclose(fp) && errno == EINTR);
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (fclose(fp) && errno == EINTR);
|
|
|
|
|
2009-01-18 16:09:01 +01:00
|
|
|
if (stat(database_path, &st) == 0)
|
2009-03-02 15:41:44 +01:00
|
|
|
database_mtime = st.st_mtime;
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
struct stat st;
|
2008-10-09 15:21:18 +02:00
|
|
|
char buffer[100];
|
2009-03-02 15:41:44 +01:00
|
|
|
bool found_charset = false, found_version = false;
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2009-01-18 16:09:01 +01:00
|
|
|
assert(database_path != NULL);
|
2009-01-04 21:18:16 +01:00
|
|
|
assert(music_root != NULL);
|
|
|
|
|
2008-10-08 11:07:35 +02:00
|
|
|
if (!music_root)
|
2008-10-08 11:08:16 +02:00
|
|
|
music_root = directory_new("", NULL);
|
2009-01-18 16:09:01 +01:00
|
|
|
while (!(fp = fopen(database_path, "r")) && errno == EINTR) ;
|
2008-10-08 11:07:35 +02:00
|
|
|
if (fp == NULL) {
|
2009-03-02 15:42:21 +01:00
|
|
|
g_set_error(error, db_quark(), errno,
|
|
|
|
"Failed to open database file \"%s\": %s",
|
|
|
|
database_path, strerror(errno));
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get initial info */
|
2009-03-02 15:42:21 +01:00
|
|
|
if (!fgets(buffer, sizeof(buffer), fp)) {
|
|
|
|
fclose(fp);
|
|
|
|
g_set_error(error, db_quark(), 0, "Unexpected end of file");
|
|
|
|
return false;
|
|
|
|
}
|
2008-10-09 15:21:18 +02:00
|
|
|
|
2008-12-28 19:54:49 +01:00
|
|
|
g_strchomp(buffer);
|
|
|
|
|
2008-10-09 15:21:18 +02:00
|
|
|
if (0 != strcmp(DIRECTORY_INFO_BEGIN, buffer)) {
|
2009-03-02 15:42:21 +01:00
|
|
|
fclose(fp);
|
|
|
|
g_set_error(error, db_quark(), 0, "Database corrupted");
|
2009-01-04 21:18:40 +01:00
|
|
|
return false;
|
2008-10-09 15:21:18 +02:00
|
|
|
}
|
|
|
|
|
2008-12-28 19:54:49 +01:00
|
|
|
while (fgets(buffer, sizeof(buffer), fp) &&
|
|
|
|
!g_str_has_prefix(buffer, DIRECTORY_INFO_END)) {
|
|
|
|
g_strchomp(buffer);
|
|
|
|
|
2008-10-28 20:33:56 +01:00
|
|
|
if (g_str_has_prefix(buffer, DIRECTORY_MPD_VERSION)) {
|
2009-03-02 15:42:21 +01:00
|
|
|
if (found_version) {
|
|
|
|
fclose(fp);
|
|
|
|
g_set_error(error, db_quark(), 0,
|
|
|
|
"Duplicate version line");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-02 15:41:44 +01:00
|
|
|
found_version = true;
|
2008-10-28 20:33:56 +01:00
|
|
|
} else if (g_str_has_prefix(buffer, DIRECTORY_FS_CHARSET)) {
|
2009-03-02 15:41:44 +01:00
|
|
|
const char *new_charset, *old_charset;
|
2008-10-09 15:21:18 +02:00
|
|
|
|
2009-03-02 15:42:21 +01:00
|
|
|
if (found_charset) {
|
|
|
|
fclose(fp);
|
|
|
|
g_set_error(error, db_quark(), 0,
|
|
|
|
"Duplicate charset line");
|
|
|
|
return false;
|
|
|
|
}
|
2008-10-09 15:21:18 +02:00
|
|
|
|
2009-03-02 15:41:44 +01:00
|
|
|
found_charset = true;
|
2008-10-09 15:21:18 +02:00
|
|
|
|
2009-03-02 15:41:44 +01:00
|
|
|
new_charset = &(buffer[strlen(DIRECTORY_FS_CHARSET)]);
|
|
|
|
old_charset = path_get_fs_charset();
|
|
|
|
if (old_charset != NULL
|
|
|
|
&& strcmp(new_charset, old_charset)) {
|
2009-03-02 15:42:21 +01:00
|
|
|
fclose(fp);
|
2009-02-12 18:31:00 +01:00
|
|
|
g_message("Existing database has charset \"%s\" "
|
2008-12-29 17:28:37 +01:00
|
|
|
"instead of \"%s\"; "
|
2009-02-12 18:31:00 +01:00
|
|
|
"discarding database file",
|
2009-03-02 15:41:44 +01:00
|
|
|
new_charset, old_charset);
|
2009-02-12 18:31:00 +01:00
|
|
|
return false;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
2009-03-02 15:42:21 +01:00
|
|
|
} else {
|
|
|
|
fclose(fp);
|
|
|
|
g_set_error(error, db_quark(), 0,
|
|
|
|
"Malformed line: %s", buffer);
|
|
|
|
return false;
|
|
|
|
}
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|
|
|
|
|
2008-12-29 17:28:37 +01:00
|
|
|
g_debug("reading DB");
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_load(fp, music_root);
|
2008-10-08 11:07:35 +02:00
|
|
|
while (fclose(fp) && errno == EINTR) ;
|
|
|
|
|
2009-01-04 20:57:06 +01:00
|
|
|
stats_update();
|
2008-10-08 11:07:35 +02:00
|
|
|
|
2009-01-18 16:09:01 +01:00
|
|
|
if (stat(database_path, &st) == 0)
|
2009-03-02 15:41:44 +01:00
|
|
|
database_mtime = st.st_mtime;
|
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
|
|
|
{
|
2009-03-02 15:41:44 +01:00
|
|
|
return database_mtime;
|
2008-10-08 11:07:35 +02:00
|
|
|
}
|