oxedyne/fe2o3/fe2o3_o3db_sync/src/file/live.rs
1.2 KiB, 9 runs
created by r1870400018:795, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::{ |
| 2 | prelude::*, |
| 3 | file::floc::FileNum, |
| 4 | }; |
| 5 | |
| 6 | |
| 7 | use std::{ |
| 8 | fs::File, |
| 9 | path::PathBuf, |
| 10 | }; |
| 11 | |
| 12 | #[derive(Debug, Default)] |
| 13 | pub struct LiveFile { |
| 14 | pub path: PathBuf, |
| 15 | pub file: Option<File>, |
| 16 | pub size: u64, |
| 17 | } |
| 18 | |
| 19 | impl LiveFile { |
| 20 | |
| 21 | pub fn get_file_len(&self) -> Outcome<u64> { |
| 22 | match &self.file { |
| 23 | Some(file) => match file.metadata() { |
| 24 | Err(e) => Err(err!(e, |
| 25 | "Could not read metadata for file {:?}.", self.path; |
| 26 | IO, File, Read)), |
| 27 | Ok(metadata) => Ok(metadata.len()), |
| 28 | }, |
| 29 | None => Err(err!( |
| 30 | "Attempt to get length of a live file {:?} but the file is None.", self.path; |
| 31 | Invalid, Input)), |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub fn close(&mut self) { |
| 36 | if let Some(file) = std::mem::take(&mut self.file) { |
| 37 | drop(file); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | } |
| 42 | |
| 43 | /// Acts as a cache to reduce repeated reformulation of the live file path and reacquisition of |
| 44 | /// the files and the data file length. |
| 45 | #[derive(Debug, Default)] |
| 46 | pub struct LivePair { |
| 47 | pub fnum: FileNum, |
| 48 | pub dat: LiveFile, |
| 49 | pub ind: LiveFile, |
| 50 | } |
| 51 | |
| 52 | impl LivePair { |
| 53 | pub fn close(&mut self) { |
| 54 | self.dat.close(); |
| 55 | self.ind.close(); |
| 56 | } |
| 57 | } |