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: