Skip to content

Commit

Permalink
fix: check some file exists or not during generating config
Browse files Browse the repository at this point in the history
Signed-off-by: ComixHe <heyuming@deepin.org>
  • Loading branch information
ComixHe committed May 31, 2024
1 parent 4329444 commit 41501cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
34 changes: 12 additions & 22 deletions apps/generators/20-devices/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,22 @@ int main()
return -1;
}

auto commonMounts = u8R"( [
{
"destination": "/run/udev",
"type": "bind",
"source": "/run/udev",
"options": [
"rbind"
]
},{
"destination": "/dev/snd",
"type": "bind",
"source": "/dev/snd",
"options": [
"rbind"
]
auto &mounts = content["mounts"];
auto bindIfExist = [&mounts](std::string_view source, std::string_view destination) mutable {
if (!std::filesystem::exists(source)) {
return;
}
])"_json;

auto &mounts = content["mounts"];
mounts.insert(mounts.end(), commonMounts.begin(), commonMounts.end());
if (std::filesystem::exists("/dev/dri")) {
mounts.push_back({ { "destination", "/dev/dri" },
auto realDest = destination.empty() ? source : destination;
mounts.push_back({ { "source", source },
{ "type", "bind" },
{ "source", "/dev/dri" },
{ "destination", realDest },
{ "options", nlohmann::json::array({ "rbind" }) } });
}
};

bindIfExist("/run/udev", "");
bindIfExist("/dev/snd", "");
bindIfExist("/dev/dri", "");

nlohmann::json videoMounts = nlohmann::json::array();
for (const auto &entry : std::filesystem::directory_iterator{ "/dev" }) {
Expand Down
35 changes: 17 additions & 18 deletions apps/generators/40-host-ipc/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ int main()
}

auto &mounts = content["mounts"];
auto bindIfExist = [&mounts](std::string_view source, std::string_view destination) mutable {
if (!std::filesystem::exists(source)) {
return;
}

mounts.push_back(u8R"( {
"destination": "/tmp/.X11-unix",
"type": "bind",
"source": "/tmp/.X11-unix",
"options": [
"rbind"
]
} )"_json);
auto realDest = destination.empty() ? source : destination;
mounts.push_back({ { "source", source },
{ "type", "bind" },
{ "destination", realDest },
{ "options", nlohmann::json::array({ "rbind" }) } });
};

bindIfExist("/tmp/.X11-unix", "");

auto mount = u8R"({
"type": "bind",
Expand Down Expand Up @@ -82,7 +86,7 @@ int main()

bool xdgRuntimeDirMounted = false;

[mount, &mounts, &xdgRuntimeDirMounted, &content]() {
[mount, &mounts, &xdgRuntimeDirMounted, &content, &bindIfExist]() {
auto *XDGRuntimeDirEnv = getenv("XDG_RUNTIME_DIR"); // NOLINT
if (XDGRuntimeDirEnv == nullptr) {
return;
Expand Down Expand Up @@ -127,15 +131,10 @@ int main()

xdgRuntimeDirMounted = true;

auto pulseMount = mount;
pulseMount["destination"] = cognitiveXDGRuntimeDir / "pulse";
pulseMount["source"] = hostXDGRuntimeDir / "pulse";
mounts.push_back(std::move(pulseMount));

auto gvfsMount = mount;
gvfsMount["destination"] = cognitiveXDGRuntimeDir / "gvfs";
gvfsMount["source"] = hostXDGRuntimeDir / "gvfs";
mounts.push_back(std::move(gvfsMount));
bindIfExist((hostXDGRuntimeDir / "pulse").string(),
(cognitiveXDGRuntimeDir / "pulse").string());
bindIfExist((hostXDGRuntimeDir / "gvfs").string(),
(cognitiveXDGRuntimeDir / "gvfs").string());

[&hostXDGRuntimeDir, &cognitiveXDGRuntimeDir, &mounts]() {
auto *waylandDisplayEnv = getenv("WAYLAND_DISPLAY"); // NOLINT
Expand Down

0 comments on commit 41501cd

Please sign in to comment.