summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/idevicebackup4.c98
1 files changed, 96 insertions, 2 deletions
diff --git a/tools/idevicebackup4.c b/tools/idevicebackup4.c
index 50bfc49..b34064e 100644
--- a/tools/idevicebackup4.c
+++ b/tools/idevicebackup4.c
@@ -967,6 +967,88 @@ static void mb2_handle_make_directory(plist_t message, const char *backup_dir)
967 } 967 }
968} 968}
969 969
970static void mb2_copy_file_by_path(const gchar *src, const gchar *dst)
971{
972 FILE *from, *to;
973 char ch;
974
975 /* open source file */
976 if ((from = fopen(src, "rb")) == NULL) {
977 printf("Cannot open source path '%s'.\n", src);
978 return;
979 }
980
981 /* open destination file */
982 if ((to = fopen(dst, "wb")) == NULL) {
983 printf("Cannot open destination file '%s'.\n", dst);
984 return;
985 }
986
987 /* copy the file */
988 while(!feof(from)) {
989 ch = fgetc(from);
990 if(ferror(from)) {
991 printf("Error reading source file.\n");
992 break;
993 }
994 if(!feof(from))
995 fputc(ch, to);
996
997 if(ferror(to)) {
998 printf("Error writing destination file.\n");
999 break;
1000 }
1001 }
1002
1003 if(fclose(from) == EOF) {
1004 printf("Error closing source file.\n");
1005 }
1006
1007 if(fclose(to) == EOF) {
1008 printf("Error closing destination file.\n");
1009 }
1010}
1011
1012static void mb2_copy_directory_by_path(const gchar *src, const gchar *dst)
1013{
1014 if (!src || !dst) {
1015 return;
1016 }
1017
1018 /* if src does not exist */
1019 if (!g_file_test(src, G_FILE_TEST_EXISTS)) {
1020 printf("ERROR: Source directory does not exist '%s': %s (%d)\n", src, strerror(errno), errno);
1021 return;
1022 }
1023
1024 /* if dst directory does not exist */
1025 if (!g_file_test(dst, G_FILE_TEST_IS_DIR)) {
1026 /* create it */
1027 if (g_mkdir_with_parents(dst, 0755) < 0) {
1028 printf("ERROR: Unable to create destination directory '%s': %s (%d)\n", dst, strerror(errno), errno);
1029 return;
1030 }
1031 }
1032
1033 /* loop over src directory contents */
1034 GDir *cur_dir = g_dir_open(src, 0, NULL);
1035 if (cur_dir) {
1036 gchar *dir_file;
1037 while ((dir_file = (gchar *)g_dir_read_name(cur_dir))) {
1038 gchar *srcpath = g_build_filename(src, dir_file, NULL);
1039 gchar *dstpath = g_build_filename(dst, dir_file, NULL);
1040 if (srcpath && dstpath) {
1041 /* copy file */
1042 mb2_copy_file_by_path(srcpath, dstpath);
1043
1044 g_free(srcpath);
1045 g_free(dstpath);
1046 }
1047 }
1048 g_dir_close(cur_dir);
1049 }
1050}
1051
970/** 1052/**
971 * signal handler function for cleaning up properly 1053 * signal handler function for cleaning up properly
972 */ 1054 */
@@ -1450,8 +1532,20 @@ checkpoint:
1450 plist_get_string_val(srcpath, &src); 1532 plist_get_string_val(srcpath, &src);
1451 plist_get_string_val(dstpath, &dst); 1533 plist_get_string_val(dstpath, &dst);
1452 if (src && dst) { 1534 if (src && dst) {
1453 printf("Copying '%s' to '%s', please wait (TODO: implemented)\n", src, dst); 1535 gchar *oldpath = g_build_path(G_DIR_SEPARATOR_S, backup_directory, src, NULL);
1454 // FIXME: implement 1536 gchar *newpath = g_build_path(G_DIR_SEPARATOR_S, backup_directory, dst, NULL);
1537
1538 PRINT_VERBOSE(1, "Copying '%s' to '%s'\n", src, dst);
1539
1540 /* check that src exists */
1541 if (g_file_test(oldpath, G_FILE_TEST_IS_DIR)) {
1542 mb2_copy_directory_by_path(oldpath, newpath);
1543 } else if (g_file_test(oldpath, G_FILE_TEST_IS_REGULAR)) {
1544 mb2_copy_file_by_path(oldpath, newpath);
1545 }
1546
1547 g_free(newpath);
1548 g_free(oldpath);
1455 } 1549 }
1456 g_free(src); 1550 g_free(src);
1457 g_free(dst); 1551 g_free(dst);