"""One SHA-256 for a whole root filesystem tree: every file's path, size and MD5, and every symlink's target, in code-point order of path. python3 treehash.py card/root The images are not bit-reproducible (mke2fs stamps the time into the superblock), so a rebuilt image never matches a published checksum. The tree it was built from does, and this is how to compare. Modes and owners are not part of the hash -- the host tree cannot carry them; they come from metadata.debugfs, which is itself checked against Psion's listing. """ import hashlib, os, sys root = sys.argv[1] lines = [] for dp, dns, fns in os.walk(root): for n in dns + fns: p = os.path.join(dp, n) rel = './' + os.path.relpath(p, root) if os.path.islink(p): lines.append(f'{rel} L {os.readlink(p)}') elif os.path.isfile(p): h = hashlib.md5() with open(p, 'rb') as f: for chunk in iter(lambda: f.read(1 << 20), b''): h.update(chunk) lines.append(f'{rel} F {os.path.getsize(p)} {h.hexdigest()}') lines.sort() print(hashlib.sha256(('\n'.join(lines) + '\n').encode('utf-8', 'surrogateescape')).hexdigest(), f'{len(lines)} files and links')