[glib] Bug 594034 - Add g_mkstemp_full()



commit 24bec5c5bd64eb829a433e4c1e8d34dc25879a64
Author: Benjamin Otte <otte gnome org>
Date:   Thu Sep 3 15:36:37 2009 +0200

    Bug 594034 - Add g_mkstemp_full()
    
    This function exposes more variables than g_mkstemp() and therefor
    allows more flexibility when creating temporary files.
    The intended use is gio's code for g_file_replace() (see next patch)

 docs/reference/glib/glib-sections.txt   |    1 +
 docs/reference/glib/tmpl/fileutils.sgml |   11 +++++++
 glib/gfileutils.c                       |   44 ++++++++++++++++++++++++-------
 glib/gfileutils.h                       |    3 ++
 glib/glib.symbols                       |    1 +
 5 files changed, 50 insertions(+), 10 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index d10443e..4e7ae1b 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -1105,6 +1105,7 @@ g_file_get_contents
 g_file_set_contents
 g_file_test
 g_mkstemp
+g_mkstemp_full
 g_file_open_tmp
 g_file_read_link
 g_mkdir_with_parents
diff --git a/docs/reference/glib/tmpl/fileutils.sgml b/docs/reference/glib/tmpl/fileutils.sgml
index eeef84c..817aeb6 100644
--- a/docs/reference/glib/tmpl/fileutils.sgml
+++ b/docs/reference/glib/tmpl/fileutils.sgml
@@ -202,6 +202,17 @@ A test to perform on a file using g_file_test().
 @Returns: 
 
 
+<!-- ##### FUNCTION g_mkstemp_full ##### -->
+<para>
+
+</para>
+
+ tmpl: 
+ flags: 
+ mode: 
+ Returns: 
+
+
 <!-- ##### FUNCTION g_file_open_tmp ##### -->
 <para>
 
diff --git a/glib/gfileutils.c b/glib/gfileutils.c
index 13f8627..fbd124d 100644
--- a/glib/gfileutils.c
+++ b/glib/gfileutils.c
@@ -54,9 +54,6 @@
 
 #include "galias.h"
 
-static gint create_temp_file (gchar *tmpl, 
-			      int    permissions);
-
 /**
  * g_mkdir_with_parents:
  * @pathname: a pathname in the GLib file name encoding
@@ -883,7 +880,7 @@ write_to_temp_file (const gchar  *contents,
   tmp_name = g_strdup_printf ("%s.XXXXXX", dest_file);
 
   errno = 0;
-  fd = create_temp_file (tmp_name, 0666);
+  fd = g_mkstemp_full (tmp_name, O_RDWR | O_BINARY, 0666);
   save_errno = errno;
 
   display_name = g_filename_display_name (tmp_name);
@@ -1144,13 +1141,37 @@ g_file_set_contents (const gchar  *filename,
   return retval;
 }
 
+/**
+ * g_mkstemp_full:
+ * @tmpl: template filename
+ * @flags: flags to pass to an open() call in addition to O_EXCL and 
+ *         O_CREAT, which are passed automatically
+ * @mode: permissios to create the temporary file with
+ *
+ * Opens a temporary file. See the mkstemp() documentation
+ * on most UNIX-like systems. 
+ *
+ * The parameter is a string that should follow the rules for
+ * mkstemp() templates, i.e. contain the string "XXXXXX". 
+ * g_mkstemp_full() is slightly more flexible than mkstemp()
+ * in that the sequence does not have to occur at the very end of the 
+ * template and you can pass a @mode and additional @flags. The X 
+ * string will be modified to form the name of a file that didn't exist.
+ * The string should be in the GLib file name encoding. Most importantly, 
+ * on Windows it should be in UTF-8.
+ *
+ * Return value: A file handle (as from open()) to the file
+ * opened for reading and writing. The file handle should be
+ * closed with close(). In case of errors, -1 is returned.  
+ */ 
 /*
- * create_temp_file based on the mkstemp implementation from the GNU C library.
+ * g_mkstemp_full based on the mkstemp implementation from the GNU C library.
  * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
  */
-static gint
-create_temp_file (gchar *tmpl, 
-		  int    permissions)
+gint
+g_mkstemp_full (gchar *tmpl, 
+                int    flags,
+		int    mode)
 {
   char *XXXXXX;
   int count, fd;
@@ -1161,6 +1182,9 @@ create_temp_file (gchar *tmpl,
   GTimeVal tv;
   static int counter = 0;
 
+  g_return_val_if_fail (tmpl != NULL, -1);
+
+
   /* find the last occurrence of "XXXXXX" */
   XXXXXX = g_strrstr (tmpl, "XXXXXX");
 
@@ -1192,7 +1216,7 @@ create_temp_file (gchar *tmpl,
       XXXXXX[5] = letters[v % NLETTERS];
 
       /* tmpl is in UTF-8 on Windows, thus use g_open() */
-      fd = g_open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, permissions);
+      fd = g_open (tmpl, flags | O_CREAT | O_EXCL, mode);
 
       if (fd >= 0)
 	return fd;
@@ -1232,7 +1256,7 @@ create_temp_file (gchar *tmpl,
 gint
 g_mkstemp (gchar *tmpl)
 {
-  return create_temp_file (tmpl, 0600);
+  return g_mkstemp_full (tmpl, O_RDWR | O_BINARY, 0600);
 }
 
 /**
diff --git a/glib/gfileutils.h b/glib/gfileutils.h
index 9a4b2af..d8f9d3b 100644
--- a/glib/gfileutils.h
+++ b/glib/gfileutils.h
@@ -99,6 +99,9 @@ gchar   *g_file_read_link    (const gchar  *filename,
 
 /* Wrapper / workalike for mkstemp() */
 gint    g_mkstemp            (gchar        *tmpl);
+gint    g_mkstemp_full       (gchar        *tmpl,
+                              int           flags,
+                              int           mode);
 
 /* Wrapper for g_mkstemp */
 gint    g_file_open_tmp      (const gchar  *tmpl,
diff --git a/glib/glib.symbols b/glib/glib.symbols
index 80f3382..0a96980 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -378,6 +378,7 @@ g_format_size_for_display
 #ifndef _WIN64
 g_mkstemp PRIVATE
 #endif
+g_mkstemp_full
 g_mkdir_with_parents
 #ifdef G_OS_WIN32
 g_file_get_contents_utf8



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