Upgrade of copy_file function in misc
authorTomas Mlcoch <tmlcoch@redhat.com>
Wed, 4 Apr 2012 08:36:50 +0000 (10:36 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Wed, 4 Apr 2012 08:36:50 +0000 (10:36 +0200)
src/misc.c

index 19ec93b..d2cbb16 100644 (file)
@@ -385,7 +385,7 @@ char *get_filename(const char *filepath)
 }
 
 
-int copy_file(const char *src, const char *dst)
+int copy_file(const char *src, const char *in_dst)
 {
     size_t readed;
     char buf[BUFFER_SIZE];
@@ -393,6 +393,17 @@ int copy_file(const char *src, const char *dst)
     FILE *orig;
     FILE *new;
 
+    if (!src || !in_dst) {
+        g_debug(MODULE"%s: File name cannot be NULL", __func__);
+        return CR_COPY_ERR;
+    }
+
+    // If destination is dir use filename from src
+    gchar dst = in_dst;
+    if (g_str_has_suffix(in_dst, "/")) {
+        dst = g_strconcat(in_dst, get_filename(src), NULL);
+    }
+
     if ((orig = fopen(src, "r")) == NULL) {
         g_debug(MODULE"%s: Cannot open source file %s (%s)", __func__, src, strerror(errno));
         return CR_COPY_ERR;
@@ -420,6 +431,10 @@ int copy_file(const char *src, const char *dst)
         }
     }
 
+    if (dst != in_dst) {
+        g_free(dst);
+    }
+
     fclose(new);
     fclose(orig);