Copying a file to the same directory zeros out the file
I discovered that in 3.14.0 the cmake -E copy
command will zero out a file if it gets copied into the same directory (e.g. cmake -E copy foo.txt .
. I tracked the issue to a change in cmcmd.cxx
, in the method int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
. The copy code calls cmsys::SystemTools::CopyFileAlways()
. I'm not sure why it calls that particular method, but going through the code I found that cmsys::SystemTools::CopyAFile()
with false
passed in as the "always" parameter works correctly.
I made a patch that fixes the issue:
--- Source/cmcmd.cxx.orig 2019-03-21 21:36:19.035781047 -0500
+++ Source/cmcmd.cxx 2019-03-21 21:37:03.919430843 -0500
@@ -483,7 +483,7 @@
// If error occurs we want to continue copying next files.
bool return_value = false;
for (std::string::size_type cc = 2; cc < args.size() - 1; cc++) {
- if (!cmsys::SystemTools::CopyFileAlways(args[cc], args.back())) {
+ if (!cmsys::SystemTools::CopyAFile(args[cc], args.back(), false)) {
std::cerr << "Error copying file \"" << args[cc] << "\" to \""
<< args.back() << "\".\n";
return_value = true;
To reproduce the issue:
$ echo hello world > foo.txt
(approx 9 bytes)
$ cmake -E copy foo.txt .
$ ls -lah foo.txt
(will be 0 bytes)