libcruft-util/fs/tmp_posix.cpp

37 lines
1.0 KiB
C++
Raw Normal View History

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"
///////////////////////////////////////////////////////////////////////////////
std::filesystem::path
cruft::fs::mktmpdir (void)
{
// The pattern suffix we'll append to the temp directory.
//
// * It needs a slash to ensure we don't try to create a new 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));
path = root.string ();
path += SUFFIX;
if (!mkdtemp (path.data ()))
cruft::posix::error::throw_code ();
return path;
}