diff --git a/ChangeLog b/ChangeLog index a30b26c4..5044a6f2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,11 @@ Reset the file status when saving a file failed. Disable GIO based file monitoring for now since it doesn't work yet as stable as it should. Define USE_GIO_FILEMON to use. + * src/utils.c: + Use g_file_set_contents() to write files to disk in + utils_write_file() for better error checking and to avoid corruption + of config files when there is no more free disk space. + This is not used for saving documents (part of Debian bug #503391). 2009-01-22 Enrico Tröger diff --git a/src/utils.c b/src/utils.c index 602c2a6c..53c31a1e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -201,7 +201,10 @@ gboolean utils_is_opening_brace(gchar c, gboolean include_angles) /** * Write the given @c text into a file with @c filename. * If the file doesn't exist, it will be created. - * If it already exists, it will be overwritten. + * If it already exists, it will be overwritten. Internally, g_file_set_contents() is used + * to write the file with all its error checking and related limitations like + * destroying hard links and possibly losing file permissions. Please read the + * API documentation of g_file_set_contents() for details. * * @param filename The filename of the file to write, in locale encoding. * @param text The text to write into the file. @@ -211,34 +214,16 @@ gboolean utils_is_opening_brace(gchar c, gboolean include_angles) **/ gint utils_write_file(const gchar *filename, const gchar *text) { - FILE *fp; - gint bytes_written, len; + GError *error = NULL; - if (filename == NULL) + g_return_val_if_fail(filename != NULL, ENOENT); + g_return_val_if_fail(text != NULL, EINVAL); + + if (! g_file_set_contents(filename, text, -1, &error)) { - return ENOENT; - } - - len = strlen(text); - - fp = g_fopen(filename, "w"); - if (fp != NULL) - { - bytes_written = fwrite(text, sizeof (gchar), len, fp); - fclose(fp); - - if (len != bytes_written) - { - geany_debug("utils_write_file(): written only %d bytes, had to write %d bytes to %s", - bytes_written, len, filename); - return EIO; - } - } - else - { - geany_debug("utils_write_file(): could not write to file %s (%s)", - filename, g_strerror(errno)); - return errno; + geany_debug("%s: could not write to file %s (%s)", G_STRFUNC, filename, error->message); + g_error_free(error); + return EIO; } return 0; }