[vcpkg hash] Add sanity checking
MODIFY
include/vcpkg/commands.h
include/vcpkg/commands.h
@@ -132,9 +132,9 @@ namespace vcpkg::Commands
namespace Hash
{
std::string get_string_hash(const std::string& s, const std::string& hash_type);
- std::string get_file_hash(const fs::path& path, const std::string& hash_type);
+ std::string get_file_hash(const VcpkgPaths& paths, const fs::path& path, const std::string& hash_type);
- void perform_and_exit(const VcpkgCmdArguments& args);
+ void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
}
namespace Fetch

MODIFY
src/vcpkg/build.cpp
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(config.port_dir / "portfile.cmake", "SHA1")});
+ AbiEntry{"portfile", Commands::Hash::get_file_hash(paths, config.port_dir / "portfile.cmake", "SHA1")});
abi_tag_entries.emplace_back(
- AbiEntry{"control", Commands::Hash::get_file_hash(config.port_dir / "CONTROL", "SHA1")});
+ AbiEntry{"control", Commands::Hash::get_file_hash(paths, 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(abi_file_path, "SHA1"), abi_file_path};
+ return AbiTagAndFile{Commands::Hash::get_file_hash(paths, 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(triplet_file_path, "SHA1");
+ auto hash = Commands::Hash::get_file_hash(paths, triplet_file_path, "SHA1");
s_hash_cache.emplace(triplet_file_path, hash);
return hash;
}

MODIFY
src/vcpkg/commands.cpp
src/vcpkg/commands.cpp
@@ -42,6 +42,7 @@ 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;
@@ -52,7 +53,6 @@ namespace vcpkg::Commands
static std::vector<PackageNameAndFunction<CommandTypeC>> t = {
{"version", &Version::perform_and_exit},
{"contact", &Contact::perform_and_exit},
- {"hash", &Hash::perform_and_exit},
};
return t;
}

MODIFY
src/vcpkg/commands.fetch.cpp
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(download_path_part, "SHA512");
+ const std::string actual_hash = Hash::get_file_hash(paths, download_path_part, "SHA512");
Checks::check_exit(VCPKG_LINE_INFO,
sha512 == actual_hash,
"File does not have the expected hash:\n"

MODIFY
src/vcpkg/commands.hash.cpp
src/vcpkg/commands.hash.cpp
#include <vcpkg/commands.h>
#include <vcpkg/help.h>
+namespace vcpkg::Commands::Hash
+{
+ static void verify_has_only_allowed_chars(const std::string& s)
+ {
+ static const std::regex ALLOWED_CHARS{"^[a-zA-Z0-9-]*$"};
+ Checks::check_exit(VCPKG_LINE_INFO,
+ std::regex_match(s, ALLOWED_CHARS),
+ "Only alphanumeric chars and dashes are currently allowed. String was:\n"
+ " % s",
+ s);
+ }
+}
+
#if defined(_WIN32)
#include <bcrypt.h>
@@ -138,13 +151,16 @@ namespace vcpkg::Commands::Hash
};
}
- std::string get_file_hash(const fs::path& path, const std::string& hash_type)
+ std::string get_file_hash(const VcpkgPaths& paths, const fs::path& path, const std::string& hash_type)
{
+ Checks::check_exit(
+ VCPKG_LINE_INFO, paths.get_filesystem().exists(path), "File %s does not exist", path.u8string());
return BCryptHasher{hash_type}.hash_file(path);
}
std::string get_string_hash(const std::string& s, const std::string& hash_type)
{
+ verify_has_only_allowed_chars(s);
return BCryptHasher{hash_type}.hash_string(s);
}
}
@@ -174,9 +190,11 @@ namespace vcpkg::Commands::Hash
return Strings::trim(std::string{ec_data.output});
}
- std::string get_file_hash(const fs::path& path, const std::string& hash_type)
+ std::string get_file_hash(const VcpkgPaths& paths, const fs::path& path, const std::string& hash_type)
{
const std::string digest_size = get_digest_size(hash_type);
+ Checks::check_exit(
+ VCPKG_LINE_INFO, paths.get_filesystem().exists(path), "File %s does not exist", path.u8string());
const std::string cmd_line = Strings::format(
R"(shasum -a %s "%s" | awk '{ print $1 }')", digest_size, path.u8string());
return run_shasum_and_post_process(cmd_line);
@@ -185,12 +203,7 @@ namespace vcpkg::Commands::Hash
std::string get_string_hash(const std::string& s, const std::string& hash_type)
{
const std::string digest_size = get_digest_size(hash_type);
-
- static const std::regex ALLOWED_CHARS{"^[a-zA-Z0-9:]*$"};
-
- Checks::check_exit(VCPKG_LINE_INFO,
- std::regex_match(s, ALLOWED_CHARS),
- "Only alphanumeric chars and colons are currently allowed");
+ verify_has_only_allowed_chars(s);
const std::string cmd_line = Strings::format(
R"(echo -n "%s" | shasum -a %s | awk '{ print $1 }')", s, digest_size);
@@ -210,13 +223,13 @@ namespace vcpkg::Commands::Hash
nullptr,
};
- void perform_and_exit(const VcpkgCmdArguments& args)
+ void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
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(file_to_hash, algorithm);
+ const std::string hash = get_file_hash(paths, file_to_hash, algorithm);
System::println(hash);
Checks::exit_success(VCPKG_LINE_INFO);
}

[vcpkg hash] Add sanity checking