Skip to content

Commit 9cb49fd

Browse files
committed
home: a reader and a writer of a home's file retry the one refusal another program's hold explains, on every system
1 parent a5a1de8 commit 9cb49fd

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

‎src/home/files.cpp‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module mcpp.home.files;
44

55
import std;
66
import mcpplibs.os.fs;
7-
import mcpplibs.os.platform;
87

98
namespace 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+
107117
bool 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});

‎src/home/files.cppm‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ bool write_replacing(const std::filesystem::path& file, std::string_view text);
103103

104104
// The text of `file`, or nothing when it cannot be read. Windows refuses an
105105
// open for a moment while another program renames over the file
106-
// (write_replacing): there an open refused for any reason but absence is
107-
// retried, for up to two seconds, as the writer's rename is.
106+
// (write_replacing): an open refused for the reason another program's hold
107+
// gives is retried, for up to two seconds, as the writer's rename is.
108108
std::optional<std::string> read_whole(const std::filesystem::path& file);
109109

110110
// Whether a directory another program wrote may be read and run from: it

0 commit comments

Comments
 (0)