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