Skip to content

Instantly share code, notes, and snippets.

@slavanap
Last active January 23, 2025 16:52
Show Gist options
  • Save slavanap/44ef8dfbd36d1bcdfe54f8408cea8cdc to your computer and use it in GitHub Desktop.
Save slavanap/44ef8dfbd36d1bcdfe54f8408cea8cdc to your computer and use it in GitHub Desktop.
Slurp a file into stdout. Disk space is freed as soon as contents are read
#!/usr/bin/env python3
import argparse
import os
import sys
from ctypes import *
FALLOC_FL_KEEP_SIZE = 0x01
FALLOC_FL_PUNCH_HOLE = 0x02
def main():
parser = argparse.ArgumentParser()
parser.add_argument('fn', type=str, help='file name to slurp')
args = parser.parse_args()
libc = CDLL("libc.so.6", use_errno=True)
offset = 0
with open(args.fn, 'rb+') as f:
while True:
chunk = f.read(100 * 1024**2) # 100MB block
if not chunk:
break
sys.stdout.buffer.write(chunk)
if libc.fallocate64(f.fileno(), FALLOC_FL_PUNCH_HOLE+FALLOC_FL_KEEP_SIZE, c_longlong(offset), c_longlong(len(chunk))) != 0:
print(f"fallocate error: {os.strerror(ctypes.get_errno())} @ offset={offset}, chunk={len(chunk)}", file=sys.stderr)
offset += len(chunk)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment