Преглед изворни кода

Change ComputeFileMD5 to ComputeFileHash

* Use a parameter to select hash algorithm
* Return a std::string as result or an empty
  string if it fails
* Avoids unnecessary copy of hash value
André Klitzing пре 8 година
родитељ
комит
c4647d8432

+ 4 - 5
Source/CPack/cmCPackDebGenerator.cxx

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

+ 5 - 7
Source/CTest/cmCTestSubmitHandler.cxx

@@ -13,6 +13,7 @@
 #include "cmCTest.h"
 #include "cmCTest.h"
 #include "cmCTestCurl.h"
 #include "cmCTestCurl.h"
 #include "cmCTestScriptHandler.h"
 #include "cmCTestScriptHandler.h"
+#include "cmCryptoHash.h"
 #include "cmCurl.h"
 #include "cmCurl.h"
 #include "cmGeneratedFileStream.h"
 #include "cmGeneratedFileStream.h"
 #include "cmProcessOutput.h"
 #include "cmProcessOutput.h"
@@ -428,10 +429,8 @@ bool cmCTestSubmitHandler::SubmitUsingHTTP(const std::string& localprefix,
       if (cmSystemTools::IsOn(this->GetOption("InternalTest"))) {
       if (cmSystemTools::IsOn(this->GetOption("InternalTest"))) {
         upload_as += "bad_md5sum";
         upload_as += "bad_md5sum";
       } else {
       } 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())) {
       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
   // 1. request the buildid and check to see if the file
   //    has already been uploaded
   //    has already been uploaded
   // TODO I added support for subproject. You would need to add
   // TODO I added support for subproject. You would need to add

+ 6 - 8
Source/cmSystemTools.cxx

@@ -934,19 +934,17 @@ bool cmSystemTools::RenameFile(const char* oldname, const char* newname)
 #endif
 #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)
 #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
 #else
   (void)source;
   (void)source;
-  (void)md5out;
-  cmSystemTools::Message("md5sum not supported in bootstrapping mode",
+  cmSystemTools::Message("hashsum not supported in bootstrapping mode",
                          "Error");
                          "Error");
-  return false;
+  return std::string();
 #endif
 #endif
 }
 }
 
 

+ 4 - 2
Source/cmSystemTools.h

@@ -5,6 +5,7 @@
 
 
 #include "cmConfigure.h"
 #include "cmConfigure.h"
 
 
+#include "cmCryptoHash.h"
 #include "cmProcessOutput.h"
 #include "cmProcessOutput.h"
 #include "cmsys/Process.h"
 #include "cmsys/Process.h"
 #include "cmsys/SystemTools.hxx" // IWYU pragma: export
 #include "cmsys/SystemTools.hxx" // IWYU pragma: export
@@ -179,8 +180,9 @@ public:
       if possible).  */
       if possible).  */
   static bool RenameFile(const char* oldname, const char* newname);
   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.  */
   /** Compute the md5sum of a string.  */
   static std::string ComputeStringMD5(const std::string& input);
   static std::string ComputeStringMD5(const std::string& input);

+ 11 - 7
Source/cmcmd.cxx

@@ -3,6 +3,7 @@
 #include "cmcmd.h"
 #include "cmcmd.h"
 
 
 #include "cmAlgorithms.h"
 #include "cmAlgorithms.h"
+#include "cmCryptoHash.h"
 #include "cmGlobalGenerator.h"
 #include "cmGlobalGenerator.h"
 #include "cmLocalGenerator.h"
 #include "cmLocalGenerator.h"
 #include "cmMakefile.h"
 #include "cmMakefile.h"
@@ -641,7 +642,6 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
 
 
     // Command to calculate the md5sum of a file
     // Command to calculate the md5sum of a file
     if (args[1] == "md5sum" && args.size() >= 3) {
     if (args[1] == "md5sum" && args.size() >= 3) {
-      char md5out[32];
       int retval = 0;
       int retval = 0;
       for (std::string::size_type cc = 2; cc < args.size(); cc++) {
       for (std::string::size_type cc = 2; cc < args.size(); cc++) {
         const char* filename = args[cc].c_str();
         const char* filename = args[cc].c_str();
@@ -649,13 +649,17 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
         if (cmSystemTools::FileIsDirectory(filename)) {
         if (cmSystemTools::FileIsDirectory(filename)) {
           std::cerr << "Error: " << filename << " is a directory" << std::endl;
           std::cerr << "Error: " << filename << " is a directory" << std::endl;
           retval++;
           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 {
         } else {
-          std::cout << std::string(md5out, 32) << "  " << filename
-                    << std::endl;
+          std::string value =
+            cmSystemTools::ComputeFileHash(filename, cmCryptoHash::AlgoMD5);
+          if (value.empty()) {
+            // To mimic "md5sum" behavior in a shell:
+            std::cerr << filename << ": No such file or directory"
+                      << std::endl;
+            retval++;
+          } else {
+            std::cout << value << "  " << filename << std::endl;
+          }
         }
         }
       }
       }
       return retval;
       return retval;