diff --git a/FWCore/ParameterSet/bin/edmWriteConfigs.cpp b/FWCore/ParameterSet/bin/edmWriteConfigs.cpp index b056ed9074eb1..715901b0a7698 100644 --- a/FWCore/ParameterSet/bin/edmWriteConfigs.cpp +++ b/FWCore/ParameterSet/bin/edmWriteConfigs.cpp @@ -54,6 +54,8 @@ #include #include #include +#include +#include static char const* const kHelpOpt = "help"; static char const* const kHelpCommandOpt = "help,h"; @@ -136,18 +138,33 @@ namespace { } } - void writeModulesFile() { + bool open_temp(std::string& path, std::ofstream& f) { + path += "/XXXXXX"; + path += '\0'; // ensure that the string is null-terminated + int fd = mkstemp(path.data()); + if (fd != -1) { + f.open(path.c_str(), std::ios_base::trunc | std::ios_base::out); + close(fd); + return true; + } + return false; + } + + bool writeModulesFile() { if (std::filesystem::exists(std::filesystem::current_path() / "modules.py")) return; - std::array buffer; - std::tmpnam(buffer.data()); - std::ofstream file{buffer.data()}; - - file << "from FWCore.ParameterSet.ModulesProxy import _setupProxies\n" - "locals().update(_setupProxies(__file__))\n"; - file.close(); - std::filesystem::copy(buffer.data(), "modules.py"); - std::filesystem::remove(buffer.data()); + std::ofstream file; + std::string path(std::filesystem::current_path()); + bool res = open_temp(path, file); + if (res) { + file << "from FWCore.ParameterSet.ModulesProxy import _setupProxies\n" + "locals().update(_setupProxies(__file__))\n"; + file.close(); + std::filesystem::copy(path.data(), "modules.py"); + std::filesystem::remove(path.data()); + return true; + } + return false; } } // namespace @@ -304,7 +321,11 @@ int main(int argc, char** argv) try { std::set usedCfiFileNames; edm::for_all(pluginNames, std::bind(&writeCfisForPlugin, _1, factory, std::ref(usedCfiFileNames))); if (not pluginNames.empty()) { - writeModulesFile(); + bool res = writeModulesFile(); + if (!res) { + std::cerr << "writeModulesFile() failed" << std::endl; + return 1; + } } }); } catch (cms::Exception& iException) {