diff options
| -rw-r--r-- | tools/idevicebackup2.c | 65 |
1 files changed, 62 insertions, 3 deletions
diff --git a/tools/idevicebackup2.c b/tools/idevicebackup2.c index f7ea53a..e10846d 100644 --- a/tools/idevicebackup2.c +++ b/tools/idevicebackup2.c | |||
| @@ -1393,6 +1393,10 @@ int main(int argc, char *argv[]) | |||
| 1393 | plist_t info_plist = NULL; | 1393 | plist_t info_plist = NULL; |
| 1394 | plist_t opts = NULL; | 1394 | plist_t opts = NULL; |
| 1395 | mobilebackup2_error_t err; | 1395 | mobilebackup2_error_t err; |
| 1396 | uint64_t restore_applications_file = 0; | ||
| 1397 | plist_t applications_plist = NULL; | ||
| 1398 | char * applications_plist_xml = NULL; | ||
| 1399 | uint32_t applications_plist_xml_length = 0; | ||
| 1396 | 1400 | ||
| 1397 | /* we need to exit cleanly on running backups and restores or we cause havok */ | 1401 | /* we need to exit cleanly on running backups and restores or we cause havok */ |
| 1398 | signal(SIGINT, clean_exit); | 1402 | signal(SIGINT, clean_exit); |
| @@ -1709,7 +1713,7 @@ int main(int argc, char *argv[]) | |||
| 1709 | } | 1713 | } |
| 1710 | 1714 | ||
| 1711 | afc_client_t afc = NULL; | 1715 | afc_client_t afc = NULL; |
| 1712 | if (cmd == CMD_BACKUP) { | 1716 | if (cmd == CMD_BACKUP || cmd == CMD_RESTORE) { |
| 1713 | /* start AFC, we need this for the lock file */ | 1717 | /* start AFC, we need this for the lock file */ |
| 1714 | service->port = 0; | 1718 | service->port = 0; |
| 1715 | service->ssl_enabled = 0; | 1719 | service->ssl_enabled = 0; |
| @@ -1776,7 +1780,7 @@ int main(int argc, char *argv[]) | |||
| 1776 | } | 1780 | } |
| 1777 | 1781 | ||
| 1778 | uint64_t lockfile = 0; | 1782 | uint64_t lockfile = 0; |
| 1779 | if (cmd == CMD_BACKUP) { | 1783 | if (cmd == CMD_BACKUP || cmd == CMD_RESTORE) { |
| 1780 | do_post_notification(device, NP_SYNC_WILL_START); | 1784 | do_post_notification(device, NP_SYNC_WILL_START); |
| 1781 | afc_file_open(afc, "/com.apple.itunes.lock_sync", AFC_FOPEN_RW, &lockfile); | 1785 | afc_file_open(afc, "/com.apple.itunes.lock_sync", AFC_FOPEN_RW, &lockfile); |
| 1782 | } | 1786 | } |
| @@ -1923,6 +1927,51 @@ checkpoint: | |||
| 1923 | } | 1927 | } |
| 1924 | PRINT_VERBOSE(1, "Backup password: %s\n", (backup_password == NULL ? "No":"Yes")); | 1928 | PRINT_VERBOSE(1, "Backup password: %s\n", (backup_password == NULL ? "No":"Yes")); |
| 1925 | 1929 | ||
| 1930 | /* Write /iTunesRestore/RestoreApplications.plist so that the device will start | ||
| 1931 | * restoring applications once the rest of the restore process is finished */ | ||
| 1932 | applications_plist = plist_dict_get_item(info_plist, "Applications"); | ||
| 1933 | if (applications_plist) { | ||
| 1934 | plist_to_xml(applications_plist, &applications_plist_xml, &applications_plist_xml_length); | ||
| 1935 | plist_free(applications_plist); | ||
| 1936 | } | ||
| 1937 | if (!applications_plist_xml) { | ||
| 1938 | printf("Error preparing RestoreApplications.plist\n"); | ||
| 1939 | cmd = CMD_LEAVE; | ||
| 1940 | break; | ||
| 1941 | } | ||
| 1942 | |||
| 1943 | afc_error_t afc_err = 0; | ||
| 1944 | afc_err = afc_make_directory(afc, "/iTunesRestore"); | ||
| 1945 | if (afc_err != AFC_E_SUCCESS) { | ||
| 1946 | printf("Error creating directory /iTunesRestore, error code %d\n", afc_err); | ||
| 1947 | cmd = CMD_LEAVE; | ||
| 1948 | break; | ||
| 1949 | } | ||
| 1950 | |||
| 1951 | afc_err = afc_file_open(afc, "/iTunesRestore/RestoreApplications.plist", AFC_FOPEN_WR, &restore_applications_file); | ||
| 1952 | if (afc_err != AFC_E_SUCCESS || !restore_applications_file) { | ||
| 1953 | printf("Error creating /iTunesRestore/RestoreApplications.plist, error code %d\n", afc_err); | ||
| 1954 | cmd = CMD_LEAVE; | ||
| 1955 | break; | ||
| 1956 | } | ||
| 1957 | |||
| 1958 | uint32_t bytes_written = 0; | ||
| 1959 | afc_err = afc_file_write(afc, restore_applications_file, applications_plist_xml, applications_plist_xml_length, &bytes_written); | ||
| 1960 | if (afc_err != AFC_E_SUCCESS || bytes_written != applications_plist_xml_length) { | ||
| 1961 | printf("Error writing /iTunesRestore/RestoreApplications.plist, error code %d, wrote %u of %u bytes\n", afc_err, bytes_written, applications_plist_xml_length); | ||
| 1962 | cmd = CMD_LEAVE; | ||
| 1963 | break; | ||
| 1964 | } | ||
| 1965 | |||
| 1966 | afc_err = afc_file_close(afc, restore_applications_file); | ||
| 1967 | restore_applications_file = 0; | ||
| 1968 | if (afc_err != AFC_E_SUCCESS) { | ||
| 1969 | cmd = CMD_LEAVE; | ||
| 1970 | break; | ||
| 1971 | } | ||
| 1972 | printf("Wrote RestoreApplications.plist\n"); | ||
| 1973 | |||
| 1974 | /* Start restore */ | ||
| 1926 | err = mobilebackup2_send_request(mobilebackup2, "Restore", udid, source_udid, opts); | 1975 | err = mobilebackup2_send_request(mobilebackup2, "Restore", udid, source_udid, opts); |
| 1927 | plist_free(opts); | 1976 | plist_free(opts); |
| 1928 | if (err != MOBILEBACKUP2_E_SUCCESS) { | 1977 | if (err != MOBILEBACKUP2_E_SUCCESS) { |
| @@ -2392,7 +2441,7 @@ files_out: | |||
| 2392 | afc_file_lock(afc, lockfile, AFC_LOCK_UN); | 2441 | afc_file_lock(afc, lockfile, AFC_LOCK_UN); |
| 2393 | afc_file_close(afc, lockfile); | 2442 | afc_file_close(afc, lockfile); |
| 2394 | lockfile = 0; | 2443 | lockfile = 0; |
| 2395 | if (cmd == CMD_BACKUP) | 2444 | if (cmd == CMD_BACKUP || cmd == CMD_RESTORE) |
| 2396 | do_post_notification(device, NP_SYNC_DID_FINISH); | 2445 | do_post_notification(device, NP_SYNC_DID_FINISH); |
| 2397 | } | 2446 | } |
| 2398 | } else { | 2447 | } else { |
| @@ -2411,6 +2460,11 @@ files_out: | |||
| 2411 | mobilebackup2 = NULL; | 2460 | mobilebackup2 = NULL; |
| 2412 | } | 2461 | } |
| 2413 | 2462 | ||
| 2463 | if (restore_applications_file) { | ||
| 2464 | afc_file_close(afc, restore_applications_file); | ||
| 2465 | restore_applications_file = 0; | ||
| 2466 | } | ||
| 2467 | |||
| 2414 | if (afc) { | 2468 | if (afc) { |
| 2415 | afc_client_free(afc); | 2469 | afc_client_free(afc); |
| 2416 | afc = NULL; | 2470 | afc = NULL; |
| @@ -2421,6 +2475,11 @@ files_out: | |||
| 2421 | np = NULL; | 2475 | np = NULL; |
| 2422 | } | 2476 | } |
| 2423 | 2477 | ||
| 2478 | if (applications_plist_xml) { | ||
| 2479 | free(applications_plist_xml); | ||
| 2480 | applications_plist_xml = NULL; | ||
| 2481 | } | ||
| 2482 | |||
| 2424 | idevice_free(device); | 2483 | idevice_free(device); |
| 2425 | device = NULL; | 2484 | device = NULL; |
| 2426 | 2485 | ||
