MCPcopy Create free account
hub / github.com/MeisApps/pcbu-desktop / AddEntryToFile

Method AddEntryToFile

desktop/src/installer/PAMHelper.cpp:108–130  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

106}
107
108void 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
132void PAMHelper::RemoveEntryFromFile(const std::filesystem::path &filePath, const std::string &entry) {
133 auto fileData = Shell::ReadBytes(filePath);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected