ENH: Improved implementation of FilesDiffer to avoid allocating enough memory for the entire file twice. Instead using a block-at-a-time comparison.
This commit is contained in:
parent
58d0c5e176
commit
b8a589bb1d
@ -1419,6 +1419,7 @@ bool SystemTools::CopyFileIfDifferent(const char* source,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define KWSYS_ST_BUFFER 4096
|
||||||
|
|
||||||
bool SystemTools::FilesDiffer(const char* source,
|
bool SystemTools::FilesDiffer(const char* source,
|
||||||
const char* destination)
|
const char* destination)
|
||||||
@ -1459,28 +1460,36 @@ bool SystemTools::FilesDiffer(const char* source,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* source_buf = new char[statSource.st_size];
|
// Compare the files a block at a time.
|
||||||
char* dest_buf = new char[statSource.st_size];
|
char source_buf[KWSYS_ST_BUFFER];
|
||||||
|
char dest_buf[KWSYS_ST_BUFFER];
|
||||||
finSource.read(source_buf, statSource.st_size);
|
long nleft = statSource.st_size;
|
||||||
finDestination.read(dest_buf, statSource.st_size);
|
while(nleft > 0)
|
||||||
|
{
|
||||||
if(statSource.st_size != static_cast<long>(finSource.gcount()) ||
|
// Read a block from each file.
|
||||||
statSource.st_size != static_cast<long>(finDestination.gcount()))
|
long nnext = (nleft > KWSYS_ST_BUFFER)? KWSYS_ST_BUFFER : nleft;
|
||||||
|
finSource.read(source_buf, nnext);
|
||||||
|
finDestination.read(dest_buf, nnext);
|
||||||
|
|
||||||
|
// If either failed to read assume they are different.
|
||||||
|
if(static_cast<long>(finSource.gcount()) != nnext ||
|
||||||
|
static_cast<long>(finDestination.gcount()) != nnext)
|
||||||
{
|
{
|
||||||
// Failed to read files.
|
|
||||||
delete [] source_buf;
|
|
||||||
delete [] dest_buf;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
int ret = memcmp((const void*)source_buf,
|
|
||||||
(const void*)dest_buf,
|
|
||||||
statSource.st_size);
|
|
||||||
|
|
||||||
delete [] dest_buf;
|
// If this block differs the file differs.
|
||||||
delete [] source_buf;
|
if(memcmp((const void*)source_buf, (const void*)dest_buf, nnext) != 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return ret != 0;
|
// Update the byte count remaining.
|
||||||
|
nleft -= nnext;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No differences found.
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user