@@ -846,6 +846,25 @@ impl Actor {
846846 }
847847}
848848
849+ #[ derive( Debug ) ]
850+ pub struct RepackPackBlobs {
851+ pub pack_id : PackId ,
852+ pub offset : u32 ,
853+ pub length : u32 ,
854+ pub blobs : Vec < IndexBlob > ,
855+ }
856+
857+ impl RepackPackBlobs {
858+ pub fn from_index_blob ( pack_id : PackId , blob : IndexBlob ) -> Self {
859+ Self {
860+ pack_id,
861+ offset : blob. offset ,
862+ length : blob. length ,
863+ blobs : vec ! [ blob] ,
864+ }
865+ }
866+ }
867+
849868/// The `Repacker` is responsible for repacking blobs into pack files.
850869///
851870/// # Type Parameters
@@ -862,6 +881,8 @@ where
862881 packer : Packer < BE > ,
863882 /// The size limit of the pack file.
864883 size_limit : u32 ,
884+ /// the blob type
885+ blob_type : BlobType ,
865886}
866887
867888impl < BE : DecryptFullBackend > Repacker < BE > {
@@ -895,6 +916,7 @@ impl<BE: DecryptFullBackend> Repacker<BE> {
895916 be,
896917 packer,
897918 size_limit,
919+ blob_type,
898920 } )
899921 }
900922
@@ -909,30 +931,38 @@ impl<BE: DecryptFullBackend> Repacker<BE> {
909931 ///
910932 /// * If the blob could not be added
911933 /// * If reading the blob from the backend fails
912- pub fn add_fast ( & self , pack_id : & PackId , blob : & IndexBlob ) -> RusticResult < ( ) > {
934+ pub fn add_fast ( & self , pack_blobs : RepackPackBlobs ) -> RusticResult < ( ) > {
935+ let offset = pack_blobs. offset ;
913936 let data = self . be . read_partial (
914937 FileType :: Pack ,
915- pack_id,
916- blob . tpe . is_cacheable ( ) ,
917- blob . offset ,
918- blob . length ,
938+ & pack_blobs . pack_id ,
939+ self . blob_type . is_cacheable ( ) ,
940+ offset,
941+ pack_blobs . length ,
919942 ) ?;
920943
921- self . packer
922- . add_raw (
923- & data,
924- & blob. id ,
925- 0 ,
926- blob. uncompressed_length ,
927- Some ( self . size_limit ) ,
928- )
929- . map_err ( |err| {
930- err. overwrite_kind ( ErrorKind :: Internal )
931- . prepend_guidance_line (
932- "Failed to fast-add (unchecked) blob `{blob_id}` to packfile." ,
933- )
934- . attach_context ( "blob_id" , blob. id . to_string ( ) )
935- } ) ?;
944+ // TODO: write in parallel
945+ for blob in pack_blobs. blobs {
946+ let start = usize:: try_from ( blob. offset - offset)
947+ . expect ( "convert from u32 to usize should not fail!" ) ;
948+ let end = usize:: try_from ( blob. offset + blob. length - offset)
949+ . expect ( "convert from u32 to usize should not fail!" ) ;
950+ self . packer
951+ . add_raw (
952+ & data[ start..end] ,
953+ & blob. id ,
954+ 0 ,
955+ blob. uncompressed_length ,
956+ Some ( self . size_limit ) ,
957+ )
958+ . map_err ( |err| {
959+ err. overwrite_kind ( ErrorKind :: Internal )
960+ . prepend_guidance_line (
961+ "Failed to fast-add (unchecked) blob `{blob_id}` to packfile." ,
962+ )
963+ . attach_context ( "blob_id" , blob. id . to_string ( ) )
964+ } ) ?;
965+ }
936966
937967 Ok ( ( ) )
938968 }
@@ -948,25 +978,36 @@ impl<BE: DecryptFullBackend> Repacker<BE> {
948978 ///
949979 /// * If the blob could not be added
950980 /// * If reading the blob from the backend fails
951- pub fn add ( & self , pack_id : & PackId , blob : & IndexBlob ) -> RusticResult < ( ) > {
952- let data = self . be . read_encrypted_partial (
981+ pub fn add ( & self , pack_blobs : RepackPackBlobs ) -> RusticResult < ( ) > {
982+ let offset = pack_blobs. offset ;
983+ let read_data = self . be . read_partial (
953984 FileType :: Pack ,
954- pack_id,
955- blob. tpe . is_cacheable ( ) ,
956- blob. offset ,
957- blob. length ,
958- blob. uncompressed_length ,
985+ & pack_blobs. pack_id ,
986+ self . blob_type . is_cacheable ( ) ,
987+ offset,
988+ pack_blobs. length ,
959989 ) ?;
960990
961- self . packer
962- . add_with_sizelimit ( data, blob. id , Some ( self . size_limit ) )
963- . map_err ( |err| {
964- RusticError :: with_source (
965- ErrorKind :: Internal ,
966- "Failed to add blob to packfile." ,
967- err,
968- )
969- } ) ?;
991+ // TODO: write in parallel
992+ for blob in pack_blobs. blobs {
993+ let start = usize:: try_from ( blob. offset - offset)
994+ . expect ( "convert from u32 to usize should not fail!" ) ;
995+ let end = usize:: try_from ( blob. offset + blob. length - offset)
996+ . expect ( "convert from u32 to usize should not fail!" ) ;
997+ let data = self
998+ . be
999+ . read_encrypted_from_partial ( & read_data[ start..end] , blob. uncompressed_length ) ?;
1000+
1001+ self . packer
1002+ . add_with_sizelimit ( data, blob. id , Some ( self . size_limit ) )
1003+ . map_err ( |err| {
1004+ RusticError :: with_source (
1005+ ErrorKind :: Internal ,
1006+ "Failed to add blob to packfile." ,
1007+ err,
1008+ )
1009+ } ) ?;
1010+ }
9701011
9711012 Ok ( ( ) )
9721013 }
0 commit comments