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"
58
#ifdef HAVE_LINUX_MAGIC_H /* for btrfs check */
59
#include <linux/magic.h>
64
* g_mkdir_with_parents:
65
* @pathname: a pathname in the GLib file name encoding
66
* @mode: permissions to use for newly created directories
68
* Create a directory if it doesn't already exist. Create intermediate
69
* parent directories as needed, too.
71
* Returns: 0 if the directory already exists, or was successfully
72
* created. Returns -1 if an error occurred, with errno set.
77
g_mkdir_with_parents (const gchar *pathname,
82
if (pathname == NULL || *pathname == '\0')
88
fn = g_strdup (pathname);
90
if (g_path_is_absolute (fn))
91
p = (gchar *) g_path_skip_root (fn);
97
while (*p && !G_IS_DIR_SEPARATOR (*p))
105
if (!g_file_test (fn, G_FILE_TEST_EXISTS))
107
if (g_mkdir (fn, mode) == -1 && errno != EEXIST)
109
int errno_save = errno;
115
else if (!g_file_test (fn, G_FILE_TEST_IS_DIR))
123
*p++ = G_DIR_SEPARATOR;
124
while (*p && G_IS_DIR_SEPARATOR (*p))
137
* @filename: a filename to test in the GLib file name encoding
138
* @test: bitfield of #GFileTest flags
140
* Returns %TRUE if any of the tests in the bitfield @test are
141
* %TRUE. For example, <literal>(G_FILE_TEST_EXISTS |
142
* G_FILE_TEST_IS_DIR)</literal> will return %TRUE if the file exists;
143
* the check whether it's a directory doesn't matter since the existence
144
* test is %TRUE. With the current set of available tests, there's no point
145
* passing in more than one test at a time.
147
* Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
148
* so for a symbolic link to a regular file g_file_test() will return
149
* %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
151
* Note, that for a dangling symbolic link g_file_test() will return
152
* %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
154
* You should never use g_file_test() to test whether it is safe
155
* to perform an operation, because there is always the possibility
156
* of the condition changing before you actually perform the operation.
157
* For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
158
* to know whether it is safe to write to a file without being
159
* tricked into writing into a different location. It doesn't work!
161
* /* DON'T DO THIS */
162
* if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK))
164
* fd = g_open (filename, O_WRONLY);
165
* /* write to fd */
169
* Another thing to note is that %G_FILE_TEST_EXISTS and
170
* %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
171
* system call. This usually doesn't matter, but if your program
172
* is setuid or setgid it means that these tests will give you
173
* the answer for the real user ID and group ID, rather than the
174
* effective user ID and group ID.
176
* On Windows, there are no symlinks, so testing for
177
* %G_FILE_TEST_IS_SYMLINK will always return %FALSE. Testing for
178
* %G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and
179
* its name indicates that it is executable, checking for well-known
180
* extensions and those listed in the %PATHEXT environment variable.
182
* Return value: whether a test was %TRUE
185
g_file_test (const gchar *filename,
189
/* stuff missing in std vc6 api */
190
# ifndef INVALID_FILE_ATTRIBUTES
191
# define INVALID_FILE_ATTRIBUTES -1
193
# ifndef FILE_ATTRIBUTE_DEVICE
194
# define FILE_ATTRIBUTE_DEVICE 64
197
wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
199
if (wfilename == NULL)
202
attributes = GetFileAttributesW (wfilename);
206
if (attributes == INVALID_FILE_ATTRIBUTES)
209
if (test & G_FILE_TEST_EXISTS)
212
if (test & G_FILE_TEST_IS_REGULAR)
214
if ((attributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) == 0)
218
if (test & G_FILE_TEST_IS_DIR)
220
if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
224
/* "while" so that we can exit this "loop" with a simple "break" */
225
while (test & G_FILE_TEST_IS_EXECUTABLE)
227
const gchar *lastdot = strrchr (filename, '.');
228
const gchar *pathext = NULL, *p;
234
if (_stricmp (lastdot, ".exe") == 0 ||
235
_stricmp (lastdot, ".cmd") == 0 ||
236
_stricmp (lastdot, ".bat") == 0 ||
237
_stricmp (lastdot, ".com") == 0)
240
/* Check if it is one of the types listed in %PATHEXT% */
242
pathext = g_getenv ("PATHEXT");
246
pathext = g_utf8_casefold (pathext, -1);
248
lastdot = g_utf8_casefold (lastdot, -1);
249
extlen = strlen (lastdot);
254
const gchar *q = strchr (p, ';');
257
if (extlen == q - p &&
258
memcmp (lastdot, p, extlen) == 0)
260
g_free ((gchar *) pathext);
261
g_free ((gchar *) lastdot);
270
g_free ((gchar *) pathext);
271
g_free ((gchar *) lastdot);
277
if ((test & G_FILE_TEST_EXISTS) && (access (filename, F_OK) == 0))
280
if ((test & G_FILE_TEST_IS_EXECUTABLE) && (access (filename, X_OK) == 0))
285
/* For root, on some POSIX systems, access (filename, X_OK)
286
* will succeed even if no executable bits are set on the
287
* file. We fall through to a stat test to avoid that.
291
test &= ~G_FILE_TEST_IS_EXECUTABLE;
293
if (test & G_FILE_TEST_IS_SYMLINK)
297
if ((lstat (filename, &s) == 0) && S_ISLNK (s.st_mode))
301
if (test & (G_FILE_TEST_IS_REGULAR |
303
G_FILE_TEST_IS_EXECUTABLE))
307
if (stat (filename, &s) == 0)
309
if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
312
if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
315
/* The extra test for root when access (file, X_OK) succeeds.
317
if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
318
((s.st_mode & S_IXOTH) ||
319
(s.st_mode & S_IXUSR) ||
320
(s.st_mode & S_IXGRP)))
330
g_file_error_quark (void)
332
return g_quark_from_static_string ("g-file-error-quark");
336
* g_file_error_from_errno:
337
* @err_no: an "errno" value
339
* Gets a #GFileError constant based on the passed-in @errno.
340
* For example, if you pass in %EEXIST this function returns
341
* #G_FILE_ERROR_EXIST. Unlike @errno values, you can portably
342
* assume that all #GFileError values will exist.
344
* Normally a #GFileError value goes into a #GError returned
345
* from a function that manipulates files. So you would use
346
* g_file_error_from_errno() when constructing a #GError.
348
* Return value: #GFileError corresponding to the given @errno
351
g_file_error_from_errno (gint err_no)
357
return G_FILE_ERROR_EXIST;
363
return G_FILE_ERROR_ISDIR;
369
return G_FILE_ERROR_ACCES;
375
return G_FILE_ERROR_NAMETOOLONG;
381
return G_FILE_ERROR_NOENT;
387
return G_FILE_ERROR_NOTDIR;
393
return G_FILE_ERROR_NXIO;
399
return G_FILE_ERROR_NODEV;
405
return G_FILE_ERROR_ROFS;
411
return G_FILE_ERROR_TXTBSY;
417
return G_FILE_ERROR_FAULT;
423
return G_FILE_ERROR_LOOP;
429
return G_FILE_ERROR_NOSPC;
435
return G_FILE_ERROR_NOMEM;
441
return G_FILE_ERROR_MFILE;
447
return G_FILE_ERROR_NFILE;
453
return G_FILE_ERROR_BADF;
459
return G_FILE_ERROR_INVAL;
465
return G_FILE_ERROR_PIPE;
471
return G_FILE_ERROR_AGAIN;
477
return G_FILE_ERROR_INTR;
483
return G_FILE_ERROR_IO;
489
return G_FILE_ERROR_PERM;
495
return G_FILE_ERROR_NOSYS;
500
return G_FILE_ERROR_FAILED;
506
get_contents_stdio (const gchar *display_filename,
515
gsize total_bytes = 0;
516
gsize total_allocated = 0;
519
g_assert (f != NULL);
525
bytes = fread (buf, 1, sizeof (buf), f);
528
while ((total_bytes + bytes + 1) > total_allocated)
531
total_allocated *= 2;
533
total_allocated = MIN (bytes + 1, sizeof (buf));
535
tmp = g_try_realloc (str, total_allocated);
542
_("Could not allocate %lu bytes to read file \"%s\""),
543
(gulong) total_allocated,
556
g_file_error_from_errno (save_errno),
557
_("Error reading file '%s': %s"),
559
g_strerror (save_errno));
564
memcpy (str + total_bytes, buf, bytes);
566
if (total_bytes + bytes < total_bytes)
571
_("File \"%s\" is too large"),
577
total_bytes += bytes;
582
if (total_allocated == 0)
584
str = g_new (gchar, 1);
588
str[total_bytes] = '\0';
591
*length = total_bytes;
608
get_contents_regfile (const gchar *display_filename,
609
struct stat *stat_buf,
620
size = stat_buf->st_size;
622
alloc_size = size + 1;
623
buf = g_try_malloc (alloc_size);
630
_("Could not allocate %lu bytes to read file \"%s\""),
638
while (bytes_read < size)
642
rc = read (fd, buf + bytes_read, size - bytes_read);
648
int save_errno = errno;
653
g_file_error_from_errno (save_errno),
654
_("Failed to read from file '%s': %s"),
656
g_strerror (save_errno));
667
buf[bytes_read] = '\0';
670
*length = bytes_read;
686
get_contents_posix (const gchar *filename,
691
struct stat stat_buf;
693
gchar *display_filename = g_filename_display_name (filename);
695
/* O_BINARY useful on Cygwin */
696
fd = open (filename, O_RDONLY|O_BINARY);
700
int save_errno = errno;
704
g_file_error_from_errno (save_errno),
705
_("Failed to open file '%s': %s"),
707
g_strerror (save_errno));
708
g_free (display_filename);
713
/* I don't think this will ever fail, aside from ENOMEM, but. */
714
if (fstat (fd, &stat_buf) < 0)
716
int save_errno = errno;
721
g_file_error_from_errno (save_errno),
722
_("Failed to get attributes of file '%s': fstat() failed: %s"),
724
g_strerror (save_errno));
725
g_free (display_filename);
730
if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
732
gboolean retval = get_contents_regfile (display_filename,
738
g_free (display_filename);
747
f = fdopen (fd, "r");
751
int save_errno = errno;
755
g_file_error_from_errno (save_errno),
756
_("Failed to open file '%s': fdopen() failed: %s"),
758
g_strerror (save_errno));
759
g_free (display_filename);
764
retval = get_contents_stdio (display_filename, f, contents, length, error);
765
g_free (display_filename);
771
#else /* G_OS_WIN32 */
774
get_contents_win32 (const gchar *filename,
781
gchar *display_filename = g_filename_display_name (filename);
784
f = g_fopen (filename, "rb");
791
g_file_error_from_errno (save_errno),
792
_("Failed to open file '%s': %s"),
794
g_strerror (save_errno));
795
g_free (display_filename);
800
retval = get_contents_stdio (display_filename, f, contents, length, error);
801
g_free (display_filename);
809
* g_file_get_contents:
810
* @filename: (type filename): name of a file to read contents from, in the GLib file name encoding
811
* @contents: (out) (array length=length) (element-type guint8): location to store an allocated string, use g_free() to free
812
* the returned string
813
* @length: (allow-none): location to store length in bytes of the contents, or %NULL
814
* @error: return location for a #GError, or %NULL
816
* Reads an entire file into allocated memory, with good error
819
* If the call was successful, it returns %TRUE and sets @contents to the file
820
* contents and @length to the length of the file contents in bytes. The string
821
* stored in @contents will be nul-terminated, so for text files you can pass
822
* %NULL for the @length argument. If the call was not successful, it returns
823
* %FALSE and sets @error. The error domain is #G_FILE_ERROR. Possible error
824
* codes are those in the #GFileError enumeration. In the error case,
825
* @contents is set to %NULL and @length is set to zero.
827
* Return value: %TRUE on success, %FALSE if an error occurred
830
g_file_get_contents (const gchar *filename,
835
g_return_val_if_fail (filename != NULL, FALSE);
836
g_return_val_if_fail (contents != NULL, FALSE);
843
return get_contents_win32 (filename, contents, length, error);
845
return get_contents_posix (filename, contents, length, error);
850
rename_file (const char *old_name,
851
const char *new_name,
855
if (g_rename (old_name, new_name) == -1)
857
int save_errno = errno;
858
gchar *display_old_name = g_filename_display_name (old_name);
859
gchar *display_new_name = g_filename_display_name (new_name);
863
g_file_error_from_errno (save_errno),
864
_("Failed to rename file '%s' to '%s': g_rename() failed: %s"),
867
g_strerror (save_errno));
869
g_free (display_old_name);
870
g_free (display_new_name);
879
write_to_temp_file (const gchar *contents,
881
const gchar *dest_file,
893
tmp_name = g_strdup_printf ("%s.XXXXXX", dest_file);
896
fd = g_mkstemp_full (tmp_name, O_RDWR | O_BINARY, 0666);
899
display_name = g_filename_display_name (tmp_name);
905
g_file_error_from_errno (save_errno),
906
_("Failed to create file '%s': %s"),
907
display_name, g_strerror (save_errno));
913
file = fdopen (fd, "wb");
919
g_file_error_from_errno (save_errno),
920
_("Failed to open file '%s' for writing: fdopen() failed: %s"),
922
g_strerror (save_errno));
936
n_written = fwrite (contents, 1, length, file);
938
if (n_written < length)
944
g_file_error_from_errno (save_errno),
945
_("Failed to write file '%s': fwrite() failed: %s"),
947
g_strerror (save_errno));
957
if (fflush (file) != 0)
963
g_file_error_from_errno (save_errno),
964
_("Failed to write file '%s': fflush() failed: %s"),
966
g_strerror (save_errno));
974
#ifdef BTRFS_SUPER_MAGIC
978
/* On Linux, on btrfs, skip the fsync since rename-over-existing is
979
* guaranteed to be atomic and this is the only case in which we
980
* would fsync() anyway.
983
if (fstatfs (fd, &buf) == 0 && buf.f_type == BTRFS_SUPER_MAGIC)
993
/* If the final destination exists and is > 0 bytes, we want to sync the
994
* newly written file to ensure the data is on disk when we rename over
995
* the destination. Otherwise if we get a system crash we can lose both
996
* the new and the old file on some filesystems. (I.E. those that don't
997
* guarantee the data is written to the disk before the metadata.)
999
if (g_lstat (dest_file, &statbuf) == 0 &&
1000
statbuf.st_size > 0 &&
1001
fsync (fileno (file)) != 0)
1007
g_file_error_from_errno (save_errno),
1008
_("Failed to write file '%s': fsync() failed: %s"),
1010
g_strerror (save_errno));
1013
g_unlink (tmp_name);
1020
#ifdef BTRFS_SUPER_MAGIC
1025
if (fclose (file) == EOF)
1031
g_file_error_from_errno (save_errno),
1032
_("Failed to close file '%s': fclose() failed: %s"),
1034
g_strerror (save_errno));
1037
g_unlink (tmp_name);
1042
retval = g_strdup (tmp_name);
1046
g_free (display_name);
1052
* g_file_set_contents:
1053
* @filename: (type filename): name of a file to write @contents to, in the GLib file name
1055
* @contents: (array length=length) (element-type guint8): string to write to the file
1056
* @length: length of @contents, or -1 if @contents is a nul-terminated string
1057
* @error: return location for a #GError, or %NULL
1059
* Writes all of @contents to a file named @filename, with good error checking.
1060
* If a file called @filename already exists it will be overwritten.
1062
* This write is atomic in the sense that it is first written to a temporary
1063
* file which is then renamed to the final name. Notes:
1066
* On Unix, if @filename already exists hard links to @filename will break.
1067
* Also since the file is recreated, existing permissions, access control
1068
* lists, metadata etc. may be lost. If @filename is a symbolic link,
1069
* the link itself will be replaced, not the linked file.
1072
* On Windows renaming a file will not remove an existing file with the
1073
* new name, so on Windows there is a race condition between the existing
1074
* file being removed and the temporary file being renamed.
1077
* On Windows there is no way to remove a file that is open to some
1078
* process, or mapped into memory. Thus, this function will fail if
1079
* @filename already exists and is open.
1083
* If the call was successful, it returns %TRUE. If the call was not successful,
1084
* it returns %FALSE and sets @error. The error domain is #G_FILE_ERROR.
1085
* Possible error codes are those in the #GFileError enumeration.
1087
* Note that the name for the temporary file is constructed by appending up
1088
* to 7 characters to @filename.
1090
* Return value: %TRUE on success, %FALSE if an error occurred
1095
g_file_set_contents (const gchar *filename,
1096
const gchar *contents,
1100
gchar *tmp_filename;
1102
GError *rename_error = NULL;
1104
g_return_val_if_fail (filename != NULL, FALSE);
1105
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1106
g_return_val_if_fail (contents != NULL || length == 0, FALSE);
1107
g_return_val_if_fail (length >= -1, FALSE);
1110
length = strlen (contents);
1112
tmp_filename = write_to_temp_file (contents, length, filename, error);
1120
if (!rename_file (tmp_filename, filename, &rename_error))
1124
g_unlink (tmp_filename);
1125
g_propagate_error (error, rename_error);
1129
#else /* G_OS_WIN32 */
1131
/* Renaming failed, but on Windows this may just mean
1132
* the file already exists. So if the target file
1133
* exists, try deleting it and do the rename again.
1135
if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1137
g_unlink (tmp_filename);
1138
g_propagate_error (error, rename_error);
1143
g_error_free (rename_error);
1145
if (g_unlink (filename) == -1)
1147
gchar *display_filename = g_filename_display_name (filename);
1149
int save_errno = errno;
1153
g_file_error_from_errno (save_errno),
1154
_("Existing file '%s' could not be removed: g_unlink() failed: %s"),
1156
g_strerror (save_errno));
1158
g_free (display_filename);
1159
g_unlink (tmp_filename);
1164
if (!rename_file (tmp_filename, filename, error))
1166
g_unlink (tmp_filename);
1177
g_free (tmp_filename);
1182
* get_tmp_file based on the mkstemp implementation from the GNU C library.
1183
* Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
1185
typedef gint (*GTmpFileCallback) (gchar *, gint, gint);
1188
get_tmp_file (gchar *tmpl,
1195
static const char letters[] =
1196
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1197
static const int NLETTERS = sizeof (letters) - 1;
1200
static int counter = 0;
1202
g_return_val_if_fail (tmpl != NULL, -1);
1204
/* find the last occurrence of "XXXXXX" */
1205
XXXXXX = g_strrstr (tmpl, "XXXXXX");
1207
if (!XXXXXX || strncmp (XXXXXX, "XXXXXX", 6))
1213
/* Get some more or less random data. */
1214
g_get_current_time (&tv);
1215
value = (tv.tv_usec ^ tv.tv_sec) + counter++;
1217
for (count = 0; count < 100; value += 7777, ++count)
1221
/* Fill in the random bits. */
1222
XXXXXX[0] = letters[v % NLETTERS];
1224
XXXXXX[1] = letters[v % NLETTERS];
1226
XXXXXX[2] = letters[v % NLETTERS];
1228
XXXXXX[3] = letters[v % NLETTERS];
1230
XXXXXX[4] = letters[v % NLETTERS];
1232
XXXXXX[5] = letters[v % NLETTERS];
1234
fd = f (tmpl, flags, mode);
1238
else if (errno != EEXIST)
1239
/* Any other error will apply also to other names we might
1240
* try, and there are 2^32 or so of them, so give up now.
1245
/* We got out of the loop because we ran out of combinations to try. */
1251
wrap_mkdir (gchar *tmpl,
1252
int flags G_GNUC_UNUSED,
1255
/* tmpl is in UTF-8 on Windows, thus use g_mkdir() */
1256
return g_mkdir (tmpl, mode);
1261
* @tmpl: (type filename): template directory name
1262
* @mode: permissions to create the temporary directory with
1264
* Creates a temporary directory. See the mkdtemp() documentation
1265
* on most UNIX-like systems.
1267
* The parameter is a string that should follow the rules for
1268
* mkdtemp() templates, i.e. contain the string "XXXXXX".
1269
* g_mkdtemp() is slightly more flexible than mkdtemp() in that the
1270
* sequence does not have to occur at the very end of the template
1271
* and you can pass a @mode. The X string will be modified to form
1272
* the name of a directory that didn't exist. The string should be
1273
* in the GLib file name encoding. Most importantly, on Windows it
1274
* should be in UTF-8.
1276
* Return value: A pointer to @tmpl, which has been modified
1277
* to hold the directory name. In case of errors, %NULL is
1278
* returned, and %errno will be set.
1283
g_mkdtemp_full (gchar *tmpl,
1286
if (get_tmp_file (tmpl, wrap_mkdir, 0, mode) == -1)
1294
* @tmpl: (type filename): template directory name
1296
* Creates a temporary directory. See the mkdtemp() documentation
1297
* on most UNIX-like systems.
1299
* The parameter is a string that should follow the rules for
1300
* mkdtemp() templates, i.e. contain the string "XXXXXX".
1301
* g_mkdtemp() is slightly more flexible than mkdtemp() in that the
1302
* sequence does not have to occur at the very end of the template
1303
* and you can pass a @mode and additional @flags. The X string will
1304
* be modified to form the name of a directory that didn't exist.
1305
* The string should be in the GLib file name encoding. Most importantly,
1306
* on Windows it should be in UTF-8.
1308
* Return value: A pointer to @tmpl, which has been modified
1309
* to hold the directory name. In case of errors, %NULL is
1310
* returned and %errno will be set.
1315
g_mkdtemp (gchar *tmpl)
1317
return g_mkdtemp_full (tmpl, 0700);
1322
* @tmpl: (type filename): template filename
1323
* @flags: flags to pass to an open() call in addition to O_EXCL
1324
* and O_CREAT, which are passed automatically
1325
* @mode: permissions to create the temporary file with
1327
* Opens a temporary file. See the mkstemp() documentation
1328
* on most UNIX-like systems.
1330
* The parameter is a string that should follow the rules for
1331
* mkstemp() templates, i.e. contain the string "XXXXXX".
1332
* g_mkstemp_full() is slightly more flexible than mkstemp()
1333
* in that the sequence does not have to occur at the very end of the
1334
* template and you can pass a @mode and additional @flags. The X
1335
* string will be modified to form the name of a file that didn't exist.
1336
* The string should be in the GLib file name encoding. Most importantly,
1337
* on Windows it should be in UTF-8.
1339
* Return value: A file handle (as from open()) to the file
1340
* opened for reading and writing. The file handle should be
1341
* closed with close(). In case of errors, -1 is returned
1342
* and %errno will be set.
1347
g_mkstemp_full (gchar *tmpl,
1351
/* tmpl is in UTF-8 on Windows, thus use g_open() */
1352
return get_tmp_file (tmpl, (GTmpFileCallback) g_open,
1353
flags | O_CREAT | O_EXCL, mode);
1358
* @tmpl: (type filename): template filename
1360
* Opens a temporary file. See the mkstemp() documentation
1361
* on most UNIX-like systems.
1363
* The parameter is a string that should follow the rules for
1364
* mkstemp() templates, i.e. contain the string "XXXXXX".
1365
* g_mkstemp() is slightly more flexible than mkstemp() in that the
1366
* sequence does not have to occur at the very end of the template.
1367
* The X string will be modified to form the name of a file that
1368
* didn't exist. The string should be in the GLib file name encoding.
1369
* Most importantly, on Windows it should be in UTF-8.
1371
* Return value: A file handle (as from open()) to the file
1372
* opened for reading and writing. The file is opened in binary
1373
* mode on platforms where there is a difference. The file handle
1374
* should be closed with close(). In case of errors, -1 is
1375
* returned and %errno will be set.
1378
g_mkstemp (gchar *tmpl)
1380
return g_mkstemp_full (tmpl, O_RDWR | O_BINARY, 0600);
1384
g_get_tmp_name (const gchar *tmpl,
1400
if ((slash = strchr (tmpl, G_DIR_SEPARATOR)) != NULL
1402
|| (strchr (tmpl, '/') != NULL && (slash = "/"))
1406
gchar *display_tmpl = g_filename_display_name (tmpl);
1413
G_FILE_ERROR_FAILED,
1414
_("Template '%s' invalid, should not contain a '%s'"),
1416
g_free (display_tmpl);
1421
if (strstr (tmpl, "XXXXXX") == NULL)
1423
gchar *display_tmpl = g_filename_display_name (tmpl);
1426
G_FILE_ERROR_FAILED,
1427
_("Template '%s' doesn't contain XXXXXX"),
1429
g_free (display_tmpl);
1433
tmpdir = g_get_tmp_dir ();
1435
if (G_IS_DIR_SEPARATOR (tmpdir [strlen (tmpdir) - 1]))
1438
sep = G_DIR_SEPARATOR_S;
1440
fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
1442
retval = get_tmp_file (fulltemplate, f, flags, mode);
1445
int save_errno = errno;
1446
gchar *display_fulltemplate = g_filename_display_name (fulltemplate);
1450
g_file_error_from_errno (save_errno),
1451
_("Failed to create file '%s': %s"),
1452
display_fulltemplate, g_strerror (save_errno));
1453
g_free (display_fulltemplate);
1454
g_free (fulltemplate);
1458
*name_used = fulltemplate;
1465
* @tmpl: (type filename) (allow-none): Template for file name, as in
1466
* g_mkstemp(), basename only, or %NULL for a default template
1467
* @name_used: (out) (type filename): location to store actual name used,
1469
* @error: return location for a #GError
1471
* Opens a file for writing in the preferred directory for temporary
1472
* files (as returned by g_get_tmp_dir()).
1474
* @tmpl should be a string in the GLib file name encoding containing
1475
* a sequence of six 'X' characters, as the parameter to g_mkstemp().
1476
* However, unlike these functions, the template should only be a
1477
* basename, no directory components are allowed. If template is
1478
* %NULL, a default template is used.
1480
* Note that in contrast to g_mkstemp() (and mkstemp()) @tmpl is not
1481
* modified, and might thus be a read-only literal string.
1483
* Upon success, and if @name_used is non-%NULL, the actual name used
1484
* is returned in @name_used. This string should be freed with g_free()
1485
* when not needed any longer. The returned name is in the GLib file
1488
* Return value: A file handle (as from open()) to the file opened for
1489
* reading and writing. The file is opened in binary mode on platforms
1490
* where there is a difference. The file handle should be closed with
1491
* close(). In case of errors, -1 is returned and @error will be set.
1494
g_file_open_tmp (const gchar *tmpl,
1498
gchar *fulltemplate;
1501
result = g_get_tmp_name (tmpl, &fulltemplate,
1502
(GTmpFileCallback) g_open,
1503
O_CREAT | O_EXCL | O_RDWR | O_BINARY,
1509
*name_used = fulltemplate;
1511
g_free (fulltemplate);
1519
* @tmpl: (type filename) (allow-none): Template for directory name,
1520
* as in g_mkdtemp(), basename only, or %NULL for a default template
1521
* @error: return location for a #GError
1523
* Creates a subdirectory in the preferred directory for temporary
1524
* files (as returned by g_get_tmp_dir()).
1526
* @tmpl should be a string in the GLib file name encoding containing
1527
* a sequence of six 'X' characters, as the parameter to g_mkstemp().
1528
* However, unlike these functions, the template should only be a
1529
* basename, no directory components are allowed. If template is
1530
* %NULL, a default template is used.
1532
* Note that in contrast to g_mkdtemp() (and mkdtemp()) @tmpl is not
1533
* modified, and might thus be a read-only literal string.
1535
* Return value: (type filename): The actual name used. This string
1536
* should be freed with g_free() when not needed any longer and is
1537
* is in the GLib file name encoding. In case of errors, %NULL is
1538
* returned and @error will be set.
1543
g_dir_make_tmp (const gchar *tmpl,
1546
gchar *fulltemplate;
1548
if (g_get_tmp_name (tmpl, &fulltemplate, wrap_mkdir, 0, 0700, error) == -1)
1551
return fulltemplate;
1555
g_build_path_va (const gchar *separator,
1556
const gchar *first_element,
1561
gint separator_len = strlen (separator);
1562
gboolean is_first = TRUE;
1563
gboolean have_leading = FALSE;
1564
const gchar *single_element = NULL;
1565
const gchar *next_element;
1566
const gchar *last_trailing = NULL;
1569
result = g_string_new (NULL);
1572
next_element = str_array[i++];
1574
next_element = first_element;
1578
const gchar *element;
1584
element = next_element;
1586
next_element = str_array[i++];
1588
next_element = va_arg (*args, gchar *);
1593
/* Ignore empty elements */
1601
while (strncmp (start, separator, separator_len) == 0)
1602
start += separator_len;
1605
end = start + strlen (start);
1609
while (end >= start + separator_len &&
1610
strncmp (end - separator_len, separator, separator_len) == 0)
1611
end -= separator_len;
1613
last_trailing = end;
1614
while (last_trailing >= element + separator_len &&
1615
strncmp (last_trailing - separator_len, separator, separator_len) == 0)
1616
last_trailing -= separator_len;
1620
/* If the leading and trailing separator strings are in the
1621
* same element and overlap, the result is exactly that element
1623
if (last_trailing <= start)
1624
single_element = element;
1626
g_string_append_len (result, element, start - element);
1627
have_leading = TRUE;
1630
single_element = NULL;
1637
g_string_append (result, separator);
1639
g_string_append_len (result, start, end - start);
1645
g_string_free (result, TRUE);
1646
return g_strdup (single_element);
1651
g_string_append (result, last_trailing);
1653
return g_string_free (result, FALSE);
1659
* @separator: a string used to separator the elements of the path.
1660
* @args: (array zero-terminated=1): %NULL-terminated array of strings containing the path elements.
1662
* Behaves exactly like g_build_path(), but takes the path elements
1663
* as a string array, instead of varargs. This function is mainly
1664
* meant for language bindings.
1666
* Return value: a newly-allocated string that must be freed with g_free().
1671
g_build_pathv (const gchar *separator,
1677
return g_build_path_va (separator, NULL, NULL, args);
1683
* @separator: a string used to separator the elements of the path.
1684
* @first_element: the first element in the path
1685
* @...: remaining elements in path, terminated by %NULL
1687
* Creates a path from a series of elements using @separator as the
1688
* separator between elements. At the boundary between two elements,
1689
* any trailing occurrences of separator in the first element, or
1690
* leading occurrences of separator in the second element are removed
1691
* and exactly one copy of the separator is inserted.
1693
* Empty elements are ignored.
1695
* The number of leading copies of the separator on the result is
1696
* the same as the number of leading copies of the separator on
1697
* the first non-empty element.
1699
* The number of trailing copies of the separator on the result is
1700
* the same as the number of trailing copies of the separator on
1701
* the last non-empty element. (Determination of the number of
1702
* trailing copies is done without stripping leading copies, so
1703
* if the separator is <literal>ABA</literal>, <literal>ABABA</literal>
1704
* has 1 trailing copy.)
1706
* However, if there is only a single non-empty element, and there
1707
* are no characters in that element not part of the leading or
1708
* trailing separators, then the result is exactly the original value
1711
* Other than for determination of the number of leading and trailing
1712
* copies of the separator, elements consisting only of copies
1713
* of the separator are ignored.
1715
* Return value: a newly-allocated string that must be freed with g_free().
1718
g_build_path (const gchar *separator,
1719
const gchar *first_element,
1725
g_return_val_if_fail (separator != NULL, NULL);
1727
va_start (args, first_element);
1728
str = g_build_path_va (separator, first_element, &args, NULL);
1737
g_build_pathname_va (const gchar *first_element,
1741
/* Code copied from g_build_pathv(), and modified to use two
1742
* alternative single-character separators.
1745
gboolean is_first = TRUE;
1746
gboolean have_leading = FALSE;
1747
const gchar *single_element = NULL;
1748
const gchar *next_element;
1749
const gchar *last_trailing = NULL;
1750
gchar current_separator = '\\';
1753
result = g_string_new (NULL);
1756
next_element = str_array[i++];
1758
next_element = first_element;
1762
const gchar *element;
1768
element = next_element;
1770
next_element = str_array[i++];
1772
next_element = va_arg (*args, gchar *);
1777
/* Ignore empty elements */
1786
(*start == '\\' || *start == '/'))
1788
current_separator = *start;
1793
end = start + strlen (start);
1797
while (end >= start + 1 &&
1798
(end[-1] == '\\' || end[-1] == '/'))
1800
current_separator = end[-1];
1804
last_trailing = end;
1805
while (last_trailing >= element + 1 &&
1806
(last_trailing[-1] == '\\' || last_trailing[-1] == '/'))
1811
/* If the leading and trailing separator strings are in the
1812
* same element and overlap, the result is exactly that element
1814
if (last_trailing <= start)
1815
single_element = element;
1817
g_string_append_len (result, element, start - element);
1818
have_leading = TRUE;
1821
single_element = NULL;
1828
g_string_append_len (result, ¤t_separator, 1);
1830
g_string_append_len (result, start, end - start);
1836
g_string_free (result, TRUE);
1837
return g_strdup (single_element);
1842
g_string_append (result, last_trailing);
1844
return g_string_free (result, FALSE);
1851
* g_build_filenamev:
1852
* @args: (array zero-terminated=1): %NULL-terminated array of strings containing the path elements.
1854
* Behaves exactly like g_build_filename(), but takes the path elements
1855
* as a string array, instead of varargs. This function is mainly
1856
* meant for language bindings.
1858
* Return value: a newly-allocated string that must be freed with g_free().
1863
g_build_filenamev (gchar **args)
1868
str = g_build_path_va (G_DIR_SEPARATOR_S, NULL, NULL, args);
1870
str = g_build_pathname_va (NULL, NULL, args);
1878
* @first_element: the first element in the path
1879
* @...: remaining elements in path, terminated by %NULL
1881
* Creates a filename from a series of elements using the correct
1882
* separator for filenames.
1884
* On Unix, this function behaves identically to <literal>g_build_path
1885
* (G_DIR_SEPARATOR_S, first_element, ....)</literal>.
1887
* On Windows, it takes into account that either the backslash
1888
* (<literal>\</literal> or slash (<literal>/</literal>) can be used
1889
* as separator in filenames, but otherwise behaves as on Unix. When
1890
* file pathname separators need to be inserted, the one that last
1891
* previously occurred in the parameters (reading from left to right)
1894
* No attempt is made to force the resulting filename to be an absolute
1895
* path. If the first element is a relative path, the result will
1896
* be a relative path.
1898
* Return value: a newly-allocated string that must be freed with g_free().
1901
g_build_filename (const gchar *first_element,
1907
va_start (args, first_element);
1909
str = g_build_path_va (G_DIR_SEPARATOR_S, first_element, &args, NULL);
1911
str = g_build_pathname_va (first_element, &args, NULL);
1918
#define KILOBYTE_FACTOR (G_GOFFSET_CONSTANT (1000))
1919
#define MEGABYTE_FACTOR (KILOBYTE_FACTOR * KILOBYTE_FACTOR)
1920
#define GIGABYTE_FACTOR (MEGABYTE_FACTOR * KILOBYTE_FACTOR)
1921
#define TERABYTE_FACTOR (GIGABYTE_FACTOR * KILOBYTE_FACTOR)
1922
#define PETABYTE_FACTOR (TERABYTE_FACTOR * KILOBYTE_FACTOR)
1923
#define EXABYTE_FACTOR (PETABYTE_FACTOR * KILOBYTE_FACTOR)
1925
#define KIBIBYTE_FACTOR (G_GOFFSET_CONSTANT (1024))
1926
#define MEBIBYTE_FACTOR (KIBIBYTE_FACTOR * KIBIBYTE_FACTOR)
1927
#define GIBIBYTE_FACTOR (MEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
1928
#define TEBIBYTE_FACTOR (GIBIBYTE_FACTOR * KIBIBYTE_FACTOR)
1929
#define PEBIBYTE_FACTOR (TEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
1930
#define EXBIBYTE_FACTOR (PEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
1934
* @size: a size in bytes
1936
* Formats a size (for example the size of a file) into a human readable
1937
* string. Sizes are rounded to the nearest size prefix (kB, MB, GB)
1938
* and are displayed rounded to the nearest tenth. E.g. the file size
1939
* 3292528 bytes will be converted into the string "3.2 MB".
1941
* The prefix units base is 1000 (i.e. 1 kB is 1000 bytes).
1943
* This string should be freed with g_free() when not needed any longer.
1945
* See g_format_size_full() for more options about how the size might be
1948
* Returns: a newly-allocated formatted string containing a human readable
1954
g_format_size (guint64 size)
1956
return g_format_size_full (size, G_FORMAT_SIZE_DEFAULT);
1960
* g_format_size_full:
1961
* @size: a size in bytes
1962
* @flags: #GFormatSizeFlags to modify the output
1966
* This function is similar to g_format_size() but allows for flags that
1967
* modify the output. See #GFormatSizeFlags.
1969
* Returns: a newly-allocated formatted string containing a human
1970
* readable file size.
1976
* @G_FORMAT_SIZE_DEFAULT: behave the same as g_format_size()
1977
* @G_FORMAT_SIZE_LONG_FORMAT: include the exact number of bytes as part
1978
* of the returned string. For example,
1979
* "45.6 kB (45,612 bytes)".
1980
* @G_FORMAT_SIZE_IEC_UNITS: use IEC (base 1024) units with "KiB"-style
1981
* suffixes. IEC units should only be used
1982
* for reporting things with a strong "power
1983
* of 2" basis, like RAM sizes or RAID stripe
1984
* sizes. Network and storage sizes should
1985
* be reported in the normal SI units.
1987
* Flags to modify the format of the string returned by
1988
* g_format_size_full().
1991
g_format_size_full (guint64 size,
1992
GFormatSizeFlags flags)
1996
string = g_string_new (NULL);
1998
if (flags & G_FORMAT_SIZE_IEC_UNITS)
2000
if (size < KIBIBYTE_FACTOR)
2002
g_string_printf (string,
2003
g_dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes", (guint) size),
2005
flags &= ~G_FORMAT_SIZE_LONG_FORMAT;
2008
else if (size < MEBIBYTE_FACTOR)
2009
g_string_printf (string, _("%.1f KiB"), (gdouble) size / (gdouble) KIBIBYTE_FACTOR);
2011
else if (size < GIBIBYTE_FACTOR)
2012
g_string_printf (string, _("%.1f MiB"), (gdouble) size / (gdouble) MEBIBYTE_FACTOR);
2014
else if (size < TEBIBYTE_FACTOR)
2015
g_string_printf (string, _("%.1f GiB"), (gdouble) size / (gdouble) GIBIBYTE_FACTOR);
2017
else if (size < PEBIBYTE_FACTOR)
2018
g_string_printf (string, _("%.1f TiB"), (gdouble) size / (gdouble) TEBIBYTE_FACTOR);
2020
else if (size < EXBIBYTE_FACTOR)
2021
g_string_printf (string, _("%.1f PiB"), (gdouble) size / (gdouble) PEBIBYTE_FACTOR);
2024
g_string_printf (string, _("%.1f EiB"), (gdouble) size / (gdouble) EXBIBYTE_FACTOR);
2028
if (size < KILOBYTE_FACTOR)
2030
g_string_printf (string,
2031
g_dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes", (guint) size),
2033
flags &= ~G_FORMAT_SIZE_LONG_FORMAT;
2036
else if (size < MEGABYTE_FACTOR)
2037
g_string_printf (string, _("%.1f kB"), (gdouble) size / (gdouble) KILOBYTE_FACTOR);
2039
else if (size < GIGABYTE_FACTOR)
2040
g_string_printf (string, _("%.1f MB"), (gdouble) size / (gdouble) MEGABYTE_FACTOR);
2042
else if (size < TERABYTE_FACTOR)
2043
g_string_printf (string, _("%.1f GB"), (gdouble) size / (gdouble) GIGABYTE_FACTOR);
2045
else if (size < PETABYTE_FACTOR)
2046
g_string_printf (string, _("%.1f TB"), (gdouble) size / (gdouble) TERABYTE_FACTOR);
2048
else if (size < EXABYTE_FACTOR)
2049
g_string_printf (string, _("%.1f PB"), (gdouble) size / (gdouble) PETABYTE_FACTOR);
2052
g_string_printf (string, _("%.1f EB"), (gdouble) size / (gdouble) EXABYTE_FACTOR);
2055
if (flags & G_FORMAT_SIZE_LONG_FORMAT)
2057
/* First problem: we need to use the number of bytes to decide on
2058
* the plural form that is used for display, but the number of
2059
* bytes potentially exceeds the size of a guint (which is what
2060
* ngettext() takes).
2062
* From a pragmatic standpoint, it seems that all known languages
2063
* base plural forms on one or both of the following:
2065
* - the lowest digits of the number
2067
* - if the number if greater than some small value
2069
* Here's how we fake it: Draw an arbitrary line at one thousand.
2070
* If the number is below that, then fine. If it is above it,
2071
* then we take the modulus of the number by one thousand (in
2072
* order to keep the lowest digits) and add one thousand to that
2073
* (in order to ensure that 1001 is not treated the same as 1).
2075
guint plural_form = size < 1000 ? size : size % 1000 + 1000;
2077
/* Second problem: we need to translate the string "%u byte" and
2078
* "%u bytes" for pluralisation, but the correct number format to
2079
* use for a gsize is different depending on which architecture
2082
* Solution: format the number separately and use "%s bytes" on
2085
const gchar *translated_format;
2086
gchar *formatted_number;
2088
/* Translators: the %s in "%s bytes" will always be replaced by a number. */
2089
translated_format = g_dngettext(GETTEXT_PACKAGE, "%s byte", "%s bytes", plural_form);
2091
/* XXX: Windows doesn't support the "'" format modifier, so we
2092
* must not use it there. Instead, just display the number
2093
* without separation. Bug #655336 is open until a solution is
2097
formatted_number = g_strdup_printf ("%'"G_GUINT64_FORMAT, size);
2099
formatted_number = g_strdup_printf ("%"G_GUINT64_FORMAT, size);
2102
g_string_append (string, " (");
2103
g_string_append_printf (string, translated_format, formatted_number);
2104
g_free (formatted_number);
2105
g_string_append (string, ")");
2108
return g_string_free (string, FALSE);
2112
* g_format_size_for_display:
2113
* @size: a size in bytes.
2115
* Formats a size (for example the size of a file) into a human readable string.
2116
* Sizes are rounded to the nearest size prefix (KB, MB, GB) and are displayed
2117
* rounded to the nearest tenth. E.g. the file size 3292528 bytes will be
2118
* converted into the string "3.1 MB".
2120
* The prefix units base is 1024 (i.e. 1 KB is 1024 bytes).
2122
* This string should be freed with g_free() when not needed any longer.
2124
* Returns: a newly-allocated formatted string containing a human readable
2127
* Deprecated:2.30: This function is broken due to its use of SI
2128
* suffixes to denote IEC units. Use g_format_size()
2133
g_format_size_for_display (goffset size)
2135
if (size < (goffset) KIBIBYTE_FACTOR)
2136
return g_strdup_printf (g_dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes",(guint) size), (guint) size);
2139
gdouble displayed_size;
2141
if (size < (goffset) MEBIBYTE_FACTOR)
2143
displayed_size = (gdouble) size / (gdouble) KIBIBYTE_FACTOR;
2144
return g_strdup_printf (_("%.1f KB"), displayed_size);
2146
else if (size < (goffset) GIBIBYTE_FACTOR)
2148
displayed_size = (gdouble) size / (gdouble) MEBIBYTE_FACTOR;
2149
return g_strdup_printf (_("%.1f MB"), displayed_size);
2151
else if (size < (goffset) TEBIBYTE_FACTOR)
2153
displayed_size = (gdouble) size / (gdouble) GIBIBYTE_FACTOR;
2154
return g_strdup_printf (_("%.1f GB"), displayed_size);
2156
else if (size < (goffset) PEBIBYTE_FACTOR)
2158
displayed_size = (gdouble) size / (gdouble) TEBIBYTE_FACTOR;
2159
return g_strdup_printf (_("%.1f TB"), displayed_size);
2161
else if (size < (goffset) EXBIBYTE_FACTOR)
2163
displayed_size = (gdouble) size / (gdouble) PEBIBYTE_FACTOR;
2164
return g_strdup_printf (_("%.1f PB"), displayed_size);
2168
displayed_size = (gdouble) size / (gdouble) EXBIBYTE_FACTOR;
2169
return g_strdup_printf (_("%.1f EB"), displayed_size);
2177
* @filename: the symbolic link
2178
* @error: return location for a #GError
2180
* Reads the contents of the symbolic link @filename like the POSIX
2181
* readlink() function. The returned string is in the encoding used
2182
* for filenames. Use g_filename_to_utf8() to convert it to UTF-8.
2184
* Returns: A newly-allocated string with the contents of the symbolic link,
2185
* or %NULL if an error occurred.
2190
g_file_read_link (const gchar *filename,
2193
#ifdef HAVE_READLINK
2199
buffer = g_malloc (size);
2203
read_size = readlink (filename, buffer, size);
2204
if (read_size < 0) {
2205
int save_errno = errno;
2206
gchar *display_filename = g_filename_display_name (filename);
2211
g_file_error_from_errno (save_errno),
2212
_("Failed to read the symbolic link '%s': %s"),
2214
g_strerror (save_errno));
2215
g_free (display_filename);
2220
if (read_size < size)
2222
buffer[read_size] = 0;
2227
buffer = g_realloc (buffer, size);
2230
g_set_error_literal (error,
2233
_("Symbolic links not supported"));
2239
/* NOTE : Keep this part last to ensure nothing in this file uses the
2240
* below binary compatibility versions.
2242
#if defined (G_OS_WIN32) && !defined (_WIN64)
2244
/* Binary compatibility versions. Will be called by code compiled
2245
* against quite old (pre-2.8, I think) headers only, not from more
2246
* recently compiled code.
2252
g_file_test (const gchar *filename,
2255
gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2258
if (utf8_filename == NULL)
2261
retval = g_file_test_utf8 (utf8_filename, test);
2263
g_free (utf8_filename);
2268
#undef g_file_get_contents
2271
g_file_get_contents (const gchar *filename,
2276
gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2279
if (utf8_filename == NULL)
2282
retval = g_file_get_contents_utf8 (utf8_filename, contents, length, error);
2284
g_free (utf8_filename);
2292
g_mkstemp (gchar *tmpl)
2294
/* This is the backward compatibility system codepage version,
2295
* thus use normal open().
2297
return get_tmp_file (tmpl, (GTmpFileCallback) open,
2298
O_RDWR | O_CREAT | O_EXCL, 0600);
2301
#undef g_file_open_tmp
2304
g_file_open_tmp (const gchar *tmpl,
2308
gchar *utf8_tmpl = g_locale_to_utf8 (tmpl, -1, NULL, NULL, error);
2309
gchar *utf8_name_used;
2312
if (utf8_tmpl == NULL)
2315
retval = g_file_open_tmp_utf8 (utf8_tmpl, &utf8_name_used, error);
2321
*name_used = g_locale_from_utf8 (utf8_name_used, -1, NULL, NULL, NULL);
2323
g_free (utf8_name_used);