Skip to content

Commit

Permalink
refactor(gen): add blackList for hide some directory all the time
Browse files Browse the repository at this point in the history
std::exist will follow symlink
so we should get status at first

Signed-off-by: ComixHe <[email protected]>
  • Loading branch information
ComixHe authored and dengbo11 committed Nov 16, 2024
1 parent 57c3306 commit e6c9279
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions apps/generators/30-user-home/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <unordered_set>

#include <pwd.h>
#include <unistd.h>
Expand Down Expand Up @@ -310,7 +311,9 @@ int main() // NOLINT
// FIXME: we should resolve user dirs through ${XDG_CONFIG_HOME}/user-dirs.dirs

// process blocklist
static const std::unordered_set<std::string_view> blackList = { ".gnupg", ".ssh" };
auto disallowedDirs = directories.disallowed.value_or(std::vector<std::string>{});
disallowedDirs.insert(disallowedDirs.end(), blackList.begin(), blackList.end());
std::sort(disallowedDirs.begin(), disallowedDirs.end());
auto dupIt = std::unique(disallowedDirs.begin(), disallowedDirs.end());
disallowedDirs.erase(dupIt, disallowedDirs.end());
Expand Down Expand Up @@ -349,24 +352,23 @@ int main() // NOLINT
return -1;
}

auto hostLocation = hostHomeDir / relative;
if (!std::filesystem::exists(hostLocation, ec)) {
if (ec) {
std::cerr << "failed to get state of " << hostLocation << ": " << ec.message()
<< std::endl;
return -1;
}

if (blackList.find(relative.string()) != blackList.end()) {
continue;
}

if (!std::filesystem::is_symlink(hostLocation, ec)) {
if (ec) {
std::cerr << "failed to get file type of" << hostLocation << ": " << ec.message()
<< std::endl;
return -1;
auto hostLocation = hostHomeDir / relative;
std::filesystem::file_status status = std::filesystem::symlink_status(hostLocation, ec);
if (ec) {
if (ec == std::errc::no_such_file_or_directory) {
continue;
}

std::cerr << "failed to get file type of" << hostLocation << ": " << ec.message()
<< std::endl;
return -1;
}

if (status.type() != std::filesystem::file_type::symlink) {
if (!mountDir(hostLocation, cognitiveHomeDir / relative)) {
return -1;
}
Expand Down

0 comments on commit e6c9279

Please sign in to comment.