diff options
author | Alexander Neundorf <neundorf@kde.org> | 2007-07-16 14:54:32 (GMT) |
---|---|---|
committer | Alexander Neundorf <neundorf@kde.org> | 2007-07-16 14:54:32 (GMT) |
commit | 5bb94ce16635954f460edcacd5422bc8d53fb8c1 (patch) | |
tree | 2632f80554b159a3cd195d4afdc2be1bdc856c17 | |
parent | 56838c115ed813711f3e5cbd5bef4646677e5467 (diff) | |
download | CMake-5bb94ce16635954f460edcacd5422bc8d53fb8c1.zip CMake-5bb94ce16635954f460edcacd5422bc8d53fb8c1.tar.gz CMake-5bb94ce16635954f460edcacd5422bc8d53fb8c1.tar.bz2 |
ENH: apply patch from Mathieu, add argument -E md5sum to compute md5sums of
files, compatible to md5sum output
Alex
-rw-r--r-- | Source/cmSystemTools.cxx | 47 | ||||
-rw-r--r-- | Source/cmSystemTools.h | 3 | ||||
-rw-r--r-- | Source/cmake.cxx | 26 |
3 files changed, 76 insertions, 0 deletions
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 41444ad..5e1a38b 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -22,6 +22,7 @@ #include <cmsys/RegularExpression.hxx> #include <cmsys/Directory.hxx> #include <cmsys/System.h> +#include <cmsys/MD5.h> // support for realpath call #ifndef _WIN32 @@ -1046,6 +1047,52 @@ bool cmSystemTools::CopyFileIfDifferent(const char* source, return Superclass::CopyFileIfDifferent(source, destination); } +bool cmSystemTools::ComputeFileMD5(const char* source, char* md5out) +{ + if(!cmSystemTools::FileExists(source)) + { + return false; + } + + // Open files +#if defined(_WIN32) || defined(__CYGWIN__) + cmsys_ios::ifstream fin(source, cmsys_ios::ios::binary | cmsys_ios::ios::in); +#else + cmsys_ios::ifstream fin(source); +#endif + if(!fin) + { + return false; + } + + cmsysMD5* md5 = cmsysMD5_New(); + cmsysMD5_Initialize(md5); + + // Should be efficient enough on most system: + const int bufferSize = 4096; + char buffer[bufferSize]; + // This copy loop is very sensitive on certain platforms with + // slightly broken stream libraries (like HPUX). Normally, it is + // incorrect to not check the error condition on the fin.read() + // before using the data, but the fin.gcount() will be zero if an + // error occurred. Therefore, the loop should be safe everywhere. + while(fin) + { + fin.read(buffer, bufferSize); + if(fin.gcount()) + { + cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(buffer), + fin.gcount()); + } + } + cmsysMD5_FinalizeHex(md5, md5out); + cmsysMD5_Delete(md5); + + fin.close(); + + return true; +} + void cmSystemTools::Glob(const char *directory, const char *regexp, std::vector<std::string>& files) { diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index f785358..0cba39c 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -165,6 +165,9 @@ public: static bool CopyFileIfDifferent(const char* source, const char* destination); + ///! Compute the md5sum of a file + static bool ComputeFileMD5(const char* source, char* md5out); + /** * Run an executable command and put the stdout in output. * A temporary file is created in the binaryDir for storing the diff --git a/Source/cmake.cxx b/Source/cmake.cxx index c928603..3518067 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -865,6 +865,7 @@ void CMakeCommandUsage(const char* program) " line\n" << " environment - display the current enviroment\n" << " make_directory dir - create a directory\n" + << " md5sum file1 [...] - compute md5sum of files\n" << " remove_directory dir - remove a directory and its contents\n" << " remove file1 file2 ... - remove the file(s)\n" << " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar.\n" @@ -1056,6 +1057,31 @@ int cmake::ExecuteCMakeCommand(std::vector<std::string>& args) return 0; } + // Command to calculate the md5sum of a file + else if (args[1] == "md5sum" && args.size() >= 3) + { + char md5out[32]; + 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; + } + else if(!cmSystemTools::ComputeFileMD5(filename, md5out)) + { + // To mimic md5sum behavior in a shell: + std::cerr << filename << ": No such file or directory" << std::endl; + } + else + { + std::cout << std::string(md5out,32) << " " << filename << std::endl; + } + } + return 1; + } + // Command to change directory and run a program. else if (args[1] == "chdir" && args.size() >= 4) { |