Add check for pipe/fifo files
[fio.git] / filesetup.c
index e9dc72db740669952d4c8d4fb24dea38855fdfeb..bb25cd5c20bf43ea46646f3b5fb799637e23f5f2 100644 (file)
@@ -8,11 +8,10 @@
 #include <sys/types.h>
 
 #include "fio.h"
-#include "os.h"
 
 static int extend_file(struct thread_data *td, struct fio_file *f)
 {
-       int r, new_layout = 0, flags;
+       int r, new_layout = 0, unlink_file = 0, flags;
        unsigned long long left;
        unsigned int bs;
        char *b;
@@ -24,8 +23,10 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
         */
        if (td_read(td) || (td_write(td) && td->o.overwrite))
                new_layout = 1;
+       if (td_write(td) && !td->o.overwrite)
+               unlink_file = 1;
 
-       if (new_layout && (f->flags & FIO_FILE_EXISTS)) {
+       if ((unlink_file || new_layout) && (f->flags & FIO_FILE_EXISTS)) {
                if (unlink(f->file_name) < 0) {
                        td_verror(td, errno, "unlink");
                        return 1;
@@ -180,7 +181,7 @@ int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
                        log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
                        ret = 0;
                }
-       } else if (f->filetype == FIO_TYPE_CHAR)
+       } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
                ret = 0;
 
        if (ret < 0) {
@@ -274,20 +275,29 @@ int open_files(struct thread_data *td)
 /*
  * open/close all files, so that ->real_file_size gets set
  */
-static void get_file_sizes(struct thread_data *td)
+static int get_file_sizes(struct thread_data *td)
 {
        struct fio_file *f;
        unsigned int i;
+       int err = 0;
 
        for_each_file(td, f, i) {
-               if (td->io_ops->open_file(td, f))
+               if (td->io_ops->open_file(td, f)) {
+                       if (td->error != ENOENT) {
+                               log_err("%s\n", td->verror);
+                               err = 1;
+                       }
                        clear_error(td);
-               else
-                       td->io_ops->close_file(td, f);
+               } else {
+                       if (td->io_ops->close_file)
+                               td->io_ops->close_file(td, f);
+               }
 
                if (f->real_file_size == -1ULL && td->o.size)
                        f->real_file_size = td->o.size / td->o.nr_files;
        }
+
+       return err;
 }
 
 /*
@@ -308,7 +318,7 @@ int setup_files(struct thread_data *td)
        if (td->io_ops->setup)
                err = td->io_ops->setup(td);
        else
-               get_file_sizes(td);
+               err = get_file_sizes(td);
 
        if (err)
                return err;
@@ -475,6 +485,8 @@ static void get_file_type(struct fio_file *f)
                        f->filetype = FIO_TYPE_BD;
                else if (S_ISCHR(sb.st_mode))
                        f->filetype = FIO_TYPE_CHAR;
+               else if (S_ISFIFO(sb.st_mode))
+                       f->filetype = FIO_TYPE_PIPE;
        }
 }
 
@@ -491,6 +503,12 @@ void add_file(struct thread_data *td, const char *fname)
        memset(f, 0, sizeof(*f));
        f->fd = -1;
 
+       /*
+        * init function, io engine may not be loaded yet
+        */
+       if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
+               f->real_file_size = -1ULL;
+
        if (td->o.directory)
                len = sprintf(file_name, "%s/", td->o.directory);
 
@@ -523,6 +541,7 @@ void put_file(struct thread_data *td, struct fio_file *f)
 
        if (td->io_ops->close_file)
                td->io_ops->close_file(td, f);
+
        td->nr_open_files--;
        f->flags &= ~FIO_FILE_OPEN;
 }