summaryrefslogtreecommitdiffstats
path: root/tools/afcclient.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2025-06-29 23:54:16 +0200
committerGravatar Nikias Bassen2025-06-29 23:54:16 +0200
commitcb34a171994562a78da7ea14b801759747d0fdf7 (patch)
tree8a5c93443ec58b323e437db4655b603f622eeadb /tools/afcclient.c
parent9049ffb7eaab5a75018bb8fb1a54a9c9903daf00 (diff)
downloadlibimobiledevice-cb34a171994562a78da7ea14b801759747d0fdf7.tar.gz
libimobiledevice-cb34a171994562a78da7ea14b801759747d0fdf7.tar.bz2
tools: Use new afc_get_file_info_plist/afc_get_device_info_plist functions
Diffstat (limited to 'tools/afcclient.c')
-rw-r--r--tools/afcclient.c171
1 files changed, 68 insertions, 103 deletions
diff --git a/tools/afcclient.c b/tools/afcclient.c
index 1cddb36..a958c23 100644
--- a/tools/afcclient.c
+++ b/tools/afcclient.c
@@ -365,62 +365,55 @@ static char* get_realpath(const char* path)
365 365
366static void handle_devinfo(afc_client_t afc, int argc, char** argv) 366static void handle_devinfo(afc_client_t afc, int argc, char** argv)
367{ 367{
368 char **info = NULL; 368 plist_t info = NULL;
369 afc_error_t err = afc_get_device_info(afc, &info); 369 afc_error_t err = afc_get_device_info_plist(afc, &info);
370 if (err == AFC_E_SUCCESS && info) { 370 if (err == AFC_E_SUCCESS && info) {
371 int i; 371 if (argc > 0 && !strcmp(argv[0], "--plain")) {
372 for (i = 0; info[i]; i += 2) { 372 plist_write_to_stream(info, stdout, PLIST_FORMAT_LIMD, PLIST_OPT_NONE);
373 printf("%s: %s\n", info[i], info[i+1]); 373 } else {
374 plist_write_to_stream(info, stdout, PLIST_FORMAT_JSON, PLIST_OPT_NONE);
374 } 375 }
375 } else { 376 } else {
376 printf("Error: Failed to get device info: %s (%d)\n", afc_strerror(err), err); 377 printf("Error: Failed to get device info: %s (%d)\n", afc_strerror(err), err);
377 } 378 }
378 afc_dictionary_free(info); 379 plist_free(info);
379} 380}
380 381
381static int get_file_info_stat(afc_client_t afc, const char* path, struct afc_file_stat *stbuf) 382static int get_file_info_stat(afc_client_t afc, const char* path, struct afc_file_stat *stbuf)
382{ 383{
383 char **info = NULL; 384 plist_t info = NULL;
384 afc_error_t ret = afc_get_file_info(afc, path, &info); 385 afc_error_t ret = afc_get_file_info_plist(afc, path, &info);
385 memset(stbuf, 0, sizeof(struct afc_file_stat)); 386 memset(stbuf, 0, sizeof(struct afc_file_stat));
386 if (ret != AFC_E_SUCCESS) { 387 if (ret != AFC_E_SUCCESS) {
387 return -1; 388 return -1;
388 } else if (!info) { 389 } else if (!info) {
389 return -1; 390 return -1;
390 } else {
391 // get file attributes from info list
392 int i;
393 for (i = 0; info[i]; i += 2) {
394 if (!strcmp(info[i], "st_size")) {
395 stbuf->st_size = atoll(info[i+1]);
396 } else if (!strcmp(info[i], "st_blocks")) {
397 stbuf->st_blocks = atoi(info[i+1]);
398 } else if (!strcmp(info[i], "st_ifmt")) {
399 if (!strcmp(info[i+1], "S_IFREG")) {
400 stbuf->st_mode = S_IFREG;
401 } else if (!strcmp(info[i+1], "S_IFDIR")) {
402 stbuf->st_mode = S_IFDIR;
403 } else if (!strcmp(info[i+1], "S_IFLNK")) {
404 stbuf->st_mode = S_IFLNK;
405 } else if (!strcmp(info[i+1], "S_IFBLK")) {
406 stbuf->st_mode = S_IFBLK;
407 } else if (!strcmp(info[i+1], "S_IFCHR")) {
408 stbuf->st_mode = S_IFCHR;
409 } else if (!strcmp(info[i+1], "S_IFIFO")) {
410 stbuf->st_mode = S_IFIFO;
411 } else if (!strcmp(info[i+1], "S_IFSOCK")) {
412 stbuf->st_mode = S_IFSOCK;
413 }
414 } else if (!strcmp(info[i], "st_nlink")) {
415 stbuf->st_nlink = atoi(info[i+1]);
416 } else if (!strcmp(info[i], "st_mtime")) {
417 stbuf->st_mtime = (time_t)(atoll(info[i+1]) / 1000000000);
418 } else if (!strcmp(info[i], "st_birthtime")) { /* available on iOS 7+ */
419 stbuf->st_birthtime = (time_t)(atoll(info[i+1]) / 1000000000);
420 }
421 }
422 afc_dictionary_free(info);
423 } 391 }
392 stbuf->st_size = plist_dict_get_uint(info, "st_size");
393 stbuf->st_blocks = plist_dict_get_uint(info, "st_blocks");
394 const char* s_ifmt = plist_get_string_ptr(plist_dict_get_item(info, "st_ifmt"), NULL);
395 if (s_ifmt) {
396 if (!strcmp(s_ifmt, "S_IFREG")) {
397 stbuf->st_mode = S_IFREG;
398 } else if (!strcmp(s_ifmt, "S_IFDIR")) {
399 stbuf->st_mode = S_IFDIR;
400 } else if (!strcmp(s_ifmt, "S_IFLNK")) {
401 stbuf->st_mode = S_IFLNK;
402 } else if (!strcmp(s_ifmt, "S_IFBLK")) {
403 stbuf->st_mode = S_IFBLK;
404 } else if (!strcmp(s_ifmt, "S_IFCHR")) {
405 stbuf->st_mode = S_IFCHR;
406 } else if (!strcmp(s_ifmt, "S_IFIFO")) {
407 stbuf->st_mode = S_IFIFO;
408 } else if (!strcmp(s_ifmt, "S_IFSOCK")) {
409 stbuf->st_mode = S_IFSOCK;
410 }
411 }
412 stbuf->st_nlink = plist_dict_get_uint(info, "st_nlink");
413 stbuf->st_mtime = (time_t)(plist_dict_get_uint(info, "st_mtime") / 1000000000);
414 /* available on iOS 7+ */
415 stbuf->st_birthtime = (time_t)(plist_dict_get_uint(info, "st_birthtime") / 1000000000);
416 plist_free(info);
424 return 0; 417 return 0;
425} 418}
426 419
@@ -431,22 +424,23 @@ static void handle_file_info(afc_client_t afc, int argc, char** argv)
431 return; 424 return;
432 } 425 }
433 426
434 char **info = NULL; 427 plist_t info = NULL;
435 char* abspath = get_absolute_path(argv[0]); 428 char* abspath = get_absolute_path(argv[0]);
436 if (!abspath) { 429 if (!abspath) {
437 printf("Error: Invalid argument\n"); 430 printf("Error: Invalid argument\n");
438 return; 431 return;
439 } 432 }
440 afc_error_t err = afc_get_file_info(afc, abspath, &info); 433 afc_error_t err = afc_get_file_info_plist(afc, abspath, &info);
441 if (err == AFC_E_SUCCESS && info) { 434 if (err == AFC_E_SUCCESS && info) {
442 int i; 435 if (argc > 1 && !strcmp(argv[1], "--plain")) {
443 for (i = 0; info[i]; i += 2) { 436 plist_write_to_stream(info, stdout, PLIST_FORMAT_LIMD, PLIST_OPT_NONE);
444 printf("%s: %s\n", info[i], info[i+1]); 437 } else {
438 plist_write_to_stream(info, stdout, PLIST_FORMAT_JSON, PLIST_OPT_NONE);
445 } 439 }
446 } else { 440 } else {
447 printf("Error: Failed to get file info for %s: %s (%d)\n", argv[0], afc_strerror(err), err); 441 printf("Error: Failed to get file info for %s: %s (%d)\n", argv[0], afc_strerror(err), err);
448 } 442 }
449 afc_dictionary_free(info); 443 plist_free(info);
450 free(abspath); 444 free(abspath);
451} 445}
452 446
@@ -784,28 +778,19 @@ static int __mkdir(const char* path)
784 778
785static uint8_t get_file(afc_client_t afc, const char *srcpath, const char *dstpath, uint8_t force_overwrite, uint8_t recursive_get) 779static uint8_t get_file(afc_client_t afc, const char *srcpath, const char *dstpath, uint8_t force_overwrite, uint8_t recursive_get)
786{ 780{
787 char **info = NULL; 781 plist_t info = NULL;
788 uint64_t file_size = 0; 782 uint64_t file_size = 0;
789 afc_error_t err = afc_get_file_info(afc, srcpath, &info); 783 afc_error_t err = afc_get_file_info_plist(afc, srcpath, &info);
790 if (err == AFC_E_OBJECT_NOT_FOUND) { 784 if (err == AFC_E_OBJECT_NOT_FOUND) {
791 printf("Error: Failed to read from file '%s': %s (%d)\n", srcpath, afc_strerror(err), err); 785 printf("Error: Failed to read from file '%s': %s (%d)\n", srcpath, afc_strerror(err), err);
792 return 0; 786 return 0;
793 } 787 }
794 uint8_t is_dir = 0; 788 uint8_t is_dir = 0;
795 if (info) { 789 if (info) {
796 char **p = info; 790 file_size = plist_dict_get_uint(info, "st_size");
797 while (p && *p) { 791 const char* ifmt = plist_get_string_ptr(plist_dict_get_item(info, "st_ifmt"), NULL);
798 if (!strcmp(*p, "st_size")) { 792 is_dir = (ifmt && !strcmp(ifmt, "S_IFDIR"));
799 p++; 793 plist_free(info);
800 file_size = (uint64_t) strtoull(*p, NULL, 10);
801 }
802 if (!strcmp(*p, "st_ifmt")) {
803 p++;
804 is_dir = !strcmp(*p, "S_IFDIR");
805 }
806 p++;
807 }
808 afc_dictionary_free(info);
809 } 794 }
810 uint8_t succeed = 1; 795 uint8_t succeed = 1;
811 if (is_dir) { 796 if (is_dir) {
@@ -945,11 +930,11 @@ static void handle_get(afc_client_t afc, int argc, char **argv)
945 930
946static uint8_t put_single_file(afc_client_t afc, const char *srcpath, const char *dstpath, uint8_t force_overwrite) 931static uint8_t put_single_file(afc_client_t afc, const char *srcpath, const char *dstpath, uint8_t force_overwrite)
947{ 932{
948 char **info = NULL; 933 plist_t info = NULL;
949 afc_error_t ret = afc_get_file_info(afc, dstpath, &info); 934 afc_error_t ret = afc_get_file_info_plist(afc, dstpath, &info);
950 // file exists, only overwrite with '-f' option was set 935 // file exists, only overwrite with '-f' option was set
951 if (ret == AFC_E_SUCCESS && info) { 936 if (ret == AFC_E_SUCCESS && info) {
952 afc_dictionary_free(info); 937 plist_free(info);
953 if (!force_overwrite) { 938 if (!force_overwrite) {
954 printf("Error: Failed to write into existing file without '-f' option: %s\n", dstpath); 939 printf("Error: Failed to write into existing file without '-f' option: %s\n", dstpath);
955 return 0; 940 return 0;
@@ -1030,10 +1015,11 @@ static uint8_t put_file(afc_client_t afc, const char *srcpath, const char *dstpa
1030 printf("Error: Failed to put directory without '-r' option: %s\n", srcpath); 1015 printf("Error: Failed to put directory without '-r' option: %s\n", srcpath);
1031 return 0; 1016 return 0;
1032 } 1017 }
1033 char **info = NULL; 1018 plist_t info = NULL;
1034 afc_error_t err = afc_get_file_info(afc, dstpath, &info); 1019 afc_error_t err = afc_get_file_info_plist(afc, dstpath, &info);
1035 //create if target directory does not exist 1020 //create if target directory does not exist
1036 afc_dictionary_free(info); 1021 plist_free(info);
1022 info = NULL;
1037 if (err == AFC_E_OBJECT_NOT_FOUND) { 1023 if (err == AFC_E_OBJECT_NOT_FOUND) {
1038 err = afc_make_directory(afc, dstpath); 1024 err = afc_make_directory(afc, dstpath);
1039 if (err != AFC_E_SUCCESS) { 1025 if (err != AFC_E_SUCCESS) {
@@ -1044,19 +1030,12 @@ static uint8_t put_file(afc_client_t afc, const char *srcpath, const char *dstpa
1044 printf("Error: Failed to put existing directory without '-f' option: %s\n", dstpath); 1030 printf("Error: Failed to put existing directory without '-f' option: %s\n", dstpath);
1045 return 0; 1031 return 0;
1046 } 1032 }
1047 afc_get_file_info(afc, dstpath, &info); 1033 afc_get_file_info_plist(afc, dstpath, &info);
1048 uint8_t is_dir = 0; 1034 uint8_t is_dir = 0;
1049 if (info) { 1035 if (info) {
1050 char **p = info; 1036 const char* ifmt = plist_get_string_ptr(plist_dict_get_item(info, "st_ifmt"), NULL);
1051 while (p && *p) { 1037 is_dir = (ifmt && !strcmp(ifmt, "S_IFDIR"));
1052 if (!strcmp(*p, "st_ifmt")) { 1038 plist_free(info);
1053 p++;
1054 is_dir = !strcmp(*p, "S_IFDIR");
1055 break;
1056 }
1057 p++;
1058 }
1059 afc_dictionary_free(info);
1060 } 1039 }
1061 if (!is_dir) { 1040 if (!is_dir) {
1062 printf("Error: Failed to create or access directory: '%s'\n", dstpath); 1041 printf("Error: Failed to create or access directory: '%s'\n", dstpath);
@@ -1148,8 +1127,8 @@ static void handle_put(afc_client_t afc, int argc, char **argv)
1148 printf("Error: Invalid number of arguments\n"); 1127 printf("Error: Invalid number of arguments\n");
1149 return; 1128 return;
1150 } 1129 }
1151 char **info = NULL; 1130 plist_t info = NULL;
1152 afc_error_t err = afc_get_file_info(afc, dstpath, &info); 1131 afc_error_t err = afc_get_file_info_plist(afc, dstpath, &info);
1153 // target does not exist, put directly 1132 // target does not exist, put directly
1154 if (err == AFC_E_OBJECT_NOT_FOUND) { 1133 if (err == AFC_E_OBJECT_NOT_FOUND) {
1155 put_file(afc, srcpath, dstpath, force_overwrite, recursive_put); 1134 put_file(afc, srcpath, dstpath, force_overwrite, recursive_put);
@@ -1158,16 +1137,9 @@ static void handle_put(afc_client_t afc, int argc, char **argv)
1158 } else { 1137 } else {
1159 uint8_t is_dir = 0; 1138 uint8_t is_dir = 0;
1160 if (info) { 1139 if (info) {
1161 char **p = info; 1140 const char* ifmt = plist_get_string_ptr(plist_dict_get_item(info, "st_ifmt"), NULL);
1162 while (p && *p) { 1141 is_dir = (ifmt && !strcmp(ifmt, "S_IFDIR"));
1163 if (!strcmp(*p, "st_ifmt")) { 1142 plist_free(info);
1164 p++;
1165 is_dir = !strcmp(*p, "S_IFDIR");
1166 break;
1167 }
1168 p++;
1169 }
1170 afc_dictionary_free(info);
1171 } 1143 }
1172 // target is a directory, try to put under this directory 1144 // target is a directory, try to put under this directory
1173 if (is_dir) { 1145 if (is_dir) {
@@ -1228,19 +1200,12 @@ static void handle_cd(afc_client_t afc, int argc, char** argv)
1228 1200
1229 char* path = get_realpath(argv[0]); 1201 char* path = get_realpath(argv[0]);
1230 int is_dir = 0; 1202 int is_dir = 0;
1231 char **info = NULL; 1203 plist_t info = NULL;
1232 afc_error_t err = afc_get_file_info(afc, path, &info); 1204 afc_error_t err = afc_get_file_info_plist(afc, path, &info);
1233 if (err == AFC_E_SUCCESS && info) { 1205 if (err == AFC_E_SUCCESS && info) {
1234 int i; 1206 const char* ifmt = plist_get_string_ptr(plist_dict_get_item(info, "st_ifmt"), NULL);
1235 for (i = 0; info[i]; i += 2) { 1207 is_dir = (ifmt && !strcmp(ifmt, "S_IFDIR"));
1236 if (!strcmp(info[i], "st_ifmt")) { 1208 plist_free(info);
1237 if (!strcmp(info[i+1], "S_IFDIR")) {
1238 is_dir = 1;
1239 }
1240 break;
1241 }
1242 }
1243 afc_dictionary_free(info);
1244 } else { 1209 } else {
1245 printf("Error: Failed to get file info for %s: %s (%d)\n", path, afc_strerror(err), err); 1210 printf("Error: Failed to get file info for %s: %s (%d)\n", path, afc_strerror(err), err);
1246 free(path); 1211 free(path);