From 5c5ead0337f0d90be8fe58b09a63ef6527fd316c Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Wed, 10 Dec 2014 18:40:38 +0100 Subject: [PATCH] Set correct permission for stats, manifest and ccache.conf files This fixes a regression in a07f46a3b0f7e452d50690b831bb4c99fbc6650f and fc61ca9a9232f26ee3f714d4b8738d916f6948ff where mkstemp was introduced for creating temporary files. However, mkstemp creates files with 0600 permissions, which is bad for temporary files that are renamed to a persistent name in the cache. --- util.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/util.c b/util.c index a375a4269..80eb58b6e 100644 --- a/util.c +++ b/util.c @@ -223,6 +223,21 @@ mkstemp(char *template) } #endif +#ifndef _WIN32 +static mode_t +get_umask(void) +{ + static bool mask_retrieved = false; + static mode_t mask; + if (!mask_retrieved) { + mask = umask(0); + umask(mask); + mask_retrieved = true; + } + return mask; +} +#endif + /* * Copy src to dest, decompressing src if needed. compress_level > 0 decides * whether dest will be compressed, and with which compression level. @@ -235,9 +250,6 @@ copy_file(const char *src, const char *dest, int compress_level) char buf[10240]; int n, written; char *tmp_name; -#ifndef _WIN32 - mode_t mask; -#endif struct stat st; int errnum; @@ -337,10 +349,7 @@ copy_file(const char *src, const char *dest, int compress_level) } #ifndef _WIN32 - /* get perms right on the tmp file */ - mask = umask(0); - fchmod(fd_out, 0666 & ~mask); - umask(mask); + fchmod(fd_out, 0666 & ~get_umask()); #endif /* the close can fail on NFS if out of space */ @@ -1064,6 +1073,11 @@ create_tmp_fd(char **fname) if (fd == -1) { fatal("Failed to create file %s: %s", template, strerror(errno)); } + +#ifndef _WIN32 + fchmod(fd, 0666 & ~get_umask()); +#endif + free(*fname); *fname = template; return fd; -- 2.47.2