MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ write_if_changed()

bool MayaFlux::Reflect::Tool::write_if_changed ( const std::filesystem::path & path,
const std::string & content,
std::string & error )
nodiscard

Replaces path with content only when the content differs.

Definition at line 137 of file Sidecar.cpp.

138{
139 if (read_text(path) == std::optional<std::string>(content)) {
140 return true;
141 }
142 const std::filesystem::path staged = path.string() + ".tmp";
143 {
144 std::ofstream stream(staged, std::ios::binary | std::ios::trunc);
145 stream << content;
146 if (!stream) {
147 error = "cannot write " + staged.string();
148 return false;
149 }
150 }
151 std::error_code code;
152 std::filesystem::rename(staged, path, code);
153 if (code) {
154 std::filesystem::remove(staged, code);
155 error = "cannot replace " + path.string();
156 return false;
157 }
158 return true;
159}
std::optional< std::string > read_text(const std::filesystem::path &path)
The whole content of path, or nullopt when it cannot be read.
Definition Sidecar.cpp:30

References read_text().

Referenced by ensure_stub(), insert_include(), write_depfile(), and write_generated().

Here is the call graph for this function:
Here is the caller graph for this function: