Skip to content

Commit ffcb110

Browse files
committed
adapt to changes in gix-object
1 parent 9283a9d commit ffcb110

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

gix-odb/src/find.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ mod header {
104104
}
105105
}
106106

107-
impl From<(usize, gix_object::Kind)> for Header {
108-
fn from((object_size, kind): (usize, gix_object::Kind)) -> Self {
107+
impl From<(u64, gix_object::Kind)> for Header {
108+
fn from((object_size, kind): (u64, gix_object::Kind)) -> Self {
109109
Header::Loose {
110110
kind,
111-
size: object_size as u64,
111+
size: object_size,
112112
}
113113
}
114114
}

gix-odb/src/sink.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
22
cell::RefCell,
3-
convert::TryInto,
43
io::{self, Write},
54
};
65

@@ -24,11 +23,10 @@ impl crate::traits::Write for Sink {
2423
fn write_stream(
2524
&self,
2625
kind: gix_object::Kind,
27-
size: u64,
26+
mut size: u64,
2827
from: &mut dyn io::Read,
2928
) -> Result<gix_hash::ObjectId, crate::write::Error> {
30-
let mut size = size.try_into().expect("object size to fit into usize");
31-
let mut buf = [0u8; 8096];
29+
let mut buf = [0u8; u16::MAX as usize];
3230
let header = gix_object::encode::loose_header(kind, size);
3331

3432
let possibly_compress = |buf: &[u8]| -> io::Result<()> {
@@ -43,11 +41,11 @@ impl crate::traits::Write for Sink {
4341
possibly_compress(&header).map_err(Box::new)?;
4442

4543
while size != 0 {
46-
let bytes = size.min(buf.len());
44+
let bytes = (size as usize).min(buf.len());
4745
from.read_exact(&mut buf[..bytes]).map_err(Box::new)?;
4846
hasher.update(&buf[..bytes]);
4947
possibly_compress(&buf[..bytes]).map_err(Box::new)?;
50-
size -= bytes;
48+
size -= bytes as u64;
5149
}
5250
if let Some(compressor) = self.compressor.as_ref() {
5351
let mut c = compressor.borrow_mut();

gix-odb/src/store_impls/loose/find.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ pub enum Error {
1414
path: PathBuf,
1515
},
1616
#[error("file at '{path}' showed invalid size of inflated data, expected {expected}, got {actual}")]
17-
SizeMismatch {
18-
actual: usize,
19-
expected: usize,
20-
path: PathBuf,
21-
},
17+
SizeMismatch { actual: u64, expected: u64, path: PathBuf },
2218
#[error(transparent)]
2319
Decode(#[from] gix_object::decode::LooseHeaderDecodeError),
20+
#[error("Cannot store {size} in memory as it's not representable")]
21+
OutOfMemory { size: u64 },
2422
#[error("Could not {action} data at '{path}'")]
2523
Io {
2624
source: std::io::Error,
@@ -137,7 +135,7 @@ impl Store {
137135

138136
/// Return only the decompressed size of the object and its kind without fully reading it into memory as tuple of `(size, kind)`.
139137
/// Returns `None` if `id` does not exist in the database.
140-
pub fn try_header(&self, id: &gix_hash::oid) -> Result<Option<(usize, gix_object::Kind)>, Error> {
138+
pub fn try_header(&self, id: &gix_hash::oid) -> Result<Option<(u64, gix_object::Kind)>, Error> {
141139
const BUF_SIZE: usize = 256;
142140
let mut buf = [0_u8; BUF_SIZE];
143141
let path = hash_path(id, self.path.clone());
@@ -224,16 +222,17 @@ impl Store {
224222
let decompressed_body_bytes_sans_header =
225223
decompressed_start + header_size..decompressed_start + consumed_out;
226224

227-
if consumed_out != size + header_size {
225+
if consumed_out as u64 != size + header_size as u64 {
228226
return Err(Error::SizeMismatch {
229-
expected: size + header_size,
230-
actual: consumed_out,
227+
expected: size + header_size as u64,
228+
actual: consumed_out as u64,
231229
path,
232230
});
233231
}
234232
buf.copy_within(decompressed_body_bytes_sans_header, 0);
235233
} else {
236-
buf.resize(bytes_read + size + header_size, 0);
234+
let new_len = bytes_read as u64 + size + header_size as u64;
235+
buf.resize(new_len.try_into().map_err(|_| Error::OutOfMemory { size: new_len })?, 0);
237236
{
238237
let (input, output) = buf.split_at_mut(bytes_read);
239238
let num_decompressed_bytes = zlib::stream::inflate::read(
@@ -246,17 +245,21 @@ impl Store {
246245
action: "deflate",
247246
path: path.to_owned(),
248247
})?;
249-
if num_decompressed_bytes + consumed_out != size + header_size {
248+
if num_decompressed_bytes as u64 + consumed_out as u64 != size + header_size as u64 {
250249
return Err(Error::SizeMismatch {
251-
expected: size + header_size,
252-
actual: num_decompressed_bytes + consumed_out,
250+
expected: size + header_size as u64,
251+
actual: num_decompressed_bytes as u64 + consumed_out as u64,
253252
path,
254253
});
255254
}
256255
};
257256
buf.copy_within(decompressed_start + header_size.., 0);
258257
}
259-
buf.resize(size, 0);
258+
buf.resize(
259+
size.try_into()
260+
.expect("BUG: here the size is already confirmed to fit into memory"),
261+
0,
262+
);
260263
Ok(gix_object::Data { kind, data: buf })
261264
}
262265
}

gix-odb/src/store_impls/loose/write.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{convert::TryInto, fs, io, io::Write, path::PathBuf};
1+
use std::{fs, io, io::Write, path::PathBuf};
22

33
use gix_features::{hash, zlib::stream::deflate};
44
use gix_object::WriteTo;
@@ -48,7 +48,7 @@ impl crate::traits::Write for Store {
4848
/// This will cost at least 4 IO operations.
4949
fn write_buf(&self, kind: gix_object::Kind, from: &[u8]) -> Result<gix_hash::ObjectId, crate::write::Error> {
5050
let mut to = self.dest().map_err(Box::new)?;
51-
to.write_all(&gix_object::encode::loose_header(kind, from.len()))
51+
to.write_all(&gix_object::encode::loose_header(kind, from.len() as u64))
5252
.map_err(|err| Error::Io {
5353
source: err,
5454
message: "write header to tempfile in",
@@ -74,15 +74,12 @@ impl crate::traits::Write for Store {
7474
mut from: &mut dyn io::Read,
7575
) -> Result<gix_hash::ObjectId, crate::write::Error> {
7676
let mut to = self.dest().map_err(Box::new)?;
77-
to.write_all(&gix_object::encode::loose_header(
78-
kind,
79-
size.try_into().expect("object size to fit into usize"),
80-
))
81-
.map_err(|err| Error::Io {
82-
source: err,
83-
message: "write header to tempfile in",
84-
path: self.path.to_owned(),
85-
})?;
77+
to.write_all(&gix_object::encode::loose_header(kind, size))
78+
.map_err(|err| Error::Io {
79+
source: err,
80+
message: "write header to tempfile in",
81+
path: self.path.to_owned(),
82+
})?;
8683

8784
io::copy(&mut from, &mut to)
8885
.map_err(|err| Error::Io {

gix-odb/tests/odb/store/loose.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ cjHJZXWmV4CcRfmLsXzU8s2cR9A0DBvOxhPD1TlKC2JhBFXigjuL9U4Rbq9tdegB
420420
let id = id?;
421421
let expected = db.try_find(&id, &mut buf)?.expect("exists");
422422
let (size, kind) = db.try_header(&id)?.expect("header exists");
423-
assert_eq!(size, expected.data.len());
423+
assert_eq!(size, expected.data.len() as u64);
424424
assert_eq!(kind, expected.kind);
425425
}
426426
Ok(())

0 commit comments

Comments
 (0)