@@ -4,7 +4,6 @@ module mcpp.home.files;
44
55import std;
66import mcpplibs.os.fs;
7- import mcpplibs.os.platform;
87
98namespace mcpp ::home {
109
@@ -104,6 +103,17 @@ Turn::~Turn() {
104103 if (auto it = turns.find (key_); it != turns.end () && --it->second .depth == 0 ) turns.erase (it);
105104}
106105
106+ // A refusal another program's hold on the file explains, which a moment
107+ // later may not be there: Windows refuses an open or a rename while another
108+ // program holds the file, or while a rename over it leaves the replaced copy
109+ // pending deletion (a sharing violation, or access denied). Elsewhere these
110+ // answers are final, and cost the failing call the wait.
111+ namespace {
112+ bool refused_for_now (const mcpplibs::os::error& e) {
113+ return e.code == mcpplibs::os::errc::again || e.code == mcpplibs::os::errc::permission;
114+ }
115+ } // namespace
116+
107117bool replace_file (const std::filesystem::path& source, const std::filesystem::path& destination,
108118 std::error_code& ec) {
109119 // Windows refuses the rename while another program holds the destination
@@ -119,11 +129,7 @@ bool replace_file(const std::filesystem::path& source, const std::filesystem::pa
119129 for (;;) {
120130 const auto r = mcpplibs::os::fs::rename (source, destination, true );
121131 if (r) { ec.clear (); return true ; }
122- // A refusal is retried as the sharing violation it is on Windows;
123- // elsewhere it is final, and costs the failing write the wait.
124- const bool held = r.error ().code == mcpplibs::os::errc::again
125- || r.error ().code == mcpplibs::os::errc::permission;
126- if (!held || std::chrono::steady_clock::now () + delay > deadline) {
132+ if (!refused_for_now (r.error ()) || std::chrono::steady_clock::now () + delay > deadline) {
127133 ec = std::make_error_code (r.error ().code == mcpplibs::os::errc::not_found
128134 ? std::errc::no_such_file_or_directory
129135 : std::errc::permission_denied);
@@ -178,10 +184,8 @@ std::optional<std::string> read_whole(const std::filesystem::path& file) {
178184 // reader's open and the query of the name alike with a refusal (the
179185 // copy being replaced is pending deletion), and a reader that took the
180186 // query's failure for absence gave up within the rename (unit
181- // Config/ConfigValueIsReplacedWhole, 2026-11-14). Elsewhere a rename
182- // replaces the name at once, and a refused read is final.
183- if (!mcpplibs::os::is_windows || text.error ().code == mcpplibs::os::errc::not_found
184- || std::chrono::steady_clock::now () + delay > deadline)
187+ // Config/ConfigValueIsReplacedWhole, 2026-11-14).
188+ if (!refused_for_now (text.error ()) || std::chrono::steady_clock::now () + delay > deadline)
185189 return std::nullopt ;
186190 std::this_thread::sleep_for (delay);
187191 delay = std::min (delay * 2 , std::chrono::milliseconds{200 });
0 commit comments