11import contextlib
22import dataclasses
33import enum
4- import shutil
54from abc import abstractmethod , ABC
65from typing import BinaryIO , Union , ContextManager , Tuple , Callable , Literal , Generator
76
87from typing_extensions import Protocol , override
98
9+ from prime_backup .utils import file_utils
1010from prime_backup .utils .bypass_io import BypassReader , BypassWriter
1111from prime_backup .utils .path_like import PathLike
1212
@@ -43,6 +43,7 @@ def ensure_lib(cls):
4343 def copy_compressed (
4444 self , source_path : PathLike , dest_path : PathLike , * ,
4545 calc_hash : bool = False ,
46+ estimate_read_size : int = 0 ,
4647 open_r_func : Callable [[PathLike , "Literal['rb']" ], BinaryIO ] = open ,
4748 open_w_func : Callable [[PathLike , "Literal['wb']" ], BinaryIO ] = open ,
4849 ) -> CopyCompressResult :
@@ -52,7 +53,7 @@ def copy_compressed(
5253 with open_r_func (source_path , 'rb' ) as f_in , open_w_func (dest_path , 'wb' ) as f_out :
5354 reader = BypassReader (f_in , calc_hash = calc_hash )
5455 writer = BypassWriter (f_out )
55- self ._copy_compressed (reader , writer )
56+ self ._copy_compressed (reader , writer , estimate_read_size = estimate_read_size )
5657 return self .CopyCompressResult (reader .get_read_len (), reader .get_hash (), writer .get_write_len ())
5758
5859 def copy_decompressed (
@@ -106,19 +107,19 @@ def open_decompressed_bypassed(self, source_path: PathLike) -> Generator[Tuple[B
106107 with self .decompress_stream (reader ) as f_decompressed :
107108 yield reader , f_decompressed
108109
109- def _copy_compressed (self , f_in : BinaryIO , f_out : BinaryIO ):
110+ def _copy_compressed (self , f_in : BinaryIO , f_out : BinaryIO , * , estimate_read_size : int = 0 ):
110111 """
111112 (f_in) --[compress]--> (f_out)
112113 """
113114 with self .compress_stream (f_out ) as compressed_out :
114- shutil . copyfileobj (f_in , compressed_out )
115+ file_utils . copy_file_obj_fast (f_in , compressed_out , estimate_read_size = estimate_read_size )
115116
116- def _copy_decompressed (self , f_in : BinaryIO , f_out : BinaryIO ):
117+ def _copy_decompressed (self , f_in : BinaryIO , f_out : BinaryIO , * , estimate_read_size : int = 0 ):
117118 """
118119 (f_in) --[decompress]--> (f_out)
119120 """
120121 with self .decompress_stream (f_in ) as compressed_in :
121- shutil . copyfileobj (compressed_in , f_out )
122+ file_utils . copy_file_obj_fast (compressed_in , f_out , estimate_read_size = estimate_read_size )
122123
123124 @abstractmethod
124125 def compress_stream (self , f_out : BinaryIO ) -> ContextManager [BinaryIO ]:
0 commit comments