Write a key/value file from memory to disk. If there is no data to be written, this function will not create a file and will remove any exiting file.
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 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),
125 "Cannot open file %s: %s", filename,
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));
#define DEBUG(format, args...)