Skip to content

Commit 53bf2ae

Browse files
author
virco
committed
implement folder statuses for syncs
1 parent d815803 commit 53bf2ae

File tree

9 files changed

+758
-87
lines changed

9 files changed

+758
-87
lines changed

pdownload.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "plocalscan.h"
4444
#include "pupload.h"
4545
#include "pasyncnet.h"
46+
#include "ppathstatus.h"
4647

4748
typedef struct {
4849
psync_list list;
@@ -270,14 +271,14 @@ static int call_func_for_folder_name(psync_folderid_t localfolderid, psync_folde
270271
return res;
271272
}
272273

273-
static void delete_local_folder_from_db(psync_folderid_t localfolderid){
274+
static void delete_local_folder_from_db(psync_folderid_t localfolderid, psync_syncid_t syncid){
274275
psync_sql_res *res;
275276
psync_uint_row row;
276277
if (likely(localfolderid)){
277-
res=psync_sql_query("SELECT id FROM localfolder WHERE localparentfolderid=?");
278+
res=psync_sql_query("SELECT id, syncid FROM localfolder WHERE localparentfolderid=?");
278279
psync_sql_bind_uint(res, 1, localfolderid);
279280
while ((row=psync_sql_fetch_rowint(res)))
280-
delete_local_folder_from_db(row[0]);
281+
delete_local_folder_from_db(row[0], row[1]);
281282
psync_sql_free_result(res);
282283
res=psync_sql_query("SELECT id FROM localfile WHERE localparentfolderid=?");
283284
psync_sql_bind_uint(res, 1, localfolderid);
@@ -291,12 +292,14 @@ static void delete_local_folder_from_db(psync_folderid_t localfolderid){
291292
psync_sql_bind_uint(res, 1, localfolderid);
292293
psync_sql_run_free(res);
293294
}
295+
psync_path_status_sync_folder_deleted(syncid, localfolderid);
294296
}
295297

296298
static int task_renamefolder(psync_syncid_t newsyncid, psync_folderid_t folderid, psync_folderid_t localfolderid,
297299
psync_folderid_t newlocalparentfolderid, const char *newname){
298300
psync_sql_res *res;
299301
psync_variant_row row;
302+
psync_uint_row urow;
300303
char *oldpath, *newpath;
301304
psync_syncid_t oldsyncid;
302305
int ret;
@@ -324,6 +327,15 @@ static int task_renamefolder(psync_syncid_t newsyncid, psync_folderid_t folderid
324327
}
325328
psync_sql_start_transaction();
326329
psync_restart_localscan();
330+
res=psync_sql_query_nolock("SELECT syncid, localparentfolderid FROM localfolder WHERE id=?");
331+
psync_sql_bind_uint(res, 1, localfolderid);
332+
if ((urow=psync_sql_fetch_rowint(res))) {
333+
psync_path_status_sync_folder_moved(localfolderid, urow[0], urow[1], newsyncid, newlocalparentfolderid);
334+
psync_sql_free_result(res);
335+
} else {
336+
psync_sql_free_result(res);
337+
debug(D_NOTICE, "localfolderid %u not found in localfolder", (unsigned)localfolderid);
338+
}
327339
res=psync_sql_prep_statement("UPDATE localfolder SET syncid=?, localparentfolderid=?, name=? WHERE id=?");
328340
psync_sql_bind_uint(res, 1, newsyncid);
329341
psync_sql_bind_uint(res, 2, newlocalparentfolderid);
@@ -1019,8 +1031,10 @@ static void task_run_download_file_thread(void *ptr){
10191031
set_task_inprogress(dt->taskid, 0);
10201032
psync_wake_download();
10211033
}
1022-
else
1034+
else{
10231035
delete_task(dt->taskid);
1036+
psync_path_status_sync_folder_task_completed(dt->dwllist.syncid, dt->localfolderid);
1037+
}
10241038
free_download_task(dt);
10251039
psync_status_recalc_to_download_async();
10261040
}
@@ -1208,6 +1222,7 @@ static void task_del_folder_rec_do(const char *localpath, psync_folderid_t local
12081222
psync_sql_bind_uint(res, 1, localfolderid);
12091223
psync_sql_bind_uint(res, 2, syncid);
12101224
psync_sql_run_free(res);
1225+
psync_path_status_sync_folder_deleted(syncid, localfolderid);
12111226
}
12121227

12131228
static int task_del_folder_rec(psync_folderid_t localfolderid, psync_folderid_t folderid, psync_syncid_t syncid){
@@ -1244,7 +1259,7 @@ static int download_task(uint64_t taskid, uint32_t type, psync_syncid_t syncid,
12441259
res=call_func_for_folder_name(localitemid, itemid, name, syncid, PEVENT_LOCAL_FOLDER_DELETED, task_rmdir, 0, "local folder deleted");
12451260
if (!res){
12461261
psync_sql_start_transaction();
1247-
delete_local_folder_from_db(localitemid);
1262+
delete_local_folder_from_db(localitemid, syncid);
12481263
psync_sql_commit_transaction();
12491264
}
12501265
break;
@@ -1292,8 +1307,10 @@ static void download_thread(){
12921307
psync_get_string_or_null(row[6]),
12931308
psync_get_number_or_null(row[7]))){
12941309
delete_task(taskid);
1295-
if (type==PSYNC_DOWNLOAD_FILE)
1296-
psync_status_recalc_to_download_async();
1310+
if (type==PSYNC_DOWNLOAD_FILE){
1311+
psync_status_recalc_to_download_async();
1312+
psync_path_status_sync_folder_task_completed(psync_get_number(row[2]), psync_get_number(row[4]));
1313+
}
12971314
}
12981315
else if (type!=PSYNC_DOWNLOAD_FILE)
12991316
psync_milisleep(PSYNC_SLEEP_ON_FAILED_DOWNLOAD);

plocalscan.c

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "pupload.h"
3737
#include "pfolder.h"
3838
#include "pcallbacks.h"
39+
#include "ppathstatus.h"
3940
#include <string.h>
4041

4142
typedef struct {
@@ -374,14 +375,40 @@ static int compare_inode(const psync_list *l1, const psync_list *l2){
374375

375376
static void scan_rename_file(sync_folderlist *rnfr, sync_folderlist *rnto){
376377
psync_sql_res *res;
378+
psync_uint_row row;
379+
psync_folderid_t old_parentfolderid;
380+
psync_syncid_t old_syncid;
381+
int filetoupload;
377382
debug(D_NOTICE, "file renamed from %s to %s", rnfr->name, rnto->name);
383+
res=psync_sql_query_nolock("SELECT syncid, localparentfolderid FROM localfile WHERE id=?");
384+
psync_sql_bind_uint(res, 1, rnfr->localid);
385+
if ((row=psync_sql_fetch_rowint(res))) {
386+
old_syncid=row[0];
387+
old_parentfolderid=row[1];
388+
psync_sql_free_result(res);
389+
if (rnto->syncid!=old_syncid || rnto->localparentfolderid!=old_parentfolderid) {
390+
res=psync_sql_query_nolock("SELECT 1 FROM task WHERE type="NTO_STR(PSYNC_UPLOAD_FILE)" AND localitemid=?");
391+
psync_sql_bind_uint(res, 1, rnfr->localid);
392+
filetoupload=!!psync_sql_fetch_rowint(res);
393+
psync_sql_free_result(res);
394+
} else {
395+
filetoupload=0;
396+
}
397+
} else {
398+
psync_sql_free_result(res);
399+
return;
400+
}
378401
res=psync_sql_prep_statement("UPDATE localfile SET localparentfolderid=?, syncid=?, name=? WHERE id=?");
379402
psync_sql_bind_uint(res, 1, rnto->localparentfolderid);
380403
psync_sql_bind_uint(res, 2, rnto->syncid);
381404
psync_sql_bind_string(res, 3, rnto->name);
382405
psync_sql_bind_uint(res, 4, rnfr->localid);
383406
psync_sql_run_free(res);
384407
psync_task_rename_remote_file(rnfr->syncid, rnto->syncid, rnfr->localid, rnto->localparentfolderid, rnto->name);
408+
if (filetoupload) {
409+
psync_path_status_sync_folder_task_added_locked(rnto->syncid, rnto->localparentfolderid);
410+
psync_path_status_sync_folder_task_completed(old_syncid, old_parentfolderid);
411+
}
385412
}
386413

387414
static void scan_upload_file(sync_folderlist *fl){
@@ -406,6 +433,7 @@ static void scan_upload_file(sync_folderlist *fl){
406433
return;
407434
localfileid=psync_sql_insertid();
408435
psync_task_upload_file_silent(fl->syncid, localfileid, fl->name);
436+
psync_path_status_sync_folder_task_added(fl->syncid, fl->localparentfolderid);
409437
}
410438

411439
static void scan_upload_modified_file(sync_folderlist *fl){
@@ -420,6 +448,7 @@ static void scan_upload_modified_file(sync_folderlist *fl){
420448
psync_sql_bind_uint(res, 5, fl->localid);
421449
psync_sql_run_free(res);
422450
psync_task_upload_file_silent(fl->syncid, fl->localid, fl->name);
451+
psync_path_status_sync_folder_task_added(fl->syncid, fl->localparentfolderid);
423452
}
424453

425454
static void scan_delete_file(sync_folderlist *fl){
@@ -512,8 +541,21 @@ static void scan_created_folder(sync_folderlist *fl){
512541

513542
static void scan_rename_folder(sync_folderlist *rnfr, sync_folderlist *rnto){
514543
psync_sql_res *res;
515-
char *localpath;
544+
psync_uint_row row;
545+
// char *localpath;
516546
debug(D_NOTICE, "folder renamed from %s to %s", rnfr->name, rnto->name);
547+
res=psync_sql_query_nolock("SELECT syncid, localparentfolderid FROM localfolder WHERE id=?");
548+
psync_sql_bind_uint(res, 1, rnfr->localid);
549+
if ((row=psync_sql_fetch_rowint(res))) {
550+
psync_path_status_sync_folder_moved(rnfr->localid, row[0], row[1], rnto->syncid, rnto->localparentfolderid);
551+
psync_sql_free_result(res);
552+
} else {
553+
psync_sql_free_result(res);
554+
debug(D_NOTICE, "localfolderid %u not found in localfolder", (unsigned)rnfr->localid);
555+
// This can prorably happen if we race with a task to delete the folder that comes from the download thread.
556+
// In any case it is safe not to do anything as we are going to restart the scan anyway
557+
return;
558+
}
517559
res=psync_sql_prep_statement("UPDATE localfolder SET localparentfolderid=?, syncid=?, name=? WHERE id=?");
518560
psync_sql_bind_uint(res, 1, rnto->localparentfolderid);
519561
psync_sql_bind_uint(res, 2, rnto->syncid);
@@ -527,12 +569,14 @@ static void scan_rename_folder(sync_folderlist *rnfr, sync_folderlist *rnto){
527569
psync_sql_bind_uint(res, 4, rnfr->syncid);
528570
psync_sql_run_free(res);
529571
psync_task_rename_remote_folder(rnfr->syncid, rnto->syncid, rnfr->localid, rnto->localparentfolderid, rnto->name);
572+
/* remove the scan from here and restart the full scan once we finished with current update makes more sense when we found moved folders
573+
*
530574
localpath=psync_local_path_for_local_folder(rnfr->localid, rnto->syncid, NULL);
531575
if (likely_log(localpath)){
532-
//TODO: this is probably run in transaction, so it may make sense not to run scan_folder here
533576
scanner_scan_folder(localpath, rnfr->remoteid, rnfr->localid, rnto->syncid, rnto->synctype, rnto->deviceid);
534577
psync_free(localpath);
535578
}
579+
*/
536580
}
537581

538582
static void delete_local_folder_rec(psync_folderid_t localfolderid){
@@ -551,6 +595,11 @@ static void delete_local_folder_rec(psync_folderid_t localfolderid){
551595
res=psync_sql_prep_statement("DELETE FROM localfile WHERE localparentfolderid=?");
552596
psync_sql_bind_uint(res, 1, localfolderid);
553597
psync_sql_run_free(res);
598+
res=psync_sql_query("SELECT syncid FROM localfolder WHERE id=?");
599+
psync_sql_bind_uint(res, 1, localfolderid);
600+
if (row)
601+
psync_path_status_sync_folder_deleted(row[0], localfolderid);
602+
psync_sql_free_result(res);
554603
res=psync_sql_prep_statement("DELETE FROM localfolder WHERE id=?");
555604
psync_sql_bind_uint(res, 1, localfolderid);
556605
psync_sql_run_free(res);
@@ -612,6 +661,7 @@ static void scanner_scan(int first){
612661
sync_folderlist *fl;
613662
sync_list *l;
614663
psync_uint_t i, w, trn, restartsleep;
664+
int movedfolders;
615665
if (first)
616666
localsleepperfolder=0;
617667
else{
@@ -638,6 +688,7 @@ static void scanner_scan(int first){
638688
psync_list_init(&scan_lists[i]);
639689
scanner_set_syncs_to_list(&slist);
640690
changes=0;
691+
movedfolders=0;
641692
psync_list_for_each_element(l, &slist, sync_list, list)
642693
scanner_scan_folder(l->localpath, l->folderid, 0, l->syncid, l->synctype, l->deviceid);
643694
psync_list_for_each_element_call(&slist, sync_list, list, psync_free);
@@ -682,6 +733,7 @@ static void scanner_scan(int first){
682733
w++;
683734
check_for_query_cnt();
684735
}
736+
movedfolders=1;
685737
psync_sql_commit_transaction();
686738
psync_list_init(&newtmp);
687739
psync_list_for_each_safe(l1, l2, &scan_lists[SCAN_LIST_NEWFOLDERS]){
@@ -741,13 +793,19 @@ static void scanner_scan(int first){
741793
w++;
742794
check_for_query_cnt();
743795
}
796+
psync_path_status_clear_sync_path_cache();
744797
psync_sql_commit_transaction();
745798
if (w){
746799
psync_wake_upload();
747800
psync_status_recalc_to_upload_async();
748801
}
749802
for (i=0; i<SCAN_LIST_CNT; i++)
750803
psync_list_for_each_element_call(&scan_lists[i], sync_folderlist, list, psync_free);
804+
if (movedfolders) {
805+
starttime=psync_current_time;
806+
restartsleep=1000;
807+
goto restart;
808+
}
751809
}
752810

753811
static int scanner_wait(){
@@ -771,7 +829,7 @@ static int scanner_wait(){
771829
static void scanner_thread(){
772830
time_t lastscan;
773831
int w;
774-
psync_milisleep(25);
832+
psync_milisleep(1500);
775833
psync_wait_statuses_array(requiredstatuses, ARRAY_SIZE(requiredstatuses));
776834
psync_wait_status(PSTATUS_TYPE_RUN, PSTATUS_RUN_RUN|PSTATUS_RUN_PAUSE);
777835
scanner_scan(1);

0 commit comments

Comments
 (0)