Mount.cxx 3.08 KB
Newer Older
Max Kellermann's avatar
Max Kellermann committed
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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
#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,
34
			       std::string_view base)
Max Kellermann's avatar
Max Kellermann committed
35 36 37 38 39 40 41 42
		:LightDirectory(directory),
		 buffer(IsRoot()
			? std::string(base)
			: PathTraitsUTF8::Build(base, uri)) {
		uri = buffer.c_str();
	}
};

43
static void
44
PrefixVisitDirectory(std::string_view 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
51
PrefixVisitSong(std::string_view 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
58
PrefixVisitPlaylist(std::string_view base, const VisitPlaylist &visit_playlist,
Max Kellermann's avatar
Max Kellermann committed
59
		    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
67 68
WalkMount(std::string_view base, const Database &db,
	  std::string_view uri,
69
	  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
{
	VisitDirectory vd;
	if (visit_directory)
75 76
		vd = [base,&visit_directory](const auto &dir)
			{ return PrefixVisitDirectory(base, visit_directory, dir); };
Max Kellermann's avatar
Max Kellermann committed
77 78 79

	VisitSong vs;
	if (visit_song)
80 81
		vs = [base,&visit_song](const auto &song)
			{ return PrefixVisitSong(base, visit_song, song); };
Max Kellermann's avatar
Max Kellermann committed
82 83 84

	VisitPlaylist vp;
	if (visit_playlist)
85 86
		vp = [base,&visit_playlist](const auto &playlist, const auto &dir)
			{ return PrefixVisitPlaylist(base, visit_playlist, playlist, dir); };
Max Kellermann's avatar
Max Kellermann committed
87

88 89 90
	DatabaseSelection selection(old_selection);
	selection.uri = uri;

91 92
	SongFilter prefix_filter;

93
	if (base.data() != nullptr && selection.filter != nullptr) {
94 95 96 97
		/* 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 */
98 99
		prefix_filter = selection.filter->WithoutBasePrefix(base);
		selection.filter = &prefix_filter;
100 101
	}

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