| 1 |
use std::ffi::CStr; |
| 2 |
use std::path::Path; |
| 3 |
use std::{io, marker, mem, ptr}; |
| 4 |
|
| 5 |
use libc::c_void; |
| 6 |
|
| 7 |
use crate::odb::{write_pack_progress_cb, OdbPackwriterCb}; |
| 8 |
use crate::util::Binding; |
| 9 |
use crate::ObjectFormat; |
| 10 |
use crate::{raw, Error, IntoCString, Odb}; |
| 11 |
|
| 12 |
/// Struct representing the progress by an in-flight transfer. |
| 13 |
pub struct Progress<'a> { |
| 14 |
pub(crate) raw: ProgressState, |
| 15 |
pub(crate) _marker: marker::PhantomData<&'a raw::git_indexer_progress>, |
| 16 |
} |
| 17 |
|
| 18 |
pub(crate) enum ProgressState { |
| 19 |
Borrowed(*const raw::git_indexer_progress), |
| 20 |
Owned(raw::git_indexer_progress), |
| 21 |
} |
| 22 |
|
| 23 |
/// Callback to be invoked while indexing is in progress. |
| 24 |
/// |
| 25 |
/// This callback will be periodically called with updates to the progress of |
| 26 |
/// the indexing so far. The return value indicates whether the indexing or |
| 27 |
/// transfer should continue. A return value of `false` will cancel the |
| 28 |
/// indexing or transfer. |
| 29 |
/// |
| 30 |
/// * `progress` - the progress being made so far. |
| 31 |
pub type IndexerProgress<'a> = dyn FnMut(Progress<'_>) -> bool + 'a; |
| 32 |
|
| 33 |
impl<'a> Progress<'a> { |
| 34 |
/// Number of objects in the packfile being downloaded |
| 35 |
pub fn total_objects(&self) -> usize { |
| 36 |
unsafe { (*self.raw()).total_objects as usize } |
| 37 |
} |
| 38 |
/// Received objects that have been hashed |
| 39 |
pub fn indexed_objects(&self) -> usize { |
| 40 |
unsafe { (*self.raw()).indexed_objects as usize } |
| 41 |
} |
| 42 |
/// Objects which have been downloaded |
| 43 |
pub fn received_objects(&self) -> usize { |
| 44 |
unsafe { (*self.raw()).received_objects as usize } |
| 45 |
} |
| 46 |
/// Locally-available objects that have been injected in order to fix a thin |
| 47 |
/// pack. |
| 48 |
pub fn local_objects(&self) -> usize { |
| 49 |
unsafe { (*self.raw()).local_objects as usize } |
| 50 |
} |
| 51 |
/// Number of deltas in the packfile being downloaded |
| 52 |
pub fn total_deltas(&self) -> usize { |
| 53 |
unsafe { (*self.raw()).total_deltas as usize } |
| 54 |
} |
| 55 |
/// Received deltas that have been hashed. |
| 56 |
pub fn indexed_deltas(&self) -> usize { |
| 57 |
unsafe { (*self.raw()).indexed_deltas as usize } |
| 58 |
} |
| 59 |
/// Size of the packfile received up to now |
| 60 |
pub fn received_bytes(&self) -> usize { |
| 61 |
unsafe { (*self.raw()).received_bytes as usize } |
| 62 |
} |
| 63 |
|
| 64 |
/// Convert this to an owned version of `Progress`. |
| 65 |
pub fn to_owned(&self) -> Progress<'static> { |
| 66 |
Progress { |
| 67 |
raw: ProgressState::Owned(unsafe { *self.raw() }), |
| 68 |
_marker: marker::PhantomData, |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl<'a> Binding for Progress<'a> { |
| 74 |
type Raw = *const raw::git_indexer_progress; |
| 75 |
unsafe fn from_raw(raw: *const raw::git_indexer_progress) -> Progress<'a> { |
| 76 |
Progress { |
| 77 |
raw: ProgressState::Borrowed(raw), |
| 78 |
_marker: marker::PhantomData, |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
fn raw(&self) -> *const raw::git_indexer_progress { |
| 83 |
match self.raw { |
| 84 |
ProgressState::Borrowed(raw) => raw, |
| 85 |
ProgressState::Owned(ref raw) => raw as *const _, |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/// Callback to be invoked while a transfer is in progress. |
| 91 |
/// |
| 92 |
/// This callback will be periodically called with updates to the progress of |
| 93 |
/// the transfer so far. The return value indicates whether the transfer should |
| 94 |
/// continue. A return value of `false` will cancel the transfer. |
| 95 |
/// |
| 96 |
/// * `progress` - the progress being made so far. |
| 97 |
#[deprecated( |
| 98 |
since = "0.11.0", |
| 99 |
note = "renamed to `IndexerProgress` to match upstream" |
| 100 |
)] |
| 101 |
#[allow(dead_code)] |
| 102 |
pub type TransportProgress<'a> = IndexerProgress<'a>; |
| 103 |
|
| 104 |
/// A stream to write and index a packfile |
| 105 |
/// |
| 106 |
/// This is equivalent to [`crate::OdbPackwriter`], but allows to store the pack |
| 107 |
/// and index at an arbitrary path. It also does not require access to an object |
| 108 |
/// database if, and only if, the pack file is self-contained (i.e. not "thin"). |
| 109 |
pub struct Indexer<'odb> { |
| 110 |
raw: *mut raw::git_indexer, |
| 111 |
progress: raw::git_indexer_progress, |
| 112 |
progress_payload_ptr: *mut OdbPackwriterCb<'odb>, |
| 113 |
} |
| 114 |
|
| 115 |
impl<'a> Indexer<'a> { |
| 116 |
/// Create a new indexer |
| 117 |
/// |
| 118 |
/// The [`Odb`] is used to resolve base objects when fixing thin packs. It |
| 119 |
/// can be `None` if no thin pack is expected, in which case missing bases |
| 120 |
/// will result in an error. |
| 121 |
/// |
| 122 |
/// `path` is the directory where the packfile should be stored. |
| 123 |
/// |
| 124 |
/// `mode` is the permissions to use for the output files, use `0` for defaults. |
| 125 |
/// |
| 126 |
/// If `verify` is `false`, the indexer will bypass object connectivity checks. |
| 127 |
/// |
| 128 |
/// This creates an indexer assuming SHA1 object format. Use |
| 129 |
/// [`Indexer::new_ext`] to specify a different format. |
| 130 |
pub fn new(odb: Option<&Odb<'a>>, path: &Path, mode: u32, verify: bool) -> Result<Self, Error> { |
| 131 |
Self::new_ext(odb, path, mode, verify, ObjectFormat::Sha1) |
| 132 |
} |
| 133 |
|
| 134 |
/// Creates a new indexer with a specific object format. |
| 135 |
/// |
| 136 |
/// See [`Indexer::new`] for more details. |
| 137 |
pub fn new_ext( |
| 138 |
odb: Option<&Odb<'a>>, |
| 139 |
path: &Path, |
| 140 |
mode: u32, |
| 141 |
verify: bool, |
| 142 |
format: ObjectFormat, |
| 143 |
) -> Result<Self, Error> { |
| 144 |
crate::init(); |
| 145 |
let path = path.into_c_string()?; |
| 146 |
|
| 147 |
let odb = odb.map(Binding::raw).unwrap_or_else(ptr::null_mut); |
| 148 |
|
| 149 |
let mut out = ptr::null_mut(); |
| 150 |
let progress_cb: raw::git_indexer_progress_cb = Some(write_pack_progress_cb); |
| 151 |
let progress_payload = Box::new(OdbPackwriterCb { cb: None }); |
| 152 |
let progress_payload_ptr = Box::into_raw(progress_payload); |
| 153 |
|
| 154 |
unsafe { |
| 155 |
let mut opts = mem::zeroed(); |
| 156 |
try_call!(raw::git_indexer_options_init( |
| 157 |
&mut opts, |
| 158 |
raw::GIT_INDEXER_OPTIONS_VERSION |
| 159 |
)); |
| 160 |
opts.progress_cb = progress_cb; |
| 161 |
opts.progress_cb_payload = progress_payload_ptr as *mut c_void; |
| 162 |
opts.verify = verify.into(); |
| 163 |
|
| 164 |
#[cfg(not(feature = "unstable-sha256"))] |
| 165 |
{ |
| 166 |
let _ = format; |
| 167 |
try_call!(raw::git_indexer_new(&mut out, path, mode, odb, &mut opts)); |
| 168 |
} |
| 169 |
#[cfg(feature = "unstable-sha256")] |
| 170 |
{ |
| 171 |
opts.mode = mode; |
| 172 |
opts.oid_type = format.raw(); |
| 173 |
opts.odb = odb; |
| 174 |
try_call!(raw::git_indexer_new(&mut out, path, &mut opts)); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
Ok(Self { |
| 179 |
raw: out, |
| 180 |
progress: Default::default(), |
| 181 |
progress_payload_ptr, |
| 182 |
}) |
| 183 |
} |
| 184 |
|
| 185 |
/// Finalize the pack and index |
| 186 |
/// |
| 187 |
/// Resolves any pending deltas and writes out the index file. The returned |
| 188 |
/// string is the hexadecimal checksum of the packfile, which is also used |
| 189 |
/// to name the pack and index files (`pack-<checksum>.pack` and |
| 190 |
/// `pack-<checksum>.idx` respectively). |
| 191 |
pub fn commit(mut self) -> Result<String, Error> { |
| 192 |
unsafe { |
| 193 |
try_call!(raw::git_indexer_commit(self.raw, &mut self.progress)); |
| 194 |
|
| 195 |
let name = CStr::from_ptr(raw::git_indexer_name(self.raw)); |
| 196 |
Ok(name.to_str().expect("pack name not utf8").to_owned()) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/// The callback through which progress is monitored. Be aware that this is |
| 201 |
/// called inline, so performance may be affected. |
| 202 |
pub fn progress<F>(&mut self, cb: F) -> &mut Self |
| 203 |
where |
| 204 |
F: FnMut(Progress<'_>) -> bool + 'a, |
| 205 |
{ |
| 206 |
let progress_payload = |
| 207 |
unsafe { &mut *(self.progress_payload_ptr as *mut OdbPackwriterCb<'_>) }; |
| 208 |
progress_payload.cb = Some(Box::new(cb) as Box<IndexerProgress<'a>>); |
| 209 |
|
| 210 |
self |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
impl io::Write for Indexer<'_> { |
| 215 |
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 216 |
unsafe { |
| 217 |
let ptr = buf.as_ptr() as *mut c_void; |
| 218 |
let len = buf.len(); |
| 219 |
|
| 220 |
let res = raw::git_indexer_append(self.raw, ptr, len, &mut self.progress); |
| 221 |
if res < 0 { |
| 222 |
Err(io::Error::new(io::ErrorKind::Other, Error::last_error(res))) |
| 223 |
} else { |
| 224 |
Ok(buf.len()) |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
fn flush(&mut self) -> io::Result<()> { |
| 230 |
Ok(()) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
impl Drop for Indexer<'_> { |
| 235 |
fn drop(&mut self) { |
| 236 |
unsafe { |
| 237 |
raw::git_indexer_free(self.raw); |
| 238 |
drop(Box::from_raw(self.progress_payload_ptr)) |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
#[cfg(test)] |
| 244 |
mod tests { |
| 245 |
use crate::{Buf, Indexer}; |
| 246 |
use std::io::prelude::*; |
| 247 |
|
| 248 |
#[test] |
| 249 |
fn indexer() { |
| 250 |
let (_td, repo_source) = crate::test::repo_init(); |
| 251 |
let (_td, repo_target) = crate::test::repo_init(); |
| 252 |
|
| 253 |
let mut progress_called = false; |
| 254 |
|
| 255 |
// Create an in-memory packfile |
| 256 |
let mut builder = t!(repo_source.packbuilder()); |
| 257 |
let mut buf = Buf::new(); |
| 258 |
let (commit_source_id, _tree) = crate::test::commit(&repo_source); |
| 259 |
t!(builder.insert_object(commit_source_id, None)); |
| 260 |
t!(builder.write_buf(&mut buf)); |
| 261 |
|
| 262 |
// Write it to the standard location in the target repo, but via indexer |
| 263 |
let odb = repo_source.odb().unwrap(); |
| 264 |
let mut indexer = Indexer::new( |
| 265 |
Some(&odb), |
| 266 |
repo_target.path().join("objects").join("pack").as_path(), |
| 267 |
0o644, |
| 268 |
true, |
| 269 |
) |
| 270 |
.unwrap(); |
| 271 |
indexer.progress(|_| { |
| 272 |
progress_called = true; |
| 273 |
true |
| 274 |
}); |
| 275 |
indexer.write(&buf).unwrap(); |
| 276 |
indexer.commit().unwrap(); |
| 277 |
|
| 278 |
// Assert that target repo picks it up as valid |
| 279 |
let commit_target = repo_target.find_commit(commit_source_id).unwrap(); |
| 280 |
assert_eq!(commit_target.id(), commit_source_id); |
| 281 |
assert!(progress_called); |
| 282 |
} |
| 283 |
|
| 284 |
#[test] |
| 285 |
#[cfg(feature = "unstable-sha256")] |
| 286 |
fn indexer_sha256() { |
| 287 |
let (_td, repo_source) = crate::test::repo_init_sha256(); |
| 288 |
let (_td, repo_target) = crate::test::repo_init_sha256(); |
| 289 |
|
| 290 |
let mut progress_called = false; |
| 291 |
|
| 292 |
// Create an in-memory packfile |
| 293 |
let mut builder = t!(repo_source.packbuilder()); |
| 294 |
let mut buf = Buf::new(); |
| 295 |
let (commit_source_id, _tree) = crate::test::commit(&repo_source); |
| 296 |
t!(builder.insert_object(commit_source_id, None)); |
| 297 |
t!(builder.write_buf(&mut buf)); |
| 298 |
|
| 299 |
// Write it to the standard location in the target repo, but via indexer |
| 300 |
let odb = repo_source.odb().unwrap(); |
| 301 |
let mut indexer = Indexer::new_ext( |
| 302 |
Some(&odb), |
| 303 |
repo_target.path().join("objects").join("pack").as_path(), |
| 304 |
0o644, |
| 305 |
true, |
| 306 |
crate::ObjectFormat::Sha256, |
| 307 |
) |
| 308 |
.unwrap(); |
| 309 |
indexer.progress(|_| { |
| 310 |
progress_called = true; |
| 311 |
true |
| 312 |
}); |
| 313 |
indexer.write(&buf).unwrap(); |
| 314 |
indexer.commit().unwrap(); |
| 315 |
|
| 316 |
// Assert that target repo picks it up as valid |
| 317 |
let commit_target = repo_target.find_commit(commit_source_id).unwrap(); |
| 318 |
assert_eq!(commit_target.id(), commit_source_id); |
| 319 |
assert!(progress_called); |
| 320 |
} |
| 321 |
} |