Skip to content

Commit dffc818

Browse files
Merge pull request #42 from telerik/fatme/fix-afc-connections
Close afc connection after each file operation
2 parents 8ee6cbc + cfe6614 commit dffc818

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

IOSDeviceLib/IOSDeviceLib.cpp

+43-10
Original file line numberDiff line numberDiff line change
@@ -671,13 +671,17 @@ void read_dir(afc_connection* afc_conn_p, const char* dir, json &files, std::str
671671
}
672672
}
673673

674+
std::mutex list_files_mutex;
674675
void list_files(std::string device_identifier, const char *application_identifier, const char *device_path, std::string method_id)
675676
{
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);
678681
if (!afc_conn_p)
679682
{
680683
print_error("Could not establish AFC Connection", device_identifier, method_id);
684+
list_files_mutex.unlock();
681685
return;
682686
}
683687

@@ -694,6 +698,9 @@ void list_files(std::string device_identifier, const char *application_identifie
694698
{
695699
print_error(errors.str().c_str(), device_identifier, method_id, kAFCCustomError);
696700
}
701+
702+
cleanup_file_resources(device_identifier, application_identifier);
703+
list_files_mutex.unlock();
697704
}
698705

699706
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
714721
return true;
715722
}
716723

724+
std::mutex upload_file_mutex;
717725
void upload_file(std::string device_identifier, const char *application_identifier, const std::vector<FileUploadData>& files, std::string method_id) {
718726
json success_json = json({ { kResponse, "Successfully uploaded files" },{ kId, method_id },{ kDeviceId, device_identifier } });
719727
if (!files.size())
720728
{
721729
print(success_json);
722730
return;
723731
}
732+
733+
upload_file_mutex.lock();
724734

725735
std::string afc_destination_str = windows_path_to_unix(files[0].destination);
726736
afc_connection* afc_conn_p = get_afc_connection(device_identifier, application_identifier, afc_destination_str, method_id);
727737
if (!afc_conn_p)
728738
{
739+
upload_file_mutex.unlock();
729740
// If there is no opened afc connection the get_afc_connection will print the error for the operation.
730741
return;
731742
}
@@ -810,6 +821,8 @@ void upload_file(std::string device_identifier, const char *application_identifi
810821

811822
// After a batch is completed we need to empty file_upload_threads so that the new batch may take the old one's place
812823
file_upload_threads.clear();
824+
cleanup_file_resources(device_identifier, application_identifier);
825+
upload_file_mutex.unlock();
813826
}
814827

815828
std::vector<std::string> filtered_errors;
@@ -825,18 +838,29 @@ void upload_file(std::string device_identifier, const char *application_identifi
825838
}
826839
}
827840

841+
std::mutex delete_file_mutex;
828842
void delete_file(std::string device_identifier, const char *application_identifier, const char *destination, std::string method_id) {
843+
delete_file_mutex.lock();
844+
829845
std::string destination_str = windows_path_to_unix(destination);
830846
afc_connection* afc_conn_p = get_afc_connection(device_identifier, application_identifier, destination_str, method_id);
831847
if (!afc_conn_p)
832848
{
849+
delete_file_mutex.unlock();
833850
return;
834851
}
835852

836853
destination = destination_str.c_str();
837854
std::stringstream error_message;
838855
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);
840864
print(json({{ kResponse, "Successfully removed file" },{ kId, method_id },{ kDeviceId, device_identifier } }));
841865
}
842866

@@ -857,10 +881,14 @@ std::unique_ptr<afc_file> get_afc_file(std::string device_identifier, const char
857881
return result;
858882
}
859883

884+
std::mutex read_file_mutex;
860885
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+
861888
std::unique_ptr<afc_file> file = get_afc_file(device_identifier, application_identifier, path, method_id);
862889
if (!file)
863890
{
891+
read_file_mutex.unlock();
864892
return;
865893
}
866894

@@ -897,8 +925,13 @@ void read_file(std::string device_identifier, const char *application_identifier
897925

898926
result = std::string(file_contents.begin(), file_contents.end());
899927
}
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();
900933

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);
902935
print(json({{ kResponse, result },{ kId, method_id },{ kDeviceId, device_identifier } }));
903936
}
904937

@@ -974,9 +1007,9 @@ std::map<std::string, std::string> parse_cfdictionary(CFDictionaryRef dict)
9741007
CFDictionaryGetKeysAndValues(dict, keys, values);
9751008
for (size_t index = 0; index < count; index++)
9761009
{
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];
9801013
std::string key = get_cstring_from_cfstring(key_str);
9811014
if (CFGetTypeID(value) == CFStringGetTypeID())
9821015
{
@@ -1002,9 +1035,9 @@ std::map<std::string, std::map<std::string, std::string>> parse_lookup_cfdiction
10021035
CFDictionaryGetKeysAndValues(dict, keys, values);
10031036
for (size_t index = 0; index < count; index++)
10041037
{
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);
10081041
result[key] = parse_cfdictionary(value_dict);
10091042
}
10101043

0 commit comments

Comments
 (0)