ENH: more paranoid checkings

This commit is contained in:
Sebastien Barre 2002-04-14 15:32:16 -04:00
parent c3c4e415bb
commit 36189ce2a8
1 changed files with 72 additions and 31 deletions

View File

@ -970,8 +970,10 @@ bool cmSystemTools::FilesDiffer(const char* source,
} }
#if defined(_WIN32) || defined(__CYGWIN__) #if defined(_WIN32) || defined(__CYGWIN__)
std::ifstream finSource(source, std::ios::binary | std::ios::in); std::ifstream finSource(source,
std::ifstream finDestination(destination, std::ios::binary | std::ios::in); std::ios::binary | std::ios::in);
std::ifstream finDestination(destination,
std::ios::binary | std::ios::in);
#else #else
std::ifstream finSource(source); std::ifstream finSource(source);
std::ifstream finDestination(destination); std::ifstream finDestination(destination);
@ -982,7 +984,19 @@ bool cmSystemTools::FilesDiffer(const char* source,
} }
char* source_buf = new char[statSource.st_size]; char* source_buf = new char[statSource.st_size];
if (!source_buf)
{
cmSystemTools::Error("FilesDiffer failed to allocate memory for source!");
return false;
}
char* dest_buf = new char[statSource.st_size]; char* dest_buf = new char[statSource.st_size];
if (!dest_buf)
{
cmSystemTools::Error("FilesDiffer failed to allocate memory for dest!");
delete [] source_buf;
return false;
}
finSource.read(source_buf, statSource.st_size); finSource.read(source_buf, statSource.st_size);
finDestination.read(dest_buf, statSource.st_size); finDestination.read(dest_buf, statSource.st_size);
@ -990,12 +1004,17 @@ bool cmSystemTools::FilesDiffer(const char* source,
if(statSource.st_size != finSource.gcount() || if(statSource.st_size != finSource.gcount() ||
statSource.st_size != finDestination.gcount()) statSource.st_size != finDestination.gcount())
{ {
cmSystemTools::Error("FilesDiffer failed reading files!"); char msg[256];
delete [] dest_buf; sprintf(msg, "FilesDiffer failed to read files (allocated: %lu, source: %lu, dest: %lu)", statSource.st_size, finSource.gcount(), finDestination.gcount());
cmSystemTools::Error(msg);
delete [] source_buf; delete [] source_buf;
delete [] dest_buf;
return false; return false;
} }
finSource.close();
finDestination.close();
int ret = memcmp((const void*)source_buf, int ret = memcmp((const void*)source_buf,
(const void*)dest_buf, (const void*)dest_buf,
statSource.st_size); statSource.st_size);
@ -1016,23 +1035,9 @@ void cmSystemTools::cmCopyFile(const char* source,
const int bufferSize = 4096; const int bufferSize = 4096;
char buffer[bufferSize]; char buffer[bufferSize];
#if defined(_WIN32) || defined(__CYGWIN__)
std::ifstream fin(source, std::ios::binary | std::ios::in);
#else
std::ifstream fin(source);
#endif
if(!fin)
{
cmSystemTools::Error("CopyFile failed to open input file \"",
source, "\"");
return;
}
// If destination is a directory, try to create a file with the same // If destination is a directory, try to create a file with the same
// name as the source in that directory. // name as the source in that directory.
const char* dest = destination;
std::string new_destination; std::string new_destination;
if(cmSystemTools::FileExists(destination) && if(cmSystemTools::FileExists(destination) &&
cmSystemTools::FileIsDirectory(destination)) cmSystemTools::FileIsDirectory(destination))
@ -1042,24 +1047,41 @@ void cmSystemTools::cmCopyFile(const char* source,
new_destination += '/'; new_destination += '/';
std::string source_name = source; std::string source_name = source;
new_destination += cmSystemTools::GetFilenameName(source_name); new_destination += cmSystemTools::GetFilenameName(source_name);
dest = new_destination.c_str(); destination = new_destination.c_str();
} }
// Create destination directory // Create destination directory
std::string destination_dir = dest; std::string destination_dir = destination;
destination_dir = cmSystemTools::GetFilenamePath(destination_dir); destination_dir = cmSystemTools::GetFilenamePath(destination_dir);
cmSystemTools::MakeDirectory(destination_dir.c_str()); cmSystemTools::MakeDirectory(destination_dir.c_str());
// Open files
#if defined(_WIN32) || defined(__CYGWIN__) #if defined(_WIN32) || defined(__CYGWIN__)
std::ofstream fout(dest, std::ios::binary | std::ios::out | std::ios::trunc); std::ifstream fin(source,
std::ios::binary | std::ios::in);
#else #else
std::ofstream fout(dest, std::ios::out | std::ios::trunc); std::ifstream fin(source);
#endif
if(!fin)
{
cmSystemTools::Error("CopyFile failed to open input file \"",
source, "\"");
return;
}
#if defined(_WIN32) || defined(__CYGWIN__)
std::ofstream fout(destination,
std::ios::binary | std::ios::out | std::ios::trunc);
#else
std::ofstream fout(destination,
std::ios::out | std::ios::trunc);
#endif #endif
if(!fout) if(!fout)
{ {
cmSystemTools::Error("CopyFile failed to open output file \"", cmSystemTools::Error("CopyFile failed to open output file \"",
dest, "\""); destination, "\"");
return; return;
} }
@ -1076,6 +1098,25 @@ void cmSystemTools::cmCopyFile(const char* source,
fout.write(buffer, fin.gcount()); fout.write(buffer, fin.gcount());
} }
} }
fin.close();
fout.close();
// More checks
struct stat statSource, statDestination;
if (stat(source, &statSource) != 0 ||
stat(destination, &statDestination) != 0)
{
cmSystemTools::Error("CopyFile failed to copy files!");
}
if (statSource.st_size != statDestination.st_size)
{
char msg[256];
sprintf(msg, "CopyFile failed to copy files (sizes differ, source: %lu, dest: %lu)", statSource.st_size, statDestination.st_size);
cmSystemTools::Error(msg);
}
} }
// return true if the file exists // return true if the file exists