From 2344ec753ea58f93f1a8b889cc564d72bf084964 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Wed, 6 Aug 2014 03:12:12 +0200 Subject: idevicebackup2/idevicebackup: Remove code duplication and use common code for reading/writing plist files --- tools/Makefile.am | 4 +-- tools/idevicebackup.c | 89 +---------------------------------------------- tools/idevicebackup2.c | 93 +------------------------------------------------- 3 files changed, 4 insertions(+), 182 deletions(-) diff --git a/tools/Makefile.am b/tools/Makefile.am index 5fcd692..5b743f8 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -32,12 +32,12 @@ idevice_id_LDADD = $(top_builddir)/src/libimobiledevice.la idevicebackup_SOURCES = idevicebackup.c idevicebackup_CFLAGS = $(AM_CFLAGS) -idevicebackup_LDFLAGS = $(AM_LDFLAGS) +idevicebackup_LDFLAGS = $(top_builddir)/common/libinternalcommon.la $(AM_LDFLAGS) idevicebackup_LDADD = $(top_builddir)/src/libimobiledevice.la idevicebackup2_SOURCES = idevicebackup2.c idevicebackup2_CFLAGS = $(AM_CFLAGS) -idevicebackup2_LDFLAGS = $(AM_LDFLAGS) +idevicebackup2_LDFLAGS = $(top_builddir)/common/libinternalcommon.la $(AM_LDFLAGS) idevicebackup2_LDADD = $(top_builddir)/src/libimobiledevice.la ideviceimagemounter_SOURCES = ideviceimagemounter.c diff --git a/tools/idevicebackup.c b/tools/idevicebackup.c index e30540e..c126869 100644 --- a/tools/idevicebackup.c +++ b/tools/idevicebackup.c @@ -43,6 +43,7 @@ #include #include #include +#include "common/utils.h" #define MOBILEBACKUP_SERVICE_NAME "com.apple.mobilebackup" #define NP_SERVICE_NAME "com.apple.mobile.notification_proxy" @@ -67,11 +68,6 @@ enum cmd_mode { CMD_LEAVE }; -enum plist_format_t { - PLIST_FORMAT_XML, - PLIST_FORMAT_BINARY -}; - enum device_link_file_status_t { DEVICE_LINK_FILE_STATUS_NONE = 0, DEVICE_LINK_FILE_STATUS_HUNK, @@ -353,89 +349,6 @@ static void mobilebackup_info_update_last_backup_date(plist_t info_plist) node = NULL; } -static void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length) -{ - FILE *f; - uint64_t size; - - *length = 0; - - f = fopen(filename, "rb"); - if (!f) { - return; - } - - fseek(f, 0, SEEK_END); - size = ftell(f); - rewind(f); - - if (size == 0) { - return; - } - - *buffer = (char*)malloc(sizeof(char)*size); - fread(*buffer, sizeof(char), size, f); - fclose(f); - - *length = size; -} - -static void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length) -{ - FILE *f; - - f = fopen(filename, "ab"); - fwrite(buffer, sizeof(char), length, f); - fclose(f); -} - -static int plist_read_from_filename(plist_t *plist, const char *filename) -{ - char *buffer = NULL; - uint64_t length; - - if (!filename) - return 0; - - buffer_read_from_filename(filename, &buffer, &length); - - if (!buffer) { - return 0; - } - - if ((length > 8) && (memcmp(buffer, "bplist00", 8) == 0)) { - plist_from_bin(buffer, length, plist); - } else { - plist_from_xml(buffer, length, plist); - } - - free(buffer); - - return 1; -} - -static int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format) -{ - char *buffer = NULL; - uint32_t length; - - if (!plist || !filename) - return 0; - - if (format == PLIST_FORMAT_XML) - plist_to_xml(plist, &buffer, &length); - else if (format == PLIST_FORMAT_BINARY) - plist_to_bin(plist, &buffer, &length); - else - return 0; - - buffer_write_to_filename(filename, buffer, length); - - free(buffer); - - return 1; -} - static int plist_strcmp(plist_t node, const char *str) { char *buffer = NULL; 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 @@ #include #include #include +#include "common/utils.h" #include @@ -73,11 +74,6 @@ enum cmd_mode { CMD_CLOUD }; -enum plist_format_t { - PLIST_FORMAT_XML, - PLIST_FORMAT_BINARY -}; - enum cmd_flags { CMD_FLAG_RESTORE_SYSTEM_FILES = (1 << 1), CMD_FLAG_RESTORE_REBOOT = (1 << 2), @@ -357,93 +353,6 @@ static plist_t mobilebackup_factory_info_plist_new(const char* udid, lockdownd_c return ret; } -static void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length) -{ - FILE *f; - uint64_t size; - - *length = 0; - - f = fopen(filename, "rb"); - if (!f) { - return; - } - - fseek(f, 0, SEEK_END); - size = ftell(f); - rewind(f); - - if (size == 0) { - return; - } - - *buffer = (char*)malloc(sizeof(char)*size); - fread(*buffer, sizeof(char), size, f); - fclose(f); - - *length = size; -} - -static void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length) -{ - FILE *f; - - f = fopen(filename, "ab"); - if (!f) - f = fopen(filename, "wb"); - if (f) { - fwrite(buffer, sizeof(char), length, f); - fclose(f); - } -} - -static int plist_read_from_filename(plist_t *plist, const char *filename) -{ - char *buffer = NULL; - uint64_t length; - - if (!filename) - return 0; - - buffer_read_from_filename(filename, &buffer, &length); - - if (!buffer) { - return 0; - } - - if ((length > 8) && (memcmp(buffer, "bplist00", 8) == 0)) { - plist_from_bin(buffer, length, plist); - } else { - plist_from_xml(buffer, length, plist); - } - - free(buffer); - - return 1; -} - -static int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format) -{ - char *buffer = NULL; - uint32_t length; - - if (!plist || !filename) - return 0; - - if (format == PLIST_FORMAT_XML) - plist_to_xml(plist, &buffer, &length); - else if (format == PLIST_FORMAT_BINARY) - plist_to_bin(plist, &buffer, &length); - else - return 0; - - buffer_write_to_filename(filename, buffer, length); - - free(buffer); - - return 1; -} - static int mb2_status_check_snapshot_state(const char *path, const char *udid, const char *matches) { int ret = -1; -- cgit v1.1-32-gdbae