From 93e64fa80280b0cbee598bf17d16447222e59c03 Mon Sep 17 00:00:00 2001 From: Alexey Kopytov Date: Wed, 23 Mar 2016 11:25:47 +0300 Subject: [PATCH] Re-create files after removing them when starting a --file-test-mode=seqwr benchmark. --- sysbench/tests/fileio/sb_fileio.c | 36 +++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/sysbench/tests/fileio/sb_fileio.c b/sysbench/tests/fileio/sb_fileio.c index 766f42b..90e98a7 100644 --- a/sysbench/tests/fileio/sb_fileio.c +++ b/sysbench/tests/fileio/sb_fileio.c @@ -309,7 +309,8 @@ static unsigned long sb_getpagesize(void); static unsigned long sb_get_allocation_granularity(void); static void *sb_memalign(size_t size); static void sb_free_memaligned(void *buf); -static FILE_DESCRIPTOR sb_open(const char *name); +static FILE_DESCRIPTOR sb_open(const char *); +static FILE_DESCRIPTOR sb_create(const char *); int register_test_fileio(sb_list_t *tests) { @@ -355,8 +356,15 @@ int file_prepare(void) { snprintf(file_name, sizeof(file_name), "test_file.%d",i); /* remove test files for creation test if they exist */ - if (test_mode == MODE_WRITE) + if (test_mode == MODE_WRITE) + { unlink(file_name); + if (sb_create(file_name)) + { + log_errno(LOG_FATAL, "Cannot create file '%s'", file_name); + return 1; + } + } log_text(LOG_DEBUG, "Opening file: %s", file_name); files[i] = sb_open(file_name); @@ -2073,6 +2081,30 @@ static FILE_DESCRIPTOR sb_open(const char *name) return file; } +/* + Create a file with a given path. Signal an error if the file already + exists. Return a non-zero value on error. +*/ + +static int sb_create(const char *path) +{ + FILE_DESCRIPTOR file; + int res; + +#ifndef _WIN32 + file = open(path, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); + res = !VALID_FILE(file); + close(file); +#else + file = CreateFile(name, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW, + flags, NULL); + res = !VALID_FILE(file); + CloseHandle(file); +#endif + + return res; +} + /* Fill buffer with random values and write checksum */