From 93159d978b0ce0b10bba47f2777d75ecbce724db Mon Sep 17 00:00:00 2001 From: Michael Schroeder Date: Tue, 6 Apr 2021 16:01:29 +0200 Subject: [PATCH] Make zchunkopen a bit easier to understand --- ext/solv_xfopen.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ext/solv_xfopen.c b/ext/solv_xfopen.c index 7dca41f0..369003b4 100644 --- a/ext/solv_xfopen.c +++ b/ext/solv_xfopen.c @@ -598,7 +598,6 @@ static void *zchunkopen(const char *path, const char *mode, int fd) { FILE *fp; void *f; - int tmpfd; if ((!path && fd < 0) || (path && fd >= 0)) return 0; if (strcmp(mode, "r") != 0) @@ -612,17 +611,20 @@ static void *zchunkopen(const char *path, const char *mode, int fd) f = solv_zchunk_open(fp, 1); if (!f) { - /* When 0 is returned, fd passed by user must not be closed! */ - /* Dup (save) the original fd to a temporary variable and then back. */ - /* It is ugly and thread unsafe hack (non atomical sequence fclose dup2). */ - if (!path) - tmpfd = dup(fd); - fclose(fp); if (!path) { + /* The fd passed by user must not be closed! */ + /* Dup (save) the original fd to a temporary variable and then back. */ + /* It is ugly and thread unsafe hack (non atomical sequence fclose dup2). */ + int tmpfd = dup(fd); + fclose(fp); dup2(tmpfd, fd); close(tmpfd); } + else + { + fclose(fp); + } } return cookieopen(f, mode, (ssize_t (*)(void *, char *, size_t))solv_zchunk_read, 0, (int (*)(void *))solv_zchunk_close); } -- 2.47.2