From 48e6a374bce99a96628d34b15a87fdf60e21a8d6 Mon Sep 17 00:00:00 2001 From: Tor Lillqvist Date: Mon, 5 Jan 2009 10:08:00 +0000 Subject: [PATCH] Bug 566348 - g_file_open_tmp uses the wrong g_mkstemp on win32 2009-01-05 Tor Lillqvist Bug 566348 - g_file_open_tmp uses the wrong g_mkstemp on win32 * glib/gfileutils.c: Move the binary compatibility versions of g_file_test(), g_file_get_contents(), g_mkstemp() and g_file_open_tmp() last in the file. Remove the restriction that the XXXXXX is last in the template in the binary compatibility version of g_mkstemp(). Thanks to Morten Welinder and Jody Goldberg. svn path=/trunk/; revision=7770 --- ChangeLog | 11 ++ glib/gfileutils.c | 296 ++++++++++++++++++++++++++---------------------------- 2 files changed, 153 insertions(+), 154 deletions(-) diff --git a/ChangeLog b/ChangeLog index d283e54..a461c9a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2009-01-05 Tor Lillqvist + + Bug 566348 - g_file_open_tmp uses the wrong g_mkstemp on win32 + + * glib/gfileutils.c: Move the binary compatibility versions of + g_file_test(), g_file_get_contents(), g_mkstemp() and + g_file_open_tmp() last in the file. Remove the restriction that + the XXXXXX is last in the template in the binary compatibility + version of g_mkstemp(). Thanks to Morten Welinder and Jody + Goldberg. + 2009-01-03 Matthias Clasen * NEWS: Updates diff --git a/glib/gfileutils.c b/glib/gfileutils.c index 609b439..82b1d59 100644 --- a/glib/gfileutils.c +++ b/glib/gfileutils.c @@ -316,31 +316,6 @@ g_file_test (const gchar *filename, #endif } -#if defined (G_OS_WIN32) && !defined (_WIN64) - -#undef g_file_test - -/* Binary compatibility version. Not for newly compiled code. */ - -gboolean -g_file_test (const gchar *filename, - GFileTest test) -{ - gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL); - gboolean retval; - - if (utf8_filename == NULL) - return FALSE; - - retval = g_file_test_utf8 (utf8_filename, test); - - g_free (utf8_filename); - - return retval; -} - -#endif - GQuark g_file_error_quark (void) { @@ -860,33 +835,6 @@ g_file_get_contents (const gchar *filename, #endif } -#if defined (G_OS_WIN32) && !defined (_WIN64) - -#undef g_file_get_contents - -/* Binary compatibility version. Not for newly compiled code. */ - -gboolean -g_file_get_contents (const gchar *filename, - gchar **contents, - gsize *length, - GError **error) -{ - gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error); - gboolean retval; - - if (utf8_filename == NULL) - return FALSE; - - retval = g_file_get_contents_utf8 (utf8_filename, contents, length, error); - - g_free (utf8_filename); - - return retval; -} - -#endif - static gboolean rename_file (const char *old_name, const char *new_name, @@ -1238,75 +1186,6 @@ g_mkstemp (gchar *tmpl) return create_temp_file (tmpl, 0600); } -#if defined (G_OS_WIN32) && !defined (_WIN64) - -#undef g_mkstemp - -/* Binary compatibility version. Not for newly compiled code. */ - -gint -g_mkstemp (gchar *tmpl) -{ - char *XXXXXX; - int count, fd; - static const char letters[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - static const int NLETTERS = sizeof (letters) - 1; - glong value; - GTimeVal tv; - static int counter = 0; - - /* find the last occurrence of 'XXXXXX' */ - XXXXXX = g_strrstr (tmpl, "XXXXXX"); - - if (!XXXXXX || strcmp (XXXXXX, "XXXXXX")) - { - errno = EINVAL; - return -1; - } - - /* Get some more or less random data. */ - g_get_current_time (&tv); - value = (tv.tv_usec ^ tv.tv_sec) + counter++; - - for (count = 0; count < 100; value += 7777, ++count) - { - glong v = value; - - /* Fill in the random bits. */ - XXXXXX[0] = letters[v % NLETTERS]; - v /= NLETTERS; - XXXXXX[1] = letters[v % NLETTERS]; - v /= NLETTERS; - XXXXXX[2] = letters[v % NLETTERS]; - v /= NLETTERS; - XXXXXX[3] = letters[v % NLETTERS]; - v /= NLETTERS; - XXXXXX[4] = letters[v % NLETTERS]; - v /= NLETTERS; - XXXXXX[5] = letters[v % NLETTERS]; - - /* This is the backward compatibility system codepage version, - * thus use normal open(). - */ - fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600); - - if (fd >= 0) - return fd; - else if (errno != EEXIST) - /* Any other error will apply also to other names we might - * try, and there are 2^32 or so of them, so give up now. - */ - return -1; - } - - /* We got out of the loop because we ran out of combinations to try. */ - errno = EEXIST; - return -1; -} - -#endif - /** * g_file_open_tmp: * @tmpl: Template for file name, as in g_mkstemp(), basename only, @@ -1417,39 +1296,6 @@ g_file_open_tmp (const gchar *tmpl, return retval; } -#if defined (G_OS_WIN32) && !defined (_WIN64) - -#undef g_file_open_tmp - -/* Binary compatibility version. Not for newly compiled code. */ - -gint -g_file_open_tmp (const gchar *tmpl, - gchar **name_used, - GError **error) -{ - gchar *utf8_tmpl = g_locale_to_utf8 (tmpl, -1, NULL, NULL, error); - gchar *utf8_name_used; - gint retval; - - if (utf8_tmpl == NULL) - return -1; - - retval = g_file_open_tmp_utf8 (utf8_tmpl, &utf8_name_used, error); - - if (retval == -1) - return -1; - - if (name_used) - *name_used = g_locale_from_utf8 (utf8_name_used, -1, NULL, NULL, NULL); - - g_free (utf8_name_used); - - return retval; -} - -#endif - static gchar * g_build_path_va (const gchar *separator, const gchar *first_element, @@ -1929,5 +1775,147 @@ g_file_read_link (const gchar *filename, #endif } +/* NOTE : Keep this part last to ensure nothing in this file uses the + * below binary compatibility versions. + */ +#if defined (G_OS_WIN32) && !defined (_WIN64) + +/* Binary compatibility versions. Will be called by code compiled + * against quite old (pre-2.8, I think) headers only, not from more + * recently compiled code. + */ + +#undef g_file_test + +gboolean +g_file_test (const gchar *filename, + GFileTest test) +{ + gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL); + gboolean retval; + + if (utf8_filename == NULL) + return FALSE; + + retval = g_file_test_utf8 (utf8_filename, test); + + g_free (utf8_filename); + + return retval; +} + +#undef g_file_get_contents + +gboolean +g_file_get_contents (const gchar *filename, + gchar **contents, + gsize *length, + GError **error) +{ + gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error); + gboolean retval; + + if (utf8_filename == NULL) + return FALSE; + + retval = g_file_get_contents_utf8 (utf8_filename, contents, length, error); + + g_free (utf8_filename); + + return retval; +} + +#undef g_mkstemp + +gint +g_mkstemp (gchar *tmpl) +{ + char *XXXXXX; + int count, fd; + static const char letters[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + static const int NLETTERS = sizeof (letters) - 1; + glong value; + GTimeVal tv; + static int counter = 0; + + /* find the last occurrence of 'XXXXXX' */ + XXXXXX = g_strrstr (tmpl, "XXXXXX"); + + if (!XXXXXX) + { + errno = EINVAL; + return -1; + } + + /* Get some more or less random data. */ + g_get_current_time (&tv); + value = (tv.tv_usec ^ tv.tv_sec) + counter++; + + for (count = 0; count < 100; value += 7777, ++count) + { + glong v = value; + + /* Fill in the random bits. */ + XXXXXX[0] = letters[v % NLETTERS]; + v /= NLETTERS; + XXXXXX[1] = letters[v % NLETTERS]; + v /= NLETTERS; + XXXXXX[2] = letters[v % NLETTERS]; + v /= NLETTERS; + XXXXXX[3] = letters[v % NLETTERS]; + v /= NLETTERS; + XXXXXX[4] = letters[v % NLETTERS]; + v /= NLETTERS; + XXXXXX[5] = letters[v % NLETTERS]; + + /* This is the backward compatibility system codepage version, + * thus use normal open(). + */ + fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600); + + if (fd >= 0) + return fd; + else if (errno != EEXIST) + /* Any other error will apply also to other names we might + * try, and there are 2^32 or so of them, so give up now. + */ + return -1; + } + + /* We got out of the loop because we ran out of combinations to try. */ + errno = EEXIST; + return -1; +} + +#undef g_file_open_tmp + +gint +g_file_open_tmp (const gchar *tmpl, + gchar **name_used, + GError **error) +{ + gchar *utf8_tmpl = g_locale_to_utf8 (tmpl, -1, NULL, NULL, error); + gchar *utf8_name_used; + gint retval; + + if (utf8_tmpl == NULL) + return -1; + + retval = g_file_open_tmp_utf8 (utf8_tmpl, &utf8_name_used, error); + + if (retval == -1) + return -1; + + if (name_used) + *name_used = g_locale_from_utf8 (utf8_name_used, -1, NULL, NULL, NULL); + + g_free (utf8_name_used); + + return retval; +} + +#endif + #define __G_FILEUTILS_C__ #include "galiasdef.c" -- 2.7.4