1010#include " utils.h"
1111
1212
13- int DeviceBridge::afc_upload_file (afc_client_t &afc, const QString &filename, const QString &dstfn, std::function<void (uint32_t ,uint32_t )> callback)
13+ int DeviceBridge::afc_upload_file (afc_client_t &afc, const QString &filename, const QString &dstfn, std::function<void (uint32_t ,uint32_t )> callback, std::function<bool()> should_stop )
1414{
1515 FILE *f = NULL ;
1616 uint64_t af = 0 ;
@@ -32,11 +32,22 @@ int DeviceBridge::afc_upload_file(afc_client_t &afc, const QString &filename, co
3232
3333 size_t amount = 0 ;
3434 do {
35+ if (should_stop && should_stop ()) {
36+ afc_file_close (afc, af);
37+ fclose (f);
38+ return -3 ;
39+ }
40+
3541 amount = fread (buf, 1 , sizeof (buf), f);
3642 if (amount > 0 ) {
3743 uint32_t written, total = 0 ;
3844 afc_error_t aerr = AFC_E_SUCCESS ;
3945 while (total < amount) {
46+ if (should_stop && should_stop ()) {
47+ afc_file_close (afc, af);
48+ fclose (f);
49+ return -3 ;
50+ }
4051 written = 0 ;
4152 aerr = afc_file_write (afc, af, buf, amount, &written);
4253 if (aerr != AFC_E_SUCCESS ) {
@@ -60,7 +71,7 @@ int DeviceBridge::afc_upload_file(afc_client_t &afc, const QString &filename, co
6071 return AFC_E_SUCCESS ;
6172}
6273
63- int DeviceBridge::afc_download_file (afc_client_t &afc, const QString &srcfn, const QString &dstfn, std::function<void (uint32_t ,uint32_t )> callback)
74+ int DeviceBridge::afc_download_file (afc_client_t &afc, const QString &srcfn, const QString &dstfn, std::function<void (uint32_t ,uint32_t )> callback, std::function<bool()> should_stop )
6475{
6576 FILE *f = NULL ;
6677 uint64_t af = 0 ;
@@ -97,6 +108,12 @@ int DeviceBridge::afc_download_file(afc_client_t &afc, const QString &srcfn, con
97108 afc_error_t aerr = AFC_E_SUCCESS ;
98109 uint32_t bytes_read = 0 ;
99110 do {
111+ if (should_stop && should_stop ()) {
112+ fclose (f);
113+ afc_file_close (afc, af);
114+ return -3 ;
115+ }
116+
100117 bytes_read = 0 ;
101118 aerr = afc_file_read (afc, af, buf, sizeof (buf), &bytes_read);
102119 if (aerr != AFC_E_SUCCESS ) {
@@ -120,7 +137,7 @@ int DeviceBridge::afc_download_file(afc_client_t &afc, const QString &srcfn, con
120137 return AFC_E_SUCCESS ;
121138}
122139
123- bool DeviceBridge::afc_upload_dir (afc_client_t &afc, const QString &path, const QString &afcpath, std::function<void (int ,int ,QString)> callback)
140+ bool DeviceBridge::afc_upload_dir (afc_client_t &afc, const QString &path, const QString &afcpath, std::function<void (int ,int ,QString)> callback, std::function<bool()> should_stop )
124141{
125142 QStringList list_dirs, list_files;
126143 QDir dirpath (path);
@@ -137,6 +154,9 @@ bool DeviceBridge::afc_upload_dir(afc_client_t &afc, const QString &path, const
137154 int total = list_dirs.count () + list_files.count ();
138155 foreach (QString dir_name, list_dirs)
139156 {
157+ if (should_stop && should_stop ())
158+ return false ;
159+
140160 idx++;
141161 QString targetpath = afcpath + " /" + dirpath.relativeFilePath (dir_name);
142162 if (callback) callback (idx, total, QString::asprintf (" Adding `%s' to device..." , targetpath.toUtf8 ().data ()));
@@ -149,6 +169,9 @@ bool DeviceBridge::afc_upload_dir(afc_client_t &afc, const QString &path, const
149169 }
150170 foreach (QString file_name, list_files)
151171 {
172+ if (should_stop && should_stop ())
173+ return false ;
174+
152175 idx++;
153176 QString targetpath = afcpath + " /" + dirpath.relativeFilePath (file_name);
154177 auto afc_callback = [&](uint32_t uploaded_bytes, uint32_t total_bytes)
@@ -158,7 +181,7 @@ bool DeviceBridge::afc_upload_dir(afc_client_t &afc, const QString &path, const
158181 BytesToString (total_bytes).toUtf8 ().data (),
159182 targetpath.toUtf8 ().data ()));
160183 };
161- int result = afc_upload_file (afc, file_name, targetpath, afc_callback);
184+ int result = afc_upload_file (afc, file_name, targetpath, afc_callback, should_stop );
162185 if (result != 0 )
163186 {
164187 if (callback) callback (idx, total, QString::asprintf (" Can't send `%s' to device : afc error code %d" , targetpath.toUtf8 ().data (), result));
@@ -168,8 +191,12 @@ bool DeviceBridge::afc_upload_dir(afc_client_t &afc, const QString &path, const
168191 return true ;
169192}
170193
171- int DeviceBridge::afc_count_recursive (afc_client_t afc, const char *path)
194+ int DeviceBridge::afc_count_recursive (afc_client_t afc, const char *path, std::function< bool ()> should_stop )
172195{
196+ if (should_stop && should_stop ()) {
197+ return 0 ;
198+ }
199+
173200 char **file_list = NULL ;
174201 int count = 0 ;
175202
@@ -178,6 +205,10 @@ int DeviceBridge::afc_count_recursive(afc_client_t afc, const char *path)
178205 }
179206
180207 for (int i = 0 ; file_list[i]; i++) {
208+ if (should_stop && should_stop ()) {
209+ break ;
210+ }
211+
181212 if (strcmp (file_list[i], " ." ) == 0 || strcmp (file_list[i], " .." ) == 0 ) continue ;
182213
183214 char full_path[2048 ];
@@ -202,22 +233,30 @@ int DeviceBridge::afc_count_recursive(afc_client_t afc, const char *path)
202233
203234 count++;
204235 if (is_dir) {
205- count += afc_count_recursive (afc, full_path);
236+ count += afc_count_recursive (afc, full_path, should_stop );
206237 }
207238 }
208239 afc_dictionary_free (file_list);
209240 return count;
210241}
211242
212- void DeviceBridge::afc_traverse_recursive (afc_client_t afc, const char *path, int * visited, int total, std::function<void (int ,int )> progress_cb)
243+ void DeviceBridge::afc_traverse_recursive (afc_client_t afc, const char *path, int * visited, int total, std::function<void (int ,int )> progress_cb, std::function<bool()> should_stop )
213244{
245+ if (should_stop && should_stop ()) {
246+ return ;
247+ }
248+
214249 char **file_list = NULL ;
215250
216251 if (afc_read_directory (afc, path, &file_list) != AFC_E_SUCCESS || !file_list) {
217252 return ;
218253 }
219254
220255 for (int i = 0 ; file_list[i]; i++) {
256+ if (should_stop && should_stop ()) {
257+ break ;
258+ }
259+
221260 if (strcmp (file_list[i], " ." ) == 0 || strcmp (file_list[i], " .." ) == 0 ) continue ;
222261
223262 char full_path[2048 ];
@@ -258,7 +297,7 @@ void DeviceBridge::afc_traverse_recursive(afc_client_t afc, const char *path, in
258297 }
259298
260299 if (is_dir) {
261- afc_traverse_recursive (afc, full_path, visited, total, progress_cb);
300+ afc_traverse_recursive (afc, full_path, visited, total, progress_cb, should_stop );
262301 }
263302 }
264303 afc_dictionary_free (file_list);
0 commit comments