From a87004ed6644d980868c2c19954e38b6d11683f3 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 2 Nov 2022 18:38:29 +0000 Subject: [PATCH] util: Allow setting file mode for temporary files Fixes: #12974 Signed-off-by: Michael Tremer --- src/libpakfire/build.c | 4 ++-- src/libpakfire/downloader.c | 2 +- src/libpakfire/include/pakfire/util.h | 2 +- src/libpakfire/packager.c | 2 +- src/libpakfire/repo.c | 2 +- src/libpakfire/request.c | 2 +- src/libpakfire/util.c | 9 ++++++++- 7 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index c669b3abb..c5651f58f 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -233,7 +233,7 @@ static int pakfire_build_find_dependencies(struct pakfire_build* build, return 1; // Create a temporary file - FILE* f = pakfire_mktemp(path); + FILE* f = pakfire_mktemp(path, 0); if (!f) goto ERROR; @@ -408,7 +408,7 @@ static int pakfire_build_add_scriptlet_requires(struct pakfire_build* build, const char* data = pakfire_scriptlet_get_data(scriptlet, &size); // Create a temporary file - FILE* f = pakfire_mktemp(path); + FILE* f = pakfire_mktemp(path, 0); if (!f) return 1; diff --git a/src/libpakfire/downloader.c b/src/libpakfire/downloader.c index 113eb3b1b..25086512d 100644 --- a/src/libpakfire/downloader.c +++ b/src/libpakfire/downloader.c @@ -765,7 +765,7 @@ static int pakfire_downloader_prepare_transfer(struct pakfire_downloader* downlo if (r) return 1; - transfer->f = pakfire_mktemp(transfer->tempfile); + transfer->f = pakfire_mktemp(transfer->tempfile, 0); if (!transfer->f) { ERROR(downloader->pakfire, "Could not create temporary file for download %s: %m\n", transfer->tempfile); diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 944cd5e6a..3d627d1e4 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -66,7 +66,7 @@ int pakfire_file_write(struct pakfire* pakfire, const char* path, int pakfire_touch(const char* path, mode_t mode); int pakfire_mkparentdir(const char* path, mode_t mode); int pakfire_mkdir(const char* path, mode_t mode); -FILE* pakfire_mktemp(char* path); +FILE* pakfire_mktemp(char* path, const mode_t mode); char* pakfire_mkdtemp(char* path); int pakfire_rmtree(const char* path, int flags); diff --git a/src/libpakfire/packager.c b/src/libpakfire/packager.c index f2161be69..812c25725 100644 --- a/src/libpakfire/packager.c +++ b/src/libpakfire/packager.c @@ -548,7 +548,7 @@ int pakfire_packager_finish_to_directory(struct pakfire_packager* packager, goto ERROR; // Create a temporary result file - f = pakfire_mktemp(tmppath); + f = pakfire_mktemp(tmppath, 0); if (!f) goto ERROR; diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index 548828bd1..619166e95 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -1235,7 +1235,7 @@ static int pakfire_repo_write_database(struct pakfire_repo* repo, const char* pa return r; // Create a temporary file to write to - FILE* f = pakfire_mktemp(tmp); + FILE* f = pakfire_mktemp(tmp, 0644); if (!f) { ERROR(repo->pakfire, "Could not open temporary file for writing: %m\n"); return 1; diff --git a/src/libpakfire/request.c b/src/libpakfire/request.c index 2206e196d..e18a2d7bc 100644 --- a/src/libpakfire/request.c +++ b/src/libpakfire/request.c @@ -414,7 +414,7 @@ static int pakfire_request_add_url(struct pakfire_request* request, int action, char path[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-download.XXXXXX"; // Allocate a temporary file name - FILE* f = pakfire_mktemp(path); + FILE* f = pakfire_mktemp(path, 0); if (!f) return 1; diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index f81496264..21bd2d49d 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -450,7 +450,7 @@ int pakfire_mkdir(const char* path, mode_t mode) { return pakfire_try_mkdir(path, mode); } -FILE* pakfire_mktemp(char* path) { +FILE* pakfire_mktemp(char* path, const mode_t mode) { int r = pakfire_mkparentdir(path, 0755); if (r) return NULL; @@ -460,6 +460,13 @@ FILE* pakfire_mktemp(char* path) { if (fd < 0) return NULL; + // Set desired mode + if (mode) { + r = fchmod(fd, mode); + if (r) + return NULL; + } + // Re-open as file handle return fdopen(fd, "w+"); } -- 2.39.5