mpd/src/db/plugins/simple/Directory.cxx

268 lines
5.5 KiB
C++
Raw Normal View History

/*
2019-02-20 20:39:49 +01:00
* Copyright 2003-2019 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.
*/
2013-01-02 22:52:08 +01:00
#include "Directory.hxx"
2013-04-17 22:25:57 +02:00
#include "SongSort.hxx"
2013-07-28 13:25:12 +02:00
#include "Song.hxx"
#include "Mount.hxx"
2014-02-26 09:17:41 +01:00
#include "db/LightDirectory.hxx"
#include "song/LightSong.hxx"
2014-02-26 09:17:41 +01:00
#include "db/Uri.hxx"
#include "db/DatabaseLock.hxx"
#include "db/Interface.hxx"
#include "db/Selection.hxx"
#include "song/Filter.hxx"
#include "lib/icu/Collate.hxx"
2013-10-21 10:26:53 +02:00
#include "fs/Traits.hxx"
#include "util/Alloc.hxx"
2015-10-19 15:57:00 +02:00
#include "util/DeleteDisposer.hxx"
2012-07-30 07:26:08 +02:00
#include <assert.h>
#include <string.h>
2009-01-02 16:26:19 +01:00
#include <stdlib.h>
2019-02-20 20:39:49 +01:00
Directory::Directory(std::string &&_path_utf8, Directory *_parent) noexcept
:parent(_parent),
path(std::move(_path_utf8))
{
}
2019-02-20 20:39:49 +01:00
Directory::~Directory() noexcept
{
if (mounted_database != nullptr) {
mounted_database->Close();
mounted_database.reset();
}
songs.clear_and_dispose(Song::Disposer());
2015-10-19 15:57:00 +02:00
children.clear_and_dispose(DeleteDisposer());
}
void
2019-02-20 20:39:49 +01:00
Directory::Delete() noexcept
{
assert(holding_db_lock());
2013-01-02 23:06:10 +01:00
assert(parent != nullptr);
parent->children.erase_and_dispose(parent->children.iterator_to(*this),
2015-10-19 15:57:00 +02:00
DeleteDisposer());
}
const char *
Directory::GetName() const noexcept
{
2013-01-02 23:06:10 +01:00
assert(!IsRoot());
return PathTraitsUTF8::GetBase(path.c_str());
}
Directory *
2019-02-20 20:39:49 +01:00
Directory::CreateChild(const char *name_utf8) noexcept
{
assert(holding_db_lock());
2013-10-19 18:19:03 +02:00
assert(name_utf8 != nullptr);
assert(*name_utf8 != 0);
std::string path_utf8 = IsRoot()
? std::string(name_utf8)
: PathTraitsUTF8::Build(GetPath(), name_utf8);
Directory *child = new Directory(std::move(path_utf8), this);
children.push_back(*child);
2013-01-02 23:06:10 +01:00
return child;
}
const Directory *
Directory::FindChild(const char *name) const noexcept
{
assert(holding_db_lock());
for (const auto &child : children)
if (strcmp(child.GetName(), name) == 0)
return &child;
2013-10-19 18:19:03 +02:00
return nullptr;
}
void
Directory::PruneEmpty() noexcept
{
assert(holding_db_lock());
for (auto child = children.begin(), end = children.end();
child != end;) {
2013-01-02 23:06:10 +01:00
child->PruneEmpty();
if (child->IsEmpty() && !child->IsMount())
2015-10-19 15:57:00 +02:00
child = children.erase_and_dispose(child,
DeleteDisposer());
else
++child;
}
}
Directory::LookupResult
Directory::LookupDirectory(const char *uri) noexcept
{
assert(holding_db_lock());
2013-10-19 18:19:03 +02:00
assert(uri != nullptr);
if (isRootDirectory(uri))
return { this, nullptr };
char *duplicated = xstrdup(uri), *name = duplicated;
Directory *d = this;
while (true) {
char *slash = strchr(name, '/');
if (slash == name)
break;
2013-10-19 18:19:03 +02:00
if (slash != nullptr)
*slash = '\0';
Directory *tmp = d->FindChild(name);
if (tmp == nullptr)
/* not found */
break;
d = tmp;
if (slash == nullptr) {
/* found everything */
name = nullptr;
break;
}
name = slash + 1;
}
free(duplicated);
const char *rest = name == nullptr
? nullptr
: uri + (name - duplicated);
return { d, rest };
}
void
2019-02-20 20:39:49 +01:00
Directory::AddSong(Song *song) noexcept
{
assert(holding_db_lock());
2013-10-19 18:19:03 +02:00
assert(song != nullptr);
2013-01-02 23:06:10 +01:00
assert(song->parent == this);
songs.push_back(*song);
}
void
Directory::RemoveSong(Song *song) noexcept
{
assert(holding_db_lock());
2013-10-19 18:19:03 +02:00
assert(song != nullptr);
2013-01-02 23:06:10 +01:00
assert(song->parent == this);
songs.erase(songs.iterator_to(*song));
}
2013-07-28 13:25:12 +02:00
const Song *
Directory::FindSong(const char *name_utf8) const noexcept
{
assert(holding_db_lock());
2013-10-19 18:19:03 +02:00
assert(name_utf8 != nullptr);
for (auto &song : songs) {
assert(song.parent == this);
if (strcmp(song.uri, name_utf8) == 0)
return &song;
}
2013-10-19 18:19:03 +02:00
return nullptr;
}
gcc_pure
static bool
directory_cmp(const Directory &a, const Directory &b) noexcept
{
return IcuCollate(a.path.c_str(), b.path.c_str()) < 0;
}
void
Directory::Sort() noexcept
{
assert(holding_db_lock());
children.sort(directory_cmp);
song_list_sort(songs);
for (auto &child : children)
child.Sort();
}
void
Directory::Walk(bool recursive, const SongFilter *filter,
2012-07-30 07:26:08 +02:00
VisitDirectory visit_directory, VisitSong visit_song,
VisitPlaylist visit_playlist) const
{
if (IsMount()) {
assert(IsEmpty());
/* TODO: eliminate this unlock/lock; it is necessary
because the child's SimpleDatabasePlugin::Visit()
call will lock it again */
const ScopeDatabaseUnlock unlock;
WalkMount(GetPath(), *mounted_database,
"", DatabaseSelection("", recursive, filter),
visit_directory, visit_song,
visit_playlist);
return;
}
2012-07-30 07:26:08 +02:00
if (visit_song) {
for (auto &song : songs){
const LightSong song2 = song.Export();
if (filter == nullptr || filter->Match(song2))
visit_song(song2);
}
}
2012-07-30 07:26:08 +02:00
if (visit_playlist) {
2013-01-02 22:16:52 +01:00
for (const PlaylistInfo &p : playlists)
visit_playlist(p, Export());
2011-09-13 22:02:37 +02:00
}
for (auto &child : children) {
if (visit_directory)
visit_directory(child.Export());
if (recursive)
child.Walk(recursive, filter,
visit_directory, visit_song,
visit_playlist);
}
}
LightDirectory
Directory::Export() const noexcept
{
return LightDirectory(GetPath(), mtime);
}