Files
mars_nwe_pcab/dos-compat-export.py

34 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""Explicit read-only DOS snapshot; does not modify source or synthesize NCP aliases."""
import argparse,pathlib,os,hashlib,re,shutil
p=argparse.ArgumentParser(description=__doc__)
p.add_argument('source',type=pathlib.Path);p.add_argument('destination',type=pathlib.Path)
a=p.parse_args();src=a.source.resolve(strict=True);dst=a.destination.resolve()
if not src.is_dir() or dst.exists() or dst.is_relative_to(src):p.error('Destination must be new and outside the source tree')
# Refuse links/special files rather than leaking files outside the chosen tree.
for root,dirs,files in os.walk(src):
for name in dirs+files:
f=pathlib.Path(root)/name
if f.is_symlink() or not (f.is_dir() or f.is_file()):p.error('Unsupported link or special file: '+str(f))
def export(source,target):
target.mkdir();used={'LFNAMES.TXT'};rows=['DOS name -> original name (CP850; escaped unsupported characters)']
for f in sorted(source.iterdir(),key=lambda x:x.name):
upper=f.name.upper()
if re.fullmatch(r'[A-Z0-9_]{1,8}(\.[A-Z0-9_]{1,3})?',upper) and upper not in used and not re.fullmatch(r'(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\..*)?',upper):alias=upper
else:
ext=re.sub('[^A-Z0-9]','',f.suffix.upper())[0:3] if f.is_file() else ''
for salt in range(100000):
stem='F'+hashlib.sha256((f.name+'\0'+str(salt)).encode()).hexdigest()[:7].upper()
alias=stem+('.'+ext if ext else '')
if alias not in used:break
else:raise RuntimeError('Alias space exhausted')
if len(str((target/alias).relative_to(dst)))>63:raise RuntimeError('DOS path exceeds 63 characters; choose a shallower source subtree')
used.add(alias);rows.append(alias+' -> '+f.name.replace('\r',r'\r').replace('\n',r'\n'))
if f.is_dir():export(f,target/alias)
else:shutil.copyfile(f,target/alias);(target/alias).chmod(0o444)
(target/'LFNAMES.TXT').write_bytes(('\r\n'.join(rows)+'\r\n').encode('cp850','backslashreplace'))
(target/'LFNAMES.TXT').chmod(0o444);target.chmod(0o555)
export(src,dst)
print('Read-only DOS snapshot: '+str(dst))
print('Configure a SEPARATE read-only NetWare volume and its trustee rights. Source ACLs are not copied.')