From 3fa68c95644b9a69c0e519987577fb1bb57bf1dc Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Mon, 17 Feb 2020 00:04:01 +0200 Subject: fix saving the output file. Signed-off-by: Gediminas Jakutis --- src/io.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/io.c b/src/io.c index 142e230..a8dfa2f 100644 --- a/src/io.c +++ b/src/io.c @@ -91,7 +91,6 @@ err: int stream_close(struct stream *in) { - char path[PATH_MAX]; int ret = 0; if (!in || in->fd < 0) { @@ -99,8 +98,26 @@ int stream_close(struct stream *in) goto early_err; } - if (in->out && in->name) { + if (!in->out) { + goto out; + } + + if (in->name) { + char path[PATH_MAX]; + struct stat st; + snprintf(path, PATH_MAX, "/proc/self/fd/%i", in->fd); + + if (!stat(in->name, &st)) { + if (st.st_mode & S_IFREG) { + unlink(in->name); + } else { + ret = EINVAL; + /* TODO: error message */ + goto err; + } + } + if (linkat(AT_FDCWD, path, AT_FDCWD, in->name, AT_SYMLINK_FOLLOW)) { ret = errno; /* TODO: error message */ @@ -111,6 +128,7 @@ int stream_close(struct stream *in) goto err; } +out: err: close(in->fd); in->fd = -1; -- cgit v1.2.3