mpd/src/Directory.cxx

336 lines
6.2 KiB
C++
Raw Normal View History

/*
2011-01-29 10:13:54 +01:00
* Copyright (C) 2003-2011 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
2013-01-02 22:52:08 +01:00
#include "Directory.hxx"
2012-08-29 19:12:26 +02:00
#include "SongFilter.hxx"
2013-01-02 20:25:20 +01:00
#include "PlaylistVector.hxx"
2013-01-02 20:56:21 +01:00
#include "DatabaseLock.hxx"
2013-04-17 22:25:57 +02:00
#include "SongSort.hxx"
2013-07-28 13:25:12 +02:00
#include "Song.hxx"
2012-07-30 07:26:08 +02:00
extern "C" {
#include "util/list_sort.h"
2012-07-30 07:26:08 +02:00
}
2009-01-02 16:26:19 +01:00
#include <glib.h>
#include <assert.h>
#include <string.h>
2009-01-02 16:26:19 +01:00
#include <stdlib.h>
inline Directory *
Directory::Allocate(const char *path)
{
assert(path != NULL);
const size_t path_size = strlen(path) + 1;
Directory *directory =
(Directory *)g_malloc0(sizeof(*directory)
- sizeof(directory->path)
+ path_size);
new(directory) Directory(path);
return directory;
}
Directory::Directory()
{
INIT_LIST_HEAD(&children);
INIT_LIST_HEAD(&songs);
path[0] = 0;
}
Directory::Directory(const char *_path)
{
INIT_LIST_HEAD(&children);
INIT_LIST_HEAD(&songs);
strcpy(path, _path);
}
Directory::~Directory()
{
2013-07-28 13:25:12 +02:00
Song *song, *ns;
2013-01-02 23:06:10 +01:00
directory_for_each_song_safe(song, ns, this)
2013-07-28 13:25:12 +02:00
song->Free();
Directory *child, *n;
2013-01-02 23:06:10 +01:00
directory_for_each_child_safe(child, n, this)
child->Free();
}
Directory *
Directory::NewGeneric(const char *path, Directory *parent)
{
assert(path != NULL);
assert((*path == 0) == (parent == NULL));
Directory *directory = Allocate(path);
directory->parent = parent;
return directory;
}
void
Directory::Free()
{
this->Directory::~Directory();
2013-01-02 23:06:10 +01:00
g_free(this);
}
void
Directory::Delete()
{
assert(holding_db_lock());
2013-01-02 23:06:10 +01:00
assert(parent != nullptr);
2013-01-02 23:06:10 +01:00
list_del(&siblings);
Free();
}
const char *
Directory::GetName() const
{
2013-01-02 23:06:10 +01:00
assert(!IsRoot());
assert(path != nullptr);
2013-01-02 23:06:10 +01:00
const char *slash = strrchr(path, '/');
assert((slash == nullptr) == parent->IsRoot());
return slash != NULL
? slash + 1
2013-01-02 23:06:10 +01:00
: path;
}
Directory *
Directory::CreateChild(const char *name_utf8)
{
assert(holding_db_lock());
assert(name_utf8 != NULL);
assert(*name_utf8 != 0);
char *allocated;
const char *path_utf8;
2013-01-02 23:06:10 +01:00
if (IsRoot()) {
allocated = NULL;
path_utf8 = name_utf8;
} else {
2013-01-02 23:06:10 +01:00
allocated = g_strconcat(GetPath(),
"/", name_utf8, NULL);
path_utf8 = allocated;
}
Directory *child = NewGeneric(path_utf8, this);
g_free(allocated);
2013-01-02 23:06:10 +01:00
list_add_tail(&child->siblings, &children);
return child;
}
const Directory *
Directory::FindChild(const char *name) const
{
assert(holding_db_lock());
const Directory *child;
2013-01-02 23:06:10 +01:00
directory_for_each_child(child, this)
if (strcmp(child->GetName(), name) == 0)
return child;
return NULL;
}
void
Directory::PruneEmpty()
{
assert(holding_db_lock());
Directory *child, *n;
2013-01-02 23:06:10 +01:00
directory_for_each_child_safe(child, n, this) {
child->PruneEmpty();
2013-01-02 23:06:10 +01:00
if (child->IsEmpty())
child->Delete();
}
}
Directory *
Directory::LookupDirectory(const char *uri)
{
assert(holding_db_lock());
assert(uri != NULL);
if (isRootDirectory(uri))
2013-01-02 23:06:10 +01:00
return this;
char *duplicated = g_strdup(uri), *name = duplicated;
Directory *d = this;
while (1) {
char *slash = strchr(name, '/');
if (slash == name) {
2013-01-02 23:06:10 +01:00
d = NULL;
break;
}
if (slash != NULL)
*slash = '\0';
2013-01-02 23:06:10 +01:00
d = d->FindChild(name);
if (d == NULL || slash == NULL)
break;
name = slash + 1;
}
g_free(duplicated);
2013-01-02 23:06:10 +01:00
return d;
}
void
2013-07-28 13:25:12 +02:00
Directory::AddSong(Song *song)
{
assert(holding_db_lock());
assert(song != NULL);
2013-01-02 23:06:10 +01:00
assert(song->parent == this);
2013-01-02 23:06:10 +01:00
list_add_tail(&song->siblings, &songs);
}
void
2013-07-28 13:25:12 +02:00
Directory::RemoveSong(Song *song)
{
assert(holding_db_lock());
assert(song != NULL);
2013-01-02 23:06:10 +01:00
assert(song->parent == this);
list_del(&song->siblings);
}
2013-07-28 13:25:12 +02:00
const Song *
Directory::FindSong(const char *name_utf8) const
{
assert(holding_db_lock());
assert(name_utf8 != NULL);
2013-07-28 13:25:12 +02:00
Song *song;
2013-01-02 23:06:10 +01:00
directory_for_each_song(song, this) {
assert(song->parent == this);
if (strcmp(song->uri, name_utf8) == 0)
return song;
}
return NULL;
}
2013-07-28 13:25:12 +02:00
Song *
Directory::LookupSong(const char *uri)
{
char *duplicated, *base;
assert(holding_db_lock());
assert(uri != NULL);
duplicated = g_strdup(uri);
base = strrchr(duplicated, '/');
Directory *d = this;
if (base != NULL) {
*base++ = 0;
2013-01-02 23:06:10 +01:00
d = d->LookupDirectory(duplicated);
if (d == nullptr) {
g_free(duplicated);
return NULL;
}
} else
base = duplicated;
2013-07-28 13:25:12 +02:00
Song *song = d->FindSong(base);
2013-01-02 23:06:10 +01:00
assert(song == NULL || song->parent == d);
g_free(duplicated);
return song;
}
static int
2013-08-04 23:48:01 +02:00
directory_cmp(gcc_unused void *priv,
struct list_head *_a, struct list_head *_b)
{
const Directory *a = (const Directory *)_a;
const Directory *b = (const Directory *)_b;
return g_utf8_collate(a->path, b->path);
}
void
Directory::Sort()
{
assert(holding_db_lock());
2013-01-02 23:06:10 +01:00
list_sort(NULL, &children, directory_cmp);
song_list_sort(&songs);
Directory *child;
2013-01-02 23:06:10 +01:00
directory_for_each_child(child, this)
child->Sort();
}
bool
Directory::Walk(bool recursive, const SongFilter *filter,
2012-07-30 07:26:08 +02:00
VisitDirectory visit_directory, VisitSong visit_song,
VisitPlaylist visit_playlist,
GError **error_r) const
{
assert(error_r == NULL || *error_r == NULL);
2012-07-30 07:26:08 +02:00
if (visit_song) {
2013-07-28 13:25:12 +02:00
Song *song;
2012-07-30 07:26:08 +02:00
directory_for_each_song(song, this)
2012-08-29 19:27:03 +02:00
if ((filter == nullptr || filter->Match(*song)) &&
!visit_song(*song, error_r))
return false;
}
2012-07-30 07:26:08 +02:00
if (visit_playlist) {
2013-01-02 22:16:52 +01:00
for (const PlaylistInfo &p : playlists)
if (!visit_playlist(p, *this, error_r))
2011-09-13 22:02:37 +02:00
return false;
}
Directory *child;
2012-07-30 07:26:08 +02:00
directory_for_each_child(child, this) {
if (visit_directory &&
!visit_directory(*child, error_r))
return false;
2011-09-10 18:48:10 +02:00
if (recursive &&
2012-08-29 19:27:03 +02:00
!child->Walk(recursive, filter,
visit_directory, visit_song, visit_playlist,
error_r))
return false;
}
return true;
}