about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/common.c35
-rw-r--r--src/common.h2
-rw-r--r--src/plugins/plugins.c3
3 files changed, 17 insertions, 23 deletions
diff --git a/src/common.c b/src/common.c
index 164523a2..251eddc6 100644
--- a/src/common.c
+++ b/src/common.c
@@ -46,6 +46,7 @@
 #include <curl/curl.h>
 #include <curl/easy.h>
 #include <glib.h>
+#include <gio/gio.h>
 
 #ifdef HAVE_NCURSESW_NCURSES_H
 #include <ncursesw/ncurses.h>
@@ -105,28 +106,22 @@ mkdir_recursive(const char *dir)
 }
 
 gboolean
-copy_file(const char *const sourcepath, const char *const targetpath)
+copy_file(const char *const sourcepath, const char *const targetpath, const gboolean overwrite_existing)
 {
-    int ch;
-    FILE *source = fopen(sourcepath, "rb");
-    if (source == NULL) {
-        return FALSE;
-    }
-
-    FILE *target = fopen(targetpath, "wb");
-    if (target == NULL) {
-        fclose(source);
-        return FALSE;
-    }
-
-    while((ch = fgetc(source)) != EOF) {
-        fputc(ch, target);
+    GFile *source = g_file_new_for_path(sourcepath);
+    GFile *dest = g_file_new_for_path(targetpath);
+    GError *error = NULL;
+    gboolean success = false;
+    
+    if (overwrite_existing)
+    {
+        success = g_file_copy (source, dest, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
+    } 
+    else 
+    {
+        success = g_file_copy (source, dest, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
     }
-
-    fclose(source);
-    fclose(target);
-
-    return TRUE;
+    return success;
 }
 
 char*
diff --git a/src/common.h b/src/common.h
index b2c36c3f..cb0a3b5a 100644
--- a/src/common.h
+++ b/src/common.h
@@ -82,7 +82,7 @@ typedef enum {
 
 gboolean create_dir(char *name);
 gboolean mkdir_recursive(const char *dir);
-gboolean copy_file(const char *const src, const char *const target);
+gboolean copy_file(const char *const src, const char *const target, const gboolean overwrite_existing);
 char* str_replace(const char *string, const char *substr, const char *replacement);
 int str_contains(const char str[], int size, char ch);
 gboolean strtoi_range(char *str, int *saveptr, int min, int max, char **err_msg);
diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c
index 889bb91b..8c1bc2cf 100644
--- a/src/plugins/plugins.c
+++ b/src/plugins/plugins.c
@@ -184,13 +184,12 @@ plugins_install(const char *const plugin_name, const char *const filename, GStri
         return FALSE;
     }
 
-    gboolean result = copy_file(filename, target_path->str);
+    gboolean result = copy_file(filename, target_path->str, false);
     g_string_free(target_path, TRUE);
 
     if (result) {
         result = plugins_load(plugin_name);
     }
-
     return result;
 }