[Xfce4-commits] <midori:master> Write cookies to a temporary file first, to be safe

Christian Dywan noreply at xfce.org
Tue Sep 21 00:10:02 CEST 2010


Updating branch refs/heads/master
         to 16ce2e9516676e00c0f441a04c3305614de4d32a (commit)
       from 2c10312ce21d3f7a0fe4929d7ea255e16abb74d0 (commit)

commit 16ce2e9516676e00c0f441a04c3305614de4d32a
Author: Christian Dywan <christian at twotoasts.de>
Date:   Mon Sep 20 23:33:08 2010 +0200

    Write cookies to a temporary file first, to be safe
    
    It may happen that we are running out of space in the middle
    of writing the file, and fopen deleted it already.

 katze/katze-http-cookies.c |   41 +++++++++++++++++++++++++++++++----------
 1 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/katze/katze-http-cookies.c b/katze/katze-http-cookies.c
index c66ee66..c52f788 100644
--- a/katze/katze-http-cookies.c
+++ b/katze/katze-http-cookies.c
@@ -16,9 +16,13 @@
 #include "katze-http-cookies.h"
 
 #include <stdlib.h>
+#ifdef HAVE_UNISTD_H
+    #include <unistd.h>
+#endif
 #include <glib/gi18n.h>
 #include <libsoup/soup.h>
 #include <gtk/gtk.h>
+#include <glib/gstdio.h>
 
 struct _KatzeHttpCookies
 {
@@ -145,12 +149,12 @@ cookie_jar_load (SoupCookieJar* jar,
    Copyright (C) 2008 Xan Lopez <xan at gnome.org>
    Copyright (C) 2008 Dan Winship <danw at gnome.org>
    Copied from libSoup 2.24, coding style preserved */
-static void
+static gboolean
 write_cookie (FILE *out, SoupCookie *cookie)
 {
 	fseek (out, 0, SEEK_END);
 
-	fprintf (out, "%s%s\t%s\t%s\t%s\t%lu\t%s\t%s\n",
+	if (fprintf (out, "%s%s\t%s\t%s\t%s\t%lu\t%s\t%s\n",
 		 cookie->http_only ? "#HttpOnly_" : "",
 		 cookie->domain,
 		 *cookie->domain == '.' ? "TRUE" : "FALSE",
@@ -158,7 +162,9 @@ write_cookie (FILE *out, SoupCookie *cookie)
 		 cookie->secure ? "TRUE" : "FALSE",
 		 (gulong)soup_date_to_time_t (cookie->expires),
 		 cookie->name,
-		 cookie->value);
+		 cookie->value) < 0)
+            return FALSE;
+        return TRUE;
 }
 
 /* Cookie jar saving to Mozilla format
@@ -170,18 +176,21 @@ delete_cookie (const char *filename, SoupCookie *cookie)
 {
 	char *contents = NULL, *line, *p;
 	gsize length = 0;
+	gint fn = 0;
 	FILE *f;
+	gchar* temporary_filename = NULL;
 	SoupCookie *c;
 	time_t now = time (NULL);
 
 	if (!g_file_get_contents (filename, &contents, &length, NULL))
 		return;
 
-	f = fopen (filename, "w");
-	if (!f) {
-		g_free (contents);
-		return;
-	}
+	fn = g_file_open_tmp (NULL, &temporary_filename, NULL);
+	if (fn == -1)
+		goto failed;
+	f = fopen (temporary_filename, "w");
+	if (!f)
+		goto failed;
 
 	line = contents;
 	for (p = contents; *p; p++) {
@@ -200,12 +209,24 @@ delete_cookie (const char *filename, SoupCookie *cookie)
 	c = parse_cookie (line, now);
 	if (c) {
 		if (!soup_cookie_equal (cookie, c))
-			write_cookie (f, c);
+			if (!write_cookie (f, c))
+				goto failed;
 		soup_cookie_free (c);
 	}
 
+	if (!fclose (f))
+            goto failed;
+
 	g_free (contents);
-	fclose (f);
+        close (fn);
+        g_rename (temporary_filename, filename);
+        g_free (temporary_filename);
+        return;
+failed:
+        g_free (contents);
+        close (fn);
+        g_unlink (temporary_filename);
+        g_free (temporary_filename);
 }
 
 /* Cookie jar saving to Mozilla format



More information about the Xfce4-commits mailing list