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"
|
2008-10-08 11:07:35 +02:00
|
|
|
#include "database.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2006-08-06 15:53:53 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "utils.h"
|
2008-09-29 12:16:57 +02:00
|
|
|
#include "client.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"
|
2005-03-06 20:00:58 +01:00
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_new(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 11:07:58 +02:00
|
|
|
directory_free(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 */
|
2008-10-08 11:07:58 +02:00
|
|
|
/*directory_get_path(NULL); */
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:07:35 +02:00
|
|
|
void
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_prune_empty(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; ) {
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_prune_empty(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-10-08 10:49:05 +02:00
|
|
|
struct directory *
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_get_directory(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-10-08 11:06:44 +02:00
|
|
|
assert(name != NULL);
|
|
|
|
|
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 11:07:35 +02:00
|
|
|
static int
|
2008-10-08 11:07:58 +02:00
|
|
|
dirvec_print(struct client *client, const 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",
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_get_path(dv->base[i]));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-08 11:07:35 +02:00
|
|
|
int
|
|
|
|
directory_print(struct client *client, const struct directory *directory)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 11:07:58 +02:00
|
|
|
dirvec_print(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 11:07:35 +02:00
|
|
|
int
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_save(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-10-08 11:07:58 +02:00
|
|
|
directory_get_path(directory));
|
2008-09-29 13:17:52 +02:00
|
|
|
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;
|
2008-10-08 11:07:58 +02:00
|
|
|
if (directory_save(fp, cur) < 0)
|
2008-09-29 13:17:52 +02:00
|
|
|
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",
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_get_path(directory)) < 0)
|
2008-09-29 13:17:52 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:07:35 +02:00
|
|
|
void
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_load(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-08 11:07:55 +02:00
|
|
|
if ((subdir = db_get_directory(name))) {
|
2008-10-06 18:37:13 +02:00
|
|
|
assert(subdir->parent == directory);
|
|
|
|
} else {
|
2008-10-08 11:07:58 +02:00
|
|
|
subdir = directory_new(name, directory);
|
2008-10-06 18:37:13 +02:00
|
|
|
dirvec_add(&directory->children, subdir);
|
|
|
|
}
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_load(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 11:07:58 +02:00
|
|
|
directory_sort(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; )
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_sort(dv->base[i]);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:07:35 +02:00
|
|
|
int
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_walk(struct directory *directory,
|
|
|
|
int (*forEachSong)(struct 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)
|
2008-10-08 11:07:58 +02:00
|
|
|
err = directory_walk(dv->base[j], forEachSong,
|
2008-10-07 22:07:44 +02:00
|
|
|
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
|
|
|
}
|