Simplify mux symlink logic
Use std::filesystem::path all the way through instead of converting to
string and path back and forth.
loop over incoming json, instead of channels, which allows json array to
be of smaller size than channels without error
Tested By:
Ran code, observed all muxes get created correctly.
No user impact
Change-Id: I07c0e5a92296d7ac951c40698de94ddbde3f91e7
Signed-off-by: Ed Tanous <ed.tanous@intel.com>
diff --git a/src/Overlay.cpp b/src/Overlay.cpp
index 8c246d4..5a6ee04 100644
--- a/src/Overlay.cpp
+++ b/src/Overlay.cpp
@@ -106,73 +106,58 @@
return in.dump();
}
-void linkMux(const std::string& muxName, size_t bus, size_t address,
+void linkMux(const std::string& muxName, size_t busIndex, size_t address,
const nlohmann::json::array_t& channelNames)
{
- // create directory first time
- static bool createDir = false;
std::error_code ec;
- if (!createDir)
- {
- std::filesystem::create_directory(MUX_SYMLINK_DIR, ec);
- createDir = true;
- }
- std::ostringstream hex;
- hex << std::hex << std::setfill('0') << std::setw(4) << address;
- const std::string& addressHex = hex.str();
+ std::filesystem::path muxSymlinkDir(MUX_SYMLINK_DIR);
+ std::filesystem::create_directory(muxSymlinkDir, ec);
+ // ignore error codes here if the directory already exists
+ ec.clear();
+ std::filesystem::path linkDir = muxSymlinkDir / muxName;
+ std::filesystem::create_directory(linkDir, ec);
+
+ std::ostringstream hexAddress;
+ hexAddress << std::hex << std::setfill('0') << std::setw(4) << address;
std::filesystem::path devDir(I2C_DEVS_DIR);
- devDir /= std::to_string(bus) + "-" + addressHex;
+ devDir /= std::to_string(busIndex) + "-" + hexAddress.str();
- size_t channel = 0;
std::string channelName;
- std::filesystem::path channelPath = devDir / "channel-0";
- while (is_symlink(channelPath))
- {
- if (channel > channelNames.size())
- {
- channelName = std::to_string(channel);
- }
- else
- {
- const std::string* ptr =
- channelNames[channel].get_ptr<const std::string*>();
- if (ptr == nullptr)
- {
- channelName = channelNames[channel].dump();
- }
- else
- {
- channelName = *ptr;
- }
- }
- // if configuration has name empty, don't put it in dev
- if (!channelName.empty())
- {
- std::filesystem::path bus =
- std::filesystem::read_symlink(channelPath);
- const std::string& busName = bus.filename();
- std::string linkDir = MUX_SYMLINK_DIR + std::string("/") + muxName;
- if (channel == 0)
- {
- std::filesystem::create_directory(linkDir, ec);
- }
- std::filesystem::create_symlink(
- "/dev/" + busName, linkDir + std::string("/") + channelName,
- ec);
-
- if (ec)
- {
- std::cerr << "Failure creating symlink for " << busName << "\n";
- }
- }
- channel++;
- channelPath = devDir / ("channel-" + std::to_string(channel));
- }
- if (channel == 0)
+ for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
+ channelIndex++)
{
- std::cerr << "Mux missing channels " << devDir << "\n";
+ const std::string* channelName =
+ channelNames[channelIndex].get_ptr<const std::string*>();
+ if (channelName == nullptr)
+ {
+ continue;
+ }
+ if (channelName->empty())
+ {
+ continue;
+ }
+
+ std::filesystem::path channelPath =
+ devDir / ("channel-" + std::to_string(channelIndex));
+ if (!is_symlink(channelPath))
+ {
+ std::cerr << channelPath << "for mux channel " << *channelName
+ << " doesn't exist!\n";
+ continue;
+ }
+ std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
+
+ std::filesystem::path fp("/dev" / bus.filename());
+ std::filesystem::path link(linkDir / *channelName);
+
+ std::filesystem::create_symlink(fp, link, ec);
+ if (ec)
+ {
+ std::cerr << "Failure creating symlink for " << fp << " to " << link
+ << "\n";
+ }
}
}