fs: add tmp file creation routines
This commit is contained in:
parent
a2259013f5
commit
acbfa674ff
@ -342,6 +342,8 @@ list (
|
|||||||
float.hpp
|
float.hpp
|
||||||
fourcc.cpp
|
fourcc.cpp
|
||||||
fourcc.hpp
|
fourcc.hpp
|
||||||
|
fs/tmp_posix.cpp
|
||||||
|
fs/tmp.hpp
|
||||||
functor.hpp
|
functor.hpp
|
||||||
geom/fwd.hpp
|
geom/fwd.hpp
|
||||||
geom/aabb.cpp
|
geom/aabb.cpp
|
||||||
@ -706,6 +708,8 @@ if (TESTS)
|
|||||||
extent
|
extent
|
||||||
fixed
|
fixed
|
||||||
float
|
float
|
||||||
|
fs/scoped
|
||||||
|
fs/tmp
|
||||||
geom/aabb
|
geom/aabb
|
||||||
geom/ellipse
|
geom/ellipse
|
||||||
geom/frustum
|
geom/frustum
|
||||||
|
23
fs/tmp.hpp
Normal file
23
fs/tmp.hpp
Normal 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
36
fs/tmp_posix.cpp
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user