use gio functions for file copy

This commit is contained in:
Philip Flohr
2018-05-07 19:41:47 +02:00
committed by Dmitry Podgorny
parent 2795dc487c
commit e4ddced420
3 changed files with 17 additions and 23 deletions

View File

@@ -46,6 +46,7 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <curl/easy.h> #include <curl/easy.h>
#include <glib.h> #include <glib.h>
#include <gio/gio.h>
#ifdef HAVE_NCURSESW_NCURSES_H #ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h> #include <ncursesw/ncurses.h>
@@ -105,28 +106,22 @@ mkdir_recursive(const char *dir)
} }
gboolean 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; GFile *source = g_file_new_for_path(sourcepath);
FILE *source = fopen(sourcepath, "rb"); GFile *dest = g_file_new_for_path(targetpath);
if (source == NULL) { GError *error = NULL;
return FALSE; gboolean success = false;
if (overwrite_existing)
{
success = g_file_copy (source, dest, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
} }
else
FILE *target = fopen(targetpath, "wb"); {
if (target == NULL) { success = g_file_copy (source, dest, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
fclose(source);
return FALSE;
} }
return success;
while((ch = fgetc(source)) != EOF) {
fputc(ch, target);
}
fclose(source);
fclose(target);
return TRUE;
} }
char* char*

View File

@@ -82,7 +82,7 @@ typedef enum {
gboolean create_dir(char *name); gboolean create_dir(char *name);
gboolean mkdir_recursive(const char *dir); 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); char* str_replace(const char *string, const char *substr, const char *replacement);
int str_contains(const char str[], int size, char ch); int str_contains(const char str[], int size, char ch);
gboolean strtoi_range(char *str, int *saveptr, int min, int max, char **err_msg); gboolean strtoi_range(char *str, int *saveptr, int min, int max, char **err_msg);

View File

@@ -184,13 +184,12 @@ plugins_install(const char *const plugin_name, const char *const filename, GStri
return FALSE; 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); g_string_free(target_path, TRUE);
if (result) { if (result) {
result = plugins_load(plugin_name); result = plugins_load(plugin_name);
} }
return result; return result;
} }