gfileutils.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* gfileutils.h - File utility functions
  2. *
  3. * Copyright 2000 Red Hat, Inc.
  4. *
  5. * SPDX-License-Identifier: LGPL-2.1-or-later
  6. *
  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.
  11. *
  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.
  16. *
  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/>.
  19. */
  20. #ifndef __G_FILEUTILS_H__
  21. #define __G_FILEUTILS_H__
  22. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  23. #error "Only <glib.h> can be included directly."
  24. #endif
  25. #include <glibconfig.h>
  26. #include <glib/gerror.h>
  27. G_BEGIN_DECLS
  28. #define G_FILE_ERROR g_file_error_quark ()
  29. typedef enum
  30. {
  31. G_FILE_ERROR_EXIST,
  32. G_FILE_ERROR_ISDIR,
  33. G_FILE_ERROR_ACCES,
  34. G_FILE_ERROR_NAMETOOLONG,
  35. G_FILE_ERROR_NOENT,
  36. G_FILE_ERROR_NOTDIR,
  37. G_FILE_ERROR_NXIO,
  38. G_FILE_ERROR_NODEV,
  39. G_FILE_ERROR_ROFS,
  40. G_FILE_ERROR_TXTBSY,
  41. G_FILE_ERROR_FAULT,
  42. G_FILE_ERROR_LOOP,
  43. G_FILE_ERROR_NOSPC,
  44. G_FILE_ERROR_NOMEM,
  45. G_FILE_ERROR_MFILE,
  46. G_FILE_ERROR_NFILE,
  47. G_FILE_ERROR_BADF,
  48. G_FILE_ERROR_INVAL,
  49. G_FILE_ERROR_PIPE,
  50. G_FILE_ERROR_AGAIN,
  51. G_FILE_ERROR_INTR,
  52. G_FILE_ERROR_IO,
  53. G_FILE_ERROR_PERM,
  54. G_FILE_ERROR_NOSYS,
  55. G_FILE_ERROR_FAILED
  56. } GFileError;
  57. /* For backward-compat reasons, these are synced to an old
  58. * anonymous enum in libgnome. But don't use that enum
  59. * in new code.
  60. */
  61. typedef enum
  62. {
  63. G_FILE_TEST_IS_REGULAR = 1 << 0,
  64. G_FILE_TEST_IS_SYMLINK = 1 << 1,
  65. G_FILE_TEST_IS_DIR = 1 << 2,
  66. G_FILE_TEST_IS_EXECUTABLE = 1 << 3,
  67. G_FILE_TEST_EXISTS = 1 << 4
  68. } GFileTest;
  69. /**
  70. * GFileSetContentsFlags:
  71. * @G_FILE_SET_CONTENTS_NONE: No guarantees about file consistency or durability.
  72. * The most dangerous setting, which is slightly faster than other settings.
  73. * @G_FILE_SET_CONTENTS_CONSISTENT: Guarantee file consistency: after a crash,
  74. * either the old version of the file or the new version of the file will be
  75. * available, but not a mixture. On Unix systems this equates to an `fsync()`
  76. * on the file and use of an atomic `rename()` of the new version of the file
  77. * over the old.
  78. * @G_FILE_SET_CONTENTS_DURABLE: Guarantee file durability: after a crash, the
  79. * new version of the file will be available. On Unix systems this equates to
  80. * an `fsync()` on the file (if %G_FILE_SET_CONTENTS_CONSISTENT is unset), or
  81. * the effects of %G_FILE_SET_CONTENTS_CONSISTENT plus an `fsync()` on the
  82. * directory containing the file after calling `rename()`.
  83. * @G_FILE_SET_CONTENTS_ONLY_EXISTING: Only apply consistency and durability
  84. * guarantees if the file already exists. This may speed up file operations
  85. * if the file doesn’t currently exist, but may result in a corrupted version
  86. * of the new file if the system crashes while writing it.
  87. *
  88. * Flags to pass to g_file_set_contents_full() to affect its safety and
  89. * performance.
  90. *
  91. * Since: 2.66
  92. */
  93. typedef enum
  94. {
  95. G_FILE_SET_CONTENTS_NONE = 0,
  96. G_FILE_SET_CONTENTS_CONSISTENT = 1 << 0,
  97. G_FILE_SET_CONTENTS_DURABLE = 1 << 1,
  98. G_FILE_SET_CONTENTS_ONLY_EXISTING = 1 << 2
  99. } GFileSetContentsFlags
  100. GLIB_AVAILABLE_ENUMERATOR_IN_2_66;
  101. GLIB_AVAILABLE_IN_ALL
  102. GQuark g_file_error_quark (void);
  103. /* So other code can generate a GFileError */
  104. GLIB_AVAILABLE_IN_ALL
  105. GFileError g_file_error_from_errno (gint err_no);
  106. GLIB_AVAILABLE_IN_ALL
  107. gboolean g_file_test (const gchar *filename,
  108. GFileTest test);
  109. GLIB_AVAILABLE_IN_ALL
  110. gboolean g_file_get_contents (const gchar *filename,
  111. gchar **contents,
  112. gsize *length,
  113. GError **error);
  114. GLIB_AVAILABLE_IN_ALL
  115. gboolean g_file_set_contents (const gchar *filename,
  116. const gchar *contents,
  117. gssize length,
  118. GError **error);
  119. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  120. GLIB_AVAILABLE_IN_2_66
  121. gboolean g_file_set_contents_full (const gchar *filename,
  122. const gchar *contents,
  123. gssize length,
  124. GFileSetContentsFlags flags,
  125. int mode,
  126. GError **error);
  127. G_GNUC_END_IGNORE_DEPRECATIONS
  128. GLIB_AVAILABLE_IN_ALL
  129. gchar *g_file_read_link (const gchar *filename,
  130. GError **error);
  131. /* Wrapper / workalike for mkdtemp() */
  132. GLIB_AVAILABLE_IN_2_30
  133. gchar *g_mkdtemp (gchar *tmpl);
  134. GLIB_AVAILABLE_IN_2_30
  135. gchar *g_mkdtemp_full (gchar *tmpl,
  136. gint mode);
  137. /* Wrapper / workalike for mkstemp() */
  138. GLIB_AVAILABLE_IN_ALL
  139. gint g_mkstemp (gchar *tmpl);
  140. GLIB_AVAILABLE_IN_ALL
  141. gint g_mkstemp_full (gchar *tmpl,
  142. gint flags,
  143. gint mode);
  144. /* Wrappers for g_mkstemp and g_mkdtemp() */
  145. GLIB_AVAILABLE_IN_ALL
  146. gint g_file_open_tmp (const gchar *tmpl,
  147. gchar **name_used,
  148. GError **error);
  149. GLIB_AVAILABLE_IN_2_30
  150. gchar *g_dir_make_tmp (const gchar *tmpl,
  151. GError **error);
  152. GLIB_AVAILABLE_IN_ALL
  153. gchar *g_build_path (const gchar *separator,
  154. const gchar *first_element,
  155. ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
  156. GLIB_AVAILABLE_IN_ALL
  157. gchar *g_build_pathv (const gchar *separator,
  158. gchar **args) G_GNUC_MALLOC;
  159. GLIB_AVAILABLE_IN_ALL
  160. gchar *g_build_filename (const gchar *first_element,
  161. ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
  162. GLIB_AVAILABLE_IN_ALL
  163. gchar *g_build_filenamev (gchar **args) G_GNUC_MALLOC;
  164. GLIB_AVAILABLE_IN_2_56
  165. gchar *g_build_filename_valist (const gchar *first_element,
  166. va_list *args) G_GNUC_MALLOC;
  167. GLIB_AVAILABLE_IN_ALL
  168. gint g_mkdir_with_parents (const gchar *pathname,
  169. gint mode);
  170. #ifdef G_OS_WIN32
  171. /* On Win32, the canonical directory separator is the backslash, and
  172. * the search path separator is the semicolon. Note that also the
  173. * (forward) slash works as directory separator.
  174. */
  175. #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
  176. #else /* !G_OS_WIN32 */
  177. #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
  178. #endif /* !G_OS_WIN32 */
  179. GLIB_AVAILABLE_IN_ALL
  180. gboolean g_path_is_absolute (const gchar *file_name);
  181. GLIB_AVAILABLE_IN_ALL
  182. const gchar *g_path_skip_root (const gchar *file_name);
  183. GLIB_DEPRECATED_FOR(g_path_get_basename)
  184. const gchar *g_basename (const gchar *file_name);
  185. #define g_dirname g_path_get_dirname GLIB_DEPRECATED_MACRO_IN_2_26_FOR(g_path_get_dirname)
  186. GLIB_AVAILABLE_IN_ALL
  187. gchar *g_get_current_dir (void);
  188. GLIB_AVAILABLE_IN_ALL
  189. gchar *g_path_get_basename (const gchar *file_name) G_GNUC_MALLOC;
  190. GLIB_AVAILABLE_IN_ALL
  191. gchar *g_path_get_dirname (const gchar *file_name) G_GNUC_MALLOC;
  192. GLIB_AVAILABLE_IN_2_58
  193. gchar *g_canonicalize_filename (const gchar *filename,
  194. const gchar *relative_to) G_GNUC_MALLOC;
  195. G_END_DECLS
  196. #endif /* __G_FILEUTILS_H__ */