[evolution-ews/gnome-3-2] Use SHA256 of EWS ItemId for cache filename, not the ItemId itself.



commit 13f3c0639534027b79809dca4586c982851bea07
Author: David Woodhouse <David Woodhouse intel com>
Date:   Thu Oct 20 23:32:02 2011 +0100

    Use SHA256 of EWS ItemId for cache filename, not the ItemId itself.
    
    An ecryptfs user complained; it can't handle long filenames.
    
    Use the sha256 of the id instead, and automatically rename old files as we
    try to open them.
    (cherry picked from commit f7b8b507958a6f3f7ed3918e7df46d4e3b7b5780)

 src/camel/camel-ews-folder.c |   80 ++++++++++++++++++++++++++++++++++++++---
 1 files changed, 74 insertions(+), 6 deletions(-)
---
diff --git a/src/camel/camel-ews-folder.c b/src/camel/camel-ews-folder.c
index 60fe972..8da7801 100644
--- a/src/camel/camel-ews-folder.c
+++ b/src/camel/camel-ews-folder.c
@@ -101,8 +101,64 @@ static gchar *
 ews_get_filename (CamelFolder *folder, const gchar *uid, GError **error)
 {
 	CamelEwsFolder *ews_folder = CAMEL_EWS_FOLDER(folder);
+	GChecksum *sha = g_checksum_new (G_CHECKSUM_SHA256);
+	gchar *ret;
+
+	g_checksum_update(sha, (guchar *)uid, strlen(uid));
+	ret = camel_data_cache_get_filename (ews_folder->cache, "cur",
+					     g_checksum_get_string (sha),
+					     error);
+	g_checksum_free (sha);
+	return ret;
+}
 
-	return camel_data_cache_get_filename (ews_folder->cache, "cache", uid, error);
+static gint
+ews_data_cache_remove (CamelDataCache *cdc,
+		       const gchar *path,
+		       const gchar *key,
+		       GError **error)
+{
+	GChecksum *sha = g_checksum_new (G_CHECKSUM_SHA256);
+	gint ret;
+
+	g_checksum_update(sha, (guchar *)key, strlen(key));
+	ret = camel_data_cache_remove (cdc, path, g_checksum_get_string (sha),
+				       error);
+	g_checksum_free (sha);
+	return ret;
+}
+
+static CamelStream *
+ews_data_cache_get (CamelDataCache *cdc,
+		    const gchar *path,
+		    const gchar *key,
+		    GError **error)
+{
+	GChecksum *sha = g_checksum_new (G_CHECKSUM_SHA256);
+	CamelStream *ret;
+
+	g_checksum_update(sha, (guchar *)key, strlen(key));
+	ret = camel_data_cache_get (cdc, path, g_checksum_get_string (sha),
+				    error);
+	g_checksum_free (sha);
+	return ret;
+}
+
+static gchar *
+ews_data_cache_get_filename (CamelDataCache *cdc,
+			     const gchar *path,
+			     const gchar *key,
+			     GError **error)
+{
+	GChecksum *sha = g_checksum_new (G_CHECKSUM_SHA256);
+	gchar *ret;
+
+	g_checksum_update(sha, (guchar *)key, strlen(key));
+	ret = camel_data_cache_get_filename (cdc, path,
+					     g_checksum_get_string (sha),
+					     error);
+	g_checksum_free (sha);
+	return ret;
 }
 
 
@@ -116,10 +172,22 @@ camel_ews_folder_get_message_from_cache (CamelEwsFolder *ews_folder, const gchar
 	priv = ews_folder->priv;
 
 	g_static_rec_mutex_lock (&priv->cache_lock);
-	stream = camel_data_cache_get (ews_folder->cache, "cur", uid, error);
+	stream = ews_data_cache_get (ews_folder->cache, "cur", uid, error);
 	if (!stream) {
+		gchar *old_fname = camel_data_cache_get_filename (ews_folder->cache, "cur",
+								  uid, error);
+		if (!g_access (old_fname, R_OK)) {
+			gchar *new_fname = ews_data_cache_get_filename (ews_folder->cache,
+									"cur", uid, error);
+			g_rename (old_fname, new_fname);
+			g_free (new_fname);
+			stream = ews_data_cache_get (ews_folder->cache, "cur", uid, error);
+		}
+		g_free (old_fname);
+		if (!stream) {
 			g_static_rec_mutex_unlock (&priv->cache_lock);
-		return NULL;
+			return NULL;
+		}
 	}
 
 	msg = camel_mime_message_new ();
@@ -416,8 +484,8 @@ camel_ews_folder_get_message (CamelFolder *folder, const gchar *uid, gint pri, G
 		}
 	}
 
-	cache_file = camel_data_cache_get_filename  (ews_folder->cache, "cur",
-						     uid, error);
+	cache_file = ews_data_cache_get_filename (ews_folder->cache, "cur",
+						  uid, error);
 	temp = g_strrstr (cache_file, "/");
 	dir = g_strndup (cache_file, temp - cache_file);
 
@@ -1237,7 +1305,7 @@ ews_delete_messages (CamelFolder *folder, GSList *deleted_items, gboolean expung
 				camel_folder_summary_lock (folder->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 				camel_folder_change_info_remove_uid (changes, uid);
 				camel_folder_summary_remove_uid (folder->summary, uid);
-				camel_data_cache_remove(ews_folder->cache, "cache", uid, NULL);
+				ews_data_cache_remove(ews_folder->cache, "cur", uid, NULL);
 				camel_folder_summary_unlock (folder->summary, CAMEL_FOLDER_SUMMARY_SUMMARY_LOCK);
 				deleted_items = g_slist_next (deleted_items);
 			}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]