diff options
| author | Robert Schumacher <roschuma@microsoft.com> | 2018-02-21 22:18:15 -0800 |
|---|---|---|
| committer | Robert Schumacher <roschuma@microsoft.com> | 2018-02-21 22:18:43 -0800 |
| commit | 65e241cf8b47a07b85efae85bac14e723864dccb (patch) | |
| tree | 85c3904f72e4847ff148844eeb41e715d70e5803 | |
| parent | f6d652c1bf36717d492305defa3dcf94f724b2c9 (diff) | |
| download | vcpkg-65e241cf8b47a07b85efae85bac14e723864dccb.tar.gz vcpkg-65e241cf8b47a07b85efae85bac14e723864dccb.zip | |
[vcpkg] Add non-throwing implementation of write_contents()
| -rw-r--r-- | toolsrc/include/vcpkg/base/files.h | 10 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/base/checks.cpp | 4 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/base/files.cpp | 21 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/metrics.cpp | 5 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/userconfig.cpp | 3 |
5 files changed, 33 insertions, 10 deletions
diff --git a/toolsrc/include/vcpkg/base/files.h b/toolsrc/include/vcpkg/base/files.h index 9bdc0aa4b..a2bf9c33c 100644 --- a/toolsrc/include/vcpkg/base/files.h +++ b/toolsrc/include/vcpkg/base/files.h @@ -34,7 +34,7 @@ namespace vcpkg::Files virtual std::vector<fs::path> get_files_non_recursive(const fs::path& dir) const = 0; virtual void write_lines(const fs::path& file_path, const std::vector<std::string>& lines) = 0; - virtual void write_contents(const fs::path& file_path, const std::string& data) = 0; + virtual void write_contents(const fs::path& file_path, const std::string& data, std::error_code& ec) = 0; virtual void rename(const fs::path& oldpath, const fs::path& newpath) = 0; virtual bool remove(const fs::path& path) = 0; virtual bool remove(const fs::path& path, std::error_code& ec) = 0; @@ -51,6 +51,14 @@ namespace vcpkg::Files fs::copy_options opts, std::error_code& ec) = 0; virtual fs::file_status status(const fs::path& path, std::error_code& ec) const = 0; + + inline void write_contents(const fs::path& file_path, const std::string& data) + { + std::error_code ec; + write_contents(file_path, data, ec); + Checks::check_exit( + VCPKG_LINE_INFO, ec, "error while writing file: %s: %s", file_path.u8string(), ec.message()); + } }; Filesystem& get_real_filesystem(); diff --git a/toolsrc/src/vcpkg/base/checks.cpp b/toolsrc/src/vcpkg/base/checks.cpp index 23f2cc630..2977a1d8b 100644 --- a/toolsrc/src/vcpkg/base/checks.cpp +++ b/toolsrc/src/vcpkg/base/checks.cpp @@ -10,6 +10,10 @@ namespace vcpkg::Checks { [[noreturn]] static void cleanup_and_exit(const int exit_code) { + static std::atomic<bool> have_entered = false; + if (have_entered) std::terminate(); + have_entered = true; + const auto elapsed_us = GlobalState::timer.lock()->microseconds(); auto metrics = Metrics::g_metrics.lock(); diff --git a/toolsrc/src/vcpkg/base/files.cpp b/toolsrc/src/vcpkg/base/files.cpp index 060a14da3..95ae0fd33 100644 --- a/toolsrc/src/vcpkg/base/files.cpp +++ b/toolsrc/src/vcpkg/base/files.cpp @@ -164,21 +164,30 @@ namespace vcpkg::Files { return fs::stdfs::status(path, ec); } - virtual void write_contents(const fs::path& file_path, const std::string& data) override + virtual void write_contents(const fs::path& file_path, const std::string& data, std::error_code& ec) override { + ec = std::error_code(); + FILE* f = nullptr; #if defined(_WIN32) - auto ec = _wfopen_s(&f, file_path.native().c_str(), L"wb"); + auto err = _wfopen_s(&f, file_path.native().c_str(), L"wb"); #else f = fopen(file_path.native().c_str(), "wb"); - int ec = f != nullptr ? 0 : 1; + int err = f != nullptr ? 0 : 1; #endif - Checks::check_exit( - VCPKG_LINE_INFO, ec == 0, "Error: Could not open file for writing: %s", file_path.u8string().c_str()); + if (err != 0) + { + ec.assign(err, std::system_category()); + return; + } + auto count = fwrite(data.data(), sizeof(data[0]), data.size(), f); fclose(f); - Checks::check_exit(VCPKG_LINE_INFO, count == data.size()); + if (count != data.size()) + { + ec = std::make_error_code(std::errc::no_space_on_device); + } } }; diff --git a/toolsrc/src/vcpkg/metrics.cpp b/toolsrc/src/vcpkg/metrics.cpp index 1f9ddde38..1a322e99c 100644 --- a/toolsrc/src/vcpkg/metrics.cpp +++ b/toolsrc/src/vcpkg/metrics.cpp @@ -427,8 +427,9 @@ namespace vcpkg::Metrics } const fs::path vcpkg_metrics_txt_path = temp_folder_path / ("vcpkg" + generate_random_UUID() + ".txt"); - fs.write_contents(vcpkg_metrics_txt_path, payload); - + std::error_code ec; + fs.write_contents(vcpkg_metrics_txt_path, payload, ec); + if (ec) return; const std::string cmd_line = Strings::format("start \"vcpkgmetricsuploader.exe\" \"%s\" \"%s\"", temp_folder_path_exe.u8string(), vcpkg_metrics_txt_path.u8string()); diff --git a/toolsrc/src/vcpkg/userconfig.cpp b/toolsrc/src/vcpkg/userconfig.cpp index 906594691..c7c57ce26 100644 --- a/toolsrc/src/vcpkg/userconfig.cpp +++ b/toolsrc/src/vcpkg/userconfig.cpp @@ -78,7 +78,8 @@ namespace vcpkg user_id, user_time, user_mac, - last_completed_survey)); + last_completed_survey), + ec); } catch (...) { |
