helpers: add io_uring_create_file() helper

cleanup: add io_uring_create_file() helper,
and replace create_file() with io_uring_calloc() in tests.

Signed-off-by: Zhiqiang Liu <[email protected]>
diff --git a/test/cq-overflow.c b/test/cq-overflow.c
index 0303966..a22da6b 100644
--- a/test/cq-overflow.c
+++ b/test/cq-overflow.c
@@ -32,25 +32,6 @@
 	return 0;
 }
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 #define ENTRIES	8
 
 static int test_io(const char *file, unsigned long usecs, unsigned *drops, int fault)
@@ -492,10 +473,8 @@
 		return ret;
 	}
 
-	if (create_file(".basic-rw")) {
-		fprintf(stderr, "file creation failed\n");
-		goto err;
-	}
+	io_uring_create_file(".basic-rw", FILE_SIZE);
+
 	if (create_buffers()) {
 		fprintf(stderr, "file creation failed\n");
 		goto err;
diff --git a/test/d4ae271dfaae-test.c b/test/d4ae271dfaae-test.c
index 98b43cc..06ec268 100644
--- a/test/d4ae271dfaae-test.c
+++ b/test/d4ae271dfaae-test.c
@@ -16,26 +16,6 @@
 
 #define FILE_SIZE	(128 * 1024)
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	fsync(fd);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 int main(int argc, char *argv[])
 {
 	struct io_uring ring;
@@ -64,10 +44,7 @@
 		fname = argv[1];
 	} else {
 		fname = ".sqpoll.tmp";
-		if (create_file(fname)) {
-			fprintf(stderr, "file creation failed\n");
-			goto out;
-		}
+		io_uring_create_file(fname, FILE_SIZE);
 	}
 
 	fd = open(fname, O_RDONLY | O_DIRECT);
diff --git a/test/fadvise.c b/test/fadvise.c
index 6bf58ef..69dc985 100644
--- a/test/fadvise.c
+++ b/test/fadvise.c
@@ -42,26 +42,6 @@
 	return utime_since(tv, &end);
 }
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	fsync(fd);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 static int do_fadvise(struct io_uring *ring, int fd, off_t offset, off_t len,
 		      int advice)
 {
@@ -184,10 +164,7 @@
 		fname = argv[1];
 	} else {
 		fname = ".fadvise.tmp";
-		if (create_file(".fadvise.tmp")) {
-			fprintf(stderr, "file creation failed\n");
-			goto err;
-		}
+		io_uring_create_file(fname, FILE_SIZE);
 	}
 	if (io_uring_queue_init(8, &ring, 0)) {
 		fprintf(stderr, "ring creation failed\n");
diff --git a/test/fsync.c b/test/fsync.c
index 1cd4f8d..5aa5a83 100644
--- a/test/fsync.c
+++ b/test/fsync.c
@@ -138,35 +138,13 @@
 
 #define FILE_SIZE 1024
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 static int test_sync_file_range(struct io_uring *ring)
 {
 	int ret, fd, save_errno;
 	struct io_uring_sqe *sqe;
 	struct io_uring_cqe *cqe;
 
-	if (create_file(".sync_file_range")) {
-		fprintf(stderr, "file creation failed\n");
-		return 1;
-	}
+	io_uring_create_file(".sync_file_range", FILE_SIZE);
 
 	fd = open(".sync_file_range", O_RDWR);
 	save_errno = errno;
diff --git a/test/helpers.c b/test/helpers.c
index af6d936..c5a82fa 100644
--- a/test/helpers.c
+++ b/test/helpers.c
@@ -4,6 +4,10 @@
  */
 #include <stdlib.h>
 #include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include "helpers.h"
 
@@ -41,4 +45,25 @@
 	return ret;
 }
 
+/*
+ * Helper for creating file and write @size byte buf with 0xaa value in the file.
+ */
+void io_uring_create_file(const char *file, size_t size)
+{
+	ssize_t ret;
+	char *buf;
+	int fd; 
+
+	buf = io_uring_malloc(size);
+	memset(buf, 0xaa, size);
+
+	fd = open(file, O_WRONLY | O_CREAT, 0644);
+	assert(fd >= 0);
+
+	ret = write(fd, buf, size);
+	fsync(fd);
+	close(fd);
+	free(buf);
+	assert(ret == size);
+}
 
diff --git a/test/helpers.h b/test/helpers.h
index dc192d4..4cc357f 100644
--- a/test/helpers.h
+++ b/test/helpers.h
@@ -27,6 +27,12 @@
  */
 void *io_uring_calloc(size_t nmemb, size_t size);
 
+
+/*
+ * Helper for creating file and write @size byte buf with 0xaa value in the file.
+ */
+void io_uring_create_file(const char *file, size_t size);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/test/io-cancel.c b/test/io-cancel.c
index 4dfcef0..25bac7a 100644
--- a/test/io-cancel.c
+++ b/test/io-cancel.c
@@ -33,25 +33,6 @@
 	return 0;
 }
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 static unsigned long long utime_since(const struct timeval *s,
 				      const struct timeval *e)
 {
@@ -235,10 +216,8 @@
 	if (argc > 1)
 		return 0;
 
-	if (create_file(".basic-rw")) {
-		fprintf(stderr, "file creation failed\n");
-		goto err;
-	}
+	io_uring_create_file(".basic-rw", FILE_SIZE);
+
 	if (create_buffers()) {
 		fprintf(stderr, "file creation failed\n");
 		goto err;
diff --git a/test/iopoll.c b/test/iopoll.c
index 9a27dac..b1dafd0 100644
--- a/test/iopoll.c
+++ b/test/iopoll.c
@@ -37,25 +37,6 @@
 	return 0;
 }
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 static int provide_buffers(struct io_uring *ring)
 {
 	struct io_uring_sqe *sqe;
@@ -366,10 +347,7 @@
 		fname = argv[1];
 	} else {
 		fname = ".iopoll-rw";
-		if (create_file(".iopoll-rw")) {
-			fprintf(stderr, "file creation failed\n");
-			goto err;
-		}
+		io_uring_create_file(fname, FILE_SIZE);
 	}
 
 	if (create_buffers()) {
diff --git a/test/madvise.c b/test/madvise.c
index 2a10779..c06465f 100644
--- a/test/madvise.c
+++ b/test/madvise.c
@@ -44,26 +44,6 @@
 	return utime_since(tv, &end);
 }
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	fsync(fd);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 static int do_madvise(struct io_uring *ring, void *addr, off_t len, int advice)
 {
 	struct io_uring_sqe *sqe;
@@ -179,10 +159,7 @@
 		fname = argv[1];
 	} else {
 		fname = ".madvise.tmp";
-		if (create_file(".madvise.tmp")) {
-			fprintf(stderr, "file creation failed\n");
-			goto err;
-		}
+		io_uring_create_file(fname, FILE_SIZE);
 	}
 
 	if (io_uring_queue_init(8, &ring, 0)) {
diff --git a/test/open-close.c b/test/open-close.c
index 59c6d3a..6d44c6a 100644
--- a/test/open-close.c
+++ b/test/open-close.c
@@ -13,25 +13,6 @@
 #include "helpers.h"
 #include "liburing.h"
 
-static int create_file(const char *file, size_t size)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(size);
-	memset(buf, 0xaa, size);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, size);
-	close(fd);
-	return ret != size;
-}
-
 static int test_close(struct io_uring *ring, int fd, int is_ring_fd)
 {
 	struct io_uring_cqe *cqe;
@@ -119,14 +100,10 @@
 		do_unlink = 1;
 	}
 
-	if (create_file(path, 4096)) {
-		fprintf(stderr, "file create failed\n");
-		return 1;
-	}
-	if (do_unlink && create_file(path_rel, 4096)) {
-		fprintf(stderr, "file create failed\n");
-		return 1;
-	}
+	io_uring_create_file(path, 4096);
+
+	if (do_unlink)
+		io_uring_create_file(path_rel, 4096);
 
 	ret = test_openat(&ring, path, -1);
 	if (ret < 0) {
diff --git a/test/openat2.c b/test/openat2.c
index 7994804..750a2b4 100644
--- a/test/openat2.c
+++ b/test/openat2.c
@@ -13,25 +13,6 @@
 #include "helpers.h"
 #include "liburing.h"
 
-static int create_file(const char *file, size_t size)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(size);
-	memset(buf, 0xaa, size);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, size);
-	close(fd);
-	return ret != size;
-}
-
 static int test_openat2(struct io_uring *ring, const char *path, int dfd)
 {
 	struct io_uring_cqe *cqe;
@@ -88,14 +69,10 @@
 		do_unlink = 1;
 	}
 
-	if (create_file(path, 4096)) {
-		fprintf(stderr, "file create failed\n");
-		return 1;
-	}
-	if (do_unlink && create_file(path_rel, 4096)) {
-		fprintf(stderr, "file create failed\n");
-		return 1;
-	}
+	io_uring_create_file(path, 4096);
+
+	if (do_unlink)
+		io_uring_create_file(path_rel, 4096);
 
 	ret = test_openat2(&ring, path, -1);
 	if (ret < 0) {
diff --git a/test/read-write.c b/test/read-write.c
index cb15c26..bd6df37 100644
--- a/test/read-write.c
+++ b/test/read-write.c
@@ -55,25 +55,6 @@
 	return 0;
 }
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 static int __test_io(const char *file, struct io_uring *ring, int write,
 		     int buffered, int sqthread, int fixed, int nonvec,
 		     int buf_select, int seq, int exp_len)
@@ -784,10 +765,7 @@
 		fname = argv[1];
 	} else {
 		fname = ".basic-rw";
-		if (create_file(fname)) {
-			fprintf(stderr, "file creation failed\n");
-			goto err;
-		}
+		io_uring_create_file(fname, FILE_SIZE);
 	}
 
 	if (create_buffers()) {
diff --git a/test/short-read.c b/test/short-read.c
index 4d84937..c839799 100644
--- a/test/short-read.c
+++ b/test/short-read.c
@@ -15,25 +15,6 @@
 #define BUF_SIZE 4096
 #define FILE_SIZE 1024
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 int main(int argc, char *argv[])
 {
 	int ret, fd, save_errno;
@@ -48,10 +29,7 @@
 	vec.iov_base = io_uring_malloc(BUF_SIZE);
 	vec.iov_len = BUF_SIZE;
 
-	if (create_file(".short-read")) {
-		fprintf(stderr, "file creation failed\n");
-		return 1;
-	}
+	io_uring_create_file(".short-read", FILE_SIZE);
 
 	fd = open(".short-read", O_RDONLY);
 	save_errno = errno;
diff --git a/test/sq-poll-dup.c b/test/sq-poll-dup.c
index cda0b5f..18dae3f 100644
--- a/test/sq-poll-dup.c
+++ b/test/sq-poll-dup.c
@@ -39,25 +39,6 @@
 	return 0;
 }
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 static int wait_io(struct io_uring *ring, int nr_ios)
 {
 	struct io_uring_cqe *cqe;
@@ -193,10 +174,7 @@
 		fname = argv[1];
 	} else {
 		fname = ".basic-rw";
-		if (create_file(fname)) {
-			fprintf(stderr, "file creation failed\n");
-			goto err;
-		}
+		io_uring_create_file(fname, FILE_SIZE);
 	}
 
 	if (create_buffers()) {
diff --git a/test/sq-poll-share.c b/test/sq-poll-share.c
index f74278e..d2e82a0 100644
--- a/test/sq-poll-share.c
+++ b/test/sq-poll-share.c
@@ -37,25 +37,6 @@
 	return 0;
 }
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 static int wait_io(struct io_uring *ring, int nr_ios)
 {
 	struct io_uring_cqe *cqe;
@@ -115,10 +96,7 @@
 		fname = argv[1];
 	} else {
 		fname = ".basic-rw";
-		if (create_file(fname)) {
-			fprintf(stderr, "file creation failed\n");
-			goto err;
-		}
+		io_uring_create_file(fname, FILE_SIZE);
 	}
 
 	if (create_buffers()) {
diff --git a/test/statx.c b/test/statx.c
index abbc4cb..bd8b5a3 100644
--- a/test/statx.c
+++ b/test/statx.c
@@ -31,25 +31,6 @@
 }
 #endif
 
-static int create_file(const char *file, size_t size)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(size);
-	memset(buf, 0xaa, size);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, size);
-	close(fd);
-	return ret != size;
-}
-
 static int statx_syscall_supported(void)
 {
 	return errno == ENOSYS ? 0 : -1;
@@ -162,10 +143,7 @@
 		fname = argv[1];
 	} else {
 		fname = "/tmp/.statx";
-		if (create_file(fname, 4096)) {
-			fprintf(stderr, "file create failed\n");
-			return 1;
-		}
+		io_uring_create_file(fname, 4096);
 	}
 
 	ret = test_statx(&ring, fname);
diff --git a/test/submit-reuse.c b/test/submit-reuse.c
index 35109fb..3c09d8d 100644
--- a/test/submit-reuse.c
+++ b/test/submit-reuse.c
@@ -38,26 +38,6 @@
 	return NULL;
 }
 
-static int create_file(const char *file)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(FILE_SIZE);
-	memset(buf, 0xaa, FILE_SIZE);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, FILE_SIZE);
-	fsync(fd);
-	close(fd);
-	return ret != FILE_SIZE;
-}
-
 static char str1[STR_SIZE];
 static char str2[STR_SIZE];
 
@@ -177,14 +157,10 @@
 		return 0;
 	}
 
-	if (do_unlink && create_file(fname1)) {
-		fprintf(stderr, "file creation failed\n");
-		goto err;
-	}
-	if (create_file(".reuse.2")) {
-		fprintf(stderr, "file creation failed\n");
-		goto err;
-	}
+	if (do_unlink)
+		io_uring_create_file(fname1, FILE_SIZE);
+
+	io_uring_create_file(".reuse.2", FILE_SIZE);
 
 	fd1 = open(fname1, O_RDONLY);
 	if (fd1 < 0) {
diff --git a/test/thread-exit.c b/test/thread-exit.c
index 2f15aa7..dc9bfaf 100644
--- a/test/thread-exit.c
+++ b/test/thread-exit.c
@@ -20,26 +20,6 @@
 #define NR_IOS	8
 #define WSIZE	512
 
-static int create_file(const char *file, size_t size)
-{
-	ssize_t ret;
-	char *buf;
-	int fd;
-
-	buf = io_uring_malloc(size);
-	memset(buf, 0xaa, size);
-
-	fd = open(file, O_WRONLY | O_CREAT, 0644);
-	if (fd < 0) {
-		perror("open file");
-		return 1;
-	}
-	ret = write(fd, buf, size);
-	close(fd);
-	free(buf);
-	return ret != size;
-}
-
 struct d {
 	int fd;
 	struct io_uring *ring;
@@ -108,10 +88,8 @@
 		do_unlink = 1;
 	}
 
-	if (do_unlink && create_file(fname, 4096)) {
-		fprintf(stderr, "file create failed\n");
-		return 1;
-	}
+	if (do_unlink)
+		io_uring_create_file(fname, 4096);
 
 	fd = open(fname, O_WRONLY);
 	if (fd < 0) {