diff --git a/CMakeLists.txt b/CMakeLists.txt index 673f001e..4baa4c87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/fs/tmp.hpp b/fs/tmp.hpp new file mode 100644 index 00000000..3f87a50b --- /dev/null +++ b/fs/tmp.hpp @@ -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 + */ + +#pragma once + +#include + +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); +} \ No newline at end of file diff --git a/fs/tmp_posix.cpp b/fs/tmp_posix.cpp new file mode 100644 index 00000000..14c19afe --- /dev/null +++ b/fs/tmp_posix.cpp @@ -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 + */ + +#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; +}