diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/io.c | 91 | ||||
-rw-r--r-- | src/io.h | 16 | ||||
-rw-r--r-- | src/main.c | 34 | ||||
-rw-r--r-- | src/meson.build | 6 |
4 files changed, 145 insertions, 2 deletions
diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..74ea274 --- /dev/null +++ b/src/io.c @@ -0,0 +1,91 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <libgen.h> +#include "io.h" + +int openstream(struct stream *in) +{ + struct stat st; + mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP; + char *dname = NULL; + int ret = 0; + + in->fd = -1; + + if (!in->name) { + ret = EINVAL; + goto err; + } + + if (in->out) { + char *tmp[2]; + + tmp[0] = strdup(in->name); + tmp[1] = dirname(tmp[0]); + dname = strdup(tmp[1]); + free(tmp[0]); + + if (stat(dname, &st)) { + ret = errno; + /* TODO: error message */ + goto err; + } + + if(!(st.st_mode & S_IFDIR)) { + ret = EINVAL; + /* TODO: error message */ + goto err; + } + + if (!stat(in->name, &st)) { + if (!(st.st_mode & S_IFREG)) { + ret = EINVAL; + /* TODO: error message */ + goto err; + } + mode = st.st_mode; + } + } else if (stat(in->name, &st)) { + ret = errno; + /* TODO: error message */ + goto err; + } else if (!(st.st_mode & S_IFREG) || !st.st_size || (st.st_size % in->stride)) { + ret = EINVAL; + /* TODO: error message */ + goto err; + } else { + in->n = st.st_size / in->stride; + } + + if (in->out) { + in->fd = open(dname, O_TMPFILE | O_WRONLY, mode); + if (in->fd < 0) { + ret = errno; + /* TODO: error message */ + goto err; + } + + if (ftruncate(in->fd, in->stride * in->n)) { + ret = errno; + /* TODO: error message */ + goto err; + } + } else { + in->fd = open(in->name, O_RDONLY | O_NOATIME); + if (in->fd < 0) { + ret = errno; + /* TODO: error message */ + goto err; + } + } + +err: + free(dname); + return ret; +} diff --git a/src/io.h b/src/io.h new file mode 100644 index 0000000..efc3f94 --- /dev/null +++ b/src/io.h @@ -0,0 +1,16 @@ +#ifndef ALGOS_IO_H_INCLUDED +#define ALGOS_IO_H_INCLUDED + +#include <stddef.h> + +struct stream { + size_t n; + size_t stride; + int fd; + int out; + char *name; +}; + +int openstream(struct stream *in); + +#endif /* ALGOS_IO_H_INCLUDED */ @@ -1,8 +1,10 @@ +#include <unistd.h> #include <errno.h> #include <stddef.h> #include <stdlib.h> #include <stdio.h> #include <string.h> +#include "io.h" static struct settings_s { size_t fetchpos; @@ -12,6 +14,9 @@ static struct settings_s { int mode; } settings = {0}; +static struct stream file_in = {.fd = -1}; +static struct stream file_out = {.fd = -1}; + static int parseargs(int argc, char **argv, struct settings_s * settings); void printhelp(const char * const name); @@ -23,7 +28,25 @@ int main(int argc, char **argv) goto out; } + file_in.name = settings.filein; + file_in.stride = 0; /* TODO */ + + if ((ret = openstream(&file_in))) { + goto out; + } + + file_out.name = settings.fileout ? settings.fileout : settings.filein; + file_out.out = 1; + file_out.n = file_in.n; + file_out.stride = 0; /* TODO */ + + if ((ret = openstream(&file_out))) { + goto out; + } + out: + close(file_in.fd); + close(file_out.fd); return ret; } @@ -39,9 +62,9 @@ static int parseargs(int argc, char **argv, struct settings_s * settings) for (i = 1; i < argc - 1; ++i) { if (!(strcmp(argv[i], "--sort"))) { - s.mode = 0; + s.mode &= ~1; } else if (!(strcmp(argv[i], "--fetch"))) { - s.mode = 1; + s.mode |= 1; } else if (!(strncmp(argv[i], "--position=", 11))) { if (strlen(argv[i]) > 11) { s.fetchpos = strtoul(argv[i] + 11, NULL, 10); @@ -71,7 +94,14 @@ static int parseargs(int argc, char **argv, struct settings_s * settings) s.filein = argv[i]; } + if (!s.fileout) { + s.fileout = s.filein; + } + + s.fetchto = s.fetchpos + s.fetchto; + *settings = s; + if (0) { err: ret = EINVAL; diff --git a/src/meson.build b/src/meson.build index d639f02..13ff4d7 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,5 +1,11 @@ source_files = [ 'main.c', + 'io.c', +] + +header_files = [ + 'io.h', ] sources = files(source_files) +sources += files(header_files) |