Revision e4067290 trunk/Pithos.Core/Agents/PollAgent.cs

b/trunk/Pithos.Core/Agents/PollAgent.cs
503 503
            //Don't use the tuple info, it may have been deleted
504 504
            var localInfo = FileInfoExtensions.FromPath(localFilePath);
505 505

  
506

  
506 507
            // Local file unchanged? If both C and L are null, make sure it's because 
507 508
            //both the file is missing and the state checksum is not missing
508 509
            if (tuple.C == tuple.L && (localInfo.Exists || tuple.FileState==null))
......
512 513
                if (tuple.S == tuple.L)
513 514
                {
514 515
                    // No server changes
515
                    ;
516
                    //Has the file been renamed on the server?
517
                    MoveForServerMove(accountInfo, tuple);
516 518
                }
517 519
                else
518 520
                {
......
534 536
                        {
535 537
                            //Server file exists
536 538
                            //downloadServerObject() // Result: L = S
537
                            StatusKeeper.SetFileState(localFilePath, FileStatus.Modified,
539
                            //If the file has moved on the server, move it locally before downloading
540
                            var targetPath=MoveForServerMove(accountInfo,tuple);
541

  
542
                            StatusKeeper.SetFileState(targetPath, FileStatus.Modified,
538 543
                                                      FileOverlayStatus.Modified, "");
539 544
                            NetworkAgent.Downloader.DownloadCloudFile(accountInfo,
540 545
                                                                            tuple.ObjectInfo,
541
                                                                            localFilePath, token).Wait(token);
546
                                                                            targetPath, token).Wait(token);
542 547
                            //updateRecord( L = S )
543
                            StatusKeeper.UpdateFileChecksum(localFilePath, tuple.ObjectInfo.ETag,
548
                            StatusKeeper.UpdateFileChecksum(targetPath, tuple.ObjectInfo.ETag,
544 549
                                                            tuple.ObjectInfo.X_Object_Hash);
545 550

  
546
                            StatusKeeper.SetFileState(localFilePath, FileStatus.Unchanged,
551
                            StatusKeeper.StoreInfo(targetPath, tuple.ObjectInfo);
552

  
553
/*
554
                            StatusKeeper.SetFileState(targetPath, FileStatus.Unchanged,
547 555
                                                      FileOverlayStatus.Normal, "");
556
*/
548 557
                        }
549 558
                    }
550 559
                }
......
576 585
                                                               "Poll", isUnselected);
577 586
                            NetworkAgent.Uploader.UploadCloudFile(action, token).Wait(token);
578 587

  
579

  
580 588
                            //updateRecord( S = C )
581
                            StatusKeeper.SetFileState(localFilePath, FileStatus.Unchanged,
582
                                                      FileOverlayStatus.Normal, "");
589
                            //State updated by the uploader
590
                            
583 591
                            if (isUnselected)
584 592
                            {
585 593
                                ProcessChildren(accountInfo, tuple, agent, token);
......
595 603
                        {
596 604
                            // (Identical Changes) Result: L = S
597 605
                            //doNothing()
598
                            StatusKeeper.UpdateFileChecksum(localFilePath, tuple.ObjectInfo.ETag,
599
                                                            tuple.ObjectInfo.X_Object_Hash);
600
                            StatusKeeper.SetFileState(localFilePath, FileStatus.Unchanged,
601
                                                      FileOverlayStatus.Normal, "");
606
                            //Detect server moves
607
                            var targetPath=MoveForServerMove(accountInfo, tuple);
608
                            StatusKeeper.StoreInfo(targetPath,tuple.ObjectInfo);
602 609
                        }
603 610
                        else
604 611
                        {
......
619 626
            }
620 627
        }
621 628

  
629
        private string MoveForServerMove(AccountInfo accountInfo, StateTuple tuple)
630
        {
631
            var relativePath = tuple.ObjectInfo.RelativeUrlToFilePath(accountInfo.UserName);
632
            var serverPath = Path.Combine(accountInfo.AccountPath, relativePath);
633

  
634
            if (tuple.FilePath == serverPath) return serverPath;
635

  
636
            if (tuple.FileInfo.Exists)
637
            {                    
638
                var fi = tuple.FileInfo as FileInfo;
639
                if (fi != null)
640
                    fi.MoveTo(serverPath);
641
                var di = tuple.FileInfo as DirectoryInfo;
642
                if (di != null)
643
                    di.MoveTo(serverPath);
644
                StatusKeeper.StoreInfo(serverPath, tuple.ObjectInfo);
645
            }
646
            else
647
            {
648
                Debug.Assert(false, "File does not exist");
649
            }
650
            return serverPath;
651
        }
652

  
622 653
        private void DeleteCloudFile(AccountInfo accountInfo, StateTuple tuple)
623 654
        {
624 655
            StatusKeeper.SetFileState(tuple.FilePath, FileStatus.Deleted,
......
647 678
            IEnumerable<Tuple<FileSystemInfo, string>> files, 
648 679
            IEnumerable<FileState> states)
649 680
        {
650
            var dct = new Dictionary<string, StateTuple>();
681
            var tuplesByPath = new Dictionary<string, StateTuple>();
651 682
            foreach (var file in files)
652 683
            {
653 684
                var fsInfo = file.Item1;
654 685
                var fileHash = fsInfo is DirectoryInfo? MERKLE_EMPTY:file.Item2;
655 686

  
656
                dct[fsInfo.FullName] = new StateTuple {FileInfo = fsInfo, MD5 = fileHash};
687
                tuplesByPath[fsInfo.FullName] = new StateTuple {FileInfo = fsInfo, MD5 = fileHash};
657 688
            }
658 689
            foreach (var state in states)
659 690
            {
660 691
                StateTuple hashTuple;
661
                if (dct.TryGetValue(state.FilePath, out hashTuple))
692
                if (tuplesByPath.TryGetValue(state.FilePath, out hashTuple))
662 693
                {
663 694
                    hashTuple.FileState = state;
664 695
                }
665 696
                else
666 697
                {
667 698
                    var fsInfo = FileInfoExtensions.FromPath(state.FilePath);
668
                    dct[state.FilePath] = new StateTuple {FileInfo = fsInfo, FileState = state};
699
                    tuplesByPath[state.FilePath] = new StateTuple {FileInfo = fsInfo, FileState = state};
669 700
                }
670 701
            }
702

  
703
            var tuplesByID = tuplesByPath.Values
704
                .Where(tuple => tuple.FileState != null && tuple.FileState.ObjectID!=null)
705
                .ToDictionary(tuple=>tuple.FileState.ObjectID,tuple=>tuple);//new Dictionary<Guid, StateTuple>();
706

  
671 707
            foreach (var info in infos)
672 708
            {
673 709
                StateTuple hashTuple;
674 710
                var filePath = info.Item1;
675 711
                var objectInfo = info.Item2;
676
                if (dct.TryGetValue(filePath, out hashTuple))
712
                var objectID = objectInfo.UUID;
713

  
714
                if (tuplesByID.TryGetValue(objectID, out hashTuple))
715
                {
716
                    hashTuple.ObjectInfo = objectInfo;                    
717
                }
718
                else if (tuplesByPath.TryGetValue(filePath, out hashTuple))
677 719
                {
678 720
                    hashTuple.ObjectInfo = objectInfo;
679 721
                }
680 722
                else
681 723
                {
682 724
                    var fsInfo = FileInfoExtensions.FromPath(filePath);
683
                    dct[filePath] = new StateTuple {FileInfo = fsInfo, ObjectInfo = objectInfo};
725
                    var tuple = new StateTuple {FileInfo = fsInfo, ObjectInfo = objectInfo};
726
                    tuplesByPath[filePath] = tuple;
727
                    tuplesByID[objectInfo.UUID] = tuple;
684 728
                }
685 729
            }
686
            return dct.Values;
730
            return tuplesByPath.Values;
687 731
        }
688 732

  
689 733
        /// <summary>

Also available in: Unified diff