Fix nasty double free crashes
[geeqie.git] / src / collect-io.c
index 941964a..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
  *
@@ -38,7 +38,7 @@ 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;
 
@@ -52,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;
@@ -148,17 +148,20 @@ static gint collection_load_private(CollectionData *cd, const gchar *path, Colle
                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] == G_DIR_SEPARATOR && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE));
+                       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);
-                       g_free(buf);
 
                        total++;
                        if (!valid)
@@ -187,7 +190,7 @@ 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;
@@ -207,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))
                {
@@ -304,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;
 
@@ -321,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;
@@ -388,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))
                {
@@ -399,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);
 }
@@ -444,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,
@@ -494,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)
@@ -630,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;
@@ -668,14 +677,11 @@ static void collect_manager_refresh(void)
 {
        GList *list;
        GList *work;
-       gchar *base;
        FileData *dir_fd;
 
-       base = g_build_filename(homedir(), GQ_RC_DIR_COLLECTIONS, NULL);
-       dir_fd = file_data_new_simple(base);
+       dir_fd = file_data_new_dir(get_collections_dir());
        filelist_read(dir_fd, &list, NULL);
        file_data_unref(dir_fd);
-       g_free(base);
 
        work = collection_manager_entry_list;
        while (work && list)
@@ -704,6 +710,8 @@ static void collect_manager_refresh(void)
                        else
                                {
                                collect_manager_entry_free(entry);
+
+                               entry = NULL;
                                }
                        }
                }
@@ -725,7 +733,7 @@ static void collect_manager_refresh(void)
 static void collect_manager_process_actions(gint max)
 {
        if (collection_manager_action_list) DEBUG_1("collection manager processing actions");
-       
+
        while (collection_manager_action_list != NULL && max > 0)
                {
                CollectManagerAction *action;
@@ -781,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;
 
@@ -815,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);
@@ -827,24 +834,24 @@ static gint collect_manager_process_cb(gpointer data)
        return FALSE;
 }
 
-static gint collect_manager_timer_cb(gpointer data)
+static gboolean collect_manager_timer_cb(gpointer data)
 {
        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)
@@ -934,10 +941,10 @@ void collect_manager_flush(void)
 
 void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer data)
 {
+       if (!(type & NOTIFY_CHANGE) || !fd->change) return;
 
-       if (type != NOTIFY_TYPE_CHANGE || !fd->change) return;
-       
-       switch(fd->change->type)
+       DEBUG_1("Notify collect_manager: %s %04x", fd->path, type);
+       switch (fd->change->type)
                {
                case FILEDATA_CHANGE_MOVE:
                        collect_manager_moved(fd);
@@ -948,9 +955,10 @@ void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer data)
                        collect_manager_moved(fd);
                        break;
                case FILEDATA_CHANGE_DELETE:
-                       break;
                case FILEDATA_CHANGE_UNSPECIFIED:
+               case FILEDATA_CHANGE_WRITE_METADATA:
                        break;
                }
 
 }
+/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */