From ea6b64b18b4cddbd66bb0a2e6e4d99584caa1483 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Tue, 29 Nov 2005 20:30:41 +0100 Subject: [PATCH] [PATCH] fio: extend file if it's too small --- fio.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/fio.c b/fio.c index f2ec8bb..52b3aae 100644 --- a/fio.c +++ b/fio.c @@ -1238,12 +1238,13 @@ static int init_io_u(struct thread_data *td) return 0; } -static int create_file(struct thread_data *td) +static int create_file(struct thread_data *td, unsigned long long size, + int extend) { unsigned long long left; unsigned int bs; + int r, oflags; char *b; - int r; /* * unless specifically asked for overwrite, let normal io extend it @@ -1251,21 +1252,27 @@ static int create_file(struct thread_data *td) if (td_write(td) && !td->overwrite) return 0; - if (!td->file_size) { + if (!size) { fprintf(stderr, "Need size for create\n"); td->error = EINVAL; return 1; } - printf("Client%d: Laying out IO file\n", td->thread_number); + if (!extend) { + oflags = O_CREAT | O_TRUNC; + printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20); + } else { + oflags = O_APPEND; + printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20); + } - td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); + td->fd = open(td->file_name, O_WRONLY | oflags, 0644); if (td->fd < 0) { td->error = errno; return 1; } - if (ftruncate(td->fd, td->file_size) == -1) { + if (!extend && ftruncate(td->fd, td->file_size) == -1) { td->error = errno; return 1; } @@ -1274,7 +1281,7 @@ static int create_file(struct thread_data *td) b = malloc(td->max_bs); memset(b, 0, td->max_bs); - left = td->file_size; + left = size; while (left && !td->terminate) { bs = td->max_bs; if (bs > left) @@ -1306,16 +1313,6 @@ static int create_file(struct thread_data *td) return 0; } -static int file_exists(struct thread_data *td) -{ - struct stat st; - - if (stat(td->file_name, &st) != -1) - return 1; - - return errno != ENOENT; -} - static int file_size(struct thread_data *td) { struct stat st; @@ -1446,14 +1443,22 @@ static int setup_file_plain(struct thread_data *td) static int setup_file(struct thread_data *td) { + struct stat st; int flags = 0; - if (!file_exists(td)) { + if (stat(td->file_name, &st) == -1) { + if (errno != ENOENT) { + td->error = errno; + return 1; + } if (!td->create_file) { td->error = ENOENT; return 1; } - if (create_file(td)) + if (create_file(td, td->file_size, 0)) + return 1; + } else if (st.st_size < td->file_size) { + if (create_file(td, td->file_size - st.st_size, 1)) return 1; } -- 2.25.1