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
use std::{hash::Hash, io, io::SeekFrom, path::PathBuf, sync::Arc};
use git_tempfile::handle::Writable;
#[derive(Debug, Clone)]
pub struct Options {
pub thread_limit: Option<usize>,
pub iteration_mode: crate::data::input::Mode,
pub index_version: crate::index::Version,
pub object_hash: git_hash::Kind,
}
impl Default for Options {
fn default() -> Self {
Options {
thread_limit: None,
iteration_mode: crate::data::input::Mode::Verify,
index_version: Default::default(),
object_hash: Default::default(),
}
}
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Outcome {
pub index: crate::index::write::Outcome,
pub pack_version: crate::data::Version,
pub object_hash: git_hash::Kind,
pub index_path: Option<PathBuf>,
pub data_path: Option<PathBuf>,
pub keep_path: Option<PathBuf>,
}
impl Outcome {
pub fn to_bundle(&self) -> Option<Result<crate::Bundle, crate::bundle::init::Error>> {
self.index_path
.as_ref()
.map(|path| crate::Bundle::at(path, self.object_hash))
}
}
pub(crate) type SharedTempFile = Arc<parking_lot::Mutex<std::io::BufWriter<git_tempfile::Handle<Writable>>>>;
pub(crate) struct PassThrough<R> {
pub reader: R,
pub writer: Option<SharedTempFile>,
}
impl<R> io::Read for PassThrough<R>
where
R: io::Read,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let bytes_read = self.reader.read(buf)?;
if let Some(writer) = self.writer.as_mut() {
use std::io::Write;
writer.lock().write_all(&buf[..bytes_read])?;
}
Ok(bytes_read)
}
}
impl<R> io::BufRead for PassThrough<R>
where
R: io::BufRead,
{
fn fill_buf(&mut self) -> io::Result<&[u8]> {
self.reader.fill_buf()
}
fn consume(&mut self, amt: usize) {
self.reader.consume(amt)
}
}
pub(crate) struct LockWriter {
pub writer: SharedTempFile,
}
impl io::Write for LockWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.writer.lock().write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.writer.lock().flush()
}
}
impl io::Read for LockWriter {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.writer.lock().get_mut().read(buf)
}
}
impl io::Seek for LockWriter {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.writer.lock().seek(pos)
}
}