From 782723959399a1a0725ac4921b1b7a7c9d10baf7 Mon Sep 17 00:00:00 2001 From: Nicole Mazzuca Date: Thu, 8 Aug 2019 16:14:59 -0700 Subject: =?UTF-8?q?(#7757)=20[vcpkg]=20Switch=20to=20internal=20hash=20alg?= =?UTF-8?q?orithms=20=F0=9F=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On non-Windows platforms, there is no standard way to get the hash of an item -- before this PR, what we did was check for the existence of a few common utility names (shasum, sha1, sha256, sha512), and then call that utility on a file we created containing the contents we wish to hash. This PR adds internal hashers for sha1, sha256, and sha512, and standardizes the interface to allow anyone to implement hashers in the future. These hashers are not extremely optimized, so it's likely that in the future we could get more optimized, but for now we just call out to BCryptHasher on Windows, since it's standard and easy to use (and about 2x faster for sha1 and sha256, and 1.5x faster for sha512). However, they are reasonably fast for being unoptimized. I attempted a few minor optimizations, which actually made the code slower! So as of right now, it's implemented as just a basic conversion of the code on Wikipedia to C++. I have tested these on the standard NIST test vectors (and those test vectors are located in vcpkg-test/hash.cpp). --- toolsrc/include/vcpkg/base/hash.h | 44 +++++++++++++++++++++++++++++++-- toolsrc/include/vcpkg/base/strings.h | 12 ++++++++- toolsrc/include/vcpkg/base/stringview.h | 5 ++-- 3 files changed, 56 insertions(+), 5 deletions(-) (limited to 'toolsrc/include') diff --git a/toolsrc/include/vcpkg/base/hash.h b/toolsrc/include/vcpkg/base/hash.h index 9e6f118c0..d62fd3921 100644 --- a/toolsrc/include/vcpkg/base/hash.h +++ b/toolsrc/include/vcpkg/base/hash.h @@ -6,6 +6,46 @@ namespace vcpkg::Hash { - std::string get_string_hash(const std::string& s, const std::string& hash_type); - std::string get_file_hash(const Files::Filesystem& fs, const fs::path& path, const std::string& hash_type); + enum class Algorithm + { + Sha1, + Sha256, + Sha512, + }; + + const char* to_string(Algorithm algo) noexcept; + Optional algorithm_from_string(StringView sv) noexcept; + + struct Hasher + { + virtual void add_bytes(const void* start, const void* end) noexcept = 0; + + // one may only call this once before calling `clear()` or the dtor + virtual std::string get_hash() noexcept = 0; + virtual void clear() noexcept = 0; + virtual ~Hasher() = default; + }; + + std::unique_ptr get_hasher_for(Algorithm algo) noexcept; + + std::string get_bytes_hash(const void* first, const void* last, Algorithm algo) noexcept; + std::string get_string_hash(StringView s, Algorithm algo) noexcept; + std::string get_file_hash(const Files::Filesystem& fs, + const fs::path& path, + Algorithm algo, + std::error_code& ec) noexcept; + inline std::string get_file_hash(LineInfo li, + const Files::Filesystem& fs, + const fs::path& path, + Algorithm algo) noexcept + { + std::error_code ec; + const auto result = get_file_hash(fs, path, algo, ec); + if (ec) + { + Checks::exit_with_message(li, "Failure to read file for hashing: %s", ec.message()); + } + + return result; + } } diff --git a/toolsrc/include/vcpkg/base/strings.h b/toolsrc/include/vcpkg/base/strings.h index 0f25607df..481e686d1 100644 --- a/toolsrc/include/vcpkg/base/strings.h +++ b/toolsrc/include/vcpkg/base/strings.h @@ -12,15 +12,25 @@ namespace vcpkg::Strings::details { template - auto to_printf_arg(const T& t) -> decltype(t.to_string()) + auto to_string(const T& t) -> decltype(t.to_string()) { return t.to_string(); } + // first looks up to_string on `T` using ADL; then, if that isn't found, + // uses the above definition which returns t.to_string() + template::value>> + auto to_printf_arg(const T& t) -> decltype(to_string(t)) + { + return to_string(t); + } + inline const char* to_printf_arg(const std::string& s) { return s.c_str(); } inline const char* to_printf_arg(const char* s) { return s; } + inline const wchar_t* to_printf_arg(const wchar_t* s) { return s; } + template::value>> T to_printf_arg(T s) { diff --git a/toolsrc/include/vcpkg/base/stringview.h b/toolsrc/include/vcpkg/base/stringview.h index fef5bef4e..8503f5f10 100644 --- a/toolsrc/include/vcpkg/base/stringview.h +++ b/toolsrc/include/vcpkg/base/stringview.h @@ -40,10 +40,11 @@ namespace vcpkg std::string to_string() const; void to_string(std::string& out) const; - bool operator==(StringView other) const; - private: const char* m_ptr = 0; size_t m_size = 0; }; + + bool operator==(StringView lhs, StringView rhs) noexcept; + bool operator!=(StringView lhs, StringView rhs) noexcept; } -- cgit v1.2.3 From 65d4bc146bf7c1c21989b680497b1f6f9a09c967 Mon Sep 17 00:00:00 2001 From: Victor Romero Date: Wed, 28 Aug 2019 11:47:17 -0700 Subject: [vcpkg install] Enable Download Mode (#7797) * [portfile functions] Override execute_process() to accept ALLOW_IN_DOWNLOAD_MODE option * [vcpkg install] Set VCPKG_DOWNLOAD_MODE when using --only-downloads option * [vcpkg_find_acquire_program] Allow in Download Mode * Don't stop when build fails for a package * Download sources for all packages in dependency graph * Improve output messages * Enable acquiring MSYS packages in download mode * Documentation * Update documentation * execute_process() always fails on Download Mode * Regenerate docs and fix formatting * Run clang-format * Use _execute_process on vcpkg_from_ helpers --- toolsrc/include/vcpkg/build.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'toolsrc/include') diff --git a/toolsrc/include/vcpkg/build.h b/toolsrc/include/vcpkg/build.h index 4f6397662..b535698dc 100644 --- a/toolsrc/include/vcpkg/build.h +++ b/toolsrc/include/vcpkg/build.h @@ -39,6 +39,12 @@ namespace vcpkg::Build YES }; + enum class OnlyDownloads + { + NO = 0, + YES + }; + enum class CleanBuildtrees { NO = 0, @@ -86,6 +92,7 @@ namespace vcpkg::Build { UseHeadVersion use_head_version; AllowDownloads allow_downloads; + OnlyDownloads only_downloads; CleanBuildtrees clean_buildtrees; CleanPackages clean_packages; CleanDownloads clean_downloads; @@ -103,6 +110,7 @@ namespace vcpkg::Build FILE_CONFLICTS, CASCADED_DUE_TO_MISSING_DEPENDENCIES, EXCLUDED, + DOWNLOADED }; static constexpr std::array BUILD_RESULT_VALUES = { -- cgit v1.2.3 From 4b404e8cfbdde4277733adaacc399fa4e1b57320 Mon Sep 17 00:00:00 2001 From: Victor Romero Date: Wed, 28 Aug 2019 11:59:30 -0700 Subject: Revert "[vcpkg install] Enable Download Mode (#7797)" (#7949) This reverts commit 65d4bc146bf7c1c21989b680497b1f6f9a09c967. --- toolsrc/include/vcpkg/build.h | 8 -------- 1 file changed, 8 deletions(-) (limited to 'toolsrc/include') diff --git a/toolsrc/include/vcpkg/build.h b/toolsrc/include/vcpkg/build.h index b535698dc..4f6397662 100644 --- a/toolsrc/include/vcpkg/build.h +++ b/toolsrc/include/vcpkg/build.h @@ -39,12 +39,6 @@ namespace vcpkg::Build YES }; - enum class OnlyDownloads - { - NO = 0, - YES - }; - enum class CleanBuildtrees { NO = 0, @@ -92,7 +86,6 @@ namespace vcpkg::Build { UseHeadVersion use_head_version; AllowDownloads allow_downloads; - OnlyDownloads only_downloads; CleanBuildtrees clean_buildtrees; CleanPackages clean_packages; CleanDownloads clean_downloads; @@ -110,7 +103,6 @@ namespace vcpkg::Build FILE_CONFLICTS, CASCADED_DUE_TO_MISSING_DEPENDENCIES, EXCLUDED, - DOWNLOADED }; static constexpr std::array BUILD_RESULT_VALUES = { -- cgit v1.2.3 From f5c732b40d43f062278f247036b773477823813b Mon Sep 17 00:00:00 2001 From: Victor Romero Date: Wed, 28 Aug 2019 13:49:29 -0700 Subject: Download Mode (#7950) * [portfile functions] Override execute_process() to accept ALLOW_IN_DOWNLOAD_MODE option * [vcpkg install] Set VCPKG_DOWNLOAD_MODE when using --only-downloads option * [vcpkg_find_acquire_program] Allow in Download Mode * Don't stop when build fails for a package * Download sources for all packages in dependency graph * Improve output messages * Enable acquiring MSYS packages in download mode * Documentation * Update documentation * execute_process() always fails on Download Mode * Regenerate docs and fix formatting * Run clang-format * Use _execute_process on vcpkg_from_ helpers * Fix calls to _execute_process() when not in Download Mode --- toolsrc/include/vcpkg/build.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'toolsrc/include') diff --git a/toolsrc/include/vcpkg/build.h b/toolsrc/include/vcpkg/build.h index 4f6397662..b535698dc 100644 --- a/toolsrc/include/vcpkg/build.h +++ b/toolsrc/include/vcpkg/build.h @@ -39,6 +39,12 @@ namespace vcpkg::Build YES }; + enum class OnlyDownloads + { + NO = 0, + YES + }; + enum class CleanBuildtrees { NO = 0, @@ -86,6 +92,7 @@ namespace vcpkg::Build { UseHeadVersion use_head_version; AllowDownloads allow_downloads; + OnlyDownloads only_downloads; CleanBuildtrees clean_buildtrees; CleanPackages clean_packages; CleanDownloads clean_downloads; @@ -103,6 +110,7 @@ namespace vcpkg::Build FILE_CONFLICTS, CASCADED_DUE_TO_MISSING_DEPENDENCIES, EXCLUDED, + DOWNLOADED }; static constexpr std::array BUILD_RESULT_VALUES = { -- cgit v1.2.3 From 84ba23ad3300a86e00841c5e26cbf753f6e89f3f Mon Sep 17 00:00:00 2001 From: Victor Romero Date: Fri, 6 Sep 2019 11:35:56 -0700 Subject: =?UTF-8?q?[x-history]=20Prints=20CONTROL=20version=20history=20of?= =?UTF-8?q?=20a=20port=20=F0=9F=91=BB=20(#7377)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [port-history] Print port CONTROL version history * Add commands.porthistory.cpp to VS project * Get most recent commit for each version * Apply clang-format * Fix output format * Rename command to x-history --- toolsrc/include/vcpkg/commands.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'toolsrc/include') diff --git a/toolsrc/include/vcpkg/commands.h b/toolsrc/include/vcpkg/commands.h index 8a502122e..e73077e1d 100644 --- a/toolsrc/include/vcpkg/commands.h +++ b/toolsrc/include/vcpkg/commands.h @@ -100,6 +100,11 @@ namespace vcpkg::Commands void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths); } + namespace PortHistory + { + void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths); + } + namespace Autocomplete { void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths); -- cgit v1.2.3