36
36
#include "pupload.h"
37
37
#include "pfolder.h"
38
38
#include "pcallbacks.h"
39
+ #include "ppathstatus.h"
39
40
#include <string.h>
40
41
41
42
typedef struct {
@@ -374,14 +375,40 @@ static int compare_inode(const psync_list *l1, const psync_list *l2){
374
375
375
376
static void scan_rename_file (sync_folderlist * rnfr , sync_folderlist * rnto ){
376
377
psync_sql_res * res ;
378
+ psync_uint_row row ;
379
+ psync_folderid_t old_parentfolderid ;
380
+ psync_syncid_t old_syncid ;
381
+ int filetoupload ;
377
382
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
+ }
378
401
res = psync_sql_prep_statement ("UPDATE localfile SET localparentfolderid=?, syncid=?, name=? WHERE id=?" );
379
402
psync_sql_bind_uint (res , 1 , rnto -> localparentfolderid );
380
403
psync_sql_bind_uint (res , 2 , rnto -> syncid );
381
404
psync_sql_bind_string (res , 3 , rnto -> name );
382
405
psync_sql_bind_uint (res , 4 , rnfr -> localid );
383
406
psync_sql_run_free (res );
384
407
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
+ }
385
412
}
386
413
387
414
static void scan_upload_file (sync_folderlist * fl ){
@@ -406,6 +433,7 @@ static void scan_upload_file(sync_folderlist *fl){
406
433
return ;
407
434
localfileid = psync_sql_insertid ();
408
435
psync_task_upload_file_silent (fl -> syncid , localfileid , fl -> name );
436
+ psync_path_status_sync_folder_task_added (fl -> syncid , fl -> localparentfolderid );
409
437
}
410
438
411
439
static void scan_upload_modified_file (sync_folderlist * fl ){
@@ -420,6 +448,7 @@ static void scan_upload_modified_file(sync_folderlist *fl){
420
448
psync_sql_bind_uint (res , 5 , fl -> localid );
421
449
psync_sql_run_free (res );
422
450
psync_task_upload_file_silent (fl -> syncid , fl -> localid , fl -> name );
451
+ psync_path_status_sync_folder_task_added (fl -> syncid , fl -> localparentfolderid );
423
452
}
424
453
425
454
static void scan_delete_file (sync_folderlist * fl ){
@@ -512,8 +541,21 @@ static void scan_created_folder(sync_folderlist *fl){
512
541
513
542
static void scan_rename_folder (sync_folderlist * rnfr , sync_folderlist * rnto ){
514
543
psync_sql_res * res ;
515
- char * localpath ;
544
+ psync_uint_row row ;
545
+ // char *localpath;
516
546
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
+ }
517
559
res = psync_sql_prep_statement ("UPDATE localfolder SET localparentfolderid=?, syncid=?, name=? WHERE id=?" );
518
560
psync_sql_bind_uint (res , 1 , rnto -> localparentfolderid );
519
561
psync_sql_bind_uint (res , 2 , rnto -> syncid );
@@ -527,12 +569,14 @@ static void scan_rename_folder(sync_folderlist *rnfr, sync_folderlist *rnto){
527
569
psync_sql_bind_uint (res , 4 , rnfr -> syncid );
528
570
psync_sql_run_free (res );
529
571
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
+ *
530
574
localpath=psync_local_path_for_local_folder(rnfr->localid, rnto->syncid, NULL);
531
575
if (likely_log(localpath)){
532
- //TODO: this is probably run in transaction, so it may make sense not to run scan_folder here
533
576
scanner_scan_folder(localpath, rnfr->remoteid, rnfr->localid, rnto->syncid, rnto->synctype, rnto->deviceid);
534
577
psync_free(localpath);
535
578
}
579
+ */
536
580
}
537
581
538
582
static void delete_local_folder_rec (psync_folderid_t localfolderid ){
@@ -551,6 +595,11 @@ static void delete_local_folder_rec(psync_folderid_t localfolderid){
551
595
res = psync_sql_prep_statement ("DELETE FROM localfile WHERE localparentfolderid=?" );
552
596
psync_sql_bind_uint (res , 1 , localfolderid );
553
597
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 );
554
603
res = psync_sql_prep_statement ("DELETE FROM localfolder WHERE id=?" );
555
604
psync_sql_bind_uint (res , 1 , localfolderid );
556
605
psync_sql_run_free (res );
@@ -612,6 +661,7 @@ static void scanner_scan(int first){
612
661
sync_folderlist * fl ;
613
662
sync_list * l ;
614
663
psync_uint_t i , w , trn , restartsleep ;
664
+ int movedfolders ;
615
665
if (first )
616
666
localsleepperfolder = 0 ;
617
667
else {
@@ -638,6 +688,7 @@ static void scanner_scan(int first){
638
688
psync_list_init (& scan_lists [i ]);
639
689
scanner_set_syncs_to_list (& slist );
640
690
changes = 0 ;
691
+ movedfolders = 0 ;
641
692
psync_list_for_each_element (l , & slist , sync_list , list )
642
693
scanner_scan_folder (l -> localpath , l -> folderid , 0 , l -> syncid , l -> synctype , l -> deviceid );
643
694
psync_list_for_each_element_call (& slist , sync_list , list , psync_free );
@@ -682,6 +733,7 @@ static void scanner_scan(int first){
682
733
w ++ ;
683
734
check_for_query_cnt ();
684
735
}
736
+ movedfolders = 1 ;
685
737
psync_sql_commit_transaction ();
686
738
psync_list_init (& newtmp );
687
739
psync_list_for_each_safe (l1 , l2 , & scan_lists [SCAN_LIST_NEWFOLDERS ]){
@@ -741,13 +793,19 @@ static void scanner_scan(int first){
741
793
w ++ ;
742
794
check_for_query_cnt ();
743
795
}
796
+ psync_path_status_clear_sync_path_cache ();
744
797
psync_sql_commit_transaction ();
745
798
if (w ){
746
799
psync_wake_upload ();
747
800
psync_status_recalc_to_upload_async ();
748
801
}
749
802
for (i = 0 ; i < SCAN_LIST_CNT ; i ++ )
750
803
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
+ }
751
809
}
752
810
753
811
static int scanner_wait (){
@@ -771,7 +829,7 @@ static int scanner_wait(){
771
829
static void scanner_thread (){
772
830
time_t lastscan ;
773
831
int w ;
774
- psync_milisleep (25 );
832
+ psync_milisleep (1500 );
775
833
psync_wait_statuses_array (requiredstatuses , ARRAY_SIZE (requiredstatuses ));
776
834
psync_wait_status (PSTATUS_TYPE_RUN , PSTATUS_RUN_RUN |PSTATUS_RUN_PAUSE );
777
835
scanner_scan (1 );
0 commit comments