]> Savannah Git Hosting - gnulib.git/commitdiff
clean-temp: Add support for temporary files with given mode.
authorBruno Haible <bruno@clisp.org>
Sat, 4 Jul 2020 16:06:52 +0000 (18:06 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 4 Jul 2020 16:06:52 +0000 (18:06 +0200)
* 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
lib/clean-temp.c
lib/clean-temp.h

index 8e0408c019940bf5b27c8db64433575fc6c7454e..15f5dd0ff8a123a1876197e5303cff22e261fa2d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2020-07-04  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        clean-temp: Document limitations.
index 948a7edb5036cb82055d771c5f1db3b41a118d97..34ebb9b8dd27c652c6484c4504b8da7947747c03 100644 (file)
@@ -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, &params, 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.
index df7e7442c057f4b1c94b3af2b8b7798dea373761..d660b18cb6e4a76ea98832784022698b2c30412b 100644 (file)
@@ -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.