aboutsummaryrefslogtreecommitdiff
path: root/src/csum_file.rs
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2026-03-12 10:56:02 -0700
committerJunio C Hamano <gitster@pobox.com>2026-03-12 10:56:02 -0700
commit8194f1795bf0ca36f245adccc84bc86ab2aa90d1 (patch)
treec8d1bc1cc14dd618832cd9d2a5a932c33b27fd22 /src/csum_file.rs
parent7f19e4e1b6a3ad259e2ed66033e01e03b8b74c5e (diff)
parentd49f23ae2f9def3c9065738bccbb9ca8dfb4b0f0 (diff)
downloadgit-8194f1795bf0ca36f245adccc84bc86ab2aa90d1.tar.xz
Merge branch 'bc/sha1-256-interop-02'
The code to maintain mapping between object names in multiple hash functions is being added, written in Rust. * bc/sha1-256-interop-02: object-file-convert: always make sure object ID algo is valid rust: add a small wrapper around the hashfile code rust: add a new binary object map format rust: add functionality to hash an object rust: add a build.rs script for tests rust: fix linking binaries with cargo hash: expose hash context functions to Rust write-or-die: add an fsync component for the object map csum-file: define hashwrite's count as a uint32_t rust: add additional helpers for ObjectID hash: add a function to look up hash algo structs rust: add a hash algorithm abstraction rust: add a ObjectID struct hash: use uint32_t for object_id algorithm conversion: don't crash when no destination algo repository: require Rust support for interoperability
Diffstat (limited to 'src/csum_file.rs')
-rw-r--r--src/csum_file.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/csum_file.rs b/src/csum_file.rs
new file mode 100644
index 0000000000..7f2c6c4fcb
--- /dev/null
+++ b/src/csum_file.rs
@@ -0,0 +1,81 @@
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation: version 2 of the License, dated June 1991.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, see <https://www.gnu.org/licenses/>.
+
+use crate::hash::{HashAlgorithm, GIT_MAX_RAWSZ};
+use std::ffi::CStr;
+use std::io::{self, Write};
+use std::os::raw::c_void;
+
+/// A writer that can write files identified by their hash or containing a trailing hash.
+pub struct HashFile {
+ ptr: *mut c_void,
+ algo: HashAlgorithm,
+}
+
+impl HashFile {
+ /// Create a new HashFile.
+ ///
+ /// The hash used will be `algo`, its name should be in `name`, and an open file descriptor
+ /// pointing to that file should be in `fd`.
+ pub fn new(algo: HashAlgorithm, fd: i32, name: &CStr) -> HashFile {
+ HashFile {
+ ptr: unsafe { c::hashfd(algo.hash_algo_ptr(), fd, name.as_ptr()) },
+ algo,
+ }
+ }
+
+ /// Finalize this HashFile instance.
+ ///
+ /// Returns the hash computed over the data.
+ pub fn finalize(self, component: u32, flags: u32) -> Vec<u8> {
+ let mut result = vec![0u8; GIT_MAX_RAWSZ];
+ unsafe { c::finalize_hashfile(self.ptr, result.as_mut_ptr(), component, flags) };
+ result.truncate(self.algo.raw_len());
+ result
+ }
+}
+
+impl Write for HashFile {
+ fn write(&mut self, data: &[u8]) -> io::Result<usize> {
+ for chunk in data.chunks(u32::MAX as usize) {
+ unsafe {
+ c::hashwrite(
+ self.ptr,
+ chunk.as_ptr() as *const c_void,
+ chunk.len() as u32,
+ )
+ };
+ }
+ Ok(data.len())
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ unsafe { c::hashflush(self.ptr) };
+ Ok(())
+ }
+}
+
+pub mod c {
+ use std::os::raw::{c_char, c_int, c_void};
+
+ extern "C" {
+ pub fn hashfd(algop: *const c_void, fd: i32, name: *const c_char) -> *mut c_void;
+ pub fn hashwrite(f: *mut c_void, data: *const c_void, len: u32);
+ pub fn hashflush(f: *mut c_void);
+ pub fn finalize_hashfile(
+ f: *mut c_void,
+ data: *mut u8,
+ component: u32,
+ flags: u32,
+ ) -> c_int;
+ }
+}