Rayburn Davis
2014-12-01 4ea6565090d17a25fbb88a5edf8c802070d1d5f1
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             // Folders to scan
fb4519 701             List<Folder> scanFolders = new List<Folder>();
344044 702             scanFolders.AddRange(Folders);
RD 703
704             // Master file list that stores all files and folders that are in the folders that the user selected
705             DirectoryList masterFolderTable = new DirectoryList();
706
707             // Crawl the folders that the user added to the list; add them to the master file table 
fb4519 708             foreach (Folder folder in scanFolders)
344044 709             {
fb4519 710                 masterFolderTable.Add(new ObjectInfo(folder.ClientPath, 0, ObjectInfo.FileType.Folder));
344044 711             }
RD 712
713             foreach (string folder in ExcludedFolders)
714             {
715                 // Now exclude any excluded directories
716                 for (i = 0; i < masterFolderTable.Count; i++)
717                 {
718                     if (ExcludedFolders.IndexOf(masterFolderTable[i].Name) != -1)
719                     {
720                         masterFolderTable.RemoveAt(i);
721                         continue;
722                     }
723                 }
724             }
725
726             // Now crawl the master folder table
727             for(i = 0; i < masterFolderTable.Count; i++)
728             {
729                 if (Canceled == true)
730                     break;
731
732                 foreach (string folder in ExcludedFolders)
733                 {
734                     if (addFolder == false)
735                     {
736                         addFolder = true;
737                     }
738                     if (ExcludedFolders.IndexOf(masterFolderTable[i].Name) != -1)
739                     {
740                         masterFolderTable.RemoveAt(i);
741                         i--;
742                         addFolder = false;
ae7142 743                         break;
344044 744                     }
ae7142 745                 }
RD 746
747                 if (addFolder == false)
748                 {
749                     addFolder = true;
750                     continue;
751                 }
344044 752
RD 753                 try
754                 {
ae7142 755
344044 756                         DirectoryList folders = DirectoryEnumerator.Scan(masterFolderTable[i].Name);
RD 757
758                         if (addFolder == true)
759                         {
760                             foreach (ObjectInfo objectInfo in folders)
761                             {
762                                 if (objectInfo.Type == ObjectInfo.FileType.Folder)
763                                 {
764                                     FoundFolders += 1;
765
766                                     if (IncludeSubfolders == false)
767                                     {
768                                         for (j = 0; j < scanFolders.Count; j++)
769                                         {
fb4519 770                                             if (scanFolders[j].ClientPath.ToLower().IndexOf(objectInfo.Path.ToLower()) == 0 && scanFolders[j].ClientPath.ToLower() != objectInfo.Path.ToLower())
344044 771                                                 addFolder = false;
RD 772                                         }
773                                     }
774                                 }
775
776                                 if (IncludeSubfolders == true || IncludeSubfolders == false && addFolder == true)
777                                     masterFolderTable.Add(objectInfo);
778
779                                 addFolder = true;
780                             }
781                         }
782
783                         addFolder = true;
784
785                 }
786                 catch
787                 {
788                     continue;
789                 }
790             }
ae7142 791
344044 792             Status = ProgressStatusMode.CachingFiles;
RD 793
794             if (ScanMode == Mode.CompareByActualData)
795             {
796
797                 progressStep = (double)100 / masterFolderTable.Count;
798                 progress = 0;
799
800                 // Now cache all the files
801                     foreach (ObjectInfo objectInfo in masterFolderTable)
802                     {
803                         if (Canceled == true)
804                             break;
805
806                         try
807                         {
808                             if (objectInfo.Type == ObjectInfo.FileType.File)
809                             {
25f449 810                                 if(!FindSizeIndex(objectInfo.Size, FileList))
RD 811                                     AddSizeIndex(objectInfo.Size, FileList);
344044 812
RD 813                                 // TODO: Make the longs and ints uniform
814
815                                 // add the file to the list and process any settings the user may have specified (file type inclusions, etc.)
25f449 816                                 FileList[(int)objectInfo.Size].FileList.Add(objectInfo, FileTypeInclusions, ExcludedFiletypes, FileSizeBracket, MinimumSize, MaximumSize, ExcludedFolders);
344044 817
RD 818                                 FoundFiles++;
819                             }
820
821                             progress += (double)progressStep;
822                             Progress = (int)progress;
823                         }
824                         catch
825                         {
826                             continue;
827                         }
828                     }
829                 
830                 masterFolderTable.Clear();
831
832                 Status = ProgressStatusMode.IdentifyingDuplicates;
833
834                 progressStep = (double)100 / FileList.Count;
835                 progress = 0;
836
25f449 837                 var enumerator = FileList.GetEnumerator();
RD 838
839               //  SizeIndexTrackerEnumerator enumerator = new SizeIndexTrackerEnumerator(SizeIndexTracker, FileList);
344044 840                 SizeInfo sizeInfo;
25f449 841                 KeyValuePair<long, SizeInfo> currentData;
344044 842
RD 843                 // loop through the duplicates
25f449 844                 while(enumerator.MoveNext())
344044 845                 {
RD 846                     if (Canceled == true)
847                         break;
848
25f449 849                     currentData = enumerator.Current;
RD 850                     sizeInfo = currentData.Value;
344044 851
25f449 852                    // if (enumerator.Status == SizeIndexTrackerEnumerator.EnumerationStatus.Success)
RD 853                     //{
344044 854
RD 855                         if (Canceled == true)
856                             break;
857
858                         // if there is more than 1 file in the collection, read the checksum
859                         if (sizeInfo.FileList.Count >= 2)
860                         {
861                             for (j = 0; j < sizeInfo.FileList.Count; j++)
862                             {
863                                 if (Canceled == true)
864                                     break;
865                                 
866                                 sizeInfo.FileList[j].ChecksumReadStatus = ReadFileChecksum(sizeInfo.FileList[j], FileReadPercentage);
867                             }
868
869                             // now compare the checksum
870
871                             for (j = 0; j < sizeInfo.FileList.Count - 1; j++)
872                             {
873                                 if (Canceled == true)
874                                     break;
875
876                                 if (sizeInfo.FileList[j].ChecksumReadStatus == ChecksumReadStatus.Success)
877                                 {
878                                     for (k = j + 1; k < sizeInfo.FileList.Count; k++)
879                                     {
880                                         if (Canceled == true)
881                                             break;
882
883                                         if (sizeInfo.FileList[k].ChecksumReadStatus == ChecksumReadStatus.Success)
884                                         {
885                                             
886                                             if (sizeInfo.FileList[j].ChecksumSum.Sum == sizeInfo.FileList[k].ChecksumSum.Sum)
887                                             {
888
889                                                 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
890                                                     && sizeInfo.FileList[j].FileInfo.FileIndexLow == sizeInfo.FileList[k].FileInfo.FileIndexLow && sizeInfo.FileList[j].FileInfo.VolumeSerialNumber == sizeInfo.FileList[k].FileInfo.VolumeSerialNumber) // hard link
891                                                 {
892                                                     if (IgnoreLinks == true)
893                                                         continue;
894                                                     else
895                                                         hardLink = true;
896                                                 }
897
898                                                 if (duplicateFirstLoop == true)
899                                                 {
900                                                     duplicateFirstLoop = false;
901
902                                                     duplicateGroup = new DuplicateGroup();
903
904                                                     duplicateGroup.GroupName = DuplicateListView.DuplicateListView.FormatSizeString(sizeInfo.FileList[j].Size);
905
906                                                     duplicateFile = new DuplicateFile(sizeInfo.FileList[j].Name, sizeInfo.FileList[j].Size, sizeInfo.FileList[j].DateModified, sizeInfo.FileList[j].Network, sizeInfo.FileList[j].NetworkIP);
907
908                                                     if (hardLink == true)
909                                                         duplicateFile.Link = true;
910
911                                                     duplicateGroup.Add(duplicateFile);
912                                                 }
913
914                                                 duplicateFile = new DuplicateFile(sizeInfo.FileList[k].Name, sizeInfo.FileList[k].Size, sizeInfo.FileList[k].DateModified, sizeInfo.FileList[k].Network, sizeInfo.FileList[k].NetworkIP);
915
916                                                 if (hardLink == true)
917                                                     duplicateFile.Link = true;
918
25f449 919                                                 if(!duplicateGroup.Contains(duplicateFile))
RD 920                                                     duplicateGroup.Add(duplicateFile);
344044 921
RD 922                                                 sizeInfo.FileList.RemoveAt(k);
923                                                 k--;
924                                             }
925                                         }
926                                         else
927                                         {
928                                             if (Errors != null)
929                                             {
930                                                 if (sizeInfo.FileList[k].ChecksumReadStatus == ChecksumReadStatus.AccessDenied)
931                                                     Errors.AddError(sizeInfo.FileList[k].Name, "Access was denied.");
932                                                 else if (sizeInfo.FileList[k].ChecksumReadStatus == ChecksumReadStatus.FileNotFound)
933                                                     Errors.AddError(sizeInfo.FileList[k].Name, "The file was not found.");
934                                                 //  else if (size_info.file_list[k].ChecksumReadStatus == ChecksumReadStatus.UnknownError)
935                                                 //     Errors.AddError(size_info.file_list[k].Name, "An unknown error occurred.");
936                                             }
937                                         }
938                                     }
939
940
941                                     if (duplicateGroup != null)
942                                     {
943                                         GlobalBuffers.Duplicates.Add(duplicateGroup);
944                                         sizeInfo.FileList.RemoveAt(j);
945                                         j--;
946                                     }
947
948                                     duplicateGroup = null;
949
950                                     duplicateFirstLoop = true;
951                                 }
952                                 else
953                                 {
954                                     if (Errors != null)
955                                     {
956                                         if (sizeInfo.FileList[j].ChecksumReadStatus == ChecksumReadStatus.AccessDenied)
957                                             Errors.AddError(sizeInfo.FileList[j].Name, "Access was denied.");
958                                         else if (sizeInfo.FileList[j].ChecksumReadStatus == ChecksumReadStatus.FileNotFound)
959                                             Errors.AddError(sizeInfo.FileList[j].Name, "The file was not found.");
960                                         // else if (size_info.file_list[j].ChecksumReadStatus == ChecksumReadStatus.UnknownError)
961                                         //    Errors.AddError(size_info.file_list[j].Name, "An unknown error occurred.");
962                                     }
963                                 }
964
965                                 hardLink = false;
966                             }
967                         }
968
969                         progress += (double)progressStep;
970                         Progress = (int)progress;
25f449 971                 //    }
344044 972
RD 973
974                 }
975             }
976             else if (ScanMode == Mode.CompareByFilename)
977             {
978                 Status = ProgressStatusMode.CachingFiles;
979
980                 List<ObjectInfo>[] fileList = new List<ObjectInfo>[2775 + 1];
981
982                 for (i = 0; i < 2776; i++)
983                 {
984                     fileList[i] = new List<ObjectInfo>();
985                 }
986
987                 int arrayIndex;
988                 int sizeChanged = -1;
989
990                 progressStep = (double)100 / masterFolderTable.Count;
991                 progress = 0;
992
993                 // Now cache all the files
994                 foreach (ObjectInfo info in masterFolderTable)
995                 {
996                     if (Canceled == true)
997                         break;
998
999                     if (info.Type == ObjectInfo.FileType.File)
1000                     {
1001                         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))
1002                         {
1003                             arrayIndex = GetCorrectArrayIndex(Path.GetFileName(info.Name), 2);
1004
1005                             if (arrayIndex == -1)
1006                                 continue;
1007
1008                             fileList[arrayIndex].Add(info);
1009
1010                             FoundFiles++;
1011                         }
1012                     }
1013
1014                     progress += (double)progressStep;
1015                     Progress = (int)progress;
1016                 }
1017
1018                 Status = ProgressStatusMode.IdentifyingDuplicates;
1019
1020                 progressStep = (double)100 / 2776;
1021                 progress = 0;
1022
1023                 for (i = 0; i < 2776; i++)
1024                 {
1025                     if (Canceled == true)
1026                         break;
1027
1028                     if (fileList[i].Count > 1)
1029                     {
1030
1031
1032                         for (j = 0; j < fileList[i].Count-1; j++)
1033                         {
1034                             if (Canceled == true)
1035                                 break;
1036
1037                             sizeChanged = -1;
1038
1039                             for (k = j + 1; k < fileList[i].Count; k++)
1040                             {
1041                                 if (Canceled == true)
1042                                     break;
1043
1044                                 if (EvaluateFilenames(fileList[i][j], fileList[i][k]) == true)
1045                                 {
1046                                     if (duplicateFirstLoop == true)
1047                                     {
1048                                         duplicateFirstLoop = false;
1049
1050                                         duplicateGroup = new DuplicateGroup();
1051
1052                                         if (CompareByExtension == true)
1053                                             duplicateGroup.GroupName = Path.GetFileName(fileList[i][j].Name);
1054                                         else
1055                                             duplicateGroup.GroupName = Path.GetFileNameWithoutExtension(fileList[i][j].Name);
1056
1057                                         duplicateGroup.Add(new DuplicateFile(fileList[i][j].Name, fileList[i][j].Size, fileList[i][j].DateModified));
1058                                     }
1059
1060                                     if (sizeChanged == -1)
1061                                     {
1062                                         for (l = 0; l < duplicateGroup.Count; l++)
1063                                         {
1064                                             if (Canceled == true)
1065                                                 break;
1066
1067                                             if (duplicateGroup[l].Size != fileList[i][k].Size)
1068                                             {
1069                                                 sizeChanged = 1;
1070                                                 break;
1071                                             }
1072
1073                                             if (l == duplicateGroup.Count - 1)
1074                                                 sizeChanged = 0;
1075                                         }
1076                                     }
1077
1078                                     duplicateGroup.Add(new DuplicateFile(fileList[i][k].Name, fileList[i][k].Size, fileList[i][k].DateModified));
1079
1080                                     fileList[i].RemoveAt(k);
1081                                     k--;
1082
1083                                 }
1084                             }
1085
1086                             if (duplicateGroup != null)
1087                             {
1088                                 if (sizeChanged == 0)
1089                                     duplicateGroup.State = DuplicateGroup.GroupState.Undeterminable;
1090                                 else
1091                                     duplicateGroup.State = DuplicateGroup.GroupState.FalseDuplicate;
1092
1093                                 
1094
1095                                 GlobalBuffers.Duplicates.Add(duplicateGroup);   
1096                             }
1097
1098
1099                             duplicateGroup = null;
1100
1101                             duplicateFirstLoop = true;
1102                             
1103                         }
1104
1105                         
1106                     }
1107
1108                     sizeChanged = -1;
1109
1110
1111                     progress += (double)progressStep;
1112                     Progress = (int)progress;
1113                 }
1114             }
1115
1116             // cache the folders
1117             GlobalBuffers.folders.Clear();
1118             
1119             for (i = 0; i < GlobalBuffers.Duplicates.Count; i++)
1120             {
1121                 if (Canceled == true)
1122                     break;
1123
1124                 for (j = 0; j < GlobalBuffers.Duplicates[i].Count; j++)
1125                 {
1126                     if (Canceled == true)
1127                         break;
1128
1129                     if (GlobalBuffers.folders.Count == 0)
1130                         GlobalBuffers.folders.Add(Path.GetDirectoryName(GlobalBuffers.Duplicates[i][j].Filename));
1131                     else
1132                     {
1133                         for (l = 0; l < GlobalBuffers.folders.Count; l++)
1134                         {
1135                             if (Canceled == true)
1136                                 break;
1137
1138                             if (GlobalBuffers.folders[l] == Path.GetDirectoryName(GlobalBuffers.Duplicates[i][j].Filename))
1139                                 break;
1140                             else if (l == GlobalBuffers.folders.Count - 1)
1141                                 GlobalBuffers.folders.Add(Path.GetDirectoryName(GlobalBuffers.Duplicates[i][j].Filename));
1142                         }
1143                     }
1144                 }
1145             }
fb4519 1146
RD 1147             GlobalBuffers.Duplicates.Sort(new DuplicateSorter(this));
344044 1148             
RD 1149
1150             endTime = DateTime.Now;
1151             ScanTime = endTime.Subtract(start_time);
1152
1153             Status = ProgressStatusMode.Finished;
1154
1155             GC.Collect();
1156
1157             if (Errors != null)
1158                 Errors.ShowErrorWindow();
1159
1160             if (ScanComplete != null)
1161             {
1162                 ScanComplete(new ScanCompleteArgs(GlobalBuffers.Duplicates.TotalQuantity));
1163             }
1164             
1165         }
1166
1167         public bool EvaluateFilenames(ObjectInfo file1, ObjectInfo file2)
1168         {
1169             bool result = false;
1170
1171             if (CompareByExtension == true)
1172             {
1173                 if (Path.GetExtension(file1.Name) == Path.GetExtension(file2.Name))
1174                     result = true;
1175                 else
1176                     return false;
1177             }
1178
1179             if (CompareByModified == true)
1180             {
1181                 if (file1.DateModified.CompareTo(file2.DateModified) == 0)
1182                     result = true;
1183                 else
1184                     return false;
1185             }
1186
1187             if (CompareBySize == true)
1188             {
1189                 if (file1.Size == file2.Size)
1190                     result = true;
1191                 else
1192                     return false;
1193             }
1194
1195             if (Path.GetFileNameWithoutExtension(file1.Name).ToLower() == Path.GetFileNameWithoutExtension(file2.Name).ToLower())
1196                 result = true;
1197             else
1198                 result = false;
1199
1200             return result;
1201         }
1202
25f449 1203         public bool FindSizeIndex(long size, Dictionary<long, SizeInfo> fileDict)
RD 1204         {
1205             if (!fileDict.ContainsKey(size))
1206                 return false;
1207
1208             return true;
1209         }
1210
1211        /* public long FindSizeIndex(long size, SizeInfoCollection size_data)
344044 1212         {
RD 1213             int i;
1214             long percentage;
1215             List<long> indexes;
1216             ConvertedSize size_metric = SizeIndexTracker.GetSizeMetric(size);
1217
1218             if (size > size_maximum_size)
1219                 return -1;
1220             if (size < size_minimum_size)
1221                 return -1;
1222
1223             percentage = SizeIndexTracker.GetPercentage(size_metric.ConvertedLength, size_metric.Unit);
1224             indexes = SizeIndexTracker.GetPercentageIndexArray(percentage, size_metric.Unit);
1225
1226             for (i = 0; i < indexes.Count; i++)
1227             {
1228                 if (size_data[(int)indexes[i]].size == size)
1229                     return (long)indexes[i];
1230             }
1231
1232             return -1;
25f449 1233         }*/
344044 1234
25f449 1235        /* private long AddSizeIndex(long size, SizeInfoCollection size_tracker)
344044 1236         {
RD 1237             SizeInfo sd1 = new SizeInfo();
1238             long percentage;
1239             int i, new_index ;
1240             List<long> indexes;
1241             ConvertedSize size_metric = SizeIndexTracker.GetSizeMetric(size);
1242
1243             sd1.size = size;
1244
1245             percentage = SizeIndexTracker.GetPercentage(size_metric.ConvertedLength, size_metric.Unit);
1246             indexes = SizeIndexTracker.GetPercentageIndexArray(percentage, size_metric.Unit);
1247
1248             new_index = size_tracker.Add(sd1);
1249
1250             if (size > size_maximum_size)
1251                 size_maximum_size = size;
1252             if (size < size_minimum_size)
1253                 size_minimum_size = size;
1254
1255             for (i = 0; i < indexes.Count; i++)
1256             {
1257                 if (size_tracker[(int)indexes[i]].size > size)
1258                 {
1259                     indexes.Insert(i, new_index);
1260                     return new_index;
1261                 }
1262             }
1263
1264             indexes.Add(new_index);
1265
1266             return new_index;
25f449 1267         }*/
RD 1268
1269         private void AddSizeIndex(long size, Dictionary<long, SizeInfo> fileDict)
1270         {
1271             fileDict.Add(size, new SizeInfo());
344044 1272         }
RD 1273
1274         private int ReturnSortedSizeIndex(long size, SizeInfoCollection size_tracker)
1275         {
1276             int i;
1277
1278             if (size > size_maximum_size)
1279                 return size_tracker.Count;
1280
1281             if (size < size_minimum_size)
1282                 return 0;
1283
1284
1285             for (i = 0; i < size_tracker.Count; i++)
1286             {
1287                 if (size_tracker[i].size > size)
1288                 {
1289                     return i;
1290                 }
1291             }
1292
1293             return size_tracker.Count == 0 ? 0 : size_tracker.Count;
1294         }
1295
1296         private ChecksumReadStatus ReadFileChecksum(ObjectInfo fileInfo, int percent)
1297         {
1298             if (fileInfo.Checksum == null)
1299             {
1300                 byte[] temp;
1301                 int j;
1302                 double percent_size = (double)0.0;
1303                 
1304                 FileChecksum checksum1 = new FileChecksum();
1305
1306                 double progress;
1307
1308                 if (DirectoryEnumerator.Exists(fileInfo.Name))
1309                 {
1310                     FileStream file_stream;
1311                     try
1312                     {
1313                         file_stream = new FileStream(fileInfo.Name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
1314                     }
1315                     catch (UnauthorizedAccessException)
1316                     {
1317                         return ChecksumReadStatus.AccessDenied;
1318                     }
1319                     catch (FileNotFoundException)
1320                     {
1321                         return ChecksumReadStatus.FileNotFound;
1322                     }
1323                     catch (IOException)
1324                     {
1325                         return ChecksumReadStatus.UnknownError;
1326                     }
1327
1328                     FileChecksumScanInProgress = true;
1329                     FileChecksumObject = fileInfo;
1330
1331                     GetFileInformationByHandle(file_stream.SafeFileHandle.DangerousGetHandle(), out fileInfo.FileInfo);
1332
1333                     percent_size = (double)percent / 100 * file_stream.Length;
1334                     percent_size = RoundSize(percent_size);
1335
1336                     double loop_count = (double)percent_size / 2097152;
1337
1338                     FileChecksumProgress = (double)0;
1339                     progress = (double)100 / loop_count;
1340
1341                     int i;
1342                     double remainder;
1343
1344                     for (i = 0; i < (int)loop_count; i++)
1345                     {
1346                         if (Canceled == true)
1347                             break;
1348
1349                         try
1350                         {
1351                             file_stream.Read(checksum_data, 0, 2097152);
1352                         }
1353                         catch
1354                         {
1355                             file_stream.Close();
1356                             FileChecksumScanInProgress = false;
1357                             return ChecksumReadStatus.AccessDenied;
1358                         }
1359
1360                         temp = sha_handle.ComputeHash(checksum_data, 0, 2097152);
1361
1362                         for (j = 0; j < 16; j++)
1363                             checksum1.checksum[j] += temp[j];
1364
1365
1366
1367                         FileChecksumProgress += (double)progress;
1368                     }
1369                     if ((double)loop_count > (int)loop_count)
1370                     {
1371                         remainder = loop_count - (int)loop_count;
1372                         remainder = (double)remainder * 2097152;
1373
1374                         try
1375                         {
1376                             file_stream.Read(checksum_data, 0, (int)remainder);
1377                         }
1378                         catch
1379                         {
1380                             file_stream.Close();
1381                             FileChecksumScanInProgress = false;
1382                             return ChecksumReadStatus.AccessDenied;
1383                         }
1384
1385                         temp = sha_handle.ComputeHash(checksum_data, 0, (int)remainder);
1386
1387                         for (j = 0; j < 16; j++)
1388                             checksum1.checksum[j] += temp[j];
1389
1390                         //compare_prg2 += (double)compare_prg;
1391                     }
1392
1393                     file_stream.Close();
1394
1395                     fileInfo.Checksum = checksum1.checksum;
1396                     fileInfo.ChecksumSum = new ChecksumSum(ChecksumSum.returnSum(checksum1.checksum));
1397
1398                 }
1399                 else
1400                 {
1401                     FileChecksumScanInProgress = false;
1402                     return ChecksumReadStatus.FileNotFound;
1403                 }
1404
1405             }
1406             else
1407             {
1408                 FileChecksumScanInProgress = false;
1409                 return ChecksumReadStatus.UnknownError;
1410             }
1411
1412             FileChecksumScanInProgress = false;
1413
1414             return ChecksumReadStatus.Success;
1415         }
1416
1417         private static string formatSizeString(ObjectInfo file_info)
1418         {
1419             string file_size_string = "";
1420
1421             if (file_info.Size < 1024 && file_info.Size != 1)
1422                 file_size_string = file_info.Size.ToString() + " bytes";
1423
1424             else if (file_info.Size == 1)
1425                 file_size_string = file_info.Size.ToString() + " byte";
1426
1427             else if (file_info.Size >= 1024 && file_info.Size < 1048576)
1428                 file_size_string = (file_info.Size / 1024).ToString() + " KB";
1429
1430             else if (file_info.Size >= 1048576 && file_info.Size < 1073741824)
1431             {
1432                 file_size_string = ((double)file_info.Size / 1024 / 1024).ToString();
1433                 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";
1434             }
1435
1436             else if (file_info.Size >= 1073741824 && file_info.Size < 1099511627776)
1437             {
1438                 file_size_string = ((double)file_info.Size / 1024 / 1024 / 1024).ToString();
1439                 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";
1440             }
1441
1442             else if (file_info.Size >= 1099511627776 && file_info.Size < 1125899906842624)
1443             {
1444                 file_size_string = ((double)file_info.Size / 1024 / 1024 / 1024 / 1024).ToString();
1445                 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";
1446             }
1447             return file_size_string;
1448         }
1449
1450         public static double formatSizeString(string size_string)
1451         {
1452             int space_pos = size_string.IndexOf(" ");
1453
1454             double size = (double)Convert.ToDouble(size_string.Substring(0, space_pos));
1455
1456             if (size_string.Substring(space_pos + 1) == "KB")
1457                 size = size * 1024;
1458             else if (size_string.Substring(space_pos + 1) == "MB")
1459                 size = size * 1024 * 1024;
1460             else if (size_string.Substring(space_pos + 1) == "GB")
1461                 size = size * 1024 * 1024 * 1024;
1462             else if (size_string.Substring(space_pos + 1) == "TB")
1463                 size = size * 1024 * 1024 * 1024 * 1024;
1464
1465             return size;
1466         }
1467
1468         private double ConvertDateToDouble(DateTime dateTime)
1469         {
1470             string temp;
1471             string month = "", date = "", year = "", hour = "", minute = "", second = "";
1472
1473             if (dateTime.Month < 10)
1474                 month = "0";
1475
1476             month += dateTime.Month.ToString();
1477
1478             if (dateTime.Day < 10)
1479                 date = "0";
1480
1481             date += dateTime.Day.ToString();
1482
1483             if (dateTime.Year < 10)
1484                 year = "0";
1485
1486             year += dateTime.Year.ToString();
1487
1488             if (dateTime.Hour < 10)
1489                 hour = "0";
1490
1491             hour += dateTime.Hour.ToString();
1492
1493             if (dateTime.Minute < 10)
1494                 minute = "0";
1495
1496             minute += dateTime.Minute.ToString();
1497
1498             if (dateTime.Second < 10)
1499                 second = "0";
1500
1501             second += dateTime.Second.ToString();
1502
1503
1504             temp = month + date + year + "." + hour + minute + second;
1505
1506             return (double)Convert.ToDouble(temp);
1507         }
1508
1509         private string FormatTime(DateTime dateTime)
1510         {
1511             string output;
1512             string AMPM;
1513
1514             AMPM = (dateTime.Hour > 12 ? " PM" : " AM");
1515
1516             output = (dateTime.Hour == 0 ? 12 : (dateTime.Hour > 12 ? dateTime.Hour - 12 : dateTime.Hour)) + ":" + (dateTime.Minute < 10 ? "0" + dateTime.Minute.ToString() : dateTime.Minute.ToString()) + AMPM;
1517
1518             return output;
1519         }
1520
1521         private string FormatDate(DateTime dateTime)
1522         {
1523             string output;
1524
1525             output = dateTime.ToLocalTime().Month.ToString() + "/" + dateTime.Day.ToString() + "/" + dateTime.Year.ToString();
1526
1527             return output;
1528         }
1529
1530         private double RoundSize(double percent_size)
1531         {
1532             return ((double)percent_size > (int)percent_size ? (double)percent_size + 1 : (double)percent_size);
1533         }
1534
1535         private int GetCorrectArrayIndex(string in_filename, int max_filename)
1536         {
1537             int i = 0, array_out = 0, multiplier = 74, subtracter = 0x60, adder = 1;
1538
1539             in_filename = in_filename.ToLower();
1540
1541             if (max_filename > in_filename.Length)
1542                 max_filename = in_filename.Length;
1543
1544             for (i = 0; i < max_filename; i++)
1545             {
1546                 if (in_filename[i] > 0x60 && in_filename[i] < 0x7b)
1547                 {
1548                     subtracter = 0x60;
1549                     array_out += (in_filename[i] - subtracter) * adder;
1550                 }
1551                 else if (in_filename[i] > 0x2f && in_filename[i] < 0x3a)
1552                 {
1553                     subtracter = 26;
1554                     array_out += (in_filename[i] - subtracter) * adder;
1555                 }
1556                 else
1557                 {
1558                     array_out += (37 * i);
1559                 }
1560                 adder += multiplier;
1561             }
1562
1563             return array_out;
1564         }
1565
1566         public long size_minimum_size;
1567         public long size_maximum_size;
1568         public long size_minimum_index;
1569         public long size_maximum_index;
1570
1571     }
1572 }