1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! # Runtime Tempfile Manager
//!
//! This module provides a thread-safe, globally accessible interface for
//! managing a temporary file that exists only for the duration of the
//! application.
//!
//! ## Architecture
//! The module uses the **RAII principle** (Resource Acquisition Is Initialization).
//! The file is created upon first access and automatically deleted as soon as
//! the application terminates normally or aborts due to a panic.
//!
//! ### Core Components:
//! - `OnceLock`: Guarantees thread-safe lazy initialization.
//! - `Mutex`: Enables synchronized access from multiple threads.
//! - `NamedTempFile`: Manages the physical file and its deletion.
//!
//! ## Example
//!
//! ```rust
//! fn main() {
//! write_to_temp("Hello World!").unwrap();
//! write_to_temp("\nSecond Line").unwrap();
//! write_to_temp("\nLast Line").unwrap();
//!
//! match read_from_temp() {
//! Ok(content) => println!("Content of file: \n{}", content),
//! Err(e) => eprintln!("Error with writing to: {}", e),
//! }
//!
//! println!("Before:\n{}", read_from_temp().unwrap());
//!
//! remove_line_from_temp(1).unwrap();
//! println!("Delete line 1:\n{}", read_from_temp().unwrap());
//!
//! clear_temp_file().unwrap();
//! println!("After Clear: '{}'", read_from_temp().unwrap());
//!
//! write_to_temp("Hello World!").unwrap();
//! println!("The file will be deleted as soon as the program closes.");
//! }
//! ```
/**
* temp_app_file.rs
*
* (C) Copyright 2026 AtlantisOS Project
* by @NachtsternBuild
*
* License: GNU GENERAL PUBLIC LICENSE Version 3
*/
use ;
use ;
use NamedTempFile;
/// Global storage for the temporary file.
/// The file is automatically deleted when this object is dropped.
static TEMP_FILE: = new;
/// Initializes the temporary file or returns a reference to the existing one.
///
/// # Panics
/// This function panics if the temporary file cannot be created in the system temp directory
///
/// Writes data to the temporary file.
///
/// The data is written to the end of the file. After writing,
/// the buffer is flushed to prevent data loss.
///
/// # Arguments
/// * `data` - A string slice to be written to the file.
///
/// # Errors
/// Returns a `std::io::Result::Err` if the write operation fails.
///
/// # Example
/// ```rust
/// write_to_temp("Important runtime data").unwrap();
/// ```
/// Reads the entire contents of the temporary file.
///
/// This function sets the file pointer to the beginning (position 0) before reading.
///
/// # Returns
/// An `io::Result<String>` containing the complete contents of the file.
///
/// # Errors
/// Returns an error if the file cannot be read or
/// contains invalid UTF-8.
///
/// # Example
///
/// ```rust
/// match read_from_temp() {
/// Ok(content) => println!("Content of file: \n{}", content),
/// Err(e) => eprintln!("Error with writing to: {}", e),
/// }
/// ```
/// Deletes the entire contents of the temporary file.
///
/// The file remains in existence and reserved, but its size is set to 0
/// and the file pointer is reset to the beginning.
///
/// # Errors
/// Returns an error if the file cannot be truncated.
///
/// # Example
///
/// ```rust
/// clear_temp_file().unwrap();
/// ```
/// Deletes a specific line from the temporary file based on the line number.
///
/// **Performance Note:** Since files in the file system cannot simply be
/// "truncated" in the middle, this function must read and rewrite the entire file.
/// For very large files, this function should be used sparingly.
///
/// # Arguments
/// * `line_number` - The 0-based index number of the line to be removed.
///
/// # Errors
/// Returns an error if the file cannot be read or rewritten.
///
/// # Example
///
/// ```rust
/// remove_line_from_temp(1).unwrap();
/// ```