-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileStaging.mpp
More file actions
132 lines (110 loc) · 3.84 KB
/
Copy pathFileStaging.mpp
File metadata and controls
132 lines (110 loc) · 3.84 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
export module CppUtils.FileSystem.FileStaging;
import std;
export namespace CppUtils::FileSystem
{
class FileStaging final
{
private:
std::filesystem::path m_inputPath{};
std::filesystem::path m_stagedPath{};
std::filesystem::path m_quarantineDirectory{};
std::filesystem::path m_archiveDirectory{};
public:
FileStaging() = default;
FileStaging(
std::filesystem::path inputPath,
std::filesystem::path stagedPath,
std::filesystem::path quarantineDirectory = "",
std::filesystem::path archiveDirectory = ""):
m_inputPath{std::move(inputPath)},
m_stagedPath{std::move(stagedPath)},
m_quarantineDirectory{std::move(quarantineDirectory)},
m_archiveDirectory{std::move(archiveDirectory)}
{}
[[nodiscard]] static auto stage(
const std::filesystem::path& inputFilePath,
const std::filesystem::path& stagingDirectory = "staging",
const std::filesystem::path& quarantineDirectory = "",
const std::filesystem::path& archiveDirectory = "") -> std::expected<FileStaging, std::error_code>
{
auto errorCode = std::error_code{};
std::filesystem::create_directories(stagingDirectory, errorCode);
if (errorCode)
return std::unexpected{errorCode};
const auto now = std::chrono::system_clock::now().time_since_epoch();
const auto timestamp = std::chrono::duration_cast<std::chrono::microseconds>(now).count();
const auto uniqueFilename = std::format("{}_{}{}", inputFilePath.stem().string(), timestamp, inputFilePath.extension().string());
const auto targetStagedPath = stagingDirectory / uniqueFilename;
std::filesystem::rename(inputFilePath, targetStagedPath, errorCode);
if (errorCode)
return std::unexpected{errorCode};
return FileStaging{
inputFilePath,
targetStagedPath,
quarantineDirectory,
archiveDirectory};
}
[[nodiscard]] auto inputPath() const noexcept -> const std::filesystem::path&
{
return m_inputPath;
}
[[nodiscard]] auto stagedPath() const noexcept -> const std::filesystem::path&
{
return m_stagedPath;
}
[[nodiscard]] auto archiveDirectory() const noexcept -> const std::filesystem::path&
{
return m_archiveDirectory;
}
[[nodiscard]] auto quarantineDirectory() const noexcept -> const std::filesystem::path&
{
return m_quarantineDirectory;
}
auto complete() const -> std::expected<std::filesystem::path, std::error_code>
{
auto errorCode = std::error_code{};
if (std::ranges::empty(m_archiveDirectory))
{
std::filesystem::remove(m_stagedPath, errorCode);
if (errorCode)
return std::unexpected{errorCode};
return "";
}
std::filesystem::create_directories(m_archiveDirectory, errorCode);
if (errorCode)
return std::unexpected{errorCode};
const auto archivePath = m_archiveDirectory / m_stagedPath.filename();
std::filesystem::rename(m_stagedPath, archivePath, errorCode);
if (errorCode)
return std::unexpected{errorCode};
return archivePath;
}
auto abort() const -> std::expected<std::filesystem::path, std::error_code>
{
auto errorCode = std::error_code{};
if (std::ranges::empty(m_quarantineDirectory))
{
std::filesystem::remove(m_stagedPath, errorCode);
if (errorCode)
return std::unexpected{errorCode};
return "";
}
std::filesystem::create_directories(m_quarantineDirectory, errorCode);
if (errorCode)
return std::unexpected{errorCode};
const auto quarantinePath = m_quarantineDirectory / m_stagedPath.filename();
std::filesystem::rename(m_stagedPath, quarantinePath, errorCode);
if (errorCode)
return std::unexpected{errorCode};
return quarantinePath;
}
auto retry() const -> std::expected<std::filesystem::path, std::error_code>
{
auto errorCode = std::error_code{};
std::filesystem::rename(m_stagedPath, m_inputPath, errorCode);
if (errorCode)
return std::unexpected{errorCode};
return m_inputPath;
}
};
}