Skip to content

Instantly share code, notes, and snippets.

@dpchamps
Last active November 7, 2021 01:39
Show Gist options
  • Save dpchamps/ff36bb8a7767f6a2d40dbbf5a4817c86 to your computer and use it in GitHub Desktop.
Save dpchamps/ff36bb8a7767f6a2d40dbbf5a4817c86 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
void create_file(char* f) {
int fd = open(f, O_WRONLY| O_CREAT | O_EXCL);
char* msg = "write some bytes";
write(fd, msg, 16);
close(fd);
}
int copy_with_sendfile(char* from, char* to) {
int fd1 = open(from, O_RDONLY);
struct stat fd1_info;
fstat(fd1, &fd1_info);
int fd2 = open(to, O_WRONLY | O_CREAT|O_EXCL, 0100644);
int bytes = sendfile(fd2, fd1, NULL, fd1_info.st_size);
close(fd1);
close(fd2);
return bytes;
}
int copy_with_cfr(char* from, char* to) {
int fd1 = open(from, O_RDONLY);
struct stat fd1_info;
fstat(fd1, &fd1_info);
int fd2 = open(to, O_WRONLY | O_CREAT|O_EXCL, 0100644);
int bytes = copy_file_range(fd1, 0, fd2, NULL, fd1_info.st_size, 0);
close(fd1);
close(fd2);
return bytes;
}
int main() {
create_file("test");
int og_sf_bw = copy_with_sendfile("test", "test-sf");
int sf_cfr_bw = copy_with_cfr("test-sf", "test-cfr");
int og_cfr_bw = copy_with_cfr("test", "test-cfr-og");
int cfr_cfr_bw = copy_with_cfr("test-cfr-og", "test-cfr-cfr");
printf("\tsendfile bytes written: %d\n\tsendfile -> copy_file_range bytes written: %d\n\tcopy_file_range bytes written: %d\n\tcfr->cfr bytes written: %d\n",
og_sf_bw,
sf_cfr_bw,
og_cfr_bw,
cfr_cfr_bw);
return 0;
}
// apk add gcc gcompat strace musl-dev
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
char* infile = getenv("IN_FILE");
char* outfile = getenv("OUT_FILE");
int fd = open(infile, O_RDONLY|O_LARGEFILE|O_CLOEXEC);
int fd2 = open(outfile, O_WRONLY|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0100644);
int bytes_written = copy_file_range(fd, 0, fd2, NULL, 12, 0);
printf("Bytes written %d\n", bytes_written);
close(fd);
close(fd2);
return 0;
}
// apk add gcc gcompat strace musl-dev
mkdir /tmp/copy_file_range_test
cd /tmp/copy_file_range_test
echo "hello" > testa
cp testa testb
wget https://gist.githubusercontent.com/dpchamps/ff36bb8a7767f6a2d40dbbf5a4817c86/raw/f7d7bb3a90d8af323ec0548fd5d46d7aaa13d8d8/copy-file.c
gcc copy-file.c -o ./copy-file
IN_FILE=testa OUT_FILE=testa-copy ./copy-file
echo "testa-copy contents:"
cat testa-copy
IN_FILE=testb OUT_FILE=testb-copy ./copy-file
echo "testb-copy contents:"
cat testb-copy
FROM node:16-alpine3.12
RUN apk add gcc gcompat strace musl-dev
RUN wget https://gist.githubusercontent.com/dpchamps/ff36bb8a7767f6a2d40dbbf5a4817c86/raw/45e930dddcc36e0b7bf83bffb24de3f6b47f6d8f/do-test.sh
CMD ["sh", "./do-test.sh"]
wget -O - https://gist.githubusercontent.com/dpchamps/ff36bb8a7767f6a2d40dbbf5a4817c86/raw/45e930dddcc36e0b7bf83bffb24de3f6b47f6d8f/do-test.sh | sh
/cstuff # strace ./copy_file_test
execve("./copy_file_test", ["./copy_file_test"], 0x7ffc0ea35eb0 /* 9 vars */) = 0
arch_prctl(ARCH_SET_FS, 0x7f7ac4ed2b48) = 0
set_tid_address(0x7f7ac4ed2f90) = 334
brk(NULL) = 0x559680da6000
brk(0x559680da8000) = 0x559680da8000
mmap(0x559680da6000, 4096, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x559680da6000
mprotect(0x7f7ac4ecf000, 4096, PROT_READ) = 0
mprotect(0x55967fec0000, 4096, PROT_READ) = 0
open("./foo/test", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
open("./foo/test2", O_WRONLY|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0100644) = 4
fcntl(4, F_SETFD, FD_CLOEXEC) = 0
copy_file_range(3, NULL, 4, NULL, 12, 0) = 6
close(3) = 0
close(4) = 0
exit_group(0) = ?
+++ exited with 0 +++
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("./foo/test", O_RDONLY|O_LARGEFILE|O_CLOEXEC);
int fd2 = open("./foo/test2", O_WRONLY|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0100644);
copy_file_range(fd, 0, fd2, NULL, 12, 0);
close(fd);
close(fd2);
return 0;
}
cstuff # strace ./copy_file_test
execve("./copy_file_test", ["./copy_file_test"], 0x7ffd25f9b4a0 /* 9 vars */) = 0
arch_prctl(ARCH_SET_FS, 0x7f3b9b8fbb48) = 0
set_tid_address(0x7f3b9b8fbf90) = 349
brk(NULL) = 0x555709f0b000
brk(0x555709f0d000) = 0x555709f0d000
mmap(0x555709f0b000, 4096, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x555709f0b000
mprotect(0x7f3b9b8f8000, 4096, PROT_READ) = 0
mprotect(0x555708fb8000, 4096, PROT_READ) = 0
open("./bar/test", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
open("./bar/test2", O_WRONLY|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0100644) = 4
fcntl(4, F_SETFD, FD_CLOEXEC) = 0
copy_file_range(3, NULL, 4, NULL, 12, 0) = 0
close(3) = 0
close(4) = 0
exit_group(0) = ?
+++ exited with 0 +++
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("./bar/test", O_RDONLY|O_LARGEFILE|O_CLOEXEC);
int fd2 = open("./bar/test2", O_WRONLY|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0100644);
copy_file_range(fd, 0, fd2, NULL, 12, 0);
close(fd);
close(fd2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment