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.
34 #include <sys/types.h>
42 #endif /* G_OS_WIN32 */
59 * @filename: a filename to test in the GLib file name encoding
60 * @test: bitfield of #GFileTest flags
62 * Returns %TRUE if any of the tests in the bitfield @test are
63 * %TRUE. For example, <literal>(G_FILE_TEST_EXISTS |
64 * G_FILE_TEST_IS_DIR)</literal> will return %TRUE if the file exists;
65 * the check whether it's a directory doesn't matter since the existence
66 * test is %TRUE. With the current set of available tests, there's no point
67 * passing in more than one test at a time.
69 * Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
70 * so for a symbolic link to a regular file g_file_test() will return
71 * %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
73 * Note, that for a dangling symbolic link g_file_test() will return
74 * %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
76 * You should never use g_file_test() to test whether it is safe
77 * to perform an operation, because there is always the possibility
78 * of the condition changing before you actually perform the operation.
79 * For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
80 * to know whether it is is safe to write to a file without being
81 * tricked into writing into a different location. It doesn't work!
83 * <informalexample><programlisting>
84 * /* DON'T DO THIS */
85 * if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK)) {
86 * fd = g_open (filename, O_WRONLY);
87 * /* write to fd */
89 * </programlisting></informalexample>
91 * Another thing to note is that %G_FILE_TEST_EXISTS and
92 * %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
93 * system call. This usually doesn't matter, but if your program
94 * is setuid or setgid it means that these tests will give you
95 * the answer for the real user ID and group ID, rather than the
96 * effective user ID and group ID.
98 * On Windows, there are no symlinks, so testing for
99 * %G_FILE_TEST_IS_SYMLINK will always return %FALSE. Testing for
100 * %G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and
101 * its name indicates that it is executable, checking for well-known
102 * extensions and those listed in the %PATHEXT environment variable.
104 * Return value: whether a test was %TRUE
107 g_file_test (const gchar
*filename
,
111 /* stuff missing in std vc6 api */
112 # ifndef INVALID_FILE_ATTRIBUTES
113 # define INVALID_FILE_ATTRIBUTES -1
115 # ifndef FILE_ATTRIBUTE_DEVICE
116 # define FILE_ATTRIBUTE_DEVICE 64
120 if (G_WIN32_HAVE_WIDECHAR_API ())
122 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
124 if (wfilename
== NULL
)
127 attributes
= GetFileAttributesW (wfilename
);
133 gchar
*cpfilename
= g_locale_from_utf8 (filename
, -1, NULL
, NULL
, NULL
);
135 if (cpfilename
== NULL
)
138 attributes
= GetFileAttributesA (cpfilename
);
143 if (attributes
== INVALID_FILE_ATTRIBUTES
)
146 if (test
& G_FILE_TEST_EXISTS
)
149 if (test
& G_FILE_TEST_IS_REGULAR
)
150 return (attributes
& (FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_DEVICE
)) == 0;
152 if (test
& G_FILE_TEST_IS_DIR
)
153 return (attributes
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
155 if (test
& G_FILE_TEST_IS_EXECUTABLE
)
157 const gchar
*lastdot
= strrchr (filename
, '.');
158 const gchar
*pathext
= NULL
, *p
;
164 if (stricmp (lastdot
, ".exe") == 0 ||
165 stricmp (lastdot
, ".cmd") == 0 ||
166 stricmp (lastdot
, ".bat") == 0 ||
167 stricmp (lastdot
, ".com") == 0)
170 /* Check if it is one of the types listed in %PATHEXT% */
172 pathext
= g_getenv ("PATHEXT");
176 pathext
= g_utf8_casefold (pathext
, -1);
178 lastdot
= g_utf8_casefold (lastdot
, -1);
179 extlen
= strlen (lastdot
);
184 const gchar
*q
= strchr (p
, ';');
187 if (extlen
== q
- p
&&
188 memcmp (lastdot
, p
, extlen
) == 0)
190 g_free ((gchar
*) pathext
);
191 g_free ((gchar
*) lastdot
);
200 g_free ((gchar
*) pathext
);
201 g_free ((gchar
*) lastdot
);
207 if ((test
& G_FILE_TEST_EXISTS
) && (access (filename
, F_OK
) == 0))
210 if ((test
& G_FILE_TEST_IS_EXECUTABLE
) && (access (filename
, X_OK
) == 0))
215 /* For root, on some POSIX systems, access (filename, X_OK)
216 * will succeed even if no executable bits are set on the
217 * file. We fall through to a stat test to avoid that.
221 test
&= ~G_FILE_TEST_IS_EXECUTABLE
;
223 if (test
& G_FILE_TEST_IS_SYMLINK
)
227 if ((lstat (filename
, &s
) == 0) && S_ISLNK (s
.st_mode
))
231 if (test
& (G_FILE_TEST_IS_REGULAR
|
233 G_FILE_TEST_IS_EXECUTABLE
))
237 if (stat (filename
, &s
) == 0)
239 if ((test
& G_FILE_TEST_IS_REGULAR
) && S_ISREG (s
.st_mode
))
242 if ((test
& G_FILE_TEST_IS_DIR
) && S_ISDIR (s
.st_mode
))
245 /* The extra test for root when access (file, X_OK) succeeds.
247 if ((test
& G_FILE_TEST_IS_EXECUTABLE
) &&
248 ((s
.st_mode
& S_IXOTH
) ||
249 (s
.st_mode
& S_IXUSR
) ||
250 (s
.st_mode
& S_IXGRP
)))
263 /* Binary compatibility version. Not for newly compiled code. */
266 g_file_test (const gchar
*filename
,
269 gchar
*utf8_filename
= g_locale_to_utf8 (filename
, -1, NULL
, NULL
, NULL
);
272 if (utf8_filename
== NULL
)
275 retval
= g_file_test_utf8 (utf8_filename
, test
);
277 g_free (utf8_filename
);
285 g_file_error_quark (void)
289 q
= g_quark_from_static_string ("g-file-error-quark");
295 * g_file_error_from_errno:
296 * @err_no: an "errno" value
298 * Gets a #GFileError constant based on the passed-in @errno.
299 * For example, if you pass in %EEXIST this function returns
300 * #G_FILE_ERROR_EXIST. Unlike @errno values, you can portably
301 * assume that all #GFileError values will exist.
303 * Normally a #GFileError value goes into a #GError returned
304 * from a function that manipulates files. So you would use
305 * g_file_error_from_errno() when constructing a #GError.
307 * Return value: #GFileError corresponding to the given @errno
310 g_file_error_from_errno (gint err_no
)
316 return G_FILE_ERROR_EXIST
;
322 return G_FILE_ERROR_ISDIR
;
328 return G_FILE_ERROR_ACCES
;
334 return G_FILE_ERROR_NAMETOOLONG
;
340 return G_FILE_ERROR_NOENT
;
346 return G_FILE_ERROR_NOTDIR
;
352 return G_FILE_ERROR_NXIO
;
358 return G_FILE_ERROR_NODEV
;
364 return G_FILE_ERROR_ROFS
;
370 return G_FILE_ERROR_TXTBSY
;
376 return G_FILE_ERROR_FAULT
;
382 return G_FILE_ERROR_LOOP
;
388 return G_FILE_ERROR_NOSPC
;
394 return G_FILE_ERROR_NOMEM
;
400 return G_FILE_ERROR_MFILE
;
406 return G_FILE_ERROR_NFILE
;
412 return G_FILE_ERROR_BADF
;
418 return G_FILE_ERROR_INVAL
;
424 return G_FILE_ERROR_PIPE
;
430 return G_FILE_ERROR_AGAIN
;
436 return G_FILE_ERROR_INTR
;
442 return G_FILE_ERROR_IO
;
448 return G_FILE_ERROR_PERM
;
454 return G_FILE_ERROR_NOSYS
;
459 return G_FILE_ERROR_FAILED
;
465 get_contents_stdio (const gchar
*display_filename
,
474 size_t total_bytes
= 0;
475 size_t total_allocated
= 0;
477 g_assert (f
!= NULL
);
483 bytes
= fread (buf
, 1, sizeof (buf
), f
);
486 while ((total_bytes
+ bytes
+ 1) > total_allocated
)
489 total_allocated
*= 2;
491 total_allocated
= MIN (bytes
+ 1, sizeof (buf
));
493 str
= g_try_realloc (str
, total_allocated
);
500 _("Could not allocate %lu bytes to read file \"%s\""),
501 (gulong
) total_allocated
,
512 g_file_error_from_errno (save_errno
),
513 _("Error reading file '%s': %s"),
515 g_strerror (save_errno
));
520 memcpy (str
+ total_bytes
, buf
, bytes
);
521 total_bytes
+= bytes
;
526 str
[total_bytes
] = '\0';
529 *length
= total_bytes
;
546 get_contents_regfile (const gchar
*display_filename
,
547 struct stat
*stat_buf
,
558 size
= stat_buf
->st_size
;
560 alloc_size
= size
+ 1;
561 buf
= g_try_malloc (alloc_size
);
568 _("Could not allocate %lu bytes to read file \"%s\""),
576 while (bytes_read
< size
)
580 rc
= read (fd
, buf
+ bytes_read
, size
- bytes_read
);
586 int save_errno
= errno
;
591 g_file_error_from_errno (save_errno
),
592 _("Failed to read from file '%s': %s"),
594 g_strerror (save_errno
));
605 buf
[bytes_read
] = '\0';
608 *length
= bytes_read
;
624 get_contents_posix (const gchar
*filename
,
629 struct stat stat_buf
;
631 gchar
*display_filename
= g_filename_display_name (filename
);
633 /* O_BINARY useful on Cygwin */
634 fd
= open (filename
, O_RDONLY
|O_BINARY
);
638 int save_errno
= errno
;
642 g_file_error_from_errno (save_errno
),
643 _("Failed to open file '%s': %s"),
645 g_strerror (save_errno
));
646 g_free (display_filename
);
651 /* I don't think this will ever fail, aside from ENOMEM, but. */
652 if (fstat (fd
, &stat_buf
) < 0)
654 int save_errno
= errno
;
659 g_file_error_from_errno (save_errno
),
660 _("Failed to get attributes of file '%s': fstat() failed: %s"),
662 g_strerror (save_errno
));
663 g_free (display_filename
);
668 if (stat_buf
.st_size
> 0 && S_ISREG (stat_buf
.st_mode
))
670 gboolean retval
= get_contents_regfile (display_filename
,
676 g_free (display_filename
);
685 f
= fdopen (fd
, "r");
689 int save_errno
= errno
;
693 g_file_error_from_errno (save_errno
),
694 _("Failed to open file '%s': fdopen() failed: %s"),
696 g_strerror (save_errno
));
697 g_free (display_filename
);
702 retval
= get_contents_stdio (display_filename
, f
, contents
, length
, error
);
703 g_free (display_filename
);
709 #else /* G_OS_WIN32 */
712 get_contents_win32 (const gchar
*filename
,
719 gchar
*display_filename
= g_filename_display_name (filename
);
722 f
= g_fopen (filename
, "rb");
729 g_file_error_from_errno (save_errno
),
730 _("Failed to open file '%s': %s"),
732 g_strerror (save_errno
));
733 g_free (display_filename
);
738 retval
= get_contents_stdio (display_filename
, f
, contents
, length
, error
);
739 g_free (display_filename
);
747 * g_file_get_contents:
748 * @filename: name of a file to read contents from, in the GLib file name encoding
749 * @contents: location to store an allocated string
750 * @length: location to store length in bytes of the contents, or %NULL
751 * @error: return location for a #GError, or %NULL
753 * Reads an entire file into allocated memory, with good error
756 * If the call was successful, it returns %TRUE and sets @contents to the file
757 * contents and @length to the length of the file contents in bytes. The string
758 * stored in @contents will be nul-terminated, so for text files you can pass
759 * %NULL for the @length argument. If the call was not successful, it returns
760 * %FALSE and sets @error. The error domain is #G_FILE_ERROR. Possible error
761 * codes are those in the #GFileError enumeration. In the error case,
762 * @contents is set to %NULL and @length is set to zero.
764 * Return value: %TRUE on success, %FALSE if an error occurred
767 g_file_get_contents (const gchar
*filename
,
772 g_return_val_if_fail (filename
!= NULL
, FALSE
);
773 g_return_val_if_fail (contents
!= NULL
, FALSE
);
780 return get_contents_win32 (filename
, contents
, length
, error
);
782 return get_contents_posix (filename
, contents
, length
, error
);
788 #undef g_file_get_contents
790 /* Binary compatibility version. Not for newly compiled code. */
793 g_file_get_contents (const gchar
*filename
,
798 gchar
*utf8_filename
= g_locale_to_utf8 (filename
, -1, NULL
, NULL
, error
);
801 if (utf8_filename
== NULL
)
804 retval
= g_file_get_contents_utf8 (utf8_filename
, contents
, length
, error
);
806 g_free (utf8_filename
);
815 rename_file (const char *old_name
,
816 const char *new_name
,
820 if (g_rename (old_name
, new_name
) == -1)
822 int save_errno
= errno
;
823 gchar
*display_old_name
= g_filename_display_name (old_name
);
824 gchar
*display_new_name
= g_filename_display_name (new_name
);
828 g_file_error_from_errno (save_errno
),
829 _("Failed to rename file '%s' to '%s': g_rename() failed: %s"),
832 g_strerror (save_errno
));
834 g_free (display_old_name
);
835 g_free (display_new_name
);
844 write_to_temp_file (const gchar
*contents
,
846 const gchar
*template,
858 tmp_name
= g_strdup_printf ("%s.XXXXXX", template);
861 fd
= g_mkstemp (tmp_name
);
863 display_name
= g_filename_display_name (tmp_name
);
869 g_file_error_from_errno (save_errno
),
870 _("Failed to create file '%s': %s"),
871 display_name
, g_strerror (save_errno
));
877 file
= fdopen (fd
, "wb");
882 g_file_error_from_errno (errno
),
883 _("Failed to open file '%s' for writing: fdopen() failed: %s"),
899 n_written
= fwrite (contents
, 1, length
, file
);
901 if (n_written
< length
)
905 g_file_error_from_errno (errno
),
906 _("Failed to write file '%s': fwrite() failed: %s"),
918 if (fclose (file
) == EOF
)
922 g_file_error_from_errno (errno
),
923 _("Failed to close file '%s': fclose() failed: %s"),
932 retval
= g_strdup (tmp_name
);
936 g_free (display_name
);
943 * @filename: name of a file to write @contents to, in the GLib file name
945 * @contents: string to write to the file
946 * @length: length of @contents, or -1 if @contents is a nul-terminated string
947 * @error: return location for a #GError, or %NULL
949 * Writes all of @contents to a file named @filename, with good error checking.
950 * If a file called @filename already exists it will be overwritten.
952 * This write is atomic in the sense that it is first written to a temporary
953 * file which is then renamed to the final name. Notes:
956 * On Unix, if @filename already exists hard links to @filename will break.
957 * Also since the file is recreated, existing permissions, access control
958 * lists, metadata etc. may be lost. If @filename is a symbolic link,
959 * the link itself will be replaced, not the linked file.
962 * On Windows renaming a file will not remove an existing file with the
963 * new name, so on Windows there is a race condition between the existing
964 * file being removed and the temporary file being renamed.
967 * On Windows there is no way to remove a file that is open to some
968 * process, or mapped into memory. Thus, this function will fail if
969 * @filename already exists and is open.
973 * If the call was sucessful, it returns %TRUE. If the call was not successful,
974 * it returns %FALSE and sets @error. The error domain is #G_FILE_ERROR.
975 * Possible error codes are those in the #GFileError enumeration.
977 * Return value: %TRUE on success, %FALSE if an error occurred
982 g_file_replace (const gchar
*filename
,
983 const gchar
*contents
,
989 GError
*rename_error
= NULL
;
991 g_return_val_if_fail (filename
!= NULL
, FALSE
);
992 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
993 g_return_val_if_fail (contents
!= NULL
|| length
== 0, FALSE
);
994 g_return_val_if_fail (length
>= -1, FALSE
);
997 length
= strlen (contents
);
999 tmp_filename
= write_to_temp_file (contents
, length
, filename
, error
);
1007 if (!rename_file (tmp_filename
, filename
, &rename_error
))
1011 g_unlink (tmp_filename
);
1012 g_propagate_error (error
, rename_error
);
1016 #else /* G_OS_WIN32 */
1018 /* Renaming failed, but on Windows this may just mean
1019 * the file already exists. So if the target file
1020 * exists, try deleting it and do the rename again.
1022 if (!g_file_test (filename
, G_FILE_TEST_EXISTS
))
1024 g_unlink (tmp_filename
);
1025 g_propagate_error (error
, rename_error
);
1030 g_error_free (rename_error
);
1032 if (g_unlink (filename
) == -1)
1034 gchar
*display_filename
= g_filename_display_name (filename
);
1038 g_file_error_from_errno (errno
),
1039 _("Existing file '%s' could not be removed: g_unlink() failed: %s"),
1041 g_strerror (errno
));
1043 g_free (display_filename
);
1044 g_unlink (tmp_filename
);
1049 if (!rename_file (tmp_filename
, filename
, error
))
1051 g_unlink (tmp_filename
);
1062 g_free (tmp_filename
);
1067 * mkstemp() implementation is from the GNU C library.
1068 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
1072 * @tmpl: template filename
1074 * Opens a temporary file. See the mkstemp() documentation
1075 * on most UNIX-like systems. This is a portability wrapper, which simply calls
1076 * mkstemp() on systems that have it, and implements
1077 * it in GLib otherwise.
1079 * The parameter is a string that should match the rules for
1080 * mkstemp(), i.e. end in "XXXXXX". The X string will
1081 * be modified to form the name of a file that didn't exist.
1082 * The string should be in the GLib file name encoding. Most importantly,
1083 * on Windows it should be in UTF-8.
1085 * Return value: A file handle (as from open()) to the file
1086 * opened for reading and writing. The file is opened in binary mode
1087 * on platforms where there is a difference. The file handle should be
1088 * closed with close(). In case of errors, -1 is returned.
1091 g_mkstemp (gchar
*tmpl
)
1094 return mkstemp (tmpl
);
1099 static const char letters
[] =
1100 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1101 static const int NLETTERS
= sizeof (letters
) - 1;
1104 static int counter
= 0;
1106 len
= strlen (tmpl
);
1107 if (len
< 6 || strcmp (&tmpl
[len
- 6], "XXXXXX"))
1113 /* This is where the Xs start. */
1114 XXXXXX
= &tmpl
[len
- 6];
1116 /* Get some more or less random data. */
1117 g_get_current_time (&tv
);
1118 value
= (tv
.tv_usec
^ tv
.tv_sec
) + counter
++;
1120 for (count
= 0; count
< 100; value
+= 7777, ++count
)
1124 /* Fill in the random bits. */
1125 XXXXXX
[0] = letters
[v
% NLETTERS
];
1127 XXXXXX
[1] = letters
[v
% NLETTERS
];
1129 XXXXXX
[2] = letters
[v
% NLETTERS
];
1131 XXXXXX
[3] = letters
[v
% NLETTERS
];
1133 XXXXXX
[4] = letters
[v
% NLETTERS
];
1135 XXXXXX
[5] = letters
[v
% NLETTERS
];
1137 /* tmpl is in UTF-8 on Windows, thus use g_open() */
1138 fd
= g_open (tmpl
, O_RDWR
| O_CREAT
| O_EXCL
| O_BINARY
, 0600);
1142 else if (errno
!= EEXIST
)
1143 /* Any other error will apply also to other names we might
1144 * try, and there are 2^32 or so of them, so give up now.
1149 /* We got out of the loop because we ran out of combinations to try. */
1159 /* Binary compatibility version. Not for newly compiled code. */
1162 g_mkstemp (gchar
*tmpl
)
1167 static const char letters
[] =
1168 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1169 static const int NLETTERS
= sizeof (letters
) - 1;
1172 static int counter
= 0;
1174 len
= strlen (tmpl
);
1175 if (len
< 6 || strcmp (&tmpl
[len
- 6], "XXXXXX"))
1181 /* This is where the Xs start. */
1182 XXXXXX
= &tmpl
[len
- 6];
1184 /* Get some more or less random data. */
1185 g_get_current_time (&tv
);
1186 value
= (tv
.tv_usec
^ tv
.tv_sec
) + counter
++;
1188 for (count
= 0; count
< 100; value
+= 7777, ++count
)
1192 /* Fill in the random bits. */
1193 XXXXXX
[0] = letters
[v
% NLETTERS
];
1195 XXXXXX
[1] = letters
[v
% NLETTERS
];
1197 XXXXXX
[2] = letters
[v
% NLETTERS
];
1199 XXXXXX
[3] = letters
[v
% NLETTERS
];
1201 XXXXXX
[4] = letters
[v
% NLETTERS
];
1203 XXXXXX
[5] = letters
[v
% NLETTERS
];
1205 /* This is the backward compatibility system codepage version,
1206 * thus use normal open().
1208 fd
= open (tmpl
, O_RDWR
| O_CREAT
| O_EXCL
| O_BINARY
, 0600);
1212 else if (errno
!= EEXIST
)
1213 /* Any other error will apply also to other names we might
1214 * try, and there are 2^32 or so of them, so give up now.
1219 /* We got out of the loop because we ran out of combinations to try. */
1228 * @tmpl: Template for file name, as in g_mkstemp(), basename only
1229 * @name_used: location to store actual name used
1230 * @error: return location for a #GError
1232 * Opens a file for writing in the preferred directory for temporary
1233 * files (as returned by g_get_tmp_dir()).
1235 * @tmpl should be a string in the GLib file name encoding ending with
1236 * six 'X' characters, as the parameter to g_mkstemp() (or mkstemp()).
1237 * However, unlike these functions, the template should only be a
1238 * basename, no directory components are allowed. If template is
1239 * %NULL, a default template is used.
1241 * Note that in contrast to g_mkstemp() (and mkstemp())
1242 * @tmpl is not modified, and might thus be a read-only literal string.
1244 * The actual name used is returned in @name_used if non-%NULL. This
1245 * string should be freed with g_free() when not needed any longer.
1246 * The returned name is in the GLib file name encoding.
1248 * Return value: A file handle (as from open()) to
1249 * the file opened for reading and writing. The file is opened in binary
1250 * mode on platforms where there is a difference. The file handle should be
1251 * closed with close(). In case of errors, -1 is returned
1252 * and @error will be set.
1255 g_file_open_tmp (const gchar
*tmpl
,
1268 if ((slash
= strchr (tmpl
, G_DIR_SEPARATOR
)) != NULL
1270 || (strchr (tmpl
, '/') != NULL
&& (slash
= "/"))
1274 gchar
*display_tmpl
= g_filename_display_name (tmpl
);
1281 G_FILE_ERROR_FAILED
,
1282 _("Template '%s' invalid, should not contain a '%s'"),
1284 g_free (display_tmpl
);
1289 if (strlen (tmpl
) < 6 ||
1290 strcmp (tmpl
+ strlen (tmpl
) - 6, "XXXXXX") != 0)
1292 gchar
*display_tmpl
= g_filename_display_name (tmpl
);
1295 G_FILE_ERROR_FAILED
,
1296 _("Template '%s' doesn't end with XXXXXX"),
1298 g_free (display_tmpl
);
1302 tmpdir
= g_get_tmp_dir ();
1304 if (G_IS_DIR_SEPARATOR (tmpdir
[strlen (tmpdir
) - 1]))
1307 sep
= G_DIR_SEPARATOR_S
;
1309 fulltemplate
= g_strconcat (tmpdir
, sep
, tmpl
, NULL
);
1311 retval
= g_mkstemp (fulltemplate
);
1315 int save_errno
= errno
;
1316 gchar
*display_fulltemplate
= g_filename_display_name (fulltemplate
);
1320 g_file_error_from_errno (save_errno
),
1321 _("Failed to create file '%s': %s"),
1322 display_fulltemplate
, g_strerror (save_errno
));
1323 g_free (display_fulltemplate
);
1324 g_free (fulltemplate
);
1329 *name_used
= fulltemplate
;
1331 g_free (fulltemplate
);
1338 #undef g_file_open_tmp
1340 /* Binary compatibility version. Not for newly compiled code. */
1343 g_file_open_tmp (const gchar
*tmpl
,
1347 gchar
*utf8_tmpl
= g_locale_to_utf8 (tmpl
, -1, NULL
, NULL
, error
);
1348 gchar
*utf8_name_used
;
1351 if (utf8_tmpl
== NULL
)
1354 retval
= g_file_open_tmp_utf8 (utf8_tmpl
, &utf8_name_used
, error
);
1360 *name_used
= g_locale_from_utf8 (utf8_name_used
, -1, NULL
, NULL
, NULL
);
1362 g_free (utf8_name_used
);
1370 g_build_pathv (const gchar
*separator
,
1371 const gchar
*first_element
,
1375 gint separator_len
= strlen (separator
);
1376 gboolean is_first
= TRUE
;
1377 gboolean have_leading
= FALSE
;
1378 const gchar
*single_element
= NULL
;
1379 const gchar
*next_element
;
1380 const gchar
*last_trailing
= NULL
;
1382 result
= g_string_new (NULL
);
1384 next_element
= first_element
;
1388 const gchar
*element
;
1394 element
= next_element
;
1395 next_element
= va_arg (args
, gchar
*);
1400 /* Ignore empty elements */
1409 strncmp (start
, separator
, separator_len
) == 0)
1410 start
+= separator_len
;
1413 end
= start
+ strlen (start
);
1417 while (end
>= start
+ separator_len
&&
1418 strncmp (end
- separator_len
, separator
, separator_len
) == 0)
1419 end
-= separator_len
;
1421 last_trailing
= end
;
1422 while (last_trailing
>= element
+ separator_len
&&
1423 strncmp (last_trailing
- separator_len
, separator
, separator_len
) == 0)
1424 last_trailing
-= separator_len
;
1428 /* If the leading and trailing separator strings are in the
1429 * same element and overlap, the result is exactly that element
1431 if (last_trailing
<= start
)
1432 single_element
= element
;
1434 g_string_append_len (result
, element
, start
- element
);
1435 have_leading
= TRUE
;
1438 single_element
= NULL
;
1445 g_string_append (result
, separator
);
1447 g_string_append_len (result
, start
, end
- start
);
1453 g_string_free (result
, TRUE
);
1454 return g_strdup (single_element
);
1459 g_string_append (result
, last_trailing
);
1461 return g_string_free (result
, FALSE
);
1467 * @separator: a string used to separator the elements of the path.
1468 * @first_element: the first element in the path
1469 * @Varargs: remaining elements in path, terminated by %NULL
1471 * Creates a path from a series of elements using @separator as the
1472 * separator between elements. At the boundary between two elements,
1473 * any trailing occurrences of separator in the first element, or
1474 * leading occurrences of separator in the second element are removed
1475 * and exactly one copy of the separator is inserted.
1477 * Empty elements are ignored.
1479 * The number of leading copies of the separator on the result is
1480 * the same as the number of leading copies of the separator on
1481 * the first non-empty element.
1483 * The number of trailing copies of the separator on the result is
1484 * the same as the number of trailing copies of the separator on
1485 * the last non-empty element. (Determination of the number of
1486 * trailing copies is done without stripping leading copies, so
1487 * if the separator is <literal>ABA</literal>, <literal>ABABA</literal>
1488 * has 1 trailing copy.)
1490 * However, if there is only a single non-empty element, and there
1491 * are no characters in that element not part of the leading or
1492 * trailing separators, then the result is exactly the original value
1495 * Other than for determination of the number of leading and trailing
1496 * copies of the separator, elements consisting only of copies
1497 * of the separator are ignored.
1499 * Return value: a newly-allocated string that must be freed with g_free().
1502 g_build_path (const gchar
*separator
,
1503 const gchar
*first_element
,
1509 g_return_val_if_fail (separator
!= NULL
, NULL
);
1511 va_start (args
, first_element
);
1512 str
= g_build_pathv (separator
, first_element
, args
);
1520 * @first_element: the first element in the path
1521 * @Varargs: remaining elements in path, terminated by %NULL
1523 * Creates a filename from a series of elements using the correct
1524 * separator for filenames.
1526 * On Unix, this function behaves identically to <literal>g_build_path
1527 * (G_DIR_SEPARATOR_S, first_element, ....)</literal>.
1529 * On Windows, it takes into account that either the backslash
1530 * (<literal>\</literal> or slash (<literal>/</literal>) can be used
1531 * as separator in filenames, but otherwise behaves as on Unix. When
1532 * file pathname separators need to be inserted, the one that last
1533 * previously occurred in the parameters (reading from left to right)
1536 * No attempt is made to force the resulting filename to be an absolute
1537 * path. If the first element is a relative path, the result will
1538 * be a relative path.
1540 * Return value: a newly-allocated string that must be freed with g_free().
1543 g_build_filename (const gchar
*first_element
,
1550 va_start (args
, first_element
);
1551 str
= g_build_pathv (G_DIR_SEPARATOR_S
, first_element
, args
);
1556 /* Code copied from g_build_pathv(), and modifed to use two
1557 * alternative single-character separators.
1561 gboolean is_first
= TRUE
;
1562 gboolean have_leading
= FALSE
;
1563 const gchar
*single_element
= NULL
;
1564 const gchar
*next_element
;
1565 const gchar
*last_trailing
= NULL
;
1566 gchar current_separator
= '\\';
1568 va_start (args
, first_element
);
1570 result
= g_string_new (NULL
);
1572 next_element
= first_element
;
1576 const gchar
*element
;
1582 element
= next_element
;
1583 next_element
= va_arg (args
, gchar
*);
1588 /* Ignore empty elements */
1597 (*start
== '\\' || *start
== '/'))
1599 current_separator
= *start
;
1604 end
= start
+ strlen (start
);
1608 while (end
>= start
+ 1 &&
1609 (end
[-1] == '\\' || end
[-1] == '/'))
1611 current_separator
= end
[-1];
1615 last_trailing
= end
;
1616 while (last_trailing
>= element
+ 1 &&
1617 (last_trailing
[-1] == '\\' || last_trailing
[-1] == '/'))
1622 /* If the leading and trailing separator strings are in the
1623 * same element and overlap, the result is exactly that element
1625 if (last_trailing
<= start
)
1626 single_element
= element
;
1628 g_string_append_len (result
, element
, start
- element
);
1629 have_leading
= TRUE
;
1632 single_element
= NULL
;
1639 g_string_append_len (result
, ¤t_separator
, 1);
1641 g_string_append_len (result
, start
, end
- start
);
1649 g_string_free (result
, TRUE
);
1650 return g_strdup (single_element
);
1655 g_string_append (result
, last_trailing
);
1657 return g_string_free (result
, FALSE
);
1664 * @filename: the symbolic link
1665 * @error: return location for a #GError
1667 * Reads the contents of the symbolic link @filename like the POSIX
1668 * readlink() function. The returned string is in the encoding used
1669 * for filenames. Use g_filename_to_utf8() to convert it to UTF-8.
1671 * Returns: A newly allocated string with the contents of the symbolic link,
1672 * or %NULL if an error occurred.
1677 g_file_read_link (const gchar
*filename
,
1680 #ifdef HAVE_READLINK
1686 buffer
= g_malloc (size
);
1690 read_size
= readlink (filename
, buffer
, size
);
1691 if (read_size
< 0) {
1692 int save_errno
= errno
;
1693 gchar
*display_filename
= g_filename_display_name (filename
);
1698 g_file_error_from_errno (save_errno
),
1699 _("Failed to read the symbolic link '%s': %s"),
1701 g_strerror (save_errno
));
1702 g_free (display_filename
);
1707 if (read_size
< size
)
1709 buffer
[read_size
] = 0;
1714 buffer
= g_realloc (buffer
, size
);
1720 _("Symbolic links not supported"));
1726 #define __G_FILEUTILS_C__
1727 #include "galiasdef.c"