Skip to content

Commit ff18e0b

Browse files
nathaniel-broughByron
authored andcommitted
feat: Add packed::Buffer::from_bytes().
This way the source of the `packed-refs` file contents can be arbitrary.
1 parent 3be3b15 commit ff18e0b

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

gix-ref/src/store/packed/buffer.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,8 @@ pub mod open {
2626

2727
/// Initialization
2828
impl packed::Buffer {
29-
/// Open the file at `path` and map it into memory if the file size is larger than `use_memory_map_if_larger_than_bytes`.
30-
///
31-
/// In order to allow fast lookups and optimizations, the contents of the packed refs must be sorted.
32-
/// If that's not the case, they will be sorted on the fly with the data being written into a memory buffer.
33-
pub fn open(path: PathBuf, use_memory_map_if_larger_than_bytes: u64) -> Result<Self, Error> {
29+
fn open_with_backing(backing: packed::Backing, path: PathBuf) -> Result<Self, Error> {
3430
let (backing, offset) = {
35-
let backing = if std::fs::metadata(&path)?.len() <= use_memory_map_if_larger_than_bytes {
36-
packed::Backing::InMemory(std::fs::read(&path)?)
37-
} else {
38-
packed::Backing::Mapped(
39-
// SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file.
40-
#[allow(unsafe_code)]
41-
unsafe {
42-
Mmap::map(&std::fs::File::open(&path)?)?
43-
},
44-
)
45-
};
46-
4731
let (offset, sorted) = {
4832
let mut input = backing.as_ref();
4933
if *input.first().unwrap_or(&b' ') == b'#' {
@@ -84,6 +68,34 @@ pub mod open {
8468
path,
8569
})
8670
}
71+
72+
/// Open the file at `path` and map it into memory if the file size is larger than `use_memory_map_if_larger_than_bytes`.
73+
///
74+
/// In order to allow fast lookups and optimizations, the contents of the packed refs must be sorted.
75+
/// If that's not the case, they will be sorted on the fly with the data being written into a memory buffer.
76+
pub fn open(path: PathBuf, use_memory_map_if_larger_than_bytes: u64) -> Result<Self, Error> {
77+
let backing = if std::fs::metadata(&path)?.len() <= use_memory_map_if_larger_than_bytes {
78+
packed::Backing::InMemory(std::fs::read(&path)?)
79+
} else {
80+
packed::Backing::Mapped(
81+
// SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file.
82+
#[allow(unsafe_code)]
83+
unsafe {
84+
Mmap::map(&std::fs::File::open(&path)?)?
85+
},
86+
)
87+
};
88+
Self::open_with_backing(backing, path)
89+
}
90+
91+
/// Open a buffer from `bytes`, which is the content of a typical `packed-refs` file.
92+
///
93+
/// In order to allow fast lookups and optimizations, the contents of the packed refs must be sorted.
94+
/// If that's not the case, they will be sorted on the fly.
95+
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
96+
let backing = packed::Backing::InMemory(bytes.into());
97+
Self::open_with_backing(backing, PathBuf::from("<memory>"))
98+
}
8799
}
88100

89101
mod error {

0 commit comments

Comments
 (0)