rust: add a small wrapper around the hashfile code
Our new binary object map code avoids needing to be intimately involved with file handling by simply writing data to an object implement Write. This makes it very easy to test by writing to a Cursor wrapping a Vec for tests, and thus decouples it from intimate knowledge about how we handle files. However, we will actually want to write our data to an actual file, since that's the most practical way to persist data. Implement a wrapper around the hashfile code that implements the Write trait so that we can write our object map into a file. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Feb 7, 2026 at 20:04 UTC
39e4dcf77dfe65f9342000894c1868075ed12415
4 files changed
+84
Makefile
+1
@@ -1545,6 +1545,7 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
1545
1546
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
1547
1548
+RUST_SOURCES += src/csum_file.rs
1549
RUST_SOURCES += src/hash.rs
1550
RUST_SOURCES += src/lib.rs
1551
RUST_SOURCES += src/loose.rs
src/csum_file.rs
new
+81
@@ -0,0 +1,81 @@
1
+// This program is free software; you can redistribute it and/or modify
2
+// it under the terms of the GNU General Public License as published by
3
+// the Free Software Foundation: version 2 of the License, dated June 1991.
4
+//
5
+// This program is distributed in the hope that it will be useful,
6
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
7
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8
+// GNU General Public License for more details.
9
+//
10
+// You should have received a copy of the GNU General Public License along
11
+// with this program; if not, see <https://www.gnu.org/licenses/>.
12
+
13
+use crate::hash::{HashAlgorithm, GIT_MAX_RAWSZ};
14
+use std::ffi::CStr;
15
+use std::io::{self, Write};
16
+use std::os::raw::c_void;
17
+
18
+/// A writer that can write files identified by their hash or containing a trailing hash.
19
+pub struct HashFile {
20
+ ptr: *mut c_void,
21
+ algo: HashAlgorithm,
22
+}
23
+
24
+impl HashFile {
25
+ /// Create a new HashFile.
26
+ ///
27
+ /// The hash used will be `algo`, its name should be in `name`, and an open file descriptor
28
+ /// pointing to that file should be in `fd`.
29
+ pub fn new(algo: HashAlgorithm, fd: i32, name: &CStr) -> HashFile {
30
+ HashFile {
31
+ ptr: unsafe { c::hashfd(algo.hash_algo_ptr(), fd, name.as_ptr()) },
32
+ algo,
33
+ }
34
+ }
35
+
36
+ /// Finalize this HashFile instance.
37
+ ///
38
+ /// Returns the hash computed over the data.
39
+ pub fn finalize(self, component: u32, flags: u32) -> Vec<u8> {
40
+ let mut result = vec![0u8; GIT_MAX_RAWSZ];
41
+ unsafe { c::finalize_hashfile(self.ptr, result.as_mut_ptr(), component, flags) };
42
+ result.truncate(self.algo.raw_len());
43
+ result
44
+ }
45
+}
46
+
47
+impl Write for HashFile {
48
+ fn write(&mut self, data: &[u8]) -> io::Result<usize> {
49
+ for chunk in data.chunks(u32::MAX as usize) {
50
+ unsafe {
51
+ c::hashwrite(
52
+ self.ptr,
53
+ chunk.as_ptr() as *const c_void,
54
+ chunk.len() as u32,
55
+ )
56
+ };
57
+ }
58
+ Ok(data.len())
59
+ }
60
+
61
+ fn flush(&mut self) -> io::Result<()> {
62
+ unsafe { c::hashflush(self.ptr) };
63
+ Ok(())
64
+ }
65
+}
66
+
67
+pub mod c {
68
+ use std::os::raw::{c_char, c_int, c_void};
69
+
70
+ extern "C" {
71
+ pub fn hashfd(algop: *const c_void, fd: i32, name: *const c_char) -> *mut c_void;
72
+ pub fn hashwrite(f: *mut c_void, data: *const c_void, len: u32);
73
+ pub fn hashflush(f: *mut c_void);
74
+ pub fn finalize_hashfile(
75
+ f: *mut c_void,
76
+ data: *mut u8,
77
+ component: u32,
78
+ flags: u32,
79
+ ) -> c_int;
80
+ }
81
+}
src/lib.rs
+1
@@ -1,3 +1,4 @@
1
+pub mod csum_file;
2
pub mod hash;
3
pub mod loose;
4
pub mod varint;
src/meson.build
+1
@@ -1,4 +1,5 @@
1
libgit_rs_sources = [
2
+ 'csum_file.rs',
3
'hash.rs',
4
'lib.rs',
5
'loose.rs',