summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2013-01-24 03:32:05 +0100
committerGravatar Nikias Bassen2013-01-24 03:32:05 +0100
commit1eff94c02a2f72debb73d2983724c193e42fd9d4 (patch)
tree5bfd2cb5d1dd30eea00857e900aa97969bff4eb6
parent22345f50309395859bbd3989694fb49b43e0a14a (diff)
downloadlibimobiledevice-1eff94c02a2f72debb73d2983724c193e42fd9d4.tar.gz
libimobiledevice-1eff94c02a2f72debb73d2983724c193e42fd9d4.tar.bz2
idevicebackup2: change several global variables to local ones
-rw-r--r--tools/idevicebackup2.c111
1 files changed, 59 insertions, 52 deletions
diff --git a/tools/idevicebackup2.c b/tools/idevicebackup2.c
index 50d74c0..0426d43 100644
--- a/tools/idevicebackup2.c
+++ b/tools/idevicebackup2.c
@@ -58,11 +58,6 @@
58#define CODE_ERROR_REMOTE 0x0b 58#define CODE_ERROR_REMOTE 0x0b
59#define CODE_FILE_DATA 0x0c 59#define CODE_FILE_DATA 0x0c
60 60
61static mobilebackup2_client_t mobilebackup2 = NULL;
62static lockdownd_client_t client = NULL;
63static afc_client_t afc = NULL;
64static idevice_t device = NULL;
65
66static int verbose = 1; 61static int verbose = 1;
67static int quit_flag = 0; 62static int quit_flag = 0;
68 63
@@ -121,7 +116,7 @@ static void free_dictionary(char **dictionary)
121 free(dictionary); 116 free(dictionary);
122} 117}
123 118
124static void mobilebackup_afc_get_file_contents(const char *filename, char **data, uint64_t *size) 119static void mobilebackup_afc_get_file_contents(afc_client_t afc, const char *filename, char **data, uint64_t *size)
125{ 120{
126 if (!afc || !data || !size) { 121 if (!afc || !data || !size) {
127 return; 122 return;
@@ -261,7 +256,7 @@ static char* format_size_for_display(uint64_t size)
261 return strdup(buf); 256 return strdup(buf);
262} 257}
263 258
264static plist_t mobilebackup_factory_info_plist_new(const char* udid) 259static plist_t mobilebackup_factory_info_plist_new(const char* udid, lockdownd_client_t lockdown, afc_client_t afc)
265{ 260{
266 /* gather data from lockdown */ 261 /* gather data from lockdown */
267 plist_t value_node = NULL; 262 plist_t value_node = NULL;
@@ -271,7 +266,7 @@ static plist_t mobilebackup_factory_info_plist_new(const char* udid)
271 plist_t ret = plist_new_dict(); 266 plist_t ret = plist_new_dict();
272 267
273 /* get basic device information in one go */ 268 /* get basic device information in one go */
274 lockdownd_get_value(client, NULL, NULL, &root_node); 269 lockdownd_get_value(lockdown, NULL, NULL, &root_node);
275 270
276 /* set fields we understand */ 271 /* set fields we understand */
277 value_node = plist_dict_get_item(root_node, "BuildVersion"); 272 value_node = plist_dict_get_item(root_node, "BuildVersion");
@@ -322,7 +317,7 @@ static plist_t mobilebackup_factory_info_plist_new(const char* udid)
322 317
323 char *data_buf = NULL; 318 char *data_buf = NULL;
324 uint64_t data_size = 0; 319 uint64_t data_size = 0;
325 mobilebackup_afc_get_file_contents("/Books/iBooksData2.plist", &data_buf, &data_size); 320 mobilebackup_afc_get_file_contents(afc, "/Books/iBooksData2.plist", &data_buf, &data_size);
326 if (data_buf) { 321 if (data_buf) {
327 plist_dict_insert_item(ret, "iBooks Data 2", plist_new_data(data_buf, data_size)); 322 plist_dict_insert_item(ret, "iBooks Data 2", plist_new_data(data_buf, data_size));
328 free(data_buf); 323 free(data_buf);
@@ -349,7 +344,7 @@ static plist_t mobilebackup_factory_info_plist_new(const char* udid)
349 char *fname = (char*)malloc(strlen("/iTunes_Control/iTunes/") + strlen(itunesfiles[i]) + 1); 344 char *fname = (char*)malloc(strlen("/iTunes_Control/iTunes/") + strlen(itunesfiles[i]) + 1);
350 strcpy(fname, "/iTunes_Control/iTunes/"); 345 strcpy(fname, "/iTunes_Control/iTunes/");
351 strcat(fname, itunesfiles[i]); 346 strcat(fname, itunesfiles[i]);
352 mobilebackup_afc_get_file_contents(fname, &data_buf, &data_size); 347 mobilebackup_afc_get_file_contents(afc, fname, &data_buf, &data_size);
353 free(fname); 348 free(fname);
354 if (data_buf) { 349 if (data_buf) {
355 plist_dict_insert_item(files, itunesfiles[i], plist_new_data(data_buf, data_size)); 350 plist_dict_insert_item(files, itunesfiles[i], plist_new_data(data_buf, data_size));
@@ -359,7 +354,7 @@ static plist_t mobilebackup_factory_info_plist_new(const char* udid)
359 plist_dict_insert_item(ret, "iTunes Files", files); 354 plist_dict_insert_item(ret, "iTunes Files", files);
360 355
361 plist_t itunes_settings = plist_new_dict(); 356 plist_t itunes_settings = plist_new_dict();
362 lockdownd_get_value(client, "com.apple.iTunes", NULL, &itunes_settings); 357 lockdownd_get_value(lockdown, "com.apple.iTunes", NULL, &itunes_settings);
363 plist_dict_insert_item(ret, "iTunes Settings", itunes_settings); 358 plist_dict_insert_item(ret, "iTunes Settings", itunes_settings);
364 359
365 plist_dict_insert_item(ret, "iTunes Version", plist_new_string("10.0.1")); 360 plist_dict_insert_item(ret, "iTunes Version", plist_new_string("10.0.1"));
@@ -482,18 +477,18 @@ static int mb2_status_check_snapshot_state(const char *path, const char *udid, c
482 return ret; 477 return ret;
483} 478}
484 479
485static void do_post_notification(const char *notification) 480static void do_post_notification(idevice_t device, const char *notification)
486{ 481{
487 uint16_t nport = 0; 482 uint16_t nport = 0;
488 np_client_t np; 483 np_client_t np;
489 484
490 if (!client) { 485 lockdownd_client_t lockdown = NULL;
491 if (lockdownd_client_new_with_handshake(device, &client, "idevicebackup") != LOCKDOWN_E_SUCCESS) { 486
492 return; 487 if (lockdownd_client_new_with_handshake(device, &lockdown, "idevicebackup") != LOCKDOWN_E_SUCCESS) {
493 } 488 return;
494 } 489 }
495 490
496 lockdownd_start_service(client, NP_SERVICE_NAME, &nport); 491 lockdownd_start_service(lockdown, NP_SERVICE_NAME, &nport);
497 if (nport) { 492 if (nport) {
498 np_client_new(device, nport, &np); 493 np_client_new(device, nport, &np);
499 if (np) { 494 if (np) {
@@ -613,7 +608,7 @@ static int win32err_to_errno(int err_value)
613} 608}
614#endif 609#endif
615 610
616static int mb2_handle_send_file(const char *backup_dir, const char *path, plist_t *errplist) 611static int mb2_handle_send_file(mobilebackup2_client_t mobilebackup2, const char *backup_dir, const char *path, plist_t *errplist)
617{ 612{
618 uint32_t nlen = 0; 613 uint32_t nlen = 0;
619 uint32_t pathlen = strlen(path); 614 uint32_t pathlen = strlen(path);
@@ -752,7 +747,7 @@ leave_proto_err:
752 return result; 747 return result;
753} 748}
754 749
755static void mb2_handle_send_files(plist_t message, const char *backup_dir) 750static void mb2_handle_send_files(mobilebackup2_client_t mobilebackup2, plist_t message, const char *backup_dir)
756{ 751{
757 uint32_t cnt; 752 uint32_t cnt;
758 uint32_t i = 0; 753 uint32_t i = 0;
@@ -775,7 +770,7 @@ static void mb2_handle_send_files(plist_t message, const char *backup_dir)
775 if (!str) 770 if (!str)
776 continue; 771 continue;
777 772
778 if (mb2_handle_send_file(backup_dir, str, &errplist) < 0) { 773 if (mb2_handle_send_file(mobilebackup2, backup_dir, str, &errplist) < 0) {
779 free(str); 774 free(str);
780 //printf("Error when sending file '%s' to device\n", str); 775 //printf("Error when sending file '%s' to device\n", str);
781 // TODO: perhaps we can continue, we've got a multi status response?! 776 // TODO: perhaps we can continue, we've got a multi status response?!
@@ -798,7 +793,7 @@ static void mb2_handle_send_files(plist_t message, const char *backup_dir)
798 } 793 }
799} 794}
800 795
801static int mb2_receive_filename(char** filename) 796static int mb2_receive_filename(mobilebackup2_client_t mobilebackup2, char** filename)
802{ 797{
803 uint32_t nlen = 0; 798 uint32_t nlen = 0;
804 uint32_t rlen = 0; 799 uint32_t rlen = 0;
@@ -844,7 +839,7 @@ static int mb2_receive_filename(char** filename)
844 return nlen; 839 return nlen;
845} 840}
846 841
847static int mb2_handle_receive_files(plist_t message, const char *backup_dir) 842static int mb2_handle_receive_files(mobilebackup2_client_t mobilebackup2, plist_t message, const char *backup_dir)
848{ 843{
849 uint64_t backup_real_size = 0; 844 uint64_t backup_real_size = 0;
850 uint64_t backup_total_size = 0; 845 uint64_t backup_total_size = 0;
@@ -877,12 +872,12 @@ static int mb2_handle_receive_files(plist_t message, const char *backup_dir)
877 if (quit_flag) 872 if (quit_flag)
878 break; 873 break;
879 874
880 nlen = mb2_receive_filename(&dname); 875 nlen = mb2_receive_filename(mobilebackup2, &dname);
881 if (nlen == 0) { 876 if (nlen == 0) {
882 break; 877 break;
883 } 878 }
884 879
885 nlen = mb2_receive_filename(&fname); 880 nlen = mb2_receive_filename(mobilebackup2, &fname);
886 if (!nlen) { 881 if (!nlen) {
887 break; 882 break;
888 } 883 }
@@ -1007,7 +1002,7 @@ static int mb2_handle_receive_files(plist_t message, const char *backup_dir)
1007 return file_count; 1002 return file_count;
1008} 1003}
1009 1004
1010static void mb2_handle_list_directory(plist_t message, const char *backup_dir) 1005static void mb2_handle_list_directory(mobilebackup2_client_t mobilebackup2, plist_t message, const char *backup_dir)
1011{ 1006{
1012 if (!message || (plist_get_node_type(message) != PLIST_ARRAY) || plist_array_get_size(message) < 2 || !backup_dir) return; 1007 if (!message || (plist_get_node_type(message) != PLIST_ARRAY) || plist_array_get_size(message) < 2 || !backup_dir) return;
1013 1008
@@ -1065,7 +1060,7 @@ static void mb2_handle_list_directory(plist_t message, const char *backup_dir)
1065 } 1060 }
1066} 1061}
1067 1062
1068static void mb2_handle_make_directory(plist_t message, const char *backup_dir) 1063static void mb2_handle_make_directory(mobilebackup2_client_t mobilebackup2, plist_t message, const char *backup_dir)
1069{ 1064{
1070 if (!message || (plist_get_node_type(message) != PLIST_ARRAY) || plist_array_get_size(message) < 2 || !backup_dir) return; 1065 if (!message || (plist_get_node_type(message) != PLIST_ARRAY) || plist_array_get_size(message) < 2 || !backup_dir) return;
1071 1066
@@ -1465,6 +1460,7 @@ int main(int argc, char *argv[])
1465 } 1460 }
1466 } 1461 }
1467 1462
1463 idevice_t device = NULL;
1468 if (udid) { 1464 if (udid) {
1469 ret = idevice_new(&device, udid); 1465 ret = idevice_new(&device, udid);
1470 if (ret != IDEVICE_E_SUCCESS) { 1466 if (ret != IDEVICE_E_SUCCESS) {
@@ -1534,14 +1530,15 @@ int main(int argc, char *argv[])
1534 } 1530 }
1535 } 1531 }
1536 1532
1537 if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &client, "idevicebackup")) { 1533 lockdownd_client_t lockdown = NULL;
1534 if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lockdown, "idevicebackup")) {
1538 idevice_free(device); 1535 idevice_free(device);
1539 return -1; 1536 return -1;
1540 } 1537 }
1541 1538
1542 /* start notification_proxy */ 1539 /* start notification_proxy */
1543 np_client_t np = NULL; 1540 np_client_t np = NULL;
1544 ret = lockdownd_start_service(client, NP_SERVICE_NAME, &port); 1541 ret = lockdownd_start_service(lockdown, NP_SERVICE_NAME, &port);
1545 if ((ret == LOCKDOWN_E_SUCCESS) && port) { 1542 if ((ret == LOCKDOWN_E_SUCCESS) && port) {
1546 np_client_new(device, port, &np); 1543 np_client_new(device, port, &np);
1547 np_set_notify_callback(np, notify_cb, NULL); 1544 np_set_notify_callback(np, notify_cb, NULL);
@@ -1557,19 +1554,20 @@ int main(int argc, char *argv[])
1557 printf("ERROR: Could not start service %s.\n", NP_SERVICE_NAME); 1554 printf("ERROR: Could not start service %s.\n", NP_SERVICE_NAME);
1558 } 1555 }
1559 1556
1560 afc = NULL; 1557 afc_client_t afc = NULL;
1561 if (cmd == CMD_BACKUP) { 1558 if (cmd == CMD_BACKUP) {
1562 /* start AFC, we need this for the lock file */ 1559 /* start AFC, we need this for the lock file */
1563 port = 0; 1560 port = 0;
1564 ret = lockdownd_start_service(client, "com.apple.afc", &port); 1561 ret = lockdownd_start_service(lockdown, "com.apple.afc", &port);
1565 if ((ret == LOCKDOWN_E_SUCCESS) && port) { 1562 if ((ret == LOCKDOWN_E_SUCCESS) && port) {
1566 afc_client_new(device, port, &afc); 1563 afc_client_new(device, port, &afc);
1567 } 1564 }
1568 } 1565 }
1569 1566
1570 /* start mobilebackup service and retrieve port */ 1567 /* start mobilebackup service and retrieve port */
1568 mobilebackup2_client_t mobilebackup2 = NULL;
1571 port = 0; 1569 port = 0;
1572 ret = lockdownd_start_service(client, MOBILEBACKUP2_SERVICE_NAME, &port); 1570 ret = lockdownd_start_service(lockdown, MOBILEBACKUP2_SERVICE_NAME, &port);
1573 if ((ret == LOCKDOWN_E_SUCCESS) && port) { 1571 if ((ret == LOCKDOWN_E_SUCCESS) && port) {
1574 PRINT_VERBOSE(1, "Started \"%s\" service on port %d.\n", MOBILEBACKUP2_SERVICE_NAME, port); 1572 PRINT_VERBOSE(1, "Started \"%s\" service on port %d.\n", MOBILEBACKUP2_SERVICE_NAME, port);
1575 mobilebackup2_client_new(device, port, &mobilebackup2); 1573 mobilebackup2_client_new(device, port, &mobilebackup2);
@@ -1613,16 +1611,16 @@ int main(int argc, char *argv[])
1613 1611
1614 uint64_t lockfile = 0; 1612 uint64_t lockfile = 0;
1615 if (cmd == CMD_BACKUP) { 1613 if (cmd == CMD_BACKUP) {
1616 do_post_notification(NP_SYNC_WILL_START); 1614 do_post_notification(device, NP_SYNC_WILL_START);
1617 afc_file_open(afc, "/com.apple.itunes.lock_sync", AFC_FOPEN_RW, &lockfile); 1615 afc_file_open(afc, "/com.apple.itunes.lock_sync", AFC_FOPEN_RW, &lockfile);
1618 } 1616 }
1619 if (lockfile) { 1617 if (lockfile) {
1620 afc_error_t aerr; 1618 afc_error_t aerr;
1621 do_post_notification(NP_SYNC_LOCK_REQUEST); 1619 do_post_notification(device, NP_SYNC_LOCK_REQUEST);
1622 for (i = 0; i < LOCK_ATTEMPTS; i++) { 1620 for (i = 0; i < LOCK_ATTEMPTS; i++) {
1623 aerr = afc_file_lock(afc, lockfile, AFC_LOCK_EX); 1621 aerr = afc_file_lock(afc, lockfile, AFC_LOCK_EX);
1624 if (aerr == AFC_E_SUCCESS) { 1622 if (aerr == AFC_E_SUCCESS) {
1625 do_post_notification(NP_SYNC_DID_START); 1623 do_post_notification(device, NP_SYNC_DID_START);
1626 break; 1624 break;
1627 } else if (aerr == AFC_E_OP_WOULD_BLOCK) { 1625 } else if (aerr == AFC_E_OP_WOULD_BLOCK) {
1628 usleep(LOCK_WAIT); 1626 usleep(LOCK_WAIT);
@@ -1643,7 +1641,7 @@ int main(int argc, char *argv[])
1643 } 1641 }
1644 uint8_t willEncrypt = 0; 1642 uint8_t willEncrypt = 0;
1645 node_tmp = NULL; 1643 node_tmp = NULL;
1646 lockdownd_get_value(client, "com.apple.mobile.backup", "WillEncrypt", &node_tmp); 1644 lockdownd_get_value(lockdown, "com.apple.mobile.backup", "WillEncrypt", &node_tmp);
1647 if (node_tmp) { 1645 if (node_tmp) {
1648 if (plist_get_node_type(node_tmp) == PLIST_BOOLEAN) { 1646 if (plist_get_node_type(node_tmp) == PLIST_BOOLEAN) {
1649 plist_get_bool_val(node_tmp, &willEncrypt); 1647 plist_get_bool_val(node_tmp, &willEncrypt);
@@ -1683,7 +1681,7 @@ checkpoint:
1683 plist_free(info_plist); 1681 plist_free(info_plist);
1684 info_plist = NULL; 1682 info_plist = NULL;
1685 } 1683 }
1686 info_plist = mobilebackup_factory_info_plist_new(udid); 1684 info_plist = mobilebackup_factory_info_plist_new(udid, lockdown, afc);
1687 remove(info_path); 1685 remove(info_path);
1688 plist_write_to_filename(info_plist, info_path, PLIST_FORMAT_XML); 1686 plist_write_to_filename(info_plist, info_path, PLIST_FORMAT_XML);
1689 free(info_path); 1687 free(info_path);
@@ -1859,9 +1857,9 @@ checkpoint:
1859 } 1857 }
1860 1858
1861 /* close down the lockdown connection as it is no longer needed */ 1859 /* close down the lockdown connection as it is no longer needed */
1862 if (client) { 1860 if (lockdown) {
1863 lockdownd_client_free(client); 1861 lockdownd_client_free(lockdown);
1864 client = NULL; 1862 lockdown = NULL;
1865 } 1863 }
1866 1864
1867 if (cmd != CMD_LEAVE) { 1865 if (cmd != CMD_LEAVE) {
@@ -1890,11 +1888,11 @@ checkpoint:
1890 if (!strcmp(dlmsg, "DLMessageDownloadFiles")) { 1888 if (!strcmp(dlmsg, "DLMessageDownloadFiles")) {
1891 /* device wants to download files from the computer */ 1889 /* device wants to download files from the computer */
1892 mb2_set_overall_progress_from_message(message, dlmsg); 1890 mb2_set_overall_progress_from_message(message, dlmsg);
1893 mb2_handle_send_files(message, backup_directory); 1891 mb2_handle_send_files(mobilebackup2, message, backup_directory);
1894 } else if (!strcmp(dlmsg, "DLMessageUploadFiles")) { 1892 } else if (!strcmp(dlmsg, "DLMessageUploadFiles")) {
1895 /* device wants to send files to the computer */ 1893 /* device wants to send files to the computer */
1896 mb2_set_overall_progress_from_message(message, dlmsg); 1894 mb2_set_overall_progress_from_message(message, dlmsg);
1897 file_count += mb2_handle_receive_files(message, backup_directory); 1895 file_count += mb2_handle_receive_files(mobilebackup2, message, backup_directory);
1898 } else if (!strcmp(dlmsg, "DLMessageGetFreeDiskSpace")) { 1896 } else if (!strcmp(dlmsg, "DLMessageGetFreeDiskSpace")) {
1899 /* device wants to know how much disk space is available on the computer */ 1897 /* device wants to know how much disk space is available on the computer */
1900 uint64_t freespace = 0; 1898 uint64_t freespace = 0;
@@ -1914,10 +1912,10 @@ checkpoint:
1914 mobilebackup2_send_status_response(mobilebackup2, res, NULL, plist_new_uint(freespace)); 1912 mobilebackup2_send_status_response(mobilebackup2, res, NULL, plist_new_uint(freespace));
1915 } else if (!strcmp(dlmsg, "DLContentsOfDirectory")) { 1913 } else if (!strcmp(dlmsg, "DLContentsOfDirectory")) {
1916 /* list directory contents */ 1914 /* list directory contents */
1917 mb2_handle_list_directory(message, backup_directory); 1915 mb2_handle_list_directory(mobilebackup2, message, backup_directory);
1918 } else if (!strcmp(dlmsg, "DLMessageCreateDirectory")) { 1916 } else if (!strcmp(dlmsg, "DLMessageCreateDirectory")) {
1919 /* make a directory */ 1917 /* make a directory */
1920 mb2_handle_make_directory(message, backup_directory); 1918 mb2_handle_make_directory(mobilebackup2, message, backup_directory);
1921 } else if (!strcmp(dlmsg, "DLMessageMoveFiles") || !strcmp(dlmsg, "DLMessageMoveItems")) { 1919 } else if (!strcmp(dlmsg, "DLMessageMoveFiles") || !strcmp(dlmsg, "DLMessageMoveItems")) {
1922 /* perform a series of rename operations */ 1920 /* perform a series of rename operations */
1923 mb2_set_overall_progress_from_message(message, dlmsg); 1921 mb2_set_overall_progress_from_message(message, dlmsg);
@@ -2204,35 +2202,44 @@ files_out:
2204 afc_file_close(afc, lockfile); 2202 afc_file_close(afc, lockfile);
2205 lockfile = 0; 2203 lockfile = 0;
2206 if (cmd == CMD_BACKUP) 2204 if (cmd == CMD_BACKUP)
2207 do_post_notification(NP_SYNC_DID_FINISH); 2205 do_post_notification(device, NP_SYNC_DID_FINISH);
2208 } 2206 }
2209 } else { 2207 } else {
2210 printf("ERROR: Could not start service %s.\n", MOBILEBACKUP2_SERVICE_NAME); 2208 printf("ERROR: Could not start service %s.\n", MOBILEBACKUP2_SERVICE_NAME);
2211 lockdownd_client_free(client); 2209 lockdownd_client_free(lockdown);
2212 client = NULL; 2210 lockdown = NULL;
2213 } 2211 }
2214 2212
2215 if (client) { 2213 if (lockdown) {
2216 lockdownd_client_free(client); 2214 lockdownd_client_free(lockdown);
2217 client = NULL; 2215 lockdown = NULL;
2218 } 2216 }
2219 2217
2220 if (mobilebackup2) 2218 if (mobilebackup2) {
2221 mobilebackup2_client_free(mobilebackup2); 2219 mobilebackup2_client_free(mobilebackup2);
2220 mobilebackup2 = NULL;
2221 }
2222 2222
2223 if (afc) 2223 if (afc) {
2224 afc_client_free(afc); 2224 afc_client_free(afc);
2225 afc = NULL;
2226 }
2225 2227
2226 if (np) 2228 if (np) {
2227 np_client_free(np); 2229 np_client_free(np);
2230 np = NULL;
2231 }
2228 2232
2229 idevice_free(device); 2233 idevice_free(device);
2234 device = NULL;
2230 2235
2231 if (udid) { 2236 if (udid) {
2232 free(udid); 2237 free(udid);
2238 udid = NULL;
2233 } 2239 }
2234 if (source_udid) { 2240 if (source_udid) {
2235 free(source_udid); 2241 free(source_udid);
2242 source_udid = NULL;
2236 } 2243 }
2237 2244
2238 return result_code; 2245 return result_code;