[cmake-commits] alex committed cmake.cxx 1.308 1.309 cmSystemTools.h
1.139 1.140 cmSystemTools.cxx 1.339 1.340
cmake-commits at cmake.org
cmake-commits at cmake.org
Mon Jul 16 10:54:34 EDT 2007
Update of /cvsroot/CMake/CMake/Source
In directory public:/mounts/ram/cvs-serv20837
Modified Files:
cmake.cxx cmSystemTools.h cmSystemTools.cxx
Log Message:
ENH: apply patch from Mathieu, add argument -E md5sum to compute md5sums of
files, compatible to md5sum output
Alex
Index: cmSystemTools.h
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSystemTools.h,v
retrieving revision 1.139
retrieving revision 1.140
diff -u -d -r1.139 -r1.140
--- cmSystemTools.h 12 Mar 2007 17:50:28 -0000 1.139
+++ cmSystemTools.h 16 Jul 2007 14:54:31 -0000 1.140
@@ -165,6 +165,9 @@
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
Index: cmSystemTools.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSystemTools.cxx,v
retrieving revision 1.339
retrieving revision 1.340
diff -u -d -r1.339 -r1.340
--- cmSystemTools.cxx 17 May 2007 15:27:46 -0000 1.339
+++ cmSystemTools.cxx 16 Jul 2007 14:54:32 -0000 1.340
@@ -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 @@
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)
{
Index: cmake.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmake.cxx,v
retrieving revision 1.308
retrieving revision 1.309
diff -u -d -r1.308 -r1.309
--- cmake.cxx 13 Jul 2007 04:58:43 -0000 1.308
+++ cmake.cxx 16 Jul 2007 14:54:30 -0000 1.309
@@ -865,6 +865,7 @@
" 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 @@
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)
{
More information about the Cmake-commits
mailing list