Merge topic 'cmake-E-sha'

c4de0a25 Add sha1sum, sha224sum, sha256sum, sha384sum and sha512sum to command mode
c4647d84 Change ComputeFileMD5 to ComputeFileHash
501a4fee Add some unit tests for md5sum

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !1021
This commit is contained in:
Brad King 2017-07-18 15:12:36 +00:00 committed by Kitware Robot
commit 407c7415f4
48 changed files with 175 additions and 40 deletions

View File

@ -262,6 +262,36 @@ Available commands are:
351abe79cd3800b38cdfb25d45015a15 file1.txt
052f86c15bbde68af55c7f7b340ab639 file2.txt
``sha1sum <file>...``
Create SHA1 checksum of files in ``sha1sum`` compatible format::
4bb7932a29e6f73c97bb9272f2bdc393122f86e0 file1.txt
1df4c8f318665f9a5f2ed38f55adadb7ef9f559c file2.txt
``sha224sum <file>...``
Create SHA224 checksum of files in ``sha224sum`` compatible format::
b9b9346bc8437bbda630b0b7ddfc5ea9ca157546dbbf4c613192f930 file1.txt
6dfbe55f4d2edc5fe5c9197bca51ceaaf824e48eba0cc453088aee24 file2.txt
``sha256sum <file>...``
Create SHA256 checksum of files in ``sha256sum`` compatible format::
76713b23615d31680afeb0e9efe94d47d3d4229191198bb46d7485f9cb191acc file1.txt
15b682ead6c12dedb1baf91231e1e89cfc7974b3787c1e2e01b986bffadae0ea file2.txt
``sha384sum <file>...``
Create SHA384 checksum of files in ``sha384sum`` compatible format::
acc049fedc091a22f5f2ce39a43b9057fd93c910e9afd76a6411a28a8f2b8a12c73d7129e292f94fc0329c309df49434 file1.txt
668ddeb108710d271ee21c0f3acbd6a7517e2b78f9181c6a2ff3b8943af92b0195dcb7cce48aa3e17893173c0a39e23d file2.txt
``sha512sum <file>...``
Create SHA512 checksum of files in ``sha512sum`` compatible format::
2a78d7a6c5328cfb1467c63beac8ff21794213901eaadafd48e7800289afbc08e5fb3e86aa31116c945ee3d7bf2a6194489ec6101051083d1108defc8e1dba89 file1.txt
7a0b54896fe5e70cca6dd643ad6f672614b189bf26f8153061c4d219474b05dad08c4e729af9f4b009f1a1a280cb625454bf587c690f4617c27e3aebdf3b7a2d file2.txt
``remove [-f] <file>...``
Remove the file(s). If any of the listed files already do not
exist, the command returns a non-zero exit code, but no message

View File

@ -0,0 +1,5 @@
cmake-command-mode-shasum
-------------------------
* Added sha1sum, sha224sum, sha256sum, sha384sum and sha512sum
as an equivalent to existing md5sum to cmake command mode.

View File

@ -6,6 +6,7 @@
#include "cmCPackComponentGroup.h"
#include "cmCPackGenerator.h"
#include "cmCPackLog.h"
#include "cmCryptoHash.h"
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
#include "cm_sys_stat.h"
@ -527,15 +528,13 @@ int cmCPackDebGenerator::createDeb()
continue;
}
char md5sum[33];
if (!cmSystemTools::ComputeFileMD5(*fileIt, md5sum)) {
std::string output =
cmSystemTools::ComputeFileHash(*fileIt, cmCryptoHash::AlgoMD5);
if (output.empty()) {
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem computing the md5 of "
<< *fileIt << std::endl);
}
md5sum[32] = 0;
std::string output(md5sum);
output += " " + *fileIt + "\n";
// debian md5sums entries are like this:
// 014f3604694729f3bf19263bac599765 usr/bin/ccmake

View File

@ -13,6 +13,7 @@
#include "cmCTest.h"
#include "cmCTestCurl.h"
#include "cmCTestScriptHandler.h"
#include "cmCryptoHash.h"
#include "cmCurl.h"
#include "cmGeneratedFileStream.h"
#include "cmProcessOutput.h"
@ -428,10 +429,8 @@ bool cmCTestSubmitHandler::SubmitUsingHTTP(const std::string& localprefix,
if (cmSystemTools::IsOn(this->GetOption("InternalTest"))) {
upload_as += "bad_md5sum";
} else {
char md5[33];
cmSystemTools::ComputeFileMD5(local_file, md5);
md5[32] = 0;
upload_as += md5;
upload_as +=
cmSystemTools::ComputeFileHash(local_file, cmCryptoHash::AlgoMD5);
}
if (!cmSystemTools::FileExists(local_file.c_str())) {
@ -1058,9 +1057,8 @@ int cmCTestSubmitHandler::HandleCDashUploadFile(std::string const& file,
}
}
char md5sum[33];
md5sum[32] = 0;
cmSystemTools::ComputeFileMD5(file, md5sum);
std::string md5sum =
cmSystemTools::ComputeFileHash(file, cmCryptoHash::AlgoMD5);
// 1. request the buildid and check to see if the file
// has already been uploaded
// TODO I added support for subproject. You would need to add

View File

@ -934,19 +934,17 @@ bool cmSystemTools::RenameFile(const char* oldname, const char* newname)
#endif
}
bool cmSystemTools::ComputeFileMD5(const std::string& source, char* md5out)
std::string cmSystemTools::ComputeFileHash(const std::string& source,
cmCryptoHash::Algo algo)
{
#if defined(CMAKE_BUILD_WITH_CMAKE)
cmCryptoHash md5(cmCryptoHash::AlgoMD5);
std::string const str = md5.HashFile(source);
strncpy(md5out, str.c_str(), 32);
return !str.empty();
cmCryptoHash hash(algo);
return hash.HashFile(source);
#else
(void)source;
(void)md5out;
cmSystemTools::Message("md5sum not supported in bootstrapping mode",
cmSystemTools::Message("hashsum not supported in bootstrapping mode",
"Error");
return false;
return std::string();
#endif
}

View File

@ -5,6 +5,7 @@
#include "cmConfigure.h"
#include "cmCryptoHash.h"
#include "cmProcessOutput.h"
#include "cmsys/Process.h"
#include "cmsys/SystemTools.hxx" // IWYU pragma: export
@ -179,8 +180,9 @@ public:
if possible). */
static bool RenameFile(const char* oldname, const char* newname);
///! Compute the md5sum of a file
static bool ComputeFileMD5(const std::string& source, char* md5out);
///! Compute the hash of a file
static std::string ComputeFileHash(const std::string& source,
cmCryptoHash::Algo algo);
/** Compute the md5sum of a string. */
static std::string ComputeStringMD5(const std::string& input);

View File

@ -88,6 +88,11 @@ void CMakeCommandUsage(const char* program)
<< " environment - display the current environment\n"
<< " make_directory <dir>... - create parent and <dir> directories\n"
<< " md5sum <file>... - create MD5 checksum of files\n"
<< " sha1sum <file>... - create SHA1 checksum of files\n"
<< " sha224sum <file>... - create SHA224 checksum of files\n"
<< " sha256sum <file>... - create SHA256 checksum of files\n"
<< " sha384sum <file>... - create SHA384 checksum of files\n"
<< " sha512sum <file>... - create SHA512 checksum of files\n"
<< " remove [-f] <file>... - remove the file(s), use -f to force "
"it\n"
<< " remove_directory dir - remove a directory and its contents\n"
@ -641,24 +646,28 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
// Command to calculate the md5sum of a file
if (args[1] == "md5sum" && args.size() >= 3) {
char md5out[32];
int retval = 0;
for (std::string::size_type cc = 2; cc < args.size(); cc++) {
const char* filename = args[cc].c_str();
// Cannot compute md5sum of a directory
if (cmSystemTools::FileIsDirectory(filename)) {
std::cerr << "Error: " << filename << " is a directory" << std::endl;
retval++;
} else if (!cmSystemTools::ComputeFileMD5(filename, md5out)) {
// To mimic md5sum behavior in a shell:
std::cerr << filename << ": No such file or directory" << std::endl;
retval++;
} else {
std::cout << std::string(md5out, 32) << " " << filename
<< std::endl;
}
}
return retval;
return HashSumFile(args, cmCryptoHash::AlgoMD5);
}
// Command to calculate the sha1sum of a file
if (args[1] == "sha1sum" && args.size() >= 3) {
return HashSumFile(args, cmCryptoHash::AlgoSHA1);
}
if (args[1] == "sha224sum" && args.size() >= 3) {
return HashSumFile(args, cmCryptoHash::AlgoSHA224);
}
if (args[1] == "sha256sum" && args.size() >= 3) {
return HashSumFile(args, cmCryptoHash::AlgoSHA256);
}
if (args[1] == "sha384sum" && args.size() >= 3) {
return HashSumFile(args, cmCryptoHash::AlgoSHA384);
}
if (args[1] == "sha512sum" && args.size() >= 3) {
return HashSumFile(args, cmCryptoHash::AlgoSHA512);
}
// Command to change directory and run a program.
@ -1074,6 +1083,33 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
return 1;
}
int cmcmd::HashSumFile(std::vector<std::string>& args, cmCryptoHash::Algo algo)
{
if (args.size() < 3) {
return -1;
}
int retval = 0;
for (std::string::size_type cc = 2; cc < args.size(); cc++) {
const char* filename = args[cc].c_str();
// Cannot compute sum of a directory
if (cmSystemTools::FileIsDirectory(filename)) {
std::cerr << "Error: " << filename << " is a directory" << std::endl;
retval++;
} else {
std::string value = cmSystemTools::ComputeFileHash(filename, algo);
if (value.empty()) {
// To mimic "md5sum/shasum" behavior in a shell:
std::cerr << filename << ": No such file or directory" << std::endl;
retval++;
} else {
std::cout << value << " " << filename << std::endl;
}
}
}
return retval;
}
int cmcmd::SymlinkLibrary(std::vector<std::string>& args)
{
int result = 0;

View File

@ -4,6 +4,7 @@
#define cmcmd_h
#include "cmConfigure.h" // IWYU pragma: keep
#include "cmCryptoHash.h"
#include <string>
#include <vector>
@ -18,6 +19,8 @@ public:
static int ExecuteCMakeCommand(std::vector<std::string>&);
protected:
static int HashSumFile(std::vector<std::string>& args,
cmCryptoHash::Algo algo);
static int SymlinkLibrary(std::vector<std::string>& args);
static int SymlinkExecutable(std::vector<std::string>& args);
static bool SymlinkInternal(std::string const& file,

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
Error: . is a directory

View File

@ -0,0 +1 @@
2

View File

@ -0,0 +1,2 @@
Error: . is a directory
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
275876e34cf609db118f3d84b799a790 ../dummy

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
275876e34cf609db118f3d84b799a790 ../dummy

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
Error: . is a directory

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
829c3804401b0727f70f73d4415e162400cbe57b ../dummy

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
Error: . is a directory

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
37d32c6dbabed711cb1d4620b64090fef0ef63ab16a4a51d668259e6 ../dummy

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
Error: . is a directory

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259 ../dummy

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
Error: . is a directory

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
43c1835ceba2e29596f05e3859d4fe2b6d124a181ed670f68e914bd3ed251b02b4be609608a13f23ec3d98da6c4eb8cd ../dummy

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
Error: . is a directory

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
nonexisting: No such file or directory

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1 @@
1692526aab84461a8aebcefddcba2b33fb5897ab180c53e8b345ae125484d0aaa35baf60487050be21ed8909a48eace93851bf139087ce1f7a87d97b6120a651 ../dummy

View File

@ -172,6 +172,30 @@ run_cmake_command(E_env-bad-arg1 ${CMAKE_COMMAND} -E env -bad-arg1)
run_cmake_command(E_env-set ${CMAKE_COMMAND} -E env TEST_ENV=1 ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/E_env-set.cmake)
run_cmake_command(E_env-unset ${CMAKE_COMMAND} -E env TEST_ENV=1 ${CMAKE_COMMAND} -E env --unset=TEST_ENV ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/E_env-unset.cmake)
run_cmake_command(E_md5sum-dir ${CMAKE_COMMAND} -E md5sum .)
run_cmake_command(E_sha1sum-dir ${CMAKE_COMMAND} -E sha1sum .)
run_cmake_command(E_sha224sum-dir ${CMAKE_COMMAND} -E sha224sum .)
run_cmake_command(E_sha256sum-dir ${CMAKE_COMMAND} -E sha256sum .)
run_cmake_command(E_sha384sum-dir ${CMAKE_COMMAND} -E sha384sum .)
run_cmake_command(E_sha512sum-dir ${CMAKE_COMMAND} -E sha512sum .)
run_cmake_command(E_md5sum-no-file ${CMAKE_COMMAND} -E md5sum nonexisting)
run_cmake_command(E_sha1sum-no-file ${CMAKE_COMMAND} -E sha1sum nonexisting)
run_cmake_command(E_sha224sum-no-file ${CMAKE_COMMAND} -E sha224sum nonexisting)
run_cmake_command(E_sha256sum-no-file ${CMAKE_COMMAND} -E sha256sum nonexisting)
run_cmake_command(E_sha384sum-no-file ${CMAKE_COMMAND} -E sha384sum nonexisting)
run_cmake_command(E_sha512sum-no-file ${CMAKE_COMMAND} -E sha512sum nonexisting)
file(WRITE "${RunCMake_BINARY_DIR}/dummy" "dummy")
run_cmake_command(E_md5sum ${CMAKE_COMMAND} -E md5sum ../dummy)
run_cmake_command(E_md5sum-mixed ${CMAKE_COMMAND} -E md5sum . ../dummy nonexisting)
run_cmake_command(E_sha1sum ${CMAKE_COMMAND} -E sha1sum ../dummy)
run_cmake_command(E_sha224sum ${CMAKE_COMMAND} -E sha224sum ../dummy)
run_cmake_command(E_sha256sum ${CMAKE_COMMAND} -E sha256sum ../dummy)
run_cmake_command(E_sha384sum ${CMAKE_COMMAND} -E sha384sum ../dummy)
run_cmake_command(E_sha512sum ${CMAKE_COMMAND} -E sha512sum ../dummy)
file(REMOVE "${RunCMake_BINARY_DIR}/dummy")
set(RunCMake_DEFAULT_stderr ".")
run_cmake_command(E_sleep-no-args ${CMAKE_COMMAND} -E sleep)
unset(RunCMake_DEFAULT_stderr)