From 36d4b7ed93c3c29505f939de6725b7f68b860fa5 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 4 Jul 2020 18:06:52 +0200 Subject: [PATCH] clean-temp: Add support for temporary files with given mode. * lib/clean-temp.h (gen_register_open_temp): Add mode argument. * lib/clean-temp.c (struct try_create_file_params): New type. (try_create_file): New function. (gen_register_open_temp): Add mode argument. Use try_tempname instead of gen_tempname. --- ChangeLog | 9 +++++++++ lib/clean-temp.c | 31 ++++++++++++++++++++++++++++--- lib/clean-temp.h | 6 ++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8e0408c019..15f5dd0ff8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2020-07-04 Bruno Haible + + clean-temp: Add support for temporary files with given mode. + * lib/clean-temp.h (gen_register_open_temp): Add mode argument. + * lib/clean-temp.c (struct try_create_file_params): New type. + (try_create_file): New function. + (gen_register_open_temp): Add mode argument. Use try_tempname instead of + gen_tempname. + 2020-07-04 Bruno Haible clean-temp: Document limitations. diff --git a/lib/clean-temp.c b/lib/clean-temp.c index 948a7edb50..34ebb9b8dd 100644 --- a/lib/clean-temp.c +++ b/lib/clean-temp.c @@ -962,18 +962,42 @@ fopen_temp (const char *file_name, const char *mode, bool delete_on_close) } #if GNULIB_TEMPNAME + +struct try_create_file_params +{ + int flags; + mode_t mode; +}; + +static int +try_create_file (char *file_name_tmpl, void *params_) +{ + struct try_create_file_params *params = params_; + return open (file_name_tmpl, + (params->flags & ~O_ACCMODE) | O_RDWR | O_CREAT | O_EXCL, + params->mode); +} + /* Open a temporary file, generating its name based on FILE_NAME_TMPL. FILE_NAME_TMPL must match the rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix). The name constructed does not exist at the time of the call. FILE_NAME_TMPL is overwritten with the result. + A safe choice for MODE is S_IRUSR | S_IWUSR, a.k.a. 0600. Registers the file for deletion. - Opens the file, with the given FLAGS and mode 0600. + Opens the file, with the given FLAGS and mode MODE. Registers the resulting file descriptor to be closed. */ int -gen_register_open_temp (char *file_name_tmpl, int suffixlen, int flags) +gen_register_open_temp (char *file_name_tmpl, int suffixlen, + int flags, mode_t mode) { block_fatal_signals (); - int fd = gen_tempname (file_name_tmpl, suffixlen, flags, GT_FILE); + + struct try_create_file_params params; + params.flags = flags; + params.mode = mode; + + int fd = try_tempname (file_name_tmpl, suffixlen, ¶ms, try_create_file); + int saved_errno = errno; if (fd >= 0) { @@ -985,6 +1009,7 @@ gen_register_open_temp (char *file_name_tmpl, int suffixlen, int flags) errno = saved_errno; return fd; } + #endif /* Close a temporary file. diff --git a/lib/clean-temp.h b/lib/clean-temp.h index df7e7442c0..d660b18cb6 100644 --- a/lib/clean-temp.h +++ b/lib/clean-temp.h @@ -164,10 +164,12 @@ extern FILE * fopen_temp (const char *file_name, const char *mode, FILE_NAME_TMPL must match the rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix). The name constructed does not exist at the time of the call. FILE_NAME_TMPL is overwritten with the result. + A safe choice for MODE is S_IRUSR | S_IWUSR, a.k.a. 0600. Registers the file for deletion. - Opens the file, with the given FLAGS and mode 0600. + Opens the file, with the given FLAGS and mode MODE. Registers the resulting file descriptor to be closed. */ -extern int gen_register_open_temp (char *file_name_tmpl, int suffixlen, int flags); +extern int gen_register_open_temp (char *file_name_tmpl, int suffixlen, + int flags, mode_t mode); /* Close a temporary file. FD must have been returned by open_temp or gen_register_open_temp. -- 2.39.5