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
|
use crate::{common::temp_file, server::Stream};
use log::error;
use std::{
fs::{self, File},
io::{BufReader, BufWriter, Read, Seek, SeekFrom, Write},
ops::Index,
path::PathBuf,
slice::SliceIndex,
};
pub struct FileHandle {
buffer: Vec<u8>,
size: usize,
tmp: Option<(PathBuf, BufWriter<File>)>,
reader: Option<BufReader<File>>,
dirty: bool,
}
impl FileHandle {
pub fn empty() -> std::io::Result<Self> {
let (temp_path, temp_file) = temp_file()?;
Ok(Self {
buffer: Vec::new(),
size: 0,
tmp: Some((temp_path, BufWriter::new(temp_file))),
reader: None,
dirty: false,
})
}
pub fn new_rw(reader: BufReader<File>) -> std::io::Result<Self> {
// TODO: Copy read to tmp
let (temp_path, temp_file) = temp_file()?;
Ok(Self {
buffer: Vec::new(),
size: 0,
tmp: Some((temp_path, BufWriter::new(temp_file))),
reader: Some(reader),
dirty: false,
})
}
pub fn new_ro(reader: BufReader<File>) -> Self {
Self {
buffer: Vec::new(),
size: 0,
tmp: None,
reader: Some(reader),
dirty: false,
}
}
pub fn is_dirty(&self) -> bool {
self.dirty
}
pub fn write(&mut self, data: &[u8], offset: usize) -> Option<usize> {
let Some((_, ref mut tmp)) = self.tmp else {
error!("Tried to write to a RO file handle");
return None;
};
tmp.seek(SeekFrom::Start(offset as u64))
.inspect_err(|e| error!("Seek error: {e}"))
.ok()?;
let written = tmp
.write(data)
.inspect_err(|e| error!("Write error: {e}"))
.ok()?;
self.dirty = true;
Some(written)
}
pub fn read(&mut self, offset: usize, size: usize) -> Vec<u8> {
let Some(mut reader) = self.reader else {
error!("Tried to read from an empty file handle");
return Vec::new();
};
let mut buf = Vec::with_capacity(size);
// TODO: error handling...
reader.seek(SeekFrom::Start(offset as u64)).unwrap();
reader.read_exact(&mut buf).unwrap();
buf
}
pub fn clear(&mut self) {
self.tmp.as_mut().map(|b| b.1.flush());
self.buffer.clear();
self.dirty = false;
}
}
impl Drop for FileHandle {
fn drop(&mut self) {
let Some((ref temp_path, _)) = self.tmp else {
return;
};
if let Err(e) = fs::remove_file(temp_path) {
error!("Couldn't delete temp file {temp_path:?}: {e}");
}
}
}
|