Tomáš Pecka | 9b1c967 | 2020-11-11 15:24:06 +0100 | [diff] [blame] | 1 | #include <fstream> |
| 2 | #include "FileInjector.h" |
| 3 | |
| 4 | /** @short Creates a file with specific permissions and content */ |
| 5 | FileInjector::FileInjector(const std::filesystem::path& path, const std::filesystem::perms permissions, const std::string& content) |
| 6 | : path(path) |
| 7 | { |
| 8 | auto fileStream = std::ofstream(path, std::ios_base::out | std::ios_base::trunc); |
| 9 | if (!fileStream.is_open()) { |
| 10 | throw std::invalid_argument("FileInjector could not open file " + std::string(path) + " for writing"); |
| 11 | } |
| 12 | fileStream << content; |
| 13 | std::filesystem::permissions(path, permissions); |
| 14 | } |
| 15 | |
| 16 | /** @short Removes file associated with this FileInjector instance (if exists) */ |
| 17 | FileInjector::~FileInjector() noexcept(false) |
| 18 | { |
| 19 | std::filesystem::remove(path); |
| 20 | } |
| 21 | |
| 22 | /** @short Sets file permissions */ |
| 23 | void FileInjector::setPermissions(const std::filesystem::perms permissions) |
| 24 | { |
| 25 | std::filesystem::permissions(path, permissions); |
| 26 | } |