git_internal/internal/pack/
decode.rs

1use std::io::{self, BufRead, Cursor, ErrorKind, Read};
2use std::path::PathBuf;
3use std::sync::Arc;
4use std::sync::atomic::{AtomicUsize, Ordering};
5use std::thread::{self, JoinHandle};
6use std::time::Instant;
7
8use axum::Error;
9use bytes::Bytes;
10use flate2::bufread::ZlibDecoder;
11use futures_util::{Stream, StreamExt};
12use threadpool::ThreadPool;
13use tokio::sync::mpsc::UnboundedSender;
14use uuid::Uuid;
15
16use crate::errors::GitError;
17use crate::hash::SHA1;
18
19use crate::internal::metadata::{EntryMeta, MetaAttached};
20use crate::zstdelta;
21
22use crate::internal::object::types::ObjectType;
23
24use super::cache_object::CacheObjectInfo;
25use crate::internal::pack::cache::_Cache;
26use crate::internal::pack::cache::Caches;
27use crate::internal::pack::cache_object::{CacheObject, MemSizeRecorder};
28use crate::internal::pack::channel_reader::StreamBufReader;
29use crate::internal::pack::entry::Entry;
30use crate::internal::pack::waitlist::Waitlist;
31use crate::internal::pack::wrapper::Wrapper;
32use crate::internal::pack::{DEFAULT_TMP_DIR, Pack, utils};
33use crate::utils::CountingReader;
34
35/// For the convenience of passing parameters
36struct SharedParams {
37    pub pool: Arc<ThreadPool>,
38    pub waitlist: Arc<Waitlist>,
39    pub caches: Arc<Caches>,
40    pub cache_objs_mem_size: Arc<AtomicUsize>,
41    pub callback: Arc<dyn Fn(MetaAttached<Entry, EntryMeta>) + Sync + Send>,
42}
43
44impl Drop for Pack {
45    fn drop(&mut self) {
46        if self.clean_tmp {
47            self.caches.remove_tmp_dir();
48        }
49    }
50}
51
52impl Pack {
53    /// # Parameters
54    /// - `thread_num`: The number of threads to use for decoding and cache, `None` mean use the number of logical CPUs.
55    ///   It can't be zero, or panic <br>
56    /// - `mem_limit`: The maximum size of the memory cache in bytes, or None for unlimited.
57    ///   The 80% of it will be used for [Caches]  <br>
58    ///   ​**Not very accurate, because of memory alignment and other reasons, overuse about 15%** <br>
59    /// - `temp_path`: The path to a directory for temporary files, default is "./.cache_temp" <br>
60    ///   For example, thread_num = 4 will use up to 8 threads (4 for decoding and 4 for cache) <br>
61    /// - `clean_tmp`: whether to remove temp directory when Pack is dropped
62    pub fn new(
63        thread_num: Option<usize>,
64        mem_limit: Option<usize>,
65        temp_path: Option<PathBuf>,
66        clean_tmp: bool,
67    ) -> Self {
68        let mut temp_path = temp_path.unwrap_or(PathBuf::from(DEFAULT_TMP_DIR));
69        // add 8 random characters as subdirectory, check if the directory exists
70        loop {
71            let sub_dir = Uuid::new_v4().to_string()[..8].to_string();
72            temp_path.push(sub_dir);
73            if !temp_path.exists() {
74                break;
75            }
76            temp_path.pop();
77        }
78        let thread_num = thread_num.unwrap_or_else(num_cpus::get);
79        let cache_mem_size = mem_limit.map(|mem_limit| mem_limit * 4 / 5);
80        Pack {
81            number: 0,
82            signature: SHA1::default(),
83            objects: Vec::new(),
84            pool: Arc::new(ThreadPool::new(thread_num)),
85            waitlist: Arc::new(Waitlist::new()),
86            caches: Arc::new(Caches::new(cache_mem_size, temp_path, thread_num)),
87            mem_limit,
88            cache_objs_mem: Arc::new(AtomicUsize::default()),
89            clean_tmp,
90        }
91    }
92
93    /// Checks and reads the header of a Git pack file.
94    ///
95    /// This function reads the first 12 bytes of a pack file, which include the b"PACK" magic identifier,
96    /// the version number, and the number of objects in the pack. It verifies that the magic identifier
97    /// is correct and that the version number is 2 (which is the version currently supported by Git).
98    /// It also collects these header bytes for later use, such as for hashing the entire pack file.
99    ///
100    /// # Parameters
101    /// * `pack` - A mutable reference to an object implementing the `Read` trait,
102    ///   representing the source of the pack file data (e.g., file, memory stream).
103    ///
104    /// # Returns
105    /// A `Result` which is:
106    /// * `Ok((u32, Vec<u8>))`: On successful reading and validation of the header, returns a tuple where:
107    ///     - The first element is the number of objects in the pack file (`u32`).
108    ///     - The second element is a vector containing the bytes of the pack file header (`Vec<u8>`).
109    /// * `Err(GitError)`: On failure, returns a [`GitError`] with a description of the issue.
110    ///
111    /// # Errors
112    /// This function can return an error in the following situations:
113    /// * If the pack file does not start with the "PACK" magic identifier.
114    /// * If the pack file's version number is not 2.
115    /// * If there are any issues reading from the provided `pack` source.
116    pub fn check_header(pack: &mut impl BufRead) -> Result<(u32, Vec<u8>), GitError> {
117        // A vector to store the header data for hashing later
118        let mut header_data = Vec::new();
119
120        // Read the first 4 bytes which should be "PACK"
121        let mut magic = [0; 4];
122        // Read the magic "PACK" identifier
123        let result = pack.read_exact(&mut magic);
124        match result {
125            Ok(_) => {
126                // Store these bytes for later
127                header_data.extend_from_slice(&magic);
128
129                // Check if the magic bytes match "PACK"
130                if magic != *b"PACK" {
131                    // If not, return an error indicating invalid pack header
132                    return Err(GitError::InvalidPackHeader(format!(
133                        "{},{},{},{}",
134                        magic[0], magic[1], magic[2], magic[3]
135                    )));
136                }
137            }
138            Err(e) => {
139                // If there is an error in reading, return a GitError
140                return Err(GitError::InvalidPackFile(format!(
141                    "Error reading magic identifier: {e}"
142                )));
143            }
144        }
145
146        // Read the next 4 bytes for the version number
147        let mut version_bytes = [0; 4];
148        let result = pack.read_exact(&mut version_bytes); // Read the version number
149        match result {
150            Ok(_) => {
151                // Store these bytes
152                header_data.extend_from_slice(&version_bytes);
153
154                // Convert the version bytes to an u32 integer
155                let version = u32::from_be_bytes(version_bytes);
156                if version != 2 {
157                    // Git currently supports version 2, so error if not version 2
158                    return Err(GitError::InvalidPackFile(format!(
159                        "Version Number is {version}, not 2"
160                    )));
161                }
162            }
163            Err(e) => {
164                // If there is an error in reading, return a GitError
165                return Err(GitError::InvalidPackFile(format!(
166                    "Error reading version number: {e}"
167                )));
168            }
169        }
170
171        // Read the next 4 bytes for the number of objects in the pack
172        let mut object_num_bytes = [0; 4];
173        // Read the number of objects
174        let result = pack.read_exact(&mut object_num_bytes);
175        match result {
176            Ok(_) => {
177                // Store these bytes
178                header_data.extend_from_slice(&object_num_bytes);
179                // Convert the object number bytes to an u32 integer
180                let object_num = u32::from_be_bytes(object_num_bytes);
181                // Return the number of objects and the header data for further processing
182                Ok((object_num, header_data))
183            }
184            Err(e) => {
185                // If there is an error in reading, return a GitError
186                Err(GitError::InvalidPackFile(format!(
187                    "Error reading object number: {e}"
188                )))
189            }
190        }
191    }
192
193    /// Decompresses data from a given Read and BufRead source using Zlib decompression.
194    ///
195    /// # Parameters
196    /// * `pack`: A source that implements both Read and BufRead traits (e.g., file, network stream).
197    /// * `expected_size`: The expected decompressed size of the data.
198    ///
199    /// # Returns
200    /// Returns a `Result` containing either:
201    /// * A tuple with a `Vec<u8>` of the decompressed data and the total number of input bytes processed,
202    /// * Or a `GitError` in case of a mismatch in expected size or any other reading error.
203    ///
204    pub fn decompress_data(
205        pack: &mut (impl BufRead + Send),
206        expected_size: usize,
207    ) -> Result<(Vec<u8>, usize), GitError> {
208        // Create a buffer with the expected size for the decompressed data
209        let mut buf = Vec::with_capacity(expected_size);
210
211        let mut counting_reader = CountingReader::new(pack);
212        // Create a new Zlib decoder with the original data
213        //let mut deflate = ZlibDecoder::new(pack);
214        let mut deflate = ZlibDecoder::new(&mut counting_reader);
215        // Attempt to read data to the end of the buffer
216        match deflate.read_to_end(&mut buf) {
217            Ok(_) => {
218                // Check if the length of the buffer matches the expected size
219                if buf.len() != expected_size {
220                    Err(GitError::InvalidPackFile(format!(
221                        "The object size {} does not match the expected size {}",
222                        buf.len(),
223                        expected_size
224                    )))
225                } else {
226                    // If everything is as expected, return the buffer, the original data, and the total number of input bytes processed
227                    let actual_input_bytes = counting_reader.bytes_read as usize;
228                    Ok((buf, actual_input_bytes))
229                }
230            }
231            Err(e) => {
232                // If there is an error in reading, return a GitError
233                Err(GitError::InvalidPackFile(format!(
234                    "Decompression error: {e}"
235                )))
236            }
237        }
238    }
239
240    /// Decodes a pack object from a given Read and BufRead source and returns the object as a [`CacheObject`].
241    ///
242    /// # Parameters
243    /// * `pack`: A source that implements both Read and BufRead traits.
244    /// * `offset`: A mutable reference to the current offset within the pack.
245    ///
246    /// # Returns
247    /// Returns a `Result` containing either:
248    /// * A tuple of the next offset in the pack and the original compressed data as `Vec<u8>`,
249    /// * Or a `GitError` in case of any reading or decompression error.
250    ///
251    pub fn decode_pack_object(
252        pack: &mut (impl BufRead + Send),
253        offset: &mut usize,
254    ) -> Result<CacheObject, GitError> {
255        let init_offset = *offset;
256
257        // Attempt to read the type and size, handle potential errors
258        let (type_bits, size) = match utils::read_type_and_varint_size(pack, offset) {
259            Ok(result) => result,
260            Err(e) => {
261                // Handle the error e.g., by logging it or converting it to GitError
262                // and then return from the function
263                return Err(GitError::InvalidPackFile(format!("Read error: {e}")));
264            }
265        };
266
267        // Check if the object type is valid
268        let t = ObjectType::from_u8(type_bits)?;
269
270        match t {
271            ObjectType::Commit | ObjectType::Tree | ObjectType::Blob | ObjectType::Tag => {
272                let (data, raw_size) = Pack::decompress_data(pack, size)?;
273                *offset += raw_size;
274                Ok(CacheObject::new_for_undeltified(t, data, init_offset))
275            }
276            ObjectType::OffsetDelta | ObjectType::OffsetZstdelta => {
277                let (delta_offset, bytes) = utils::read_offset_encoding(pack).unwrap();
278                *offset += bytes;
279
280                let (data, raw_size) = Pack::decompress_data(pack, size)?;
281                *offset += raw_size;
282
283                // Count the base object offset: the current offset - delta offset
284                let base_offset = init_offset
285                    .checked_sub(delta_offset as usize)
286                    .ok_or_else(|| {
287                        GitError::InvalidObjectInfo("Invalid OffsetDelta offset".to_string())
288                    })
289                    .unwrap();
290
291                let mut reader = Cursor::new(&data);
292                let (_, final_size) = utils::read_delta_object_size(&mut reader)?;
293
294                let obj_info = match t {
295                    ObjectType::OffsetDelta => {
296                        CacheObjectInfo::OffsetDelta(base_offset, final_size)
297                    }
298                    ObjectType::OffsetZstdelta => {
299                        CacheObjectInfo::OffsetZstdelta(base_offset, final_size)
300                    }
301                    _ => unreachable!(),
302                };
303                Ok(CacheObject {
304                    info: obj_info,
305                    offset: init_offset,
306                    data_decompressed: data,
307                    mem_recorder: None,
308                    is_delta_in_pack: true,
309                })
310            }
311            ObjectType::HashDelta => {
312                // Read 20 bytes to get the reference object SHA1 hash
313                let ref_sha1 = SHA1::from_stream(pack).unwrap();
314                // Offset is incremented by 20 bytes
315                *offset += SHA1::SIZE;
316
317                let (data, raw_size) = Pack::decompress_data(pack, size)?;
318                *offset += raw_size;
319
320                let mut reader = Cursor::new(&data);
321                let (_, final_size) = utils::read_delta_object_size(&mut reader)?;
322
323                Ok(CacheObject {
324                    info: CacheObjectInfo::HashDelta(ref_sha1, final_size),
325                    offset: init_offset,
326                    data_decompressed: data,
327                    mem_recorder: None,
328                    is_delta_in_pack: true,
329                })
330            }
331        }
332    }
333
334    /// Decodes a pack file from a given Read and BufRead source, for each object in the pack,
335    /// it decodes the object and processes it using the provided callback function.
336    ///
337    /// # Parameters
338    /// * pack_id_callback: A callback that seed pack_file sha1 for updating database
339    ///
340    pub fn decode<F, C>(
341        &mut self,
342        pack: &mut (impl BufRead + Send),
343        callback: F,
344        pack_id_callback: Option<C>,
345    ) -> Result<(), GitError>
346    where
347        F: Fn(MetaAttached<Entry, EntryMeta>) + Sync + Send + 'static,
348        C: FnOnce(SHA1) + Send + 'static,
349    {
350        let time = Instant::now();
351        let mut last_update_time = time.elapsed().as_millis();
352        let log_info = |_i: usize, pack: &Pack| {
353            tracing::info!(
354                "time {:.2} s \t decode: {:?} \t dec-num: {} \t cah-num: {} \t Objs: {} MB \t CacheUsed: {} MB",
355                time.elapsed().as_millis() as f64 / 1000.0,
356                _i,
357                pack.pool.queued_count(),
358                pack.caches.queued_tasks(),
359                pack.cache_objs_mem_used() / 1024 / 1024,
360                pack.caches.memory_used() / 1024 / 1024
361            );
362        };
363        let callback = Arc::new(callback);
364
365        let caches = self.caches.clone();
366        let mut reader = Wrapper::new(io::BufReader::new(pack));
367
368        let result = Pack::check_header(&mut reader);
369        match result {
370            Ok((object_num, _)) => {
371                self.number = object_num as usize;
372            }
373            Err(e) => {
374                return Err(e);
375            }
376        }
377        tracing::info!("The pack file has {} objects", self.number);
378        let mut offset: usize = 12;
379        let mut i = 0;
380        while i < self.number {
381            // log per 1000 objects and 1 second
382            if i % 1000 == 0 {
383                let time_now = time.elapsed().as_millis();
384                if time_now - last_update_time > 1000 {
385                    log_info(i, self);
386                    last_update_time = time_now;
387                }
388            }
389            // 3 parts: Waitlist + TheadPool + Caches
390            // hardcode the limit of the tasks of threads_pool queue, to limit memory
391            while self.pool.queued_count() > 2000
392                || self
393                    .mem_limit
394                    .map(|limit| self.memory_used() > limit)
395                    .unwrap_or(false)
396            {
397                thread::yield_now();
398            }
399            let r: Result<CacheObject, GitError> =
400                Pack::decode_pack_object(&mut reader, &mut offset);
401            match r {
402                Ok(mut obj) => {
403                    obj.set_mem_recorder(self.cache_objs_mem.clone());
404                    obj.record_mem_size();
405
406                    // Wrapper of Arc Params, for convenience to pass
407                    let params = Arc::new(SharedParams {
408                        pool: self.pool.clone(),
409                        waitlist: self.waitlist.clone(),
410                        caches: self.caches.clone(),
411                        cache_objs_mem_size: self.cache_objs_mem.clone(),
412                        callback: callback.clone(),
413                    });
414
415                    let caches = caches.clone();
416                    let waitlist = self.waitlist.clone();
417                    self.pool.execute(move || {
418                        match obj.info {
419                            CacheObjectInfo::BaseObject(_, _) => {
420                                Self::cache_obj_and_process_waitlist(params, obj);
421                            }
422                            CacheObjectInfo::OffsetDelta(base_offset, _)
423                            | CacheObjectInfo::OffsetZstdelta(base_offset, _) => {
424                                if let Some(base_obj) = caches.get_by_offset(base_offset) {
425                                    Self::process_delta(params, obj, base_obj);
426                                } else {
427                                    // You can delete this 'if' block ↑, because there are Second check in 'else'
428                                    // It will be more readable, but the performance will be slightly reduced
429                                    waitlist.insert_offset(base_offset, obj);
430                                    // Second check: prevent that the base_obj thread has finished before the waitlist insert
431                                    if let Some(base_obj) = caches.get_by_offset(base_offset) {
432                                        Self::process_waitlist(params, base_obj);
433                                    }
434                                }
435                            }
436                            CacheObjectInfo::HashDelta(base_ref, _) => {
437                                if let Some(base_obj) = caches.get_by_hash(base_ref) {
438                                    Self::process_delta(params, obj, base_obj);
439                                } else {
440                                    waitlist.insert_ref(base_ref, obj);
441                                    if let Some(base_obj) = caches.get_by_hash(base_ref) {
442                                        Self::process_waitlist(params, base_obj);
443                                    }
444                                }
445                            }
446                        }
447                    });
448                }
449                Err(e) => {
450                    return Err(e);
451                }
452            }
453            i += 1;
454        }
455        log_info(i, self);
456        let render_hash = reader.final_hash();
457        self.signature = SHA1::from_stream(&mut reader).unwrap();
458
459        if render_hash != self.signature {
460            return Err(GitError::InvalidPackFile(format!(
461                "The pack file hash {} does not match the trailer hash {}",
462                render_hash, self.signature
463            )));
464        }
465
466        let end = utils::is_eof(&mut reader);
467        if !end {
468            return Err(GitError::InvalidPackFile(
469                "The pack file is not at the end".to_string(),
470            ));
471        }
472
473        self.pool.join(); // wait for all threads to finish
474
475        // send pack id for metadata
476        if let Some(pack_callback) = pack_id_callback {
477            pack_callback(self.signature);
478        }
479        // !Attention: Caches threadpool may not stop, but it's not a problem (garbage file data)
480        // So that files != self.number
481        assert_eq!(self.waitlist.map_offset.len(), 0);
482        assert_eq!(self.waitlist.map_ref.len(), 0);
483        assert_eq!(self.number, caches.total_inserted());
484        tracing::info!(
485            "The pack file has been decoded successfully, takes: [ {:?} ]",
486            time.elapsed()
487        );
488        self.caches.clear(); // clear cached objects & stop threads
489        assert_eq!(self.cache_objs_mem_used(), 0); // all the objs should be dropped until here
490
491        // impl in Drop Trait
492        // if self.clean_tmp {
493        //     self.caches.remove_tmp_dir();
494        // }
495
496        Ok(())
497    }
498
499    /// Decode a Pack in a new thread and send the CacheObjects while decoding.
500    /// <br> Attention: It will consume the `pack` and return in a JoinHandle.
501    pub fn decode_async(
502        mut self,
503        mut pack: impl BufRead + Send + 'static,
504        sender: UnboundedSender<Entry>,
505    ) -> JoinHandle<Pack> {
506        thread::spawn(move || {
507            self.decode(
508                &mut pack,
509                move |entry| {
510                    if let Err(e) = sender.send(entry.inner) {
511                        eprintln!("Channel full, failed to send entry: {e:?}");
512                    }
513                },
514                None::<fn(SHA1)>,
515            )
516            .unwrap();
517            self
518        })
519    }
520
521    /// Decodes a `Pack` from a `Stream` of `Bytes`, and sends the `Entry` while decoding.
522    pub async fn decode_stream(
523        mut self,
524        mut stream: impl Stream<Item = Result<Bytes, Error>> + Unpin + Send + 'static,
525        sender: UnboundedSender<MetaAttached<Entry, EntryMeta>>,
526        pack_hash_send: Option<UnboundedSender<SHA1>>,
527    ) -> Self {
528        let (tx, rx) = std::sync::mpsc::channel();
529        let mut reader = StreamBufReader::new(rx);
530        tokio::spawn(async move {
531            while let Some(chunk) = stream.next().await {
532                let data = chunk.unwrap().to_vec();
533                if let Err(e) = tx.send(data) {
534                    eprintln!("Sending Error: {e:?}");
535                    break;
536                }
537            }
538        });
539        // CPU-bound task, so use spawn_blocking
540        // DO NOT use thread::spawn, because it will block tokio runtime (if single-threaded runtime, like in tests)
541        tokio::task::spawn_blocking(move || {
542            self.decode(
543                &mut reader,
544                move |entry: MetaAttached<Entry, EntryMeta>| {
545                    // as we used unbound channel here, it will never full so can be send with synchronous
546                    if let Err(e) = sender.send(entry) {
547                        eprintln!("unbound channel Sending Error: {e:?}");
548                    }
549                },
550                Some(move |pack_id: SHA1| {
551                    if let Some(pack_id_send) = pack_hash_send
552                        && let Err(e) = pack_id_send.send(pack_id)
553                    {
554                        eprintln!("unbound channel Sending Error: {e:?}");
555                    }
556                }),
557            )
558            .unwrap();
559            self
560        })
561        .await
562        .unwrap()
563    }
564
565    /// CacheObjects + Index size of Caches
566    fn memory_used(&self) -> usize {
567        self.cache_objs_mem_used() + self.caches.memory_used_index()
568    }
569
570    /// The total memory used by the CacheObjects of this Pack
571    fn cache_objs_mem_used(&self) -> usize {
572        self.cache_objs_mem.load(Ordering::Acquire)
573    }
574
575    /// Rebuild the Delta Object in a new thread & process the objects waiting for it recursively.
576    /// <br> This function must be *static*, because [&self] can't be moved into a new thread.
577    fn process_delta(
578        shared_params: Arc<SharedParams>,
579        delta_obj: CacheObject,
580        base_obj: Arc<CacheObject>,
581    ) {
582        shared_params.pool.clone().execute(move || {
583            let mut new_obj = match delta_obj.info {
584                CacheObjectInfo::OffsetDelta(_, _) | CacheObjectInfo::HashDelta(_, _) => {
585                    Pack::rebuild_delta(delta_obj, base_obj)
586                }
587                CacheObjectInfo::OffsetZstdelta(_, _) => {
588                    Pack::rebuild_zstdelta(delta_obj, base_obj)
589                }
590                _ => unreachable!(),
591            };
592
593            new_obj.set_mem_recorder(shared_params.cache_objs_mem_size.clone());
594            new_obj.record_mem_size();
595            Self::cache_obj_and_process_waitlist(shared_params, new_obj); //Indirect Recursion
596        });
597    }
598
599    /// Cache the new object & process the objects waiting for it (in multi-threading).
600    fn cache_obj_and_process_waitlist(shared_params: Arc<SharedParams>, new_obj: CacheObject) {
601        (shared_params.callback)(new_obj.to_entry_metadata());
602        let new_obj = shared_params.caches.insert(
603            new_obj.offset,
604            new_obj.base_object_hash().unwrap(),
605            new_obj,
606        );
607        Self::process_waitlist(shared_params, new_obj);
608    }
609
610    fn process_waitlist(shared_params: Arc<SharedParams>, base_obj: Arc<CacheObject>) {
611        let wait_objs = shared_params
612            .waitlist
613            .take(base_obj.offset, base_obj.base_object_hash().unwrap());
614        for obj in wait_objs {
615            // Process the objects waiting for the new object(base_obj = new_obj)
616            Self::process_delta(shared_params.clone(), obj, base_obj.clone());
617        }
618    }
619
620    /// Reconstruct the Delta Object based on the "base object"
621    /// and return the new object.
622    pub fn rebuild_delta(delta_obj: CacheObject, base_obj: Arc<CacheObject>) -> CacheObject {
623        const COPY_INSTRUCTION_FLAG: u8 = 1 << 7;
624        const COPY_OFFSET_BYTES: u8 = 4;
625        const COPY_SIZE_BYTES: u8 = 3;
626        const COPY_ZERO_SIZE: usize = 0x10000;
627
628        let mut stream = Cursor::new(&delta_obj.data_decompressed);
629
630        // Read the base object size
631        // (Size Encoding)
632        let (base_size, result_size) = utils::read_delta_object_size(&mut stream).unwrap();
633
634        // Get the base object data
635        let base_info = &base_obj.data_decompressed;
636        assert_eq!(base_info.len(), base_size, "Base object size mismatch");
637
638        let mut result = Vec::with_capacity(result_size);
639
640        loop {
641            // Check if the stream has ended, meaning the new object is done
642            let instruction = match utils::read_bytes(&mut stream) {
643                Ok([instruction]) => instruction,
644                Err(err) if err.kind() == ErrorKind::UnexpectedEof => break,
645                Err(err) => {
646                    panic!(
647                        "{}",
648                        GitError::DeltaObjectError(format!("Wrong instruction in delta :{err}"))
649                    );
650                }
651            };
652
653            if instruction & COPY_INSTRUCTION_FLAG == 0 {
654                // Data instruction; the instruction byte specifies the number of data bytes
655                if instruction == 0 {
656                    // Appending 0 bytes doesn't make sense, so git disallows it
657                    panic!(
658                        "{}",
659                        GitError::DeltaObjectError(String::from("Invalid data instruction"))
660                    );
661                }
662
663                // Append the provided bytes
664                let mut data = vec![0; instruction as usize];
665                stream.read_exact(&mut data).unwrap();
666                result.extend_from_slice(&data);
667            } else {
668                // Copy instruction
669                // +----------+---------+---------+---------+---------+-------+-------+-------+
670                // | 1xxxxxxx | offset1 | offset2 | offset3 | offset4 | size1 | size2 | size3 |
671                // +----------+---------+---------+---------+---------+-------+-------+-------+
672                let mut nonzero_bytes = instruction;
673                let offset =
674                    utils::read_partial_int(&mut stream, COPY_OFFSET_BYTES, &mut nonzero_bytes)
675                        .unwrap();
676                let mut size =
677                    utils::read_partial_int(&mut stream, COPY_SIZE_BYTES, &mut nonzero_bytes)
678                        .unwrap();
679                if size == 0 {
680                    // Copying 0 bytes doesn't make sense, so git assumes a different size
681                    size = COPY_ZERO_SIZE;
682                }
683                // Copy bytes from the base object
684                let base_data = base_info.get(offset..(offset + size)).ok_or_else(|| {
685                    GitError::DeltaObjectError("Invalid copy instruction".to_string())
686                });
687
688                match base_data {
689                    Ok(data) => result.extend_from_slice(data),
690                    Err(e) => panic!("{}", e),
691                }
692            }
693        }
694        assert_eq!(result_size, result.len(), "Result size mismatch");
695
696        let hash = utils::calculate_object_hash(base_obj.object_type(), &result);
697        // create new obj from `delta_obj` & `result` instead of modifying `delta_obj` for heap-size recording
698        CacheObject {
699            info: CacheObjectInfo::BaseObject(base_obj.object_type(), hash),
700            offset: delta_obj.offset,
701            data_decompressed: result,
702            mem_recorder: None,
703            is_delta_in_pack: delta_obj.is_delta_in_pack,
704        } // Canonical form (Complete Object)
705        // Memory recording will happen after this function returns. See `process_delta`
706    }
707    pub fn rebuild_zstdelta(delta_obj: CacheObject, base_obj: Arc<CacheObject>) -> CacheObject {
708        let result = zstdelta::apply(&base_obj.data_decompressed, &delta_obj.data_decompressed)
709            .expect("Failed to apply zstdelta");
710        let hash = utils::calculate_object_hash(base_obj.object_type(), &result);
711        CacheObject {
712            info: CacheObjectInfo::BaseObject(base_obj.object_type(), hash),
713            offset: delta_obj.offset,
714            data_decompressed: result,
715            mem_recorder: None,
716            is_delta_in_pack: delta_obj.is_delta_in_pack,
717        } // Canonical form (Complete Object)
718        // Memory recording will happen after this function returns. See `process_delta`
719    }
720}
721
722#[cfg(test)]
723mod tests {
724    use std::fs;
725    use std::io::BufReader;
726    use std::io::Cursor;
727    use std::io::prelude::*;
728    use std::sync::Arc;
729    use std::sync::atomic::{AtomicUsize, Ordering};
730    use std::{env, path::PathBuf};
731
732    use flate2::Compression;
733    use flate2::write::ZlibEncoder;
734    use tokio_util::io::ReaderStream;
735
736    use crate::hash::SHA1;
737    use crate::internal::pack::Pack;
738    use crate::internal::pack::tests::init_logger;
739    use futures_util::TryStreamExt;
740
741    #[tokio::test]
742    async fn test_pack_check_header() {
743        let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
744        source.push("tests/data/packs/git-2d187177923cd618a75da6c6db45bb89d92bd504.pack");
745
746        let f = fs::File::open(source).unwrap();
747        let mut buf_reader = BufReader::new(f);
748        let (object_num, _) = Pack::check_header(&mut buf_reader).unwrap();
749
750        assert_eq!(object_num, 358109);
751    }
752
753    #[test]
754    fn test_decompress_data() {
755        let data = b"Hello, world!"; // Sample data to compress and then decompress
756        let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
757        encoder.write_all(data).unwrap();
758        let compressed_data = encoder.finish().unwrap();
759        let compressed_size = compressed_data.len();
760
761        // Create a cursor for the compressed data to simulate a BufRead source
762        let mut cursor: Cursor<Vec<u8>> = Cursor::new(compressed_data);
763        let expected_size = data.len();
764
765        // Decompress the data and assert correctness
766        let result = Pack::decompress_data(&mut cursor, expected_size);
767        match result {
768            Ok((decompressed_data, bytes_read)) => {
769                assert_eq!(bytes_read, compressed_size);
770                assert_eq!(decompressed_data, data);
771            }
772            Err(e) => panic!("Decompression failed: {e:?}"),
773        }
774    }
775
776    #[test]
777    fn test_pack_decode_without_delta() {
778        let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
779        source.push("tests/data/packs/pack-1d0e6c14760c956c173ede71cb28f33d921e232f.pack");
780
781        let tmp = PathBuf::from("/tmp/.cache_temp");
782
783        let f = fs::File::open(source).unwrap();
784        let mut buffered = BufReader::new(f);
785        let mut p = Pack::new(None, Some(1024 * 1024 * 20), Some(tmp), true);
786        p.decode(&mut buffered, |_| {}, None::<fn(SHA1)>).unwrap();
787    }
788
789    #[test]
790    // #[traced_test]
791    fn test_pack_decode_with_ref_delta() {
792        init_logger();
793
794        let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
795        source.push("tests/data/packs/ref-delta-65d47638aa7cb7c39f1bd1d5011a415439b887a8.pack");
796
797        let tmp = PathBuf::from("/tmp/.cache_temp");
798
799        let f = fs::File::open(source).unwrap();
800        let mut buffered = BufReader::new(f);
801        let mut p = Pack::new(None, Some(1024 * 1024 * 20), Some(tmp), true);
802        p.decode(&mut buffered, |_| {}, None::<fn(SHA1)>).unwrap();
803    }
804
805    #[test]
806    fn test_pack_decode_no_mem_limit() {
807        let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
808        source.push("tests/data/packs/pack-1d0e6c14760c956c173ede71cb28f33d921e232f.pack");
809
810        let tmp = PathBuf::from("/tmp/.cache_temp");
811
812        let f = fs::File::open(source).unwrap();
813        let mut buffered = BufReader::new(f);
814        let mut p = Pack::new(None, None, Some(tmp), true);
815        p.decode(&mut buffered, |_| {}, None::<fn(SHA1)>).unwrap();
816    }
817
818    #[tokio::test]
819    async fn test_pack_decode_with_large_file_with_delta_without_ref() {
820        init_logger();
821        let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
822        source.push("tests/data/packs/git-2d187177923cd618a75da6c6db45bb89d92bd504.pack");
823
824        let tmp = PathBuf::from("/tmp/.cache_temp");
825
826        let f = fs::File::open(source).unwrap();
827        let mut buffered = BufReader::new(f);
828        let mut p = Pack::new(
829            Some(20),
830            Some(1024 * 1024 * 1024 * 2),
831            Some(tmp.clone()),
832            true,
833        );
834        let rt = p.decode(
835            &mut buffered,
836            |_obj| {
837                // println!("{:?} {}", obj.hash.to_string(), offset);
838            },
839            None::<fn(SHA1)>,
840        );
841        if let Err(e) = rt {
842            fs::remove_dir_all(tmp).unwrap();
843            panic!("Error: {e:?}");
844        }
845    } // it will be stuck on dropping `Pack` on Windows if `mem_size` is None, so we need `mimalloc`
846
847    #[tokio::test]
848    async fn test_decode_large_file_stream() {
849        init_logger();
850        let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
851        source.push("tests/data/packs/git-2d187177923cd618a75da6c6db45bb89d92bd504.pack");
852
853        let tmp = PathBuf::from("/tmp/.cache_temp");
854        let f = tokio::fs::File::open(source).await.unwrap();
855        let stream = ReaderStream::new(f).map_err(axum::Error::new);
856        let p = Pack::new(
857            Some(20),
858            Some(1024 * 1024 * 1024 * 4),
859            Some(tmp.clone()),
860            true,
861        );
862
863        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
864        let handle = tokio::spawn(async move { p.decode_stream(stream, tx, None).await });
865        let count = Arc::new(AtomicUsize::new(0));
866        let count_c = count.clone();
867        // in tests, RUNTIME is single-threaded, so `sync code` will block the tokio runtime
868        let consume = tokio::spawn(async move {
869            let mut cnt = 0;
870            while let Some(_entry) = rx.recv().await {
871                cnt += 1;
872            }
873            tracing::info!("Received: {}", cnt);
874            count_c.store(cnt, Ordering::Release);
875        });
876        let p = handle.await.unwrap();
877        consume.await.unwrap();
878        assert_eq!(count.load(Ordering::Acquire), p.number);
879        assert_eq!(p.number, 358109);
880    }
881
882    #[tokio::test]
883    async fn test_decode_large_file_async() {
884        let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
885        source.push("tests/data/packs/git-2d187177923cd618a75da6c6db45bb89d92bd504.pack");
886
887        let tmp = PathBuf::from("/tmp/.cache_temp");
888        let f = fs::File::open(source).unwrap();
889        let buffered = BufReader::new(f);
890        let p = Pack::new(
891            Some(20),
892            Some(1024 * 1024 * 1024 * 2),
893            Some(tmp.clone()),
894            true,
895        );
896
897        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
898        let handle = p.decode_async(buffered, tx); // new thread
899        let mut cnt = 0;
900        while let Some(_entry) = rx.recv().await {
901            cnt += 1; //use entry here
902        }
903        let p = handle.join().unwrap();
904        assert_eq!(cnt, p.number);
905    }
906
907    #[test]
908    fn test_pack_decode_with_delta_without_ref() {
909        let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
910        source.push("tests/data/packs/pack-d50df695086eea6253a237cb5ac44af1629e7ced.pack");
911
912        let tmp = PathBuf::from("/tmp/.cache_temp");
913
914        let f = fs::File::open(source).unwrap();
915        let mut buffered = BufReader::new(f);
916        let mut p = Pack::new(None, Some(1024 * 1024 * 20), Some(tmp), true);
917        print!("pack_id: {:?}", p.signature);
918        p.decode(&mut buffered, |_| {}, None::<fn(SHA1)>).unwrap();
919        print!("pack_id: {:?}", p.signature.to_string());
920    }
921
922    #[test] // Take too long time
923    fn test_pack_decode_multi_task_with_large_file_with_delta_without_ref() {
924        let task1 = std::thread::spawn(|| {
925            test_pack_decode_with_large_file_with_delta_without_ref();
926        });
927        let task2 = std::thread::spawn(|| {
928            test_pack_decode_with_large_file_with_delta_without_ref();
929        });
930
931        task1.join().unwrap();
932        task2.join().unwrap();
933    }
934}