@@ -671,13 +671,17 @@ void read_dir(afc_connection* afc_conn_p, const char* dir, json &files, std::str
671
671
}
672
672
}
673
673
674
+ std::mutex list_files_mutex;
674
675
void list_files (std::string device_identifier, const char *application_identifier, const char *device_path, std::string method_id)
675
676
{
676
- std::string device_root (device_path);
677
- afc_connection* afc_conn_p = get_afc_connection (device_identifier, application_identifier, device_root, method_id);
677
+ list_files_mutex.lock ();
678
+
679
+ std::string device_root (device_path);
680
+ afc_connection* afc_conn_p = get_afc_connection (device_identifier, application_identifier, device_root, method_id);
678
681
if (!afc_conn_p)
679
682
{
680
683
print_error (" Could not establish AFC Connection" , device_identifier, method_id);
684
+ list_files_mutex.unlock ();
681
685
return ;
682
686
}
683
687
@@ -694,6 +698,9 @@ void list_files(std::string device_identifier, const char *application_identifie
694
698
{
695
699
print_error (errors.str ().c_str (), device_identifier, method_id, kAFCCustomError );
696
700
}
701
+
702
+ cleanup_file_resources (device_identifier, application_identifier);
703
+ list_files_mutex.unlock ();
697
704
}
698
705
699
706
bool ensure_device_path_exists (std::string &device_path, afc_connection *connection)
@@ -714,18 +721,22 @@ bool ensure_device_path_exists(std::string &device_path, afc_connection *connect
714
721
return true ;
715
722
}
716
723
724
+ std::mutex upload_file_mutex;
717
725
void upload_file (std::string device_identifier, const char *application_identifier, const std::vector<FileUploadData>& files, std::string method_id) {
718
726
json success_json = json ({ { kResponse , " Successfully uploaded files" },{ kId , method_id },{ kDeviceId , device_identifier } });
719
727
if (!files.size ())
720
728
{
721
729
print (success_json);
722
730
return ;
723
731
}
732
+
733
+ upload_file_mutex.lock ();
724
734
725
735
std::string afc_destination_str = windows_path_to_unix (files[0 ].destination );
726
736
afc_connection* afc_conn_p = get_afc_connection (device_identifier, application_identifier, afc_destination_str, method_id);
727
737
if (!afc_conn_p)
728
738
{
739
+ upload_file_mutex.unlock ();
729
740
// If there is no opened afc connection the get_afc_connection will print the error for the operation.
730
741
return ;
731
742
}
@@ -810,6 +821,8 @@ void upload_file(std::string device_identifier, const char *application_identifi
810
821
811
822
// After a batch is completed we need to empty file_upload_threads so that the new batch may take the old one's place
812
823
file_upload_threads.clear ();
824
+ cleanup_file_resources (device_identifier, application_identifier);
825
+ upload_file_mutex.unlock ();
813
826
}
814
827
815
828
std::vector<std::string> filtered_errors;
@@ -825,18 +838,29 @@ void upload_file(std::string device_identifier, const char *application_identifi
825
838
}
826
839
}
827
840
841
+ std::mutex delete_file_mutex;
828
842
void delete_file (std::string device_identifier, const char *application_identifier, const char *destination, std::string method_id) {
843
+ delete_file_mutex.lock ();
844
+
829
845
std::string destination_str = windows_path_to_unix (destination);
830
846
afc_connection* afc_conn_p = get_afc_connection (device_identifier, application_identifier, destination_str, method_id);
831
847
if (!afc_conn_p)
832
848
{
849
+ delete_file_mutex.unlock ();
833
850
return ;
834
851
}
835
852
836
853
destination = destination_str.c_str ();
837
854
std::stringstream error_message;
838
855
error_message << " Could not remove file " << destination;
839
- PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT (AFCRemovePath (afc_conn_p, destination), error_message.str ().c_str (), device_identifier, method_id);
856
+
857
+ unsigned afcRemovePathResult = AFCRemovePath (afc_conn_p, destination);
858
+
859
+ cleanup_file_resources (device_identifier, application_identifier);
860
+
861
+ delete_file_mutex.unlock ();
862
+
863
+ PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT (afcRemovePathResult, error_message.str ().c_str (), device_identifier, method_id);
840
864
print (json ({{ kResponse , " Successfully removed file" },{ kId , method_id },{ kDeviceId , device_identifier } }));
841
865
}
842
866
@@ -857,10 +881,14 @@ std::unique_ptr<afc_file> get_afc_file(std::string device_identifier, const char
857
881
return result;
858
882
}
859
883
884
+ std::mutex read_file_mutex;
860
885
void read_file (std::string device_identifier, const char *application_identifier, const char *path, std::string method_id, const char *destination = nullptr ) {
886
+ read_file_mutex.lock ();
887
+
861
888
std::unique_ptr<afc_file> file = get_afc_file (device_identifier, application_identifier, path, method_id);
862
889
if (!file)
863
890
{
891
+ read_file_mutex.unlock ();
864
892
return ;
865
893
}
866
894
@@ -897,8 +925,13 @@ void read_file(std::string device_identifier, const char *application_identifier
897
925
898
926
result = std::string (file_contents.begin (), file_contents.end ());
899
927
}
928
+
929
+ unsigned afcFileRefCloseResult = AFCFileRefClose (file->afc_conn_p , file->file_ref );
930
+ cleanup_file_resources (device_identifier, application_identifier);
931
+
932
+ read_file_mutex.unlock ();
900
933
901
- PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT (AFCFileRefClose (file-> afc_conn_p , file-> file_ref ) , " Could not close file reference" , device_identifier, method_id);
934
+ PRINT_ERROR_AND_RETURN_IF_FAILED_RESULT (afcFileRefCloseResult , " Could not close file reference" , device_identifier, method_id);
902
935
print (json ({{ kResponse , result },{ kId , method_id },{ kDeviceId , device_identifier } }));
903
936
}
904
937
@@ -974,9 +1007,9 @@ std::map<std::string, std::string> parse_cfdictionary(CFDictionaryRef dict)
974
1007
CFDictionaryGetKeysAndValues (dict, keys, values);
975
1008
for (size_t index = 0 ; index < count; index ++)
976
1009
{
977
- // The casts below are necessary - Xcode's compiler doesn't cope without them
978
- CFStringRef value = (CFStringRef )values[index ];
979
- CFStringRef key_str = (CFStringRef )keys[index ];
1010
+ // The casts below are necessary - Xcode's compiler doesn't cope without them
1011
+ CFStringRef value = (CFStringRef )values[index ];
1012
+ CFStringRef key_str = (CFStringRef )keys[index ];
980
1013
std::string key = get_cstring_from_cfstring (key_str);
981
1014
if (CFGetTypeID (value) == CFStringGetTypeID ())
982
1015
{
@@ -1002,9 +1035,9 @@ std::map<std::string, std::map<std::string, std::string>> parse_lookup_cfdiction
1002
1035
CFDictionaryGetKeysAndValues (dict, keys, values);
1003
1036
for (size_t index = 0 ; index < count; index ++)
1004
1037
{
1005
- CFStringRef key_str = (CFStringRef )keys[index ];
1006
- CFDictionaryRef value_dict = (CFDictionaryRef )values[index ];
1007
- std::string key = get_cstring_from_cfstring (key_str);
1038
+ CFStringRef key_str = (CFStringRef )keys[index ];
1039
+ CFDictionaryRef value_dict = (CFDictionaryRef )values[index ];
1040
+ std::string key = get_cstring_from_cfstring (key_str);
1008
1041
result[key] = parse_cfdictionary (value_dict);
1009
1042
}
1010
1043
0 commit comments