blob: 9d37cfcd818555ea3bf238dd18ce7be8a38d0e31 [file] [log] [blame]
Szymon Dompke1cdd7e42022-06-08 14:43:13 +02001#include "utils/dbus_path_utils.hpp"
2
3namespace utils
4{
5sdbusplus::message::object_path pathAppend(sdbusplus::message::object_path path,
6 const std::string& appended)
7{
8 if (appended.starts_with('/') || !isValidDbusPath(appended))
9 {
10 throw sdbusplus::exception::SdBusError(
11 static_cast<int>(std::errc::invalid_argument),
12 "Invalid appended string");
13 }
14
15 size_t pos_start = 0;
16 size_t pos_end = 0;
17 while ((pos_end = appended.find('/', pos_start)) != std::string::npos)
18 {
19 if (pos_start == pos_end)
20 {
21 throw sdbusplus::exception::SdBusError(
22 static_cast<int>(std::errc::invalid_argument),
23 "Invalid appended string");
24 }
25 path /= std::string_view(appended.begin() + pos_start,
26 appended.begin() + pos_end);
27 pos_start = pos_end + 1;
28 }
29 path /= std::string_view(appended.begin() + pos_start, appended.end());
30 return path;
31}
32
33std::string reportPathToId(const sdbusplus::message::object_path& path)
34{
35 if (path.str.starts_with(constants::reportDirStr))
36 {
37 auto id = path.str.substr(constants::reportDirStr.length());
38 if (std::cmp_greater(std::count(id.begin(), id.end(), '/'),
39 constants::maxPrefixesInId))
40 {
41 throw sdbusplus::exception::SdBusError(
42 static_cast<int>(std::errc::invalid_argument),
43 "Too many prefixes in id");
44 }
45 return id;
46 }
47 throw sdbusplus::exception::SdBusError(
48 static_cast<int>(std::errc::invalid_argument), "Invalid path prefix");
49}
50} // namespace utils