Add tests.
authorMatthias Clasen <mclasen@redhat.com>
Fri, 16 Jun 2006 15:12:32 +0000 (15:12 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Fri, 16 Jun 2006 15:12:32 +0000 (15:12 +0000)
2006-06-16  Matthias Clasen  <mclasen@redhat.com>

* tests/file-test.c (test_mkstemp): Add tests.

* glib/gfileutils.c (g_mkstemp): Allow the XXXXXX to occur
inside the template, not just at the end.

ChangeLog
ChangeLog.pre-2-12
glib/gfileutils.c
tests/file-test.c

index 27168c5..9747fa3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2006-06-16  Matthias Clasen  <mclasen@redhat.com>
+
+       * tests/file-test.c (test_mkstemp): Add tests.
+
+       * glib/gfileutils.c (g_mkstemp): Allow the XXXXXX to occur
+       inside the template, not just at the end.  
+
 2006-06-14  Matthias Clasen  <mclasen@redhat.com>
        
        * glib/gkeyfile.c (g_key_file_to_data): Separate groups by
index 27168c5..9747fa3 100644 (file)
@@ -1,3 +1,10 @@
+2006-06-16  Matthias Clasen  <mclasen@redhat.com>
+
+       * tests/file-test.c (test_mkstemp): Add tests.
+
+       * glib/gfileutils.c (g_mkstemp): Allow the XXXXXX to occur
+       inside the template, not just at the end.  
+
 2006-06-14  Matthias Clasen  <mclasen@redhat.com>
        
        * glib/gkeyfile.c (g_key_file_to_data): Separate groups by
index 0c0c077..ebef3a9 100644 (file)
@@ -1154,7 +1154,6 @@ static gint
 create_temp_file (gchar *tmpl, 
                  int    permissions)
 {
-  int len;
   char *XXXXXX;
   int count, fd;
   static const char letters[] =
@@ -1164,16 +1163,15 @@ create_temp_file (gchar *tmpl,
   GTimeVal tv;
   static int counter = 0;
 
-  len = strlen (tmpl);
-  if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
+  /* find the last occurrence of "XXXXXX" */
+  XXXXXX = g_strrstr (tmpl, "XXXXXX");
+
+  if (!XXXXXX || strncmp (XXXXXX, "XXXXXX", 6))
     {
       errno = EINVAL;
       return -1;
     }
 
-  /* This is where the Xs start.  */
-  XXXXXX = &tmpl[len - 6];
-
   /* Get some more or less random data.  */
   g_get_current_time (&tv);
   value = (tv.tv_usec ^ tv.tv_sec) + counter++;
@@ -1217,12 +1215,13 @@ create_temp_file (gchar *tmpl,
  * @tmpl: template filename
  *
  * Opens a temporary file. See the mkstemp() documentation
- * on most UNIX-like systems. This is a portability wrapper, which simply calls 
- * mkstemp() on systems that have it, and implements 
- * it in GLib otherwise.
+ * on most UNIX-like systems. 
  *
- * The parameter is a string that should match the rules for
- * mkstemp(), i.e. end in "XXXXXX". The X string will 
+ * The parameter is a string that should follow the rules for
+ * mkstemp() templates, i.e. contain the string "XXXXXX". 
+ * g_mkstemp() is slightly more flexible than mkstemp()
+ * in that the sequence does not have to occur at the very end of the 
+ * template. 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.
@@ -1230,16 +1229,12 @@ create_temp_file (gchar *tmpl,
  * Return value: A file handle (as from open()) to the file
  * opened for reading and writing. The file is opened in binary mode
  * on platforms where there is a difference. The file handle should be
- * closed with close(). In case of errors, -1 is returned.
- */
+ * closed with close(). In case of errors, -1 is returned.  
+ */ 
 gint
 g_mkstemp (gchar *tmpl)
 {
-#ifdef HAVE_MKSTEMP
-  return mkstemp (tmpl);
-#else
   return create_temp_file (tmpl, 0600);
-#endif
 }
 
 #ifdef G_OS_WIN32
@@ -1251,7 +1246,6 @@ g_mkstemp (gchar *tmpl)
 gint
 g_mkstemp (gchar *tmpl)
 {
-  int len;
   char *XXXXXX;
   int count, fd;
   static const char letters[] =
@@ -1261,16 +1255,15 @@ g_mkstemp (gchar *tmpl)
   GTimeVal tv;
   static int counter = 0;
 
-  len = strlen (tmpl);
-  if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
+  /* find the last occurrence of 'XXXXXX' */
+  XXXXXX = g_strrstr (tmpl, "XXXXXX");
+
+  if (!XXXXXX || strcmp (XXXXXX, "XXXXXX"))
     {
       errno = EINVAL;
       return -1;
     }
 
-  /* This is where the Xs start.  */
-  XXXXXX = &tmpl[len - 6];
-
   /* Get some more or less random data.  */
   g_get_current_time (&tv);
   value = (tv.tv_usec ^ tv.tv_sec) + counter++;
@@ -1315,15 +1308,16 @@ g_mkstemp (gchar *tmpl)
 
 /**
  * g_file_open_tmp:
- * @tmpl: Template for file name, as in g_mkstemp(), basename only
+ * @tmpl: Template for file name, as in g_mkstemp(), basename only,
+ *        or %NULL, to a default template
  * @name_used: location to store actual name used
  * @error: return location for a #GError
  *
  * Opens a file for writing in the preferred directory for temporary
  * files (as returned by g_get_tmp_dir()). 
  *
- * @tmpl should be a string in the GLib file name encoding ending with
- * six 'X' characters, as the parameter to g_mkstemp() (or mkstemp()).
+ * @tmpl should be a string in the GLib file name encoding containing 
+ * a sequence of six 'X' characters, as the parameter to g_mkstemp().
  * However, unlike these functions, the template should only be a
  * basename, no directory components are allowed. If template is
  * %NULL, a default template is used.
@@ -1376,14 +1370,13 @@ g_file_open_tmp (const gchar *tmpl,
       return -1;
     }
   
-  if (strlen (tmpl) < 6 ||
-      strcmp (tmpl + strlen (tmpl) - 6, "XXXXXX") != 0)
+  if (strstr (tmpl, "XXXXXX") == NULL)
     {
       gchar *display_tmpl = g_filename_display_name (tmpl);
       g_set_error (error,
                   G_FILE_ERROR,
                   G_FILE_ERROR_FAILED,
-                  _("Template '%s' doesn't end with XXXXXX"),
+                  _("Template '%s' doesn't contain XXXXXX"),
                   display_tmpl);
       g_free (display_tmpl);
       return -1;
index 42661c3..b38d165 100644 (file)
@@ -60,7 +60,13 @@ test_mkstemp (void)
   strcpy (template, "foobar");
   fd = g_mkstemp (template);
   if (fd != -1)
-    g_warning ("g_mkstemp works even if template doesn't end in XXXXXX");
+    g_warning ("g_mkstemp works even if template doesn't contain XXXXXX");
+  close (fd);
+
+  strcpy (template, "foobarXXX");
+  fd = g_mkstemp (template);
+  if (fd != -1)
+    g_warning ("g_mkstemp works even if template contains less than six X");
   close (fd);
 
   strcpy (template, "fooXXXXXX");
@@ -80,6 +86,13 @@ test_mkstemp (void)
 
   close (fd);
   remove (template);
+
+  strcpy (template, "fooXXXXXX.pdf");
+  fd = g_mkstemp (template);
+  g_assert (fd != -1 && "g_mkstemp didn't work for template fooXXXXXX.pdf");
+
+  close (fd);
+  remove (template);
 }
 
 static void