Code cleanup

This commit is contained in:
pmb
2025-06-25 17:25:50 -07:00
parent e880f6ba1d
commit 2a0c8b6dc8
4 changed files with 42 additions and 79 deletions
+36 -4
View File
@@ -3,14 +3,46 @@
#include <fstream>
#include <string_view>
const std::filesystem::path kPATH{"/var/finger/users/"};
bool RealFilesystemWrapper::exists(const std::filesystem::path &path) const {
return std::filesystem::exists(path);
}
std::string
RealFilesystemWrapper::read_file(const std::filesystem::path &path) const {
std::ifstream file(path);
if (!file.is_open()) {
return "";
}
std::string content;
std::string line;
while (std::getline(file, line)) {
content += line + "\n";
}
if (content.empty()) {
return "";
}
// Return the content with proper line endings
if (content.back() == '\n') {
content.pop_back(); // Remove the last newline
content += "\r\n";
return content;
}
content += "\r\n";
return content;
}
std::string process(const std::string &username) {
RealFilesystemWrapper fs;
return process(username, fs);
return process(username, fs, kPATH);
}
std::string process(const std::string &username, const IFilesystemWrapper &fs) {
std::string process(const std::string &username, const IFilesystemWrapper &fs,
const std::filesystem::path &basepath) {
try {
// Check for directory traversal patterns
if (username.find("../") != std::string::npos ||
@@ -34,7 +66,7 @@ std::string process(const std::string &username, const IFilesystemWrapper &fs) {
}
// Attempt to open the plan file (if any) and return the contents as a string
std::filesystem::path planPath = kPATH / username;
std::filesystem::path planPath = basepath / username;
// Check if the plan file exists using the filesystem wrapper
if (!fs.exists(planPath)) {