fs: add tmp file creation routines

This commit is contained in:
Danny Robson 2022-01-19 11:01:16 +10:00
parent a2259013f5
commit acbfa674ff
3 changed files with 63 additions and 0 deletions

View File

@ -342,6 +342,8 @@ list (
float.hpp
fourcc.cpp
fourcc.hpp
fs/tmp_posix.cpp
fs/tmp.hpp
functor.hpp
geom/fwd.hpp
geom/aabb.cpp
@ -706,6 +708,8 @@ if (TESTS)
extent
fixed
float
fs/scoped
fs/tmp
geom/aabb
geom/ellipse
geom/frustum

23
fs/tmp.hpp Normal file
View File

@ -0,0 +1,23 @@
/*
* 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>
*/
#pragma once
#include <filesystem>
namespace cruft::fs {
/// Create a temporary directory and return the path to the a newly
/// created directory (in a system appropriate location).
///
/// It is the caller's responsibility to clean up this directory if
/// required.
///
/// No guarantees are made about the longevity of the directory after
/// the application terminates.
std::filesystem::path mktmpdir [[nodiscard]] (void);
}

36
fs/tmp_posix.cpp Normal file
View File

@ -0,0 +1,36 @@
/*
* 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;
}