Skip to content

Commit

Permalink
Introduce Utility::CreateTempDir()
Browse files Browse the repository at this point in the history
refs #7742
  • Loading branch information
Al2Klimov committed Mar 23, 2020
1 parent 14de659 commit 939c52d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/base/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/base/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 939c52d

Please sign in to comment.