diff options
Diffstat (limited to 'src/io.c')
-rw-r--r-- | src/io.c | 36 |
1 files changed, 32 insertions, 4 deletions
@@ -1,5 +1,6 @@ #include <sys/types.h> #include <sys/stat.h> +#include <linux/limits.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> @@ -9,16 +10,14 @@ #include <libgen.h> #include "io.h" -int openstream(struct stream *in) +int stream_open(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) { + if (!in || in->fd > 0 || !in->name) { ret = EINVAL; goto err; } @@ -89,3 +88,32 @@ err: free(dname); return ret; } + +int stream_close(struct stream *in) +{ + char path[PATH_MAX]; + int ret = 0; + + if (!in || in->fd < 0) { + ret = EINVAL; + goto early_err; + } + + if (in->out && in->name) { + snprintf(path, PATH_MAX, "/proc/self/fd/%i", in->fd); + if (linkat(AT_FDCWD, path, AT_FDCWD, in->name, AT_SYMLINK_FOLLOW)) { + ret = errno; + /* TODO: error message */ + goto err; + } + } else { + ret = EINVAL; + goto err; + } + +err: + close(in->fd); + in->fd = -1; +early_err: + return ret; +} |