splice1.c :-(
authorStefan Metzmacher <metze@samba.org>
Wed, 8 Feb 2023 13:34:28 +0000 (14:34 +0100)
committerStefan Metzmacher <metze@samba.org>
Mon, 16 Oct 2023 11:03:53 +0000 (13:03 +0200)
splice1.c [new file with mode: 0644]

diff --git a/splice1.c b/splice1.c
new file mode 100644 (file)
index 0000000..238ca93
--- /dev/null
+++ b/splice1.c
@@ -0,0 +1,58 @@
+
+#define _GNU_SOURCE         /* See feature_test_macros(7) */
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+int main(void)
+{
+       int ffd;
+       int pfds[2];
+#define BUF_SIZE 4096*2
+       char buf [BUF_SIZE] = {0, };
+       char buf2 [BUF_SIZE] = {0, };
+       ssize_t sret;
+       int ret;
+       off_t ofs;
+
+       memset(buf, 0x1f, BUF_SIZE);
+
+       // O_DIRECT, O_DSYNC, O_SYNC, O_LARGEFILE, O_NOFOLLOW, O_TMPFILE
+       ffd = open("/tmp/splice1.dat",
+                  O_RDWR | O_CREAT | O_DIRECT, // | O_TMPFILE,
+                  S_IRUSR | S_IWUSR);
+       printf("open(O_TMPFILE) => ffd[%d]\n", ffd);
+
+       sret = pwrite(ffd, buf, BUF_SIZE, BUF_SIZE);
+       printf("pwrite(count=BUF_SIZE,ofs=BUF_SIZE) sret[%zd]\n", sret);
+
+       ret = pipe(pfds);
+       printf("pipe() => ret[%d]\n", ret);
+
+       ofs = 0;
+       sret = splice(ffd, &ofs, pfds[1], NULL, BUF_SIZE*2, SPLICE_F_MOVE);
+       printf("splice(count=BUF_SIZE*2,ofs=0) sret[%zd]\n", sret);
+
+       memset(buf, 0xf0, BUF_SIZE);
+
+       sret = pwrite(ffd, buf, BUF_SIZE, 0);
+       printf("pwrite(count=BUF_SIZE,ofs=0) sret[%zd]\n", sret);
+       sret = pwrite(ffd, buf, BUF_SIZE, BUF_SIZE);
+       printf("pwrite(count=BUF_SIZE,ofs=BUF_SIZE) sret[%zd]\n", sret);
+
+       sret = read(pfds[0], buf, BUF_SIZE);
+       printf("read(count=BUF_SIZE,ofs=0) sret[%zd]\n", sret);
+
+       memset(buf2, 0x00, BUF_SIZE);
+       ret = memcmp(buf, buf2, BUF_SIZE);
+       printf("memcmp() at ofs=0, expecting 0x00 => ret[%d]\n", ret);
+
+       sret = read(pfds[0], buf, BUF_SIZE);
+       printf("read(count=BUF_SIZE,ofs=BUF_SIZE) sret[%zd]\n", sret);
+
+       memset(buf2, 0x1f, BUF_SIZE);
+       ret = memcmp(buf, buf2, BUF_SIZE);
+       printf("memcmp() at ofs=BUF_SIZE, expecting 0x1f => ret[%d]\n", ret);
+       return 0;
+}