You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@@ -26,68 +27,54 @@ The location of temporary files generated by this library is determined by the `
26
27
27
28
### Temporary directories
28
29
29
-
`tmp::directory` is a RAII-handle for unique temporary directories. This can be useful for:
30
-
31
-
1.**Batch Processing:** Temporary directories can be used during batch processing tasks where multiple files need to be processed in a specific order. For example, a script that processes a large number of images may create temporary directories to organize and manage the input and output files efficiently.
32
-
33
-
2.**Software Installation:** Temporary directories are commonly used during software installation processes to store temporary files needed for installation. For instance, when installing a new application, temporary directories can be used to extract installation packages and store temporary configuration files.
30
+
`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.
34
31
35
-
3.**Data Backup:** Temporary directories can be utilized for creating temporary backups of important data before performing critical operations. For example, before making changes to a database, a temporary directory can be used to store a backup copy of the database to ensure data integrity.
32
+
The temporary directory is deleted when either of the following happens:
33
+
- the `tmp::directory` object is destroyed
34
+
- the `tmp::directory` object is assigned another directory using `operator=`
36
35
37
-
4.**File Compression:** Temporary directories are often used during file compression tasks where multiple files need to be compressed into a single archive. For instance, when creating a zip file containing multiple documents, a temporary directory can be used to store the compressed files before creating the final archive.
38
-
39
-
5.**Data Migration:** Temporary directories can be helpful during data migration processes where data needs to be transferred from one system to another. For example, when migrating data from a legacy system to a new system, temporary directories can be used to stage and process the data before final migration.
36
+
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:
40
37
41
38
```cpp
42
39
#include<tmp/directory>
43
-
...
44
-
{
45
-
// Create a unique temporary directory
46
-
auto tmpdir = tmp::directory("org.example.product");
47
-
48
-
// Populate this directory
49
-
fs::copy(file1, tmpdir);
50
-
fs::copy(file2, tmpdir);
51
-
52
-
// Make an archive from this directory
53
-
archiver::zip(tmpdir);
54
-
} // The directory will be deleted when exiting the scope
55
-
```
56
40
57
-
### Temporary files
41
+
autofunc() {
42
+
auto tmpdir = tmp::directory("org.example.product");
43
+
process::exec(executable, args, tmpdir);
58
44
59
-
`tmp::file` is a RAII-handle for unique temporary files. This can be useful for:
45
+
return archive::glob(tmpdir, "*.log");
60
46
61
-
1.**Caching:** Temporary files can be used to cache data that needs to be accessed frequently but doesn't need to be stored permanently. For example, a web application can store pre-rendered HTML pages in temporary files to improve performance by serving them quickly without re-generating them each time.
47
+
// The temporary directory is deleted recursively when the
48
+
// tmp::directory object goes out of scope
49
+
}
50
+
```
62
51
63
-
2.**Data Processing:**Temporary files are often used during data processing tasks where intermediate results need to be stored temporarily before further processing. For instance, a script that manipulates a large dataset may write intermediate results to temporary files to avoid memory constraints.
52
+
### Temporary files
64
53
65
-
3.**File Conversion:** Temporary files can be utilized during file format conversions. For instance, when converting a video file from one format to another, temporary files can be used to store the partially converted data before completing the conversion process.
54
+
`tmp::file` is a smart handle that manages a temporary file, ensuring its deletion when the handle goes out of scope. Upon creation, a `tmp::file` object generates a unique temporary file in the current user's temporary directory, opening it for both reading and writing.
66
55
67
-
4.**Testing:** Temporary files can be handy during testing scenarios where you need to create mock data or simulate certain conditions. For example, a unit test for a file upload feature might create a temporary file to mimic the behavior of an uploaded file without actually saving it permanently.
56
+
The temporary file is deleted of when either of the following happens:
57
+
- the `tmp::file` object is destroyed
58
+
- the `tmp::file` object is assigned another file using `operator=`
68
59
69
-
5.**Logging:** Temporary files can also be used for logging purposes, where log data is written to a temporary file before being processed and stored in a more permanent location. This can be helpful for managing log data efficiently in high-traffic systems.
60
+
`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.
70
61
71
-
`tmp::file` extends the `std::iostream` class and uses an instance of `std::filebuf` internally. It is essentially `std::fstream` without open/close methods
62
+
The example below demonstrates a usage of a tmp::file object to validate a request content and then move it to persistent storage; note that after the `move()` call, the temporary file will not be deleted, as its ownership is transferred to the specified location:
72
63
73
64
```cpp
74
65
#include<tmp/file>
75
-
...
76
-
{
77
-
// Create a unique temporary file
78
-
auto tmpfile = tmp::file(std::ios::binary);
79
-
80
-
// Write its contents and metadata
81
-
tmpfile << contents << metadata;
82
-
83
-
// Validate the file with an external validator
84
-
if (validate(tmpfile)) {
85
-
// Save the file to the persistent storage
86
-
tmpfile.move(storage / "new_file");
87
-
} else {
88
-
throw InvalidRequestError();
89
-
}
90
-
} // The file will be deleted when exiting the scope
0 commit comments