UDisks2.cxx 4.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright 2003-2018 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 "UDisks2.hxx"
21
#include "Message.hxx"
22
#include "ReadIter.hxx"
23
#include "ObjectManager.hxx"
24
#include "util/StringAPI.hxx"
25
#include "util/StringView.hxx"
26
#include "util/Compiler.h"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#include <functional>

namespace UDisks2 {

template<typename I>
gcc_pure
static const char *
CheckString(I &&i) noexcept
{
	if (i.GetArgType() != DBUS_TYPE_STRING)
		return nullptr;

	return i.GetString();
}

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
template<typename I>
gcc_pure
static StringView
CheckRecursedByteArrayToString(I &&i) noexcept
{
	if (i.GetArgType() != DBUS_TYPE_BYTE)
		return nullptr;

	auto value = i.template GetFixedArray<char>();
	return { value.data, value.size };
}

template<typename I>
gcc_pure
static StringView
CheckByteArrayToString(I &&i) noexcept
{
	if (i.GetArgType() != DBUS_TYPE_ARRAY)
		return nullptr;

	return CheckRecursedByteArrayToString(i.Recurse());
}

template<typename I>
gcc_pure
static StringView
CheckByteArrayArrayFrontToString(I &&i) noexcept
{
	if (i.GetArgType() != DBUS_TYPE_ARRAY)
		return nullptr;

	return CheckByteArrayToString(i.Recurse());
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
static void
ParseDriveDictEntry(Object &o, const char *name,
		    ODBus::ReadMessageIter &&value_i) noexcept
{
	if (StringIsEqual(name, "Id")) {
		const char *value = CheckString(value_i);
		if (value != nullptr && o.drive_id.empty())
			o.drive_id = value;
	}
}

static void
ParseBlockDictEntry(Object &o, const char *name,
		    ODBus::ReadMessageIter &&value_i) noexcept
{
	if (StringIsEqual(name, "Id")) {
		const char *value = CheckString(value_i);
		if (value != nullptr && o.block_id.empty())
			o.block_id = value;
	}
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static void
ParseFileesystemDictEntry(Object &o, const char *name,
			  ODBus::ReadMessageIter &&value_i) noexcept
{
	if (StringIsEqual(name, "MountPoints")) {
		if (!o.mount_point.empty())
			/* we already know one mount point, and we're
			   not interested in more */
			return;

		/* get the first string in the array */
		auto value = CheckByteArrayArrayFrontToString(value_i);
		if (value != nullptr)
			o.mount_point = {value.data, value.size};

		// TODO: check whether the string is a valid filesystem path
	}
}

118 119 120 121 122 123 124 125 126 127 128 129 130
static void
ParseInterface(Object &o, const char *interface,
	       ODBus::ReadMessageIter &&i) noexcept
{
	using namespace std::placeholders;
	if (StringIsEqual(interface, "org.freedesktop.UDisks2.Drive")) {
		i.ForEachProperty(std::bind(ParseDriveDictEntry,
					    std::ref(o), _1, _2));
	} else if (StringIsEqual(interface, "org.freedesktop.UDisks2.Block")) {
		i.ForEachProperty(std::bind(ParseBlockDictEntry,
					    std::ref(o), _1, _2));
	} else if (StringIsEqual(interface, "org.freedesktop.UDisks2.Filesystem")) {
		o.is_filesystem = true;
131 132 133 134

		i.ForEachProperty(std::bind(ParseFileesystemDictEntry,
					    std::ref(o), _1, _2));

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	}
}

static void
ParseInterfaceDictEntry(Object &o, ODBus::ReadMessageIter &&i) noexcept
{
	if (i.GetArgType() != DBUS_TYPE_STRING)
		return;

	const char *interface = i.GetString();
	i.Next();

	if (i.GetArgType() != DBUS_TYPE_ARRAY)
		return;

	ParseInterface(o, interface, i.Recurse());
}

void
ParseObject(Object &o, ODBus::ReadMessageIter &&i) noexcept
{
	i.ForEach(DBUS_TYPE_DICT_ENTRY, [&o](auto &&j){
			ParseInterfaceDictEntry(o, j.Recurse());
		});
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174
void
ParseObjects(ODBus::ReadMessageIter &&i,
	     std::function<void(Object &&o)> callback)
{
	using namespace ODBus;

	ForEachInterface(std::move(i), [&callback](const char *path, auto &&j){
			Object o(path);
			ParseObject(o, std::move(j));
			if (o.IsValid())
				callback(std::move(o));
		});
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
void
ParseObjects(ODBus::Message &reply,
	     std::function<void(Object &&o)> callback)
{
	using namespace ODBus;

	reply.CheckThrowError();

	ReadMessageIter i(*reply.Get());
	if (i.GetArgType() != DBUS_TYPE_ARRAY)
		throw std::runtime_error("Malformed response");

	ParseObjects(i.Recurse(), std::move(callback));
}

190
} // namespace UDisks2