@@ -365,62 +365,55 @@ static char* get_realpath(const char* path)
365365
366366static void handle_devinfo (afc_client_t afc , int argc , char * * argv )
367367{
368- char * * info = NULL ;
369- afc_error_t err = afc_get_device_info (afc , & info );
368+ plist_t info = NULL ;
369+ afc_error_t err = afc_get_device_info_plist (afc , & info );
370370 if (err == AFC_E_SUCCESS && info ) {
371- int i ;
372- for (i = 0 ; info [i ]; i += 2 ) {
373- printf ("%s: %s\n" , info [i ], info [i + 1 ]);
371+ if (argc > 0 && !strcmp (argv [0 ], "--plain" )) {
372+ plist_write_to_stream (info , stdout , PLIST_FORMAT_LIMD , PLIST_OPT_NONE );
373+ } else {
374+ plist_write_to_stream (info , stdout , PLIST_FORMAT_JSON , PLIST_OPT_NONE );
374375 }
375376 } else {
376377 printf ("Error: Failed to get device info: %s (%d)\n" , afc_strerror (err ), err );
377378 }
378- afc_dictionary_free (info );
379+ plist_free (info );
379380}
380381
381382static int get_file_info_stat (afc_client_t afc , const char * path , struct afc_file_stat * stbuf )
382383{
383- char * * info = NULL ;
384- afc_error_t ret = afc_get_file_info (afc , path , & info );
384+ plist_t info = NULL ;
385+ afc_error_t ret = afc_get_file_info_plist (afc , path , & info );
385386 memset (stbuf , 0 , sizeof (struct afc_file_stat ));
386387 if (ret != AFC_E_SUCCESS ) {
387388 return -1 ;
388389 } else if (!info ) {
389390 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 );
423391 }
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 );
424417 return 0 ;
425418}
426419
@@ -431,22 +424,23 @@ static void handle_file_info(afc_client_t afc, int argc, char** argv)
431424 return ;
432425 }
433426
434- char * * info = NULL ;
427+ plist_t info = NULL ;
435428 char * abspath = get_absolute_path (argv [0 ]);
436429 if (!abspath ) {
437430 printf ("Error: Invalid argument\n" );
438431 return ;
439432 }
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 );
441434 if (err == AFC_E_SUCCESS && info ) {
442- int i ;
443- for (i = 0 ; info [i ]; i += 2 ) {
444- printf ("%s: %s\n" , info [i ], info [i + 1 ]);
435+ if (argc > 1 && !strcmp (argv [1 ], "--plain" )) {
436+ plist_write_to_stream (info , stdout , PLIST_FORMAT_LIMD , PLIST_OPT_NONE );
437+ } else {
438+ plist_write_to_stream (info , stdout , PLIST_FORMAT_JSON , PLIST_OPT_NONE );
445439 }
446440 } else {
447441 printf ("Error: Failed to get file info for %s: %s (%d)\n" , argv [0 ], afc_strerror (err ), err );
448442 }
449- afc_dictionary_free (info );
443+ plist_free (info );
450444 free (abspath );
451445}
452446
@@ -784,28 +778,19 @@ static int __mkdir(const char* path)
784778
785779static uint8_t get_file (afc_client_t afc , const char * srcpath , const char * dstpath , uint8_t force_overwrite , uint8_t recursive_get )
786780{
787- char * * info = NULL ;
781+ plist_t info = NULL ;
788782 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 );
790784 if (err == AFC_E_OBJECT_NOT_FOUND ) {
791785 printf ("Error: Failed to read from file '%s': %s (%d)\n" , srcpath , afc_strerror (err ), err );
792786 return 0 ;
793787 }
794788 uint8_t is_dir = 0 ;
795789 if (info ) {
796- char * * p = info ;
797- while (p && * p ) {
798- if (!strcmp (* p , "st_size" )) {
799- p ++ ;
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 );
790+ file_size = plist_dict_get_uint (info , "st_size" );
791+ const char * ifmt = plist_get_string_ptr (plist_dict_get_item (info , "st_ifmt" ), NULL );
792+ is_dir = (ifmt && !strcmp (ifmt , "S_IFDIR" ));
793+ plist_free (info );
809794 }
810795 uint8_t succeed = 1 ;
811796 if (is_dir ) {
@@ -945,11 +930,11 @@ static void handle_get(afc_client_t afc, int argc, char **argv)
945930
946931static uint8_t put_single_file (afc_client_t afc , const char * srcpath , const char * dstpath , uint8_t force_overwrite )
947932{
948- char * * info = NULL ;
949- afc_error_t ret = afc_get_file_info (afc , dstpath , & info );
933+ plist_t info = NULL ;
934+ afc_error_t ret = afc_get_file_info_plist (afc , dstpath , & info );
950935 // file exists, only overwrite with '-f' option was set
951936 if (ret == AFC_E_SUCCESS && info ) {
952- afc_dictionary_free (info );
937+ plist_free (info );
953938 if (!force_overwrite ) {
954939 printf ("Error: Failed to write into existing file without '-f' option: %s\n" , dstpath );
955940 return 0 ;
@@ -1030,10 +1015,11 @@ static uint8_t put_file(afc_client_t afc, const char *srcpath, const char *dstpa
10301015 printf ("Error: Failed to put directory without '-r' option: %s\n" , srcpath );
10311016 return 0 ;
10321017 }
1033- char * * info = NULL ;
1034- afc_error_t err = afc_get_file_info (afc , dstpath , & info );
1018+ plist_t info = NULL ;
1019+ afc_error_t err = afc_get_file_info_plist (afc , dstpath , & info );
10351020 //create if target directory does not exist
1036- afc_dictionary_free (info );
1021+ plist_free (info );
1022+ info = NULL ;
10371023 if (err == AFC_E_OBJECT_NOT_FOUND ) {
10381024 err = afc_make_directory (afc , dstpath );
10391025 if (err != AFC_E_SUCCESS ) {
@@ -1044,19 +1030,12 @@ static uint8_t put_file(afc_client_t afc, const char *srcpath, const char *dstpa
10441030 printf ("Error: Failed to put existing directory without '-f' option: %s\n" , dstpath );
10451031 return 0 ;
10461032 }
1047- afc_get_file_info (afc , dstpath , & info );
1033+ afc_get_file_info_plist (afc , dstpath , & info );
10481034 uint8_t is_dir = 0 ;
10491035 if (info ) {
1050- char * * p = info ;
1051- while (p && * p ) {
1052- if (!strcmp (* p , "st_ifmt" )) {
1053- p ++ ;
1054- is_dir = !strcmp (* p , "S_IFDIR" );
1055- break ;
1056- }
1057- p ++ ;
1058- }
1059- afc_dictionary_free (info );
1036+ const char * ifmt = plist_get_string_ptr (plist_dict_get_item (info , "st_ifmt" ), NULL );
1037+ is_dir = (ifmt && !strcmp (ifmt , "S_IFDIR" ));
1038+ plist_free (info );
10601039 }
10611040 if (!is_dir ) {
10621041 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)
11481127 printf ("Error: Invalid number of arguments\n" );
11491128 return ;
11501129 }
1151- char * * info = NULL ;
1152- afc_error_t err = afc_get_file_info (afc , dstpath , & info );
1130+ plist_t info = NULL ;
1131+ afc_error_t err = afc_get_file_info_plist (afc , dstpath , & info );
11531132 // target does not exist, put directly
11541133 if (err == AFC_E_OBJECT_NOT_FOUND ) {
11551134 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)
11581137 } else {
11591138 uint8_t is_dir = 0 ;
11601139 if (info ) {
1161- char * * p = info ;
1162- while (p && * p ) {
1163- if (!strcmp (* p , "st_ifmt" )) {
1164- p ++ ;
1165- is_dir = !strcmp (* p , "S_IFDIR" );
1166- break ;
1167- }
1168- p ++ ;
1169- }
1170- afc_dictionary_free (info );
1140+ const char * ifmt = plist_get_string_ptr (plist_dict_get_item (info , "st_ifmt" ), NULL );
1141+ is_dir = (ifmt && !strcmp (ifmt , "S_IFDIR" ));
1142+ plist_free (info );
11711143 }
11721144 // target is a directory, try to put under this directory
11731145 if (is_dir ) {
@@ -1228,19 +1200,12 @@ static void handle_cd(afc_client_t afc, int argc, char** argv)
12281200
12291201 char * path = get_realpath (argv [0 ]);
12301202 int is_dir = 0 ;
1231- char * * info = NULL ;
1232- afc_error_t err = afc_get_file_info (afc , path , & info );
1203+ plist_t info = NULL ;
1204+ afc_error_t err = afc_get_file_info_plist (afc , path , & info );
12331205 if (err == AFC_E_SUCCESS && info ) {
1234- int i ;
1235- for (i = 0 ; info [i ]; i += 2 ) {
1236- if (!strcmp (info [i ], "st_ifmt" )) {
1237- if (!strcmp (info [i + 1 ], "S_IFDIR" )) {
1238- is_dir = 1 ;
1239- }
1240- break ;
1241- }
1242- }
1243- afc_dictionary_free (info );
1206+ const char * ifmt = plist_get_string_ptr (plist_dict_get_item (info , "st_ifmt" ), NULL );
1207+ is_dir = (ifmt && !strcmp (ifmt , "S_IFDIR" ));
1208+ plist_free (info );
12441209 } else {
12451210 printf ("Error: Failed to get file info for %s: %s (%d)\n" , path , afc_strerror (err ), err );
12461211 free (path );
0 commit comments