From 4869a343e884c41fae5ac5c84e0af72e920f850e Mon Sep 17 00:00:00 2001 From: Andy Polyakov Date: Mon, 6 Oct 2014 22:32:36 +0200 Subject: [PATCH 1/4] fix AFCFileRef[Read|Write] prototypes Incorrect AFCFileRefRead prototype was causing crashes in 64-bit build when reading large files. --- MobileDevice.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MobileDevice.h b/MobileDevice.h index e078a44..d97eafb 100644 --- a/MobileDevice.h +++ b/MobileDevice.h @@ -396,11 +396,11 @@ afc_error_t AFCFileRefOpen(afc_connection *conn, const char *path, afc_error_t AFCFileRefSeek(afc_connection *conn, afc_file_ref ref, unsigned long long offset1, unsigned long long offset2); afc_error_t AFCFileRefRead(afc_connection *conn, afc_file_ref ref, - void *buf, unsigned int *len); + void *buf, size_t *len); afc_error_t AFCFileRefSetFileSize(afc_connection *conn, afc_file_ref ref, unsigned long long offset); afc_error_t AFCFileRefWrite(afc_connection *conn, afc_file_ref ref, - const void *buf, unsigned int len); + const void *buf, size_t len); afc_error_t AFCFileRefClose(afc_connection *conn, afc_file_ref ref); afc_error_t AFCFileInfoOpen(afc_connection *conn, const char *path, struct @@ -493,4 +493,4 @@ typedef unsigned int (*t_performOperation)(struct am_restore_device *rdev, } #endif -#endif \ No newline at end of file +#endif From 23afce0d3714e5599462cb2ed5326343973a148e Mon Sep 17 00:00:00 2001 From: Andy Polyakov Date: Mon, 6 Oct 2014 22:40:43 +0200 Subject: [PATCH 2/4] handle no --args When no --args are passed application was started with {args} as argument instead of no argument. --- ios-deploy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ios-deploy.c b/ios-deploy.c index 75b083a..fb1f181 100644 --- a/ios-deploy.c +++ b/ios-deploy.c @@ -644,12 +644,12 @@ void write_lldb_prep_cmds(AMDeviceRef device, CFURLRef disk_app_url) { if (args) { CFStringRef cf_args = CFStringCreateWithCString(NULL, args, kCFStringEncodingASCII); CFStringFindAndReplace(cmds, CFSTR("{args}"), cf_args, range, 0); - rangeLLDB.length = CFStringGetLength(pmodule); CFStringFindAndReplace(pmodule, CFSTR("{args}"), cf_args, rangeLLDB, 0); CFRelease(cf_args); } else { - CFStringFindAndReplace(cmds, CFSTR(" {args}"), CFSTR(""), range, 0); + CFStringFindAndReplace(cmds, CFSTR("{args}"), CFSTR(""), range, 0); + CFStringFindAndReplace(pmodule, CFSTR("{args}"), CFSTR(""), rangeLLDB, 0); } range.length = CFStringGetLength(cmds); From 9a2da6f6427e740eec83d9136569968309798183 Mon Sep 17 00:00:00 2001 From: Andy Polyakov Date: Mon, 6 Oct 2014 23:02:06 +0200 Subject: [PATCH 3/4] Fix usage semantic and pair of resource leaks AFCConnectionOpen and AFCFIleInfoOpen don't require initialized pointer to pointer to result, but do require corresponding Close calls. --- ios-deploy.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/ios-deploy.c b/ios-deploy.c index fb1f181..1c636bf 100644 --- a/ios-deploy.c +++ b/ios-deploy.c @@ -1014,12 +1014,24 @@ void read_dir(service_conn_t afcFd, afc_connection* afc_conn_p, const char* dir) printf("%s\n", dir); - afc_dictionary afc_dict; - afc_dictionary* afc_dict_p = &afc_dict; + afc_dictionary* afc_dict_p; + char *key, *val; + int not_dir; + AFCFileInfoOpen(afc_conn_p, dir, &afc_dict_p); - - afc_directory afc_dir; - afc_directory* afc_dir_p = &afc_dir; + while((AFCKeyValueRead(afc_dict_p,&key,&val) == 0) && key && val) { + if (strcmp(key,"st_ifmt")==0) { + not_dir = strcmp(val,"S_IFDIR"); + break; + } + } + AFCKeyValueClose(afc_dict_p); + + if (not_dir) { + return; + } + + afc_directory* afc_dir_p; afc_error_t err = AFCDirectoryOpen(afc_conn_p, dir, &afc_dir_p); if (err != 0) @@ -1031,7 +1043,7 @@ void read_dir(service_conn_t afcFd, afc_connection* afc_conn_p, const char* dir) while(true) { err = AFCDirectoryRead(afc_conn_p, afc_dir_p, &dir_ent); - if (!dir_ent) + if (err != 0 || !dir_ent) break; if (strcmp(dir_ent, ".") == 0 || strcmp(dir_ent, "..") == 0) @@ -1119,11 +1131,11 @@ void list_files(AMDeviceRef device) { service_conn_t houseFd = start_house_arrest_service(device); - afc_connection afc_conn; - afc_connection* afc_conn_p = &afc_conn; - AFCConnectionOpen(houseFd, 0, &afc_conn_p); - - read_dir(houseFd, afc_conn_p, "/"); + afc_connection* afc_conn_p; + if (AFCConnectionOpen(houseFd, 0, &afc_conn_p) == 0) { + read_dir(houseFd, afc_conn_p, "/"); + AFCConnectionClose(afc_conn_p); + } } void upload_file(AMDeviceRef device) { From 4589cd5d05016ed89fc73bbb5c2519b6654a3708 Mon Sep 17 00:00:00 2001 From: Andy Polyakov Date: Mon, 6 Oct 2014 23:16:40 +0200 Subject: [PATCH 4/4] arrange callback in read_dir for future use Custom callback is called for every file or directory listed on device. This will be used for file download. --- ios-deploy.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ios-deploy.c b/ios-deploy.c index 1c636bf..6082c00 100644 --- a/ios-deploy.c +++ b/ios-deploy.c @@ -1002,7 +1002,8 @@ CFStringRef get_bundle_id(CFURLRef app_url) return bundle_id; } -void read_dir(service_conn_t afcFd, afc_connection* afc_conn_p, const char* dir) +void read_dir(service_conn_t afcFd, afc_connection* afc_conn_p, const char* dir, + void(*callback)(afc_connection *conn,const char *dir,int file)) { char *dir_ent; @@ -1028,16 +1029,18 @@ void read_dir(service_conn_t afcFd, afc_connection* afc_conn_p, const char* dir) AFCKeyValueClose(afc_dict_p); if (not_dir) { + if (callback) (*callback)(afc_conn_p, dir, not_dir); return; } afc_directory* afc_dir_p; afc_error_t err = AFCDirectoryOpen(afc_conn_p, dir, &afc_dir_p); - if (err != 0) - { + if (err != 0) { // Couldn't open dir - was probably a file return; + } else { + if (callback) (*callback)(afc_conn_p, dir, not_dir); } while(true) { @@ -1054,7 +1057,7 @@ void read_dir(service_conn_t afcFd, afc_connection* afc_conn_p, const char* dir) if (dir_joined[strlen(dir)-1] != '/') strcat(dir_joined, "/"); strcat(dir_joined, dir_ent); - read_dir(afcFd, afc_conn_p, dir_joined); + read_dir(afcFd, afc_conn_p, dir_joined, callback); free(dir_joined); } @@ -1133,7 +1136,7 @@ void list_files(AMDeviceRef device) afc_connection* afc_conn_p; if (AFCConnectionOpen(houseFd, 0, &afc_conn_p) == 0) { - read_dir(houseFd, afc_conn_p, "/"); + read_dir(houseFd, afc_conn_p, "/", NULL); AFCConnectionClose(afc_conn_p); } } @@ -1147,7 +1150,7 @@ void upload_file(AMDeviceRef device) { afc_connection* afc_conn_p = &afc_conn; AFCConnectionOpen(houseFd, 0, &afc_conn_p); - // read_dir(houseFd, NULL, "/"); + // read_dir(houseFd, NULL, "/", NULL); if (!target_filename) {