blob: a3a80f2ce8151277bfc657ffb6b492107a2eca4a [file] [log] [blame]
Tomáš Pecka9b1c9672020-11-11 15:24:06 +01001#include <fstream>
2#include "FileInjector.h"
3
4/** @short Creates a file with specific permissions and content */
5FileInjector::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) */
17FileInjector::~FileInjector() noexcept(false)
18{
19 std::filesystem::remove(path);
20}
21
22/** @short Sets file permissions */
23void FileInjector::setPermissions(const std::filesystem::perms permissions)
24{
25 std::filesystem::permissions(path, permissions);
26}