+static int copy_file(const char *srcpath, const char *destpath) {
+ FILE *in, *out;
+ char *buf;
+ size_t len;
+
+ if (!srcpath || !destpath)
+ {
+ DEBUG_ERROR("copyfile is failed. A given param is NULL ... srcpath[%s], destpath[%s]\n", srcpath, destpath);
+ goto FAIL;
+ }
+
+ if (!strcmp(srcpath, destpath))
+ {
+ DEBUG_ERROR("copyfile is failed. The both path are same ... srcpath[%s], destpath[%s]\n", srcpath, destpath);
+ goto FAIL;
+ }
+
+ if ((in = fopen(srcpath, "rb")) == NULL)
+ {
+ DEBUG_ERROR("copyfile is failed. The srcpath[%s] is not opened with read permission. fopen(): %s\n", srcpath, strerror(errno));
+ goto FAIL;
+ }
+
+ if ((out = fopen(destpath, "wb")) == NULL)
+ {
+ DEBUG_ERROR("copyfile is failed. The destpath[%s] is not opened with write permission. fopen(): %s\n", destpath, strerror(errno));
+ goto FAIL;
+ }
+
+ if ((buf = (char *) malloc(COPY_BUF_SIZE)) == NULL)
+ {
+ DEBUG_ERROR("copyfile is failed. Memory allocation for copy buffer is failed. Request size [%d]. malloc(): %s\n", COPY_BUF_SIZE, strerror(errno));
+ goto FAIL;
+ }
+
+ while ((len = fread(buf, sizeof(char), sizeof(buf), in)) != 0)
+ {
+ if (fwrite(buf, sizeof(char), len, out) == 0)
+ {
+ /* Reads "len" string. But, it fails to write file.
+ We need to remove wrong result. */
+ DEBUG_ERROR("copyfile is failed. fwrite fails to write. fwrite(): %s\n", strerror(errno));
+
+ fclose(out);
+ out = NULL;
+
+ if (remove(destpath) == -1)
+ DEBUG_ERROR("removing copied file with error is failed. remove(): %s\n", strerror(errno));
+
+ goto FAIL;
+ }
+ }
+
+ /* Copy file DONE! */
+ if (in) fclose(in);
+ if (out) fclose(out);
+ if (buf) free(buf);
+ return 0;
+
+FAIL:
+ if (in) fclose(in);
+ if (out) fclose(out);
+ if (buf) free(buf);
+ return -1;
+}
+
+static int move_path(const char *srcpath, const char *destpath)