Rayburn Davis
2014-09-15 fb4519e5c864e7c153b6bafcf79275ed36a9bda3
commit | author | age
344044 1 ´╗┐using System;
RD 2 using System.Collections;
3 using System.IO;
4 using System.Collections.Generic;
5 using System.Text;
6 using System.Data;
7 using Microsoft.VisualBasic.Devices;
8 using System.Runtime.InteropServices;
9 using System.Security.Cryptography;
10 using System.Windows.Forms;
11 using System.Drawing;
12 using System.Threading;
13 using System.Net.Sockets;
14 using System.Threading.Tasks;
fb4519 15 using DupliDelete.Duplicate_Search_Engine;
344044 16
RD 17 namespace DupliDelete
18 {
19     public class DuplicateFinder
20     {
21
22         public int Progress;
23         public ProgressStatusMode Status;
24
25         public bool FileChecksumScanInProgress = false;
26         public double FileChecksumProgress;
27         public ObjectInfo FileChecksumObject;
28
fb4519 29         public Folder[] Folders;
344044 30         public List<string> FileTypeInclusions = new List<string>();
RD 31         public List<string> ExcludedFolders = new List<string>();
32         public List<string> ExcludedFiletypes = new List<string>();
33         public Mode ScanMode = Mode.CompareByFilename;
34         public bool CompareBySize = false, CompareByExtension = false, CompareByModified = false;
35         public bool FileSizeBracket = false;
36         public long MinimumSize;
37         public long MaximumSize;
38
25f449 39        // SizeInfoCollection FileList;
RD 40         Dictionary<long, SizeInfo> FileList;
344044 41
RD 42         public SizeIndexTracker SizeIndexTracker = new SizeIndexTracker();
43
44         public List<String> CachedFolders = new List<string>();
45
46         public int FoundFolders = 0;
47         public int FoundFiles = 0;
48
49         public bool Canceled = false;
50
51         public bool IgnoreLinks;
52
53         public bool IncludeSubfolders = true;
54
55         public int FileReadPercentage = 100;
56
57         public TimeSpan ScanTime;
58
59         SHA1 sha_handle;
60         byte[] checksum_data = new byte[2097152];
61
62         public ErrorClass Errors;
63
64         public delegate void ScanCompleteDelegate(ScanCompleteArgs e);
65
66         public event ScanCompleteDelegate ScanComplete;
67
68         public class ScanCompleteArgs : EventArgs
69         {
70             public long DuplicateCount;
71
72             public ScanCompleteArgs(long dup_count)
73             {
74                 DuplicateCount = dup_count;
75             }
76         }
77
78         public enum Mode
79         {
80             CompareByFilename=1, CompareByActualData=2
81         }
82
83         public enum ProgressStatusMode
84         {
85             StartingRemoteScan=1, WaitingOnRemoteScan=2, ReceivingScanResult=3, ProcessingRemoteData=4, CachingFolders=5, CachingFiles=6, 
86             IdentifyingDuplicates=7, SendingScanResult=8, Finished=9
87         }
88
89         public enum ChecksumReadStatus
90         {
91             AccessDenied, FileNotFound, UnknownError, Success
92         }
93
94         public struct FILETIME
95         {
96             int dwLowDateTime;
97             int dwHighDateTime;
98         }
99         public struct BY_HANDLE_FILE_INFORMATION
100         {
101             public int FileAttributes;
102             public FILETIME CreationTime;
103             public FILETIME LastAccessTime;
104             public FILETIME LastWriteTime;
105             public int VolumeSerialNumber;
106             public int nFileSizeHigh;
107             public int nFileSizeLow;
108             public int NumberOfLinks;
109             public int FileIndexHigh;
110             public int FileIndexLow;
111         }
112
113         [DllImport("Kernel32.dll")]
114         static extern bool GetFileInformationByHandle(IntPtr hFile, out BY_HANDLE_FILE_INFORMATION by1);
115
fb4519 116         public class DuplicateSorter : System.Collections.IComparer
344044 117         {
fb4519 118             DuplicateFinder Finder { get; set; }
RD 119
120             public DuplicateSorter(DuplicateFinder finder)
121             {
122                 Finder = finder;
123             }
124
125             public int Compare(object x, object y)
126             {
127                 if (Finder.ScanMode == Mode.CompareByActualData)
128                 {
129                     string sizeStringX = ((DuplicateGroup)x).GroupName.Substring(0, ((DuplicateGroup)x).GroupName.IndexOf(" "));
130                     string sizeStringY = ((DuplicateGroup)y).GroupName.Substring(0, ((DuplicateGroup)y).GroupName.IndexOf(" "));
131                     string metricX = ((DuplicateGroup)x).GroupName.Substring(((DuplicateGroup)x).GroupName.IndexOf(" ") + 1);
132                     string metricY = ((DuplicateGroup)y).GroupName.Substring(((DuplicateGroup)y).GroupName.IndexOf(" ") + 1);
133                     double sizeX = Convert.ToDouble(sizeStringX);
134                     double sizeY = Convert.ToDouble(sizeStringY);
135
136                     if (metricX == "bytes")
137                         sizeX = shortSizeToLongSize(sizeX, 1);
138                     if (metricX == "KB")
139                         sizeX = shortSizeToLongSize(sizeX, 2);
140                     if (metricX == "MB")
141                         sizeX = shortSizeToLongSize(sizeX, 3);
142                     if (metricX == "GB")
143                         sizeX = shortSizeToLongSize(sizeX, 4);
144                     if (metricX == "TB")
145                         sizeX = shortSizeToLongSize(sizeX, 5);
146
147                     if (metricY == "bytes")
148                         sizeY = shortSizeToLongSize(sizeY, 1);
149                     if (metricY == "KB")
150                         sizeY = shortSizeToLongSize(sizeY, 2);
151                     if (metricY == "MB")
152                         sizeY = shortSizeToLongSize(sizeY, 3);
153                     if (metricY == "GB")
154                         sizeX = shortSizeToLongSize(sizeY, 4);
155                     if (metricY == "TB")
156                         sizeY = shortSizeToLongSize(sizeY, 5);
157
158                     if (sizeX < sizeY)
159                     {
160                         return -1;
161                     }
162                     else if (sizeX == sizeY)
163                         return 0;
164                     else
165                         return 1;
166                 }
167                 else
168                 {
169                     return 0;
170                 }
171             }
172
173             public long shortSizeToLongSize(double input, int metric)
174             {
175                 if (metric == 1)
176                     return (long)input;
177                 if (metric == 2)
178                     return (long)input * 1024;
179                 if (metric == 3)
180                     return (long)input * 1024 * 1024;
181                 if (metric == 4)
182                     return (long)input * 1024 * 1024 * 1024;
183                 if (metric == 5)
184                     return (long)input * 1024 * 1024 * 1024 * 1024;
185
186                 return -1;
187             }
188         }
189
190         public DuplicateFinder(Folder[] folders)
191         {
192             Folders = (Folder[]) folders.Clone();
344044 193
RD 194             sha_handle = SHA1.Create();
195         }
196
197        /* public void FindParallel()
198         {
199             int i, j, k, l;
200             FileList = new SizeInfoCollection();
201             bool duplicateFirstLoop = true;
202             DuplicateGroup duplicateGroup = null;
203             DuplicateFile duplicateFile;
204             double progressStep;
205             double progress = (double)0;
206             DateTime start_time = DateTime.Now;
207             DateTime endTime;
208             bool addFolder = true;
209             bool hardLink = false;
210
211             GlobalBuffers.Duplicates = new DuplicateGroupCollection();
212
213             Status = ProgressStatusMode.CachingFolders;
214
215
216             // Folders to scan
217             List<string> scanFolders = new List<string>();
218             scanFolders.AddRange(Folders);
219
220             // Master file list that stores all files and folders that are in the folders that the user selected
221             DirectoryList masterFolderTable = new DirectoryList();
222
223             // Crawl the folders that the user added to the list; add them to the master file table 
224             foreach (string folder in scanFolders)
225             {
226                 masterFolderTable.Add(new ObjectInfo(folder, 0, ObjectInfo.FileType.Folder));
227             }
228
229             foreach (string folder in ExcludedFolders)
230             {
231                 // Now exclude any excluded directories
232                 for (i = 0; i < masterFolderTable.Count; i++)
233                 {
234                     if (ExcludedFolders.IndexOf(masterFolderTable[i].Name) != -1)
235                     {
236                         masterFolderTable.RemoveAt(i);
237                         continue;
238                     }
239                 }
240             }
241
242             // Now crawl the master folder table
243             for (i = 0; i < masterFolderTable.Count; i++)
244             {
245                 if (Canceled == true)
246                     break;
247
248                 foreach (string folder in ExcludedFolders)
249                 {
250                     if (addFolder == false)
251                     {
252                         addFolder = true;
253                         continue;
254                     }
255                     if (ExcludedFolders.IndexOf(masterFolderTable[i].Name) != -1)
256                     {
257                         masterFolderTable.RemoveAt(i);
258                         i--;
259                         addFolder = false;
260                         continue;
261                     }
262                 }
263
264                 try
265                 {
266                     DirectoryList folders = DirectoryEnumerator.Scan(masterFolderTable[i].Name);
267
268                     if (addFolder == true)
269                     {
270                         foreach (ObjectInfo objectInfo in folders)
271                         {
272                             if (objectInfo.Type == ObjectInfo.FileType.Folder)
273                             {
274                                 FoundFolders += 1;
275
276                                 if (IncludeSubfolders == false)
277                                 {
278                                     for (j = 0; j < scanFolders.Count; j++)
279                                     {
280                                         if (scanFolders[j].ToLower().IndexOf(objectInfo.Path.ToLower()) == 0 && scanFolders[j].ToLower() != objectInfo.Path.ToLower())
281                                             addFolder = false;
282                                     }
283                                 }
284                             }
285
286                             if (IncludeSubfolders == true || IncludeSubfolders == false && addFolder == true)
287                                 masterFolderTable.Add(objectInfo);
288
289                             addFolder = true;
290                         }
291                     }
292
293                     addFolder = true;
294
295                 }
296                 catch
297                 {
298                     continue;
299                 }
300             }
301
302             Status = ProgressStatusMode.CachingFiles;
303
304             if (ScanMode == Mode.CompareByActualData)
305             {
306
307                 progressStep = (double)100 / masterFolderTable.Count;
308                 progress = 0;
309
310                 // Now cache all the files
311                 foreach (ObjectInfo objectInfo in masterFolderTable)
312                 {
313                     if (Canceled == true)
314                         break;
315
316                     try
317                     {
318                         if (objectInfo.Type == ObjectInfo.FileType.File)
319                         {
320                             long sizeIndex = FindSizeIndex(objectInfo.Size, FileList);
321
322                             if (sizeIndex == -1)
323                             {
324                                 sizeIndex = AddSizeIndex(objectInfo.Size, FileList);
325                             }
326
327                             // TODO: Make the longs and ints uniform
328
329                             // add the file to the list and process any settings the user may have specified (file type inclusions, etc.)
330                             FileList[(int)sizeIndex].FileList.Add(objectInfo, FileTypeInclusions, ExcludedFiletypes, FileSizeBracket, MinimumSize, MaximumSize, ExcludedFolders);
331
332                             FoundFiles++;
333                         }
334
335                         progress += (double)progressStep;
336                         Progress = (int)progress;
337                     }
338                     catch
339                     {
340                         continue;
341                     }
342                 }
343
344                 masterFolderTable.Clear();
345
346                 Status = ProgressStatusMode.IdentifyingDuplicates;
347
348                 progressStep = (double)100 / FileList.Count;
349                 progress = 0;
350
351                 SizeIndexTrackerEnumerator enumerator = new SizeIndexTrackerEnumerator(SizeIndexTracker, FileList);
352                 SizeInfo sizeInfo;
353
354                 // loop through the duplicates
355                 while (enumerator.Status == SizeIndexTrackerEnumerator.EnumerationStatus.Success)
356                 {
357                     if (Canceled == true)
358                         break;
359
360                     sizeInfo = enumerator.Enumerate();
361
362                     if (enumerator.Status == SizeIndexTrackerEnumerator.EnumerationStatus.Success)
363                     {
364
365                         if (Canceled == true)
366                             break;
367
368                         // if there is more than 1 file in the collection, read the checksum
369                         if (sizeInfo.FileList.Count >= 2)
370                         {
371                             for (j = 0; j < sizeInfo.FileList.Count; j++)
372                             {
373                                 if (Canceled == true)
374                                     break;
375
376                                 sizeInfo.FileList[j].ChecksumReadStatus = ReadFileChecksum(sizeInfo.FileList[j], FileReadPercentage);
377                             }
378
379                             // now compare the checksum
380
381                             for (j = 0; j < sizeInfo.FileList.Count - 1; j++)
382                             {
383                                 if (Canceled == true)
384                                     break;
385
386                                 if (sizeInfo.FileList[j].ChecksumReadStatus == ChecksumReadStatus.Success)
387                                 {
388                                     for (k = j + 1; k < sizeInfo.FileList.Count; k++)
389                                     {
390                                         if (Canceled == true)
391                                             break;
392
393                                         if (sizeInfo.FileList[k].ChecksumReadStatus == ChecksumReadStatus.Success)
394                                         {
395
396                                             if (sizeInfo.FileList[j].ChecksumSum.Sum == sizeInfo.FileList[k].ChecksumSum.Sum)
397                                             {
398
399                                                 if (sizeInfo.FileList[j].Size == sizeInfo.FileList[k].Size && sizeInfo.FileList[j].FileInfo.NumberOfLinks != 1 && sizeInfo.FileList[k].FileInfo.NumberOfLinks != 1 && sizeInfo.FileList[j].FileInfo.FileIndexHigh == sizeInfo.FileList[k].FileInfo.FileIndexHigh
400                                                     && sizeInfo.FileList[j].FileInfo.FileIndexLow == sizeInfo.FileList[k].FileInfo.FileIndexLow && sizeInfo.FileList[j].FileInfo.VolumeSerialNumber == sizeInfo.FileList[k].FileInfo.VolumeSerialNumber) // hard link
401                                                 {
402                                                     if (IgnoreLinks == true)
403                                                         continue;
404                                                     else
405                                                         hardLink = true;
406                                                 }
407
408                                                 if (duplicateFirstLoop == true)
409                                                 {
410                                                     duplicateFirstLoop = false;
411
412                                                     duplicateGroup = new DuplicateGroup();
413
414                                                     duplicateGroup.GroupName = DuplicateListView.DuplicateListView.FormatSizeString(sizeInfo.FileList[j].Size);
415
416                                                     duplicateFile = new DuplicateFile(sizeInfo.FileList[j].Name, sizeInfo.FileList[j].Size, sizeInfo.FileList[j].DateModified, sizeInfo.FileList[j].Network, sizeInfo.FileList[j].NetworkIP);
417
418                                                     if (hardLink == true)
419                                                         duplicateFile.Link = true;
420
421                                                     duplicateGroup.Add(duplicateFile);
422                                                 }
423
424                                                 duplicateFile = new DuplicateFile(sizeInfo.FileList[k].Name, sizeInfo.FileList[k].Size, sizeInfo.FileList[k].DateModified, sizeInfo.FileList[k].Network, sizeInfo.FileList[k].NetworkIP);
425
426                                                 if (hardLink == true)
427                                                     duplicateFile.Link = true;
428
429                                                 duplicateGroup.Add(duplicateFile);
430
431                                                 sizeInfo.FileList.RemoveAt(k);
432                                                 k--;
433                                             }
434                                         }
435                                         else
436                                         {
437                                             if (Errors != null)
438                                             {
439                                                 if (sizeInfo.FileList[k].ChecksumReadStatus == ChecksumReadStatus.AccessDenied)
440                                                     Errors.AddError(sizeInfo.FileList[k].Name, "Access was denied.");
441                                                 else if (sizeInfo.FileList[k].ChecksumReadStatus == ChecksumReadStatus.FileNotFound)
442                                                     Errors.AddError(sizeInfo.FileList[k].Name, "The file was not found.");
443                                                 //  else if (size_info.file_list[k].ChecksumReadStatus == ChecksumReadStatus.UnknownError)
444                                                 //     Errors.AddError(size_info.file_list[k].Name, "An unknown error occurred.");
445                                             }
446                                         }
447                                     }
448
449
450                                     if (duplicateGroup != null)
451                                     {
452                                         GlobalBuffers.Duplicates.Add(duplicateGroup);
453                                         sizeInfo.FileList.RemoveAt(j);
454                                         j--;
455                                     }
456
457                                     duplicateGroup = null;
458
459                                     duplicateFirstLoop = true;
460                                 }
461                                 else
462                                 {
463                                     if (Errors != null)
464                                     {
465                                         if (sizeInfo.FileList[j].ChecksumReadStatus == ChecksumReadStatus.AccessDenied)
466                                             Errors.AddError(sizeInfo.FileList[j].Name, "Access was denied.");
467                                         else if (sizeInfo.FileList[j].ChecksumReadStatus == ChecksumReadStatus.FileNotFound)
468                                             Errors.AddError(sizeInfo.FileList[j].Name, "The file was not found.");
469                                         // else if (size_info.file_list[j].ChecksumReadStatus == ChecksumReadStatus.UnknownError)
470                                         //    Errors.AddError(size_info.file_list[j].Name, "An unknown error occurred.");
471                                     }
472                                 }
473
474                                 hardLink = false;
475                             }
476                         }
477
478                         progress += (double)progressStep;
479                         Progress = (int)progress;
480                     }
481
482
483                 }
484             }
485             else if (ScanMode == Mode.CompareByFilename)
486             {
487                 Status = ProgressStatusMode.CachingFiles;
488
489                 List<ObjectInfo>[] fileList = new List<ObjectInfo>[2775 + 1];
490
491                 for (i = 0; i < 2776; i++)
492                 {
493                     fileList[i] = new List<ObjectInfo>();
494                 }
495
496                 int arrayIndex;
497                 int sizeChanged = -1;
498
499                 progressStep = (double)100 / masterFolderTable.Count;
500                 progress = 0;
501
502                 // Now cache all the files
503                 foreach (ObjectInfo info in masterFolderTable)
504                 {
505                     if (Canceled == true)
506                         break;
507
508                     if (info.Type == ObjectInfo.FileType.File)
509                     {
510                         if (MinimumSize == -1 || MinimumSize != -1 && info.Size >= MinimumSize && (MaximumSize != -1 && info.Size <= MaximumSize || MaximumSize == -1) && (FileTypeInclusions.Count == 0 || FileTypeInclusions.Count > 0 && FileTypeInclusions.IndexOf(Path.GetExtension(info.Name)) != -1))
511                         {
512                             arrayIndex = GetCorrectArrayIndex(Path.GetFileName(info.Name), 2);
513
514                             if (arrayIndex == -1)
515                                 continue;
516
517                             fileList[arrayIndex].Add(info);
518
519                             FoundFiles++;
520                         }
521                     }
522
523                     progress += (double)progressStep;
524                     Progress = (int)progress;
525                 }
526
527                 Status = ProgressStatusMode.IdentifyingDuplicates;
528
529                 progressStep = (double)100 / 2776;
530                 progress = 0;
531
532                 for (i = 0; i < 2776; i++)
533                 {
534                     if (Canceled == true)
535                         break;
536
537                     if (fileList[i].Count > 1)
538                     {
539
540
541                         for (j = 0; j < fileList[i].Count - 1; j++)
542                         {
543                             if (Canceled == true)
544                                 break;
545
546                             sizeChanged = -1;
547
548                             for (k = j + 1; k < fileList[i].Count; k++)
549                             {
550                                 if (Canceled == true)
551                                     break;
552
553                                 if (EvaluateFilenames(fileList[i][j], fileList[i][k]) == true)
554                                 {
555                                     if (duplicateFirstLoop == true)
556                                     {
557                                         duplicateFirstLoop = false;
558
559                                         duplicateGroup = new DuplicateGroup();
560
561                                         if (CompareByExtension == true)
562                                             duplicateGroup.GroupName = Path.GetFileName(fileList[i][j].Name);
563                                         else
564                                             duplicateGroup.GroupName = Path.GetFileNameWithoutExtension(fileList[i][j].Name);
565
566                                         duplicateGroup.Add(new DuplicateFile(fileList[i][j].Name, fileList[i][j].Size, fileList[i][j].DateModified));
567                                     }
568
569                                     if (sizeChanged == -1)
570                                     {
571                                         for (l = 0; l < duplicateGroup.Count; l++)
572                                         {
573                                             if (Canceled == true)
574                                                 break;
575
576                                             if (duplicateGroup[l].Size != fileList[i][k].Size)
577                                             {
578                                                 sizeChanged = 1;
579                                                 break;
580                                             }
581
582                                             if (l == duplicateGroup.Count - 1)
583                                                 sizeChanged = 0;
584                                         }
585                                     }
586
587                                     duplicateGroup.Add(new DuplicateFile(fileList[i][k].Name, fileList[i][k].Size, fileList[i][k].DateModified));
588
589                                     fileList[i].RemoveAt(k);
590                                     k--;
591
592                                 }
593                             }
594
595                             if (duplicateGroup != null)
596                             {
597                                 if (sizeChanged == 0)
598                                     duplicateGroup.State = DuplicateGroup.GroupState.Undeterminable;
599                                 else
600                                     duplicateGroup.State = DuplicateGroup.GroupState.FalseDuplicate;
601
602
603
604                                 GlobalBuffers.Duplicates.Add(duplicateGroup);
605                             }
606
607
608                             duplicateGroup = null;
609
610                             duplicateFirstLoop = true;
611
612                         }
613
614
615                     }
616
617                     sizeChanged = -1;
618
619
620                     progress += (double)progressStep;
621                     Progress = (int)progress;
622                 }
623             }
624
625             // cache the folders
626             GlobalBuffers.folders.Clear();
627
628             for (i = 0; i < GlobalBuffers.Duplicates.Count; i++)
629             {
630                 if (Canceled == true)
631                     break;
632
633                 for (j = 0; j < GlobalBuffers.Duplicates[i].Count; j++)
634                 {
635                     if (Canceled == true)
636                         break;
637
638                     if (GlobalBuffers.folders.Count == 0)
639                         GlobalBuffers.folders.Add(Path.GetDirectoryName(GlobalBuffers.Duplicates[i][j].Filename));
640                     else
641                     {
642                         for (l = 0; l < GlobalBuffers.folders.Count; l++)
643                         {
644                             if (Canceled == true)
645                                 break;
646
647                             if (GlobalBuffers.folders[l] == Path.GetDirectoryName(GlobalBuffers.Duplicates[i][j].Filename))
648                                 break;
649                             else if (l == GlobalBuffers.folders.Count - 1)
650                                 GlobalBuffers.folders.Add(Path.GetDirectoryName(GlobalBuffers.Duplicates[i][j].Filename));
651                         }
652                     }
653                 }
654             }
655
656
657             endTime = DateTime.Now;
658             ScanTime = endTime.Subtract(start_time);
659
660             Status = ProgressStatusMode.Finished;
661
662             GC.Collect();
663
664             if (Errors != null)
665                 Errors.ShowErrorWindow();
666
667             if (ScanComplete != null)
668             {
669                 ScanComplete(new ScanCompleteArgs(GlobalBuffers.Duplicates.TotalQuantity));
670             }
671         }*/
672
673         public void FindThread()
674         {
675             ThreadStart ts = new ThreadStart(Find);
676
677             Thread thread = new Thread(ts);
678
679             thread.Start();
680         } 
681
682         public void Find()
683         {
684             int i,j,k,l;
25f449 685             FileList = new Dictionary<long, SizeInfo>(); //new SizeInfoCollection();
344044 686             bool duplicateFirstLoop = true;
RD 687             DuplicateGroup duplicateGroup = null;
688             DuplicateFile duplicateFile;
689             double progressStep;
690             double progress = (double)0;
691             DateTime start_time = DateTime.Now;
692             DateTime endTime;
693             bool addFolder = true;
694             bool hardLink= false;
695
696             GlobalBuffers.Duplicates = new DuplicateGroupCollection();
697
698             Status = ProgressStatusMode.CachingFolders;
699             
700
701             // Folders to scan
fb4519 702             List<Folder> scanFolders = new List<Folder>();
344044 703             scanFolders.AddRange(Folders);
RD 704
705             // Master file list that stores all files and folders that are in the folders that the user selected
706             DirectoryList masterFolderTable = new DirectoryList();
707
708             // Crawl the folders that the user added to the list; add them to the master file table 
fb4519 709             foreach (Folder folder in scanFolders)
344044 710             {
fb4519 711                 masterFolderTable.Add(new ObjectInfo(folder.ClientPath, 0, ObjectInfo.FileType.Folder));
344044 712             }
RD 713
714             foreach (string folder in ExcludedFolders)
715             {
716                 // Now exclude any excluded directories
717                 for (i = 0; i < masterFolderTable.Count; i++)
718                 {
719                     if (ExcludedFolders.IndexOf(masterFolderTable[i].Name) != -1)
720                     {
721                         masterFolderTable.RemoveAt(i);
722                         continue;
723                     }
724                 }
725             }
726
727             // Now crawl the master folder table
728             for(i = 0; i < masterFolderTable.Count; i++)
729             {
730                 if (Canceled == true)
731                     break;
732
733                 foreach (string folder in ExcludedFolders)
734                 {
735                     if (addFolder == false)
736                     {
737                         addFolder = true;
738                         continue;
739                     }
740                     if (ExcludedFolders.IndexOf(masterFolderTable[i].Name) != -1)
741                     {
742                         masterFolderTable.RemoveAt(i);
743                         i--;
744                         addFolder = false;
745                         continue;
746                     }
747                 } 
748
749                 try
750                 {
751                         DirectoryList folders = DirectoryEnumerator.Scan(masterFolderTable[i].Name);
752
753                         if (addFolder == true)
754                         {
755                             foreach (ObjectInfo objectInfo in folders)
756                             {
757                                 if (objectInfo.Type == ObjectInfo.FileType.Folder)
758                                 {
759                                     FoundFolders += 1;
760
761                                     if (IncludeSubfolders == false)
762                                     {
763                                         for (j = 0; j < scanFolders.Count; j++)
764                                         {
fb4519 765                                             if (scanFolders[j].ClientPath.ToLower().IndexOf(objectInfo.Path.ToLower()) == 0 && scanFolders[j].ClientPath.ToLower() != objectInfo.Path.ToLower())
344044 766                                                 addFolder = false;
RD 767                                         }
768                                     }
769                                 }
770
771                                 if (IncludeSubfolders == true || IncludeSubfolders == false && addFolder == true)
772                                     masterFolderTable.Add(objectInfo);
773
774                                 addFolder = true;
775                             }
776                         }
777
778                         addFolder = true;
779
780                 }
781                 catch
782                 {
783                     continue;
784                 }
785             }
786
787             Status = ProgressStatusMode.CachingFiles;
788
789             if (ScanMode == Mode.CompareByActualData)
790             {
791
792                 progressStep = (double)100 / masterFolderTable.Count;
793                 progress = 0;
794
795                 // Now cache all the files
796                     foreach (ObjectInfo objectInfo in masterFolderTable)
797                     {
798                         if (Canceled == true)
799                             break;
800
801                         try
802                         {
803                             if (objectInfo.Type == ObjectInfo.FileType.File)
804                             {
25f449 805                                 if(!FindSizeIndex(objectInfo.Size, FileList))
RD 806                                     AddSizeIndex(objectInfo.Size, FileList);
344044 807
RD 808                                 // TODO: Make the longs and ints uniform
809
810                                 // add the file to the list and process any settings the user may have specified (file type inclusions, etc.)
25f449 811                                 FileList[(int)objectInfo.Size].FileList.Add(objectInfo, FileTypeInclusions, ExcludedFiletypes, FileSizeBracket, MinimumSize, MaximumSize, ExcludedFolders);
344044 812
RD 813                                 FoundFiles++;
814                             }
815
816                             progress += (double)progressStep;
817                             Progress = (int)progress;
818                         }
819                         catch
820                         {
821                             continue;
822                         }
823                     }
824                 
825                 masterFolderTable.Clear();
826
827                 Status = ProgressStatusMode.IdentifyingDuplicates;
828
829                 progressStep = (double)100 / FileList.Count;
830                 progress = 0;
831
25f449 832                 var enumerator = FileList.GetEnumerator();
RD 833
834               //  SizeIndexTrackerEnumerator enumerator = new SizeIndexTrackerEnumerator(SizeIndexTracker, FileList);
344044 835                 SizeInfo sizeInfo;
25f449 836                 KeyValuePair<long, SizeInfo> currentData;
344044 837
RD 838                 // loop through the duplicates
25f449 839                 while(enumerator.MoveNext())
344044 840                 {
RD 841                     if (Canceled == true)
842                         break;
843
25f449 844                     currentData = enumerator.Current;
RD 845                     sizeInfo = currentData.Value;
344044 846
25f449 847                    // if (enumerator.Status == SizeIndexTrackerEnumerator.EnumerationStatus.Success)
RD 848                     //{
344044 849
RD 850                         if (Canceled == true)
851                             break;
852
853                         // if there is more than 1 file in the collection, read the checksum
854                         if (sizeInfo.FileList.Count >= 2)
855                         {
856                             for (j = 0; j < sizeInfo.FileList.Count; j++)
857                             {
858                                 if (Canceled == true)
859                                     break;
860                                 
861                                 sizeInfo.FileList[j].ChecksumReadStatus = ReadFileChecksum(sizeInfo.FileList[j], FileReadPercentage);
862                             }
863
864                             // now compare the checksum
865
866                             for (j = 0; j < sizeInfo.FileList.Count - 1; j++)
867                             {
868                                 if (Canceled == true)
869                                     break;
870
871                                 if (sizeInfo.FileList[j].ChecksumReadStatus == ChecksumReadStatus.Success)
872                                 {
873                                     for (k = j + 1; k < sizeInfo.FileList.Count; k++)
874                                     {
875                                         if (Canceled == true)
876                                             break;
877
878                                         if (sizeInfo.FileList[k].ChecksumReadStatus == ChecksumReadStatus.Success)
879                                         {
880                                             
881                                             if (sizeInfo.FileList[j].ChecksumSum.Sum == sizeInfo.FileList[k].ChecksumSum.Sum)
882                                             {
883
884                                                 if (sizeInfo.FileList[j].Size == sizeInfo.FileList[k].Size && sizeInfo.FileList[j].FileInfo.NumberOfLinks != 1 && sizeInfo.FileList[k].FileInfo.NumberOfLinks != 1 && sizeInfo.FileList[j].FileInfo.FileIndexHigh == sizeInfo.FileList[k].FileInfo.FileIndexHigh
885                                                     && sizeInfo.FileList[j].FileInfo.FileIndexLow == sizeInfo.FileList[k].FileInfo.FileIndexLow && sizeInfo.FileList[j].FileInfo.VolumeSerialNumber == sizeInfo.FileList[k].FileInfo.VolumeSerialNumber) // hard link
886                                                 {
887                                                     if (IgnoreLinks == true)
888                                                         continue;
889                                                     else
890                                                         hardLink = true;
891                                                 }
892
893                                                 if (duplicateFirstLoop == true)
894                                                 {
895                                                     duplicateFirstLoop = false;
896
897                                                     duplicateGroup = new DuplicateGroup();
898
899                                                     duplicateGroup.GroupName = DuplicateListView.DuplicateListView.FormatSizeString(sizeInfo.FileList[j].Size);
900
901                                                     duplicateFile = new DuplicateFile(sizeInfo.FileList[j].Name, sizeInfo.FileList[j].Size, sizeInfo.FileList[j].DateModified, sizeInfo.FileList[j].Network, sizeInfo.FileList[j].NetworkIP);
902
903                                                     if (hardLink == true)
904                                                         duplicateFile.Link = true;
905
906                                                     duplicateGroup.Add(duplicateFile);
907                                                 }
908
909                                                 duplicateFile = new DuplicateFile(sizeInfo.FileList[k].Name, sizeInfo.FileList[k].Size, sizeInfo.FileList[k].DateModified, sizeInfo.FileList[k].Network, sizeInfo.FileList[k].NetworkIP);
910
911                                                 if (hardLink == true)
912                                                     duplicateFile.Link = true;
913
25f449 914                                                 if(!duplicateGroup.Contains(duplicateFile))
RD 915                                                     duplicateGroup.Add(duplicateFile);
344044 916
RD 917                                                 sizeInfo.FileList.RemoveAt(k);
918                                                 k--;
919                                             }
920                                         }
921                                         else
922                                         {
923                                             if (Errors != null)
924                                             {
925                                                 if (sizeInfo.FileList[k].ChecksumReadStatus == ChecksumReadStatus.AccessDenied)
926                                                     Errors.AddError(sizeInfo.FileList[k].Name, "Access was denied.");
927                                                 else if (sizeInfo.FileList[k].ChecksumReadStatus == ChecksumReadStatus.FileNotFound)
928                                                     Errors.AddError(sizeInfo.FileList[k].Name, "The file was not found.");
929                                                 //  else if (size_info.file_list[k].ChecksumReadStatus == ChecksumReadStatus.UnknownError)
930                                                 //     Errors.AddError(size_info.file_list[k].Name, "An unknown error occurred.");
931                                             }
932                                         }
933                                     }
934
935
936                                     if (duplicateGroup != null)
937                                     {
938                                         GlobalBuffers.Duplicates.Add(duplicateGroup);
939                                         sizeInfo.FileList.RemoveAt(j);
940                                         j--;
941                                     }
942
943                                     duplicateGroup = null;
944
945                                     duplicateFirstLoop = true;
946                                 }
947                                 else
948                                 {
949                                     if (Errors != null)
950                                     {
951                                         if (sizeInfo.FileList[j].ChecksumReadStatus == ChecksumReadStatus.AccessDenied)
952                                             Errors.AddError(sizeInfo.FileList[j].Name, "Access was denied.");
953                                         else if (sizeInfo.FileList[j].ChecksumReadStatus == ChecksumReadStatus.FileNotFound)
954                                             Errors.AddError(sizeInfo.FileList[j].Name, "The file was not found.");
955                                         // else if (size_info.file_list[j].ChecksumReadStatus == ChecksumReadStatus.UnknownError)
956                                         //    Errors.AddError(size_info.file_list[j].Name, "An unknown error occurred.");
957                                     }
958                                 }
959
960                                 hardLink = false;
961                             }
962                         }
963
964                         progress += (double)progressStep;
965                         Progress = (int)progress;
25f449 966                 //    }
344044 967
RD 968
969                 }
970             }
971             else if (ScanMode == Mode.CompareByFilename)
972             {
973                 Status = ProgressStatusMode.CachingFiles;
974
975                 List<ObjectInfo>[] fileList = new List<ObjectInfo>[2775 + 1];
976
977                 for (i = 0; i < 2776; i++)
978                 {
979                     fileList[i] = new List<ObjectInfo>();
980                 }
981
982                 int arrayIndex;
983                 int sizeChanged = -1;
984
985                 progressStep = (double)100 / masterFolderTable.Count;
986                 progress = 0;
987
988                 // Now cache all the files
989                 foreach (ObjectInfo info in masterFolderTable)
990                 {
991                     if (Canceled == true)
992                         break;
993
994                     if (info.Type == ObjectInfo.FileType.File)
995                     {
996                         if (MinimumSize == -1 || MinimumSize != -1 && info.Size >= MinimumSize && (MaximumSize != -1 && info.Size <= MaximumSize || MaximumSize == -1) && (FileTypeInclusions.Count == 0 || FileTypeInclusions.Count > 0 && FileTypeInclusions.IndexOf(Path.GetExtension(info.Name)) != -1))
997                         {
998                             arrayIndex = GetCorrectArrayIndex(Path.GetFileName(info.Name), 2);
999
1000                             if (arrayIndex == -1)
1001                                 continue;
1002
1003                             fileList[arrayIndex].Add(info);
1004
1005                             FoundFiles++;
1006                         }
1007                     }
1008
1009                     progress += (double)progressStep;
1010                     Progress = (int)progress;
1011                 }
1012
1013                 Status = ProgressStatusMode.IdentifyingDuplicates;
1014
1015                 progressStep = (double)100 / 2776;
1016                 progress = 0;
1017
1018                 for (i = 0; i < 2776; i++)
1019                 {
1020                     if (Canceled == true)
1021                         break;
1022
1023                     if (fileList[i].Count > 1)
1024                     {
1025
1026
1027                         for (j = 0; j < fileList[i].Count-1; j++)
1028                         {
1029                             if (Canceled == true)
1030                                 break;
1031
1032                             sizeChanged = -1;
1033
1034                             for (k = j + 1; k < fileList[i].Count; k++)
1035                             {
1036                                 if (Canceled == true)
1037                                     break;
1038
1039                                 if (EvaluateFilenames(fileList[i][j], fileList[i][k]) == true)
1040                                 {
1041                                     if (duplicateFirstLoop == true)
1042                                     {
1043                                         duplicateFirstLoop = false;
1044
1045                                         duplicateGroup = new DuplicateGroup();
1046
1047                                         if (CompareByExtension == true)
1048                                             duplicateGroup.GroupName = Path.GetFileName(fileList[i][j].Name);
1049                                         else
1050                                             duplicateGroup.GroupName = Path.GetFileNameWithoutExtension(fileList[i][j].Name);
1051
1052                                         duplicateGroup.Add(new DuplicateFile(fileList[i][j].Name, fileList[i][j].Size, fileList[i][j].DateModified));
1053                                     }
1054
1055                                     if (sizeChanged == -1)
1056                                     {
1057                                         for (l = 0; l < duplicateGroup.Count; l++)
1058                                         {
1059                                             if (Canceled == true)
1060                                                 break;
1061
1062                                             if (duplicateGroup[l].Size != fileList[i][k].Size)
1063                                             {
1064                                                 sizeChanged = 1;
1065                                                 break;
1066                                             }
1067
1068                                             if (l == duplicateGroup.Count - 1)
1069                                                 sizeChanged = 0;
1070                                         }
1071                                     }
1072
1073                                     duplicateGroup.Add(new DuplicateFile(fileList[i][k].Name, fileList[i][k].Size, fileList[i][k].DateModified));
1074
1075                                     fileList[i].RemoveAt(k);
1076                                     k--;
1077
1078                                 }
1079                             }
1080
1081                             if (duplicateGroup != null)
1082                             {
1083                                 if (sizeChanged == 0)
1084                                     duplicateGroup.State = DuplicateGroup.GroupState.Undeterminable;
1085                                 else
1086                                     duplicateGroup.State = DuplicateGroup.GroupState.FalseDuplicate;
1087
1088                                 
1089
1090                                 GlobalBuffers.Duplicates.Add(duplicateGroup);   
1091                             }
1092
1093
1094                             duplicateGroup = null;
1095
1096                             duplicateFirstLoop = true;
1097                             
1098                         }
1099
1100                         
1101                     }
1102
1103                     sizeChanged = -1;
1104
1105
1106                     progress += (double)progressStep;
1107                     Progress = (int)progress;
1108                 }
1109             }
1110
1111             // cache the folders
1112             GlobalBuffers.folders.Clear();
1113             
1114             for (i = 0; i < GlobalBuffers.Duplicates.Count; i++)
1115             {
1116                 if (Canceled == true)
1117                     break;
1118
1119                 for (j = 0; j < GlobalBuffers.Duplicates[i].Count; j++)
1120                 {
1121                     if (Canceled == true)
1122                         break;
1123
1124                     if (GlobalBuffers.folders.Count == 0)
1125                         GlobalBuffers.folders.Add(Path.GetDirectoryName(GlobalBuffers.Duplicates[i][j].Filename));
1126                     else
1127                     {
1128                         for (l = 0; l < GlobalBuffers.folders.Count; l++)
1129                         {
1130                             if (Canceled == true)
1131                                 break;
1132
1133                             if (GlobalBuffers.folders[l] == Path.GetDirectoryName(GlobalBuffers.Duplicates[i][j].Filename))
1134                                 break;
1135                             else if (l == GlobalBuffers.folders.Count - 1)
1136                                 GlobalBuffers.folders.Add(Path.GetDirectoryName(GlobalBuffers.Duplicates[i][j].Filename));
1137                         }
1138                     }
1139                 }
1140             }
fb4519 1141
RD 1142             GlobalBuffers.Duplicates.Sort(new DuplicateSorter(this));
344044 1143             
RD 1144
1145             endTime = DateTime.Now;
1146             ScanTime = endTime.Subtract(start_time);
1147
1148             Status = ProgressStatusMode.Finished;
1149
1150             GC.Collect();
1151
1152             if (Errors != null)
1153                 Errors.ShowErrorWindow();
1154
1155             if (ScanComplete != null)
1156             {
1157                 ScanComplete(new ScanCompleteArgs(GlobalBuffers.Duplicates.TotalQuantity));
1158             }
1159             
1160         }
1161
1162         public bool EvaluateFilenames(ObjectInfo file1, ObjectInfo file2)
1163         {
1164             bool result = false;
1165
1166             if (CompareByExtension == true)
1167             {
1168                 if (Path.GetExtension(file1.Name) == Path.GetExtension(file2.Name))
1169                     result = true;
1170                 else
1171                     return false;
1172             }
1173
1174             if (CompareByModified == true)
1175             {
1176                 if (file1.DateModified.CompareTo(file2.DateModified) == 0)
1177                     result = true;
1178                 else
1179                     return false;
1180             }
1181
1182             if (CompareBySize == true)
1183             {
1184                 if (file1.Size == file2.Size)
1185                     result = true;
1186                 else
1187                     return false;
1188             }
1189
1190             if (Path.GetFileNameWithoutExtension(file1.Name).ToLower() == Path.GetFileNameWithoutExtension(file2.Name).ToLower())
1191                 result = true;
1192             else
1193                 result = false;
1194
1195             return result;
1196         }
1197
25f449 1198         public bool FindSizeIndex(long size, Dictionary<long, SizeInfo> fileDict)
RD 1199         {
1200             if (!fileDict.ContainsKey(size))
1201                 return false;
1202
1203             return true;
1204         }
1205
1206        /* public long FindSizeIndex(long size, SizeInfoCollection size_data)
344044 1207         {
RD 1208             int i;
1209             long percentage;
1210             List<long> indexes;
1211             ConvertedSize size_metric = SizeIndexTracker.GetSizeMetric(size);
1212
1213             if (size > size_maximum_size)
1214                 return -1;
1215             if (size < size_minimum_size)
1216                 return -1;
1217
1218             percentage = SizeIndexTracker.GetPercentage(size_metric.ConvertedLength, size_metric.Unit);
1219             indexes = SizeIndexTracker.GetPercentageIndexArray(percentage, size_metric.Unit);
1220
1221             for (i = 0; i < indexes.Count; i++)
1222             {
1223                 if (size_data[(int)indexes[i]].size == size)
1224                     return (long)indexes[i];
1225             }
1226
1227             return -1;
25f449 1228         }*/
344044 1229
25f449 1230        /* private long AddSizeIndex(long size, SizeInfoCollection size_tracker)
344044 1231         {
RD 1232             SizeInfo sd1 = new SizeInfo();
1233             long percentage;
1234             int i, new_index ;
1235             List<long> indexes;
1236             ConvertedSize size_metric = SizeIndexTracker.GetSizeMetric(size);
1237
1238             sd1.size = size;
1239
1240             percentage = SizeIndexTracker.GetPercentage(size_metric.ConvertedLength, size_metric.Unit);
1241             indexes = SizeIndexTracker.GetPercentageIndexArray(percentage, size_metric.Unit);
1242
1243             new_index = size_tracker.Add(sd1);
1244
1245             if (size > size_maximum_size)
1246                 size_maximum_size = size;
1247             if (size < size_minimum_size)
1248                 size_minimum_size = size;
1249
1250             for (i = 0; i < indexes.Count; i++)
1251             {
1252                 if (size_tracker[(int)indexes[i]].size > size)
1253                 {
1254                     indexes.Insert(i, new_index);
1255                     return new_index;
1256                 }
1257             }
1258
1259             indexes.Add(new_index);
1260
1261             return new_index;
25f449 1262         }*/
RD 1263
1264         private void AddSizeIndex(long size, Dictionary<long, SizeInfo> fileDict)
1265         {
1266             fileDict.Add(size, new SizeInfo());
344044 1267         }
RD 1268
1269         private int ReturnSortedSizeIndex(long size, SizeInfoCollection size_tracker)
1270         {
1271             int i;
1272
1273             if (size > size_maximum_size)
1274                 return size_tracker.Count;
1275
1276             if (size < size_minimum_size)
1277                 return 0;
1278
1279
1280             for (i = 0; i < size_tracker.Count; i++)
1281             {
1282                 if (size_tracker[i].size > size)
1283                 {
1284                     return i;
1285                 }
1286             }
1287
1288             return size_tracker.Count == 0 ? 0 : size_tracker.Count;
1289         }
1290
1291         private ChecksumReadStatus ReadFileChecksum(ObjectInfo fileInfo, int percent)
1292         {
1293             if (fileInfo.Checksum == null)
1294             {
1295                 byte[] temp;
1296                 int j;
1297                 double percent_size = (double)0.0;
1298                 
1299                 FileChecksum checksum1 = new FileChecksum();
1300
1301                 double progress;
1302
1303                 if (DirectoryEnumerator.Exists(fileInfo.Name))
1304                 {
1305                     FileStream file_stream;
1306                     try
1307                     {
1308                         file_stream = new FileStream(fileInfo.Name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1309                     }
1310                     catch (UnauthorizedAccessException)
1311                     {
1312                         return ChecksumReadStatus.AccessDenied;
1313                     }
1314                     catch (FileNotFoundException)
1315                     {
1316                         return ChecksumReadStatus.FileNotFound;
1317                     }
1318                     catch (IOException)
1319                     {
1320                         return ChecksumReadStatus.UnknownError;
1321                     }
1322
1323                     FileChecksumScanInProgress = true;
1324                     FileChecksumObject = fileInfo;
1325
1326                     GetFileInformationByHandle(file_stream.SafeFileHandle.DangerousGetHandle(), out fileInfo.FileInfo);
1327
1328                     percent_size = (double)percent / 100 * file_stream.Length;
1329                     percent_size = RoundSize(percent_size);
1330
1331                     double loop_count = (double)percent_size / 2097152;
1332
1333                     FileChecksumProgress = (double)0;
1334                     progress = (double)100 / loop_count;
1335
1336                     int i;
1337                     double remainder;
1338
1339                     for (i = 0; i < (int)loop_count; i++)
1340                     {
1341                         if (Canceled == true)
1342                             break;
1343
1344                         try
1345                         {
1346                             file_stream.Read(checksum_data, 0, 2097152);
1347                         }
1348                         catch
1349                         {
1350                             file_stream.Close();
1351                             FileChecksumScanInProgress = false;
1352                             return ChecksumReadStatus.AccessDenied;
1353                         }
1354
1355                         temp = sha_handle.ComputeHash(checksum_data, 0, 2097152);
1356
1357                         for (j = 0; j < 16; j++)
1358                             checksum1.checksum[j] += temp[j];
1359
1360
1361
1362                         FileChecksumProgress += (double)progress;
1363                     }
1364                     if ((double)loop_count > (int)loop_count)
1365                     {
1366                         remainder = loop_count - (int)loop_count;
1367                         remainder = (double)remainder * 2097152;
1368
1369                         try
1370                         {
1371                             file_stream.Read(checksum_data, 0, (int)remainder);
1372                         }
1373                         catch
1374                         {
1375                             file_stream.Close();
1376                             FileChecksumScanInProgress = false;
1377                             return ChecksumReadStatus.AccessDenied;
1378                         }
1379
1380                         temp = sha_handle.ComputeHash(checksum_data, 0, (int)remainder);
1381
1382                         for (j = 0; j < 16; j++)
1383                             checksum1.checksum[j] += temp[j];
1384
1385                         //compare_prg2 += (double)compare_prg;
1386                     }
1387
1388                     file_stream.Close();
1389
1390                     fileInfo.Checksum = checksum1.checksum;
1391                     fileInfo.ChecksumSum = new ChecksumSum(ChecksumSum.returnSum(checksum1.checksum));
1392
1393                 }
1394                 else
1395                 {
1396                     FileChecksumScanInProgress = false;
1397                     return ChecksumReadStatus.FileNotFound;
1398                 }
1399
1400             }
1401             else
1402             {
1403                 FileChecksumScanInProgress = false;
1404                 return ChecksumReadStatus.UnknownError;
1405             }
1406
1407             FileChecksumScanInProgress = false;
1408
1409             return ChecksumReadStatus.Success;
1410         }
1411
1412         private static string formatSizeString(ObjectInfo file_info)
1413         {
1414             string file_size_string = "";
1415
1416             if (file_info.Size < 1024 && file_info.Size != 1)
1417                 file_size_string = file_info.Size.ToString() + " bytes";
1418
1419             else if (file_info.Size == 1)
1420                 file_size_string = file_info.Size.ToString() + " byte";
1421
1422             else if (file_info.Size >= 1024 && file_info.Size < 1048576)
1423                 file_size_string = (file_info.Size / 1024).ToString() + " KB";
1424
1425             else if (file_info.Size >= 1048576 && file_info.Size < 1073741824)
1426             {
1427                 file_size_string = ((double)file_info.Size / 1024 / 1024).ToString();
1428                 file_size_string = file_size_string.Substring(0, file_size_string.Contains(".") == true ? file_size_string.IndexOf(".") + (file_size_string.IndexOf(".") > file_size_string.Length - 3 ? 2 : 3) : file_size_string.Length) + " MB";
1429             }
1430
1431             else if (file_info.Size >= 1073741824 && file_info.Size < 1099511627776)
1432             {
1433                 file_size_string = ((double)file_info.Size / 1024 / 1024 / 1024).ToString();
1434                 file_size_string = file_size_string.Substring(0, file_size_string.Contains(".") == true ? file_size_string.IndexOf(".") + (file_size_string.IndexOf(".") > file_size_string.Length - 3 ? 2 : 3) : file_size_string.Length) + " GB";
1435             }
1436
1437             else if (file_info.Size >= 1099511627776 && file_info.Size < 1125899906842624)
1438             {
1439                 file_size_string = ((double)file_info.Size / 1024 / 1024 / 1024 / 1024).ToString();
1440                 file_size_string = file_size_string.Substring(0, file_size_string.Contains(".") == true ? file_size_string.IndexOf(".") + (file_size_string.IndexOf(".") > file_size_string.Length - 3 ? 2 : 3) : file_size_string.Length) + " TB";
1441             }
1442             return file_size_string;
1443         }
1444
1445         public static double formatSizeString(string size_string)
1446         {
1447             int space_pos = size_string.IndexOf(" ");
1448
1449             double size = (double)Convert.ToDouble(size_string.Substring(0, space_pos));
1450
1451             if (size_string.Substring(space_pos + 1) == "KB")
1452                 size = size * 1024;
1453             else if (size_string.Substring(space_pos + 1) == "MB")
1454                 size = size * 1024 * 1024;
1455             else if (size_string.Substring(space_pos + 1) == "GB")
1456                 size = size * 1024 * 1024 * 1024;
1457             else if (size_string.Substring(space_pos + 1) == "TB")
1458                 size = size * 1024 * 1024 * 1024 * 1024;
1459
1460             return size;
1461         }
1462
1463         private double ConvertDateToDouble(DateTime dateTime)
1464         {
1465             string temp;
1466             string month = "", date = "", year = "", hour = "", minute = "", second = "";
1467
1468             if (dateTime.Month < 10)
1469                 month = "0";
1470
1471             month += dateTime.Month.ToString();
1472
1473             if (dateTime.Day < 10)
1474                 date = "0";
1475
1476             date += dateTime.Day.ToString();
1477
1478             if (dateTime.Year < 10)
1479                 year = "0";
1480
1481             year += dateTime.Year.ToString();
1482
1483             if (dateTime.Hour < 10)
1484                 hour = "0";
1485
1486             hour += dateTime.Hour.ToString();
1487
1488             if (dateTime.Minute < 10)
1489                 minute = "0";
1490
1491             minute += dateTime.Minute.ToString();
1492
1493             if (dateTime.Second < 10)
1494                 second = "0";
1495
1496             second += dateTime.Second.ToString();
1497
1498
1499             temp = month + date + year + "." + hour + minute + second;
1500
1501             return (double)Convert.ToDouble(temp);
1502         }
1503
1504         private string FormatTime(DateTime dateTime)
1505         {
1506             string output;
1507             string AMPM;
1508
1509             AMPM = (dateTime.Hour > 12 ? " PM" : " AM");
1510
1511             output = (dateTime.Hour == 0 ? 12 : (dateTime.Hour > 12 ? dateTime.Hour - 12 : dateTime.Hour)) + ":" + (dateTime.Minute < 10 ? "0" + dateTime.Minute.ToString() : dateTime.Minute.ToString()) + AMPM;
1512
1513             return output;
1514         }
1515
1516         private string FormatDate(DateTime dateTime)
1517         {
1518             string output;
1519
1520             output = dateTime.ToLocalTime().Month.ToString() + "/" + dateTime.Day.ToString() + "/" + dateTime.Year.ToString();
1521
1522             return output;
1523         }
1524
1525         private double RoundSize(double percent_size)
1526         {
1527             return ((double)percent_size > (int)percent_size ? (double)percent_size + 1 : (double)percent_size);
1528         }
1529
1530         private int GetCorrectArrayIndex(string in_filename, int max_filename)
1531         {
1532             int i = 0, array_out = 0, multiplier = 74, subtracter = 0x60, adder = 1;
1533
1534             in_filename = in_filename.ToLower();
1535
1536             if (max_filename > in_filename.Length)
1537                 max_filename = in_filename.Length;
1538
1539             for (i = 0; i < max_filename; i++)
1540             {
1541                 if (in_filename[i] > 0x60 && in_filename[i] < 0x7b)
1542                 {
1543                     subtracter = 0x60;
1544                     array_out += (in_filename[i] - subtracter) * adder;
1545                 }
1546                 else if (in_filename[i] > 0x2f && in_filename[i] < 0x3a)
1547                 {
1548                     subtracter = 26;
1549                     array_out += (in_filename[i] - subtracter) * adder;
1550                 }
1551                 else
1552                 {
1553                     array_out += (37 * i);
1554                 }
1555                 adder += multiplier;
1556             }
1557
1558             return array_out;
1559         }
1560
1561         public long size_minimum_size;
1562         public long size_maximum_size;
1563         public long size_minimum_index;
1564         public long size_maximum_index;
1565
1566     }
1567 }