Raw
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 }