|
| 1 | +use std::path::PathBuf; |
1 | 2 | use std::{
|
2 | 3 | convert::{TryFrom, TryInto},
|
3 | 4 | path::Path,
|
@@ -62,24 +63,13 @@ impl File {
|
62 | 63 | pub fn at(path: impl AsRef<Path>) -> Result<File, Error> {
|
63 | 64 | Self::try_from(path.as_ref())
|
64 | 65 | }
|
65 |
| -} |
66 | 66 |
|
67 |
| -impl TryFrom<&Path> for File { |
68 |
| - type Error = Error; |
69 |
| - |
70 |
| - fn try_from(path: &Path) -> Result<Self, Self::Error> { |
71 |
| - let data = std::fs::File::open(path) |
72 |
| - .and_then(|file| { |
73 |
| - // SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file. |
74 |
| - #[allow(unsafe_code)] |
75 |
| - unsafe { |
76 |
| - Mmap::map(&file) |
77 |
| - } |
78 |
| - }) |
79 |
| - .map_err(|e| Error::Io { |
80 |
| - err: e, |
81 |
| - path: path.to_owned(), |
82 |
| - })?; |
| 67 | + /// A lower-level constructor which constructs a new instance directly from the mapping in `data`, |
| 68 | + /// assuming that it originated from `path`. |
| 69 | + /// |
| 70 | + /// Note that `path` is only used for verification of the hash its basename contains, but otherwise |
| 71 | + /// is not of importance. |
| 72 | + pub fn new(data: memmap2::Mmap, path: PathBuf) -> Result<File, Error> { |
83 | 73 | let data_size = data.len();
|
84 | 74 | if data_size < MIN_FILE_SIZE {
|
85 | 75 | return Err(Error::Corrupt(
|
@@ -240,13 +230,33 @@ impl TryFrom<&Path> for File {
|
240 | 230 | extra_edges_list_range,
|
241 | 231 | fan,
|
242 | 232 | oid_lookup_offset,
|
243 |
| - path: path.to_owned(), |
| 233 | + path, |
244 | 234 | hash_len: object_hash.len_in_bytes(),
|
245 | 235 | object_hash,
|
246 | 236 | })
|
247 | 237 | }
|
248 | 238 | }
|
249 | 239 |
|
| 240 | +impl TryFrom<&Path> for File { |
| 241 | + type Error = Error; |
| 242 | + |
| 243 | + fn try_from(path: &Path) -> Result<Self, Self::Error> { |
| 244 | + let data = std::fs::File::open(path) |
| 245 | + .and_then(|file| { |
| 246 | + // SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file. |
| 247 | + #[allow(unsafe_code)] |
| 248 | + unsafe { |
| 249 | + Mmap::map(&file) |
| 250 | + } |
| 251 | + }) |
| 252 | + .map_err(|e| Error::Io { |
| 253 | + err: e, |
| 254 | + path: path.to_owned(), |
| 255 | + })?; |
| 256 | + Self::new(data, path.to_owned()) |
| 257 | + } |
| 258 | +} |
| 259 | + |
250 | 260 | // Copied from gix-odb/pack/index/init.rs
|
251 | 261 | fn read_fan(d: &[u8]) -> ([u32; FAN_LEN], usize) {
|
252 | 262 | let mut fan = [0; FAN_LEN];
|
|
0 commit comments