]> Pileus Git - grits/blobdiff - src/data.c
fixing more memory leaks
[grits] / src / data.c
index 7c43138a5c5f51863454ac2f78b6950dee137f57..e4f2a8ce265bbfa0ea2f7dad2b1507d0770c047e 100644 (file)
@@ -1,3 +1,20 @@
+/*
+ * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <config.h>
 #include <glib.h>
 #include <gio/gio.h>
@@ -6,8 +23,10 @@
 
 typedef struct {
        AWeatherCacheDoneCallback callback;
-       gchar *src;
-       gchar *dst;
+       gchar *url;
+       gchar *local;
+       GFile *src;
+       GFile *dst;
        gchar *user_data;
 } cache_file_end_t;
 
@@ -18,22 +37,31 @@ static void cache_file_cb(GObject *source_object, GAsyncResult *res, gpointer _i
        g_file_copy_finish(G_FILE(source_object), res, &error);
        if (error) {
                g_message("error copying file ([%s]->[%s]): %s",
-                       info->src, info->dst, error->message);
+                       info->url, info->local, error->message);
+               g_error_free(error);
        } else {
-               info->callback(info->dst, info->user_data);
+               info->callback(info->local, info->user_data);
        }
-       g_free(info->src);
-       g_free(info->dst);
+       g_object_unref(info->src);
+       g_object_unref(info->dst);
+       g_free(info->url);
+       g_free(info->local);
        g_free(info);
 }
 
 static goffset g_file_get_size(GFile *file)
 {
        GError *error = NULL;
-       GFileInfo *info = g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, &error);
-       if (error)
+       GFileInfo *info = g_file_query_info(file,
+                       G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, &error);
+       if (error){
                g_warning("unable to get file size: %s", error->message);
-       return g_file_info_get_size(info);
+               g_error_free(error);
+       }
+       goffset size = g_file_info_get_size(info);
+       g_file_info_remove_attribute(info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
+       g_object_unref(info);
+       return size;
 }
 
 /**
@@ -45,23 +73,33 @@ void cache_file(char *base, char *path, AWeatherCacheDoneCallback callback, gpoi
 {
        gchar *url   = g_strconcat(base, path, NULL);
        gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
-       GFile *src = g_file_new_for_uri(url);
-       GFile *dst = g_file_new_for_path(local);
+       GFile *src   = g_file_new_for_uri(url);
+       GFile *dst   = g_file_new_for_path(local);
 
        if (!g_file_test(local, G_FILE_TEST_EXISTS))
                g_message("Caching file: local does not exist - %s", local);
        else if (g_file_get_size(src) != g_file_get_size(dst))
                g_message("Caching file: sizes mismatch - %lld != %lld",
                                g_file_get_size(src), g_file_get_size(dst));
-       else
-               return callback(local, user_data);
+       else {
+               callback(local, user_data);
+               g_object_unref(src);
+               g_object_unref(dst);
+               g_free(local);
+               g_free(url);
+               return;
+       }
 
-       if (!g_file_test(g_path_get_dirname(local), G_FILE_TEST_IS_DIR))
-               g_mkdir_with_parents(g_path_get_dirname(local), 0755);
+       char *dir = g_path_get_dirname(local);
+       if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
+               g_mkdir_with_parents(dir, 0755);
+       g_free(dir);
        cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
        info->callback  = callback;
-       info->src       = url;
-       info->dst       = local;
+       info->url       = url;
+       info->local     = local;
+       info->src       = src;
+       info->dst       = dst;
        info->user_data = user_data;
        g_file_copy_async(src, dst,
                G_FILE_COPY_OVERWRITE, // GFileCopyFlags flags,