From 870fa61e0132028f43c292b7fd7a7d7c6525a8d7 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 5 Apr 2018 04:29:43 -0700 Subject: [PATCH] [vcpkg hash] Don't use cmake for non-windows hashes. Use shasum instead --- toolsrc/include/vcpkg/commands.h | 4 +-- toolsrc/src/vcpkg/build.cpp | 8 +++--- toolsrc/src/vcpkg/commands.cpp | 8 ++++-- toolsrc/src/vcpkg/commands.fetch.cpp | 2 +- toolsrc/src/vcpkg/commands.hash.cpp | 43 ++++++++++++---------------- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/toolsrc/include/vcpkg/commands.h b/toolsrc/include/vcpkg/commands.h index f6f1de626..65e7fdd14 100644 --- a/toolsrc/include/vcpkg/commands.h +++ b/toolsrc/include/vcpkg/commands.h @@ -131,8 +131,8 @@ namespace vcpkg::Commands namespace Hash { - void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths); - std::string get_file_hash(const VcpkgPaths& paths, fs::path const& path, std::string const& hash_type); + void perform_and_exit(const VcpkgCmdArguments& args); + std::string get_file_hash(fs::path const& path, std::string const& hash_type); } namespace Fetch diff --git a/toolsrc/src/vcpkg/build.cpp b/toolsrc/src/vcpkg/build.cpp index b7055100b..16a85db52 100644 --- a/toolsrc/src/vcpkg/build.cpp +++ b/toolsrc/src/vcpkg/build.cpp @@ -457,9 +457,9 @@ namespace vcpkg::Build abi_tag_entries.insert(abi_tag_entries.end(), dependency_abis.begin(), dependency_abis.end()); abi_tag_entries.emplace_back( - AbiEntry{"portfile", Commands::Hash::get_file_hash(paths, config.port_dir / "portfile.cmake", "SHA1")}); + AbiEntry{"portfile", Commands::Hash::get_file_hash(config.port_dir / "portfile.cmake", "SHA1")}); abi_tag_entries.emplace_back( - AbiEntry{"control", Commands::Hash::get_file_hash(paths, config.port_dir / "CONTROL", "SHA1")}); + AbiEntry{"control", Commands::Hash::get_file_hash(config.port_dir / "CONTROL", "SHA1")}); abi_tag_entries.emplace_back(AbiEntry{"triplet", pre_build_info.triplet_abi_tag}); @@ -494,7 +494,7 @@ namespace vcpkg::Build auto abi_file_path = paths.buildtrees / name / (triplet.canonical_name() + ".vcpkg_abi_info.txt"); fs.write_contents(abi_file_path, full_abi_info); - return AbiTagAndFile{Commands::Hash::get_file_hash(paths, abi_file_path, "SHA1"), abi_file_path}; + return AbiTagAndFile{Commands::Hash::get_file_hash(abi_file_path, "SHA1"), abi_file_path}; } else { @@ -784,7 +784,7 @@ namespace vcpkg::Build { return it_hash->second; } - auto hash = Commands::Hash::get_file_hash(paths, triplet_file_path, "SHA1"); + auto hash = Commands::Hash::get_file_hash(triplet_file_path, "SHA1"); s_hash_cache.emplace(triplet_file_path, hash); return hash; } diff --git a/toolsrc/src/vcpkg/commands.cpp b/toolsrc/src/vcpkg/commands.cpp index c6b700aae..9041b37ff 100644 --- a/toolsrc/src/vcpkg/commands.cpp +++ b/toolsrc/src/vcpkg/commands.cpp @@ -42,7 +42,6 @@ namespace vcpkg::Commands {"cache", &Cache::perform_and_exit}, {"portsdiff", &PortsDiff::perform_and_exit}, {"autocomplete", &Autocomplete::perform_and_exit}, - {"hash", &Hash::perform_and_exit}, // {"fetch", &Fetch::perform_and_exit}, }; return t; @@ -50,8 +49,11 @@ namespace vcpkg::Commands Span> get_available_commands_type_c() { - static std::vector> t = {{"version", &Version::perform_and_exit}, - {"contact", &Contact::perform_and_exit}}; + static std::vector> t = { + {"version", &Version::perform_and_exit}, + {"contact", &Contact::perform_and_exit}, + {"hash", &Hash::perform_and_exit}, + }; return t; } } diff --git a/toolsrc/src/vcpkg/commands.fetch.cpp b/toolsrc/src/vcpkg/commands.fetch.cpp index e369f44d2..1ba043728 100644 --- a/toolsrc/src/vcpkg/commands.fetch.cpp +++ b/toolsrc/src/vcpkg/commands.fetch.cpp @@ -240,7 +240,7 @@ namespace vcpkg::Commands::Fetch R"(curl -L '%s' --create-dirs --output '%s')", url, download_path_part)); Checks::check_exit(VCPKG_LINE_INFO, code == 0, "Could not download %s", url); - const std::string actual_hash = Hash::get_file_hash(paths, download_path_part, "SHA512"); + const std::string actual_hash = Hash::get_file_hash(download_path_part, "SHA512"); Checks::check_exit(VCPKG_LINE_INFO, sha512 == actual_hash, "File does not have the expected hash:\n" diff --git a/toolsrc/src/vcpkg/commands.hash.cpp b/toolsrc/src/vcpkg/commands.hash.cpp index 517970b2c..087f3e682 100644 --- a/toolsrc/src/vcpkg/commands.hash.cpp +++ b/toolsrc/src/vcpkg/commands.hash.cpp @@ -60,7 +60,7 @@ namespace vcpkg::Commands::Hash }; } - std::string get_file_hash(const VcpkgPaths&, const fs::path& path, const std::string& hash_type) + std::string get_file_hash(const fs::path& path, const std::string& hash_type) { BCryptAlgorithmHandle algorithm_handle; @@ -108,30 +108,25 @@ namespace vcpkg::Commands::Hash #else namespace vcpkg::Commands::Hash { - std::string get_file_hash(const VcpkgPaths& paths, const fs::path& path, const std::string& hash_type) + std::string get_file_hash(const fs::path& path, const std::string& hash_type) { + if (!Strings::case_insensitive_ascii_starts_with(hash_type, "SHA")) + { + Checks::exit_with_message( + VCPKG_LINE_INFO, "shasum only supports SHA hashes, but %s was provided", hash_type); + } + + const std::string digest_size = hash_type.substr(3, hash_type.length() - 3); + const std::string cmd_line = Strings::format( - R"("%s" -E %ssum "%s")", - paths.get_tool_exe(Tools::CMAKE).u8string(), - Strings::ascii_to_lowercase(hash_type), - path.u8string()); - + R"(shasum -a %s "%s" | awk '{ print $1 }')", digest_size, path.u8string()); const auto ec_data = System::cmd_execute_and_capture_output(cmd_line); - Checks::check_exit(VCPKG_LINE_INFO, ec_data.exit_code == 0, "Running command:\n %s\n failed", cmd_line); - - std::string const& output = ec_data.output; - - const auto start = output.find_first_of(' '); - Checks::check_exit( - VCPKG_LINE_INFO, start != std::string::npos, "Unexpected output format from command: %s", cmd_line); - - const auto end = output.find_first_of("\r\n", start + 1); - Checks::check_exit( - VCPKG_LINE_INFO, end != std::string::npos, "Unexpected output format from command: %s", cmd_line); - - auto hash = output.substr(0, start); - Util::erase_remove_if(hash, isspace); - return hash; + Checks::check_exit(VCPKG_LINE_INFO, + ec_data.exit_code == 0, + "Failed to run:\n" + " %s", + cmd_line); + return Strings::trim(std::string{ec_data.output}); } } #endif @@ -147,13 +142,13 @@ namespace vcpkg::Commands::Hash nullptr, }; - void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths) + void perform_and_exit(const VcpkgCmdArguments& args) { Util::unused(args.parse_arguments(COMMAND_STRUCTURE)); const fs::path file_to_hash = args.command_arguments[0]; const std::string algorithm = args.command_arguments.size() == 2 ? args.command_arguments[1] : "SHA512"; - const std::string hash = get_file_hash(paths, file_to_hash, algorithm); + const std::string hash = get_file_hash(file_to_hash, algorithm); System::println(hash); Checks::exit_success(VCPKG_LINE_INFO); }