|
18 | 18 |
|
19 | 19 | namespace lfs::io { |
20 | 20 |
|
21 | | -enum class AtomicOutputTempName { |
22 | | - AppendSuffix, |
23 | | - PreserveExtension |
24 | | -}; |
25 | | - |
26 | | -inline std::filesystem::path make_atomic_temp_output_path( |
27 | | - const std::filesystem::path& output_path, |
28 | | - AtomicOutputTempName name_style = AtomicOutputTempName::AppendSuffix) { |
29 | | - static std::atomic_uint64_t counter{0}; |
30 | | - |
31 | | - const auto ticks = std::chrono::steady_clock::now().time_since_epoch().count(); |
32 | | - const auto unique_suffix = std::format(".{}.{}.tmp", ticks, counter.fetch_add(1, std::memory_order_relaxed)); |
33 | | - |
34 | | - if (name_style == AtomicOutputTempName::PreserveExtension && output_path.has_extension()) { |
35 | | - const auto temp_name = output_path.stem().string() + unique_suffix + output_path.extension().string(); |
36 | | - return output_path.parent_path() / temp_name; |
37 | | - } |
| 21 | + enum class AtomicOutputTempName { |
| 22 | + AppendSuffix, |
| 23 | + PreserveExtension |
| 24 | + }; |
38 | 25 |
|
39 | | - return output_path.string() + unique_suffix; |
40 | | -} |
| 26 | + inline std::filesystem::path make_atomic_temp_output_path( |
| 27 | + const std::filesystem::path& output_path, |
| 28 | + AtomicOutputTempName name_style = AtomicOutputTempName::AppendSuffix) { |
| 29 | + static std::atomic_uint64_t counter{0}; |
41 | 30 |
|
42 | | -inline Result<void> ensure_output_parent_directory(const std::filesystem::path& output_path) { |
43 | | - if (output_path.parent_path().empty()) { |
44 | | - return {}; |
45 | | - } |
| 31 | + const auto ticks = std::chrono::steady_clock::now().time_since_epoch().count(); |
| 32 | + const auto unique_suffix = std::format(".{}.{}.tmp", ticks, counter.fetch_add(1, std::memory_order_relaxed)); |
46 | 33 |
|
47 | | - std::error_code ec; |
48 | | - std::filesystem::create_directories(output_path.parent_path(), ec); |
49 | | - if (ec) { |
50 | | - return std::unexpected(Error{ |
51 | | - ErrorCode::WRITE_FAILURE, |
52 | | - std::format("Failed to create output directory '{}': {}", output_path.parent_path().string(), ec.message())}); |
| 34 | + if (name_style == AtomicOutputTempName::PreserveExtension && output_path.has_extension()) { |
| 35 | + const auto temp_name = output_path.stem().string() + unique_suffix + output_path.extension().string(); |
| 36 | + return output_path.parent_path() / temp_name; |
| 37 | + } |
| 38 | + |
| 39 | + return output_path.string() + unique_suffix; |
53 | 40 | } |
54 | 41 |
|
55 | | - return {}; |
56 | | -} |
| 42 | + inline Result<void> ensure_output_parent_directory(const std::filesystem::path& output_path) { |
| 43 | + if (output_path.parent_path().empty()) { |
| 44 | + return {}; |
| 45 | + } |
57 | 46 |
|
58 | | -inline Result<void> replace_atomic_output_file(const std::filesystem::path& temp_path, |
59 | | - const std::filesystem::path& output_path) { |
60 | | -#ifdef _WIN32 |
61 | | - const auto temp_w = temp_path.wstring(); |
62 | | - const auto output_w = output_path.wstring(); |
63 | | - if (!MoveFileExW(temp_w.c_str(), output_w.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) { |
64 | | - return std::unexpected(Error{ |
65 | | - ErrorCode::WRITE_FAILURE, |
66 | | - std::format("Failed to replace '{}' with temporary export '{}': Windows error {}", |
67 | | - output_path.string(), temp_path.string(), GetLastError())}); |
| 47 | + std::error_code ec; |
| 48 | + std::filesystem::create_directories(output_path.parent_path(), ec); |
| 49 | + if (ec) { |
| 50 | + return std::unexpected(Error{ |
| 51 | + ErrorCode::WRITE_FAILURE, |
| 52 | + std::format("Failed to create output directory '{}': {}", output_path.parent_path().string(), ec.message())}); |
| 53 | + } |
| 54 | + |
| 55 | + return {}; |
68 | 56 | } |
| 57 | + |
| 58 | + inline Result<void> replace_atomic_output_file(const std::filesystem::path& temp_path, |
| 59 | + const std::filesystem::path& output_path) { |
| 60 | +#ifdef _WIN32 |
| 61 | + const auto temp_w = temp_path.wstring(); |
| 62 | + const auto output_w = output_path.wstring(); |
| 63 | + if (!MoveFileExW(temp_w.c_str(), output_w.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) { |
| 64 | + return std::unexpected(Error{ |
| 65 | + ErrorCode::WRITE_FAILURE, |
| 66 | + std::format("Failed to replace '{}' with temporary export '{}': Windows error {}", |
| 67 | + output_path.string(), temp_path.string(), GetLastError())}); |
| 68 | + } |
69 | 69 | #else |
70 | | - std::error_code ec; |
71 | | - std::filesystem::rename(temp_path, output_path, ec); |
72 | | - if (ec) { |
73 | | - return std::unexpected(Error{ |
74 | | - ErrorCode::WRITE_FAILURE, |
75 | | - std::format("Failed to replace '{}' with temporary export '{}': {}", |
76 | | - output_path.string(), temp_path.string(), ec.message())}); |
77 | | - } |
| 70 | + std::error_code ec; |
| 71 | + std::filesystem::rename(temp_path, output_path, ec); |
| 72 | + if (ec) { |
| 73 | + return std::unexpected(Error{ |
| 74 | + ErrorCode::WRITE_FAILURE, |
| 75 | + std::format("Failed to replace '{}' with temporary export '{}': {}", |
| 76 | + output_path.string(), temp_path.string(), ec.message())}); |
| 77 | + } |
78 | 78 | #endif |
79 | 79 |
|
80 | | - return {}; |
81 | | -} |
| 80 | + return {}; |
| 81 | + } |
82 | 82 |
|
83 | | -class ScopedAtomicOutputFile { |
84 | | -public: |
85 | | - explicit ScopedAtomicOutputFile( |
86 | | - std::filesystem::path output_path, |
87 | | - AtomicOutputTempName name_style = AtomicOutputTempName::AppendSuffix) |
88 | | - : output_path_(std::move(output_path)) |
89 | | - , temp_path_(make_atomic_temp_output_path(output_path_, name_style)) {} |
| 83 | + class ScopedAtomicOutputFile { |
| 84 | + public: |
| 85 | + explicit ScopedAtomicOutputFile( |
| 86 | + std::filesystem::path output_path, |
| 87 | + AtomicOutputTempName name_style = AtomicOutputTempName::AppendSuffix) |
| 88 | + : output_path_(std::move(output_path)), temp_path_(make_atomic_temp_output_path(output_path_, name_style)) {} |
90 | 89 |
|
91 | | - ScopedAtomicOutputFile(const ScopedAtomicOutputFile&) = delete; |
92 | | - ScopedAtomicOutputFile& operator=(const ScopedAtomicOutputFile&) = delete; |
| 90 | + ScopedAtomicOutputFile(const ScopedAtomicOutputFile&) = delete; |
| 91 | + ScopedAtomicOutputFile& operator=(const ScopedAtomicOutputFile&) = delete; |
93 | 92 |
|
94 | | - ScopedAtomicOutputFile(ScopedAtomicOutputFile&&) = delete; |
95 | | - ScopedAtomicOutputFile& operator=(ScopedAtomicOutputFile&&) = delete; |
| 93 | + ScopedAtomicOutputFile(ScopedAtomicOutputFile&&) = delete; |
| 94 | + ScopedAtomicOutputFile& operator=(ScopedAtomicOutputFile&&) = delete; |
96 | 95 |
|
97 | | - ~ScopedAtomicOutputFile() { |
98 | | - if (!committed_) { |
99 | | - std::error_code ec; |
100 | | - std::filesystem::remove(temp_path_, ec); |
| 96 | + ~ScopedAtomicOutputFile() { |
| 97 | + if (!committed_) { |
| 98 | + std::error_code ec; |
| 99 | + std::filesystem::remove(temp_path_, ec); |
| 100 | + } |
101 | 101 | } |
102 | | - } |
103 | 102 |
|
104 | | - const std::filesystem::path& output_path() const { return output_path_; } |
105 | | - const std::filesystem::path& temp_path() const { return temp_path_; } |
| 103 | + const std::filesystem::path& output_path() const { return output_path_; } |
| 104 | + const std::filesystem::path& temp_path() const { return temp_path_; } |
106 | 105 |
|
107 | | - Result<void> commit() { |
108 | | - auto result = replace_atomic_output_file(temp_path_, output_path_); |
109 | | - if (!result) { |
110 | | - return result; |
| 106 | + Result<void> commit() { |
| 107 | + auto result = replace_atomic_output_file(temp_path_, output_path_); |
| 108 | + if (!result) { |
| 109 | + return result; |
| 110 | + } |
| 111 | + |
| 112 | + committed_ = true; |
| 113 | + return {}; |
111 | 114 | } |
112 | 115 |
|
113 | | - committed_ = true; |
114 | | - return {}; |
| 116 | + private: |
| 117 | + std::filesystem::path output_path_; |
| 118 | + std::filesystem::path temp_path_; |
| 119 | + bool committed_ = false; |
| 120 | + }; |
| 121 | + |
| 122 | + inline bool report_export_progress(const ExportProgressCallback& callback, float progress, const std::string& stage) { |
| 123 | + if (!callback) { |
| 124 | + return true; |
| 125 | + } |
| 126 | + return callback(progress, stage); |
115 | 127 | } |
116 | 128 |
|
117 | | -private: |
118 | | - std::filesystem::path output_path_; |
119 | | - std::filesystem::path temp_path_; |
120 | | - bool committed_ = false; |
121 | | -}; |
| 129 | + inline ExportProgressCallback scale_export_progress( |
| 130 | + ExportProgressCallback callback, |
| 131 | + float start, |
| 132 | + float end) { |
| 133 | + if (!callback) { |
| 134 | + return {}; |
| 135 | + } |
122 | 136 |
|
123 | | -inline bool report_export_progress(const ExportProgressCallback& callback, float progress, const std::string& stage) { |
124 | | - if (!callback) { |
125 | | - return true; |
| 137 | + return [callback = std::move(callback), start, end](float progress, const std::string& stage) { |
| 138 | + const float scaled = start + (end - start) * progress; |
| 139 | + return callback(scaled, stage); |
| 140 | + }; |
126 | 141 | } |
127 | | - return callback(progress, stage); |
128 | | -} |
129 | | - |
130 | | -inline ExportProgressCallback scale_export_progress( |
131 | | - ExportProgressCallback callback, |
132 | | - float start, |
133 | | - float end) { |
134 | | - if (!callback) { |
135 | | - return {}; |
136 | | - } |
137 | | - |
138 | | - return [callback = std::move(callback), start, end](float progress, const std::string& stage) { |
139 | | - const float scaled = start + (end - start) * progress; |
140 | | - return callback(scaled, stage); |
141 | | - }; |
142 | | -} |
143 | 142 |
|
144 | 143 | } // namespace lfs::io |
0 commit comments