diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2020-10-09 16:02:53 +0200 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2020-10-09 16:02:53 +0200 |
commit | 87157b2bd3224b8329ffcb73e92c64bdc36cff16 (patch) | |
tree | 899c66277480e8adf7c67df6bc3cc451b68b58c1 /src/libutil/util.cc | |
parent | 636ec171391660e986623cb75c400a99d652c991 (diff) |
writeFile(): Add error context to writeFull() failure
Issue #4092.
Diffstat (limited to 'src/libutil/util.cc')
-rw-r--r-- | src/libutil/util.cc | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc index f07e99885..1a8873136 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -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); if (!fd) 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); - while (true) { - try { - auto n = source.read(buf.data(), buf.size()); - writeFull(fd.get(), (unsigned char *) buf.data(), n); - } catch (EndOfFile &) { break; } + try { + while (true) { + try { + auto n = source.read(buf.data(), buf.size()); + writeFull(fd.get(), (unsigned char *) buf.data(), n); + } catch (EndOfFile &) { break; } + } + } catch (Error & e) { + e.addTrace({}, "writing file '%1%'", path); + throw; } } |