From 067c7c682614fbf0b7aeb13fd8b8ba7dc4fd8bf2 Mon Sep 17 00:00:00 2001 From: Martin Szulecki Date: Wed, 22 Oct 2014 20:48:00 +0200 Subject: common: Share idevicebackup helper string_build_path() from utils --- common/utils.c | 29 +++++++++++++++++++++++ common/utils.h | 1 + tools/idevicebackup.c | 29 +---------------------- tools/idevicebackup2.c | 63 +++++++++++++++----------------------------------- 4 files changed, 49 insertions(+), 73 deletions(-) diff --git a/common/utils.c b/common/utils.c index 4465490..fb6822f 100644 --- a/common/utils.c +++ b/common/utils.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "utils.h" @@ -110,6 +111,34 @@ char *string_concat(const char *str, ...) return result; } +char *string_build_path(const char *elem, ...) +{ + if (!elem) + return NULL; + va_list args; + int len = strlen(elem)+1; + va_start(args, elem); + char *arg = va_arg(args, char*); + while (arg) { + len += strlen(arg)+1; + arg = va_arg(args, char*); + } + va_end(args); + + char* out = (char*)malloc(len); + strcpy(out, elem); + + va_start(args, elem); + arg = va_arg(args, char*); + while (arg) { + strcat(out, "/"); + strcat(out, arg); + arg = va_arg(args, char*); + } + va_end(args); + return out; +} + static int get_rand(int min, int max) { int retval = (rand() % (max - min)) + min; diff --git a/common/utils.h b/common/utils.h index 388a793..96e6ba2 100644 --- a/common/utils.h +++ b/common/utils.h @@ -37,6 +37,7 @@ char *stpcpy(char *s1, const char *s2); #endif char *string_concat(const char *str, ...); +char *string_build_path(const char *elem, ...); char *generate_uuid(); void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length); diff --git a/tools/idevicebackup.c b/tools/idevicebackup.c index c5315d2..67d82fd 100644 --- a/tools/idevicebackup.c +++ b/tools/idevicebackup.c @@ -234,33 +234,6 @@ static char *str_toupper(char* str) return res; } -static char* build_path(const char* elem, ...) -{ - if (!elem) return NULL; - va_list args; - int len = strlen(elem)+1; - va_start(args, elem); - char *arg = va_arg(args, char*); - while (arg) { - len += strlen(arg)+1; - arg = va_arg(args, char*); - } - va_end(args); - - char* out = (char*)malloc(len); - strcpy(out, elem); - - va_start(args, elem); - arg = va_arg(args, char*); - while (arg) { - strcat(out, "/"); - strcat(out, arg); - arg = va_arg(args, char*); - } - va_end(args); - return out; -} - static char* format_size_for_display(uint64_t size) { char buf[32]; @@ -370,7 +343,7 @@ static char *mobilebackup_build_path(const char *backup_directory, const char *n strcpy(filename, name); if (extension != NULL) strcat(filename, extension); - char *path = build_path(backup_directory, filename, NULL); + char *path = string_build_path(backup_directory, filename, NULL); free(filename); return path; } diff --git a/tools/idevicebackup2.c b/tools/idevicebackup2.c index 4fde24f..209dfe6 100644 --- a/tools/idevicebackup2.c +++ b/tools/idevicebackup2.c @@ -199,33 +199,6 @@ static int mkdir_with_parents(const char *dir, int mode) return res; } -static char* build_path(const char* elem, ...) -{ - if (!elem) return NULL; - va_list args; - int len = strlen(elem)+1; - va_start(args, elem); - char *arg = va_arg(args, char*); - while (arg) { - len += strlen(arg)+1; - arg = va_arg(args, char*); - } - va_end(args); - - char* out = (char*)malloc(len); - strcpy(out, elem); - - va_start(args, elem); - arg = va_arg(args, char*); - while (arg) { - strcat(out, "/"); - strcat(out, arg); - arg = va_arg(args, char*); - } - va_end(args); - return out; -} - static char* format_size_for_display(uint64_t size) { char buf[32]; @@ -357,7 +330,7 @@ static int mb2_status_check_snapshot_state(const char *path, const char *udid, c { int ret = -1; plist_t status_plist = NULL; - char *file_path = build_path(path, udid, "Status.plist", NULL); + char *file_path = string_build_path(path, udid, "Status.plist", NULL); plist_read_from_filename(&status_plist, file_path); free(file_path); @@ -522,7 +495,7 @@ static int mb2_handle_send_file(mobilebackup2_client_t mobilebackup2, const char uint32_t nlen = 0; uint32_t pathlen = strlen(path); uint32_t bytes = 0; - char *localfile = build_path(backup_dir, path, NULL); + char *localfile = string_build_path(backup_dir, path, NULL); char buf[32768]; #ifdef WIN32 struct _stati64 fst; @@ -809,7 +782,7 @@ static int mb2_handle_receive_files(mobilebackup2_client_t mobilebackup2, plist_ bname = NULL; } - bname = build_path(backup_dir, fname, NULL); + bname = string_build_path(backup_dir, fname, NULL); if (fname != NULL) { free(fname); @@ -942,7 +915,7 @@ static void mb2_handle_list_directory(mobilebackup2_client_t mobilebackup2, plis return; } - char *path = build_path(backup_dir, str, NULL); + char *path = string_build_path(backup_dir, str, NULL); free(str); plist_t dirlist = plist_new_dict(); @@ -954,7 +927,7 @@ static void mb2_handle_list_directory(mobilebackup2_client_t mobilebackup2, plis if ((strcmp(ep->d_name, ".") == 0) || (strcmp(ep->d_name, "..") == 0)) { continue; } - char *fpath = build_path(path, ep->d_name, NULL); + char *fpath = string_build_path(path, ep->d_name, NULL); if (fpath) { plist_t fdict = plist_new_dict(); struct stat st; @@ -995,7 +968,7 @@ static void mb2_handle_make_directory(mobilebackup2_client_t mobilebackup2, plis char *errdesc = NULL; plist_get_string_val(dir, &str); - char *newpath = build_path(backup_dir, str, NULL); + char *newpath = string_build_path(backup_dir, str, NULL); free(str); if (mkdir_with_parents(newpath, 0755) < 0) { @@ -1075,8 +1048,8 @@ static void mb2_copy_directory_by_path(const char *src, const char *dst) if ((strcmp(ep->d_name, ".") == 0) || (strcmp(ep->d_name, "..") == 0)) { continue; } - char *srcpath = build_path(src, ep->d_name, NULL); - char *dstpath = build_path(dst, ep->d_name, NULL); + char *srcpath = string_build_path(src, ep->d_name, NULL); + char *dstpath = string_build_path(dst, ep->d_name, NULL); if (srcpath && dstpath) { /* copy file */ mb2_copy_file_by_path(srcpath, dstpath); @@ -1446,7 +1419,7 @@ int main(int argc, char *argv[]) } } else if (cmd != CMD_CLOUD) { /* backup directory must contain an Info.plist */ - info_path = build_path(backup_directory, source_udid, "Info.plist", NULL); + info_path = string_build_path(backup_directory, source_udid, "Info.plist", NULL); if (cmd == CMD_RESTORE || cmd == CMD_UNBACK) { if (stat(info_path, &st) != 0) { idevice_free(device); @@ -1454,7 +1427,7 @@ int main(int argc, char *argv[]) printf("ERROR: Backup directory \"%s\" is invalid. No Info.plist found for UDID %s.\n", backup_directory, source_udid); return -1; } - char* manifest_path = build_path(backup_directory, source_udid, "Manifest.plist", NULL); + char* manifest_path = string_build_path(backup_directory, source_udid, "Manifest.plist", NULL); if (stat(manifest_path, &st) != 0) { free(info_path); } @@ -1647,20 +1620,20 @@ checkpoint: PRINT_VERBOSE(1, "Starting backup...\n"); /* make sure backup device sub-directory exists */ - char* devbackupdir = build_path(backup_directory, source_udid, NULL); + char* devbackupdir = string_build_path(backup_directory, source_udid, NULL); __mkdir(devbackupdir, 0755); free(devbackupdir); if (strcmp(source_udid, udid) != 0) { /* handle different source backup directory */ // make sure target backup device sub-directory exists - devbackupdir = build_path(backup_directory, udid, NULL); + devbackupdir = string_build_path(backup_directory, udid, NULL); __mkdir(devbackupdir, 0755); free(devbackupdir); // use Info.plist path in target backup folder */ free(info_path); - info_path = build_path(backup_directory, udid, "Info.plist", NULL); + info_path = string_build_path(backup_directory, udid, "Info.plist", NULL); } /* TODO: check domain com.apple.mobile.backup key RequiresEncrypt and WillEncrypt with lockdown */ @@ -1942,9 +1915,9 @@ checkpoint: char *str = NULL; plist_get_string_val(val, &str); if (str) { - char *newpath = build_path(backup_directory, str, NULL); + char *newpath = string_build_path(backup_directory, str, NULL); free(str); - char *oldpath = build_path(backup_directory, key, NULL); + char *oldpath = string_build_path(backup_directory, key, NULL); #ifdef WIN32 if ((stat(newpath, &st) == 0) && S_ISDIR(st.st_mode)) @@ -2000,7 +1973,7 @@ checkpoint: suppress_warning = 1; } } - char *newpath = build_path(backup_directory, str, NULL); + char *newpath = string_build_path(backup_directory, str, NULL); free(str); #ifdef WIN32 int res = 0; @@ -2044,8 +2017,8 @@ checkpoint: plist_get_string_val(srcpath, &src); plist_get_string_val(dstpath, &dst); if (src && dst) { - char *oldpath = build_path(backup_directory, src, NULL); - char *newpath = build_path(backup_directory, dst, NULL); + char *oldpath = string_build_path(backup_directory, src, NULL); + char *newpath = string_build_path(backup_directory, dst, NULL); PRINT_VERBOSE(1, "Copying '%s' to '%s'\n", src, dst); -- cgit v1.1-32-gdbae