diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index b1c49beaa7f..4a40297873b 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -1711,6 +1711,31 @@ String Utility::CreateTempFile(const String& path, int mode, std::fstream& fp) return resultPath; } +String Utility::CreateTempDir(const String& path, int mode) +{ + auto targetPath (path.GetData()); + +#ifndef _WIN32 + if (!mkdtemp(&targetPath[0])) { +#else /* _WIN32 */ + if (!MkdTemp(&targetPath[0])) { +#endif /*_WIN32*/ + BOOST_THROW_EXCEPTION(posix_error() + << boost::errinfo_api_function("mkdtemp") + << boost::errinfo_errno(errno) + << boost::errinfo_file_name(path)); + } + + if (chmod(targetPath.c_str(), mode) < 0) { + BOOST_THROW_EXCEPTION(posix_error() + << boost::errinfo_api_function("chmod") + << boost::errinfo_errno(errno) + << boost::errinfo_file_name(targetPath)); + } + + return std::move(targetPath); +} + #ifdef _WIN32 /* mkstemp extracted from libc/sysdeps/posix/tempname.c and mkdtemp derived from it. * Copyright (C) 1991-1999, 2000, 2001, 2006 Free Software Foundation, Inc. diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp index f7211591a28..782c61f0f47 100644 --- a/lib/base/utility.hpp +++ b/lib/base/utility.hpp @@ -129,6 +129,7 @@ class Utility static String ValidateUTF8(const String& input); static String CreateTempFile(const String& path, int mode, std::fstream& fp); + static String CreateTempDir(const String& path, int mode); #ifdef _WIN32 static String GetIcingaInstallPath();