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
use crate::store::packed;
impl AsRef<[u8]> for packed::Buffer {
fn as_ref(&self) -> &[u8] {
match &self.data {
packed::Backing::InMemory(data) => &data[self.offset..],
packed::Backing::Mapped(map) => &map[self.offset..],
}
}
}
impl AsRef<[u8]> for packed::Backing {
fn as_ref(&self) -> &[u8] {
match self {
packed::Backing::InMemory(data) => data,
packed::Backing::Mapped(map) => map,
}
}
}
pub mod open {
use crate::store::packed;
use filebuffer::FileBuffer;
use std::path::PathBuf;
impl packed::Buffer {
pub fn open(path: impl Into<PathBuf>, use_memory_map_if_larger_than_bytes: u64) -> Result<Self, Error> {
let path = path.into();
let backing = if std::fs::metadata(&path)?.len() <= use_memory_map_if_larger_than_bytes {
packed::Backing::InMemory(std::fs::read(&path)?)
} else {
packed::Backing::Mapped(FileBuffer::open(&path)?)
};
let (offset, sorted) = {
let data = backing.as_ref();
if *data.get(0).unwrap_or(&b' ') == b'#' {
let (records, header) = packed::decode::header::<()>(data).map_err(|_| Error::HeaderParsing)?;
let offset = records.as_ptr() as usize - data.as_ptr() as usize;
(offset, header.sorted)
} else {
(0, false)
}
};
if !sorted {
return Err(Error::Unsorted);
}
Ok(packed::Buffer {
offset,
data: backing,
path,
})
}
}
mod error {
use quick_error::quick_error;
quick_error! {
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Error {
Unsorted {
display("The packed-refs file did not have a header or wasn't sorted.")
}
HeaderParsing {
display("The header could not be parsed, even though first line started with '#'")
}
Io(err: std::io::Error) {
display("The buffer could not be opened or read")
from()
source(err)
}
}
}
}
pub use error::Error;
}