Skip to content

Commit 2393a00

Browse files
authored
Add additional logging and defenses to artifacts use. (#947)
* Always run "set" on its own line. Resolves microsoft/vcpkg#26172 * Try to add additional logging to downloads. Hopefully helps run down what's happening in microsoft/vcpkg#30051 * Remove the old 7z download hack now that we no longer get it from nuget.org. * Remove debug if test.
1 parent a820f36 commit 2393a00

File tree

4 files changed

+88
-62
lines changed

4 files changed

+88
-62
lines changed

src/vcpkg/base/downloads.cpp

+36-30
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <vcpkg/base/json.h>
66
#include <vcpkg/base/message_sinks.h>
77
#include <vcpkg/base/parse.h>
8+
#include <vcpkg/base/strings.h>
89
#include <vcpkg/base/system.debug.h>
910
#include <vcpkg/base/system.h>
1011
#include <vcpkg/base/system.process.h>
@@ -334,18 +335,6 @@ namespace vcpkg
334335
{
335336
std::string actual_hash =
336337
vcpkg::Hash::get_file_hash(fs, downloaded_path, Hash::Algorithm::Sha512).value_or_exit(VCPKG_LINE_INFO);
337-
338-
// <HACK to handle NuGet.org changing nupkg hashes.>
339-
// This is the NEW hash for 7zip
340-
if (actual_hash == "a9dfaaafd15d98a2ac83682867ec5766720acf6e99d40d1a00d480692752603bf3f3742623f0ea85647a92374df"
341-
"405f331afd6021c5cf36af43ee8db198129c0")
342-
{
343-
// This is the OLD hash for 7zip
344-
actual_hash = "8c75314102e68d2b2347d592f8e3eb05812e1ebb525decbac472231633753f1d4ca31c8e6881a36144a8da26b257"
345-
"1305b3ae3f4e2b85fc4a290aeda63d1a13b8";
346-
}
347-
// </HACK>
348-
349338
if (!Strings::case_insensitive_ascii_equals(sha512, actual_hash))
350339
{
351340
return msg::format_error(msgDownloadFailedHashMismatch,
@@ -366,22 +355,18 @@ namespace vcpkg
366355
try_verify_downloaded_file_hash(fs, url, downloaded_path, sha512).value_or_exit(VCPKG_LINE_INFO);
367356
}
368357

369-
static bool check_downloaded_file_hash(Filesystem& fs,
370-
const Optional<std::string>& hash,
371-
StringView sanitized_url,
372-
const Path& download_part_path,
373-
std::vector<LocalizedString>& errors)
358+
static ExpectedL<Unit> check_downloaded_file_hash(Filesystem& fs,
359+
const Optional<std::string>& hash,
360+
StringView sanitized_url,
361+
const Path& download_part_path)
374362
{
375363
if (auto p = hash.get())
376364
{
377-
auto maybe_success = try_verify_downloaded_file_hash(fs, sanitized_url, download_part_path, *p);
378-
if (!maybe_success.has_value())
379-
{
380-
errors.push_back(std::move(maybe_success).error());
381-
return false;
382-
}
365+
return try_verify_downloaded_file_hash(fs, sanitized_url, download_part_path, *p);
383366
}
384-
return true;
367+
368+
Debug::println("Skipping hash check because none was specified.");
369+
return Unit{};
385370
}
386371

387372
static void url_heads_inner(View<std::string> urls,
@@ -444,7 +429,11 @@ namespace vcpkg
444429
{
445430
url_heads_inner({urls.data() + i, batch_size}, headers, &ret, secrets);
446431
}
447-
if (i != urls.size()) url_heads_inner({urls.begin() + i, urls.end()}, headers, &ret, secrets);
432+
433+
if (i != urls.size())
434+
{
435+
url_heads_inner({urls.begin() + i, urls.end()}, headers, &ret, secrets);
436+
}
448437

449438
return ret;
450439
}
@@ -513,7 +502,11 @@ namespace vcpkg
513502
{
514503
download_files_inner(fs, {url_pairs.data() + i, batch_size}, headers, &ret);
515504
}
516-
if (i != url_pairs.size()) download_files_inner(fs, {url_pairs.begin() + i, url_pairs.end()}, headers, &ret);
505+
506+
if (i != url_pairs.size())
507+
{
508+
download_files_inner(fs, {url_pairs.begin() + i, url_pairs.end()}, headers, &ret);
509+
}
517510

518511
Checks::msg_check_exit(VCPKG_LINE_INFO,
519512
ret.size() == url_pairs.size(),
@@ -561,6 +554,7 @@ namespace vcpkg
561554
{
562555
cmd.string_arg("-H").string_arg(header);
563556
}
557+
564558
cmd.string_arg("-w").string_arg("\\n" + guid_marker.to_string() + "%{http_code}");
565559
cmd.string_arg(url);
566560
cmd.string_arg("-T").string_arg(file);
@@ -733,11 +727,16 @@ namespace vcpkg
733727
{
734728
if (download_winhttp(fs, download_path_part_path, split_uri, url, secrets, errors, progress_sink))
735729
{
736-
if (check_downloaded_file_hash(fs, sha512, url, download_path_part_path, errors))
730+
auto maybe_hash_check = check_downloaded_file_hash(fs, sha512, url, download_path_part_path);
731+
if (maybe_hash_check.has_value())
737732
{
738733
fs.rename(download_path_part_path, download_path, VCPKG_LINE_INFO);
739734
return true;
740735
}
736+
else
737+
{
738+
errors.push_back(std::move(maybe_hash_check).error());
739+
}
741740
}
742741
return false;
743742
}
@@ -758,7 +757,7 @@ namespace vcpkg
758757
}
759758

760759
std::string non_progress_lines;
761-
const auto maybe_exit_code = cmd_execute_and_stream_lines(
760+
auto maybe_exit_code = cmd_execute_and_stream_lines(
762761
cmd,
763762
[&](StringView line) {
764763
const auto maybe_parsed = try_parse_curl_progress_data(line);
@@ -782,15 +781,22 @@ namespace vcpkg
782781
if (*exit_code != 0)
783782
{
784783
errors.push_back(
785-
msg::format_error(msgDownloadFailedCurl, msg::url = sanitized_url, msg::exit_code = *exit_code));
784+
msg::format_error(msgDownloadFailedCurl, msg::url = sanitized_url, msg::exit_code = *exit_code)
785+
.append_raw('\n')
786+
.append_raw(Strings::join("\n", non_progress_lines)));
786787
return false;
787788
}
788789

789-
if (check_downloaded_file_hash(fs, sha512, sanitized_url, download_path_part_path, errors))
790+
auto maybe_hash_check = check_downloaded_file_hash(fs, sha512, sanitized_url, download_path_part_path);
791+
if (maybe_hash_check.has_value())
790792
{
791793
fs.rename(download_path_part_path, download_path, VCPKG_LINE_INFO);
792794
return true;
793795
}
796+
else
797+
{
798+
errors.push_back(std::move(maybe_hash_check).error());
799+
}
794800
}
795801
else
796802
{

src/vcpkg/base/hash.cpp

+31-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include <vcpkg/base/checks.h>
22
#include <vcpkg/base/expected.h>
3+
#include <vcpkg/base/format.h>
34
#include <vcpkg/base/hash.h>
45
#include <vcpkg/base/strings.h>
6+
#include <vcpkg/base/system.debug.h>
57
#include <vcpkg/base/system.process.h>
68
#include <vcpkg/base/uint128.h>
79
#include <vcpkg/base/util.h>
@@ -516,8 +518,8 @@ namespace vcpkg::Hash
516518
#endif
517519
}
518520

519-
template<class F>
520-
static std::string do_hash(Algorithm algo, const F& f)
521+
template<class ReturnType, class F>
522+
static ReturnType do_hash(Algorithm algo, const F& f)
521523
{
522524
#if defined(_WIN32)
523525
auto hasher = BCryptHasher(algo);
@@ -542,7 +544,7 @@ namespace vcpkg::Hash
542544

543545
std::string get_bytes_hash(const void* first, const void* last, Algorithm algo)
544546
{
545-
return do_hash(algo, [first, last](Hasher& hasher) {
547+
return do_hash<std::string>(algo, [first, last](Hasher& hasher) {
546548
hasher.add_bytes(first, last);
547549
return hasher.get_hash();
548550
});
@@ -557,36 +559,37 @@ namespace vcpkg::Hash
557559

558560
ExpectedL<std::string> get_file_hash(const Filesystem& fs, const Path& path, Algorithm algo)
559561
{
562+
Debug::println("Trying to hash ", path);
560563
std::error_code ec;
561564
auto file = fs.open_for_read(path, ec);
562-
if (!ec)
565+
if (ec)
563566
{
564-
auto result = do_hash(algo, [&file, &ec](Hasher& hasher) {
565-
constexpr std::size_t buffer_size = 1024 * 32;
566-
char buffer[buffer_size];
567-
do
568-
{
569-
const auto this_read = file.read(buffer, 1, buffer_size);
570-
if (this_read != 0)
571-
{
572-
hasher.add_bytes(buffer, buffer + this_read);
573-
}
574-
else if ((ec = file.error()))
575-
{
576-
return std::string();
577-
}
578-
} while (!file.eof());
579-
return hasher.get_hash();
580-
});
567+
return msg::format(msg::msgErrorMessage)
568+
.append(msgHashFileFailureToRead, msg::path = path)
569+
.append_raw(ec.message());
570+
}
581571

582-
if (!ec)
572+
return do_hash<ExpectedL<std::string>>(algo, [&](Hasher& hasher) -> ExpectedL<std::string> {
573+
constexpr std::size_t buffer_size = 1024 * 32;
574+
char buffer[buffer_size];
575+
do
583576
{
584-
return std::move(result);
585-
}
586-
}
577+
const auto this_read = file.read(buffer, 1, buffer_size);
578+
if (this_read != 0)
579+
{
580+
hasher.add_bytes(buffer, buffer + this_read);
581+
}
582+
else if ((ec = file.error()))
583+
{
584+
return msg::format(msg::msgErrorMessage)
585+
.append(msgHashFileFailureToRead, msg::path = path)
586+
.append_raw(ec.message());
587+
}
588+
} while (!file.eof());
587589

588-
return msg::format(msg::msgErrorMessage)
589-
.append(msgHashFileFailureToRead, msg::path = path)
590-
.append_raw(ec.message());
590+
auto result_hash = hasher.get_hash();
591+
Debug::print(fmt::format("{} has hash {}\n", path, result_hash));
592+
return result_hash;
593+
});
591594
}
592595
}

src/vcpkg/configure-environment.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ namespace
3030
const Path& ce_base_path)
3131
{
3232
auto& fs = paths.get_filesystem();
33+
if (!fs.is_regular_file(ce_tarball))
34+
{
35+
Debug::println("Download succeeded but file isn't present?");
36+
Checks::msg_exit_with_error(VCPKG_LINE_INFO, msgFailedToProvisionCe);
37+
}
38+
3339
fs.remove_all(ce_base_path, VCPKG_LINE_INFO);
3440
fs.create_directories(ce_base_path, VCPKG_LINE_INFO);
3541
Path node_root = node_path.parent_path();

vcpkg-init/vcpkg-init.ps1

+15-4
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,25 @@ return
108108
IF EXIST $null DEL $null
109109
110110
:: Figure out where VCPKG_ROOT is
111-
IF EXIST "%~dp0\.vcpkg-root" SET VCPKG_ROOT=%~dp0
112-
IF "%VCPKG_ROOT:~-1%"=="\" SET VCPKG_ROOT=%VCPKG_ROOT:~0,-1%
113-
IF "%VCPKG_ROOT%"=="" SET VCPKG_ROOT=%USERPROFILE%\.vcpkg
111+
IF EXIST "%~dp0.vcpkg-root" (
112+
SET VCPKG_ROOT=%~dp0
113+
)
114+
115+
IF "%VCPKG_ROOT:~-1%"=="\" (
116+
SET VCPKG_ROOT=%VCPKG_ROOT:0,-1%
117+
)
118+
119+
IF "%VCPKG_ROOT%"=="" (
120+
SET VCPKG_ROOT=%USERPROFILE%\.vcpkg
121+
)
114122
115123
:: Call powershell which may or may not invoke bootstrap if there's a version mismatch
116124
SET Z_POWERSHELL_EXE=
117125
FOR %%i IN (pwsh.exe powershell.exe) DO (
118-
IF EXIST "%%~$PATH:i" SET Z_POWERSHELL_EXE=%%~$PATH:i & GOTO :gotpwsh
126+
IF EXIST "%%~$PATH:i" (
127+
SET Z_POWERSHELL_EXE=%%~$PATH:i
128+
GOTO :gotpwsh
129+
)
119130
)
120131
121132
:gotpwsh

0 commit comments

Comments
 (0)