[Filesystem] Prefer .read_contents(path, linfo) overload for better error messages
MODIFY
include/vcpkg/base/files.h
include/vcpkg/base/files.h
@@ -172,6 +172,7 @@ namespace vcpkg::Files
/// <summary>Read text lines from a file</summary>
/// <remarks>Lines will have up to one trailing carriage-return character stripped (CRLF)</remarks>
virtual Expected<std::vector<std::string>> read_lines(const fs::path& file_path) const = 0;
+ std::vector<std::string> 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<fs::path> get_files_recursive(const fs::path& dir) const = 0;
virtual std::vector<fs::path> get_files_non_recursive(const fs::path& dir) const = 0;

MODIFY
src/vcpkg/base/files.cpp
src/vcpkg/base/files.cpp
@@ -455,6 +455,18 @@ namespace vcpkg::Files
}
}
+ std::vector<std::string> 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<std::string> output;
std::string line;
while (std::getline(file_stream, line))

MODIFY
src/vcpkg/build.cpp
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))

MODIFY
src/vcpkg/commands.upload-metrics.cpp
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);
}

MODIFY
src/vcpkg/export.cpp
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<fs::path> files;
for (auto&& suffix : lines)
{

MODIFY
src/vcpkg/statusparagraphs.cpp
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)));

MODIFY
src/vcpkg/tools.cpp
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"###(<tools[\s]+version="([^"]+)">)###"};
- 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,

MODIFY
src/vcpkg/vcpkglib.cpp
src/vcpkg/vcpkglib.cpp
@@ -213,8 +213,7 @@ namespace vcpkg
}
const fs::path listfile_path = paths.listfile_path(pgh->package);
- std::vector<std::string> installed_files_of_current_pgh =
- fs.read_lines(listfile_path).value_or_exit(VCPKG_LINE_INFO);
+ std::vector<std::string> 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);

[Filesystem] Prefer .read_contents(path, linfo) overload for better error messages (#16)