Added proper cleanup for BlobWriter
@@ -72,12 +72,15 @@ impl<'repo> Drop for Blob<'repo> {
/// A structure to represent a git writestream for blobs
pub struct BlobWriter<'repo> {
raw: *mut raw::git_writestream,
+ need_cleanup: bool,
_marker: marker::PhantomData<Object<'repo>>,
}
impl<'repo> BlobWriter<'repo> {
/// Finalize blob writing stream and write the blob to the object db
pub fn commit(&mut self) -> Result<Oid, Error> {
+ // After commit we already doesn't need cleanup on drop
+ self.need_cleanup = false;
let mut raw = raw::git_oid { id: [0; raw::GIT_OID_RAWSZ] };
unsafe {
try_call!(raw::git_blob_create_fromstream_commit(&mut raw, self.raw));
@@ -92,12 +95,22 @@ impl<'repo> Binding for BlobWriter<'repo> {
unsafe fn from_raw(raw: *mut raw::git_writestream) -> BlobWriter<'repo> {
BlobWriter {
raw: raw,
+ need_cleanup: true,
_marker: marker::PhantomData,
}
}
fn raw(&self) -> *mut raw::git_writestream { self.raw }
}
+impl<'repo> Drop for BlobWriter<'repo> {
+ fn drop(&mut self) {
+ // We need cleanup in case the stream has not been committed
+ if self.need_cleanup {
+ unsafe { ((*self.raw).free)(self.raw) }
+ }
+ }
+}
+
impl<'repo> io::Write for BlobWriter<'repo> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
unsafe {
