Files

64 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Drop ENCODING identities nothing can read any more.
Every content step publishes a fresh ROOT/DOWNLOAD/SIZE/INSTALL/VFS generation
and keeps the previous ones in ENCODING, so casc_compact must keep their bytes.
prune_encoding() removes the content entries of the given content keys and the
encoded entries of their encoding keys, rewrites only the touched pages
(first key + MD5), drops pages that become empty and keeps the original
EKey-page end markers. Specs and the self-describing tail are unchanged.
"""
import hashlib,struct,sys
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parent))
import casc_asset_package as P
def _rewrite(pages,kind,drop):
out=[];removed=0
for first,page in pages:
old=P._encoding_entries(page,kind)
keep=[(k,r) for k,r in old if not drop(k,r)]
removed+=len(old)-len(keep)
if len(keep)==len(old):out.append((first,page));continue
if not keep:continue
end=sum(len(r) for _,r in old)
term=P._ENCODED_PAGE_TERMINATOR if kind=='encoded' and page[end:end+len(P._ENCODED_PAGE_TERMINATOR)]==P._ENCODED_PAGE_TERMINATOR else b''
body=b''.join(r for _,r in keep)+term
out.append((keep[0][0],body+bytes(4096-len(body))))
return out,removed
def prune_encoding(raw,stale_ckeys):
lay=P.encoding_layout(raw);stale=set(stale_ckeys);dead_ekeys=set()
def content_drop(k,r):
if k in stale:
dead_ekeys.update(r[22+16*j:38+16*j] for j in range(r[0]));return True
return False
content,rc=_rewrite(lay['sections']['content'],'content',content_drop)
encoded,re_=_rewrite(lay['sections']['encoded'],'encoded',lambda k,r:k in dead_ekeys)
head=bytearray(raw[:22]);struct.pack_into('>2I',head,9,len(content),len(encoded))
parts=[bytes(head),lay['specs']]
for pages in (content,encoded):
parts+= [f+hashlib.md5(p).digest() for f,p in pages]+[p for _,p in pages]
parts.append(lay['tail']);new=b''.join(parts)
check=P.encoding_layout(new)
return new,dict(content_removed=rc,encoded_removed=re_,dead_ekeys=len(dead_ekeys),
content_pages=(len(lay['sections']['content']),len(content)),encoded_pages=(len(lay['sections']['encoded']),len(encoded)),
bytes=(len(raw),len(new)),md5=check['md5'])
def prune_download(raw,dead_ekeys):
"""Remove DOWNLOAD records for dead EKeys and their bit in every tag bitmap."""
lay=P.download_layout(raw);count=lay['count'];dead=set(dead_ekeys)
drop=[i for i,at in enumerate(range(11,lay['entries_end'],22)) if raw[at:at+16] in dead]
if not drop:return raw,dict(removed=0)
gone=set(drop);records=b''.join(raw[11+22*i:33+22*i] for i in range(count) if i not in gone)
new_count=count-len(drop);tags=[]
for t in lay['tags']:
bits=[(t['bitmap'][i//8]>>(7-i%8))&1 for i in range(count) if i not in gone]
bm=bytearray((new_count+7)//8)
for i,b in enumerate(bits):
if b:bm[i//8]|=128>>(i%8)
tags.append(t['name'].encode()+b'\0'+t['type'].to_bytes(2,'big')+bytes(bm))
out=raw[:5]+new_count.to_bytes(4,'big')+raw[9:11]+records+b''.join(tags)
check=P.download_layout(out)
return out,dict(removed=len(drop),entries=(count,new_count),md5=check['md5'])