Skip to content

Instantly share code, notes, and snippets.

@moreati
moreati / ssh-3-way.sh
Last active March 2, 2023 19:20
Field expedient `scp -3 src:largefile dest:largefile` without scp
# `ssh -3 src.host.example:largefile dest.host.example:largefile` copies
# largefile from the source host to the destination, using the local machine
# as a relay. The file can be arbitrarily large. What if we don't have `scp`?
# We have
# - ssh connectivity from local machine to src.host.example
# - ssh connectivity from local machine to dest.dest.example
# - `ssh` client on local machine
# - `nc` command on remote machines
# - A large file called largefile on src.host.example
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@moreati
moreati / fsverity_digest
Last active February 2, 2023 23:44
Prototype Python implementation of `fsverity digest`
#!/usr/bin/env python3
'''
Compute fs-verity digest for the given file(s).
'''
import argparse
import enum
import functools
import hashlib
import itertools
@moreati
moreati / blob2tar
Created January 28, 2023 10:29
Sperate a tarball into blobs and headers, then recombine to recreate the original tarball
#!/usr/bin/env python3
'''
Construct a tarball from a blob summary
'''
import argparse
import tarfile
import ruamel.yaml
@moreati
moreati / tar_sum.py
Last active January 23, 2023 21:01
List sha256 digests and names in a .tar piped to stdin
#!/usr/bin/env/python3
import hashlib
import sys
import tarfile
with tarfile.open(fileobj=sys.stdin.buffer, mode='r|') as tf:
for member in tf:
if member.isfile():
with tf.extractfile(member) as mf:
@moreati
moreati / scandirat.c
Created September 14, 2022 18:25
Example calling scandirat() in glibc
#define _DEFAULT_SOURCE
#define _GNU_SOURCE
#include <dirent.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* Compile with
def corange(end):
i = 0
while i < end:
v = yield i
if isinstance(v, int):
end = v
i += 1
for i in (r := corange(10)):
print(i)
@moreati
moreati / cm.py
Created July 30, 2022 09:01
Combined Python context manager, iterator, and iterable; with a generator-like send() method
EVENS = object()
STOP = object()
class CM():
def __init__(self, n=10):
print('__init__() called')
self.n = n
self._it = iter(range(n))
self._evens = False
@moreati
moreati / walk.py
Last active July 29, 2022 20:22
Prototyping an `os.walk()` alternative
import os
import shutil
import string
import timeit
class FakeDirEntry:
def __init__(self, path):
self.path = path
self.name = path
@moreati
moreati / copy_atomic.py
Last active July 28, 2022 00:01
ETXTBSY demonstration
def copy_atomic(src, dst, follow_symlinks=True):
"""
Copy src file to dst atomically, via a temporary file that is renamed.
It is intended to be a drop in replacement for `shutil.copy2()`.
"""
if os.path.isdir(dst):
dst_dir = dst
dst_name = os.path.basename(src)
dst = os.path.join(dst, dst_name)