-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathfile.rs
More file actions
66 lines (54 loc) · 1.86 KB
/
Copy pathfile.rs
File metadata and controls
66 lines (54 loc) · 1.86 KB
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
//! Page-indexed file I/O: read/write whole pages by page number, plus the
//! special page-0 header. Deliberately thin — this is the only place that
//! touches `std::fs`, so higher layers deal in pages, not offsets.
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use crate::error::Result;
use crate::sql::pager::header::{DbHeader, decode_header, encode_header};
use crate::sql::pager::page::PAGE_SIZE;
pub struct FileStorage {
file: File,
}
impl FileStorage {
pub fn new(file: File) -> Self {
Self { file }
}
pub fn read_header(&mut self) -> Result<DbHeader> {
self.file.seek(SeekFrom::Start(0))?;
let mut buf = [0u8; PAGE_SIZE];
self.file.read_exact(&mut buf)?;
decode_header(&buf)
}
pub fn write_header(&mut self, header: &DbHeader) -> Result<()> {
let buf = encode_header(header);
self.file.seek(SeekFrom::Start(0))?;
self.file.write_all(&buf)?;
Ok(())
}
pub fn flush(&mut self) -> Result<()> {
self.file.flush()?;
self.file.sync_all()?;
Ok(())
}
/// Low-level byte I/O helpers used by the `Pager` to bypass the per-page
/// encoding when it's managing its own raw buffers.
pub fn seek_to(&mut self, offset: u64) -> Result<()> {
self.file.seek(SeekFrom::Start(offset))?;
Ok(())
}
pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
self.file.read_exact(buf)?;
Ok(())
}
pub fn write_all(&mut self, buf: &[u8]) -> Result<()> {
self.file.write_all(buf)?;
Ok(())
}
/// Shrinks the backing file to `page_count` pages. Any bytes beyond the
/// new length are discarded.
pub fn truncate_to_pages(&mut self, page_count: u32) -> Result<()> {
self.file
.set_len((page_count as u64) * (PAGE_SIZE as u64))?;
Ok(())
}
}