writeFile(): Add error context to writeFull() failure

Issue #4092.
This commit is contained in:
Eelco Dolstra 2020-10-09 16:02:53 +02:00
parent 636ec17139
commit 87157b2bd3

View file

@ -326,7 +326,12 @@ void writeFile(const Path & path, const string & s, mode_t mode)
AutoCloseFD fd = open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode); AutoCloseFD fd = open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode);
if (!fd) if (!fd)
throw SysError("opening file '%1%'", path); throw SysError("opening file '%1%'", path);
writeFull(fd.get(), s); try {
writeFull(fd.get(), s);
} catch (Error & e) {
e.addTrace({}, "writing file '%1%'", path);
throw;
}
} }
@ -338,11 +343,16 @@ void writeFile(const Path & path, Source & source, mode_t mode)
std::vector<unsigned char> buf(64 * 1024); std::vector<unsigned char> buf(64 * 1024);
while (true) { try {
try { while (true) {
auto n = source.read(buf.data(), buf.size()); try {
writeFull(fd.get(), (unsigned char *) buf.data(), n); auto n = source.read(buf.data(), buf.size());
} catch (EndOfFile &) { break; } writeFull(fd.get(), (unsigned char *) buf.data(), n);
} catch (EndOfFile &) { break; }
}
} catch (Error & e) {
e.addTrace({}, "writing file '%1%'", path);
throw;
} }
} }