"""Record what the RetroTechCollection archive cannot hold, so that a copy of the release downloaded from it can be made whole again. python3 mkrestore.py The archive keeps every file's contents and nothing else. Its uploader (RTC-Archive, tools/upload.mjs) leaves out symbolic links, every name that starts with "." and version-control folders (CVS, .git and the like: hidden() below); a folder exists there only as the path of a file in it, so empty folders are lost; and no permissions are kept, so nothing downloaded from it is executable. The build needs all of these. This writes, at the top of the release: restore-after-download.tar.gz the links, the hidden files and folders, and the folders that hold no file the archive keeps, each with its permissions -- led by a manifest of every file and folder the archive does keep, with its size and permissions restore-after-download.txt the same, to read: every link, such folder, hidden entry and unusual permission, and the bundle's SHA-256 restore-after-download.py, beside them, puts it all back. mkdist.sh runs this once everything else is in place, and before the checksums. """ import gzip, hashlib, io, os, stat, sys, tarfile BUNDLE = 'restore-after-download.tar.gz' LISTING = 'restore-after-download.txt' MANIFEST = 'restore-after-download.manifest' # hidden() in RTC-Archive's tools/upload.mjs -- change both together VCS = {'.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'} def hidden(name): return name.startswith('.') or name in VCS or name == 'error_log' def byte_order(path): return path.encode('utf-8', 'surrogateescape') def nobody(ti): # the restore never sets owners; keep the bundle free of this machine's ti.uid = ti.gid = 0 ti.uname = ti.gname = '' ti.mtime = int(ti.mtime) return ti os.chdir(sys.argv[1]) files, dirs, links, hiddens = [], [], [], [] for dp, dns, fns in os.walk('.'): here = '' if dp == '.' else dp[2:] + '/' descend = [] for n in sorted(dns + fns, key=byte_order): p = here + n if any(c in n for c in '\t\n\r'): sys.exit(f'{p}: a name with a tab or line break; the archive leaves it out and the list cannot carry it') if p in (BUNDLE, LISTING, 'SHA256SUMS.txt', '1-IMAGES/SHA256SUMS.txt'): continue # written after this st = os.lstat(p) if hidden(n): hiddens.append(p) # whole, contents and all elif stat.S_ISLNK(st.st_mode): links.append((p, os.readlink(p))) elif stat.S_ISDIR(st.st_mode): dirs.append((p, stat.S_IMODE(st.st_mode))) descend.append(n) elif stat.S_ISREG(st.st_mode): files.append((p, stat.S_IMODE(st.st_mode), st.st_size)) else: sys.exit(f'{p}: not a file, folder or link; the archive cannot hold it and nor can this') dns[:] = descend held = set() # folders with a kept file somewhere beneath for p, _, _ in files: d = os.path.dirname(p) while d and d not in held: held.add(d) d = os.path.dirname(d) folders = [(p, m) for p, m in dirs if p not in held] manifest = ['PSIONLX-RESTORE 1'] manifest += [f'd\t{m:04o}\t{p}' for p, m in dirs if p in held] manifest += [f'f\t{m:04o}\t{s}\t{p}' for p, m, s in files] manifest = ('\n'.join(manifest) + '\n').encode('utf-8', 'surrogateescape') members = sorted([(p, False) for p, _ in folders] + [(p, False) for p, _ in links] + [(p, True) for p in hiddens], key=lambda x: byte_order(x[0])) hidden_files = 0 with open(BUNDLE + '.part', 'wb') as raw: with gzip.GzipFile(filename='', mode='wb', fileobj=raw, mtime=0, compresslevel=9) as gz: with tarfile.open(fileobj=gz, mode='w', format=tarfile.PAX_FORMAT) as tar: ti = tarfile.TarInfo(MANIFEST) ti.size, ti.mode = len(manifest), 0o644 tar.addfile(ti, io.BytesIO(manifest)) for p, whole in members: tar.add(p, arcname=p, recursive=whole, filter=nobody) hidden_files = sum(1 for m in tar.members if m.isfile()) - 1 os.replace(BUNDLE + '.part', BUNDLE) h = hashlib.sha256() with open(BUNDLE, 'rb') as f: for chunk in iter(lambda: f.read(1 << 20), b''): h.update(chunk) odd = sorted([(p, m) for p, m, _ in files if m != 0o644] + [(p + '/', m) for p, m in dirs if m != 0o755], key=lambda x: byte_order(x[0])) out = [ 'PsionLX -- WHAT THE ARCHIVE CANNOT HOLD', '=======================================', '', 'The RetroTechCollection archive keeps every file\'s contents, but not', 'symbolic links, folders with nothing in them, or permissions (so nothing', 'downloaded from it is a program), and it leaves out every name that starts', 'with a dot and every CVS and .git folder. The build needs them. They are all', 'in restore-after-download.tar.gz, beside this file, and listed below.', '', 'If you downloaded this release from the archive, run, in this folder:', '', ' python3 restore-after-download.py', '', 'It puts them back and then checks that every other file is present at its', 'full size. It never overwrites or deletes anything, and on a copy that', 'already has everything it changes nothing. "--check" only reports.', '', f'{BUNDLE} SHA-256 {h.hexdigest()}', '', f' {len(links):,} links', f' {len(folders):,} folders that hold no file the archive keeps', f' {len(hiddens):,} hidden files and folders, with {hidden_files:,} files in them', f' {len(odd):,} files and folders whose permissions are not 644 and 755', f' and a manifest of the {len(files):,} files and {len(held):,} folders the archive keeps', '', '', f'LINKS ({len(links):,})', '', ] + [f' {p} -> {t}' for p, t in sorted(links, key=lambda x: byte_order(x[0]))] + [ '', '', f'FOLDERS THAT HOLD NO FILE THE ARCHIVE KEEPS ({len(folders):,})', '', ] + [f' {p}/' for p, _ in sorted(folders, key=lambda x: byte_order(x[0]))] + [ '', '', f'HIDDEN FILES AND FOLDERS ({len(hiddens):,})', '', ] + [f' {p}' for p in sorted(hiddens, key=byte_order)] + [ '', '', f'PERMISSIONS OTHER THAN 644 FOR A FILE AND 755 FOR A FOLDER ({len(odd):,})', '', ] + [f' {m:04o} {p}' for p, m in odd] with open(LISTING, 'w', encoding='utf-8', errors='surrogateescape') as f: f.write('\n'.join(out) + '\n') print(f' {len(links):,} links, {len(folders):,} empty folders, {len(hiddens):,} hidden entries ' f'({hidden_files:,} files), {len(odd):,} permissions; ' f'{os.path.getsize(BUNDLE) / 1e6:.1f} MB bundle')