ENH: apply patch from Mathieu, add argument -E md5sum to compute md5sums of

files, compatible to md5sum output

Alex
This commit is contained in:
Alexander Neundorf 2007-07-16 10:54:32 -04:00
parent 56838c115e
commit 5bb94ce166
3 changed files with 76 additions and 0 deletions

View File

@ -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)
{

View File

@ -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

View File

@ -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)
{