1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::iter::Peekable;
use git_features::hash;
use crate::data::input;
pub struct EntriesToBytesIter<I: Iterator, W> {
pub input: Peekable<I>,
output: W,
trailer: Option<git_hash::ObjectId>,
data_version: crate::data::Version,
num_entries: u32,
is_done: bool,
hash_kind: git_hash::Kind,
}
impl<I, W> EntriesToBytesIter<I, W>
where
I: Iterator<Item = Result<input::Entry, input::Error>>,
W: std::io::Read + std::io::Write + std::io::Seek,
{
pub fn new(input: I, output: W, version: crate::data::Version, hash_kind: git_hash::Kind) -> Self {
assert!(
matches!(version, crate::data::Version::V2),
"currently only pack version 2 can be written",
);
assert!(
matches!(hash_kind, git_hash::Kind::Sha1),
"currently only Sha1 is supported, right now we don't know how other hashes are encoded",
);
EntriesToBytesIter {
input: input.peekable(),
output,
hash_kind,
num_entries: 0,
trailer: None,
data_version: version,
is_done: false,
}
}
pub fn digest(&self) -> Option<git_hash::ObjectId> {
self.trailer
}
fn next_inner(&mut self, entry: input::Entry) -> Result<input::Entry, input::Error> {
if self.num_entries == 0 {
let header_bytes = crate::data::header::encode(self.data_version, 0);
self.output.write_all(&header_bytes[..])?;
}
self.num_entries += 1;
entry
.header
.write_to(entry.decompressed_size as u64, &mut self.output)?;
std::io::copy(
&mut &*entry
.compressed
.as_deref()
.expect("caller must configure generator to keep compressed bytes"),
&mut self.output,
)?;
Ok(entry)
}
fn write_header_and_digest(&mut self, last_entry: &mut input::Entry) -> Result<(), input::Error> {
let num_bytes_written = self.output.stream_position()?;
self.output.seek(std::io::SeekFrom::Start(0))?;
let header_bytes = crate::data::header::encode(self.data_version, self.num_entries);
self.output.write_all(&header_bytes[..])?;
self.output.flush()?;
self.output.seek(std::io::SeekFrom::Start(0))?;
let interrupt_never = std::sync::atomic::AtomicBool::new(false);
let digest = hash::bytes(
&mut self.output,
num_bytes_written as usize,
self.hash_kind,
&mut git_features::progress::Discard,
&interrupt_never,
)?;
self.output.write_all(digest.as_slice())?;
self.output.flush()?;
self.is_done = true;
last_entry.trailer = Some(digest);
self.trailer = Some(digest);
Ok(())
}
}
impl<I, W> Iterator for EntriesToBytesIter<I, W>
where
I: Iterator<Item = Result<input::Entry, input::Error>>,
W: std::io::Read + std::io::Write + std::io::Seek,
{
type Item = Result<input::Entry, input::Error>;
fn next(&mut self) -> Option<Self::Item> {
if self.is_done {
return None;
}
self.input.next().map(|res| match res {
Ok(entry) => self.next_inner(entry).and_then(|mut entry| {
if self.input.peek().is_none() {
self.write_header_and_digest(&mut entry).map(|_| entry)
} else {
Ok(entry)
}
}),
Err(err) => {
self.is_done = true;
Err(err)
}
})
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.input.size_hint()
}
}