1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/****************************************************************************
 |http://www.koders.com/csharp/fid17630DC520D00C18D8844EEBC8AD8D31F6A8D574.aspx?s=md5
 | Copyright (c) 2007 Novell, Inc.
 | All Rights Reserved.
 |
 | This program is free software; you can redistribute it and/or
 | modify it under the terms of version 2 of the GNU General Public License as
 | published by the Free Software Foundation.
 |
 | This program is distributed in the hope that it will be useful,
 | but WITHOUT ANY WARRANTY; without even the implied warranty of
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | GNU General Public License for more details.
 |
 | You should have received a copy of the GNU General Public License
 | along with this program; if not, contact Novell, Inc.
 |
 | To contact Novell about this file by physical or electronic mail,
 | you may find current contact information at www.novell.com 
 |
 | Author: Russ Young
 |
 | Thanks to Andrew Tridgell for the rsync algorythm.
 | http://samba.anu.edu.au/rsync/tech_report/
 |***************************************************************************/


using System;
using System.IO;
using System.Collections;
using System.Threading;
using Simias.Storage;

namespace Simias.Sync.Delta
{
	#region HashData

	/// <summary>
	/// Class used to keep track of the file Blocks and hash
	/// codes assosiated with the block.
	/// </summary>
	public class HashData
	{
		/// <summary>
		/// The serialized size of the instance.
		/// </summary>
		public static int InstanceSize = 4 + 4 + 16;
		/// <summary>
		/// The Block number that this hash represents. 0 based.
		/// </summary>
		public int		BlockNumber;
		/// <summary>
		/// The Weak or quick hash of this block.
		/// </summary>
		public UInt32	WeakHash;
		/// <summary>
		/// The strong hash of this block.
		/// </summary>
		public byte[]	StrongHash;

		/// <summary>
		/// Constructs a HashData Object.
		/// </summary>
		/// <param name="blockNumber"></param>
		/// <param name="weakHash"></param>
		/// <param name="strongHash"></param>
		public HashData(int blockNumber, UInt32 weakHash, byte[] strongHash)
		{
			this.BlockNumber = blockNumber;
			this.WeakHash = weakHash;
			this.StrongHash = strongHash;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="reader"></param>
		public HashData(BinaryReader reader)
		{
			BlockNumber = reader.ReadInt32();
			WeakHash = reader.ReadUInt32();
			StrongHash = reader.ReadBytes(16);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		public void Serialize(BinaryWriter writer)
		{
			writer.Write(BlockNumber);
			writer.Write(WeakHash);
			writer.Write(StrongHash);
		}
	}

	#endregion

	#region HashMap

	/// <summary>
	/// Used to Write and Get HashMaps of a stream.
	/// </summary>
	public class HashMap
	{
		const string			MapFilePrefix = ".simias.map.";
		Collection				collection;
		BaseFileNode			node;
		string					file;
		static int				MaxThreadCount = 20;
		static int				ThreadCount = 0;
		static Queue			mapQ = new Queue();
		static AutoResetEvent	queueEvent = new AutoResetEvent(false);
		delegate void	HashMapDelegate();
		static int				version = 1;
		
		internal struct HashFileHeader
		{
			static byte[]	signature = {(byte)'!', (byte)'M', (byte)'a', (byte)'P', (byte)'f', (byte)'I', (byte)'l', (byte)'e'};
			static int		headerSize = 24;

			/// <summary>
			/// 
			/// </summary>
			/// <param name="reader"></param>
			/// <param name="blockSize"></param>
			/// <param name="entryCount"></param>
			/// <param name="nodeRev">The revision of the node.</param>
			/// <returns></returns>
			internal static bool ReadHeader(BinaryReader reader, out int blockSize, out int entryCount, ulong nodeRev)
			{
				byte[] sig = reader.ReadBytes(8);
				blockSize = reader.ReadInt32();
				int ver = reader.ReadInt32();
				ulong fileRev = reader.ReadUInt64();
				entryCount = 0;
				if (sig.Length == signature.Length)
				{
					for (int i= 0; i < sig.Length; ++i)
					{
						if (sig[i] != signature[i])
							return false;
					}
					if (version != ver)
						return false;
					if (fileRev != nodeRev)
						return false;
					entryCount = (int)((reader.BaseStream.Length - headerSize)/ HashData.InstanceSize);
					return true;
				}
				return false;
			}

			/// <summary>
			/// 
			/// </summary>
			/// <param name="writer"></param>
			/// <param name="blockSize"></param>
			/// <param name="nodeRev">The revision of the node.</param>
			internal static void WriteHeader(BinaryWriter writer, int blockSize, ulong nodeRev)
			{
				writer.Write(signature);
				writer.Write(blockSize);
				writer.Write(version);
				writer.Write(nodeRev);
			}
		}

		// We want to keep Map file size small enough that it will work over a 56Kb/Sec modem.
		// Lets try to get the file in 60 seconds.
		// In 60 seconds we can transfer 56k / 8 = 430,080 bytes of data.
		// In 60 seconds we can transfer 17920 blocks rount to 18000
		// We are assuming that larger files are less likely to change.
		static int maxBlocks = 18000;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="collection"></param>
		/// <param name="node"></param>
		internal HashMap(Collection collection, BaseFileNode node)
		{
			this.collection = collection;
			this.node = node;
			file = Path.Combine(collection.ManagedPath, MapFilePrefix + node.ID);
		}

		/// <summary>
		/// 
		/// </summary>
		private static void HashMapThread()
		{
			// BUGBUG Encryption Here.
			// Not needed map is generated on client.
			
		
			// Now see if we have any work queued.
			while (true)
			{
				// If we have had no work for 5 min exit thread.
				bool timedOut = !queueEvent.WaitOne(5 * 60 * 1000, false);
				try
				{
					while (true)
					{
						HashMapDelegate hmd;
						lock (mapQ)
						{
							hmd = mapQ.Count > 0 ? mapQ.Dequeue() as HashMapDelegate : null;
							if (hmd == null)
							{
								if (timedOut)
								{
									// Exit the thread.
									--ThreadCount;
									return;
								}
								break;
							}
						}
						try { hmd(); }
						catch { /* Don't let the thread go away.*/ }
					}
				}
				catch{}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void CreateHashMapFile()
		{
			FileStream mapSrcStream = File.Open(node.GetFullPath(collection), FileMode.Open, FileAccess.Read, FileShare.Read);	
			int blockSize = CalculateBlockSize(mapSrcStream.Length);
			try
			{
				string mapFile = file;
				string tmpMapFile = mapFile + ".tmp";
				// Copy the current file to a tmp name.
				if (File.Exists(mapFile))
					File.Move(mapFile, tmpMapFile);

				BinaryWriter writer = new BinaryWriter( File.OpenWrite(tmpMapFile));

				// Write the header.
				HashFileHeader.WriteHeader(writer, blockSize, node.LocalIncarnation);
				try
				{
					mapSrcStream.Position = 0;
					HashMap.SerializeHashMap(mapSrcStream, writer, blockSize);
					writer.Close();
					File.Move(tmpMapFile, mapFile);
					File.SetCreationTime(mapFile, node.CreationTime);
					File.SetLastWriteTime(mapFile, node.LastWriteTime);
				}
				catch (Exception ex)
				{
					writer.Close();
					writer = null;
					File.Delete(mapFile);
					if (File.Exists(tmpMapFile))
						File.Move(tmpMapFile, mapFile);
					throw ex;
				}
				finally
				{
					if (File.Exists(tmpMapFile))
						File.Delete(tmpMapFile);
				}
			}
			finally
			{
				// Close the file.
				mapSrcStream.Close();
			}
		}

		/// <summary>
		/// Calculate the block size to use for hash blocks
		/// </summary>
		/// <param name="streamSize">The size of the file.</param>
		/// <returns>The blockSize</returns>
		private static int CalculateBlockSize(long streamSize)
		{
			long size = streamSize / HashMap.maxBlocks;
			if (size < 0x1000)
				return 0x1000;
			if (size < 0x2000)
				return 0x2000;
			if (size < 0x4000)
				return 0x4000;
			if (size < 0x8000)
				return 0x8000;
			if (size < 0x10000)
				return 0x10000;
			if (size < 0x20000)
				return 0x20000;
			if (size < 0x40000)
				return 0x40000;
			return 0x80000;
		}


		/// <summary>
		/// 
		/// </summary>
		internal void CreateHashMap()
		{
			// Delete the file now.
			Delete();
			bool startThread = false;
			lock (mapQ)
			{
				mapQ.Enqueue(new HashMapDelegate(CreateHashMapFile));
				if (ThreadCount == 0 || (mapQ.Count > 1 && ThreadCount < MaxThreadCount))
				{
					// Startup a thread.
					startThread = true;
					++ThreadCount;
				}
			}
			if (startThread)
			{
				Thread thread = new Thread(new ThreadStart(HashMapThread));
				thread.IsBackground = true;
				thread.Start();
			}
			queueEvent.Set();
		}

		internal static void Delete(Collection collection, BaseFileNode node)
		{
			new HashMap(collection, node).Delete();
		}

		internal void Delete()
		{
			if (File.Exists(file))
				File.Delete(file);
		}

		/// <summary>
		/// Gets the array of HashMap
		/// </summary>
		/// <param name="entryCount">The number of hash entries.</param>
		/// <param name="blockSize">The size of the data blocks that were hashed.</param>
		/// <param name="create">If true create hashmap on error.</param>
		/// <param name="mapRev">The desired map revision.</param>
		/// <returns></returns>
		internal FileStream GetHashMapStream(out int entryCount, out int blockSize, bool create, ulong mapRev)
		{
			if (File.Exists(file))
			{
				FileStream stream = File.OpenRead(file);
				if (HashFileHeader.ReadHeader(new BinaryReader(stream), out blockSize, out entryCount, mapRev))
				{
					return stream;
				}
				stream.Close();
			}
			if (create)
				this.CreateHashMap();
			else
				Delete();

			entryCount = 0;
			blockSize = 0;
			return null;
		}

		/// <summary>
		/// Store the Hash Map
		/// </summary>
		public void StoreHashMapFile(Stream stream, int Size)
		{
			string mapFile = file;
			
			// Delete the existing file, any way we are going to store the new one
			if(File.Exists(mapFile))
				Delete();
			
			FileStream HashStream = File.OpenWrite(mapFile);
			try
			{
				StreamStream ss = new StreamStream(HashStream);
				ss.Write(stream, Size);
			}
			catch (Exception ex)
			{
				throw ex;
			}				
			finally
			{
				HashStream.Close();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="reader"></param>
		/// <param name="count"></param>
		/// <returns></returns>
		public static HashData[] DeSerializeHashMap(BinaryReader reader, int count)
		{
			HashData[] fileMap = new HashData[count];
			for (int i = 0; i < count; ++i)
			{
				fileMap[i] = new HashData(reader);
			}
			return fileMap;
		}
		
		/// <summary>
		/// Return the number of Blocks that the HashMap will need.
		/// </summary>
		/// <param name="streamSize"></param>
		/// <returns></returns>
		public static int GetBlockCount(long streamSize, out int blockSize)
		{
			blockSize = CalculateBlockSize(streamSize);
			return (int)((streamSize + blockSize -1)/ blockSize);
		}

		/// <summary>
		/// Serialized the Hash Created from the input stream to the writer stream.
		/// </summary>
		/// <param name="inStream">The stream of raw data to create the HashMap from.</param>
		/// <param name="writer">The stream to write the HashMap to.</param>
		/// <param name="blockSize">The size of the hashed data blocks.</param>
		public static void SerializeHashMap(Stream inStream, BinaryWriter writer, int blockSize)
		{
			//
			//if (inStream.Length <= blockSize)
			//{
			//	return;
			//}
			
			byte[]			buffer = new byte[blockSize];
			StrongHash		sh = new StrongHash();
			WeakHash		wh = new WeakHash();
			int				bytesRead;
			int				currentBlock = 0;
		
			// Compute the hash codes.
			inStream.Position = 0;
			while ((bytesRead = inStream.Read(buffer, 0, blockSize)) != 0)
			{
				new HashData(
					currentBlock++,
					wh.ComputeHash(buffer, 0, (UInt16)bytesRead),
					sh.ComputeHash(buffer, 0, bytesRead)).Serialize(writer);
			}
		}
	}
	
	#endregion
}

相关文章:

  • 2022-12-23
  • 2022-01-18
  • 2021-12-30
  • 2022-02-13
  • 2022-02-17
  • 2021-09-28
  • 2022-01-24
猜你喜欢
  • 2022-02-09
  • 2021-07-31
  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
相关资源
相似解决方案