45 #include <glib/gi18n.h> 46 #include <glib/gstdio.h> 67 gboolean ignore_error,
68 gboolean return_empty_struct,
69 GError **caller_error)
74 g_return_val_if_fail(filename != NULL, NULL);
76 if (!g_file_test(filename, G_FILE_TEST_EXISTS))
79 key_file = g_key_file_new();
83 if (g_key_file_load_from_file(key_file, filename, G_KEY_FILE_NONE, &error))
87 if (!return_empty_struct)
89 g_key_file_free(key_file);
94 g_warning(
"Unable to read file %s: %s\n", filename, error->message);
95 g_propagate_error(caller_error, error);
109 gboolean success = TRUE;
111 g_return_val_if_fail(filename != NULL, FALSE);
112 g_return_val_if_fail(key_file != NULL, FALSE);
114 g_return_val_if_fail(*error == NULL, FALSE);
116 contents = g_key_file_to_data(key_file, NULL, NULL);
117 g_debug (
"Keyfile data:\n%s", contents);
118 length = strlen(contents);
119 fd = g_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
124 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
130 g_critical(
"Cannot open file %s: %s\n", filename, strerror(errno));
136 written = write(fd, contents, length);
142 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
143 "Cannot write to file %s: %s", filename,
148 g_critical(
"Cannot write to file %s: %s\n", filename, strerror(errno));
152 else if (written != length)
157 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
158 "File %s truncated (provided %d, written %d)",
159 filename, length, (
int)written);
163 g_critical(
"File %s truncated (provided %d, written %d)",
164 filename, length, (
int)written);
169 else if (close(fd) == -1)
173 *error = g_error_new(G_FILE_ERROR, g_file_error_from_errno(errno),
174 "Close failed for file %s: %s", filename,
179 g_warning(
"Close failed for file %s: %s", filename, strerror(errno));
GKeyFile helper routines.
gboolean gnc_key_file_save_to_file(const gchar *filename, GKeyFile *key_file, GError **error)
Write a key/value file from memory to disk.
GKeyFile * gnc_key_file_load_from_file(const gchar *filename, gboolean ignore_error, gboolean return_empty_struct, GError **caller_error)
Open and read a key/value file from disk into memory.