History

Use new deflate state, in preparation for full parsing

Sebastian Thiel committed 8 years ago

Commit 05971da1 parent 4a8ef202 files · +63 −78Browse files

Unified · Split

2 files
  1. lib/git-odb/src/lib.rs +48 −0
  2. lib/git-odb/src/loose.rs +15 −78
lib/git-odb/src/lib.rs+48−0
@@ -5,6 +5,54 @@ extern crate miniz_oxide; · 4 unmodified lines
55 extern crate smallvec;
66 extern crate walkdir;
77
8+mod deflate {
9+ use failure::Error;
10+ use miniz_oxide::inflate::core::DecompressorOxide;
11+ use std::io::Cursor;
12+ use miniz_oxide::inflate::core::{decompress,
13+ inflate_flags::{TINFL_FLAG_PARSE_ZLIB_HEADER,
14+ TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF}};
15+
16+ pub struct State {
17+ inner: DecompressorOxide,
18+ }
19+
20+ impl Default for State {
21+ fn default() -> Self {
22+ State {
23+ inner: DecompressorOxide::default(),
24+ }
25+ }
26+ }
27+
28+ impl State {
29+ pub fn once(
30+ &mut self,
31+ rbuf: &[u8],
32+ out: &mut Cursor<&mut [u8]>,
33+ ) -> Result<(usize, usize), Error> {
34+ let (status, read_in, read_out) = decompress(
35+ &mut self.inner,
36+ rbuf,
37+ out,
38+ TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF,
39+ );
40+
41+ use miniz_oxide::inflate::TINFLStatus::*;
42+ match status {
43+ Failed | FailedCannotMakeProgress | BadParam | Adler32Mismatch | NeedsMoreInput => {
44+ bail!(
45+ "Could not decode zip stream for reading header, status was '{:?}'",
46+ status
47+ )
48+ }
49+ Done | HasMoreOutput => {}
50+ };
51+ Ok((read_in, read_out))
52+ }
53+ }
54+}
55+
856 pub mod object {
957 use std::str;
1058
lib/git-odb/src/loose.rs+15−78
@@ -1,15 +1,11 @@
11 use object::{Id, Kind};
22
3−use std::path::PathBuf;
4−
53 use walkdir::WalkDir;
64 use failure::{Error, ResultExt};
75 use hex::{FromHex, ToHex};
8−use miniz_oxide::inflate::core::{decompress, DecompressorOxide,
9− inflate_flags::{TINFL_FLAG_PARSE_ZLIB_HEADER,
10− TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF}};
116 use smallvec::SmallVec;
12−use std::{fs::File, io::{Cursor, Read}};
7+use std::{fs::File, io::{Cursor, Read}, path::PathBuf};
8+use deflate;
139
1410 const HEADER_READ_COMPRESSED_BYTES: usize = 512;
1511
@@ -21,57 +17,7 @@ pub struct Object { · 5 unmodified lines
2117 pub kind: Kind,
2218 pub size: usize,
2319 data: SmallVec<[u8; HEADER_READ_COMPRESSED_BYTES]>,
24−}
25−
26−mod deflate {
27− use failure::Error;
28− use miniz_oxide::inflate::core::DecompressorOxide;
29− use std::io::Cursor;
30− use miniz_oxide::inflate::core::{decompress,
31− inflate_flags::{TINFL_FLAG_PARSE_ZLIB_HEADER,
32− TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF}};
33−
34− pub struct State {
35− inner: DecompressorOxide,
36− pub read_in: usize,
37− pub read_out: usize,
38− }
39−
40− impl Default for State {
41− fn default() -> Self {
42− State {
43− inner: DecompressorOxide::default(),
44− read_in: 0,
45− read_out: 0,
46− }
47− }
48− }
49−
50− impl State {
51− pub fn once(&mut self, rbuf: &[u8], out: &mut Cursor<&mut [u8]>) -> Result<usize, Error> {
52− let (status, read_in, read_out) = decompress(
53− &mut self.inner,
54− rbuf,
55− out,
56− TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF,
57− );
58−
59− self.read_in = read_in;
60− self.read_out = read_out;
61−
62− use miniz_oxide::inflate::TINFLStatus::*;
63− match status {
64− Failed | FailedCannotMakeProgress | BadParam | Adler32Mismatch | NeedsMoreInput => {
65− bail!(
66− "Could not decode zip stream for reading header, status was '{:?}'",
67− status
68− )
69− }
70− Done | HasMoreOutput => {}
71− };
72− Ok(read_out)
73− }
74− }
20+ deflate: deflate::State,
7521 }
7622
7723 impl Db {
@@ -125,31 +71,21 @@ impl Db { · 47 unmodified lines
12571 path
12672 };
12773
74+ let mut deflate = deflate::State::default();
12875 let mut out = [0; HEADER_READ_COMPRESSED_BYTES];
129− let read_out = {
130− let mut rbuf = [0; HEADER_READ_COMPRESSED_BYTES];
76+ let mut rbuf = [0; HEADER_READ_COMPRESSED_BYTES];
77+ let (_read_in, read_out) = {
13178 let bytes_read = File::open(&path)?.read(&mut rbuf[..])?;
132− let mut state = DecompressorOxide::default();
13379 let mut out = Cursor::new(&mut out[..]);
13480
135− let (status, _read_in, read_out) = decompress(
136− &mut state,
137− &rbuf[..bytes_read],
138− &mut out,
139− TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF,
140− );
141− use miniz_oxide::inflate::TINFLStatus::*;
142− match status {
143− Failed | FailedCannotMakeProgress | BadParam | Adler32Mismatch | NeedsMoreInput => {
144− bail!(
145− "Could not decode zip stream for reading header in '{}', zip status was '{:?}'",
146− path.display(),
147− status
148− )
149− }
150− Done | HasMoreOutput => {}
151− };
152− read_out
81+ deflate
82+ .once(&rbuf[..bytes_read], &mut out)
83+ .with_context(|_| {
84+ format!(
85+ "Could not decode zip stream for reading header in '{}'",
86+ path.display()
87+ )
88+ })?
15389 };
15490
15591 parse::header(&out[..read_out])
@@ -157,6 +93,7 @@ impl Db { · 1 unmodified line
15793 kind,
15894 size,
15995 data: SmallVec::from_buf(out),
96+ deflate,
16097 })
16198 .with_context(|_| {
16299 format!(