Revision cfed7823 trunk/Pithos.Core/Agents/BlockUpdater.cs

b/trunk/Pithos.Core/Agents/BlockUpdater.cs
22 22

  
23 23
        public string TempPath { get; private set; }
24 24

  
25
        readonly ConcurrentDictionary<int, string> _blocks = new ConcurrentDictionary<int, string>();
26
        readonly ConcurrentDictionary<string, string> _orphanBlocks = new ConcurrentDictionary<string, string>();
27

  
28
        [ContractInvariantMethod]
29
        private void Invariants()
30
        {
31
            Contract.Invariant(Path.IsPathRooted(FragmentsPath));
32
            Contract.Invariant(Path.IsPathRooted(FilePath));
33
            Contract.Invariant(Path.IsPathRooted(TempPath));
34
            Contract.Invariant(!Path.IsPathRooted(RelativePath));
35
            Contract.Invariant(_blocks!=null);
36
            Contract.Invariant(_orphanBlocks!=null);
37
            Contract.Invariant(ServerHash!=null);
38
        }
25 39

  
26 40
        public BlockUpdater(string fragmentsPath, string filePath, string relativePath,TreeHash serverHash)
27 41
        {   
......
50 64
            ServerHash = serverHash;
51 65
            //The file will be stored in a temporary location while downloading with an extension .download
52 66
            TempPath = Path.Combine(FragmentsPath, RelativePath + ".download");
53

  
54
            var directoryPath = Path.GetDirectoryName(TempPath);
67
            
68
            //Need to calculate the directory path because RelativePath may include folders
69
            var directoryPath = Path.GetDirectoryName(TempPath);            
70
            //directoryPath CAN be null if TempPath is a root path
71
            if (String.IsNullOrWhiteSpace(directoryPath))
72
                throw new ArgumentException("TempPath");
73
            //FragmentsPath was absolute so directoryPath is absolute too
74
            Contract.Assume(Path.IsPathRooted(directoryPath));
75
            
55 76
            if (!Directory.Exists(directoryPath))
56 77
                Directory.CreateDirectory(directoryPath);
57 78

  
......
64 85
                throw new ArgumentNullException("directoryPath");
65 86
            if (!Path.IsPathRooted(directoryPath))
66 87
                throw new ArgumentException("The directoryPath must be rooted", "directoryPath");
88
            if (ServerHash==null)
89
                throw new InvalidOperationException("ServerHash wasn't initialized");
67 90
            Contract.EndContractBlock();
68 91

  
69 92
            var fileNamename = Path.GetFileName(FilePath);
......
76 99
                    //The server truncates nulls before calculating hashes, have to do the same
77 100
                    //Find the last non-null byte, starting from the end
78 101
                    var lastByteIndex = Array.FindLastIndex(buffer, buffer.Length-1, aByte => aByte != 0);
79
                    var binHash = hasher.ComputeHash(buffer,0,lastByteIndex);
80
                    var hash = binHash.ToHashString();
81
                    _orphanBlocks[hash] = orphan;
102
                    //lastByteIndex may be -1 if the file was empty. We don't want to use that block file
103
                    if (lastByteIndex >= 0)
104
                    {
105
                        var binHash = hasher.ComputeHash(buffer, 0, lastByteIndex);
106
                        var hash = binHash.ToHashString();
107
                        _orphanBlocks[hash] = orphan;
108
                    }
82 109
                }
83 110
            }
84 111
        }
......
98 125
                File.Copy(FilePath, TempPath, true);
99 126

  
100 127
            //Set the size of the file to the size specified in the treehash
101
            //This will also create an empty file if the file doesn't exist            
128
            //This will also create an empty file if the file doesn't exist                        
129
            
130
            
102 131
            SetFileSize(TempPath, ServerHash.Bytes);
103 132

  
104 133
            //Update the temporary file with the data from the blocks
......
186 215
            return (tempLastWrite < localLastWrite);
187 216
        }*/
188 217

  
189
        ConcurrentDictionary<int,string> _blocks=new ConcurrentDictionary<int, string>();
190
        ConcurrentDictionary<string, string> _orphanBlocks = new ConcurrentDictionary<string, string>();
191 218

  
192 219
        public bool UseOrphan(int blockIndex, string blockHash)
193 220
        {

Also available in: Unified diff