RAII-wrappers for unique temporary files and directories that are deleted automatically for C++17 and later.
This library provides functionality for efficient management of temporary files and directories in long-running applications, automatically deleting them as soon as your app is done with them to optimize resource management.
When developing applications with long uptimes, such as server software, the management of temporary files and directories can become crucial. While the system may eventually clean up temporary files, this process may not occur frequently enough to meet the needs of applications with extended operational periods. Relying solely on the system for this task may lead to an accumulation of unnecessary temporary data on the device.
To address this challenge, this library provides functionality for creating temporary files and directories that are automatically deleted as soon as the application finishes using them. This ensures efficient resource management and prevents unnecessary clutter. Even in the event of a crash, the system will eventually clean up any temporary files created by this library, ensuring a seamless user experience and optimal performance.
This library has been tested on:
- macOS
- Linux systems (e.g. Ubuntu Linux, Arch Linux, etc.)
- FreeBSD (and should work on other BSD systems too)
- Windows
tmp::directory is a smart handle that manages a temporary directory, ensuring its recursive deletion when the handle goes out of scope. When a tmp::directory object is created, it generates a unique temporary directory in the current user's temporary directory.
The temporary directory is deleted when either of the following happens:
- the
tmp::directoryobject is destroyed - the
tmp::directoryobject is assigned another directory usingoperator=
The example below demonstrates a usage of a tmp::directory object to run a subprocess within it and archive its logs; when the function returns, the temporary directory is recursively deleted:
#include <tmp/directory>
auto func() {
auto tmpdir = tmp::directory("org.example.product");
process::exec(executable, args, tmpdir);
return archive::glob(tmpdir, "*.log");
// The temporary directory is deleted recursively when the
// tmp::directory object goes out of scope
}tmp::file is a smart handle that manages a binary temporary file, ensuring its deletion when the handle goes out of scope. Upon creation, a tmp::file object generates a unique temporary file, opening it for both reading and writing in binary format.
The temporary file is deleted of when either of the following happens:
- the
tmp::fileobject is destroyed - the
tmp::fileobject is assigned another file usingoperator=
tmp::file inherits from the std::iostream class, allowing it to be used seamlessly with standard input/output operations and simplifying file handling while maintaining the flexibility of stream operations.
The example below demonstrates a usage of a tmp::file object to validate a request content and then unarchive it to persistent storage:
#include <tmp/file>
auto func(std::string_view content) {
auto tmpfile = tmp::file();
tmpfile << contents << std::flush;
if (validate(tmpfile)) {
// Unarchive the file to the persistent storage
archive::unzip(tmpfile, storage);
} else {
// The file is deleted automatically
throw InvalidRequestError();
}
}To build the project without tests, use the -DBUILD_TESTING=OFF flag:
cmake -B build -DBUILD_TESTING=OFF && cmake --build buildTo build the project with tests, run:
cmake -B build && cmake --build build --target tmp.testTo run tests:
ctest --output-on-failure --test-dir buildTo install the project, configure it in the Release configuration:
cmake -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=ONbuild it:
cmake --build buildand then install:
sudo cmake --install buildOR use dpkg package manager (requires dpkg-shlibdeps):
cd build && cpack -G DEB && sudo dpkg -i *.debOR use rpm package manager (requires rpmbuild):
cd build && cpack -G RPM && sudo rpm -i *.rpmIf you are using homebrew, use the following command:
brew install bugdea1er/tap/tmpYou can also use the CMake interface target tmp::tmp and described below:
To use this library from a CMake project, locate it using find_package:
find_package(tmp REQUIRED)
# ...
add_library(foo ...)
target_link_libraries(foo PUBLIC tmp::tmp)To embed the library into your existing CMake project, place the entire source tree in a subdirectory (for example, using git submodule commands) and call add_subdirectory() in your CMakeLists.txt file:
add_subdirectory(tmp)
# ...
add_library(foo ...)
target_link_libraries(foo PUBLIC tmp::tmp)You can also use the FetchContent functions to automatically download a dependency. Put this in your CMakeLists.txt file:
include(FetchContent)
FetchContent_Declare(tmp URL https://github.com/bugdea1er/tmp/releases/download/<version>/tmp.tar.xz)
FetchContent_MakeAvailable(tmp)
target_link_libraries(foo PUBLIC tmp::tmp)Contributions are always welcome!