MCPcopy Create free account
hub / github.com/aws/clock-bound / wipe

Method wipe

clock-bound/src/shm/writer.rs:184–235  ·  view source on GitHub ↗

Initialize the file backing the memory segment. Zero out the file up to segsize, but write out header information such the readers can access it. Note that both the layout version number and the generation number are set to 0, which makes this file not usable to retrieve clock error bound data yet.

(path: &Path, segsize: usize)

Source from the content-addressed store, hash-verified

182 /// access it. Note that both the layout version number and the generation number are set to 0,
183 /// which makes this file not usable to retrieve clock error bound data yet.
184 fn wipe(path: &Path, segsize: usize) -> std::io::Result<()> {
185 // Attempt at creating intermediate directories, but do expect that the base permissions
186 // are set correctly.
187 if let Some(parent) = path.parent() {
188 match parent.to_str() {
189 Some("") => (), // This would be a relative path without parent
190 Some(_) => fs::create_dir_all(parent)?,
191 None => {
192 return Err(Error::other("Failed to extract parent dir name"));
193 }
194 }
195 }
196
197 // Opens the file in write-only mode. Create a file if it does not exist, and truncate it
198 // if it does.
199 let mut file = std::fs::File::create(path)?;
200
201 // In theory, usize may not fit within a u32. In practice, we
202 let size: u32 = match segsize.try_into() {
203 Ok(size) => size, // it did fit
204 Err(e) => {
205 return Err(std::io::Error::other(format!(
206 "Failed to convert segment size {segsize:?} into u32 {e:?}"
207 )));
208 }
209 };
210
211 // Write the ShmHeader
212 file.write_u32::<NativeEndian>(SHM_MAGIC[0])?; // Magic number 0
213 file.write_u32::<NativeEndian>(SHM_MAGIC[1])?; // Magic number 1
214 file.write_u32::<NativeEndian>(size)?; // Segsize
215 file.write_u16::<NativeEndian>(0)?; // Version
216 file.write_u16::<NativeEndian>(0)?; // Generation
217
218 // Zero the rest of the segment
219 let remaining = segsize - size_of::<ShmHeader>();
220 let buf = vec![0; remaining];
221 file.write_all(&buf)?;
222
223 // Make sure the amount of bytes written matches the segment size
224 let pos = file.stream_position()?;
225 if pos > size.into() {
226 return Err(std::io::Error::other(format!(
227 "SHM Writer implementation error: wrote {pos:?} bytes but segsize is {size:?} bytes"
228 )));
229 }
230
231 // Sync all and drop (close) the descriptor
232 file.sync_all()?;
233
234 Ok(())
235 }
236
237 /// Open and map the file at the given path to memory.
238 ///

Callers

nothing calls this directly

Calls 1

createFunction · 0.85

Tested by

no test coverage detected