| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
import shlex |
| 6 |
import hashlib |
| 7 |
import stat |
| 8 |
import argparse |
| 9 |
|
| 10 |
def log_error(error): |
| 11 |
print("Readdir error: " + error) |
| 12 |
|
| 13 |
def should_convert_whiteout(stbuf): |
| 14 |
return args.whiteout and stat.S_ISCHR(stbuf.st_mode) and stbuf.st_rdev == os.makedev(0,0) |
| 15 |
|
| 16 |
def has_whiteout_child(path): |
| 17 |
for f in os.listdir(path): |
| 18 |
s = os.lstat(os.path.join(path, f)) |
| 19 |
if should_convert_whiteout(s): |
| 20 |
return True |
| 21 |
return False |
| 22 |
|
| 23 |
def dumpfile(file, root): |
| 24 |
rel = os.path.relpath(file, root) |
| 25 |
s = os.lstat(file) |
| 26 |
|
| 27 |
st_mode = s.st_mode |
| 28 |
|
| 29 |
content = None |
| 30 |
xattrs = {} |
| 31 |
if should_convert_whiteout(s): |
| 32 |
content = b'' |
| 33 |
st_mode = (st_mode & ~stat.S_IFMT(st_mode)) | stat.S_IFREG |
| 34 |
xattrs["trusted.overlay.overlay.whiteout"] = b'' |
| 35 |
xattrs["user.overlay.whiteout"] = b'' |
| 36 |
|
| 37 |
if stat.S_ISDIR(st_mode) and has_whiteout_child(file): |
| 38 |
xattrs["trusted.overlay.overlay.whiteouts"] = b'' |
| 39 |
xattrs["user.overlay.whiteouts"] = b'' |
| 40 |
xattrs["trusted.overlay.overlay.opaque"] = b'x' |
| 41 |
xattrs["user.overlay.opaque"] = b'x' |
| 42 |
|
| 43 |
nlink = s.st_nlink; |
| 44 |
if args.no_nlink or (args.no_root_nlink and file == root): |
| 45 |
nlink = 1 |
| 46 |
print(f"{shlex.quote(rel)} {oct(st_mode)} {nlink} {s.st_uid}:{s.st_gid} {s.st_rdev} {s.st_mtime_ns}",end="") |
| 47 |
if stat.S_ISREG(st_mode): |
| 48 |
if content == None: |
| 49 |
content = open(file,'rb').read() |
| 50 |
digest = hashlib.sha256(content).hexdigest() |
| 51 |
print(f" {s.st_size} sha256:{digest}",end="") |
| 52 |
elif stat.S_ISLNK(st_mode): |
| 53 |
link = os.readlink(file) |
| 54 |
print(f" ->{shlex.quote(link)}",end="") |
| 55 |
|
| 56 |
for attr in sorted(os.listxattr(file, follow_symlinks=False)): |
| 57 |
v = os.getxattr(file, attr, follow_symlinks=False) |
| 58 |
xattrs[attr] = v |
| 59 |
|
| 60 |
for attr in sorted(xattrs.keys()): |
| 61 |
v = xattrs[attr] |
| 62 |
if args.userxattr and not attr.startswith("user."): |
| 63 |
continue |
| 64 |
if args.noescaped and attr.startswith("trusted.overlay.overlay."): |
| 65 |
continue |
| 66 |
|
| 67 |
print(f" {attr}={v}", end="") |
| 68 |
|
| 69 |
print() |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
def dumpdir(root): |
| 74 |
dumpfile(root, root) |
| 75 |
for parent, dirs, files in os.walk(root, topdown=True, onerror=log_error): |
| 76 |
dirs.sort() |
| 77 |
for file in sorted(dirs + files): |
| 78 |
dumpfile(os.path.join(parent, file), root) |
| 79 |
|
| 80 |
argParser = argparse.ArgumentParser() |
| 81 |
argParser.add_argument("--no-nlink", action='store_true') |
| 82 |
argParser.add_argument("--no-root-nlink", action='store_true') |
| 83 |
argParser.add_argument("--userxattr", action='store_true') |
| 84 |
argParser.add_argument("--whiteout", action='store_true') |
| 85 |
argParser.add_argument("--noescaped", action='store_true') |
| 86 |
argParser.add_argument('path') |
| 87 |
|
| 88 |
args = argParser.parse_args() |
| 89 |
|
| 90 |
dumpdir(args.path) |