From 41efe7b8f9f67d5956ab677f3631478c48114ac1 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Tue, 9 Feb 2021 05:43:36 +0200 Subject: A major overhaul of the whole thing. It has come to my attention that previously, the project was built on top of some *very* incorrect assumptions. This patch mostly addresses that. And while it does NOT leave the project in otherwise working state, it does weed out most, if not all, of effects of previously incorrect assumptions, so it can be built the right way from here on forth. Signed-off-by: Gediminas Jakutis --- src/io.c | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'src/io.c') diff --git a/src/io.c b/src/io.c index 688ec35..d323573 100644 --- a/src/io.c +++ b/src/io.c @@ -17,8 +17,9 @@ #include "datagen.h" #include "cache.h" -static int stream_open_in(struct stream * const in, const struct settings * const s); static int stream_open_out(struct stream * const in, const struct settings * const s); +static int stream_open_out_lite(struct stream * const in, const struct settings * const s); +static int stream_open_in(struct stream * const in, const struct settings * const s); static int stream_open_special(struct stream * const in); int stream_open(struct stream * const in, const struct settings * const s) @@ -27,9 +28,13 @@ int stream_open(struct stream * const in, const struct settings * const s) try(!in || in->fd > 0 || !in->name || !s, err, EINVAL, "invalid argunent"); - if (in->out == 1) { + if (in->out == 2) { + ret = stream_open_out_lite(in, s); + } else if (in->out == 1) { + try(!in->name, err, EINVAL, "no filename given"); ret = stream_open_out(in, s); } else if (!in->out) { + try(!in->name, err, EINVAL, "no filename given"); ret = stream_open_in(in, s); } else { ret = stream_open_special(in); @@ -101,7 +106,31 @@ static int stream_open_out(struct stream * const in, const struct settings * con mode = st.st_mode; } - in->fd = open(dname, O_TMPFILE | O_WRONLY, mode); + in->fd = open(dname, O_TMPFILE | O_RDWR, mode); + try(in->fd < 0, err, errno, "failed creating temporary output file: %s", strerror(ret)); + try(ftruncate(in->fd, s->stride * in->n), err, errno, "failed setting output file size: %s", strerror(ret)); + +err: + free(dname); + return ret; +} + +static int stream_open_out_lite(struct stream * const in, const struct settings * const s) +{ + struct stat st; + char *dname = NULL; + char *tmp[2]; + int ret = 0; + + tmp[0] = strdup(in->name); + tmp[1] = dirname(tmp[0]); + dname = strdup(tmp[1]); + free(tmp[0]); + + try(stat(dname, &st), err, errno, "stat failed: %s", strerror(ret)); + try(!(st.st_mode & S_IFDIR), err, EINVAL, "invalid output path"); + + in->fd = open(dname, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR); try(in->fd < 0, err, errno, "failed creating temporary output file: %s", strerror(ret)); try(ftruncate(in->fd, s->stride * in->n), err, errno, "failed setting output file size: %s", strerror(ret)); @@ -120,10 +149,6 @@ static int stream_open_in(struct stream * const in, const struct settings * cons in->n = st.st_size / s->stride; in->fd = open(in->name, O_RDONLY | O_NOATIME); try(in->fd < 0, err, errno, "failed opening input file: %s", strerror(ret)); - if (in->cached) { - cache_create(in, s); - cache_populate(in); - } err: return ret; @@ -141,9 +166,3 @@ static int stream_open_special(struct stream * const in) err: return ret; } - -int direct_get_array(struct stream * const restrict in, ssize_t idx, struct entry_l * const data); -int direct_get_list(struct stream * const restrict in, ssize_t idx, struct entry_l * const data); - -int direct_put_array(struct stream * const restrict in, ssize_t idx, const struct entry_l * const data); -int direct_put_list(struct stream * const restrict in, ssize_t idx, const struct entry_l * const data); -- cgit v1.2.3