@@ -98,6 +98,51 @@ def mkdir_with_dot_tmp(folder: Path)->Tuple[Path, Path]:
9898 shutil .rmtree (str (tmpdir ))
9999 tmpdir .mkdir (parents = True , exist_ok = True )
100100 return (folder , tmpdir )
101+
102+ def flush_package_info (content : str , package_info = None , is_new = True ):
103+
104+ if package_info is None :
105+ package_info = {}
106+
107+ for pkg in content .split ('\n \n ' ):
108+ if len (pkg ) < 10 : # ignore blanks
109+ continue
110+ try :
111+ pkg_filename = pattern_package_name .search (pkg ).group (1 )
112+ pkg_size = int (pattern_package_size .search (pkg ).group (1 ))
113+ pkg_checksum = pattern_package_sha256 .search (pkg ).group (1 )
114+ if pkg_filename not in package_info :
115+ if is_new :
116+ pkg_info = {
117+ 'size' : pkg_size ,
118+ 'sha256' : {
119+ 'new' : pkg_checksum ,
120+ 'old' : None
121+ }
122+ }
123+ else :
124+ pkg_info = {
125+ 'size' : None ,
126+ 'sha256' : {
127+ 'new' : None ,
128+ 'old' : pkg_checksum
129+ }
130+ }
131+ else :
132+ pkg_info = package_info [pkg_filename ]
133+ if is_new :
134+ pkg_info ['size' ] = pkg_size
135+ pkg_info ['sha256' ]['new' ] = pkg_checksum
136+ else :
137+ pkg_info ['sha256' ]['old' ] = pkg_checksum
138+ package_info .update ({
139+ pkg_filename : pkg_info
140+ })
141+ except :
142+ print ("Failed to parse one package description" , flush = True )
143+ traceback .print_exc ()
144+ return package_info
145+ return package_info
101146
102147def move_files_in (src : Path , dst : Path ):
103148 empty = True
@@ -110,6 +155,9 @@ def move_files_in(src: Path, dst: Path):
110155 print (f"{ src } is empty" )
111156
112157def apt_mirror (base_url : str , dist : str , repo : str , arch : str , dest_base_dir : Path , deb_set : Dict [str , int ])-> int :
158+
159+ package_info = {}
160+
113161 if not dest_base_dir .is_dir ():
114162 print ("Destination directory is empty, cannot continue" )
115163 return 1
@@ -134,6 +182,7 @@ def apt_mirror(base_url: str, dist: str, repo: str, arch: str, dest_base_dir: Pa
134182 pkgidx_dir ,pkgidx_tmp_dir = mkdir_with_dot_tmp (comp_dir / arch_dir )
135183 with open (release_file , "r" ) as fd :
136184 pkgidx_content = None
185+ pkgidx_content_old = None
137186 cnt_start = False
138187 for line in fd :
139188 if cnt_start :
@@ -146,6 +195,8 @@ def apt_mirror(base_url: str, dist: str, repo: str, arch: str, dest_base_dir: Pa
146195 filename .startswith (f"Contents-{ arch } " ):
147196 fn = Path (filename )
148197 pkgidx_file = dist_dir / fn .parent / ".tmp" / fn .name
198+ if pkgidx_file .stem == 'Packages' :
199+ pkgidx_file_old = Path (f'{ dist_dir } /{ filename } ' )
149200 else :
150201 print (f"Ignore the file { filename } " )
151202 continue
@@ -176,12 +227,38 @@ def apt_mirror(base_url: str, dist: str, repo: str, arch: str, dest_base_dir: Pa
176227 pkgidx_content = content .decode ('utf-8' )
177228 else :
178229 print ("unsupported format" )
230+ continue
231+
232+ package_info = flush_package_info (pkgidx_content , package_info = package_info , is_new = True )
233+
234+ if not os .path .exists (pkgidx_file_old ):
235+ continue
236+
237+ with pkgidx_file_old .open ('rb' ) as t : content = t .read ()
238+ if pkgidx_content_old is None and pkgidx_file_old .stem == 'Packages' :
239+ print (f"getting packages index content from old { pkgidx_file_old .name } " , flush = True )
240+ suffix = pkgidx_file .suffix
241+ if suffix == '.xz' :
242+ pkgidx_content_old = lzma .decompress (content ).decode ('utf-8' )
243+ elif suffix == '.bz2' :
244+ pkgidx_content_old = bz2 .decompress (content ).decode ('utf-8' )
245+ elif suffix == '.gz' :
246+ pkgidx_content_old = gzip .decompress (content ).decode ('utf-8' )
247+ elif suffix == '' :
248+ pkgidx_content_old = content .decode ('utf-8' )
249+ else :
250+ print ("unsupported format" )
251+ continue
252+
253+ package_info = flush_package_info (pkgidx_content_old , package_info = package_info , is_new = False )
254+
179255
180256 # Currently only support SHA-256 checksum, because
181257 # "Clients may not use the MD5Sum and SHA1 fields for security purposes, and must require a SHA256 or a SHA512 field."
182258 # from https://wiki.debian.org/DebianRepository/Format#A.22Release.22_files
183259 if line .startswith ('SHA256:' ):
184260 cnt_start = True
261+
185262 if not cnt_start :
186263 print ("Cannot find SHA-256 checksum" )
187264 return 1
@@ -216,18 +293,13 @@ def collect_tmp_dir():
216293 err = 0
217294 deb_count = 0
218295 deb_size = 0
219- for pkg in pkgidx_content .split ('\n \n ' ):
220- if len (pkg ) < 10 : # ignore blanks
221- continue
222- try :
223- pkg_filename = pattern_package_name .search (pkg ).group (1 )
224- pkg_size = int (pattern_package_size .search (pkg ).group (1 ))
225- pkg_checksum = pattern_package_sha256 .search (pkg ).group (1 )
226- except :
227- print ("Failed to parse one package description" , flush = True )
228- traceback .print_exc ()
229- err = 1
296+ for pkg_filename , pkg_info in package_info .items ():
297+ pkg_size = pkg_info ['size' ]
298+ pkg_checksum = pkg_info ['sha256' ]
299+
300+ if pkg_checksum ['new' ] is None and pkg_size is None :
230301 continue
302+
231303 deb_count += 1
232304 deb_size += pkg_size
233305
@@ -237,8 +309,8 @@ def collect_tmp_dir():
237309 dest_dir .mkdir (parents = True , exist_ok = True )
238310 if dest_filename .suffix == '.deb' :
239311 deb_set [str (dest_filename .relative_to (dest_base_dir ))] = pkg_size
240- if dest_filename .is_file () and dest_filename .stat ().st_size == pkg_size :
241- print (f"Skipping { pkg_filename } , size { pkg_size } " )
312+ if dest_filename .is_file () and dest_filename .stat ().st_size == pkg_size and pkg_checksum [ 'old' ] == pkg_checksum [ 'new' ] :
313+ print (f"Skipping { pkg_filename } , size { pkg_size } , sha256 { pkg_checksum [ 'new' ] } " )
242314 continue
243315
244316 pkg_url = f"{ base_url } /{ pkg_filename } "
@@ -253,8 +325,8 @@ def collect_tmp_dir():
253325 with dest_tmp_filename .open ("rb" ) as f :
254326 for block in iter (lambda : f .read (1024 ** 2 ), b"" ):
255327 sha .update (block )
256- if sha .hexdigest () != pkg_checksum :
257- print (f"Invalid checksum of { dest_filename } , expected { pkg_checksum } " )
328+ if sha .hexdigest () != pkg_checksum [ 'new' ] :
329+ print (f"Invalid checksum of { dest_filename } , expected { pkg_checksum [ 'new' ] } " )
258330 dest_tmp_filename .unlink ()
259331 continue
260332 dest_tmp_filename .rename (dest_filename )
0 commit comments