Revision 692ec33b trunk/Pithos.Core/Agents/NetworkAgent.cs

b/trunk/Pithos.Core/Agents/NetworkAgent.cs
478 478
            var fileAgent = GetFileAgent(accountInfo);
479 479
            foreach (var trashObject in trashObjects)
480 480
            {
481
                var relativePath = trashObject.RelativeUrlToFilePath(accountInfo.UserName);
482
                //and remove any matching objects from the list, adding them to the commonObjects list
481
                var barePath = trashObject.RelativeUrlToFilePath(accountInfo.UserName);
482
                //HACK: Assume only the "pithos" container is used. Must find out what happens when
483
                //deleting a file from a different container
484
                var relativePath = Path.Combine("pithos", barePath);
483 485
                fileAgent.Delete(relativePath);                                
484 486
            }
485 487
        }
......
747 749
            try
748 750
            {
749 751
                if (action == null)
750
                    throw new ArgumentNullException("action");            
752
                    throw new ArgumentNullException("action");
751 753
                Contract.EndContractBlock();
752 754

  
753
                var accountInfo=action.AccountInfo;
754
            
755
                var fileInfo=action.LocalFile;                        
755
                var accountInfo = action.AccountInfo;
756

  
757
                var fileInfo = action.LocalFile;
756 758

  
757 759
                if (fileInfo.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase))
758 760
                    return;
......
760 762
                var relativePath = fileInfo.AsRelativeTo(accountInfo.AccountPath);
761 763
                if (relativePath.StartsWith(FolderConstants.OthersFolder))
762 764
                {
763
                    var parts=relativePath.Split('\\');
765
                    var parts = relativePath.Split('\\');
764 766
                    var accountName = parts[1];
765 767
                    var oldName = accountInfo.UserName;
766 768
                    var absoluteUri = accountInfo.StorageUri.AbsoluteUri;
767
                    var nameIndex=absoluteUri.IndexOf(oldName);
768
                    var root=absoluteUri.Substring(0, nameIndex);
769
                    var nameIndex = absoluteUri.IndexOf(oldName);
770
                    var root = absoluteUri.Substring(0, nameIndex);
769 771

  
770 772
                    accountInfo = new AccountInfo
771 773
                    {
772 774
                        UserName = accountName,
773 775
                        AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
774 776
                        StorageUri = new Uri(root + accountName),
775
                        BlockHash=accountInfo.BlockHash,
776
                        BlockSize=accountInfo.BlockSize,
777
                        Token=accountInfo.Token
777
                        BlockHash = accountInfo.BlockHash,
778
                        BlockSize = accountInfo.BlockSize,
779
                        Token = accountInfo.Token
778 780
                    };
779 781
                }
780 782

  
781 783

  
782 784
                var fullFileName = fileInfo.FullName;
783
                using(var gate=NetworkGate.Acquire(fullFileName,NetworkOperation.Uploading))
785
                using (var gate = NetworkGate.Acquire(fullFileName, NetworkOperation.Uploading))
784 786
                {
785 787
                    //Abort if the file is already being uploaded or downloaded
786 788
                    if (gate.Failed)
......
791 793

  
792 794
                    var client = new CloudFilesClient(accountInfo);
793 795
                    //Even if GetObjectInfo times out, we can proceed with the upload            
794
                    var info = client.GetObjectInfo(account, cloudFile.Container, cloudFile.Name);                
796
                    var info = client.GetObjectInfo(account, cloudFile.Container, cloudFile.Name);
795 797
                    var cloudHash = info.Hash.ToLower();
796 798

  
797 799
                    var hash = action.LocalHash.Value;
798 800
                    var topHash = action.TopHash.Value;
799 801

  
800 802
                    //If the file hashes match, abort the upload
801
                    if (hash == cloudHash  || topHash ==cloudHash)
803
                    if (hash == cloudHash || topHash == cloudHash)
802 804
                    {
803 805
                        //but store any metadata changes 
804 806
                        this.StatusKeeper.StoreInfo(fullFileName, info);
......
806 808
                        return;
807 809
                    }
808 810

  
809
                    if (info.AllowedTo=="read")
811
                    if (info.AllowedTo == "read")
810 812
                        return;
811 813

  
812 814
                    //Mark the file as modified while we upload it
......
818 820

  
819 821
                    //First, calculate the tree hash
820 822
                    var treeHash = await Signature.CalculateTreeHashAsync(fileInfo.FullName, accountInfo.BlockSize,
821
                        accountInfo.BlockHash);                
822
                    
823
                    await UploadWithHashMap(accountInfo,cloudFile,fileInfo,cloudFile.Name,treeHash);
823
                        accountInfo.BlockHash);
824

  
825
                    await UploadWithHashMap(accountInfo, cloudFile, fileInfo, cloudFile.Name, treeHash);
824 826

  
825 827
                    //If everything succeeds, change the file and overlay status to normal
826 828
                    this.StatusKeeper.SetFileState(fullFileName, FileStatus.Unchanged, FileOverlayStatus.Normal);
......
834 836
                var exc = ex.InnerException as WebException;
835 837
                if (exc == null)
836 838
                    throw ex.InnerException;
837
                var response = exc.Response as HttpWebResponse;
838
                if (response == null)
839
                    throw exc;
840
                if (response.StatusCode == HttpStatusCode.Unauthorized)
841
                {
842
                    Log.Error("Not allowed to upload file", exc);
843
                    var message = String.Format("Not allowed to uplad file {0}", action.LocalFile.FullName);
844
                    StatusKeeper.SetFileState(action.LocalFile.FullName, FileStatus.Unchanged, FileOverlayStatus.Normal);
845
                    StatusNotification.NotifyChange(message, TraceLevel.Warning);
839
                if (HandleUploadWebException(action, exc)) 
846 840
                    return;
847
                }
841
                throw;
842
            }
843
            catch (WebException ex)
844
            {
845
                if (HandleUploadWebException(action, ex))
846
                    return;
847
                throw;
848
            }
849
            catch (Exception ex)
850
            {
851
                Log.Error("Unexpected error while uploading file", ex);
848 852
                throw;
849 853
            }
850 854

  
851 855
        }
852 856

  
857
        private bool HandleUploadWebException(CloudAction action, WebException exc)
858
        {
859
            var response = exc.Response as HttpWebResponse;
860
            if (response == null)
861
                throw exc;
862
            if (response.StatusCode == HttpStatusCode.Unauthorized)
863
            {
864
                Log.Error("Not allowed to upload file", exc);
865
                var message = String.Format("Not allowed to uplad file {0}", action.LocalFile.FullName);
866
                StatusKeeper.SetFileState(action.LocalFile.FullName, FileStatus.Unchanged, FileOverlayStatus.Normal);
867
                StatusNotification.NotifyChange(message, TraceLevel.Warning);
868
                return true;
869
            }
870
            return false;
871
        }
872

  
853 873
        public async Task UploadWithHashMap(AccountInfo accountInfo,ObjectInfo cloudFile,FileInfo fileInfo,string url,TreeHash treeHash)
854 874
        {
855 875
            if (accountInfo == null)

Also available in: Unified diff