bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: clean-temp: add support for temporary files anywhere


From: Bruno Haible
Subject: Re: clean-temp: add support for temporary files anywhere
Date: Sat, 04 Jul 2020 18:08:53 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-179-generic; KDE/5.18.0; x86_64; ; )

> 2020-07-04  Bruno Haible  <bruno@clisp.org>
> 
>       clean-temp: Add support for temporary files with unpredictable names.
>       * lib/clean-temp.h (gen_register_open_temp): New declaration.

This can be made a bit more powerful, by adding a 'mode' argument.


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.

diff --git a/lib/clean-temp.c b/lib/clean-temp.c
index 948a7ed..34ebb9b 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, &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.
diff --git a/lib/clean-temp.h b/lib/clean-temp.h
index df7e744..d660b18 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.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]