From 285b9f5867844dfa18406967c9352b1ff597c00d Mon Sep 17 00:00:00 2001 From: Robert Schumacher Date: Sun, 21 Feb 2021 18:31:26 +0000 Subject: [PATCH] [Filesystem] Prefer .read_contents(path, linfo) overload for better error messages --- include/vcpkg/base/files.h | 1 + src/vcpkg/base/files.cpp | 16 ++++++++++++++-- src/vcpkg/build.cpp | 3 +-- src/vcpkg/commands.upload-metrics.cpp | 2 +- src/vcpkg/export.cpp | 2 +- src/vcpkg/statusparagraphs.cpp | 2 +- src/vcpkg/tools.cpp | 2 +- src/vcpkg/vcpkglib.cpp | 3 +-- 8 files changed, 21 insertions(+), 10 deletions(-) diff --git a/include/vcpkg/base/files.h b/include/vcpkg/base/files.h index 5f2e96e91e..0a2cf5f678 100644 --- a/include/vcpkg/base/files.h +++ b/include/vcpkg/base/files.h @@ -172,6 +172,7 @@ namespace vcpkg::Files /// Read text lines from a file /// Lines will have up to one trailing carriage-return character stripped (CRLF) virtual Expected> read_lines(const fs::path& file_path) const = 0; + std::vector read_lines(const fs::path& file_path, LineInfo linfo) const; virtual fs::path find_file_recursively_up(const fs::path& starting_dir, const fs::path& filename) const = 0; virtual std::vector get_files_recursive(const fs::path& dir) const = 0; virtual std::vector get_files_non_recursive(const fs::path& dir) const = 0; diff --git a/src/vcpkg/base/files.cpp b/src/vcpkg/base/files.cpp index 3d66d56291..083e6f6545 100644 --- a/src/vcpkg/base/files.cpp +++ b/src/vcpkg/base/files.cpp @@ -455,6 +455,18 @@ namespace vcpkg::Files } } + std::vector Filesystem::read_lines(const fs::path& path, LineInfo linfo) const + { + auto maybe_lines = this->read_lines(path); + if (auto p = maybe_lines.get()) + { + return std::move(*p); + } + + Checks::exit_with_message( + linfo, "error reading file: %s: %s", fs::u8string(path), maybe_lines.error().message()); + } + std::string Filesystem::read_contents(const fs::path& path, LineInfo linfo) const { auto maybe_contents = this->read_contents(path); @@ -733,6 +745,7 @@ namespace vcpkg::Files std::fstream file_stream(file_path, std::ios_base::in | std::ios_base::binary); if (file_stream.fail()) { + Debug::print("Failed to open: ", fs::u8string(file_path), '\n'); return std::make_error_code(std::errc::no_such_file_or_directory); } @@ -756,10 +769,9 @@ namespace vcpkg::Files std::fstream file_stream(file_path, std::ios_base::in | std::ios_base::binary); if (file_stream.fail()) { - Debug::print("Missing path: ", fs::u8string(file_path), '\n'); + Debug::print("Failed to open: ", fs::u8string(file_path), '\n'); return std::make_error_code(std::errc::no_such_file_or_directory); } - std::vector output; std::string line; while (std::getline(file_stream, line)) diff --git a/src/vcpkg/build.cpp b/src/vcpkg/build.cpp index 5b1374b23f..43b9db3d2b 100644 --- a/src/vcpkg/build.cpp +++ b/src/vcpkg/build.cpp @@ -1033,8 +1033,7 @@ namespace vcpkg::Build #endif auto& helpers = paths.get_cmake_script_hashes(); - auto portfile_contents = - fs.read_contents(port_dir / fs::u8path("portfile.cmake")).value_or_exit(VCPKG_LINE_INFO); + auto portfile_contents = fs.read_contents(port_dir / fs::u8path("portfile.cmake"), VCPKG_LINE_INFO); for (auto&& helper : helpers) { if (Strings::case_insensitive_ascii_contains(portfile_contents, helper.first)) diff --git a/src/vcpkg/commands.upload-metrics.cpp b/src/vcpkg/commands.upload-metrics.cpp index acf5a53774..44ac65d79b 100644 --- a/src/vcpkg/commands.upload-metrics.cpp +++ b/src/vcpkg/commands.upload-metrics.cpp @@ -21,7 +21,7 @@ namespace vcpkg::Commands::UploadMetrics { (void)args.parse_arguments(COMMAND_STRUCTURE); const auto& payload_path = args.command_arguments[0]; - auto payload = fs.read_contents(payload_path).value_or_exit(VCPKG_LINE_INFO); + auto payload = fs.read_contents(payload_path, VCPKG_LINE_INFO); Metrics::g_metrics.lock()->upload(payload); Checks::exit_success(VCPKG_LINE_INFO); } diff --git a/src/vcpkg/export.cpp b/src/vcpkg/export.cpp index fd882cb47e..def8d24003 100644 --- a/src/vcpkg/export.cpp +++ b/src/vcpkg/export.cpp @@ -542,7 +542,7 @@ namespace vcpkg::Export action.spec.triplet().to_string(), raw_exported_dir_path / "installed" / "vcpkg" / "info" / (binary_paragraph.fullstem() + ".list")); - auto lines = fs.read_lines(paths.listfile_path(binary_paragraph)).value_or_exit(VCPKG_LINE_INFO); + auto lines = fs.read_lines(paths.listfile_path(binary_paragraph), VCPKG_LINE_INFO); std::vector files; for (auto&& suffix : lines) { diff --git a/src/vcpkg/statusparagraphs.cpp b/src/vcpkg/statusparagraphs.cpp index ba18152248..8fc8d9be68 100644 --- a/src/vcpkg/statusparagraphs.cpp +++ b/src/vcpkg/statusparagraphs.cpp @@ -175,7 +175,7 @@ namespace vcpkg { iobj.insert("usage", Json::Value::string(std::move(usage.message))); } - auto owns_files = fs.read_lines(paths.listfile_path(ipv.core->package)).value_or_exit(VCPKG_LINE_INFO); + auto owns_files = fs.read_lines(paths.listfile_path(ipv.core->package), VCPKG_LINE_INFO); Json::Array owns; for (auto&& owns_file : owns_files) owns.push_back(Json::Value::string(std::move(owns_file))); diff --git a/src/vcpkg/tools.cpp b/src/vcpkg/tools.cpp index eaef5a6b9d..18af78992d 100644 --- a/src/vcpkg/tools.cpp +++ b/src/vcpkg/tools.cpp @@ -63,7 +63,7 @@ namespace vcpkg static const std::string XML_VERSION = "2"; static const fs::path XML_PATH = paths.scripts / "vcpkgTools.xml"; static const std::regex XML_VERSION_REGEX{R"###()###"}; - static const std::string XML = paths.get_filesystem().read_contents(XML_PATH).value_or_exit(VCPKG_LINE_INFO); + static const std::string XML = paths.get_filesystem().read_contents(XML_PATH, VCPKG_LINE_INFO); std::smatch match_xml_version; const bool has_xml_version = std::regex_search(XML.cbegin(), XML.cend(), match_xml_version, XML_VERSION_REGEX); Checks::check_exit(VCPKG_LINE_INFO, diff --git a/src/vcpkg/vcpkglib.cpp b/src/vcpkg/vcpkglib.cpp index 5b8e4b4390..737b25cc51 100644 --- a/src/vcpkg/vcpkglib.cpp +++ b/src/vcpkg/vcpkglib.cpp @@ -213,8 +213,7 @@ namespace vcpkg } const fs::path listfile_path = paths.listfile_path(pgh->package); - std::vector installed_files_of_current_pgh = - fs.read_lines(listfile_path).value_or_exit(VCPKG_LINE_INFO); + std::vector installed_files_of_current_pgh = fs.read_lines(listfile_path, VCPKG_LINE_INFO); Strings::trim_all_and_remove_whitespace_strings(&installed_files_of_current_pgh); upgrade_to_slash_terminated_sorted_format(fs, &installed_files_of_current_pgh, listfile_path);