diff options
| author | 2014-08-06 03:12:12 +0200 | |
|---|---|---|
| committer | 2014-08-06 03:12:12 +0200 | |
| commit | 2344ec753ea58f93f1a8b889cc564d72bf084964 (patch) | |
| tree | 5a9a9c055c7dd21ca91395a2b4563cd6a7621ffe /tools/idevicebackup2.c | |
| parent | 99d83b9017fb46eaeb9d7682fe263fced31c39a3 (diff) | |
| download | libimobiledevice-2344ec753ea58f93f1a8b889cc564d72bf084964.tar.gz libimobiledevice-2344ec753ea58f93f1a8b889cc564d72bf084964.tar.bz2 | |
idevicebackup2/idevicebackup: Remove code duplication and use common code for reading/writing plist files
Diffstat (limited to 'tools/idevicebackup2.c')
| -rw-r--r-- | tools/idevicebackup2.c | 93 |
1 files changed, 1 insertions, 92 deletions
diff --git a/tools/idevicebackup2.c b/tools/idevicebackup2.c index 35d3ccb..2da4971 100644 --- a/tools/idevicebackup2.c +++ b/tools/idevicebackup2.c | |||
| @@ -36,6 +36,7 @@ | |||
| 36 | #include <libimobiledevice/mobilebackup2.h> | 36 | #include <libimobiledevice/mobilebackup2.h> |
| 37 | #include <libimobiledevice/notification_proxy.h> | 37 | #include <libimobiledevice/notification_proxy.h> |
| 38 | #include <libimobiledevice/afc.h> | 38 | #include <libimobiledevice/afc.h> |
| 39 | #include "common/utils.h" | ||
| 39 | 40 | ||
| 40 | #include <endianness.h> | 41 | #include <endianness.h> |
| 41 | 42 | ||
| @@ -73,11 +74,6 @@ enum cmd_mode { | |||
| 73 | CMD_CLOUD | 74 | CMD_CLOUD |
| 74 | }; | 75 | }; |
| 75 | 76 | ||
| 76 | enum plist_format_t { | ||
| 77 | PLIST_FORMAT_XML, | ||
| 78 | PLIST_FORMAT_BINARY | ||
| 79 | }; | ||
| 80 | |||
| 81 | enum cmd_flags { | 77 | enum cmd_flags { |
| 82 | CMD_FLAG_RESTORE_SYSTEM_FILES = (1 << 1), | 78 | CMD_FLAG_RESTORE_SYSTEM_FILES = (1 << 1), |
| 83 | CMD_FLAG_RESTORE_REBOOT = (1 << 2), | 79 | CMD_FLAG_RESTORE_REBOOT = (1 << 2), |
| @@ -357,93 +353,6 @@ static plist_t mobilebackup_factory_info_plist_new(const char* udid, lockdownd_c | |||
| 357 | return ret; | 353 | return ret; |
| 358 | } | 354 | } |
| 359 | 355 | ||
| 360 | static void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length) | ||
| 361 | { | ||
| 362 | FILE *f; | ||
| 363 | uint64_t size; | ||
| 364 | |||
| 365 | *length = 0; | ||
| 366 | |||
| 367 | f = fopen(filename, "rb"); | ||
| 368 | if (!f) { | ||
| 369 | return; | ||
| 370 | } | ||
| 371 | |||
| 372 | fseek(f, 0, SEEK_END); | ||
| 373 | size = ftell(f); | ||
| 374 | rewind(f); | ||
| 375 | |||
| 376 | if (size == 0) { | ||
| 377 | return; | ||
| 378 | } | ||
| 379 | |||
| 380 | *buffer = (char*)malloc(sizeof(char)*size); | ||
| 381 | fread(*buffer, sizeof(char), size, f); | ||
| 382 | fclose(f); | ||
| 383 | |||
| 384 | *length = size; | ||
| 385 | } | ||
| 386 | |||
| 387 | static void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length) | ||
| 388 | { | ||
| 389 | FILE *f; | ||
| 390 | |||
| 391 | f = fopen(filename, "ab"); | ||
| 392 | if (!f) | ||
| 393 | f = fopen(filename, "wb"); | ||
| 394 | if (f) { | ||
| 395 | fwrite(buffer, sizeof(char), length, f); | ||
| 396 | fclose(f); | ||
| 397 | } | ||
| 398 | } | ||
| 399 | |||
| 400 | static int plist_read_from_filename(plist_t *plist, const char *filename) | ||
| 401 | { | ||
| 402 | char *buffer = NULL; | ||
| 403 | uint64_t length; | ||
| 404 | |||
| 405 | if (!filename) | ||
| 406 | return 0; | ||
| 407 | |||
| 408 | buffer_read_from_filename(filename, &buffer, &length); | ||
| 409 | |||
| 410 | if (!buffer) { | ||
| 411 | return 0; | ||
| 412 | } | ||
| 413 | |||
| 414 | if ((length > 8) && (memcmp(buffer, "bplist00", 8) == 0)) { | ||
| 415 | plist_from_bin(buffer, length, plist); | ||
| 416 | } else { | ||
| 417 | plist_from_xml(buffer, length, plist); | ||
| 418 | } | ||
| 419 | |||
| 420 | free(buffer); | ||
| 421 | |||
| 422 | return 1; | ||
| 423 | } | ||
| 424 | |||
| 425 | static int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format) | ||
| 426 | { | ||
| 427 | char *buffer = NULL; | ||
| 428 | uint32_t length; | ||
| 429 | |||
| 430 | if (!plist || !filename) | ||
| 431 | return 0; | ||
| 432 | |||
| 433 | if (format == PLIST_FORMAT_XML) | ||
| 434 | plist_to_xml(plist, &buffer, &length); | ||
| 435 | else if (format == PLIST_FORMAT_BINARY) | ||
| 436 | plist_to_bin(plist, &buffer, &length); | ||
| 437 | else | ||
| 438 | return 0; | ||
| 439 | |||
| 440 | buffer_write_to_filename(filename, buffer, length); | ||
| 441 | |||
| 442 | free(buffer); | ||
| 443 | |||
| 444 | return 1; | ||
| 445 | } | ||
| 446 | |||
| 447 | static int mb2_status_check_snapshot_state(const char *path, const char *udid, const char *matches) | 356 | static int mb2_status_check_snapshot_state(const char *path, const char *udid, const char *matches) |
| 448 | { | 357 | { |
| 449 | int ret = -1; | 358 | int ret = -1; |
