Mount.cxx 2.98 KB
Newer Older
Max Kellermann's avatar
Max Kellermann committed
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
Max Kellermann's avatar
Max Kellermann committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "Mount.hxx"
#include "PrefixedLightSong.hxx"
22
#include "song/Filter.hxx"
Max Kellermann's avatar
Max Kellermann committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include "db/Selection.hxx"
#include "db/LightDirectory.hxx"
#include "db/Interface.hxx"
#include "fs/Traits.hxx"

#include <string>

struct PrefixedLightDirectory : LightDirectory {
	std::string buffer;

	PrefixedLightDirectory(const LightDirectory &directory,
			       const char *base)
		:LightDirectory(directory),
		 buffer(IsRoot()
			? std::string(base)
			: PathTraitsUTF8::Build(base, uri)) {
		uri = buffer.c_str();
	}
};

43
static void
Max Kellermann's avatar
Max Kellermann committed
44
PrefixVisitDirectory(const char *base, const VisitDirectory &visit_directory,
45
		     const LightDirectory &directory)
Max Kellermann's avatar
Max Kellermann committed
46
{
47
	visit_directory(PrefixedLightDirectory(directory, base));
Max Kellermann's avatar
Max Kellermann committed
48 49
}

50
static void
Max Kellermann's avatar
Max Kellermann committed
51
PrefixVisitSong(const char *base, const VisitSong &visit_song,
52
		const LightSong &song)
Max Kellermann's avatar
Max Kellermann committed
53
{
54
	visit_song(PrefixedLightSong(song, base));
Max Kellermann's avatar
Max Kellermann committed
55 56
}

57
static void
Max Kellermann's avatar
Max Kellermann committed
58 59
PrefixVisitPlaylist(const char *base, const VisitPlaylist &visit_playlist,
		    const PlaylistInfo &playlist,
60
		    const LightDirectory &directory)
Max Kellermann's avatar
Max Kellermann committed
61
{
62 63
	visit_playlist(playlist,
		       PrefixedLightDirectory(directory, base));
Max Kellermann's avatar
Max Kellermann committed
64 65
}

66
void
Max Kellermann's avatar
Max Kellermann committed
67
WalkMount(const char *base, const Database &db,
68 69
	  const char *uri,
	  const DatabaseSelection &old_selection,
Max Kellermann's avatar
Max Kellermann committed
70
	  const VisitDirectory &visit_directory, const VisitSong &visit_song,
71
	  const VisitPlaylist &visit_playlist)
Max Kellermann's avatar
Max Kellermann committed
72 73 74 75 76 77
{
	using namespace std::placeholders;

	VisitDirectory vd;
	if (visit_directory)
		vd = std::bind(PrefixVisitDirectory,
78
			       base, std::ref(visit_directory), _1);
Max Kellermann's avatar
Max Kellermann committed
79 80 81 82

	VisitSong vs;
	if (visit_song)
		vs = std::bind(PrefixVisitSong,
83
			       base, std::ref(visit_song), _1);
Max Kellermann's avatar
Max Kellermann committed
84 85 86 87

	VisitPlaylist vp;
	if (visit_playlist)
		vp = std::bind(PrefixVisitPlaylist,
88
			       base, std::ref(visit_playlist), _1, _2);
Max Kellermann's avatar
Max Kellermann committed
89

90 91 92
	DatabaseSelection selection(old_selection);
	selection.uri = uri;

93 94
	SongFilter prefix_filter;

95
	if (base != nullptr && selection.filter != nullptr) {
96 97 98 99
		/* if the SongFilter contains a LOCATE_TAG_BASE_TYPE
		   item, copy the SongFilter and drop the mount point
		   from the filter, because the mounted database
		   doesn't know its own location within MPD's VFS */
100 101
		prefix_filter = selection.filter->WithoutBasePrefix(base);
		selection.filter = &prefix_filter;
102 103
	}

104
	db.Visit(selection, vd, vs, vp);
Max Kellermann's avatar
Max Kellermann committed
105
}