diff options
-rw-r--r-- | src/io.c | 22 |
1 files changed, 20 insertions, 2 deletions
@@ -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; |