mirror of
https://github.com/XRPLF/rippled.git
synced 2026-07-25 16:10:57 +00:00
72 lines
1.4 KiB
C++
72 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <string>
|
|
|
|
namespace beast {
|
|
|
|
/**
|
|
* RAII temporary directory.
|
|
*
|
|
* The directory and all its contents are deleted when
|
|
* the instance of `temp_dir` is destroyed.
|
|
*/
|
|
class TempDir
|
|
{
|
|
boost::filesystem::path path_;
|
|
|
|
public:
|
|
#if !GENERATING_DOCS
|
|
TempDir(TempDir const&) = delete;
|
|
TempDir&
|
|
operator=(TempDir const&) = delete;
|
|
#endif
|
|
|
|
/**
|
|
* Construct a temporary directory.
|
|
*/
|
|
TempDir()
|
|
{
|
|
auto const dir = boost::filesystem::temp_directory_path();
|
|
do
|
|
{
|
|
path_ = dir / boost::filesystem::unique_path();
|
|
} while (boost::filesystem::exists(path_));
|
|
boost::filesystem::create_directory(path_);
|
|
}
|
|
|
|
/**
|
|
* Destroy a temporary directory.
|
|
*/
|
|
~TempDir()
|
|
{
|
|
// use non-throwing calls in the destructor
|
|
boost::system::error_code ec;
|
|
boost::filesystem::remove_all(path_, ec);
|
|
// TODO: warn/notify if ec set ?
|
|
}
|
|
|
|
/**
|
|
* Get the native path for the temporary directory
|
|
*/
|
|
[[nodiscard]] std::string
|
|
path() const
|
|
{
|
|
return path_.string();
|
|
}
|
|
|
|
/**
|
|
* Get the native path for the a file.
|
|
*
|
|
* The file does not need to exist.
|
|
*/
|
|
[[nodiscard]] std::string
|
|
file(std::string const& name) const
|
|
{
|
|
return (path_ / name).string();
|
|
}
|
|
};
|
|
|
|
} // namespace beast
|