-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathTargetFile.h
More file actions
51 lines (40 loc) · 1.43 KB
/
TargetFile.h
File metadata and controls
51 lines (40 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once
#include <string>
#include <fstream>
#include <optional>
#include <cerrno>
#include <filesystem>
namespace codeql {
// Only the first process trying to create a `TargetFile` for a given `target` is allowed to do
// so, all others will have `create` return `std::nullopt`.
// The content streamed to the `TargetFile` is written to `workingDir/target`, and is moved onto
// `targetDir/target` on destruction.
class TargetFile {
std::filesystem::path workingPath;
std::filesystem::path targetPath;
std::ofstream out;
public:
static std::optional<TargetFile> create(const std::filesystem::path& target,
const std::filesystem::path& targetDir,
const std::filesystem::path& workingDir);
~TargetFile() { commit(); }
TargetFile(TargetFile&& other) = default;
// move assignment deleted as non-trivial and not needed
TargetFile& operator=(TargetFile&& other) = delete;
template <typename T>
TargetFile& operator<<(T&& value) {
errno = 0;
out << value;
checkOutput("write to");
return *this;
}
const std::filesystem::path& target() const { return targetPath; }
private:
TargetFile(const std::filesystem::path& target,
const std::filesystem::path& targetDir,
const std::filesystem::path& workingDir);
bool init();
void checkOutput(const char* action);
void commit();
};
} // namespace codeql