From 2b8bdeb0441c9fb83fc79bf10a761d1bf11cd287 Mon Sep 17 00:00:00 2001 From: sdcb Date: Thu, 20 Oct 2016 13:03:58 +0800 Subject: [PATCH 1/4] Add hash file support. --- toolsrc/include/vcpkg_Commands.h | 1 + toolsrc/src/commands_hash.cpp | 40 +++++++++++++++++++++++++++++ toolsrc/src/commands_other.cpp | 4 ++- toolsrc/vcpkg/commands_hash.cpp | 40 +++++++++++++++++++++++++++++ toolsrc/vcpkg/vcpkg.vcxproj | 1 + toolsrc/vcpkg/vcpkg.vcxproj.filters | 3 +++ 6 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 toolsrc/src/commands_hash.cpp create mode 100644 toolsrc/vcpkg/commands_hash.cpp diff --git a/toolsrc/include/vcpkg_Commands.h b/toolsrc/include/vcpkg_Commands.h index 9785198206..dc4699dc03 100644 --- a/toolsrc/include/vcpkg_Commands.h +++ b/toolsrc/include/vcpkg_Commands.h @@ -35,6 +35,7 @@ namespace vcpkg void version_command(const vcpkg_cmd_arguments& args); void contact_command(const vcpkg_cmd_arguments& args); + void hash_command(const vcpkg_cmd_arguments& args); using command_type_a = void(*)(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths, const triplet& default_target_triplet); using command_type_b = void(*)(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths); diff --git a/toolsrc/src/commands_hash.cpp b/toolsrc/src/commands_hash.cpp new file mode 100644 index 0000000000..672e24a24e --- /dev/null +++ b/toolsrc/src/commands_hash.cpp @@ -0,0 +1,40 @@ +#include "vcpkg_Commands.h" +#include "vcpkg_System.h" +#include "vcpkg.h" +#include +#include +#include + +namespace fs = std::tr2::sys; + +namespace vcpkg +{ + void file_hash_sha512(fs::path const & path, char const * hash) + { + auto cmd_line = Strings::format("Powershell -Command (Get-FileHash %s -Algorithm %s).Hash.ToLower()", + Strings::utf16_to_utf8(path.c_str()), + hash); + auto ec_data = System::cmd_execute_and_capture_output(Strings::utf8_to_utf16(cmd_line)); + Checks::check_exit(ec_data.exit_code == 0, "Running command:\n %s\n failed", cmd_line); + System::print(ec_data.output.c_str()); + } + + void hash_command(const vcpkg_cmd_arguments& args) + { + static const std::string example = Strings::format( + "The argument should be a file path\n%s", create_example_string("hash boost_1_62_0.tar.bz2")); + args.check_min_arg_count(1, example.c_str()); + args.check_max_arg_count(2, example.c_str()); + + if (args.command_arguments.size() == 1) + { + file_hash_sha512(args.command_arguments[0], "SHA512"); + } + if (args.command_arguments.size() == 2) + { + file_hash_sha512(args.command_arguments[0], args.command_arguments[1].c_str()); + } + + exit(EXIT_SUCCESS); + } +} diff --git a/toolsrc/src/commands_other.cpp b/toolsrc/src/commands_other.cpp index 07549a437d..53d6f25062 100644 --- a/toolsrc/src/commands_other.cpp +++ b/toolsrc/src/commands_other.cpp @@ -14,6 +14,7 @@ namespace vcpkg " vcpkg remove --purge Uninstall and delete a package. \n" " vcpkg list List installed packages\n" " vcpkg update Display list of packages for updating\n" + " vcpkg hash [alg] Hash a file by specific algorithm, default SHA512\n" "\n" "%s" // Integration help "\n" @@ -93,7 +94,8 @@ namespace vcpkg { static std::vector> t = { {"version", &version_command}, - {"contact", &contact_command} + {"contact", &contact_command}, + {"hash", &hash_command }, }; return t; } diff --git a/toolsrc/vcpkg/commands_hash.cpp b/toolsrc/vcpkg/commands_hash.cpp new file mode 100644 index 0000000000..e4e0e17475 --- /dev/null +++ b/toolsrc/vcpkg/commands_hash.cpp @@ -0,0 +1,40 @@ +#include "vcpkg_Commands.h" +#include "vcpkg_System.h" +#include "vcpkg.h" +#include +#include +#include + +namespace fs = std::tr2::sys; + +namespace vcpkg +{ + void file_hash_sha512(fs::path const & path, char const * hash) + { + auto cmd_line = Strings::format("Powershell -Command (Get-FileHash %s -Algorithm %s).Hash.ToLower()", + Strings::utf16_to_utf8(path.c_str()), + hash); + auto ec_data = System::cmd_execute_and_capture_output(Strings::utf8_to_utf16(cmd_line)); + Checks::check_exit(ec_data.exit_code == 0, "Running command:\n %s\n failed", cmd_line); + System::print(ec_data.output.c_str()); + } + + void hash_command(const vcpkg_cmd_arguments& args) + { + static const std::string example = Strings::format( + "The argument should be a file path\n%s", create_example_string("hash boost_1_62_0.tar.bz2 ")); + args.check_min_arg_count(1, example.c_str()); + args.check_max_arg_count(2, example.c_str()); + + if (args.command_arguments.size() == 1) + { + file_hash_sha512(args.command_arguments[0], "SHA512"); + } + if (args.command_arguments.size() == 2) + { + file_hash_sha512(args.command_arguments[0], args.command_arguments[1].c_str()); + } + + exit(EXIT_SUCCESS); + } +} diff --git a/toolsrc/vcpkg/vcpkg.vcxproj b/toolsrc/vcpkg/vcpkg.vcxproj index 82d3e21633..ec877d09ca 100644 --- a/toolsrc/vcpkg/vcpkg.vcxproj +++ b/toolsrc/vcpkg/vcpkg.vcxproj @@ -133,6 +133,7 @@ + diff --git a/toolsrc/vcpkg/vcpkg.vcxproj.filters b/toolsrc/vcpkg/vcpkg.vcxproj.filters index 96a11162fb..e46652d903 100644 --- a/toolsrc/vcpkg/vcpkg.vcxproj.filters +++ b/toolsrc/vcpkg/vcpkg.vcxproj.filters @@ -78,6 +78,9 @@ Source Files + + Source Files + From 337c96fc067ae5908d4cf5c8a188aa37588044ac Mon Sep 17 00:00:00 2001 From: flysha Date: Fri, 21 Oct 2016 07:44:00 +0800 Subject: [PATCH 2/4] Switch to using CertUtil to generate hash. --- toolsrc/src/commands_hash.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/toolsrc/src/commands_hash.cpp b/toolsrc/src/commands_hash.cpp index 672e24a24e..20e960d02f 100644 --- a/toolsrc/src/commands_hash.cpp +++ b/toolsrc/src/commands_hash.cpp @@ -9,14 +9,24 @@ namespace fs = std::tr2::sys; namespace vcpkg { - void file_hash_sha512(fs::path const & path, char const * hash) + void file_hash_sha512(fs::path const & path, std::wstring const & hashType) { - auto cmd_line = Strings::format("Powershell -Command (Get-FileHash %s -Algorithm %s).Hash.ToLower()", - Strings::utf16_to_utf8(path.c_str()), - hash); - auto ec_data = System::cmd_execute_and_capture_output(Strings::utf8_to_utf16(cmd_line)); - Checks::check_exit(ec_data.exit_code == 0, "Running command:\n %s\n failed", cmd_line); - System::print(ec_data.output.c_str()); + auto cmd_line = Strings::wformat(LR"(CertUtil.exe -hashfile "%s" %s)", + path.c_str(), hashType.c_str()); + auto ec_data = System::cmd_execute_and_capture_output(cmd_line); + Checks::check_exit(ec_data.exit_code == 0, "Running command:\n %s\n failed", Strings::utf16_to_utf8(cmd_line)); + + std::string const & output = ec_data.output; + + auto start = output.find_first_of("\r\n"); + Checks::check_exit(start != std::string::npos, "Unexpected output format from command: %s", Strings::utf16_to_utf8(cmd_line)); + + auto end = output.find_first_of("\r\n", start + 1); + Checks::check_exit(end != std::string::npos, "Unexpected output format from command: %s", Strings::utf16_to_utf8(cmd_line)); + + auto hash = output.substr(start, end - start); + hash.erase(std::remove_if(hash.begin(), hash.end(), isspace), hash.end()); + System::println(hash.c_str()); } void hash_command(const vcpkg_cmd_arguments& args) @@ -28,11 +38,11 @@ namespace vcpkg if (args.command_arguments.size() == 1) { - file_hash_sha512(args.command_arguments[0], "SHA512"); + file_hash_sha512(args.command_arguments[0], L"SHA512"); } if (args.command_arguments.size() == 2) { - file_hash_sha512(args.command_arguments[0], args.command_arguments[1].c_str()); + file_hash_sha512(args.command_arguments[0], Strings::utf8_to_utf16(args.command_arguments[1])); } exit(EXIT_SUCCESS); From c12c3d90bee810abaec479c49d889957b52151db Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 20 Oct 2016 18:05:52 -0700 Subject: [PATCH 3/4] src Formatting --- toolsrc/include/vcpkg_Commands.h | 2 +- toolsrc/src/commands_hash.cpp | 62 ++++++++++++++++---------------- toolsrc/src/commands_other.cpp | 6 ++-- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/toolsrc/include/vcpkg_Commands.h b/toolsrc/include/vcpkg_Commands.h index dc4699dc03..3cc48ac891 100644 --- a/toolsrc/include/vcpkg_Commands.h +++ b/toolsrc/include/vcpkg_Commands.h @@ -35,7 +35,7 @@ namespace vcpkg void version_command(const vcpkg_cmd_arguments& args); void contact_command(const vcpkg_cmd_arguments& args); - void hash_command(const vcpkg_cmd_arguments& args); + void hash_command(const vcpkg_cmd_arguments& args); using command_type_a = void(*)(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths, const triplet& default_target_triplet); using command_type_b = void(*)(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths); diff --git a/toolsrc/src/commands_hash.cpp b/toolsrc/src/commands_hash.cpp index 20e960d02f..07ce9d6e77 100644 --- a/toolsrc/src/commands_hash.cpp +++ b/toolsrc/src/commands_hash.cpp @@ -9,42 +9,42 @@ namespace fs = std::tr2::sys; namespace vcpkg { - void file_hash_sha512(fs::path const & path, std::wstring const & hashType) - { - auto cmd_line = Strings::wformat(LR"(CertUtil.exe -hashfile "%s" %s)", - path.c_str(), hashType.c_str()); - auto ec_data = System::cmd_execute_and_capture_output(cmd_line); - Checks::check_exit(ec_data.exit_code == 0, "Running command:\n %s\n failed", Strings::utf16_to_utf8(cmd_line)); + void file_hash_sha512(fs::path const& path, std::wstring const& hashType) + { + auto cmd_line = Strings::wformat(LR"(CertUtil.exe -hashfile "%s" %s)", + path.c_str(), hashType.c_str()); + auto ec_data = System::cmd_execute_and_capture_output(cmd_line); + Checks::check_exit(ec_data.exit_code == 0, "Running command:\n %s\n failed", Strings::utf16_to_utf8(cmd_line)); - std::string const & output = ec_data.output; + std::string const& output = ec_data.output; - auto start = output.find_first_of("\r\n"); - Checks::check_exit(start != std::string::npos, "Unexpected output format from command: %s", Strings::utf16_to_utf8(cmd_line)); + auto start = output.find_first_of("\r\n"); + Checks::check_exit(start != std::string::npos, "Unexpected output format from command: %s", Strings::utf16_to_utf8(cmd_line)); - auto end = output.find_first_of("\r\n", start + 1); - Checks::check_exit(end != std::string::npos, "Unexpected output format from command: %s", Strings::utf16_to_utf8(cmd_line)); + auto end = output.find_first_of("\r\n", start + 1); + Checks::check_exit(end != std::string::npos, "Unexpected output format from command: %s", Strings::utf16_to_utf8(cmd_line)); - auto hash = output.substr(start, end - start); - hash.erase(std::remove_if(hash.begin(), hash.end(), isspace), hash.end()); - System::println(hash.c_str()); - } + auto hash = output.substr(start, end - start); + hash.erase(std::remove_if(hash.begin(), hash.end(), isspace), hash.end()); + System::println(hash.c_str()); + } - void hash_command(const vcpkg_cmd_arguments& args) - { - static const std::string example = Strings::format( - "The argument should be a file path\n%s", create_example_string("hash boost_1_62_0.tar.bz2")); - args.check_min_arg_count(1, example.c_str()); - args.check_max_arg_count(2, example.c_str()); + void hash_command(const vcpkg_cmd_arguments& args) + { + static const std::string example = Strings::format( + "The argument should be a file path\n%s", create_example_string("hash boost_1_62_0.tar.bz2")); + args.check_min_arg_count(1, example.c_str()); + args.check_max_arg_count(2, example.c_str()); - if (args.command_arguments.size() == 1) - { - file_hash_sha512(args.command_arguments[0], L"SHA512"); - } - if (args.command_arguments.size() == 2) - { - file_hash_sha512(args.command_arguments[0], Strings::utf8_to_utf16(args.command_arguments[1])); - } + if (args.command_arguments.size() == 1) + { + file_hash_sha512(args.command_arguments[0], L"SHA512"); + } + if (args.command_arguments.size() == 2) + { + file_hash_sha512(args.command_arguments[0], Strings::utf8_to_utf16(args.command_arguments[1])); + } - exit(EXIT_SUCCESS); - } + exit(EXIT_SUCCESS); + } } diff --git a/toolsrc/src/commands_other.cpp b/toolsrc/src/commands_other.cpp index 53d6f25062..148673afe6 100644 --- a/toolsrc/src/commands_other.cpp +++ b/toolsrc/src/commands_other.cpp @@ -14,7 +14,7 @@ namespace vcpkg " vcpkg remove --purge Uninstall and delete a package. \n" " vcpkg list List installed packages\n" " vcpkg update Display list of packages for updating\n" - " vcpkg hash [alg] Hash a file by specific algorithm, default SHA512\n" + " vcpkg hash [alg] Hash a file by specific algorithm, default SHA512\n" "\n" "%s" // Integration help "\n" @@ -94,8 +94,8 @@ namespace vcpkg { static std::vector> t = { {"version", &version_command}, - {"contact", &contact_command}, - {"hash", &hash_command }, + {"contact", &contact_command}, + {"hash", &hash_command}, }; return t; } From e118c157198ee4218492cafb7ef3aaa1e3442c78 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 20 Oct 2016 18:06:26 -0700 Subject: [PATCH 4/4] Remove accidental copy of file --- toolsrc/vcpkg/commands_hash.cpp | 40 --------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 toolsrc/vcpkg/commands_hash.cpp diff --git a/toolsrc/vcpkg/commands_hash.cpp b/toolsrc/vcpkg/commands_hash.cpp deleted file mode 100644 index e4e0e17475..0000000000 --- a/toolsrc/vcpkg/commands_hash.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "vcpkg_Commands.h" -#include "vcpkg_System.h" -#include "vcpkg.h" -#include -#include -#include - -namespace fs = std::tr2::sys; - -namespace vcpkg -{ - void file_hash_sha512(fs::path const & path, char const * hash) - { - auto cmd_line = Strings::format("Powershell -Command (Get-FileHash %s -Algorithm %s).Hash.ToLower()", - Strings::utf16_to_utf8(path.c_str()), - hash); - auto ec_data = System::cmd_execute_and_capture_output(Strings::utf8_to_utf16(cmd_line)); - Checks::check_exit(ec_data.exit_code == 0, "Running command:\n %s\n failed", cmd_line); - System::print(ec_data.output.c_str()); - } - - void hash_command(const vcpkg_cmd_arguments& args) - { - static const std::string example = Strings::format( - "The argument should be a file path\n%s", create_example_string("hash boost_1_62_0.tar.bz2 ")); - args.check_min_arg_count(1, example.c_str()); - args.check_max_arg_count(2, example.c_str()); - - if (args.command_arguments.size() == 1) - { - file_hash_sha512(args.command_arguments[0], "SHA512"); - } - if (args.command_arguments.size() == 2) - { - file_hash_sha512(args.command_arguments[0], args.command_arguments[1].c_str()); - } - - exit(EXIT_SUCCESS); - } -}