@@ -26,24 +26,8 @@ pub mod open {
26
26
27
27
/// Initialization
28
28
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 > {
34
30
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
-
47
31
let ( offset, sorted) = {
48
32
let mut input = backing. as_ref ( ) ;
49
33
if * input. first ( ) . unwrap_or ( & b' ' ) == b'#' {
@@ -84,6 +68,34 @@ pub mod open {
84
68
path,
85
69
} )
86
70
}
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
+ }
87
99
}
88
100
89
101
mod error {
0 commit comments