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"
|
2008-08-28 20:02:43 +02:00
|
|
|
#include "client.h"
|
2006-08-06 15:53:53 +02:00
|
|
|
#include "listen.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "ls.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "playlist.h"
|
2004-04-11 06:15:14 +02:00
|
|
|
#include "sig_handlers.h"
|
2006-08-06 15:53:53 +02:00
|
|
|
#include "stats.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "volume.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-23 20:48:44 +02:00
|
|
|
#include "main_notify.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#define DIRECTORY_DIR "directory: "
|
|
|
|
#define DIRECTORY_MTIME "mtime: "
|
|
|
|
#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: "
|
|
|
|
|
2004-04-16 02:41:56 +02:00
|
|
|
#define DIRECTORY_UPDATE_EXIT_NOUPDATE 0
|
|
|
|
#define DIRECTORY_UPDATE_EXIT_UPDATE 1
|
|
|
|
#define DIRECTORY_UPDATE_EXIT_ERROR 2
|
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
enum update_return {
|
|
|
|
UPDATE_RETURN_ERROR = -1,
|
|
|
|
UPDATE_RETURN_NOUPDATE = 0,
|
|
|
|
UPDATE_RETURN_UPDATED = 1
|
|
|
|
};
|
2004-04-16 02:48:47 +02:00
|
|
|
|
2008-09-23 22:37:18 +02:00
|
|
|
enum update_progress {
|
|
|
|
UPDATE_PROGRESS_IDLE = 0,
|
|
|
|
UPDATE_PROGRESS_RUNNING = 1,
|
|
|
|
UPDATE_PROGRESS_DONE = 2
|
|
|
|
} progress;
|
|
|
|
|
2007-01-14 04:07:53 +01:00
|
|
|
static Directory *mp3rootDirectory;
|
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-09-23 22:37:18 +02:00
|
|
|
static pthread_t update_thr;
|
2004-04-12 01:07:43 +02:00
|
|
|
|
2008-09-23 22:37:18 +02:00
|
|
|
static int directory_updateJobId;
|
2004-04-11 05:12:00 +02:00
|
|
|
|
2008-01-26 13:46:09 +01:00
|
|
|
static DirectoryList *newDirectoryList(void);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-23 22:36:00 +02:00
|
|
|
static enum update_return
|
|
|
|
addToDirectory(Directory * directory,
|
|
|
|
const char *shortname, const char *name);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-14 21:11:11 +02:00
|
|
|
static void freeDirectoryList(DirectoryList * list);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-14 21:11:11 +02:00
|
|
|
static void freeDirectory(Directory * directory);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
static enum update_return exploreDirectory(Directory * directory);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
static enum update_return updateDirectory(Directory * directory);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-14 21:11:11 +02:00
|
|
|
static void deleteEmptyDirectoriesInDirectory(Directory * directory);
|
2004-03-09 23:48:35 +01:00
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static void removeSongFromDirectory(Directory * directory,
|
|
|
|
const char *shortname);
|
2004-04-11 19:37:47 +02:00
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
static enum update_return addSubDirectoryToDirectory(Directory * directory,
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *shortname,
|
|
|
|
const char *name, struct stat *st);
|
2004-04-14 04:35:29 +02:00
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static Directory *getDirectoryDetails(const char *name,
|
|
|
|
const char **shortname);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static Directory *getDirectory(const char *name);
|
2004-04-11 19:37:47 +02:00
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static Song *getSongDetails(const char *file, const char **shortnameRet,
|
2006-07-20 18:02:40 +02:00
|
|
|
Directory ** directoryRet);
|
2004-04-11 19:37:47 +02:00
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
static enum update_return updatePath(const char *utf8path);
|
2004-04-14 04:35:29 +02:00
|
|
|
|
2006-07-14 21:11:11 +02:00
|
|
|
static void sortDirectory(Directory * directory);
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2006-07-14 21:11:11 +02:00
|
|
|
static int inodeFoundInParent(Directory * parent, ino_t inode, dev_t device);
|
2004-06-23 05:33:35 +02:00
|
|
|
|
2006-07-14 21:11:11 +02:00
|
|
|
static int statDirectory(Directory * dir);
|
2004-06-23 06:20:35 +02: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;
|
|
|
|
}
|
|
|
|
|
2006-08-08 04:23:21 +02:00
|
|
|
int isUpdatingDB(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-23 22:37:18 +02:00
|
|
|
return (progress != UPDATE_PROGRESS_IDLE) ? directory_updateJobId : 0;
|
2004-04-11 04:34:26 +02:00
|
|
|
}
|
|
|
|
|
2008-09-23 22:37:18 +02:00
|
|
|
void reap_update_task(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-29 12:15:16 +02:00
|
|
|
enum update_return ret;
|
|
|
|
|
2008-09-23 22:37:18 +02:00
|
|
|
if (progress != UPDATE_PROGRESS_DONE)
|
|
|
|
return;
|
2008-09-29 12:15:16 +02:00
|
|
|
if (pthread_join(update_thr, (void **)&ret))
|
|
|
|
FATAL("error joining update thread: %s\n", strerror(errno));
|
|
|
|
if (ret == UPDATE_RETURN_UPDATED)
|
|
|
|
playlistVersionChange();
|
2008-09-23 22:37:18 +02:00
|
|
|
progress = UPDATE_PROGRESS_IDLE;
|
2004-04-11 03:53:25 +02:00
|
|
|
}
|
|
|
|
|
2008-09-23 22:37:18 +02:00
|
|
|
static void * update_task(void *arg)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-23 22:37:18 +02:00
|
|
|
List *path_list = (List *)arg;
|
|
|
|
enum update_return ret = UPDATE_RETURN_NOUPDATE;
|
|
|
|
|
|
|
|
if (path_list) {
|
|
|
|
ListNode *node = path_list->firstNode;
|
|
|
|
|
|
|
|
while (node) {
|
|
|
|
switch (updatePath(node->key)) {
|
|
|
|
case UPDATE_RETURN_ERROR:
|
|
|
|
ret = UPDATE_RETURN_ERROR;
|
|
|
|
goto out;
|
|
|
|
case UPDATE_RETURN_NOUPDATE:
|
|
|
|
break;
|
|
|
|
case UPDATE_RETURN_UPDATED:
|
|
|
|
ret = UPDATE_RETURN_UPDATED;
|
|
|
|
}
|
|
|
|
node = node->nextNode;
|
2008-09-23 20:48:44 +02:00
|
|
|
}
|
2008-09-23 22:38:39 +02:00
|
|
|
freeList(path_list);
|
2008-09-23 22:37:18 +02:00
|
|
|
} else {
|
|
|
|
ret = updateDirectory(mp3rootDirectory);
|
2004-04-12 01:07:43 +02:00
|
|
|
}
|
2008-09-23 22:37:18 +02:00
|
|
|
|
|
|
|
if (ret == UPDATE_RETURN_UPDATED && writeDirectoryDB() < 0)
|
|
|
|
ret = UPDATE_RETURN_ERROR;
|
|
|
|
out:
|
|
|
|
progress = UPDATE_PROGRESS_DONE;
|
|
|
|
wakeup_main_task();
|
|
|
|
return (void *)ret;
|
2004-04-12 01:07:43 +02:00
|
|
|
}
|
|
|
|
|
2008-09-23 22:37:18 +02:00
|
|
|
int updateInit(List * path_list)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-23 22:37:18 +02:00
|
|
|
pthread_attr_t attr;
|
2004-04-16 02:41:56 +02:00
|
|
|
|
2008-09-23 22:37:18 +02:00
|
|
|
if (progress != UPDATE_PROGRESS_IDLE)
|
2004-04-11 03:53:25 +02:00
|
|
|
return -1;
|
|
|
|
|
2008-09-23 22:37:18 +02:00
|
|
|
progress = UPDATE_PROGRESS_RUNNING;
|
|
|
|
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
if (pthread_create(&update_thr, &attr, update_task, path_list))
|
|
|
|
FATAL("Failed to spawn update task: %s\n", strerror(errno));
|
2004-04-11 05:12:00 +02:00
|
|
|
directory_updateJobId++;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (directory_updateJobId > 1 << 15)
|
|
|
|
directory_updateJobId = 1;
|
2008-09-23 22:37:18 +02:00
|
|
|
DEBUG("updateInit: spawned update thread for update job id %i\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
(int)directory_updateJobId);
|
2004-04-11 03:53:25 +02:00
|
|
|
|
2008-09-07 13:50:06 +02:00
|
|
|
return (int)directory_updateJobId;
|
2004-04-11 03:53:25 +02:00
|
|
|
}
|
|
|
|
|
2008-09-23 20:47:45 +02:00
|
|
|
static void directory_set_stat(Directory * dir, const struct stat *st)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-23 20:47:45 +02:00
|
|
|
dir->inode = st->st_ino;
|
|
|
|
dir->device = st->st_dev;
|
|
|
|
dir->stat = 1;
|
2004-06-23 05:33:35 +02:00
|
|
|
}
|
|
|
|
|
2006-08-20 02:50:44 +02:00
|
|
|
static DirectoryList *newDirectoryList(void)
|
|
|
|
{
|
|
|
|
return makeList((ListFreeDataFunc *) freeDirectory, 1);
|
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static Directory *newDirectory(const char *dirname, Directory * parent)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
Directory *directory;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-23 20:48:39 +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);
|
2006-07-20 18:02:40 +02:00
|
|
|
else
|
|
|
|
directory->path = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
directory->subDirectories = newDirectoryList();
|
2008-09-23 20:48:39 +02:00
|
|
|
assert(!directory->songs.base);
|
2008-09-23 20:47:45 +02:00
|
|
|
directory->stat = 0;
|
|
|
|
directory->inode = 0;
|
|
|
|
directory->device = 0;
|
2004-06-23 05:33:35 +02:00
|
|
|
directory->parent = parent;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void freeDirectory(Directory * directory)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
freeDirectoryList(directory->subDirectories);
|
2008-09-23 20:48:39 +02:00
|
|
|
songvec_free(&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
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void freeDirectoryList(DirectoryList * directoryList)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
freeList(directoryList);
|
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static void removeSongFromDirectory(Directory * directory, const char *shortname)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-23 20:48:39 +02:00
|
|
|
Song *song = songvec_find(&directory->songs, shortname);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-09-23 20:48:39 +02:00
|
|
|
if (song) {
|
2007-12-28 03:56:25 +01:00
|
|
|
char path_max_tmp[MPD_PATH_MAX]; /* wasteful */
|
2008-09-23 20:48:39 +02:00
|
|
|
LOG("removing: %s\n", get_song_url(path_max_tmp, song));
|
|
|
|
songvec_delete(&directory->songs, song);
|
|
|
|
freeSong(song);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void deleteEmptyDirectoriesInDirectory(Directory * directory)
|
|
|
|
{
|
|
|
|
ListNode *node = directory->subDirectories->firstNode;
|
|
|
|
ListNode *nextNode;
|
|
|
|
Directory *subDir;
|
2004-03-09 23:48:35 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (node) {
|
|
|
|
subDir = (Directory *) node->data;
|
2004-03-09 23:48:35 +01:00
|
|
|
deleteEmptyDirectoriesInDirectory(subDir);
|
|
|
|
nextNode = node->nextNode;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (subDir->subDirectories->numberOfNodes == 0 &&
|
2008-09-23 20:48:39 +02:00
|
|
|
subDir->songs.nr == 0) {
|
2006-07-20 18:02:40 +02:00
|
|
|
deleteNodeFromList(directory->subDirectories, node);
|
2004-03-09 23:48:35 +01:00
|
|
|
}
|
|
|
|
node = nextNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
static enum update_return updateInDirectory(Directory * directory,
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *shortname, const char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-23 20:48:39 +02:00
|
|
|
Song *song;
|
2006-07-20 18:02:40 +02:00
|
|
|
void *subDir;
|
2004-06-22 02:55:23 +02:00
|
|
|
struct stat st;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (myStat(name, &st))
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_ERROR;
|
2004-06-22 02:55:23 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (S_ISREG(st.st_mode) && hasMusicSuffix(name, 0)) {
|
2008-09-23 20:48:39 +02:00
|
|
|
if (!(song = songvec_find(&directory->songs, shortname))) {
|
2004-11-11 03:36:25 +01:00
|
|
|
addToDirectory(directory, shortname, name);
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_UPDATED;
|
2008-09-23 20:48:39 +02:00
|
|
|
} else if (st.st_mtime != song->mtime) {
|
2006-07-20 18:02:40 +02:00
|
|
|
LOG("updating %s\n", name);
|
2008-09-23 20:48:39 +02:00
|
|
|
if (updateSongInfo(song) < 0)
|
2006-07-20 18:02:40 +02:00
|
|
|
removeSongFromDirectory(directory, shortname);
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_UPDATED;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (S_ISDIR(st.st_mode)) {
|
|
|
|
if (findInList
|
|
|
|
(directory->subDirectories, shortname, (void **)&subDir)) {
|
2008-09-23 20:47:45 +02:00
|
|
|
directory_set_stat((Directory *)subDir, &st);
|
2006-07-20 18:02:40 +02:00
|
|
|
return updateDirectory((Directory *) subDir);
|
|
|
|
} else {
|
|
|
|
return addSubDirectoryToDirectory(directory, shortname,
|
|
|
|
name, &st);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_NOUPDATE;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
/* we don't look at hidden files nor files with newlines in them */
|
|
|
|
static int skip_path(const char *path)
|
|
|
|
{
|
|
|
|
return (path[0] == '.' || strchr(path, '\n')) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
static enum update_return
|
|
|
|
removeDeletedFromDirectory(char *path_max_tmp, Directory * directory)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2007-12-28 03:56:25 +01:00
|
|
|
const char *dirname = (directory && directory->path) ?
|
|
|
|
directory->path : NULL;
|
|
|
|
ListNode *node, *tmpNode;
|
|
|
|
DirectoryList *subdirs = directory->subDirectories;
|
2008-09-23 20:49:05 +02:00
|
|
|
enum update_return ret = UPDATE_RETURN_NOUPDATE;
|
2008-09-23 20:48:39 +02:00
|
|
|
int i;
|
|
|
|
struct songvec *sv = &directory->songs;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
node = subdirs->firstNode;
|
2006-07-20 18:02:40 +02:00
|
|
|
while (node) {
|
2004-02-24 00:41:20 +01:00
|
|
|
tmpNode = node->nextNode;
|
2007-12-28 03:56:25 +01:00
|
|
|
if (node->key) {
|
|
|
|
if (dirname)
|
|
|
|
sprintf(path_max_tmp, "%s/%s", dirname,
|
|
|
|
node->key);
|
|
|
|
else
|
|
|
|
strcpy(path_max_tmp, node->key);
|
|
|
|
|
|
|
|
if (!isDir(path_max_tmp)) {
|
|
|
|
LOG("removing directory: %s\n", path_max_tmp);
|
|
|
|
deleteFromList(subdirs, node->key);
|
2008-09-23 20:49:05 +02:00
|
|
|
ret = UPDATE_RETURN_UPDATED;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
node = tmpNode;
|
|
|
|
}
|
|
|
|
|
2008-09-23 20:48:39 +02:00
|
|
|
for (i = sv->nr; --i >= 0; ) { /* cleaner deletes if we go backwards */
|
|
|
|
Song *song = sv->base[i];
|
|
|
|
if (!song || !song->url)
|
|
|
|
continue; /* does this happen?, perhaps assert() */
|
2007-12-28 03:56:25 +01:00
|
|
|
|
2008-09-23 20:48:39 +02:00
|
|
|
if (dirname)
|
|
|
|
sprintf(path_max_tmp, "%s/%s", dirname, song->url);
|
|
|
|
else
|
|
|
|
strcpy(path_max_tmp, song->url);
|
|
|
|
|
|
|
|
if (!isFile(path_max_tmp, NULL)) {
|
|
|
|
removeSongFromDirectory(directory, song->url);
|
2008-09-23 20:49:05 +02:00
|
|
|
ret = UPDATE_RETURN_UPDATED;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-16 02:41:56 +02:00
|
|
|
return ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static Directory *addDirectoryPathToDB(const char *utf8path,
|
|
|
|
const char **shortname)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2007-12-28 03:56:25 +01:00
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
2006-07-20 18:02:40 +02:00
|
|
|
char *parent;
|
|
|
|
Directory *parentDirectory;
|
|
|
|
void *directory;
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
parent = parent_path(path_max_tmp, utf8path);
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (strlen(parent) == 0)
|
|
|
|
parentDirectory = (void *)mp3rootDirectory;
|
|
|
|
else
|
|
|
|
parentDirectory = addDirectoryPathToDB(parent, shortname);
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
if (!parentDirectory)
|
2004-06-23 07:08:01 +02:00
|
|
|
return NULL;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
*shortname = utf8path + strlen(parent);
|
|
|
|
while (*(*shortname) && *(*shortname) == '/')
|
|
|
|
(*shortname)++;
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!findInList
|
|
|
|
(parentDirectory->subDirectories, *shortname, &directory)) {
|
2004-06-23 07:08:01 +02:00
|
|
|
struct stat st;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (myStat(utf8path, &st) < 0 ||
|
2007-12-28 03:56:25 +01:00
|
|
|
inodeFoundInParent(parentDirectory, st.st_ino, st.st_dev))
|
2004-06-23 07:08:01 +02:00
|
|
|
return NULL;
|
2007-12-28 03:56:25 +01:00
|
|
|
else {
|
2006-07-20 18:02:40 +02:00
|
|
|
directory = newDirectory(utf8path, parentDirectory);
|
|
|
|
insertInList(parentDirectory->subDirectories,
|
|
|
|
*shortname, directory);
|
2004-06-23 07:08:01 +02:00
|
|
|
}
|
2004-04-14 07:26:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if we're adding directory paths, make sure to delete filenames
|
2006-07-20 18:02:40 +02:00
|
|
|
with potentially the same name */
|
|
|
|
removeSongFromDirectory(parentDirectory, *shortname);
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return (Directory *) directory;
|
2004-04-14 07:26:32 +02:00
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static Directory *addParentPathToDB(const char *utf8path, const char **shortname)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
char *parent;
|
2007-12-28 03:56:25 +01:00
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
2006-07-20 18:02:40 +02:00
|
|
|
Directory *parentDirectory;
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
parent = parent_path(path_max_tmp, utf8path);
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (strlen(parent) == 0)
|
|
|
|
parentDirectory = (void *)mp3rootDirectory;
|
|
|
|
else
|
|
|
|
parentDirectory = addDirectoryPathToDB(parent, shortname);
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
if (!parentDirectory)
|
2004-06-23 07:08:01 +02:00
|
|
|
return NULL;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
*shortname = utf8path + strlen(parent);
|
|
|
|
while (*(*shortname) && *(*shortname) == '/')
|
|
|
|
(*shortname)++;
|
2004-04-14 07:26:32 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return (Directory *) parentDirectory;
|
2004-04-14 07:26:32 +02:00
|
|
|
}
|
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
static enum update_return updatePath(const char *utf8path)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
Directory *directory;
|
|
|
|
Directory *parentDirectory;
|
|
|
|
Song *song;
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *shortname;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *path = sanitizePathDup(utf8path);
|
|
|
|
time_t mtime;
|
2008-09-23 20:49:05 +02:00
|
|
|
enum update_return ret = UPDATE_RETURN_NOUPDATE;
|
2007-12-28 03:56:25 +01:00
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (NULL == path)
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_ERROR;
|
2004-04-14 04:35:29 +02:00
|
|
|
|
2004-04-14 04:55:19 +02:00
|
|
|
/* if path is in the DB try to update it, or else delete it */
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((directory = getDirectoryDetails(path, &shortname))) {
|
2004-06-23 05:33:35 +02:00
|
|
|
parentDirectory = directory->parent;
|
|
|
|
|
2004-04-14 07:26:32 +02:00
|
|
|
/* if this update directory is successfull, we are done */
|
2008-09-23 22:36:00 +02:00
|
|
|
ret = updateDirectory(directory);
|
|
|
|
if (ret != UPDATE_RETURN_ERROR) {
|
2004-04-14 07:26:32 +02:00
|
|
|
free(path);
|
|
|
|
sortDirectory(directory);
|
2004-04-16 02:41:56 +02:00
|
|
|
return ret;
|
2004-04-14 07:26:32 +02:00
|
|
|
}
|
|
|
|
/* we don't want to delete the root directory */
|
2006-07-20 18:02:40 +02:00
|
|
|
else if (directory == mp3rootDirectory) {
|
2004-04-14 07:26:32 +02:00
|
|
|
free(path);
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_NOUPDATE;
|
2004-04-14 07:26:32 +02:00
|
|
|
}
|
2007-04-25 22:18:47 +02:00
|
|
|
/* if updateDirectory fails, means we should delete it */
|
2004-04-14 07:26:32 +02:00
|
|
|
else {
|
2006-07-20 18:02:40 +02:00
|
|
|
LOG("removing directory: %s\n", path);
|
2004-04-14 04:35:29 +02:00
|
|
|
deleteFromList(parentDirectory->subDirectories,
|
2006-07-20 18:02:40 +02:00
|
|
|
shortname);
|
2008-09-23 20:49:05 +02:00
|
|
|
ret = UPDATE_RETURN_UPDATED;
|
2006-07-20 18:02:40 +02:00
|
|
|
/* don't return, path maybe a song now */
|
2004-04-14 04:35:29 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if ((song = getSongDetails(path, &shortname, &parentDirectory))) {
|
|
|
|
if (!parentDirectory->stat
|
|
|
|
&& statDirectory(parentDirectory) < 0) {
|
2004-06-23 06:20:35 +02:00
|
|
|
free(path);
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_NOUPDATE;
|
2004-06-23 06:20:35 +02:00
|
|
|
}
|
2004-04-14 07:26:32 +02:00
|
|
|
/* if this song update is successfull, we are done */
|
2006-07-20 18:02:40 +02:00
|
|
|
else if (0 == inodeFoundInParent(parentDirectory->parent,
|
2008-09-23 20:47:45 +02:00
|
|
|
parentDirectory->inode,
|
|
|
|
parentDirectory->device)
|
2007-12-28 03:56:25 +01:00
|
|
|
&& song &&
|
|
|
|
isMusic(get_song_url(path_max_tmp, song), &mtime, 0)) {
|
2004-04-14 07:26:32 +02:00
|
|
|
free(path);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song->mtime == mtime)
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_NOUPDATE;
|
2006-07-20 18:02:40 +02:00
|
|
|
else if (updateSongInfo(song) == 0)
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_UPDATED;
|
2006-07-20 18:02:40 +02:00
|
|
|
else {
|
|
|
|
removeSongFromDirectory(parentDirectory,
|
|
|
|
shortname);
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_UPDATED;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-04-14 04:55:19 +02:00
|
|
|
}
|
2007-04-25 22:18:47 +02:00
|
|
|
/* if updateDirectory fails, means we should delete it */
|
2004-04-16 02:41:56 +02:00
|
|
|
else {
|
2006-07-20 18:02:40 +02:00
|
|
|
removeSongFromDirectory(parentDirectory, shortname);
|
2008-09-23 20:49:05 +02:00
|
|
|
ret = UPDATE_RETURN_UPDATED;
|
2006-07-20 18:02:40 +02:00
|
|
|
/* don't return, path maybe a directory now */
|
|
|
|
}
|
2004-04-14 04:55:19 +02:00
|
|
|
}
|
2004-04-14 07:26:32 +02:00
|
|
|
|
|
|
|
/* path not found in the db, see if it actually exists on the fs.
|
|
|
|
* Also, if by chance a directory was replaced by a file of the same
|
2006-07-20 18:02:40 +02:00
|
|
|
* name or vice versa, we need to add it to the db
|
|
|
|
*/
|
|
|
|
if (isDir(path) || isMusic(path, NULL, 0)) {
|
|
|
|
parentDirectory = addParentPathToDB(path, &shortname);
|
|
|
|
if (!parentDirectory || (!parentDirectory->stat &&
|
|
|
|
statDirectory(parentDirectory) < 0)) {
|
|
|
|
} else if (0 == inodeFoundInParent(parentDirectory->parent,
|
2008-09-23 20:47:45 +02:00
|
|
|
parentDirectory->inode,
|
|
|
|
parentDirectory->device)
|
2006-07-20 18:02:40 +02:00
|
|
|
&& addToDirectory(parentDirectory, shortname, path)
|
2008-09-23 22:36:00 +02:00
|
|
|
== UPDATE_RETURN_UPDATED) {
|
2008-09-23 20:49:05 +02:00
|
|
|
ret = UPDATE_RETURN_UPDATED;
|
2004-06-23 06:20:35 +02:00
|
|
|
}
|
2004-04-14 04:55:19 +02:00
|
|
|
}
|
2004-04-14 07:26:32 +02:00
|
|
|
|
|
|
|
free(path);
|
2004-04-16 02:41:56 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return ret;
|
2004-04-14 04:35:29 +02:00
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static const char *opendir_path(char *path_max_tmp, const char *dirname)
|
2007-12-28 03:56:25 +01:00
|
|
|
{
|
|
|
|
if (*dirname != '\0')
|
|
|
|
return rmp2amp_r(path_max_tmp,
|
|
|
|
utf8_to_fs_charset(path_max_tmp, dirname));
|
|
|
|
return musicDir;
|
|
|
|
}
|
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
static enum update_return updateDirectory(Directory * directory)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
DIR *dir;
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *dirname = getDirectoryPath(directory);
|
2007-12-28 03:56:25 +01:00
|
|
|
struct dirent *ent;
|
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
2008-09-23 20:49:05 +02:00
|
|
|
enum update_return ret = UPDATE_RETURN_NOUPDATE;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
if (!directory->stat && statDirectory(directory) < 0)
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_ERROR;
|
2007-12-28 03:56:25 +01:00
|
|
|
else if (inodeFoundInParent(directory->parent,
|
2008-09-23 20:47:45 +02:00
|
|
|
directory->inode,
|
|
|
|
directory->device))
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_ERROR;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
dir = opendir(opendir_path(path_max_tmp, dirname));
|
|
|
|
if (!dir)
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_ERROR;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
if (removeDeletedFromDirectory(path_max_tmp, directory) > 0)
|
2008-09-23 20:49:05 +02:00
|
|
|
ret = UPDATE_RETURN_UPDATED;
|
2004-06-22 03:04:13 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while ((ent = readdir(dir))) {
|
2007-12-28 03:56:25 +01:00
|
|
|
char *utf8;
|
|
|
|
if (skip_path(ent->d_name))
|
2006-07-20 18:02:40 +02:00
|
|
|
continue;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
utf8 = fs_charset_to_utf8(path_max_tmp, ent->d_name);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!utf8)
|
|
|
|
continue;
|
2004-04-13 06:59:57 +02:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
if (directory->path)
|
|
|
|
utf8 = pfx_dir(path_max_tmp, utf8, strlen(utf8),
|
|
|
|
dirname, strlen(dirname));
|
|
|
|
if (updateInDirectory(directory, utf8, path_max_tmp) > 0)
|
2008-09-23 20:49:05 +02:00
|
|
|
ret = UPDATE_RETURN_UPDATED;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
closedir(dir);
|
|
|
|
|
2004-04-16 02:41:56 +02:00
|
|
|
return ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
static enum update_return exploreDirectory(Directory * directory)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
DIR *dir;
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *dirname = getDirectoryPath(directory);
|
2007-12-28 03:56:25 +01:00
|
|
|
struct dirent *ent;
|
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
2008-09-23 20:49:05 +02:00
|
|
|
enum update_return ret = UPDATE_RETURN_NOUPDATE;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("explore: attempting to opendir: %s\n", dirname);
|
2007-12-28 03:56:25 +01:00
|
|
|
|
|
|
|
dir = opendir(opendir_path(path_max_tmp, dirname));
|
|
|
|
if (!dir)
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_ERROR;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("explore: %s\n", dirname);
|
2007-12-28 03:56:25 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while ((ent = readdir(dir))) {
|
2007-12-28 03:56:25 +01:00
|
|
|
char *utf8;
|
|
|
|
if (skip_path(ent->d_name))
|
2006-07-20 18:02:40 +02:00
|
|
|
continue;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
utf8 = fs_charset_to_utf8(path_max_tmp, ent->d_name);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!utf8)
|
|
|
|
continue;
|
2004-04-13 06:59:57 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("explore: found: %s (%s)\n", ent->d_name, utf8);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
if (directory->path)
|
|
|
|
utf8 = pfx_dir(path_max_tmp, utf8, strlen(utf8),
|
|
|
|
dirname, strlen(dirname));
|
2008-09-23 22:36:00 +02:00
|
|
|
if (addToDirectory(directory, utf8, path_max_tmp) ==
|
|
|
|
UPDATE_RETURN_UPDATED)
|
2008-09-23 20:49:05 +02:00
|
|
|
ret = UPDATE_RETURN_UPDATED;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
closedir(dir);
|
|
|
|
|
2004-04-17 01:49:48 +02:00
|
|
|
return ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int statDirectory(Directory * dir)
|
|
|
|
{
|
2004-06-23 05:33:35 +02:00
|
|
|
struct stat st;
|
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
if (myStat(getDirectoryPath(dir), &st) < 0)
|
2004-11-11 05:34:26 +01:00
|
|
|
return -1;
|
2004-06-23 05:33:35 +02:00
|
|
|
|
2008-09-23 20:47:45 +02:00
|
|
|
directory_set_stat(dir, &st);
|
2004-06-23 05:33:35 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int inodeFoundInParent(Directory * parent, ino_t inode, dev_t device)
|
|
|
|
{
|
|
|
|
while (parent) {
|
2008-09-23 20:47:45 +02:00
|
|
|
if (!parent->stat && statDirectory(parent) < 0)
|
|
|
|
return -1;
|
|
|
|
if (parent->inode == inode && parent->device == device) {
|
2004-06-23 06:20:35 +02:00
|
|
|
DEBUG("recursive directory found\n");
|
2004-06-23 05:33:35 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
parent = parent->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
static enum update_return addSubDirectoryToDirectory(Directory * directory,
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *shortname,
|
|
|
|
const char *name, struct stat *st)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
Directory *subDirectory;
|
2004-06-23 05:33:35 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (inodeFoundInParent(directory, st->st_ino, st->st_dev))
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_NOUPDATE;
|
2004-06-23 05:33:35 +02:00
|
|
|
|
2004-11-12 18:38:52 +01:00
|
|
|
subDirectory = newDirectory(name, directory);
|
2008-09-23 20:47:45 +02:00
|
|
|
directory_set_stat(subDirectory, st);
|
2004-04-17 01:49:48 +02:00
|
|
|
|
2008-09-23 22:36:00 +02:00
|
|
|
if (exploreDirectory(subDirectory) != UPDATE_RETURN_UPDATED) {
|
2006-07-20 18:02:40 +02:00
|
|
|
freeDirectory(subDirectory);
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_NOUPDATE;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
insertInList(directory->subDirectories, shortname, subDirectory);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-23 20:49:05 +02:00
|
|
|
return UPDATE_RETURN_UPDATED;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-09-23 22:36:00 +02:00
|
|
|
static enum update_return
|
|
|
|
addToDirectory(Directory * directory,
|
|
|
|
const char *shortname, const char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-06-22 02:55:23 +02:00
|
|
|
struct stat st;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (myStat(name, &st)) {
|
2005-03-06 20:00:58 +01:00
|
|
|
DEBUG("failed to stat %s: %s\n", name, strerror(errno));
|
2008-09-23 22:36:00 +02:00
|
|
|
return UPDATE_RETURN_ERROR;
|
2005-03-06 20:00:58 +01:00
|
|
|
}
|
2004-06-22 02:55:23 +02:00
|
|
|
|
2008-09-23 20:48:39 +02:00
|
|
|
if (S_ISREG(st.st_mode) &&
|
|
|
|
hasMusicSuffix(name, 0) && isMusic(name, NULL, 0)) {
|
|
|
|
Song *song = newSong(shortname, SONG_TYPE_FILE, directory);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!song)
|
2008-09-23 22:36:00 +02:00
|
|
|
return UPDATE_RETURN_ERROR;
|
2008-09-23 20:48:39 +02:00
|
|
|
songvec_add(&directory->songs, song);
|
2004-11-11 03:36:25 +01:00
|
|
|
LOG("added %s\n", name);
|
2008-09-23 22:36:00 +02:00
|
|
|
return UPDATE_RETURN_UPDATED;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (S_ISDIR(st.st_mode)) {
|
2004-06-23 05:33:35 +02:00
|
|
|
return addSubDirectoryToDirectory(directory, shortname, name,
|
2006-07-20 18:02:40 +02:00
|
|
|
&st);
|
2004-06-22 02:55:23 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("addToDirectory: %s is not a directory or music\n", name);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-23 22:36:00 +02:00
|
|
|
return UPDATE_RETURN_ERROR;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-08-08 04:23:21 +02:00
|
|
|
void closeMp3Directory(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
freeDirectory(mp3rootDirectory);
|
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static Directory *findSubDirectory(Directory * directory, const char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
void *subDirectory;
|
2008-01-26 13:46:21 +01:00
|
|
|
char *duplicated = xstrdup(name);
|
2006-07-20 18:02:40 +02:00
|
|
|
char *key;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
key = strtok(duplicated, "/");
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!key) {
|
2008-01-26 13:46:21 +01:00
|
|
|
free(duplicated);
|
2004-02-24 00:41:20 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (findInList(directory->subDirectories, key, &subDirectory)) {
|
2008-01-26 13:46:21 +01:00
|
|
|
free(duplicated);
|
2006-07-20 18:02:40 +02:00
|
|
|
return (Directory *) subDirectory;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
free(duplicated);
|
2004-02-24 00:41:20 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
int isRootDirectory(const char *name)
|
2007-05-24 19:06:59 +02:00
|
|
|
{
|
|
|
|
if (name == NULL || name[0] == '\0' || strcmp(name, "/") == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static Directory *getSubDirectory(Directory * directory, const char *name,
|
|
|
|
const char **shortname)
|
2004-04-14 04:35:29 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
Directory *subDirectory;
|
2004-02-24 00:41:20 +01:00
|
|
|
int len;
|
|
|
|
|
2007-05-24 19:06:59 +02:00
|
|
|
if (isRootDirectory(name)) {
|
2004-02-24 00:41:20 +01:00
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if ((subDirectory = findSubDirectory(directory, name)) == NULL)
|
|
|
|
return NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-04-14 04:35:29 +02:00
|
|
|
*shortname = name;
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
len = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
while (name[len] != '/' && name[len] != '\0')
|
|
|
|
len++;
|
|
|
|
while (name[len] == '/')
|
|
|
|
len++;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return getSubDirectory(subDirectory, &(name[len]), shortname);
|
2004-04-14 04:35:29 +02:00
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static Directory *getDirectoryDetails(const char *name, const char **shortname)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-04-14 04:35:29 +02:00
|
|
|
*shortname = NULL;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return getSubDirectory(mp3rootDirectory, name, shortname);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
static Directory *getDirectory(const char *name)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *shortname;
|
2004-04-14 04:35:29 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return getSubDirectory(mp3rootDirectory, name, &shortname);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:53:55 +02:00
|
|
|
static int printDirectoryList(struct client *client, DirectoryList * directoryList)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
ListNode *node = directoryList->firstNode;
|
|
|
|
Directory *directory;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (node != NULL) {
|
|
|
|
directory = (Directory *) node->data;
|
2008-09-07 13:53:55 +02:00
|
|
|
client_printf(client, "%s%s\n", DIRECTORY_DIR,
|
|
|
|
getDirectoryPath(directory));
|
2004-02-24 00:41:20 +01:00
|
|
|
node = node->nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
Directory *directory;
|
|
|
|
|
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-07 13:53:55 +02:00
|
|
|
printDirectoryList(client, directory->subDirectories);
|
2008-09-23 20:48:39 +02:00
|
|
|
songvec_print(client, &directory->songs);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void writeDirectoryInfo(FILE * fp, Directory * directory)
|
|
|
|
{
|
|
|
|
ListNode *node = (directory->subDirectories)->firstNode;
|
|
|
|
Directory *subDirectory;
|
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,
|
2006-07-20 18:02:40 +02:00
|
|
|
getDirectoryPath(directory));
|
2007-07-16 22:31:37 +02:00
|
|
|
if (retv < 0) {
|
|
|
|
ERROR("Failed to write data to database file: %s\n",strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
while (node != NULL) {
|
|
|
|
subDirectory = (Directory *) node->data;
|
2007-07-16 22:31:37 +02:00
|
|
|
retv = fprintf(fp, "%s%s\n", DIRECTORY_DIR, node->key);
|
|
|
|
if (retv < 0) {
|
|
|
|
ERROR("Failed to write data to database file: %s\n",strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
writeDirectoryInfo(fp, subDirectory);
|
2004-02-24 00:41:20 +01:00
|
|
|
node = node->nextNode;
|
|
|
|
}
|
|
|
|
|
2008-09-23 20:48:39 +02:00
|
|
|
songvec_save(fp, &directory->songs);
|
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_END,
|
2006-07-20 18:02:40 +02:00
|
|
|
getDirectoryPath(directory));
|
2007-07-16 22:31:37 +02:00
|
|
|
if (retv < 0) {
|
|
|
|
ERROR("Failed to write data to database file: %s\n",strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void readDirectoryInfo(FILE * fp, Directory * directory)
|
|
|
|
{
|
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
|
|
|
Directory *subDirectory;
|
2004-04-10 19:56:01 +02:00
|
|
|
int strcmpRet;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *name;
|
|
|
|
ListNode *nextDirNode = directory->subDirectories->firstNode;
|
|
|
|
ListNode *nodeTemp;
|
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-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)]);
|
2004-04-10 19:56:01 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (nextDirNode && (strcmpRet =
|
|
|
|
strcmp(key,
|
|
|
|
nextDirNode->key)) > 0) {
|
2004-04-10 19:56:01 +02:00
|
|
|
nodeTemp = nextDirNode->nextNode;
|
|
|
|
deleteNodeFromList(directory->subDirectories,
|
2006-07-20 18:02:40 +02:00
|
|
|
nextDirNode);
|
2004-04-10 19:56:01 +02:00
|
|
|
nextDirNode = nodeTemp;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (NULL == nextDirNode) {
|
2004-11-12 18:38:52 +01:00
|
|
|
subDirectory = newDirectory(name, directory);
|
2004-11-11 05:34:26 +01:00
|
|
|
insertInList(directory->subDirectories,
|
2006-07-20 18:02:40 +02:00
|
|
|
key, (void *)subDirectory);
|
|
|
|
} else if (strcmpRet == 0) {
|
|
|
|
subDirectory = (Directory *) nextDirNode->data;
|
2004-04-10 19:56:01 +02:00
|
|
|
nextDirNode = nextDirNode->nextNode;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2004-11-12 18:38:52 +01:00
|
|
|
subDirectory = newDirectory(name, directory);
|
2006-07-20 18:02:40 +02:00
|
|
|
insertInListBeforeNode(directory->
|
|
|
|
subDirectories,
|
|
|
|
nextDirNode, -1, key,
|
|
|
|
(void *)subDirectory);
|
2004-04-10 19:56:01 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
readDirectoryInfo(fp, subDirectory);
|
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
|
|
|
}
|
|
|
|
}
|
2004-04-10 19:56:01 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (nextDirNode) {
|
2004-04-10 19:56:01 +02:00
|
|
|
nodeTemp = nextDirNode->nextNode;
|
2006-07-20 18:02:40 +02:00
|
|
|
deleteNodeFromList(directory->subDirectories, nextDirNode);
|
2004-04-10 19:56:01 +02:00
|
|
|
nextDirNode = nodeTemp;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void sortDirectory(Directory * directory)
|
|
|
|
{
|
|
|
|
ListNode *node = directory->subDirectories->firstNode;
|
|
|
|
Directory *subDir;
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
sortList(directory->subDirectories);
|
2008-09-23 20:48:39 +02:00
|
|
|
songvec_sort(&directory->songs);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (node != NULL) {
|
|
|
|
subDir = (Directory *) node->data;
|
2004-02-24 00:41:20 +01:00
|
|
|
sortDirectory(subDir);
|
|
|
|
node = node->nextNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
2004-03-09 23:48:35 +01:00
|
|
|
deleteEmptyDirectoriesInDirectory(mp3rootDirectory);
|
2004-11-14 00:22:22 +01:00
|
|
|
|
|
|
|
DEBUG("sorting DB\n");
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
sortDirectory(mp3rootDirectory);
|
|
|
|
|
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
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
writeDirectoryInfo(fp, mp3rootDirectory);
|
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
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!mp3rootDirectory)
|
|
|
|
mp3rootDirectory = newDirectory(NULL, NULL);
|
|
|
|
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");
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
readDirectoryInfo(fp, mp3rootDirectory);
|
|
|
|
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-09-07 13:48:24 +02:00
|
|
|
static int traverseAllInSubDirectory(Directory * directory,
|
|
|
|
int (*forEachSong) (Song *, void *),
|
|
|
|
int (*forEachDir) (Directory *, void *),
|
|
|
|
void *data)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2008-09-23 20:48:39 +02:00
|
|
|
ListNode *node;
|
2006-07-20 18:02:40 +02:00
|
|
|
Directory *dir;
|
|
|
|
int errFlag = 0;
|
|
|
|
|
|
|
|
if (forEachDir) {
|
2008-09-07 13:48:24 +02:00
|
|
|
errFlag = forEachDir(directory, data);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (errFlag)
|
|
|
|
return errFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forEachSong) {
|
2008-09-23 20:48:39 +02:00
|
|
|
int i;
|
|
|
|
struct songvec *sv = &directory->songs;
|
|
|
|
Song **sp = sv->base;
|
|
|
|
|
|
|
|
for (i = sv->nr; --i >= 0; ) {
|
|
|
|
Song *song = *sp++;
|
|
|
|
if ((errFlag = forEachSong(song, data)))
|
|
|
|
return errFlag;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node = directory->subDirectories->firstNode;
|
|
|
|
|
|
|
|
while (node != NULL && !errFlag) {
|
|
|
|
dir = (Directory *) node->data;
|
2008-09-07 13:48:24 +02:00
|
|
|
errFlag = traverseAllInSubDirectory(dir, forEachSong,
|
2006-07-20 18:02:40 +02:00
|
|
|
forEachDir, data);
|
|
|
|
node = node->nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return errFlag;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 13:48:37 +02:00
|
|
|
int traverseAllIn(const char *name,
|
2008-09-07 13:48:24 +02:00
|
|
|
int (*forEachSong) (Song *, void *),
|
|
|
|
int (*forEachDir) (Directory *, void *), void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-08-08 04:23:21 +02:00
|
|
|
void initMp3Directory(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-06-23 05:33:35 +02:00
|
|
|
mp3rootDirectory = newDirectory(NULL, NULL);
|
2004-02-24 00:41:20 +01:00
|
|
|
exploreDirectory(mp3rootDirectory);
|
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-02-05 11:17:33 +01:00
|
|
|
static Song *getSongDetails(const char *file, const char **shortnameRet,
|
2006-07-20 18:02:40 +02:00
|
|
|
Directory ** directoryRet)
|
2004-04-11 19:37:47 +02:00
|
|
|
{
|
2008-09-23 20:48:39 +02:00
|
|
|
Song *song;
|
2006-07-20 18:02:40 +02:00
|
|
|
Directory *directory;
|
|
|
|
char *dir = NULL;
|
2008-01-26 13:46:21 +01:00
|
|
|
char *duplicated = xstrdup(file);
|
|
|
|
char *shortname = duplicated;
|
|
|
|
char *c = strtok(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
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (c) {
|
2004-02-24 00:41:20 +01:00
|
|
|
shortname = c;
|
2006-07-20 18:02:40 +02:00
|
|
|
c = strtok(NULL, "/");
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
if (shortname != duplicated) {
|
|
|
|
for (c = duplicated; c < shortname - 1; c++) {
|
2006-07-20 18:02:40 +02:00
|
|
|
if (*c == '\0')
|
|
|
|
*c = '/';
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2008-01-26 13:46:21 +01:00
|
|
|
dir = duplicated;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!(directory = getDirectory(dir))) {
|
2008-01-26 13:46:21 +01:00
|
|
|
free(duplicated);
|
2004-02-24 00:41:20 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-09-23 20:48:39 +02:00
|
|
|
if (!(song = songvec_find(&directory->songs, shortname))) {
|
2008-01-26 13:46:21 +01:00
|
|
|
free(duplicated);
|
2004-02-24 00:41:20 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
free(duplicated);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (shortnameRet)
|
|
|
|
*shortnameRet = shortname;
|
|
|
|
if (directoryRet)
|
|
|
|
*directoryRet = directory;
|
2008-09-23 20:48:39 +02:00
|
|
|
return song;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-09-06 15:28:31 +02:00
|
|
|
Song *getSongFromDB(const char *file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
return getSongDetails(file, NULL, NULL);
|
2004-04-11 19:37:47 +02: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
|
|
|
}
|