Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <iostream> |
| 17 | #include <exception> |
Brad Bishop | 2442498 | 2017-01-13 16:37:14 -0500 | [diff] [blame] | 18 | #include <chrono> |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 19 | #include <algorithm> |
Saqib Khan | 101d837 | 2017-02-20 15:36:11 -0600 | [diff] [blame] | 20 | #include <phosphor-logging/log.hpp> |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 21 | #include <experimental/filesystem> |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 22 | #include "manager.hpp" |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 23 | #include "errors.hpp" |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 24 | |
Brad Bishop | 2442498 | 2017-01-13 16:37:14 -0500 | [diff] [blame] | 25 | using namespace std::literals::chrono_literals; |
| 26 | |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 27 | namespace phosphor |
| 28 | { |
| 29 | namespace inventory |
| 30 | { |
| 31 | namespace manager |
| 32 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 33 | /** @brief Fowrarding signal callback. |
| 34 | * |
| 35 | * Extracts per-signal specific context and forwards the call to the manager |
| 36 | * instance. |
| 37 | */ |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 38 | auto _signal(sd_bus_message* m, void* data, sd_bus_error* e) noexcept |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 39 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 40 | try |
| 41 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 42 | auto msg = sdbusplus::message::message(m); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 43 | auto& args = *static_cast<Manager::SigArg*>(data); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 44 | sd_bus_message_ref(m); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 45 | auto& mgr = *std::get<0>(args); |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 46 | mgr.handleEvent(msg, static_cast<const DbusSignal&>(*std::get<1>(args)), |
| 47 | *std::get<2>(args)); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 48 | } |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 49 | catch (const std::exception& e) |
| 50 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 51 | std::cerr << e.what() << std::endl; |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 57 | Manager::Manager(sdbusplus::bus::bus&& bus, const char* busname, |
| 58 | const char* root, const char* iface) : |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 59 | ServerObject<ManagerIface>(bus, root), |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 60 | _shutdown(false), _root(root), _bus(std::move(bus)), _manager(_bus, root) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 61 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 62 | for (auto& group : _events) |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 63 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 64 | for (auto pEvent : std::get<std::vector<EventBasePtr>>(group)) |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 65 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 66 | if (pEvent->type != Event::Type::DBUS_SIGNAL) |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 67 | { |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 68 | continue; |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 69 | } |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 70 | |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 71 | // Create a callback context for this event group. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 72 | auto dbusEvent = static_cast<DbusSignal*>(pEvent.get()); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 73 | |
| 74 | // Go ahead and store an iterator pointing at |
| 75 | // the event data to avoid lookups later since |
| 76 | // additional signal callbacks aren't added |
| 77 | // after the manager is constructed. |
| 78 | _sigargs.emplace_back( |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 79 | std::make_unique<SigArg>(this, dbusEvent, &group)); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 80 | |
| 81 | // Register our callback and the context for |
| 82 | // each signal event. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 83 | _matches.emplace_back(_bus, dbusEvent->signature, _signal, |
| 84 | _sigargs.back().get()); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 85 | } |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | _bus.request_name(busname); |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 89 | |
| 90 | // Restore any persistent inventory |
| 91 | restore(); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void Manager::shutdown() noexcept |
| 95 | { |
| 96 | _shutdown = true; |
| 97 | } |
| 98 | |
| 99 | void Manager::run() noexcept |
| 100 | { |
Brad Bishop | 3e4a19a | 2017-01-21 22:17:09 -0500 | [diff] [blame] | 101 | sdbusplus::message::message unusedMsg{nullptr}; |
| 102 | |
| 103 | // Run startup events. |
| 104 | for (auto& group : _events) |
| 105 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 106 | for (auto pEvent : std::get<std::vector<EventBasePtr>>(group)) |
Brad Bishop | 3e4a19a | 2017-01-21 22:17:09 -0500 | [diff] [blame] | 107 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 108 | if (pEvent->type == Event::Type::STARTUP) |
Brad Bishop | 3e4a19a | 2017-01-21 22:17:09 -0500 | [diff] [blame] | 109 | { |
| 110 | handleEvent(unusedMsg, *pEvent, group); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 115 | while (!_shutdown) |
| 116 | { |
| 117 | try |
| 118 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 119 | _bus.process_discard(); |
Brad Bishop | 2442498 | 2017-01-13 16:37:14 -0500 | [diff] [blame] | 120 | _bus.wait((5000000us).count()); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 121 | } |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 122 | catch (const std::exception& e) |
| 123 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 124 | std::cerr << e.what() << std::endl; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 129 | void Manager::updateInterfaces(const sdbusplus::message::object_path& path, |
| 130 | const Object& interfaces, |
| 131 | ObjectReferences::iterator pos, bool newObject, |
| 132 | bool restoreFromCache) |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 133 | { |
| 134 | auto& refaces = pos->second; |
| 135 | auto ifaceit = interfaces.cbegin(); |
| 136 | auto opsit = _makers.cbegin(); |
| 137 | auto refaceit = refaces.begin(); |
| 138 | std::vector<std::string> signals; |
| 139 | |
| 140 | while (ifaceit != interfaces.cend()) |
| 141 | { |
| 142 | try |
| 143 | { |
| 144 | // Find the binding ops for this interface. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 145 | opsit = std::lower_bound(opsit, _makers.cend(), ifaceit->first, |
| 146 | compareFirst(_makers.key_comp())); |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 147 | |
| 148 | if (opsit == _makers.cend() || opsit->first != ifaceit->first) |
| 149 | { |
| 150 | // This interface is not supported. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 151 | throw InterfaceError("Encountered unsupported interface.", |
| 152 | ifaceit->first); |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Find the binding insertion point or the binding to update. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 156 | refaceit = std::lower_bound(refaceit, refaces.end(), ifaceit->first, |
| 157 | compareFirst(refaces.key_comp())); |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 158 | |
| 159 | if (refaceit == refaces.end() || refaceit->first != ifaceit->first) |
| 160 | { |
| 161 | // Add the new interface. |
| 162 | auto& ctor = std::get<MakerType>(opsit->second); |
| 163 | refaceit = refaces.insert( |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 164 | refaceit, |
| 165 | std::make_pair(ifaceit->first, ctor(_bus, path.str.c_str(), |
| 166 | ifaceit->second))); |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 167 | signals.push_back(ifaceit->first); |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | // Set the new property values. |
| 172 | auto& assign = std::get<AssignerType>(opsit->second); |
| 173 | assign(ifaceit->second, refaceit->second); |
| 174 | } |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 175 | if (!restoreFromCache) |
| 176 | { |
| 177 | auto& serialize = std::get<SerializerType>(opsit->second); |
| 178 | serialize(path, ifaceit->first, refaceit->second); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | auto& deserialize = std::get<DeserializerType>(opsit->second); |
| 183 | deserialize(path, ifaceit->first, refaceit->second); |
| 184 | } |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 185 | } |
| 186 | catch (const InterfaceError& e) |
| 187 | { |
| 188 | // Reset the binding ops iterator since we are |
| 189 | // at the end. |
| 190 | opsit = _makers.cbegin(); |
| 191 | e.log(); |
| 192 | } |
| 193 | |
| 194 | ++ifaceit; |
| 195 | } |
| 196 | |
| 197 | if (newObject) |
| 198 | { |
| 199 | _bus.emit_object_added(path.str.c_str()); |
| 200 | } |
| 201 | else if (!signals.empty()) |
| 202 | { |
Gunnar Mills | a8ff815 | 2017-06-06 12:54:28 -0500 | [diff] [blame] | 203 | _bus.emit_interfaces_added(path.str.c_str(), signals); |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | void Manager::updateObjects( |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 208 | const std::map<sdbusplus::message::object_path, Object>& objs, |
| 209 | bool restoreFromCache) |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 210 | { |
| 211 | auto objit = objs.cbegin(); |
| 212 | auto refit = _refs.begin(); |
| 213 | std::string absPath; |
| 214 | bool newObj; |
| 215 | |
| 216 | while (objit != objs.cend()) |
| 217 | { |
| 218 | // Find the insertion point or the object to update. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 219 | refit = std::lower_bound(refit, _refs.end(), objit->first, |
| 220 | compareFirst(RelPathCompare(_root))); |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 221 | |
| 222 | absPath.assign(_root); |
| 223 | absPath.append(objit->first); |
| 224 | |
| 225 | newObj = false; |
| 226 | if (refit == _refs.end() || refit->first != absPath) |
| 227 | { |
| 228 | refit = _refs.insert( |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 229 | refit, std::make_pair(absPath, decltype(_refs)::mapped_type())); |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 230 | newObj = true; |
| 231 | } |
| 232 | |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 233 | updateInterfaces(absPath, objit->second, refit, newObj, |
| 234 | restoreFromCache); |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 235 | ++objit; |
| 236 | } |
| 237 | } |
| 238 | |
Brad Bishop | 03f4cd9 | 2017-02-03 15:17:21 -0500 | [diff] [blame] | 239 | void Manager::notify(std::map<sdbusplus::message::object_path, Object> objs) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 240 | { |
Brad Bishop | cda036f | 2017-02-15 10:06:48 -0500 | [diff] [blame] | 241 | updateObjects(objs); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 244 | void Manager::handleEvent(sdbusplus::message::message& msg, const Event& event, |
| 245 | const EventInfo& info) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 246 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 247 | auto& actions = std::get<1>(info); |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 248 | |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 249 | for (auto& f : event) |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 250 | { |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 251 | if (!f(_bus, msg, *this)) |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 252 | { |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 253 | return; |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 254 | } |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 255 | } |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 256 | for (auto& action : actions) |
| 257 | { |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 258 | action(_bus, *this); |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 259 | } |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 260 | } |
| 261 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 262 | void Manager::destroyObjects(const std::vector<const char*>& paths) |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 263 | { |
Brad Bishop | a5cc34c | 2017-02-03 20:57:36 -0500 | [diff] [blame] | 264 | std::string p; |
| 265 | |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 266 | for (const auto& path : paths) |
| 267 | { |
Brad Bishop | a5cc34c | 2017-02-03 20:57:36 -0500 | [diff] [blame] | 268 | p.assign(_root); |
| 269 | p.append(path); |
| 270 | _bus.emit_object_removed(p.c_str()); |
| 271 | _refs.erase(p); |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 272 | } |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Brad Bishop | eb68a68 | 2017-01-22 00:58:54 -0500 | [diff] [blame] | 275 | void Manager::createObjects( |
| 276 | const std::map<sdbusplus::message::object_path, Object>& objs) |
| 277 | { |
Brad Bishop | cda036f | 2017-02-15 10:06:48 -0500 | [diff] [blame] | 278 | updateObjects(objs); |
Brad Bishop | eb68a68 | 2017-01-22 00:58:54 -0500 | [diff] [blame] | 279 | } |
| 280 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 281 | any_ns::any& Manager::getInterfaceHolder(const char* path, |
| 282 | const char* interface) |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 283 | { |
Brad Bishop | 150147a | 2017-02-08 23:57:46 -0500 | [diff] [blame] | 284 | return const_cast<any_ns::any&>( |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 285 | const_cast<const Manager*>(this)->getInterfaceHolder(path, interface)); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 286 | } |
| 287 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 288 | const any_ns::any& Manager::getInterfaceHolder(const char* path, |
| 289 | const char* interface) const |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 290 | { |
| 291 | std::string p{path}; |
| 292 | auto oit = _refs.find(_root + p); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 293 | if (oit == _refs.end()) |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 294 | throw std::runtime_error(_root + p + " was not found"); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 295 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 296 | auto& obj = oit->second; |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 297 | auto iit = obj.find(interface); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 298 | if (iit == obj.end()) |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 299 | throw std::runtime_error("interface was not found"); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 300 | |
Brad Bishop | 150147a | 2017-02-08 23:57:46 -0500 | [diff] [blame] | 301 | return iit->second; |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 302 | } |
| 303 | |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 304 | void Manager::restore() |
| 305 | { |
| 306 | namespace fs = std::experimental::filesystem; |
| 307 | |
| 308 | if (!fs::exists(fs::path(PIM_PERSIST_PATH))) |
| 309 | { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | static const std::string remove = |
| 314 | std::string(PIM_PERSIST_PATH) + INVENTORY_ROOT; |
| 315 | |
| 316 | std::map<sdbusplus::message::object_path, Object> objects; |
| 317 | for (const auto& dirent : |
| 318 | fs::recursive_directory_iterator(PIM_PERSIST_PATH)) |
| 319 | { |
| 320 | const auto& path = dirent.path(); |
| 321 | if (fs::is_regular_file(path)) |
| 322 | { |
| 323 | auto ifaceName = path.filename().string(); |
| 324 | auto objPath = path.parent_path().string(); |
| 325 | objPath.erase(0, remove.length()); |
| 326 | auto objit = objects.find(objPath); |
| 327 | Interface propertyMap{}; |
| 328 | if (objects.end() != objit) |
| 329 | { |
| 330 | auto& object = objit->second; |
| 331 | object.emplace(std::move(ifaceName), std::move(propertyMap)); |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | Object object; |
| 336 | object.emplace(std::move(ifaceName), std::move(propertyMap)); |
| 337 | objects.emplace(std::move(objPath), std::move(object)); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | if (!objects.empty()) |
| 342 | { |
| 343 | auto restoreFromCache = true; |
| 344 | updateObjects(objects, restoreFromCache); |
| 345 | } |
| 346 | } |
| 347 | |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 348 | } // namespace manager |
| 349 | } // namespace inventory |
| 350 | } // namespace phosphor |
| 351 | |
| 352 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |