Fix nasty double free crashes
[geeqie.git] / src / collect-io.c
index 62ac2b1..bb9967d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Geeqie
  * (C) 2004 John Ellis
- * Copyright (C) 2008 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: John Ellis
  *
 #include "collect-io.h"
 
 #include "collect.h"
-#include "filelist.h"
+#include "filedata.h"
 #include "layout_util.h"
-#include "rcfile.h"
+#include "misc.h"
 #include "secure_save.h"
 #include "thumb.h"
 #include "ui_fileops.h"
 
-
 #define GQ_COLLECTION_MARKER "#" GQ_APPNAME
 
 #define GQ_COLLECTION_FAIL_MIN     300
@@ -39,11 +38,11 @@ static void collect_manager_entry_reset(CollectManagerEntry *entry);
 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr);
 
 
-static gint scan_geometry(gchar *buffer, gint *x, gint *y, gint *w, gint *h)
+static gboolean scan_geometry(gchar *buffer, gint *x, gint *y, gint *w, gint *h)
 {
        gint nx, ny, nw, nh;
 
-       if(sscanf(buffer, "%d %d %d %d", &nx, &ny, &nw, &nh) != 4) return FALSE;
+       if (sscanf(buffer, "%d %d %d %d", &nx, &ny, &nw, &nh) != 4) return FALSE;
 
        *x = nx;
        *y = ny;
@@ -53,17 +52,17 @@ static gint scan_geometry(gchar *buffer, gint *x, gint *y, gint *w, gint *h)
        return TRUE;
 }
 
-static gint collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
+static gboolean collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
 {
        gchar s_buf[GQ_COLLECTION_READ_BUFSIZE];
        FILE *f;
        gchar *pathl;
-       gint limit_failures = TRUE;
-       gint success = TRUE;
-       gint has_official_header = FALSE;
-       gint has_geometry_header = FALSE;
-       gint has_gqview_header   = FALSE;
-       gint need_header         = TRUE;
+       gboolean limit_failures = TRUE;
+       gboolean success = TRUE;
+       gboolean has_official_header = FALSE;
+       gboolean has_geometry_header = FALSE;
+       gboolean has_gqview_header   = FALSE;
+       gboolean need_header     = TRUE;
        guint total = 0;
        guint fail = 0;
        gboolean changed = FALSE;
@@ -92,7 +91,7 @@ static gint collection_load_private(CollectionData *cd, const gchar *path, Colle
 
        if (!path) path = cd->path;
 
-       if (debug) printf("collection load: append=%d flush=%d only_geometry=%d path=%s\n",
+       DEBUG_1("collection load: append=%d flush=%d only_geometry=%d path=%s",
                          append, flush, only_geometry, path);
 
        /* load it */
@@ -101,7 +100,7 @@ static gint collection_load_private(CollectionData *cd, const gchar *path, Colle
        g_free(pathl);
        if (!f)
                {
-               printf("Failed to open collection file: \"%s\"\n", path);
+               log_printf("Failed to open collection file: \"%s\"\n", path);
                return FALSE;
                }
 
@@ -118,7 +117,7 @@ static gint collection_load_private(CollectionData *cd, const gchar *path, Colle
                if (*p == '#')
                        {
                        if (!need_header) continue;
-                       if (strncasecmp(p, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0)
+                       if (g_ascii_strncasecmp(p, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0)
                                {
                                /* Looks like an official collection, allow unchecked input.
                                 * All this does is allow adding files that may not exist,
@@ -135,7 +134,7 @@ static gint collection_load_private(CollectionData *cd, const gchar *path, Colle
                                cd->window_read = TRUE;
                                if (only_geometry) break;
                                }
-                       else if (strncasecmp(p, "#GQview collection", strlen("#GQview collection")) == 0)
+                       else if (g_ascii_strncasecmp(p, "#GQview collection", strlen("#GQview collection")) == 0)
                                {
                                /* As 2008/04/15 there is no difference between our collection file format
                                 * and GQview 2.1.5 collection file format so ignore failures as well. */
@@ -146,18 +145,23 @@ static gint collection_load_private(CollectionData *cd, const gchar *path, Colle
                        continue;
                        }
 
+               if (only_geometry) continue;
+
                /* Read filenames */
-               buf = quoted_value(p, NULL);
-               if (buf)
+               while (*p && *p != '"') p++;
+               if (*p) p++;
+               buf = p;
+               while (*p && *p != '"') p++;
+               *p = 0;
+               if (*buf)
                        {
-                       gint valid;
+                       gboolean valid;
 
                        if (!flush)
                                changed |= collect_manager_process_action(entry, &buf);
 
-                       valid = (buf[0] == '/' && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE));
-                       if (debug && !valid) printf("collection invalid file: %s\n", buf);
-                       g_free(buf);
+                       valid = (buf[0] == G_DIR_SEPARATOR && collection_add_check(cd, file_data_new_group(buf), FALSE, TRUE));
+                       if (!valid) DEBUG_1("collection invalid file: %s", buf);
 
                        total++;
                        if (!valid)
@@ -167,7 +171,7 @@ static gint collection_load_private(CollectionData *cd, const gchar *path, Colle
                                    fail > GQ_COLLECTION_FAIL_MIN &&
                                    fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
                                        {
-                                       printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path);
+                                       log_printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path);
                                        success = FALSE;
                                        break;
                                        }
@@ -175,7 +179,7 @@ static gint collection_load_private(CollectionData *cd, const gchar *path, Colle
                        }
                }
 
-       if (debug) printf("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d\n",
+       DEBUG_1("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d",
                          total, fail, has_official_header, has_gqview_header, has_geometry_header);
 
        fclose(f);
@@ -186,9 +190,10 @@ static gint collection_load_private(CollectionData *cd, const gchar *path, Colle
                gchar *buf = NULL;
                while (collect_manager_process_action(entry, &buf))
                        {
-                       collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE);
+                       collection_add_check(cd, file_data_new_group(buf), FALSE, TRUE);
                        changed = TRUE;
                        g_free(buf);
+                       buf = NULL;
                        }
                }
 
@@ -205,7 +210,7 @@ static gint collection_load_private(CollectionData *cd, const gchar *path, Colle
        return success;
 }
 
-gint collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
+gboolean collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
 {
        if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
                {
@@ -222,7 +227,7 @@ static void collection_load_thumb_do(CollectionData *cd)
 
        if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
 
-       pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader, TRUE);
+       pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader);
        collection_info_set_thumb(cd->thumb_info, pixbuf);
        g_object_unref(pixbuf);
 
@@ -288,10 +293,10 @@ static void collection_load_thumb_step(CollectionData *cd)
                                   cd);
 
        /* start it */
-       if (!thumb_loader_start(cd->thumb_loader, ci->fd->path))
+       if (!thumb_loader_start(cd->thumb_loader, ci->fd))
                {
                /* error, handle it, do next */
-               if (debug) printf("error loading thumb for %s\n", ci->fd->path);
+               DEBUG_1("error loading thumb for %s", ci->fd->path);
                collection_load_thumb_do(cd);
                collection_load_thumb_step(cd);
                }
@@ -302,7 +307,7 @@ void collection_load_thumb_idle(CollectionData *cd)
        if (!cd->thumb_loader) collection_load_thumb_step(cd);
 }
 
-gint collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
+gboolean collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
 {
        if (!collection_load(cd, path, flags)) return FALSE;
 
@@ -319,7 +324,7 @@ void collection_load_stop(CollectionData *cd)
        cd->thumb_loader = NULL;
 }
 
-static gint collection_save_private(CollectionData *cd, const gchar *path)
+static gboolean collection_save_private(CollectionData *cd, const gchar *path)
 {
        SecureSaveInfo *ssi;
        GList *work;
@@ -338,7 +343,7 @@ static gint collection_save_private(CollectionData *cd, const gchar *path)
        g_free(pathl);
        if (!ssi)
                {
-               printf_term(_("failed to open collection (write) \"%s\"\n"), path);
+               log_printf(_("failed to open collection (write) \"%s\"\n"), path);
                return FALSE;
                }
 
@@ -363,7 +368,7 @@ static gint collection_save_private(CollectionData *cd, const gchar *path)
 
        if (secure_close(ssi))
                {
-               printf_term(_("error saving collection file: %s\nerror: %s\n"), path,
+               log_printf(_("error saving collection file: %s\nerror: %s\n"), path,
                            secsave_strerror(secsave_errno));
                return FALSE;
                }
@@ -386,7 +391,7 @@ static gint collection_save_private(CollectionData *cd, const gchar *path)
        return TRUE;
 }
 
-gint collection_save(CollectionData *cd, const gchar *path)
+gboolean collection_save(CollectionData *cd, const gchar *path)
 {
        if (collection_save_private(cd, path))
                {
@@ -397,7 +402,7 @@ gint collection_save(CollectionData *cd, const gchar *path)
        return FALSE;
 }
 
-gint collection_load_only_geometry(CollectionData *cd, const gchar *path)
+gboolean collection_load_only_geometry(CollectionData *cd, const gchar *path)
 {
        return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
 }
@@ -442,7 +447,7 @@ struct _CollectManagerAction
 static GList *collection_manager_entry_list = NULL;
 static GList *collection_manager_action_list = NULL;
 static GList *collection_manager_action_tail = NULL;
-static gint collection_manager_timer_id = -1;
+static guint collection_manager_timer_id = 0; /* event source id */
 
 
 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
@@ -492,8 +497,14 @@ static void collect_manager_entry_free_data(CollectManagerEntry *entry)
                collect_manager_action_unref(action);
                }
        g_list_free(entry->add_list);
-       g_hash_table_destroy(entry->oldpath_hash);
-       g_hash_table_destroy(entry->newpath_hash);
+       if (g_hash_table_size(entry->oldpath_hash) > 0)
+               g_hash_table_destroy(entry->oldpath_hash);
+       else
+               g_hash_table_unref(entry->oldpath_hash);
+       if (g_hash_table_size(entry->newpath_hash) > 0)
+               g_hash_table_destroy(entry->newpath_hash);
+       else
+               g_hash_table_unref(entry->newpath_hash);
 }
 
 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
@@ -574,7 +585,7 @@ static void collect_manager_entry_add_action(CollectManagerEntry *entry, Collect
                if (orig_action)
                        {
                        /* target already exists */
-                       printf("collection manager failed to add another action for target %s in collection %s\n",
+                       log_printf("collection manager failed to add another action for target %s in collection %s\n",
                                action->newpath, entry->path);
                        return;
                        }
@@ -615,7 +626,7 @@ static void collect_manager_entry_add_action(CollectManagerEntry *entry, Collect
        if (orig_action)
                {
                /* another action for the same source, ignore */
-               printf("collection manager failed to add another action for source %s in collection %s\n",
+               log_printf("collection manager failed to add another action for source %s in collection %s\n",
                        action->oldpath, entry->path);
                return;
                }
@@ -628,7 +639,7 @@ static void collect_manager_entry_add_action(CollectManagerEntry *entry, Collect
        collect_manager_action_ref(action);
 }
 
-static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
+static gboolean collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
 {
        gchar *path = *path_ptr;
        CollectManagerAction *action;
@@ -664,13 +675,13 @@ static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **p
 
 static void collect_manager_refresh(void)
 {
-       GList *list = NULL;
+       GList *list;
        GList *work;
-       gchar *base;
+       FileData *dir_fd;
 
-       base = g_strconcat(homedir(), "/", GQ_RC_DIR_COLLECTIONS, NULL);
-       path_list(base, &list, NULL);
-       g_free(base);
+       dir_fd = file_data_new_dir(get_collections_dir());
+       filelist_read(dir_fd, &list, NULL);
+       file_data_unref(dir_fd);
 
        work = collection_manager_entry_list;
        while (work && list)
@@ -684,21 +695,23 @@ static void collect_manager_refresh(void)
                list_step = list;
                while (list_step && entry)
                        {
-                       gchar *path;
+                       FileData *fd;
 
-                       path = list_step->data;
+                       fd = list_step->data;
                        list_step = list_step->next;
 
-                       if (strcmp(path, entry->path) == 0)
+                       if (strcmp(fd->path, entry->path) == 0)
                                {
-                               list = g_list_remove(list, path);
-                               g_free(path);
+                               list = g_list_remove(list, fd);
+                               file_data_unref(fd);
 
                                entry = NULL;
                                }
                        else
                                {
                                collect_manager_entry_free(entry);
+
+                               entry = NULL;
                                }
                        }
                }
@@ -706,24 +719,20 @@ static void collect_manager_refresh(void)
        work = list;
        while (work)
                {
-               gchar *path;
+               FileData *fd;
 
-               path = work->data;
+               fd = work->data;
                work = work->next;
 
-               collect_manager_entry_new(path);
-               g_free(path);
+               collect_manager_entry_new(fd->path);
                }
 
-       g_list_free(list);
+       filelist_free(list);
 }
 
 static void collect_manager_process_actions(gint max)
 {
-       if (debug && collection_manager_action_list)
-               {
-               printf("collection manager processing actions\n");
-               }
+       if (collection_manager_action_list) DEBUG_1("collection manager processing actions");
 
        while (collection_manager_action_list != NULL && max > 0)
                {
@@ -766,7 +775,7 @@ static void collect_manager_process_actions(gint max)
                if (action->type != COLLECTION_MANAGER_UPDATE &&
                    action->oldpath && action->newpath)
                        {
-                       printf("collection manager failed to %s %s for collection %s\n",
+                       log_printf("collection manager failed to %s %s for collection %s\n",
                                (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
                                action->oldpath, action->newpath);
                        }
@@ -780,22 +789,21 @@ static void collect_manager_process_actions(gint max)
                }
 }
 
-static gint collect_manager_process_entry(CollectManagerEntry *entry)
+static gboolean collect_manager_process_entry(CollectManagerEntry *entry)
 {
        CollectionData *cd;
-       gint success;
 
        if (entry->empty) return FALSE;
 
        cd = collection_new(entry->path);
-       success = collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
+       (void) collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
 
        collection_unref(cd);
 
        return TRUE;
 }
 
-static gint collect_manager_process_entry_list(void)
+static gboolean collect_manager_process_entry_list(void)
 {
        GList *work;
 
@@ -814,7 +822,7 @@ static gint collect_manager_process_entry_list(void)
 
 
 
-static gint collect_manager_process_cb(gpointer data)
+static gboolean collect_manager_process_cb(gpointer data)
 {
        if (collection_manager_action_list) collect_manager_refresh();
        collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
@@ -822,35 +830,35 @@ static gint collect_manager_process_cb(gpointer data)
 
        if (collect_manager_process_entry_list()) return TRUE;
 
-       if (debug) printf("collection manager is up to date\n");
+       DEBUG_1("collection manager is up to date");
        return FALSE;
 }
 
-static gint collect_manager_timer_cb(gpointer data)
+static gboolean collect_manager_timer_cb(gpointer data)
 {
-       if (debug) printf("collection manager timer expired\n");
+       DEBUG_1("collection manager timer expired");
 
        g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
 
-       collection_manager_timer_id = -1;
+       collection_manager_timer_id = 0;
        return FALSE;
 }
 
 static void collect_manager_timer_push(gint stop)
 {
-       if (collection_manager_timer_id != -1)
+       if (collection_manager_timer_id)
                {
                if (!stop) return;
 
                g_source_remove(collection_manager_timer_id);
-               collection_manager_timer_id = -1;
+               collection_manager_timer_id = 0;
                }
 
        if (!stop)
                {
                collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
                                                            collect_manager_timer_cb, NULL);
-               if (debug) printf("collection manager timer started\n");
+               DEBUG_1("collection manager timer started");
                }
 }
 
@@ -894,7 +902,7 @@ void collect_manager_add(FileData *fd, const gchar *collection)
        cw = collection_window_find_by_path(collection);
        if (cw)
                {
-               if (collection_list_find(cw->cd->list, fd->path) == NULL)
+               if (collection_list_find_fd(cw->cd->list, fd) == NULL)
                        {
                        collection_add(cw->cd, fd, FALSE);
                        }
@@ -927,6 +935,30 @@ void collect_manager_flush(void)
 {
        collect_manager_timer_push(TRUE);
 
-       if (debug) printf("collection manager flushing\n");
+       DEBUG_1("collection manager flushing");
        while (collect_manager_process_cb(NULL));
 }
+
+void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer data)
+{
+       if (!(type & NOTIFY_CHANGE) || !fd->change) return;
+
+       DEBUG_1("Notify collect_manager: %s %04x", fd->path, type);
+       switch (fd->change->type)
+               {
+               case FILEDATA_CHANGE_MOVE:
+                       collect_manager_moved(fd);
+                       break;
+               case FILEDATA_CHANGE_COPY:
+                       break;
+               case FILEDATA_CHANGE_RENAME:
+                       collect_manager_moved(fd);
+                       break;
+               case FILEDATA_CHANGE_DELETE:
+               case FILEDATA_CHANGE_UNSPECIFIED:
+               case FILEDATA_CHANGE_WRITE_METADATA:
+                       break;
+               }
+
+}
+/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */