Created
November 8, 2023 06:38
-
-
Save technillogue/5e855f4387ad32ae0890d108c80086bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3.11 | |
import os | |
import sys | |
move = os.SPLICE_F_MOVE if os.getenv("MOVE") else 0 | |
more = os.SPLICE_F_MORE if os.getenv("MORE") else 0 | |
flag = move | more | |
def copy(src_path, dst_path, buffer_size=1 << 16): | |
src_fd = os.open(src_path, os.O_RDONLY) | |
dst_fd = os.open(dst_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) | |
# create a pipe for our buffer instead of using python memory | |
r_fd, w_fd = os.pipe() | |
while True: | |
# transfer data from src to the write end of the pipe | |
# it stays in the kernel | |
bytes_in = os.splice(src_fd, w_fd, buffer_size, flags=flag) | |
if bytes_in == 0: # EOF | |
break | |
# transfer data from the read end of the pipe to dst | |
# it doesn't go through userspace | |
bytes_out = os.splice(r_fd, dest_fd, bytes_in, flags=flag) | |
if __name__ == "__main__": | |
copy(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment