diff --git a/implementations/Crafter.Build-Clang.cpp b/implementations/Crafter.Build-Clang.cpp index f512509..85b860c 100644 --- a/implementations/Crafter.Build-Clang.cpp +++ b/implementations/Crafter.Build-Clang.cpp @@ -763,6 +763,35 @@ namespace { d.programEntries = placed; return WriteFlatpakFiles(d); } + + // The name a file is written under before MoveIntoPlace gives it its + // own: beside its place, so that the move is a rename, and no other + // write's, in this process or in another. A build writes files that a + // step of another crafter-build may be reading at that moment: a + // project loaded with GitProject is built in its clone in the cache, + // which every project that names the same source shares, and nothing + // keeps two crafter-builds from building it at once. The name is made + // of random bits and not of the process's id: a crafter-build that + // runs as a Flatpak has the process ids of its own sandbox, and two of + // them the same ones. + fs::path WrittenBeside(const fs::path& file) { + std::random_device random; + return fs::path(file) += std::format(".{:08x}{:08x}.tmp", random(), random()); + } + + // Moves `written` to `file`. The rename puts it there in one step: what + // opens `file` finds the file of before or this one, never none and + // never part of one, and what has the file of before open goes on + // reading that one. Returns what went wrong, with `written` removed; + // empty when nothing did. + std::string MoveIntoPlace(const fs::path& written, const fs::path& file) { + std::error_code ec; + fs::rename(written, file, ec); + if (!ec) return ""; + std::string failed = std::format("cannot move {} to {}: {}", written.string(), file.string(), ec.message()); + fs::remove(written, ec); + return failed; + } } BuildResult Crafter::Build(Configuration& config, std::unordered_map>& depResults, std::mutex& depMutex) { @@ -1524,11 +1553,23 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map