SF.net SVN: geany:[3507] trunk

eht16 at users.sourceforge.net eht16 at xxxxx
Sat Jan 24 19:39:10 UTC 2009


Revision: 3507
          http://geany.svn.sourceforge.net/geany/?rev=3507&view=rev
Author:   eht16
Date:     2009-01-24 19:39:10 +0000 (Sat, 24 Jan 2009)

Log Message:
-----------
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).

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/src/utils.c

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2009-01-24 19:38:53 UTC (rev 3506)
+++ trunk/ChangeLog	2009-01-24 19:39:10 UTC (rev 3507)
@@ -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  <enrico(dot)troeger(at)uvena(dot)de>

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2009-01-24 19:38:53 UTC (rev 3506)
+++ trunk/src/utils.c	2009-01-24 19:39:10 UTC (rev 3507)
@@ -201,7 +201,10 @@
 /**
  *  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,35 +214,17 @@
  **/
 gint utils_write_file(const gchar *filename, const gchar *text)
 {
-	FILE *fp;
-	gint bytes_written, len;
+	GError *error = NULL;
 
-	if (filename == NULL)
-	{
-		return ENOENT;
-	}
+	g_return_val_if_fail(filename != NULL, ENOENT);
+	g_return_val_if_fail(text != NULL, EINVAL);
 
-	len = strlen(text);
-
-	fp = g_fopen(filename, "w");
-	if (fp != NULL)
+	if (! g_file_set_contents(filename, text, -1, &error))
 	{
-		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;
-		}
+		geany_debug("%s: could not write to file %s (%s)", G_STRFUNC, filename, error->message);
+		g_error_free(error);
+		return EIO;
 	}
-	else
-	{
-		geany_debug("utils_write_file(): could not write to file %s (%s)",
-			filename, g_strerror(errno));
-		return errno;
-	}
 	return 0;
 }
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list