Cache files with a dot in front when a directory with the same number exists. Fixes...
authorColin Leroy <colin@colino.net>
Sun, 15 Jun 2014 12:53:31 +0000 (14:53 +0200)
committerColin Leroy <colin@colino.net>
Sun, 15 Jun 2014 12:53:31 +0000 (14:53 +0200)
src/common/utils.c
src/imap.c

index b37a568..8382176 100644 (file)
@@ -2355,6 +2355,12 @@ gint remove_numbered_files(const gchar *dir, guint first, guint last)
        if (first == last) {
                /* Skip all the dir reading part. */
                gchar *filename = g_strdup_printf("%s%s%u", dir, G_DIR_SEPARATOR_S, first);
+               if (is_dir_exist(filename)) {
+                       /* a numbered directory with this name exists,
+                        * remove the dot-file instead */
+                       g_free(filename);
+                       filename = g_strdup_printf("%s%s.%u", dir, G_DIR_SEPARATOR_S, first);
+               }
                if (claws_unlink(filename) < 0) {
                        FILE_OP_ERROR(filename, "unlink");
                        g_free(filename);
@@ -2381,8 +2387,14 @@ gint remove_numbered_files(const gchar *dir, guint first, guint last)
        while ((dir_name = g_dir_read_name(dp)) != NULL) {
                file_no = to_number(dir_name);
                if (file_no > 0 && first <= file_no && file_no <= last) {
-                       if (is_dir_exist(dir_name))
+                       if (is_dir_exist(dir_name)) {
+                               gchar *dot_file = g_strdup_printf(".%s", dir_name);
+                               if (is_file_exist(dot_file) && claws_unlink(dot_file) < 0) {
+                                       FILE_OP_ERROR(dot_file, "unlink");
+                               }
+                               g_free(dot_file);
                                continue;
+                       }
                        if (claws_unlink(dir_name) < 0)
                                FILE_OP_ERROR(dir_name, "unlink");
                }
@@ -2439,6 +2451,14 @@ gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
                        continue;
                if (file_no > 0 && g_hash_table_lookup(wanted_files, GINT_TO_POINTER(file_no)) == NULL) {
                        debug_print("removing unwanted file %d from %s\n", file_no, dir);
+                       if (is_dir_exist(dir_name)) {
+                               gchar *dot_file = g_strdup_printf(".%s", dir_name);
+                               if (is_file_exist(dot_file) && claws_unlink(dot_file) < 0) {
+                                       FILE_OP_ERROR(dot_file, "unlink");
+                               }
+                               g_free(dot_file);
+                               continue;
+                       }
                        if (claws_unlink(dir_name) < 0)
                                FILE_OP_ERROR(dir_name, "unlink");
                }
index 9086635..fc3c50d 100644 (file)
@@ -1330,20 +1330,38 @@ static guint get_file_size_with_crs(const gchar *filename)
        return cnt;
 }
 
-static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
+static gchar *imap_get_cached_filename(FolderItem *item, guint msgnum)
 {
        gchar *path, *filename;
 
+       cm_return_val_if_fail(item != NULL, NULL);
+
        path = folder_item_get_path(item);
 
        if (!is_dir_exist(path)) {
                g_free(path);
-               return;
+               return NULL;
        }
 
-       filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
+       filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msgnum), NULL);
+
+       if (is_dir_exist(filename)) {
+               g_free(filename);
+               filename = g_strconcat(path, G_DIR_SEPARATOR_S, ".", itos(msgnum), NULL);
+       }
        g_free(path);
 
+       return filename;
+}
+
+static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
+{
+       gchar *filename;
+
+       filename = imap_get_cached_filename(item, msginfo->msgnum);
+
+       cm_return_if_fail(filename != NULL);
+
        if (is_file_exist(filename)) {
                claws_unlink(filename);
        }
@@ -1481,9 +1499,11 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
        path = folder_item_get_path(item);
        if (!is_dir_exist(path))
                make_dir_hier(path);
-       filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
        g_free(path);
+
+       filename = imap_get_cached_filename(item, uid);
        debug_print("trying to fetch cached %s\n", filename);
+
        if (is_file_exist(filename)) {
                /* see whether the local file represents the whole message
                 * or not. As the IMAP server reports size with \r chars,
@@ -1581,7 +1601,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
 
 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
 {
-       gchar *path, *filename;
+       gchar *filename;
        guint size = 0;
        MsgInfo *cached = msgcache_get_msg(item->cache,uid);
        
@@ -1592,14 +1612,9 @@ static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint
                procmsg_msginfo_free(cached);
                return TRUE;
        }
-       path = folder_item_get_path(item);
-       if (!is_dir_exist(path)) {
-               g_free(path);
-               return FALSE;
-       }
 
-       filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
-       g_free(path);
+       filename = imap_get_cached_filename(item, uid);
+
        if (is_file_exist(filename)) {
                if (cached && cached->total_size == cached->size) {
                        /* fast path */