2022-01-19 12:01:16 +11:00
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* Copyright 2022, Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "./tmp.hpp"
|
|
|
|
|
|
|
|
#include "./paths.hpp"
|
|
|
|
#include "./posix/except.hpp"
|
|
|
|
|
2024-02-21 15:54:53 +11:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-01-19 12:01:16 +11:00
|
|
|
|
2022-05-27 13:20:02 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
std::filesystem::path
|
|
|
|
cruft::fs::mktempfile ()
|
|
|
|
{
|
|
|
|
return mktempfile (cruft::paths::temp ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::filesystem::path
|
|
|
|
cruft::fs::mktempfile (std::filesystem::path const &dir)
|
|
|
|
{
|
|
|
|
auto pattern = (dir / PACKAGE_NAME "-XXXXXX").native ();
|
2024-02-21 15:54:53 +11:00
|
|
|
|
|
|
|
auto const fd = mkstemp (pattern.data ());
|
|
|
|
if (fd < 0)
|
2022-05-27 13:20:02 +10:00
|
|
|
cruft::posix::error::throw_code ();
|
2024-02-21 15:54:53 +11:00
|
|
|
|
|
|
|
cruft::posix::error::try_call(::close, fd);
|
|
|
|
|
2022-05-27 13:20:02 +10:00
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-19 12:01:16 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
std::filesystem::path
|
|
|
|
cruft::fs::mktmpdir (void)
|
|
|
|
{
|
|
|
|
// The pattern suffix we'll append to the temp directory.
|
|
|
|
//
|
|
|
|
// * We might as well append the package name for clarity
|
|
|
|
// * The libc call REQUIRES the string end with "XXXXXX"
|
|
|
|
char const SUFFIX[] = "/" PACKAGE_NAME "-XXXXXX";
|
|
|
|
|
|
|
|
auto const root = cruft::paths::temp ();
|
|
|
|
std::string path;
|
|
|
|
path.reserve (root.string ().size () + sizeof (SUFFIX));
|
2022-01-19 16:03:55 +11:00
|
|
|
path = root.string ();
|
2022-01-19 12:01:16 +11:00
|
|
|
path += SUFFIX;
|
|
|
|
|
|
|
|
if (!mkdtemp (path.data ()))
|
|
|
|
cruft::posix::error::throw_code ();
|
|
|
|
|
2022-01-19 16:03:55 +11:00
|
|
|
return std::filesystem::path (path);
|
2022-01-19 12:01:16 +11:00
|
|
|
}
|