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).

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@3507 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
Enrico Tröger 2009-01-24 19:39:10 +00:00
parent f0a1081c38
commit ba97eb158c
2 changed files with 17 additions and 27 deletions

View File

@ -6,6 +6,11 @@
Reset the file status when saving a file failed. Reset the file status when saving a file failed.
Disable GIO based file monitoring for now since it doesn't work yet Disable GIO based file monitoring for now since it doesn't work yet
as stable as it should. Define USE_GIO_FILEMON to use. 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 <enrico(dot)troeger(at)uvena(dot)de> 2009-01-22 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>

View File

@ -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. * Write the given @c text into a file with @c filename.
* If the file doesn't exist, it will be created. * 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 filename The filename of the file to write, in locale encoding.
* @param text The text to write into the file. * @param text The text to write into the file.
@ -211,35 +214,17 @@ gboolean utils_is_opening_brace(gchar c, gboolean include_angles)
**/ **/
gint utils_write_file(const gchar *filename, const gchar *text) gint utils_write_file(const gchar *filename, const gchar *text)
{ {
FILE *fp; GError *error = NULL;
gint bytes_written, len;
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; geany_debug("%s: could not write to file %s (%s)", G_STRFUNC, filename, error->message);
} g_error_free(error);
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; return EIO;
} }
}
else
{
geany_debug("utils_write_file(): could not write to file %s (%s)",
filename, g_strerror(errno));
return errno;
}
return 0; return 0;
} }