fs: add scoped_dir container

This commit is contained in:
Danny Robson 2022-01-19 11:21:30 +10:00
parent 74700363ab
commit 9b11cc92ff
4 changed files with 116 additions and 0 deletions

View File

@ -344,6 +344,8 @@ list (
float.hpp float.hpp
fourcc.cpp fourcc.cpp
fourcc.hpp fourcc.hpp
fs/scoped.cpp
fs/scoped.hpp
fs/tmp_posix.cpp fs/tmp_posix.cpp
fs/tmp.hpp fs/tmp.hpp
functor.hpp functor.hpp

51
fs/scoped.cpp Normal file
View File

@ -0,0 +1,51 @@
/*
* 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 "./scoped.hpp"
#include <cruft/util/debug/assert.hpp>
using cruft::fs::scoped_dir;
///////////////////////////////////////////////////////////////////////////////
scoped_dir::scoped_dir (std::filesystem::path const &_path)
: scoped_dir (std::filesystem::path (_path))
{}
//-----------------------------------------------------------------------------
scoped_dir::scoped_dir (std::filesystem::path &&_path)
: m_path (std::move (_path))
{
CHECK (std::filesystem::is_directory (*m_path));
}
///////////////////////////////////////////////////////////////////////////////
scoped_dir::~scoped_dir ()
{
if (m_path)
std::filesystem::remove_all (*m_path);
}
///////////////////////////////////////////////////////////////////////////////
std::filesystem::path const &
scoped_dir::operator* () const &
{
return *m_path;
}
//-----------------------------------------------------------------------------
std::filesystem::path const*
scoped_dir::operator-> () const &
{
return &*m_path;
}

37
fs/scoped.hpp Normal file
View File

@ -0,0 +1,37 @@
/*
* 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 <optional>
#include <filesystem>
///////////////////////////////////////////////////////////////////////////////
namespace cruft::fs {
///------------------------------------------------------------------------
/// Holds the path to a directory that will be deleted (along with all
/// contents) when the object goes out of scope.
class scoped_dir {
public:
explicit scoped_dir (std::filesystem::path const&);
explicit scoped_dir (std::filesystem::path &&);
scoped_dir (scoped_dir const&) = delete;
scoped_dir& operator= (scoped_dir const&) = delete;
scoped_dir (scoped_dir&&) noexcept = default;
scoped_dir& operator= (scoped_dir&&) noexcept = default;
~scoped_dir ();
std::filesystem::path const& operator* () const&;
std::filesystem::path const* operator-> () const&;
private:
std::optional<std::filesystem::path> m_path;
};
}

26
test/fs/scoped.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <cruft/util/tap.hpp>
#include <cruft/util/fs/scoped.hpp>
#include <cruft/util/fs/tmp.hpp>
int
main (int, char**)
{
cruft::TAP::logger tap;
{
bool success = true;
auto const path = cruft::fs::mktmpdir ();
{
cruft::fs::scoped_dir dir (path);
success = success && std::filesystem::is_directory (path);
}
success = success && !exists(path);
tap.expect (success, "scoped_dir removes directory after scope");
}
return tap.status ();
}