From ab26d334bfa74871095239a53a6c7af92bdbacdf Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Thu, 18 Jul 2024 07:50:46 -0400 Subject: cmCPackExternalGenerator: ensure JSON is written before running the script First, use `cmGeneratedFileStream` to avoid touching an existing file unless its contents change and to get atomic replacement. Also add a scope to ensure that the file is in place (at `fout`'s destructor) before doing anything with the packaging scripts. See: https://discourse.cmake.org/t/1773 --- Source/CPack/cmCPackExternalGenerator.cxx | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Source/CPack/cmCPackExternalGenerator.cxx b/Source/CPack/cmCPackExternalGenerator.cxx index 52eacaa..76cb876 100644 --- a/Source/CPack/cmCPackExternalGenerator.cxx +++ b/Source/CPack/cmCPackExternalGenerator.cxx @@ -15,6 +15,7 @@ #include "cmCPackComponentGroup.h" #include "cmCPackLog.h" +#include "cmGeneratedFileStream.h" #include "cmList.h" #include "cmMakefile.h" #include "cmSystemTools.h" @@ -48,17 +49,19 @@ int cmCPackExternalGenerator::PackageFiles() filename = this->packageFileNames[0]; } - cmsys::ofstream fout(filename.c_str()); - std::unique_ptr jout(builder.newStreamWriter()); + { + cmGeneratedFileStream fout(filename); + std::unique_ptr jout(builder.newStreamWriter()); - Json::Value root(Json::objectValue); + Json::Value root(Json::objectValue); - if (!this->Generator->WriteToJSON(root)) { - return 0; - } + if (!this->Generator->WriteToJSON(root)) { + return 0; + } - if (jout->write(root, &fout)) { - return 0; + if (jout->write(root, &fout)) { + return 0; + } } cmValue packageScript = this->GetOption("CPACK_EXTERNAL_PACKAGE_SCRIPT"); -- cgit v0.12