about summary refs log tree commit diff stats
path: root/src/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c76
1 files changed, 11 insertions, 65 deletions
diff --git a/src/common.c b/src/common.c
index a3893c06..3e1b51b5 100644
--- a/src/common.c
+++ b/src/common.c
@@ -1,7 +1,7 @@
 /*
  * common.c
  *
- * Copyright (C) 2012 - 2018 James Booth <boothj5@gmail.com>
+ * Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
  *
  * This file is part of Profanity.
  *
@@ -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>
@@ -55,7 +56,6 @@
 
 #include "log.h"
 #include "common.h"
-#include "tools/p_sha1.h"
 
 struct curl_data_t
 {
@@ -63,8 +63,6 @@ struct curl_data_t
     size_t size;
 };
 
-static unsigned long unique_id = 0;
-
 static size_t _data_callback(void *ptr, size_t size, size_t nmemb, void *data);
 
 gboolean
@@ -107,28 +105,16 @@ 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);
-    }
-
-    fclose(source);
-    fclose(target);
-
-    return TRUE;
+    GFile *source = g_file_new_for_path(sourcepath);
+    GFile *dest = g_file_new_for_path(targetpath);
+    GError *error = NULL;
+    GFileCopyFlags flags = overwrite_existing ? G_FILE_COPY_OVERWRITE : G_FILE_COPY_NONE;
+    gboolean success = g_file_copy (source, dest, flags, NULL, NULL, NULL, &error);
+    g_object_unref(source);
+    g_object_unref(dest);
+    return success;
 }
 
 char*
@@ -332,46 +318,6 @@ release_is_new(char *found_version)
     }
 }
 
-char*
-create_unique_id(char *prefix)
-{
-    char *result = NULL;
-    GString *result_str = g_string_new("");
-
-    unique_id++;
-    if (prefix) {
-        g_string_printf(result_str, "prof_%s_%lu", prefix, unique_id);
-    } else {
-        g_string_printf(result_str, "prof_%lu", unique_id);
-    }
-    result = result_str->str;
-    g_string_free(result_str, FALSE);
-
-    return result;
-}
-
-void
-reset_unique_id(void)
-{
-    unique_id = 0;
-}
-
-char*
-p_sha1_hash(char *str)
-{
-    P_SHA1_CTX ctx;
-    uint8_t digest[20];
-    uint8_t *input = (uint8_t*)malloc(strlen(str) + 1);
-    memcpy(input, str, strlen(str) + 1);
-
-    P_SHA1_Init(&ctx);
-    P_SHA1_Update(&ctx, input, strlen(str));
-    P_SHA1_Final(&ctx, digest);
-
-    free(input);
-    return g_base64_encode(digest, sizeof(digest));
-}
-
 static size_t
 _data_callback(void *ptr, size_t size, size_t nmemb, void *data)
 {