2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* 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 "directory.h"
|
|
|
|
|
|
|
|
#include "conf.h"
|
2006-08-06 15:53:53 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "ls.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "stats.h"
|
|
|
|
#include "utils.h"
|
2008-09-29 12:16:57 +02:00
|
|
|
#include "client.h"
|
2008-08-28 20:01:08 +02:00
|
|
|
#include "dbUtils.h"
|
2008-09-07 13:35:01 +02:00
|
|
|
#include "song_print.h"
|
|
|
|
#include "song_save.h"
|
2008-09-29 13:11:40 +02:00
|
|
|
#include "dirvec.h"
|
2008-10-08 10:48:48 +02:00
|
|
|
#include "update.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#define DIRECTORY_DIR "directory: "
|
2008-09-29 13:17:48 +02:00
|
|
|
#define DIRECTORY_MTIME "mtime: " /* DEPRECATED, noop-read-only */
|
2004-02-24 00:41:20 +01:00
|
|
|
#define DIRECTORY_BEGIN "begin: "
|
|
|
|
#define DIRECTORY_END "end: "
|
|
|
|
#define DIRECTORY_INFO_BEGIN "info_begin"
|
|
|
|
#define DIRECTORY_INFO_END "info_end"
|
|
|
|
#define DIRECTORY_MPD_VERSION "mpd_version: "
|
|
|
|
#define DIRECTORY_FS_CHARSET "fs_charset: "
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static struct directory *music_root;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
static time_t directory_dbModTime;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static void deleteEmptyDirectoriesInDirectory(struct directory * directory);
|
2004-03-09 23:48:35 +01:00
|
|
|
|
2006-08-08 04:23:21 +02:00
|
|
|
static char *getDbFile(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
ConfigParam *param = parseConfigFilePath(CONF_DB_FILE, 1);
|
2005-03-06 20:00:58 +01:00
|
|
|
|
|
|
|
assert(param);
|
|
|
|
assert(param->value);
|
|
|
|
|
|
|
|
return param->value;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *
|
|
|
|
newDirectory(const char *dirname, struct directory * parent)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *directory;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
directory = xcalloc(1, sizeof(*directory));
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (dirname && strlen(dirname))
|
2006-08-26 08:25:57 +02:00
|
|
|
directory->path = xstrdup(dirname);
|
2004-06-23 05:33:35 +02:00
|
|
|
directory->parent = parent;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
void
|
2008-10-08 10:49:05 +02:00
|
|
|
freeDirectory(struct directory * directory)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-29 13:11:40 +02:00
|
|
|
dirvec_destroy(&directory->children);
|
2008-09-29 12:17:13 +02:00
|
|
|
songvec_destroy(&directory->songs);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (directory->path)
|
|
|
|
free(directory->path);
|
2004-02-24 00:41:20 +01:00
|
|
|
free(directory);
|
2004-11-12 18:38:52 +01:00
|
|
|
/* this resets last dir returned */
|
2006-07-20 18:02:40 +02:00
|
|
|
/*getDirectoryPath(NULL); */
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static void deleteEmptyDirectoriesInDirectory(struct directory * directory)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-29 13:11:40 +02:00
|
|
|
int i;
|
|
|
|
struct dirvec *dv = &directory->children;
|
|
|
|
|
|
|
|
for (i = dv->nr; --i >= 0; ) {
|
|
|
|
deleteEmptyDirectoriesInDirectory(dv->base[i]);
|
2008-10-08 10:48:49 +02:00
|
|
|
if (directory_is_empty(dv->base[i]))
|
2008-09-29 13:11:40 +02:00
|
|
|
dirvec_delete(dv, dv->base[i]);
|
2004-03-09 23:48:35 +01:00
|
|
|
}
|
2008-09-29 13:11:40 +02:00
|
|
|
if (!dv->nr)
|
|
|
|
dirvec_destroy(dv);
|
2004-03-09 23:48:35 +01:00
|
|
|
}
|
|
|
|
|
2008-09-29 13:17:42 +02:00
|
|
|
void directory_finish(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-29 13:17:42 +02:00
|
|
|
freeDirectory(music_root);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
int isRootDirectory(const char *name)
|
2007-05-24 19:06:59 +02:00
|
|
|
{
|
2008-09-29 13:18:27 +02:00
|
|
|
return (!name || name[0] == '\0' || !strcmp(name, "/"));
|
2007-05-24 19:06:59 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *
|
2008-10-08 10:48:48 +02:00
|
|
|
directory_get_root(void)
|
|
|
|
{
|
|
|
|
assert(music_root != NULL);
|
|
|
|
|
|
|
|
return music_root;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static struct directory *
|
|
|
|
getSubDirectory(struct directory * directory, const char *name)
|
2004-04-14 04:35:29 +02:00
|
|
|
{
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *cur = directory;
|
|
|
|
struct directory *found = NULL;
|
2008-09-29 13:11:40 +02:00
|
|
|
char *duplicated;
|
|
|
|
char *locate;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
if (isRootDirectory(name))
|
2004-02-24 00:41:20 +01:00
|
|
|
return directory;
|
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
duplicated = xstrdup(name);
|
|
|
|
locate = strchr(duplicated, '/');
|
|
|
|
while (1) {
|
|
|
|
if (locate)
|
|
|
|
*locate = '\0';
|
|
|
|
if (!(found = dirvec_find(&cur->children, duplicated)))
|
|
|
|
break;
|
2008-09-29 13:17:23 +02:00
|
|
|
assert(cur == found->parent);
|
2008-09-29 13:11:40 +02:00
|
|
|
cur = found;
|
|
|
|
if (!locate)
|
|
|
|
break;
|
|
|
|
*locate = '/';
|
|
|
|
locate = strchr(locate + 1, '/');
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
free(duplicated);
|
2004-04-14 04:35:29 +02:00
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
return found;
|
2004-04-14 04:35:29 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *
|
2008-10-08 10:48:48 +02:00
|
|
|
getDirectory(const char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-29 13:17:42 +02:00
|
|
|
return getSubDirectory(music_root, name);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
static int printDirectoryList(struct client *client, struct dirvec *dv)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-29 13:11:40 +02:00
|
|
|
size_t i;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
for (i = 0; i < dv->nr; ++i) {
|
|
|
|
client_printf(client, DIRECTORY_DIR "%s\n",
|
|
|
|
getDirectoryPath(dv->base[i]));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 13:53:55 +02:00
|
|
|
int printDirectoryInfo(struct client *client, const char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *directory;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-09-07 13:49:01 +02:00
|
|
|
if ((directory = getDirectory(name)) == NULL)
|
2004-02-24 00:41:20 +01:00
|
|
|
return -1;
|
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
printDirectoryList(client, &directory->children);
|
2008-09-23 20:48:39 +02:00
|
|
|
songvec_print(client, &directory->songs);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
/* TODO error checking */
|
2008-10-08 10:49:05 +02:00
|
|
|
static int
|
|
|
|
writeDirectoryInfo(FILE * fp, struct directory * directory)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-29 13:11:40 +02:00
|
|
|
struct dirvec *children = &directory->children;
|
|
|
|
size_t i;
|
2007-07-16 22:31:37 +02:00
|
|
|
int retv;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (directory->path) {
|
2007-07-16 22:31:37 +02:00
|
|
|
retv = fprintf(fp, "%s%s\n", DIRECTORY_BEGIN,
|
2008-09-29 13:17:52 +02:00
|
|
|
getDirectoryPath(directory));
|
|
|
|
if (retv < 0)
|
|
|
|
return -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
for (i = 0; i < children->nr; ++i) {
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *cur = children->base[i];
|
2008-09-29 13:17:23 +02:00
|
|
|
const char *base = mpd_basename(cur->path);
|
2008-09-29 13:11:40 +02:00
|
|
|
|
|
|
|
retv = fprintf(fp, DIRECTORY_DIR "%s\n", base);
|
2008-09-29 13:17:52 +02:00
|
|
|
if (retv < 0)
|
|
|
|
return -1;
|
|
|
|
if (writeDirectoryInfo(fp, cur) < 0)
|
|
|
|
return -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-09-23 20:48:39 +02:00
|
|
|
songvec_save(fp, &directory->songs);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-29 13:17:52 +02:00
|
|
|
if (directory->path &&
|
|
|
|
fprintf(fp, DIRECTORY_END "%s\n",
|
|
|
|
getDirectoryPath(directory)) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static void
|
|
|
|
readDirectoryInfo(FILE * fp, struct directory * directory)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2007-12-28 03:56:25 +01:00
|
|
|
char buffer[MPD_PATH_MAX * 2];
|
|
|
|
int bufferSize = MPD_PATH_MAX * 2;
|
2008-08-29 09:39:12 +02:00
|
|
|
char key[MPD_PATH_MAX * 2];
|
2006-07-20 18:02:40 +02:00
|
|
|
char *name;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (myFgets(buffer, bufferSize, fp)
|
2008-09-23 20:48:12 +02:00
|
|
|
&& prefixcmp(buffer, DIRECTORY_END)) {
|
|
|
|
if (!prefixcmp(buffer, DIRECTORY_DIR)) {
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *subdir;
|
2008-10-06 18:37:13 +02:00
|
|
|
|
2008-08-29 09:39:12 +02:00
|
|
|
strcpy(key, &(buffer[strlen(DIRECTORY_DIR)]));
|
2007-05-26 20:15:54 +02:00
|
|
|
if (!myFgets(buffer, bufferSize, fp))
|
|
|
|
FATAL("Error reading db, fgets\n");
|
2006-07-20 18:02:40 +02:00
|
|
|
/* for compatibility with db's prior to 0.11 */
|
2008-09-23 20:48:12 +02:00
|
|
|
if (!prefixcmp(buffer, DIRECTORY_MTIME)) {
|
2007-05-26 20:15:54 +02:00
|
|
|
if (!myFgets(buffer, bufferSize, fp))
|
|
|
|
FATAL("Error reading db, fgets\n");
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2008-09-23 20:48:12 +02:00
|
|
|
if (prefixcmp(buffer, DIRECTORY_BEGIN))
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("Error reading db at line: %s\n", buffer);
|
2008-08-29 09:39:12 +02:00
|
|
|
name = &(buffer[strlen(DIRECTORY_BEGIN)]);
|
2008-10-06 18:37:13 +02:00
|
|
|
if ((subdir = getDirectory(name))) {
|
|
|
|
assert(subdir->parent == directory);
|
|
|
|
} else {
|
|
|
|
subdir = newDirectory(name, directory);
|
|
|
|
dirvec_add(&directory->children, subdir);
|
|
|
|
}
|
|
|
|
readDirectoryInfo(fp, subdir);
|
2008-09-23 20:48:12 +02:00
|
|
|
} else if (!prefixcmp(buffer, SONG_BEGIN)) {
|
2008-09-23 20:48:39 +02:00
|
|
|
readSongInfoIntoList(fp, &directory->songs, directory);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("Unknown line in db: %s\n", buffer);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
void
|
2008-10-08 10:49:05 +02:00
|
|
|
sortDirectory(struct directory * directory)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-29 13:11:40 +02:00
|
|
|
int i;
|
|
|
|
struct dirvec *dv = &directory->children;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
dirvec_sort(dv);
|
2008-09-23 20:48:39 +02:00
|
|
|
songvec_sort(&directory->songs);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
for (i = dv->nr; --i >= 0; )
|
|
|
|
sortDirectory(dv->base[i]);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-08-08 04:23:21 +02:00
|
|
|
int checkDirectoryDB(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-24 01:35:35 +02:00
|
|
|
struct stat st;
|
2008-01-03 08:22:22 +01:00
|
|
|
char *dbFile = getDbFile();
|
2006-07-24 01:10:31 +02:00
|
|
|
|
|
|
|
/* Check if the file exists */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (access(dbFile, F_OK)) {
|
2006-07-24 01:10:31 +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 */
|
2008-01-03 08:22:22 +01:00
|
|
|
char dirPath[MPD_PATH_MAX];
|
|
|
|
parent_path(dirPath, dbFile);
|
2008-01-03 08:32:59 +01:00
|
|
|
if (*dirPath == '\0')
|
|
|
|
strcpy(dirPath, "/");
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-07-24 01:42:16 +02:00
|
|
|
/* Check that the parent part of the path is a directory */
|
|
|
|
if (stat(dirPath, &st) < 0) {
|
|
|
|
ERROR("Couldn't stat parent directory of db file "
|
|
|
|
"\"%s\": %s\n", dbFile, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
|
|
|
ERROR("Couldn't create db file \"%s\" because the "
|
|
|
|
"parent path is not a directory\n", dbFile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-24 01:10:31 +02:00
|
|
|
/* Check if we can write to the directory */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (access(dirPath, R_OK | W_OK)) {
|
2006-07-24 01:10:31 +02:00
|
|
|
ERROR("Can't create db file in \"%s\": %s\n", dirPath,
|
2006-07-20 18:02:40 +02:00
|
|
|
strerror(errno));
|
2006-06-07 23:00:36 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-07-24 01:10:31 +02:00
|
|
|
|
2006-06-07 23:00:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-07-24 01:10:31 +02:00
|
|
|
|
2006-07-24 01:35:35 +02:00
|
|
|
/* Path exists, now check if it's a regular file */
|
|
|
|
if (stat(dbFile, &st) < 0) {
|
2006-07-24 01:42:16 +02:00
|
|
|
ERROR("Couldn't stat db file \"%s\": %s\n", dbFile,
|
2006-07-24 01:35:35 +02:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISREG(st.st_mode)) {
|
|
|
|
ERROR("db file \"%s\" is not a regular file\n", dbFile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And check that we can write to it */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (access(dbFile, R_OK | W_OK)) {
|
2006-07-24 01:10:31 +02:00
|
|
|
ERROR("Can't open db file \"%s\" for reading/writing: %s\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
dbFile, strerror(errno));
|
2006-05-08 23:03:47 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-05-08 23:03:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-08 04:23:21 +02:00
|
|
|
int writeDirectoryDB(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char *dbFile = getDbFile();
|
2007-04-25 22:34:30 +02:00
|
|
|
struct stat st;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-11-14 00:22:22 +01:00
|
|
|
DEBUG("removing empty directories from DB\n");
|
2008-09-29 13:17:42 +02:00
|
|
|
deleteEmptyDirectoriesInDirectory(music_root);
|
2004-11-14 00:22:22 +01:00
|
|
|
|
|
|
|
DEBUG("sorting DB\n");
|
|
|
|
|
2008-09-29 13:17:42 +02:00
|
|
|
sortDirectory(music_root);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-11-14 00:22:22 +01:00
|
|
|
DEBUG("writing DB\n");
|
|
|
|
|
2008-09-23 22:38:36 +02:00
|
|
|
fp = fopen(dbFile, "w");
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!fp) {
|
2005-03-06 20:00:58 +01:00
|
|
|
ERROR("unable to write to db file \"%s\": %s\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
dbFile, strerror(errno));
|
2005-03-06 20:00:58 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
/* block signals when writing the db so we don't get a corrupted db */
|
2006-07-31 01:32:39 +02:00
|
|
|
fprintf(fp, "%s\n", DIRECTORY_INFO_BEGIN);
|
|
|
|
fprintf(fp, "%s%s\n", DIRECTORY_MPD_VERSION, VERSION);
|
|
|
|
fprintf(fp, "%s%s\n", DIRECTORY_FS_CHARSET, getFsCharset());
|
|
|
|
fprintf(fp, "%s\n", DIRECTORY_INFO_END);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-29 13:17:52 +02:00
|
|
|
if (writeDirectoryInfo(fp, music_root) < 0) {
|
|
|
|
ERROR("Failed to write to database file: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
while (fclose(fp) && errno == EINTR);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-13 19:08:31 +02:00
|
|
|
|
2007-04-25 22:34:30 +02:00
|
|
|
while (fclose(fp) && errno == EINTR);
|
|
|
|
|
|
|
|
if (stat(dbFile, &st) == 0)
|
|
|
|
directory_dbModTime = st.st_mtime;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-08 04:23:21 +02:00
|
|
|
int readDirectoryDB(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
char *dbFile = getDbFile();
|
|
|
|
struct stat st;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-29 13:17:42 +02:00
|
|
|
if (!music_root)
|
|
|
|
music_root = newDirectory(NULL, NULL);
|
2006-07-20 18:02:40 +02:00
|
|
|
while (!(fp = fopen(dbFile, "r")) && errno == EINTR) ;
|
|
|
|
if (fp == NULL) {
|
2007-05-26 20:21:51 +02:00
|
|
|
ERROR("unable to open db file \"%s\": %s\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
dbFile, strerror(errno));
|
2005-03-06 20:00:58 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
/* get initial info */
|
|
|
|
{
|
|
|
|
char buffer[100];
|
|
|
|
int bufferSize = 100;
|
|
|
|
int foundFsCharset = 0;
|
|
|
|
int foundVersion = 0;
|
|
|
|
|
2007-05-26 20:15:54 +02:00
|
|
|
if (!myFgets(buffer, bufferSize, fp))
|
|
|
|
FATAL("Error reading db, fgets\n");
|
2006-07-20 18:02:40 +02:00
|
|
|
if (0 == strcmp(DIRECTORY_INFO_BEGIN, buffer)) {
|
|
|
|
while (myFgets(buffer, bufferSize, fp) &&
|
|
|
|
0 != strcmp(DIRECTORY_INFO_END, buffer)) {
|
2008-09-23 20:48:12 +02:00
|
|
|
if (!prefixcmp(buffer, DIRECTORY_MPD_VERSION))
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2007-05-26 20:15:54 +02:00
|
|
|
if (foundVersion)
|
|
|
|
FATAL("already found version in db\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
foundVersion = 1;
|
2008-09-23 20:48:12 +02:00
|
|
|
} else if (!prefixcmp(buffer,
|
|
|
|
DIRECTORY_FS_CHARSET)) {
|
2006-07-20 18:02:40 +02:00
|
|
|
char *fsCharset;
|
|
|
|
char *tempCharset;
|
|
|
|
|
2007-05-26 20:15:54 +02:00
|
|
|
if (foundFsCharset)
|
|
|
|
FATAL("already found fs charset in db\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
foundFsCharset = 1;
|
|
|
|
|
2007-04-26 02:48:37 +02:00
|
|
|
fsCharset = &(buffer[strlen(DIRECTORY_FS_CHARSET)]);
|
|
|
|
if ((tempCharset = getConfigParamValue(CONF_FS_CHARSET))
|
2006-07-20 18:02:40 +02:00
|
|
|
&& strcmp(fsCharset, tempCharset)) {
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("Using \"%s\" for the "
|
2004-02-24 00:41:20 +01:00
|
|
|
"filesystem charset "
|
|
|
|
"instead of \"%s\"\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
fsCharset, tempCharset);
|
2004-06-12 04:06:16 +02:00
|
|
|
WARNING("maybe you need to "
|
2004-02-24 00:41:20 +01:00
|
|
|
"recreate the db?\n");
|
|
|
|
setFsCharset(fsCharset);
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("directory: unknown line in db info: %s\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
buffer);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2004-02-24 00:41:20 +01:00
|
|
|
ERROR("db info not found in db file\n");
|
|
|
|
ERROR("you should recreate the db using --create-db\n");
|
2008-09-23 22:37:40 +02:00
|
|
|
while (fclose(fp) && errno == EINTR) ;
|
2006-07-24 01:35:35 +02:00
|
|
|
return -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-14 00:22:22 +01:00
|
|
|
DEBUG("reading DB\n");
|
|
|
|
|
2008-09-29 13:17:42 +02:00
|
|
|
readDirectoryInfo(fp, music_root);
|
2006-07-20 18:02:40 +02:00
|
|
|
while (fclose(fp) && errno == EINTR) ;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-07 13:48:37 +02:00
|
|
|
stats.numberOfSongs = countSongsIn(NULL);
|
|
|
|
stats.dbPlayTime = sumSongTimesIn(NULL);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (stat(dbFile, &st) == 0)
|
|
|
|
directory_dbModTime = st.st_mtime;
|
2004-05-17 13:56:14 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
static int
|
|
|
|
traverseAllInSubDirectory(struct directory * directory,
|
|
|
|
int (*forEachSong) (Song *, void *),
|
|
|
|
int (*forEachDir) (struct directory *, void *),
|
|
|
|
void *data)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2008-09-29 13:11:40 +02:00
|
|
|
struct dirvec *dv = &directory->children;
|
2008-10-07 22:07:44 +02:00
|
|
|
int err = 0;
|
2008-09-29 13:11:40 +02:00
|
|
|
size_t j;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-10-07 22:07:44 +02:00
|
|
|
if (forEachDir && (err = forEachDir(directory, data)) < 0)
|
|
|
|
return err;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (forEachSong) {
|
2008-10-07 22:10:48 +02:00
|
|
|
err = songvec_for_each(&directory->songs, forEachSong, data);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
2008-10-07 22:07:44 +02:00
|
|
|
for (j = 0; err >= 0 && j < dv->nr; ++j)
|
|
|
|
err = traverseAllInSubDirectory(dv->base[j], forEachSong,
|
|
|
|
forEachDir, data);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-10-07 22:07:44 +02:00
|
|
|
return err;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
int
|
|
|
|
traverseAllIn(const char *name,
|
|
|
|
int (*forEachSong) (Song *, void *),
|
|
|
|
int (*forEachDir) (struct directory *, void *), void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *directory;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((directory = getDirectory(name)) == NULL) {
|
|
|
|
Song *song;
|
|
|
|
if ((song = getSongFromDB(name)) && forEachSong) {
|
2008-09-07 13:48:24 +02:00
|
|
|
return forEachSong(song, data);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-09-07 13:48:24 +02:00
|
|
|
return traverseAllInSubDirectory(directory, forEachSong, forEachDir,
|
2006-07-20 18:02:40 +02:00
|
|
|
data);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-09-29 13:17:42 +02:00
|
|
|
void directory_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-29 13:17:42 +02:00
|
|
|
music_root = newDirectory(NULL, NULL);
|
2008-10-08 10:48:55 +02:00
|
|
|
updateDirectory(music_root);
|
2008-09-07 13:48:37 +02:00
|
|
|
stats.numberOfSongs = countSongsIn(NULL);
|
|
|
|
stats.dbPlayTime = sumSongTimesIn(NULL);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-09-29 13:17:23 +02:00
|
|
|
Song *getSongFromDB(const char *file)
|
2004-04-11 19:37:47 +02:00
|
|
|
{
|
2008-09-29 13:11:40 +02:00
|
|
|
Song *song = NULL;
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *directory;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *dir = NULL;
|
2008-01-26 13:46:21 +01:00
|
|
|
char *duplicated = xstrdup(file);
|
2008-09-29 13:11:40 +02:00
|
|
|
char *shortname = strrchr(duplicated, '/');
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("get song: %s\n", file);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
if (!shortname) {
|
|
|
|
shortname = duplicated;
|
|
|
|
} else {
|
|
|
|
*shortname = '\0';
|
|
|
|
++shortname;
|
2008-01-26 13:46:21 +01:00
|
|
|
dir = duplicated;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
if (!(directory = getDirectory(dir)))
|
|
|
|
goto out;
|
|
|
|
if (!(song = songvec_find(&directory->songs, shortname)))
|
|
|
|
goto out;
|
2008-09-29 13:17:23 +02:00
|
|
|
assert(song->parentDir == directory);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
out:
|
|
|
|
free(duplicated);
|
2008-09-23 20:48:39 +02:00
|
|
|
return song;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-08-08 04:23:21 +02:00
|
|
|
time_t getDbModTime(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-04-16 01:36:41 +02:00
|
|
|
return directory_dbModTime;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|