| 106 | } |
| 107 | |
| 108 | void PAMHelper::AddEntryToFile(const std::filesystem::path &filePath, const std::string &entry) { |
| 109 | auto fileData = Shell::ReadBytes(filePath); |
| 110 | auto fileStr = std::string(fileData.begin(), fileData.end()); |
| 111 | |
| 112 | m_Logger(fmt::format("Adding PAM entry to {}...", filePath.string())); |
| 113 | std::string resultStr{}; |
| 114 | auto hasBegin = fileStr.contains("#%PAM-1.0\n"); |
| 115 | if(!hasBegin) { |
| 116 | resultStr.append("#%PAM-1.0\n"); |
| 117 | resultStr.append(entry + '\n'); |
| 118 | } |
| 119 | |
| 120 | for(const auto &line : StringUtils::Split(fileStr, "\n")) { |
| 121 | if(line == "#%PAM-1.0" && hasBegin) { |
| 122 | resultStr.append(line + '\n'); |
| 123 | resultStr.append(entry + '\n'); |
| 124 | } else { |
| 125 | resultStr.append(line + '\n'); |
| 126 | } |
| 127 | } |
| 128 | if(!Shell::WriteBytes(filePath, {resultStr.begin(), resultStr.end()})) |
| 129 | throw std::runtime_error(I18n::Get("error_file_write", filePath.string())); |
| 130 | } |
| 131 | |
| 132 | void PAMHelper::RemoveEntryFromFile(const std::filesystem::path &filePath, const std::string &entry) { |
| 133 | auto fileData = Shell::ReadBytes(filePath); |
nothing calls this directly
no outgoing calls
no test coverage detected