1 /* gfileutils.h - File utility functions
3 * Copyright 2000 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #ifndef __G_FILEUTILS_H__
22 #define __G_FILEUTILS_H__
24 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
25 #error "Only <glib.h> can be included directly."
28 #include <glibconfig.h>
29 #include <glib/gerror.h>
33 #define G_FILE_ERROR g_file_error_quark ()
40 G_FILE_ERROR_NAMETOOLONG,
64 /* For backward-compat reasons, these are synced to an old
65 * anonymous enum in libgnome. But don't use that enum
70 G_FILE_TEST_IS_REGULAR = 1 << 0,
71 G_FILE_TEST_IS_SYMLINK = 1 << 1,
72 G_FILE_TEST_IS_DIR = 1 << 2,
73 G_FILE_TEST_IS_EXECUTABLE = 1 << 3,
74 G_FILE_TEST_EXISTS = 1 << 4
78 * GFileSetContentsFlags:
79 * @G_FILE_SET_CONTENTS_NONE: No guarantees about file consistency or durability.
80 * The most dangerous setting, which is slightly faster than other settings.
81 * @G_FILE_SET_CONTENTS_CONSISTENT: Guarantee file consistency: after a crash,
82 * either the old version of the file or the new version of the file will be
83 * available, but not a mixture. On Unix systems this equates to an `fsync()`
84 * on the file and use of an atomic `rename()` of the new version of the file
86 * @G_FILE_SET_CONTENTS_DURABLE: Guarantee file durability: after a crash, the
87 * new version of the file will be available. On Unix systems this equates to
88 * an `fsync()` on the file (if %G_FILE_SET_CONTENTS_CONSISTENT is unset), or
89 * the effects of %G_FILE_SET_CONTENTS_CONSISTENT plus an `fsync()` on the
90 * directory containing the file after calling `rename()`.
91 * @G_FILE_SET_CONTENTS_ONLY_EXISTING: Only apply consistency and durability
92 * guarantees if the file already exists. This may speed up file operations
93 * if the file doesn’t currently exist, but may result in a corrupted version
94 * of the new file if the system crashes while writing it.
96 * Flags to pass to g_file_set_contents_full() to affect its safety and
103 G_FILE_SET_CONTENTS_NONE = 0,
104 G_FILE_SET_CONTENTS_CONSISTENT = 1 << 0,
105 G_FILE_SET_CONTENTS_DURABLE = 1 << 1,
106 G_FILE_SET_CONTENTS_ONLY_EXISTING = 1 << 2
107 } GFileSetContentsFlags
108 GLIB_AVAILABLE_ENUMERATOR_IN_2_66;
110 GLIB_AVAILABLE_IN_ALL
111 GQuark g_file_error_quark (void);
112 /* So other code can generate a GFileError */
113 GLIB_AVAILABLE_IN_ALL
114 GFileError g_file_error_from_errno (gint err_no);
116 GLIB_AVAILABLE_IN_ALL
117 gboolean g_file_test (const gchar *filename,
119 GLIB_AVAILABLE_IN_ALL
120 gboolean g_file_get_contents (const gchar *filename,
124 GLIB_AVAILABLE_IN_ALL
125 gboolean g_file_set_contents (const gchar *filename,
126 const gchar *contents,
129 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
130 GLIB_AVAILABLE_IN_2_66
131 gboolean g_file_set_contents_full (const gchar *filename,
132 const gchar *contents,
134 GFileSetContentsFlags flags,
137 G_GNUC_END_IGNORE_DEPRECATIONS
138 GLIB_AVAILABLE_IN_ALL
139 gchar *g_file_read_link (const gchar *filename,
142 /* Wrapper / workalike for mkdtemp() */
143 GLIB_AVAILABLE_IN_2_30
144 gchar *g_mkdtemp (gchar *tmpl);
145 GLIB_AVAILABLE_IN_2_30
146 gchar *g_mkdtemp_full (gchar *tmpl,
149 /* Wrapper / workalike for mkstemp() */
150 GLIB_AVAILABLE_IN_ALL
151 gint g_mkstemp (gchar *tmpl);
152 GLIB_AVAILABLE_IN_ALL
153 gint g_mkstemp_full (gchar *tmpl,
157 /* Wrappers for g_mkstemp and g_mkdtemp() */
158 GLIB_AVAILABLE_IN_ALL
159 gint g_file_open_tmp (const gchar *tmpl,
162 GLIB_AVAILABLE_IN_2_30
163 gchar *g_dir_make_tmp (const gchar *tmpl,
166 GLIB_AVAILABLE_IN_ALL
167 gchar *g_build_path (const gchar *separator,
168 const gchar *first_element,
169 ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
170 GLIB_AVAILABLE_IN_ALL
171 gchar *g_build_pathv (const gchar *separator,
172 gchar **args) G_GNUC_MALLOC;
174 GLIB_AVAILABLE_IN_ALL
175 gchar *g_build_filename (const gchar *first_element,
176 ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
177 GLIB_AVAILABLE_IN_ALL
178 gchar *g_build_filenamev (gchar **args) G_GNUC_MALLOC;
179 GLIB_AVAILABLE_IN_2_56
180 gchar *g_build_filename_valist (const gchar *first_element,
181 va_list *args) G_GNUC_MALLOC;
183 GLIB_AVAILABLE_IN_ALL
184 gint g_mkdir_with_parents (const gchar *pathname,
189 /* On Win32, the canonical directory separator is the backslash, and
190 * the search path separator is the semicolon. Note that also the
191 * (forward) slash works as directory separator.
193 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
195 #else /* !G_OS_WIN32 */
197 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
199 #endif /* !G_OS_WIN32 */
201 GLIB_AVAILABLE_IN_ALL
202 gboolean g_path_is_absolute (const gchar *file_name);
203 GLIB_AVAILABLE_IN_ALL
204 const gchar *g_path_skip_root (const gchar *file_name);
206 GLIB_DEPRECATED_FOR(g_path_get_basename)
207 const gchar *g_basename (const gchar *file_name);
208 #define g_dirname g_path_get_dirname GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_path_get_dirname)
210 GLIB_AVAILABLE_IN_ALL
211 gchar *g_get_current_dir (void);
212 GLIB_AVAILABLE_IN_ALL
213 gchar *g_path_get_basename (const gchar *file_name) G_GNUC_MALLOC;
214 GLIB_AVAILABLE_IN_ALL
215 gchar *g_path_get_dirname (const gchar *file_name) G_GNUC_MALLOC;
217 GLIB_AVAILABLE_IN_2_58
218 gchar *g_canonicalize_filename (const gchar *filename,
219 const gchar *relative_to) G_GNUC_MALLOC;
223 #endif /* __G_FILEUTILS_H__ */