From 88ef0530001d43be8f18fac43a280169b45db852 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Sat, 15 Feb 2020 12:25:37 -0500 Subject: error checking in dump() removed a bunch of "{} saved" in the logs --- src/shared/directoryentry.cpp | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'src/shared/directoryentry.cpp') diff --git a/src/shared/directoryentry.cpp b/src/shared/directoryentry.cpp index 394eda7e..6c5ad9ae 100644 --- a/src/shared/directoryentry.cpp +++ b/src/shared/directoryentry.cpp @@ -1685,14 +1685,33 @@ void DirectoryEntry::addFileToList( // fileNameLower has been moved from this point } +struct DumpFailed : public std::runtime_error +{ + using runtime_error::runtime_error; +}; + void DirectoryEntry::dump(const std::wstring& file) const { - std::FILE* f = nullptr; - auto e = _wfopen_s(&f, file.c_str(), L"wb"); + try + { + std::FILE* f = nullptr; + auto e = _wfopen_s(&f, file.c_str(), L"wb"); + + if (e != 0 || !f) { + throw DumpFailed(fmt::format( + "failed to open, {} ({})", std::strerror(e), e)); + } - dump(f, L"Data"); + Guard g([&]{ std::fclose(f); }); - std::fclose(f); + dump(f, L"Data"); + } + catch(DumpFailed& e) + { + log::error( + "failed to write list to '{}': {}", + QString::fromStdWString(file).toStdString(), e.what()); + } } void DirectoryEntry::dump(std::FILE* f, const std::wstring& parentPath) const @@ -1716,7 +1735,12 @@ void DirectoryEntry::dump(std::FILE* f, const std::wstring& parentPath) const const auto line = path + L"\t(" + o.getName() + L")\r\n"; const auto lineu8 = MOShared::ToString(line, true); - std::fwrite(lineu8.data(), lineu8.size(), 1, f); + + if (std::fwrite(lineu8.data(), lineu8.size(), 1, f) != 1) { + const auto e = errno; + throw DumpFailed(fmt::format( + "failed to write, {} ({})", std::strerror(e), e)); + } } } -- cgit v1.3.1