Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 1 | #include "utils/generate_id.hpp" |
| 2 | |
| 3 | #include <sdbusplus/exception.hpp> |
| 4 | |
Ed Tanous | 36de056 | 2022-05-27 13:05:49 -0700 | [diff] [blame] | 5 | #include <algorithm> |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 6 | #include <system_error> |
| 7 | |
| 8 | namespace utils |
| 9 | { |
| 10 | namespace details |
| 11 | { |
| 12 | |
| 13 | static constexpr std::string_view allowedCharactersInId = |
| 14 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/"; |
| 15 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 16 | size_t countDigits(size_t value) |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 17 | { |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 18 | size_t result = 1; |
| 19 | while (value >= 10) |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 20 | { |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 21 | ++result; |
| 22 | value /= 10; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 23 | } |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 24 | return result; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 25 | } |
| 26 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 27 | std::string generateId(std::string_view id, std::string_view name, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 28 | const std::vector<std::string>& conflictIds, |
| 29 | size_t maxLength) |
| 30 | { |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 31 | verifyIdCharacters(id); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 32 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 33 | if (id.starts_with('/')) |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 34 | { |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 35 | id = id.substr(1); |
| 36 | } |
| 37 | |
| 38 | if ((id.length() > maxLength) || |
| 39 | (id.ends_with('/') && id.length() >= maxLength)) |
| 40 | { |
| 41 | throw sdbusplus::exception::SdBusError( |
| 42 | static_cast<int>(std::errc::invalid_argument), "Id too long"); |
| 43 | } |
| 44 | |
| 45 | if (!id.empty() && !id.ends_with('/')) |
| 46 | { |
| 47 | if (std::find(conflictIds.begin(), conflictIds.end(), id) != |
| 48 | conflictIds.end()) |
| 49 | { |
| 50 | throw sdbusplus::exception::SdBusError( |
| 51 | static_cast<int>(std::errc::file_exists), "Duplicated id"); |
| 52 | } |
| 53 | return std::string(id); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | std::string strippedId(name); |
| 57 | strippedId.erase( |
| 58 | std::remove_if(strippedId.begin(), strippedId.end(), |
| 59 | [](char c) { |
| 60 | return c == '/' || |
| 61 | details::allowedCharactersInId.find(c) == |
| 62 | std::string_view::npos; |
| 63 | }), |
| 64 | strippedId.end()); |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 65 | strippedId = std::string(id) + strippedId; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 66 | |
| 67 | size_t idx = 0; |
| 68 | std::string tmpId = strippedId.substr(0, maxLength); |
| 69 | |
| 70 | while (std::find(conflictIds.begin(), conflictIds.end(), tmpId) != |
| 71 | conflictIds.end() || |
| 72 | tmpId.empty()) |
| 73 | { |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 74 | size_t digitsInIdx = countDigits(idx); |
| 75 | |
| 76 | if (digitsInIdx > maxLength) |
| 77 | { |
| 78 | throw sdbusplus::exception::SdBusError( |
| 79 | static_cast<int>(std::errc::file_exists), |
| 80 | "Unique indices are depleted"); |
| 81 | } |
| 82 | |
| 83 | tmpId = |
| 84 | strippedId.substr(0, maxLength - digitsInIdx) + std::to_string(idx); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 85 | ++idx; |
| 86 | } |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 87 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 88 | return tmpId; |
| 89 | } |
| 90 | |
Krzysztof Grobelny | a950e42 | 2021-12-31 13:49:00 +0100 | [diff] [blame] | 91 | } // namespace details |
| 92 | |
| 93 | void verifyIdCharacters(std::string_view id) |
| 94 | { |
| 95 | if (id.find_first_not_of(details::allowedCharactersInId) != |
| 96 | std::string::npos) |
| 97 | { |
| 98 | throw sdbusplus::exception::SdBusError( |
| 99 | static_cast<int>(std::errc::invalid_argument), |
| 100 | "Invalid character in id"); |
| 101 | } |
| 102 | |
| 103 | if (auto pos = id.find_first_of("/"); |
| 104 | pos != std::string::npos && pos != id.find_last_of("/")) |
| 105 | { |
| 106 | throw sdbusplus::exception::SdBusError( |
| 107 | static_cast<int>(std::errc::invalid_argument), |
| 108 | "Too many '/' in id"); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | std::pair<std::string, std::string> generateId( |
| 113 | std::string_view id, std::string_view name, std::string_view defaultName, |
| 114 | const std::vector<std::string>& conflictIds, const size_t maxLength) |
| 115 | { |
| 116 | if (name.empty() && !id.ends_with('/')) |
| 117 | { |
| 118 | name = id; |
| 119 | |
| 120 | if (auto pos = name.find_last_of("/"); pos != std::string::npos) |
| 121 | { |
| 122 | name = name.substr(pos + 1); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (name.empty()) |
| 127 | { |
| 128 | name = defaultName; |
| 129 | } |
| 130 | |
| 131 | return std::make_pair(details::generateId(id, name, conflictIds, maxLength), |
| 132 | std::string{name}); |
| 133 | } |
| 134 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 135 | } // namespace utils |