2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "directory.h"
|
2008-10-31 09:20:02 +01:00
|
|
|
#include "song.h"
|
2008-10-13 16:32:39 +02:00
|
|
|
#include "path.h"
|
2011-09-10 19:24:30 +02:00
|
|
|
#include "db_visitor.h"
|
2005-03-06 20:00:58 +01:00
|
|
|
|
2009-01-02 16:26:19 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-10-09 15:23:37 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2009-01-02 16:26:19 +01:00
|
|
|
#include <stdlib.h>
|
2008-10-09 15:23:37 +02:00
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *
|
2008-10-13 09:55:00 +02:00
|
|
|
directory_new(const char *path, struct directory *parent)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *directory;
|
2008-10-13 09:55:00 +02:00
|
|
|
size_t pathlen = strlen(path);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-13 09:55:00 +02:00
|
|
|
assert(path != NULL);
|
|
|
|
assert((*path == 0) == (parent == NULL));
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-01-02 16:26:19 +01:00
|
|
|
directory = g_malloc0(sizeof(*directory) -
|
|
|
|
sizeof(directory->path) + pathlen + 1);
|
2004-06-23 05:33:35 +02:00
|
|
|
directory->parent = parent;
|
2008-10-13 09:55:00 +02:00
|
|
|
memcpy(directory->path, path, pathlen + 1);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2010-07-21 08:58:15 +02:00
|
|
|
playlist_vector_init(&directory->playlists);
|
|
|
|
|
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
|
|
|
{
|
2010-07-25 13:31:39 +02:00
|
|
|
playlist_vector_deinit(&directory->playlists);
|
|
|
|
|
2008-10-31 09:20:02 +01:00
|
|
|
for (unsigned i = 0; i < directory->songs.nr; ++i)
|
|
|
|
song_free(directory->songs.base[i]);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < directory->children.nr; ++i)
|
|
|
|
directory_free(directory->children.base[i]);
|
|
|
|
|
2008-09-29 13:11:40 +02:00
|
|
|
dirvec_destroy(&directory->children);
|
2008-09-29 12:17:13 +02:00
|
|
|
songvec_destroy(&directory->songs);
|
2009-01-25 18:47:21 +01:00
|
|
|
g_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
|
|
|
}
|
|
|
|
|
2012-01-24 19:07:11 +01:00
|
|
|
void
|
|
|
|
directory_delete(struct directory *directory)
|
|
|
|
{
|
|
|
|
assert(directory != NULL);
|
|
|
|
assert(directory->parent != NULL);
|
|
|
|
|
|
|
|
dirvec_delete(&directory->parent->children, directory);
|
|
|
|
directory_free(directory);
|
|
|
|
}
|
|
|
|
|
2008-10-13 16:32:39 +02:00
|
|
|
const char *
|
|
|
|
directory_get_name(const struct directory *directory)
|
|
|
|
{
|
2008-10-20 22:18:12 +02:00
|
|
|
return g_basename(directory->path);
|
2008-10-13 16:32:39 +02:00
|
|
|
}
|
|
|
|
|
2012-01-24 19:07:11 +01:00
|
|
|
struct directory *
|
|
|
|
directory_new_child(struct directory *parent, const char *name_utf8)
|
|
|
|
{
|
|
|
|
assert(parent != NULL);
|
|
|
|
assert(name_utf8 != NULL);
|
|
|
|
assert(*name_utf8 != 0);
|
|
|
|
|
|
|
|
char *allocated;
|
|
|
|
const char *path_utf8;
|
|
|
|
if (directory_is_root(parent)) {
|
|
|
|
allocated = NULL;
|
|
|
|
path_utf8 = name_utf8;
|
|
|
|
} else {
|
|
|
|
allocated = g_strconcat(directory_get_path(parent),
|
|
|
|
"/", name_utf8, NULL);
|
|
|
|
path_utf8 = allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct directory *directory = directory_new(path_utf8, parent);
|
|
|
|
g_free(allocated);
|
|
|
|
|
|
|
|
dirvec_add(&parent->children, directory);
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
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; ) {
|
2009-08-14 11:52:00 +02:00
|
|
|
struct directory *child = dv->base[i];
|
|
|
|
|
|
|
|
directory_prune_empty(child);
|
|
|
|
|
2012-01-24 19:07:11 +01:00
|
|
|
if (directory_is_empty(child))
|
|
|
|
directory_delete(child);
|
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 *
|
2009-04-01 18:41:33 +02:00
|
|
|
directory_lookup_directory(struct directory *directory, const char *uri)
|
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
|
|
|
|
2009-04-01 18:41:33 +02:00
|
|
|
assert(uri != NULL);
|
2008-10-08 11:06:44 +02:00
|
|
|
|
2009-04-01 18:41:33 +02:00
|
|
|
if (isRootDirectory(uri))
|
2004-02-24 00:41:20 +01:00
|
|
|
return directory;
|
|
|
|
|
2009-04-01 18:41:33 +02:00
|
|
|
duplicated = g_strdup(uri);
|
2008-09-29 13:11:40 +02:00
|
|
|
locate = strchr(duplicated, '/');
|
|
|
|
while (1) {
|
|
|
|
if (locate)
|
|
|
|
*locate = '\0';
|
2008-10-09 15:34:07 +02:00
|
|
|
if (!(found = directory_get_child(cur, duplicated)))
|
2008-09-29 13:11:40 +02:00
|
|
|
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
|
|
|
|
2009-01-25 18:47:21 +01:00
|
|
|
g_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
|
|
|
}
|
|
|
|
|
2009-04-01 18:41:37 +02:00
|
|
|
struct song *
|
|
|
|
directory_lookup_song(struct directory *directory, const char *uri)
|
|
|
|
{
|
|
|
|
char *duplicated, *base;
|
|
|
|
struct song *song;
|
|
|
|
|
|
|
|
assert(directory != NULL);
|
|
|
|
assert(uri != NULL);
|
|
|
|
|
|
|
|
duplicated = g_strdup(uri);
|
|
|
|
base = strrchr(duplicated, '/');
|
|
|
|
|
|
|
|
if (base != NULL) {
|
|
|
|
*base++ = 0;
|
|
|
|
directory = directory_lookup_directory(directory, duplicated);
|
|
|
|
if (directory == NULL) {
|
|
|
|
g_free(duplicated);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
base = duplicated;
|
|
|
|
|
|
|
|
song = songvec_find(&directory->songs, base);
|
|
|
|
assert(song == NULL || song->parent == directory);
|
|
|
|
|
|
|
|
g_free(duplicated);
|
|
|
|
return song;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-09-10 19:24:30 +02:00
|
|
|
bool
|
2011-09-10 18:48:10 +02:00
|
|
|
directory_walk(const struct directory *directory, bool recursive,
|
2011-09-10 19:24:30 +02:00
|
|
|
const struct db_visitor *visitor, void *ctx,
|
|
|
|
GError **error_r)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2011-09-10 19:24:30 +02:00
|
|
|
assert(directory != NULL);
|
|
|
|
assert(visitor != NULL);
|
|
|
|
assert(error_r == NULL || *error_r == NULL);
|
|
|
|
|
|
|
|
if (visitor->song != NULL) {
|
2011-09-10 18:48:10 +02:00
|
|
|
const struct songvec *sv = &directory->songs;
|
2011-09-10 19:24:30 +02:00
|
|
|
for (size_t i = 0; i < sv->nr; ++i)
|
|
|
|
if (!visitor->song(sv->base[i], ctx, error_r))
|
|
|
|
return false;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
2011-09-13 22:02:37 +02:00
|
|
|
if (visitor->playlist != NULL) {
|
|
|
|
const struct playlist_vector *pv = &directory->playlists;
|
|
|
|
for (const struct playlist_metadata *i = pv->head;
|
|
|
|
i != NULL; i = i->next)
|
2011-10-08 14:33:41 +02:00
|
|
|
if (!visitor->playlist(i, directory, ctx, error_r))
|
2011-09-13 22:02:37 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-13 20:41:06 +02:00
|
|
|
const struct dirvec *dv = &directory->children;
|
|
|
|
for (size_t i = 0; i < dv->nr; ++i) {
|
|
|
|
struct directory *child = dv->base[i];
|
|
|
|
|
2011-09-13 20:38:12 +02:00
|
|
|
if (visitor->directory != NULL &&
|
|
|
|
!visitor->directory(child, ctx, error_r))
|
|
|
|
return false;
|
|
|
|
|
2011-09-10 18:48:10 +02:00
|
|
|
if (recursive &&
|
|
|
|
!directory_walk(child, recursive, visitor, ctx, error_r))
|
2011-09-13 20:41:06 +02:00
|
|
|
return false;
|
2011-09-10 19:24:30 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2011-09-10 19:24:30 +02:00
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|