1 /* gfileutils.c - File utility functions
3 * Copyright 2000 Red Hat, Inc.
5 * GLib is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * GLib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with GLib; see the file COPYING.LIB. If not,
17 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include "glibconfig.h"
33 #include <sys/types.h>
41 #endif /* G_OS_WIN32 */
51 #include "gfileutils.h"
56 #ifdef HAVE_LINUX_MAGIC_H /* for btrfs check */
57 #include <linux/magic.h>
62 * g_mkdir_with_parents:
63 * @pathname: a pathname in the GLib file name encoding
64 * @mode: permissions to use for newly created directories
66 * Create a directory if it doesn't already exist. Create intermediate
67 * parent directories as needed, too.
69 * Returns: 0 if the directory already exists, or was successfully
70 * created. Returns -1 if an error occurred, with errno set.
75 g_mkdir_with_parents (const gchar *pathname,
80 if (pathname == NULL || *pathname == '\0')
86 fn = g_strdup (pathname);
88 if (g_path_is_absolute (fn))
89 p = (gchar *) g_path_skip_root (fn);
95 while (*p && !G_IS_DIR_SEPARATOR (*p))
103 if (!g_file_test (fn, G_FILE_TEST_EXISTS))
105 if (g_mkdir (fn, mode) == -1 && errno != EEXIST)
107 int errno_save = errno;
113 else if (!g_file_test (fn, G_FILE_TEST_IS_DIR))
121 *p++ = G_DIR_SEPARATOR;
122 while (*p && G_IS_DIR_SEPARATOR (*p))
135 * @filename: a filename to test in the GLib file name encoding
136 * @test: bitfield of #GFileTest flags
138 * Returns %TRUE if any of the tests in the bitfield @test are
139 * %TRUE. For example, <literal>(G_FILE_TEST_EXISTS |
140 * G_FILE_TEST_IS_DIR)</literal> will return %TRUE if the file exists;
141 * the check whether it's a directory doesn't matter since the existence
142 * test is %TRUE. With the current set of available tests, there's no point
143 * passing in more than one test at a time.
145 * Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
146 * so for a symbolic link to a regular file g_file_test() will return
147 * %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
149 * Note, that for a dangling symbolic link g_file_test() will return
150 * %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
152 * You should never use g_file_test() to test whether it is safe
153 * to perform an operation, because there is always the possibility
154 * of the condition changing before you actually perform the operation.
155 * For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
156 * to know whether it is safe to write to a file without being
157 * tricked into writing into a different location. It doesn't work!
159 * /* DON'T DO THIS */
160 * if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK))
162 * fd = g_open (filename, O_WRONLY);
163 * /* write to fd */
167 * Another thing to note is that %G_FILE_TEST_EXISTS and
168 * %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
169 * system call. This usually doesn't matter, but if your program
170 * is setuid or setgid it means that these tests will give you
171 * the answer for the real user ID and group ID, rather than the
172 * effective user ID and group ID.
174 * On Windows, there are no symlinks, so testing for
175 * %G_FILE_TEST_IS_SYMLINK will always return %FALSE. Testing for
176 * %G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and
177 * its name indicates that it is executable, checking for well-known
178 * extensions and those listed in the %PATHEXT environment variable.
180 * Return value: whether a test was %TRUE
183 g_file_test (const gchar *filename,
187 /* stuff missing in std vc6 api */
188 # ifndef INVALID_FILE_ATTRIBUTES
189 # define INVALID_FILE_ATTRIBUTES -1
191 # ifndef FILE_ATTRIBUTE_DEVICE
192 # define FILE_ATTRIBUTE_DEVICE 64
195 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
197 if (wfilename == NULL)
200 attributes = GetFileAttributesW (wfilename);
204 if (attributes == INVALID_FILE_ATTRIBUTES)
207 if (test & G_FILE_TEST_EXISTS)
210 if (test & G_FILE_TEST_IS_REGULAR)
212 if ((attributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) == 0)
216 if (test & G_FILE_TEST_IS_DIR)
218 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
222 /* "while" so that we can exit this "loop" with a simple "break" */
223 while (test & G_FILE_TEST_IS_EXECUTABLE)
225 const gchar *lastdot = strrchr (filename, '.');
226 const gchar *pathext = NULL, *p;
232 if (_stricmp (lastdot, ".exe") == 0 ||
233 _stricmp (lastdot, ".cmd") == 0 ||
234 _stricmp (lastdot, ".bat") == 0 ||
235 _stricmp (lastdot, ".com") == 0)
238 /* Check if it is one of the types listed in %PATHEXT% */
240 pathext = g_getenv ("PATHEXT");
244 pathext = g_utf8_casefold (pathext, -1);
246 lastdot = g_utf8_casefold (lastdot, -1);
247 extlen = strlen (lastdot);
252 const gchar *q = strchr (p, ';');
255 if (extlen == q - p &&
256 memcmp (lastdot, p, extlen) == 0)
258 g_free ((gchar *) pathext);
259 g_free ((gchar *) lastdot);
268 g_free ((gchar *) pathext);
269 g_free ((gchar *) lastdot);
275 if ((test & G_FILE_TEST_EXISTS) && (access (filename, F_OK) == 0))
278 if ((test & G_FILE_TEST_IS_EXECUTABLE) && (access (filename, X_OK) == 0))
283 /* For root, on some POSIX systems, access (filename, X_OK)
284 * will succeed even if no executable bits are set on the
285 * file. We fall through to a stat test to avoid that.
289 test &= ~G_FILE_TEST_IS_EXECUTABLE;
291 if (test & G_FILE_TEST_IS_SYMLINK)
295 if ((lstat (filename, &s) == 0) && S_ISLNK (s.st_mode))
299 if (test & (G_FILE_TEST_IS_REGULAR |
301 G_FILE_TEST_IS_EXECUTABLE))
305 if (stat (filename, &s) == 0)
307 if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
310 if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
313 /* The extra test for root when access (file, X_OK) succeeds.
315 if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
316 ((s.st_mode & S_IXOTH) ||
317 (s.st_mode & S_IXUSR) ||
318 (s.st_mode & S_IXGRP)))
328 g_file_error_quark (void)
330 return g_quark_from_static_string ("g-file-error-quark");
334 * g_file_error_from_errno:
335 * @err_no: an "errno" value
337 * Gets a #GFileError constant based on the passed-in @errno.
338 * For example, if you pass in %EEXIST this function returns
339 * #G_FILE_ERROR_EXIST. Unlike @errno values, you can portably
340 * assume that all #GFileError values will exist.
342 * Normally a #GFileError value goes into a #GError returned
343 * from a function that manipulates files. So you would use
344 * g_file_error_from_errno() when constructing a #GError.
346 * Return value: #GFileError corresponding to the given @errno
349 g_file_error_from_errno (gint err_no)
355 return G_FILE_ERROR_EXIST;
361 return G_FILE_ERROR_ISDIR;
367 return G_FILE_ERROR_ACCES;
373 return G_FILE_ERROR_NAMETOOLONG;
379 return G_FILE_ERROR_NOENT;
385 return G_FILE_ERROR_NOTDIR;
391 return G_FILE_ERROR_NXIO;
397 return G_FILE_ERROR_NODEV;
403 return G_FILE_ERROR_ROFS;
409 return G_FILE_ERROR_TXTBSY;
415 return G_FILE_ERROR_FAULT;
421 return G_FILE_ERROR_LOOP;
427 return G_FILE_ERROR_NOSPC;
433 return G_FILE_ERROR_NOMEM;
439 return G_FILE_ERROR_MFILE;
445 return G_FILE_ERROR_NFILE;
451 return G_FILE_ERROR_BADF;
457 return G_FILE_ERROR_INVAL;
463 return G_FILE_ERROR_PIPE;
469 return G_FILE_ERROR_AGAIN;
475 return G_FILE_ERROR_INTR;
481 return G_FILE_ERROR_IO;
487 return G_FILE_ERROR_PERM;
493 return G_FILE_ERROR_NOSYS;
498 return G_FILE_ERROR_FAILED;
504 get_contents_stdio (const gchar *display_filename,
513 gsize total_bytes = 0;
514 gsize total_allocated = 0;
517 g_assert (f != NULL);
523 bytes = fread (buf, 1, sizeof (buf), f);
526 while ((total_bytes + bytes + 1) > total_allocated)
529 total_allocated *= 2;
531 total_allocated = MIN (bytes + 1, sizeof (buf));
533 tmp = g_try_realloc (str, total_allocated);
540 _("Could not allocate %lu bytes to read file \"%s\""),
541 (gulong) total_allocated,
554 g_file_error_from_errno (save_errno),
555 _("Error reading file '%s': %s"),
557 g_strerror (save_errno));
562 memcpy (str + total_bytes, buf, bytes);
564 if (total_bytes + bytes < total_bytes)
569 _("File \"%s\" is too large"),
575 total_bytes += bytes;
580 if (total_allocated == 0)
582 str = g_new (gchar, 1);
586 str[total_bytes] = '\0';
589 *length = total_bytes;
606 get_contents_regfile (const gchar *display_filename,
607 struct stat *stat_buf,
618 size = stat_buf->st_size;
620 alloc_size = size + 1;
621 buf = g_try_malloc (alloc_size);
628 _("Could not allocate %lu bytes to read file \"%s\""),
636 while (bytes_read < size)
640 rc = read (fd, buf + bytes_read, size - bytes_read);
646 int save_errno = errno;
651 g_file_error_from_errno (save_errno),
652 _("Failed to read from file '%s': %s"),
654 g_strerror (save_errno));
665 buf[bytes_read] = '\0';
668 *length = bytes_read;
684 get_contents_posix (const gchar *filename,
689 struct stat stat_buf;
691 gchar *display_filename = g_filename_display_name (filename);
693 /* O_BINARY useful on Cygwin */
694 fd = open (filename, O_RDONLY|O_BINARY);
698 int save_errno = errno;
702 g_file_error_from_errno (save_errno),
703 _("Failed to open file '%s': %s"),
705 g_strerror (save_errno));
706 g_free (display_filename);
711 /* I don't think this will ever fail, aside from ENOMEM, but. */
712 if (fstat (fd, &stat_buf) < 0)
714 int save_errno = errno;
719 g_file_error_from_errno (save_errno),
720 _("Failed to get attributes of file '%s': fstat() failed: %s"),
722 g_strerror (save_errno));
723 g_free (display_filename);
728 if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
730 gboolean retval = get_contents_regfile (display_filename,
736 g_free (display_filename);
745 f = fdopen (fd, "r");
749 int save_errno = errno;
753 g_file_error_from_errno (save_errno),
754 _("Failed to open file '%s': fdopen() failed: %s"),
756 g_strerror (save_errno));
757 g_free (display_filename);
762 retval = get_contents_stdio (display_filename, f, contents, length, error);
763 g_free (display_filename);
769 #else /* G_OS_WIN32 */
772 get_contents_win32 (const gchar *filename,
779 gchar *display_filename = g_filename_display_name (filename);
782 f = g_fopen (filename, "rb");
789 g_file_error_from_errno (save_errno),
790 _("Failed to open file '%s': %s"),
792 g_strerror (save_errno));
793 g_free (display_filename);
798 retval = get_contents_stdio (display_filename, f, contents, length, error);
799 g_free (display_filename);
807 * g_file_get_contents:
808 * @filename: (type filename): name of a file to read contents from, in the GLib file name encoding
809 * @contents: (out) (array length=length) (element-type guint8): location to store an allocated string, use g_free() to free
810 * the returned string
811 * @length: (allow-none): location to store length in bytes of the contents, or %NULL
812 * @error: return location for a #GError, or %NULL
814 * Reads an entire file into allocated memory, with good error
817 * If the call was successful, it returns %TRUE and sets @contents to the file
818 * contents and @length to the length of the file contents in bytes. The string
819 * stored in @contents will be nul-terminated, so for text files you can pass
820 * %NULL for the @length argument. If the call was not successful, it returns
821 * %FALSE and sets @error. The error domain is #G_FILE_ERROR. Possible error
822 * codes are those in the #GFileError enumeration. In the error case,
823 * @contents is set to %NULL and @length is set to zero.
825 * Return value: %TRUE on success, %FALSE if an error occurred
828 g_file_get_contents (const gchar *filename,
833 g_return_val_if_fail (filename != NULL, FALSE);
834 g_return_val_if_fail (contents != NULL, FALSE);
841 return get_contents_win32 (filename, contents, length, error);
843 return get_contents_posix (filename, contents, length, error);
848 rename_file (const char *old_name,
849 const char *new_name,
853 if (g_rename (old_name, new_name) == -1)
855 int save_errno = errno;
856 gchar *display_old_name = g_filename_display_name (old_name);
857 gchar *display_new_name = g_filename_display_name (new_name);
861 g_file_error_from_errno (save_errno),
862 _("Failed to rename file '%s' to '%s': g_rename() failed: %s"),
865 g_strerror (save_errno));
867 g_free (display_old_name);
868 g_free (display_new_name);
877 write_to_temp_file (const gchar *contents,
879 const gchar *dest_file,
891 tmp_name = g_strdup_printf ("%s.XXXXXX", dest_file);
894 fd = g_mkstemp_full (tmp_name, O_RDWR | O_BINARY, 0666);
897 display_name = g_filename_display_name (tmp_name);
903 g_file_error_from_errno (save_errno),
904 _("Failed to create file '%s': %s"),
905 display_name, g_strerror (save_errno));
911 file = fdopen (fd, "wb");
917 g_file_error_from_errno (save_errno),
918 _("Failed to open file '%s' for writing: fdopen() failed: %s"),
920 g_strerror (save_errno));
934 n_written = fwrite (contents, 1, length, file);
936 if (n_written < length)
942 g_file_error_from_errno (save_errno),
943 _("Failed to write file '%s': fwrite() failed: %s"),
945 g_strerror (save_errno));
955 if (fflush (file) != 0)
961 g_file_error_from_errno (save_errno),
962 _("Failed to write file '%s': fflush() failed: %s"),
964 g_strerror (save_errno));
972 #ifdef BTRFS_SUPER_MAGIC
976 /* On Linux, on btrfs, skip the fsync since rename-over-existing is
977 * guaranteed to be atomic and this is the only case in which we
978 * would fsync() anyway.
981 if (fstatfs (fd, &buf) == 0 && buf.f_type == BTRFS_SUPER_MAGIC)
991 /* If the final destination exists and is > 0 bytes, we want to sync the
992 * newly written file to ensure the data is on disk when we rename over
993 * the destination. Otherwise if we get a system crash we can lose both
994 * the new and the old file on some filesystems. (I.E. those that don't
995 * guarantee the data is written to the disk before the metadata.)
997 if (g_lstat (dest_file, &statbuf) == 0 &&
998 statbuf.st_size > 0 &&
999 fsync (fileno (file)) != 0)
1005 g_file_error_from_errno (save_errno),
1006 _("Failed to write file '%s': fsync() failed: %s"),
1008 g_strerror (save_errno));
1011 g_unlink (tmp_name);
1018 #ifdef BTRFS_SUPER_MAGIC
1023 if (fclose (file) == EOF)
1029 g_file_error_from_errno (save_errno),
1030 _("Failed to close file '%s': fclose() failed: %s"),
1032 g_strerror (save_errno));
1035 g_unlink (tmp_name);
1040 retval = g_strdup (tmp_name);
1044 g_free (display_name);
1050 * g_file_set_contents:
1051 * @filename: (type filename): name of a file to write @contents to, in the GLib file name
1053 * @contents: (array length=length) (element-type guint8): string to write to the file
1054 * @length: length of @contents, or -1 if @contents is a nul-terminated string
1055 * @error: return location for a #GError, or %NULL
1057 * Writes all of @contents to a file named @filename, with good error checking.
1058 * If a file called @filename already exists it will be overwritten.
1060 * This write is atomic in the sense that it is first written to a temporary
1061 * file which is then renamed to the final name. Notes:
1064 * On Unix, if @filename already exists hard links to @filename will break.
1065 * Also since the file is recreated, existing permissions, access control
1066 * lists, metadata etc. may be lost. If @filename is a symbolic link,
1067 * the link itself will be replaced, not the linked file.
1070 * On Windows renaming a file will not remove an existing file with the
1071 * new name, so on Windows there is a race condition between the existing
1072 * file being removed and the temporary file being renamed.
1075 * On Windows there is no way to remove a file that is open to some
1076 * process, or mapped into memory. Thus, this function will fail if
1077 * @filename already exists and is open.
1081 * If the call was successful, it returns %TRUE. If the call was not successful,
1082 * it returns %FALSE and sets @error. The error domain is #G_FILE_ERROR.
1083 * Possible error codes are those in the #GFileError enumeration.
1085 * Note that the name for the temporary file is constructed by appending up
1086 * to 7 characters to @filename.
1088 * Return value: %TRUE on success, %FALSE if an error occurred
1093 g_file_set_contents (const gchar *filename,
1094 const gchar *contents,
1098 gchar *tmp_filename;
1100 GError *rename_error = NULL;
1102 g_return_val_if_fail (filename != NULL, FALSE);
1103 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1104 g_return_val_if_fail (contents != NULL || length == 0, FALSE);
1105 g_return_val_if_fail (length >= -1, FALSE);
1108 length = strlen (contents);
1110 tmp_filename = write_to_temp_file (contents, length, filename, error);
1118 if (!rename_file (tmp_filename, filename, &rename_error))
1122 g_unlink (tmp_filename);
1123 g_propagate_error (error, rename_error);
1127 #else /* G_OS_WIN32 */
1129 /* Renaming failed, but on Windows this may just mean
1130 * the file already exists. So if the target file
1131 * exists, try deleting it and do the rename again.
1133 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1135 g_unlink (tmp_filename);
1136 g_propagate_error (error, rename_error);
1141 g_error_free (rename_error);
1143 if (g_unlink (filename) == -1)
1145 gchar *display_filename = g_filename_display_name (filename);
1147 int save_errno = errno;
1151 g_file_error_from_errno (save_errno),
1152 _("Existing file '%s' could not be removed: g_unlink() failed: %s"),
1154 g_strerror (save_errno));
1156 g_free (display_filename);
1157 g_unlink (tmp_filename);
1162 if (!rename_file (tmp_filename, filename, error))
1164 g_unlink (tmp_filename);
1175 g_free (tmp_filename);
1180 * get_tmp_file based on the mkstemp implementation from the GNU C library.
1181 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
1183 typedef gint (*GTmpFileCallback) (gchar *, gint, gint);
1186 get_tmp_file (gchar *tmpl,
1193 static const char letters[] =
1194 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1195 static const int NLETTERS = sizeof (letters) - 1;
1198 static int counter = 0;
1200 g_return_val_if_fail (tmpl != NULL, -1);
1202 /* find the last occurrence of "XXXXXX" */
1203 XXXXXX = g_strrstr (tmpl, "XXXXXX");
1205 if (!XXXXXX || strncmp (XXXXXX, "XXXXXX", 6))
1211 /* Get some more or less random data. */
1212 g_get_current_time (&tv);
1213 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
1215 for (count = 0; count < 100; value += 7777, ++count)
1219 /* Fill in the random bits. */
1220 XXXXXX[0] = letters[v % NLETTERS];
1222 XXXXXX[1] = letters[v % NLETTERS];
1224 XXXXXX[2] = letters[v % NLETTERS];
1226 XXXXXX[3] = letters[v % NLETTERS];
1228 XXXXXX[4] = letters[v % NLETTERS];
1230 XXXXXX[5] = letters[v % NLETTERS];
1232 fd = f (tmpl, flags, mode);
1236 else if (errno != EEXIST)
1237 /* Any other error will apply also to other names we might
1238 * try, and there are 2^32 or so of them, so give up now.
1243 /* We got out of the loop because we ran out of combinations to try. */
1249 wrap_mkdir (gchar *tmpl,
1250 int flags G_GNUC_UNUSED,
1253 /* tmpl is in UTF-8 on Windows, thus use g_mkdir() */
1254 return g_mkdir (tmpl, mode);
1259 * @tmpl: (type filename): template directory name
1260 * @mode: permissions to create the temporary directory with
1262 * Creates a temporary directory. See the mkdtemp() documentation
1263 * on most UNIX-like systems.
1265 * The parameter is a string that should follow the rules for
1266 * mkdtemp() templates, i.e. contain the string "XXXXXX".
1267 * g_mkdtemp() is slightly more flexible than mkdtemp() in that the
1268 * sequence does not have to occur at the very end of the template
1269 * and you can pass a @mode. The X string will be modified to form
1270 * the name of a directory that didn't exist. The string should be
1271 * in the GLib file name encoding. Most importantly, on Windows it
1272 * should be in UTF-8.
1274 * Return value: A pointer to @tmpl, which has been modified
1275 * to hold the directory name. In case of errors, %NULL is
1276 * returned, and %errno will be set.
1281 g_mkdtemp_full (gchar *tmpl,
1284 if (get_tmp_file (tmpl, wrap_mkdir, 0, mode) == -1)
1292 * @tmpl: (type filename): template directory name
1294 * Creates a temporary directory. See the mkdtemp() documentation
1295 * on most UNIX-like systems.
1297 * The parameter is a string that should follow the rules for
1298 * mkdtemp() templates, i.e. contain the string "XXXXXX".
1299 * g_mkdtemp() is slightly more flexible than mkdtemp() in that the
1300 * sequence does not have to occur at the very end of the template
1301 * and you can pass a @mode and additional @flags. The X string will
1302 * be modified to form the name of a directory that didn't exist.
1303 * The string should be in the GLib file name encoding. Most importantly,
1304 * on Windows it should be in UTF-8.
1306 * Return value: A pointer to @tmpl, which has been modified
1307 * to hold the directory name. In case of errors, %NULL is
1308 * returned and %errno will be set.
1313 g_mkdtemp (gchar *tmpl)
1315 return g_mkdtemp_full (tmpl, 0700);
1320 * @tmpl: (type filename): template filename
1321 * @flags: flags to pass to an open() call in addition to O_EXCL
1322 * and O_CREAT, which are passed automatically
1323 * @mode: permissions to create the temporary file with
1325 * Opens a temporary file. See the mkstemp() documentation
1326 * on most UNIX-like systems.
1328 * The parameter is a string that should follow the rules for
1329 * mkstemp() templates, i.e. contain the string "XXXXXX".
1330 * g_mkstemp_full() is slightly more flexible than mkstemp()
1331 * in that the sequence does not have to occur at the very end of the
1332 * template and you can pass a @mode and additional @flags. The X
1333 * string will be modified to form the name of a file that didn't exist.
1334 * The string should be in the GLib file name encoding. Most importantly,
1335 * on Windows it should be in UTF-8.
1337 * Return value: A file handle (as from open()) to the file
1338 * opened for reading and writing. The file handle should be
1339 * closed with close(). In case of errors, -1 is returned
1340 * and %errno will be set.
1345 g_mkstemp_full (gchar *tmpl,
1349 /* tmpl is in UTF-8 on Windows, thus use g_open() */
1350 return get_tmp_file (tmpl, (GTmpFileCallback) g_open,
1351 flags | O_CREAT | O_EXCL, mode);
1356 * @tmpl: (type filename): template filename
1358 * Opens a temporary file. See the mkstemp() documentation
1359 * on most UNIX-like systems.
1361 * The parameter is a string that should follow the rules for
1362 * mkstemp() templates, i.e. contain the string "XXXXXX".
1363 * g_mkstemp() is slightly more flexible than mkstemp() in that the
1364 * sequence does not have to occur at the very end of the template.
1365 * The X string will be modified to form the name of a file that
1366 * didn't exist. The string should be in the GLib file name encoding.
1367 * Most importantly, on Windows it should be in UTF-8.
1369 * Return value: A file handle (as from open()) to the file
1370 * opened for reading and writing. The file is opened in binary
1371 * mode on platforms where there is a difference. The file handle
1372 * should be closed with close(). In case of errors, -1 is
1373 * returned and %errno will be set.
1376 g_mkstemp (gchar *tmpl)
1378 return g_mkstemp_full (tmpl, O_RDWR | O_BINARY, 0600);
1382 g_get_tmp_name (const gchar *tmpl,
1398 if ((slash = strchr (tmpl, G_DIR_SEPARATOR)) != NULL
1400 || (strchr (tmpl, '/') != NULL && (slash = "/"))
1404 gchar *display_tmpl = g_filename_display_name (tmpl);
1411 G_FILE_ERROR_FAILED,
1412 _("Template '%s' invalid, should not contain a '%s'"),
1414 g_free (display_tmpl);
1419 if (strstr (tmpl, "XXXXXX") == NULL)
1421 gchar *display_tmpl = g_filename_display_name (tmpl);
1424 G_FILE_ERROR_FAILED,
1425 _("Template '%s' doesn't contain XXXXXX"),
1427 g_free (display_tmpl);
1431 tmpdir = g_get_tmp_dir ();
1433 if (G_IS_DIR_SEPARATOR (tmpdir [strlen (tmpdir) - 1]))
1436 sep = G_DIR_SEPARATOR_S;
1438 fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
1440 retval = get_tmp_file (fulltemplate, f, flags, mode);
1443 int save_errno = errno;
1444 gchar *display_fulltemplate = g_filename_display_name (fulltemplate);
1448 g_file_error_from_errno (save_errno),
1449 _("Failed to create file '%s': %s"),
1450 display_fulltemplate, g_strerror (save_errno));
1451 g_free (display_fulltemplate);
1452 g_free (fulltemplate);
1456 *name_used = fulltemplate;
1463 * @tmpl: (type filename) (allow-none): Template for file name, as in
1464 * g_mkstemp(), basename only, or %NULL for a default template
1465 * @name_used: (out) (type filename): location to store actual name used,
1467 * @error: return location for a #GError
1469 * Opens a file for writing in the preferred directory for temporary
1470 * files (as returned by g_get_tmp_dir()).
1472 * @tmpl should be a string in the GLib file name encoding containing
1473 * a sequence of six 'X' characters, as the parameter to g_mkstemp().
1474 * However, unlike these functions, the template should only be a
1475 * basename, no directory components are allowed. If template is
1476 * %NULL, a default template is used.
1478 * Note that in contrast to g_mkstemp() (and mkstemp()) @tmpl is not
1479 * modified, and might thus be a read-only literal string.
1481 * Upon success, and if @name_used is non-%NULL, the actual name used
1482 * is returned in @name_used. This string should be freed with g_free()
1483 * when not needed any longer. The returned name is in the GLib file
1486 * Return value: A file handle (as from open()) to the file opened for
1487 * reading and writing. The file is opened in binary mode on platforms
1488 * where there is a difference. The file handle should be closed with
1489 * close(). In case of errors, -1 is returned and @error will be set.
1492 g_file_open_tmp (const gchar *tmpl,
1496 gchar *fulltemplate;
1499 result = g_get_tmp_name (tmpl, &fulltemplate,
1500 (GTmpFileCallback) g_open,
1501 O_CREAT | O_EXCL | O_RDWR | O_BINARY,
1507 *name_used = fulltemplate;
1509 g_free (fulltemplate);
1517 * @tmpl: (type filename) (allow-none): Template for directory name,
1518 * as in g_mkdtemp(), basename only, or %NULL for a default template
1519 * @error: return location for a #GError
1521 * Creates a subdirectory in the preferred directory for temporary
1522 * files (as returned by g_get_tmp_dir()).
1524 * @tmpl should be a string in the GLib file name encoding containing
1525 * a sequence of six 'X' characters, as the parameter to g_mkstemp().
1526 * However, unlike these functions, the template should only be a
1527 * basename, no directory components are allowed. If template is
1528 * %NULL, a default template is used.
1530 * Note that in contrast to g_mkdtemp() (and mkdtemp()) @tmpl is not
1531 * modified, and might thus be a read-only literal string.
1533 * Return value: (type filename): The actual name used. This string
1534 * should be freed with g_free() when not needed any longer and is
1535 * is in the GLib file name encoding. In case of errors, %NULL is
1536 * returned and @error will be set.
1541 g_dir_make_tmp (const gchar *tmpl,
1544 gchar *fulltemplate;
1546 if (g_get_tmp_name (tmpl, &fulltemplate, wrap_mkdir, 0, 0700, error) == -1)
1549 return fulltemplate;
1553 g_build_path_va (const gchar *separator,
1554 const gchar *first_element,
1559 gint separator_len = strlen (separator);
1560 gboolean is_first = TRUE;
1561 gboolean have_leading = FALSE;
1562 const gchar *single_element = NULL;
1563 const gchar *next_element;
1564 const gchar *last_trailing = NULL;
1567 result = g_string_new (NULL);
1570 next_element = str_array[i++];
1572 next_element = first_element;
1576 const gchar *element;
1582 element = next_element;
1584 next_element = str_array[i++];
1586 next_element = va_arg (*args, gchar *);
1591 /* Ignore empty elements */
1599 while (strncmp (start, separator, separator_len) == 0)
1600 start += separator_len;
1603 end = start + strlen (start);
1607 while (end >= start + separator_len &&
1608 strncmp (end - separator_len, separator, separator_len) == 0)
1609 end -= separator_len;
1611 last_trailing = end;
1612 while (last_trailing >= element + separator_len &&
1613 strncmp (last_trailing - separator_len, separator, separator_len) == 0)
1614 last_trailing -= separator_len;
1618 /* If the leading and trailing separator strings are in the
1619 * same element and overlap, the result is exactly that element
1621 if (last_trailing <= start)
1622 single_element = element;
1624 g_string_append_len (result, element, start - element);
1625 have_leading = TRUE;
1628 single_element = NULL;
1635 g_string_append (result, separator);
1637 g_string_append_len (result, start, end - start);
1643 g_string_free (result, TRUE);
1644 return g_strdup (single_element);
1649 g_string_append (result, last_trailing);
1651 return g_string_free (result, FALSE);
1657 * @separator: a string used to separator the elements of the path.
1658 * @args: (array zero-terminated=1): %NULL-terminated array of strings containing the path elements.
1660 * Behaves exactly like g_build_path(), but takes the path elements
1661 * as a string array, instead of varargs. This function is mainly
1662 * meant for language bindings.
1664 * Return value: a newly-allocated string that must be freed with g_free().
1669 g_build_pathv (const gchar *separator,
1675 return g_build_path_va (separator, NULL, NULL, args);
1681 * @separator: a string used to separator the elements of the path.
1682 * @first_element: the first element in the path
1683 * @...: remaining elements in path, terminated by %NULL
1685 * Creates a path from a series of elements using @separator as the
1686 * separator between elements. At the boundary between two elements,
1687 * any trailing occurrences of separator in the first element, or
1688 * leading occurrences of separator in the second element are removed
1689 * and exactly one copy of the separator is inserted.
1691 * Empty elements are ignored.
1693 * The number of leading copies of the separator on the result is
1694 * the same as the number of leading copies of the separator on
1695 * the first non-empty element.
1697 * The number of trailing copies of the separator on the result is
1698 * the same as the number of trailing copies of the separator on
1699 * the last non-empty element. (Determination of the number of
1700 * trailing copies is done without stripping leading copies, so
1701 * if the separator is <literal>ABA</literal>, <literal>ABABA</literal>
1702 * has 1 trailing copy.)
1704 * However, if there is only a single non-empty element, and there
1705 * are no characters in that element not part of the leading or
1706 * trailing separators, then the result is exactly the original value
1709 * Other than for determination of the number of leading and trailing
1710 * copies of the separator, elements consisting only of copies
1711 * of the separator are ignored.
1713 * Return value: a newly-allocated string that must be freed with g_free().
1716 g_build_path (const gchar *separator,
1717 const gchar *first_element,
1723 g_return_val_if_fail (separator != NULL, NULL);
1725 va_start (args, first_element);
1726 str = g_build_path_va (separator, first_element, &args, NULL);
1735 g_build_pathname_va (const gchar *first_element,
1739 /* Code copied from g_build_pathv(), and modified to use two
1740 * alternative single-character separators.
1743 gboolean is_first = TRUE;
1744 gboolean have_leading = FALSE;
1745 const gchar *single_element = NULL;
1746 const gchar *next_element;
1747 const gchar *last_trailing = NULL;
1748 gchar current_separator = '\\';
1751 result = g_string_new (NULL);
1754 next_element = str_array[i++];
1756 next_element = first_element;
1760 const gchar *element;
1766 element = next_element;
1768 next_element = str_array[i++];
1770 next_element = va_arg (*args, gchar *);
1775 /* Ignore empty elements */
1784 (*start == '\\' || *start == '/'))
1786 current_separator = *start;
1791 end = start + strlen (start);
1795 while (end >= start + 1 &&
1796 (end[-1] == '\\' || end[-1] == '/'))
1798 current_separator = end[-1];
1802 last_trailing = end;
1803 while (last_trailing >= element + 1 &&
1804 (last_trailing[-1] == '\\' || last_trailing[-1] == '/'))
1809 /* If the leading and trailing separator strings are in the
1810 * same element and overlap, the result is exactly that element
1812 if (last_trailing <= start)
1813 single_element = element;
1815 g_string_append_len (result, element, start - element);
1816 have_leading = TRUE;
1819 single_element = NULL;
1826 g_string_append_len (result, ¤t_separator, 1);
1828 g_string_append_len (result, start, end - start);
1834 g_string_free (result, TRUE);
1835 return g_strdup (single_element);
1840 g_string_append (result, last_trailing);
1842 return g_string_free (result, FALSE);
1849 * g_build_filenamev:
1850 * @args: (array zero-terminated=1): %NULL-terminated array of strings containing the path elements.
1852 * Behaves exactly like g_build_filename(), but takes the path elements
1853 * as a string array, instead of varargs. This function is mainly
1854 * meant for language bindings.
1856 * Return value: a newly-allocated string that must be freed with g_free().
1861 g_build_filenamev (gchar **args)
1866 str = g_build_path_va (G_DIR_SEPARATOR_S, NULL, NULL, args);
1868 str = g_build_pathname_va (NULL, NULL, args);
1876 * @first_element: the first element in the path
1877 * @...: remaining elements in path, terminated by %NULL
1879 * Creates a filename from a series of elements using the correct
1880 * separator for filenames.
1882 * On Unix, this function behaves identically to <literal>g_build_path
1883 * (G_DIR_SEPARATOR_S, first_element, ....)</literal>.
1885 * On Windows, it takes into account that either the backslash
1886 * (<literal>\</literal> or slash (<literal>/</literal>) can be used
1887 * as separator in filenames, but otherwise behaves as on Unix. When
1888 * file pathname separators need to be inserted, the one that last
1889 * previously occurred in the parameters (reading from left to right)
1892 * No attempt is made to force the resulting filename to be an absolute
1893 * path. If the first element is a relative path, the result will
1894 * be a relative path.
1896 * Return value: a newly-allocated string that must be freed with g_free().
1899 g_build_filename (const gchar *first_element,
1905 va_start (args, first_element);
1907 str = g_build_path_va (G_DIR_SEPARATOR_S, first_element, &args, NULL);
1909 str = g_build_pathname_va (first_element, &args, NULL);
1916 #define KILOBYTE_FACTOR (G_GOFFSET_CONSTANT (1000))
1917 #define MEGABYTE_FACTOR (KILOBYTE_FACTOR * KILOBYTE_FACTOR)
1918 #define GIGABYTE_FACTOR (MEGABYTE_FACTOR * KILOBYTE_FACTOR)
1919 #define TERABYTE_FACTOR (GIGABYTE_FACTOR * KILOBYTE_FACTOR)
1920 #define PETABYTE_FACTOR (TERABYTE_FACTOR * KILOBYTE_FACTOR)
1921 #define EXABYTE_FACTOR (PETABYTE_FACTOR * KILOBYTE_FACTOR)
1923 #define KIBIBYTE_FACTOR (G_GOFFSET_CONSTANT (1024))
1924 #define MEBIBYTE_FACTOR (KIBIBYTE_FACTOR * KIBIBYTE_FACTOR)
1925 #define GIBIBYTE_FACTOR (MEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
1926 #define TEBIBYTE_FACTOR (GIBIBYTE_FACTOR * KIBIBYTE_FACTOR)
1927 #define PEBIBYTE_FACTOR (TEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
1928 #define EXBIBYTE_FACTOR (PEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
1932 * @size: a size in bytes
1934 * Formats a size (for example the size of a file) into a human readable
1935 * string. Sizes are rounded to the nearest size prefix (kB, MB, GB)
1936 * and are displayed rounded to the nearest tenth. E.g. the file size
1937 * 3292528 bytes will be converted into the string "3.2 MB".
1939 * The prefix units base is 1000 (i.e. 1 kB is 1000 bytes).
1941 * This string should be freed with g_free() when not needed any longer.
1943 * See g_format_size_full() for more options about how the size might be
1946 * Returns: a newly-allocated formatted string containing a human readable
1952 g_format_size (guint64 size)
1954 return g_format_size_full (size, G_FORMAT_SIZE_DEFAULT);
1958 * g_format_size_full:
1959 * @size: a size in bytes
1960 * @flags: #GFormatSizeFlags to modify the output
1964 * This function is similar to g_format_size() but allows for flags that
1965 * modify the output. See #GFormatSizeFlags.
1967 * Returns: a newly-allocated formatted string containing a human
1968 * readable file size.
1974 * @G_FORMAT_SIZE_DEFAULT: behave the same as g_format_size()
1975 * @G_FORMAT_SIZE_LONG_FORMAT: include the exact number of bytes as part
1976 * of the returned string. For example,
1977 * "45.6 kB (45,612 bytes)".
1978 * @G_FORMAT_SIZE_IEC_UNITS: use IEC (base 1024) units with "KiB"-style
1979 * suffixes. IEC units should only be used
1980 * for reporting things with a strong "power
1981 * of 2" basis, like RAM sizes or RAID stripe
1982 * sizes. Network and storage sizes should
1983 * be reported in the normal SI units.
1985 * Flags to modify the format of the string returned by
1986 * g_format_size_full().
1989 g_format_size_full (guint64 size,
1990 GFormatSizeFlags flags)
1994 string = g_string_new (NULL);
1996 if (flags & G_FORMAT_SIZE_IEC_UNITS)
1998 if (size < KIBIBYTE_FACTOR)
2000 g_string_printf (string,
2001 g_dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes", (guint) size),
2003 flags &= ~G_FORMAT_SIZE_LONG_FORMAT;
2006 else if (size < MEBIBYTE_FACTOR)
2007 g_string_printf (string, _("%.1f KiB"), (gdouble) size / (gdouble) KIBIBYTE_FACTOR);
2009 else if (size < GIBIBYTE_FACTOR)
2010 g_string_printf (string, _("%.1f MiB"), (gdouble) size / (gdouble) MEBIBYTE_FACTOR);
2012 else if (size < TEBIBYTE_FACTOR)
2013 g_string_printf (string, _("%.1f GiB"), (gdouble) size / (gdouble) GIBIBYTE_FACTOR);
2015 else if (size < PEBIBYTE_FACTOR)
2016 g_string_printf (string, _("%.1f TiB"), (gdouble) size / (gdouble) TEBIBYTE_FACTOR);
2018 else if (size < EXBIBYTE_FACTOR)
2019 g_string_printf (string, _("%.1f PiB"), (gdouble) size / (gdouble) PEBIBYTE_FACTOR);
2022 g_string_printf (string, _("%.1f EiB"), (gdouble) size / (gdouble) EXBIBYTE_FACTOR);
2026 if (size < KILOBYTE_FACTOR)
2028 g_string_printf (string,
2029 g_dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes", (guint) size),
2031 flags &= ~G_FORMAT_SIZE_LONG_FORMAT;
2034 else if (size < MEGABYTE_FACTOR)
2035 g_string_printf (string, _("%.1f kB"), (gdouble) size / (gdouble) KILOBYTE_FACTOR);
2037 else if (size < GIGABYTE_FACTOR)
2038 g_string_printf (string, _("%.1f MB"), (gdouble) size / (gdouble) MEGABYTE_FACTOR);
2040 else if (size < TERABYTE_FACTOR)
2041 g_string_printf (string, _("%.1f GB"), (gdouble) size / (gdouble) GIGABYTE_FACTOR);
2043 else if (size < PETABYTE_FACTOR)
2044 g_string_printf (string, _("%.1f TB"), (gdouble) size / (gdouble) TERABYTE_FACTOR);
2046 else if (size < EXABYTE_FACTOR)
2047 g_string_printf (string, _("%.1f PB"), (gdouble) size / (gdouble) PETABYTE_FACTOR);
2050 g_string_printf (string, _("%.1f EB"), (gdouble) size / (gdouble) EXABYTE_FACTOR);
2053 if (flags & G_FORMAT_SIZE_LONG_FORMAT)
2055 /* First problem: we need to use the number of bytes to decide on
2056 * the plural form that is used for display, but the number of
2057 * bytes potentially exceeds the size of a guint (which is what
2058 * ngettext() takes).
2060 * From a pragmatic standpoint, it seems that all known languages
2061 * base plural forms on one or both of the following:
2063 * - the lowest digits of the number
2065 * - if the number if greater than some small value
2067 * Here's how we fake it: Draw an arbitrary line at one thousand.
2068 * If the number is below that, then fine. If it is above it,
2069 * then we take the modulus of the number by one thousand (in
2070 * order to keep the lowest digits) and add one thousand to that
2071 * (in order to ensure that 1001 is not treated the same as 1).
2073 guint plural_form = size < 1000 ? size : size % 1000 + 1000;
2075 /* Second problem: we need to translate the string "%u byte" and
2076 * "%u bytes" for pluralisation, but the correct number format to
2077 * use for a gsize is different depending on which architecture
2080 * Solution: format the number separately and use "%s bytes" on
2083 const gchar *translated_format;
2084 gchar *formatted_number;
2086 /* Translators: the %s in "%s bytes" will always be replaced by a number. */
2087 translated_format = g_dngettext(GETTEXT_PACKAGE, "%s byte", "%s bytes", plural_form);
2089 /* XXX: Windows doesn't support the "'" format modifier, so we
2090 * must not use it there. Instead, just display the number
2091 * without separation. Bug #655336 is open until a solution is
2095 formatted_number = g_strdup_printf ("%'"G_GUINT64_FORMAT, size);
2097 formatted_number = g_strdup_printf ("%"G_GUINT64_FORMAT, size);
2100 g_string_append (string, " (");
2101 g_string_append_printf (string, translated_format, formatted_number);
2102 g_free (formatted_number);
2103 g_string_append (string, ")");
2106 return g_string_free (string, FALSE);
2110 * g_format_size_for_display:
2111 * @size: a size in bytes.
2113 * Formats a size (for example the size of a file) into a human readable string.
2114 * Sizes are rounded to the nearest size prefix (KB, MB, GB) and are displayed
2115 * rounded to the nearest tenth. E.g. the file size 3292528 bytes will be
2116 * converted into the string "3.1 MB".
2118 * The prefix units base is 1024 (i.e. 1 KB is 1024 bytes).
2120 * This string should be freed with g_free() when not needed any longer.
2122 * Returns: a newly-allocated formatted string containing a human readable
2125 * Deprecated:2.30: This function is broken due to its use of SI
2126 * suffixes to denote IEC units. Use g_format_size()
2131 g_format_size_for_display (goffset size)
2133 if (size < (goffset) KIBIBYTE_FACTOR)
2134 return g_strdup_printf (g_dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes",(guint) size), (guint) size);
2137 gdouble displayed_size;
2139 if (size < (goffset) MEBIBYTE_FACTOR)
2141 displayed_size = (gdouble) size / (gdouble) KIBIBYTE_FACTOR;
2142 return g_strdup_printf (_("%.1f KB"), displayed_size);
2144 else if (size < (goffset) GIBIBYTE_FACTOR)
2146 displayed_size = (gdouble) size / (gdouble) MEBIBYTE_FACTOR;
2147 return g_strdup_printf (_("%.1f MB"), displayed_size);
2149 else if (size < (goffset) TEBIBYTE_FACTOR)
2151 displayed_size = (gdouble) size / (gdouble) GIBIBYTE_FACTOR;
2152 return g_strdup_printf (_("%.1f GB"), displayed_size);
2154 else if (size < (goffset) PEBIBYTE_FACTOR)
2156 displayed_size = (gdouble) size / (gdouble) TEBIBYTE_FACTOR;
2157 return g_strdup_printf (_("%.1f TB"), displayed_size);
2159 else if (size < (goffset) EXBIBYTE_FACTOR)
2161 displayed_size = (gdouble) size / (gdouble) PEBIBYTE_FACTOR;
2162 return g_strdup_printf (_("%.1f PB"), displayed_size);
2166 displayed_size = (gdouble) size / (gdouble) EXBIBYTE_FACTOR;
2167 return g_strdup_printf (_("%.1f EB"), displayed_size);
2175 * @filename: the symbolic link
2176 * @error: return location for a #GError
2178 * Reads the contents of the symbolic link @filename like the POSIX
2179 * readlink() function. The returned string is in the encoding used
2180 * for filenames. Use g_filename_to_utf8() to convert it to UTF-8.
2182 * Returns: A newly-allocated string with the contents of the symbolic link,
2183 * or %NULL if an error occurred.
2188 g_file_read_link (const gchar *filename,
2191 #ifdef HAVE_READLINK
2197 buffer = g_malloc (size);
2201 read_size = readlink (filename, buffer, size);
2202 if (read_size < 0) {
2203 int save_errno = errno;
2204 gchar *display_filename = g_filename_display_name (filename);
2209 g_file_error_from_errno (save_errno),
2210 _("Failed to read the symbolic link '%s': %s"),
2212 g_strerror (save_errno));
2213 g_free (display_filename);
2218 if (read_size < size)
2220 buffer[read_size] = 0;
2225 buffer = g_realloc (buffer, size);
2228 g_set_error_literal (error,
2231 _("Symbolic links not supported"));
2237 /* NOTE : Keep this part last to ensure nothing in this file uses the
2238 * below binary compatibility versions.
2240 #if defined (G_OS_WIN32) && !defined (_WIN64)
2242 /* Binary compatibility versions. Will be called by code compiled
2243 * against quite old (pre-2.8, I think) headers only, not from more
2244 * recently compiled code.
2250 g_file_test (const gchar *filename,
2253 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2256 if (utf8_filename == NULL)
2259 retval = g_file_test_utf8 (utf8_filename, test);
2261 g_free (utf8_filename);
2266 #undef g_file_get_contents
2269 g_file_get_contents (const gchar *filename,
2274 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2277 if (utf8_filename == NULL)
2280 retval = g_file_get_contents_utf8 (utf8_filename, contents, length, error);
2282 g_free (utf8_filename);
2290 g_mkstemp (gchar *tmpl)
2292 /* This is the backward compatibility system codepage version,
2293 * thus use normal open().
2295 return get_tmp_file (tmpl, (GTmpFileCallback) open,
2296 O_RDWR | O_CREAT | O_EXCL, 0600);
2299 #undef g_file_open_tmp
2302 g_file_open_tmp (const gchar *tmpl,
2306 gchar *utf8_tmpl = g_locale_to_utf8 (tmpl, -1, NULL, NULL, error);
2307 gchar *utf8_name_used;
2310 if (utf8_tmpl == NULL)
2313 retval = g_file_open_tmp_utf8 (utf8_tmpl, &utf8_name_used, error);
2319 *name_used = g_locale_from_utf8 (utf8_name_used, -1, NULL, NULL, NULL);
2321 g_free (utf8_name_used);