summaryrefslogtreecommitdiff
path: root/lib/pattern.c
diff options
context:
space:
mode:
authorStephen Bates <sbates@raithlin.com>2017-06-08 05:46:46 -0600
committerJens Axboe <axboe@fb.com>2017-06-08 09:19:24 -0600
commita1554f6519e7c91b262a35486d100d757b3b5b8a (patch)
tree7291d2ac83e9517e3f58d6e967e687750c8b1a9c /lib/pattern.c
parenta35ef7cb514d02671bdcb029a64785bbc288fe96 (diff)
downloadfio-a1554f6519e7c91b262a35486d100d757b3b5b8a.tar.gz
fio-a1554f6519e7c91b262a35486d100d757b3b5b8a.tar.bz2
pattern: Add support for files in buffer_pattern argument.
It is useful to be able to initialize the buffer contents using an input file (e.g. when testing certain pathological patterns for compression). Add support to the buffer_pattern input argument for reading data from a file via enclosing the filename in ``''``. Reviewed-by: Sagi Grimberg <sagi@grimberg.me> Reviewed-by: Logan Gunthorpe <logang@deltatee.com> Reviewed-By: Muli Ben-Yehuda <muli@lightbitslabs.com> Signed-off-by: Stephen Bates <sbates@raithlin.com> Signed-off-by: Jens Axboe <axboe@fb.com>
Diffstat (limited to 'lib/pattern.c')
-rw-r--r--lib/pattern.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/pattern.c b/lib/pattern.c
index 0aeb9352..420d74a9 100644
--- a/lib/pattern.c
+++ b/lib/pattern.c
@@ -4,6 +4,10 @@
#include <limits.h>
#include <errno.h>
#include <assert.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
#include "strntol.h"
#include "pattern.h"
@@ -11,6 +15,64 @@
#include "../oslib/strcasestr.h"
/**
+ * parse_file() - parses binary file to fill buffer
+ * @beg - string input, extract filename from this
+ * @out - output buffer where parsed number should be put
+ * @out_len - length of the output buffer
+ * @filled - pointer where number of bytes successfully
+ * parsed will be put
+ *
+ * Returns the end pointer where parsing has been stopped.
+ * In case of parsing error or lack of bytes in output buffer
+ * NULL will be returned.
+ */
+static const char *parse_file(const char *beg, char *out,
+ unsigned int out_len,
+ unsigned int *filled)
+{
+ const char *end;
+ char *file;
+ int fd;
+ ssize_t count;
+
+ if (!out_len)
+ goto err_out;
+
+ assert(*beg == '\'');
+ beg++;
+ end = strchr(beg, '\'');
+ if (!end)
+ goto err_out;
+
+ file = strndup(beg, end - beg);
+ if (file == NULL)
+ goto err_out;
+
+ fd = open(file, O_RDONLY);
+ if (fd < 0)
+ goto err_free_out;
+
+ count = read(fd, out, out_len);
+ if (count == -1)
+ goto err_free_close_out;
+
+ *filled = count;
+ close(fd);
+ free(file);
+
+ /* Catch up quote */
+ return end + 1;
+
+err_free_close_out:
+ close(fd);
+err_free_out:
+ free(file);
+err_out:
+ return NULL;
+
+}
+
+/**
* parse_string() - parses string in double quotes, like "abc"
* @beg - string input
* @out - output buffer where parsed number should be put
@@ -271,6 +333,9 @@ int parse_and_fill_pattern(const char *in, unsigned int in_len,
parsed_fmt = 0;
switch (*beg) {
+ case '\'':
+ end = parse_file(beg, out, out_len, &filled);
+ break;
case '"':
end = parse_string(beg, out, out_len, &filled);
break;