Source

dfhl / DFHL.cpp

Full commit
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
/* DFHL.cpp : Defines the entry point for the console application.

DFHL - Duplicate File Hard Linker, a small tool to gather some space
    from duplicate files on your hard disk
Copyright (C) 2004, 2005 Jens Scheffler & Oliver Schneider

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <stdarg.h>

// Global Definitions
// *******************************************
#define LOG_ERROR			1
#define LOG_INFO			0
#define LOG_VERBOSE			-1
#define LOG_DEBUG			-2
#define MAX_PATH_LENGTH		32768
#define TEMP_BUFFER_LENGTH	65536
#define FIRST_BLOCK_SIZE	65536 // Smallerblock size
#define BLOCK_SIZE			4194304 // 4MB seems to be a good value for performance without too much memory load
#define MIN_FILE_SIZE		1024 // Minimum file size so that hard linking will be checked...

#define PROGRAM_NAME		L"Duplicate File Hard Linker"
#define PROGRAM_VERSION     L"Version 1.2a"
#define PROGRAM_AUTHOR      L"Jens Scheffler and Oliver Schneider, http://www.jensscheffler.de"

enum CompareResult {
	EQUAL,			// File compare was successful and content is matching
	SAME,			// Files are already hard linked
	SKIP,			// Files should not be processed (filter!)
	DIFFERENT		// Files differ
};

namespace
{
	// Global Variables
	// *******************************************
	/** Logging Level for the console output */
	int logLevel = LOG_INFO;
	/** Flag if list should be displayed */
	bool outputList = false;
	/** Flag if running in real or test mode */
	bool reallyLink = false;

	// Global Code
	// *******************************************
	/**
	* Method to log a message to stdout/stderr
	*/
	void logError(LPCWSTR message, ...) {
		va_list argp;
		fwprintf(stderr, L"ERROR: ");
		va_start(argp, message);
		vfwprintf(stderr, message, argp);
		va_end(argp);
		fwprintf(stderr, L"\n");
	}

	void logError(DWORD errNumber, LPCWSTR message, ...) {
		va_list argp;
		fwprintf(stderr, L"ERROR: ");
		va_start(argp, message);
		vfwprintf(stderr, message, argp);
		va_end(argp);
		LPWSTR msgBuffer = NULL;
		FormatMessage(
			FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
			NULL,
			errNumber,
			GetSystemDefaultLangID(),
			(LPWSTR)(&msgBuffer),
			0,
			NULL);
		fwprintf(stderr, L" -> [%i] %s\n", errNumber, msgBuffer);
		LocalFree(msgBuffer);
	}

	void logInfo(LPCWSTR message, ...) {
		if (logLevel <= LOG_INFO) {
			va_list argp;
			va_start(argp, message);
			vwprintf(message, argp);
			va_end(argp);
			wprintf(L"\n");
		}
	}

	void logVerbose(LPCWSTR message, ...) {
		if (logLevel <= LOG_VERBOSE) {
			va_list argp;
			wprintf(L"  ");
			va_start(argp, message);
			vwprintf(message, argp);
			va_end(argp);
			wprintf(L"\n");
		}
	}
	void logDebug(LPCWSTR message, ...) {
		if (logLevel <= LOG_DEBUG) {
			va_list argp;
			wprintf(L"    ");
			va_start(argp, message);
			vwprintf(message, argp);
			va_end(argp);
			wprintf(L"\n");
		}
	}

	// We ignore the third parameter
	inline BOOL MyCreateHardLink(LPCTSTR lpFileName, LPCTSTR lpExistingFileName, LPSECURITY_ATTRIBUTES)
	{
		return MoveFileEx(lpExistingFileName, lpFileName, MOVEFILE_CREATE_HARDLINK);
	}
}

// Global Classes
// *******************************************

class Collection {
private:
	class Item {
	public:
		void* data;

		Item* next;

		Item(void* defaultData) {
			data = defaultData;
			next = NULL;
		}

		~Item() {
		}
	};

	int itemCount;
	Item* root;
	Item* last;
	Item* nextItem;
public:
	void append(void* data) {
		Item* newItem = new Item(data);
		if (root != NULL) {
			last->next = newItem;
			last = newItem;
		} else {
			root = last = newItem;
		}
		itemCount++;
		nextItem = root;
	}

	void push(void* data) {
		append(data);
	}

	void* pop() {
		if (root != NULL) {
			Item* temp = root;
			nextItem = root = root->next;
			if (last == temp) {
				last = root;
			}
			itemCount--;
			void* result = temp->data;
			delete temp;
			return result;
		} else {
			return NULL;
		}
	}

	void* next() {
		if (nextItem != NULL) {
			Item* temp = nextItem;
			nextItem = nextItem->next;
			return temp->data;
		} else {
			return NULL;
		}
	}

	void* item(int index) {
		if (itemCount -1 >= index) {
			Item* temp = root;
			for (int i=0; i<index; i++) {
				temp = temp->next;
			}
			nextItem = temp->next;
			return temp->data;
		} else {
			return NULL;
		}
	}

	int getSize() {
		return itemCount;
	}

	Collection() {
		itemCount = 0;
		root = last= nextItem = NULL;
	}

	~Collection() {
		while (itemCount > 0)
			pop();
	}
};

class Paths {
private:
	class PathItem {
	public:
		LPWSTR path;

		PathItem(LPCWSTR newPath) {
			path = new wchar_t[wcslen(newPath)+1];
			wcscpy(path, newPath);
		}

		~PathItem() {
			delete path;
		}
	};
	Collection* col;
public:
	void add(LPCWSTR item) {
		PathItem* p = new PathItem(item);
		col->push(p);
	}

	bool pop(LPWSTR item) {
		if (col->getSize() > 0) {
			PathItem* p = (PathItem*)col->pop();
			wcscpy(item, p->path);
			delete p;
			return true;
		} else {
			return false;
		}
	}

	Paths() {
		col = new Collection();
	}

	~Paths() {
		delete col;
	}
};

class Files {
private:
	class FileItem {
	public:
		LPWSTR name;
		INT64 size;

		FileItem(LPCWSTR newName, INT64 newSize) {
			name = new wchar_t[wcslen(newName)+1];
			wcscpy(name, newName);
			size = newSize;
		}

		~FileItem() {
			delete name;
		}
	};
	Collection* col;
public:
	void add(LPCWSTR item, INT64 size) {
		FileItem* f = new FileItem(item, size);
		col->push(f);
	}

	bool pop(LPWSTR item, INT64& size) {
		if (col->getSize() > 0) {
			FileItem* f = (FileItem*)col->pop();
			wcscpy(item, f->name);
			size = f->size;
			delete f;
			return true;
		} else {
			return false;
		}
	}

	void next(LPWSTR item, INT64& size) {
		FileItem* f = (FileItem*)col->next();
		wcscpy(item, f->name);
		size = f->size;
	}

	void item(int index, LPWSTR item, INT64& size) {
		FileItem* f = (FileItem*)col->item(index);
		wcscpy(item, f->name);
		size = f->size;
	}

	int getSize() {
		return col->getSize();
	}

	Files() {
		col = new Collection();
	}

	~Files() {
		delete col;
	}
};


class Duplicates {
private:
	class DuplicateItem {
	public:
		LPWSTR name1;
		LPWSTR name2;
		INT64 size;

		DuplicateItem(LPCWSTR newName1, LPCWSTR newName2, INT64 newSize) {
			name1 = new wchar_t[wcslen(newName1)+1];
			wcscpy(name1, newName1);
			name2 = new wchar_t[wcslen(newName2)+1];
			wcscpy(name2, newName2);
			size = newSize;
		}

		~DuplicateItem() {
			delete name1;
			delete name2;
		}
	};
	Collection* col;
	INT64 byteSum;
	int fileCount;
public:
	void add(LPCWSTR item1, LPCWSTR item2, INT64 size) {
		DuplicateItem* d = new DuplicateItem(item1, item2, size);
		col->push(d);
		fileCount++;
		byteSum += size;
	}

	bool pop(LPWSTR item1, LPWSTR item2, INT64& size) {
		if (col->getSize() > 0) {
			DuplicateItem* d = (DuplicateItem*)col->pop();
			wcscpy(item1, d->name1);
			wcscpy(item2, d->name2);
			size = d->size;
			delete d;
			return true;
		} else {
			return false;
		}
	}

	bool next(LPWSTR item1, LPWSTR item2, INT64& size) {
		DuplicateItem* d = (DuplicateItem*)col->next();
		if (d != NULL) {
			wcscpy(item1, d->name1);
			wcscpy(item2, d->name2);
			size = d->size;
			return true;
		} else {
			return false;
		}
	}

	int getSize() {
		return col->getSize();
	}

	int getFileCount() {
		return fileCount;
	}

	INT64 getByteSum() {
		return byteSum;
	}

	Duplicates() {
		col = new Collection();
		byteSum = 0;
		fileCount = 0;
	}

	~Duplicates() {
		delete col;
	}
};

/**
* Duplicate File Linker Class
*/
class DuplicateFileHardLinker {

private:
	/** Collection of paths to process */
	Paths* p;
	/** Collection of files to check */
	Files* f;
	/** List of duplicates to process */
	Duplicates* d;
	/** buffer variables for file compare */
	LPBYTE block1;
	LPBYTE block2;
	/** Flag if attributes of file need to match */
	bool attributeMustMatch;
	/** Flag if hidden files should be processed */
	bool hiddenFiles;
	/** Flag if junctions should be followed */
	bool followJunctions;
	/** Flag if small files (<1024 bytes) should be processed */
	bool smallFiles;
	/** Flag if recursive processing should be enabled */
	bool recursive;
	/** Flag if system files should be processed */
	bool systemFiles;
	/** Flag if timestamps of file need to match */
	bool dateTimeMustMatch;

	/**
	* Logs a found file to debug
	*/
	void logFile(WIN32_FIND_DATA FileData) {
		INT64 size = FileData.nFileSizeLow + ((INT64)MAXDWORD + 1) * FileData.nFileSizeHigh;
		logDebug(L"Found file \"%s\" (Size=%I64i,%s%s%s%s%s%s%s%s%s%s%s%s)", 
			FileData.cFileName,
			size,
			FILE_ATTRIBUTE_ARCHIVE      &FileData.dwFileAttributes?L"ARCHIVE ":L"",
			FILE_ATTRIBUTE_COMPRESSED   &FileData.dwFileAttributes?L"COMPRESSED ":L"",
			FILE_ATTRIBUTE_DIRECTORY    &FileData.dwFileAttributes?L"DIRECTORY ":L"",
			FILE_ATTRIBUTE_ENCRYPTED    &FileData.dwFileAttributes?L"ENCRYPTED ":L"",
			FILE_ATTRIBUTE_HIDDEN       &FileData.dwFileAttributes?L"HIDDEN ":L"",
			FILE_ATTRIBUTE_NORMAL       &FileData.dwFileAttributes?L"NORMAL ":L"",
			FILE_ATTRIBUTE_OFFLINE      &FileData.dwFileAttributes?L"OFFLINE ":L"",
			FILE_ATTRIBUTE_READONLY     &FileData.dwFileAttributes?L"READONLY ":L"",
			FILE_ATTRIBUTE_REPARSE_POINT&FileData.dwFileAttributes?L"REPARSE_POINT ":L"",
			FILE_ATTRIBUTE_SPARSE_FILE  &FileData.dwFileAttributes?L"SPARSE ":L"",
			FILE_ATTRIBUTE_SYSTEM       &FileData.dwFileAttributes?L"SYSTEM ":L"",
			FILE_ATTRIBUTE_TEMPORARY    &FileData.dwFileAttributes?L"TEMP ":L"");
	}

	/**
	* Compares the given 2 files content
	*/
	CompareResult compareFiles(LPCWSTR file1, LPCWSTR file2, INT64 size) {
		// doublecheck data consistency!
		if (wcscmp(file1, file2) == 0) {
			logError(L"Same file \"%s\"found as duplicate, ignoring!", file1);
			return DIFFERENT;
		}

		// Open File 1
		HANDLE hFile1 = CreateFile(file1, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
		if (hFile1 == INVALID_HANDLE_VALUE) {
			logError(L"Unable to open file \"%s\"", file1);
			return DIFFERENT;
		}

		// Open File 2
		HANDLE hFile2 = CreateFile(file2, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
		if (hFile2 == INVALID_HANDLE_VALUE) {
			logError(L"Unable to open file \"%s\"", file2);
			CloseHandle(hFile1);
			return DIFFERENT;
		}

		// Check file system information details...
		BY_HANDLE_FILE_INFORMATION info1;
		BY_HANDLE_FILE_INFORMATION info2;
		if (GetFileInformationByHandle(hFile1, &info1) && GetFileInformationByHandle(hFile2, &info2)) {
			// First check if the files are already hard-linked...
			if (info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber &&
				info1.nFileIndexHigh == info2.nFileIndexHigh &&
				info1.nFileIndexLow == info2.nFileIndexLow) {

					logVerbose(L"Files are already hard linked, skipping.");
					CloseHandle(hFile1);
					CloseHandle(hFile2);
					return SAME;
			}

			// check for attributes matching
			if (attributeMustMatch && info1.dwFileAttributes != info2.dwFileAttributes) {
				logVerbose(L"Attributes of files do not match, skipping.");
				CloseHandle(hFile1);
				CloseHandle(hFile2);
				return SKIP;
			}

			// check for time stamp matching
			if (dateTimeMustMatch && (
				info1.ftLastWriteTime.dwHighDateTime != info2.ftLastWriteTime.dwHighDateTime ||
				info1.ftLastWriteTime.dwLowDateTime != info2.ftLastWriteTime.dwLowDateTime
				)) {
					logVerbose(L"Modification timestamps of files do not match, skipping.");
					CloseHandle(hFile1);
					CloseHandle(hFile2);
					return SKIP;
			}
		} else {
			logInfo(L"Unable to read further file information, skipping.");
			CloseHandle(hFile1);
			CloseHandle(hFile2);
			return SKIP;
		}

		// Read File Content and compare
		if (block1 == NULL) {
			block1 = new BYTE[BLOCK_SIZE];
		}
		if (block2 == NULL) {
			block2 = new BYTE[BLOCK_SIZE];
		}
		DWORD blockSize = FIRST_BLOCK_SIZE; // Note: For the first block read smaller amount to speed up...
		DWORD read1;
		DWORD read2;
		DWORD result1;
		DWORD result2;
		INT64 bytesToRead = size;
		bool switcher = true; // helper variable for performance optimization
		while (bytesToRead > 0) {

			// Read Blocks - Performance boosted: read alternating to mimimize head shifts... ;-)
			if (switcher) {
				result1 = ReadFile(hFile1, block1, blockSize, &read1, NULL);
				result2 = ReadFile(hFile2, block2, blockSize, &read2, NULL);
			} else {
				result2 = ReadFile(hFile2, block2, blockSize, &read2, NULL);
				result1 = ReadFile(hFile1, block1, blockSize, &read1, NULL);
			}

			// change the state for the next read operation
			switcher = !switcher; // alternate read
			blockSize = BLOCK_SIZE; // use bigger block size

			// Compare Data
			bytesToRead -= read1;
			if (read1 != read2 || read1 == 0) {
				logError(L"File length differ or read error! This _should_ not happen!?!?");
				CloseHandle(hFile2);
				CloseHandle(hFile1);
				return DIFFERENT;
			}

			for (DWORD i = 0; i < read1; i++) {
				if (block1[i] != block2[i]) {
					logVerbose(L"Files differ in content.");
					CloseHandle(hFile2);
					CloseHandle(hFile1);
					return DIFFERENT;
				}
			}
		}

		// Close File 2
		CloseHandle(hFile2);

		// Close File 1
		CloseHandle(hFile1);

		logVerbose(L"Files are equal, hard link possible.");
		return EQUAL;
	}

	/**
	* Adds a file to the collection of files to process
	* @param file FindFile Structure of further file information
	*/
	void addFile(LPCWSTR file, WIN32_FIND_DATA details) {
		f->add(file, details.nFileSizeLow + ((INT64)MAXDWORD + 1) * details.nFileSizeHigh);
	}

	/**
	* Compares the given 2 files content
	*/
	void addDuplicate(LPCWSTR file1, LPCWSTR file2, INT64 size) {
		d->add(file1, file2, size);
	}

	/**
	* Adds a found entry in the file system into the collection iof items to be processed
	* This function also applies all selected filters of the user
	* @param item FindFile Structure of further file information
	*/
	void addItem(LPCWSTR base, WIN32_FIND_DATA item) {
		// check if this is a valid file and not a dummy like "." or ".."
		if (wcscmp(item.cFileName, L".") == 0 || wcscmp(item.cFileName, L"..") == 0) {
			// just ignore these entries
			return;
		}

		logFile(item);

		// check if it's a directory entry...
		LPWSTR fullPath = new wchar_t[MAX_PATH_LENGTH];
		wsprintf(fullPath, L"%s\\%s", base, item.cFileName);
		if (item.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) {

			// check for recursice setting
			if (!recursive) {
				logDebug(L"skipping folder, not running recursive");
				delete fullPath;
				return;
			}

			// check for junction
			if (!followJunctions && item.dwFileAttributes&FILE_ATTRIBUTE_REPARSE_POINT) {
				logDebug(L"ignoring junction");
				delete fullPath;
				return;
			}

			// add the path to the collection
			addPath(fullPath);

		} else {

			// check if this is a hidden file
			if (!hiddenFiles && item.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) {
				logDebug(L"ignoring file, hidden attribute is set");
				delete fullPath;
				return;
			}

			// check if the file is "big" enough
			if (!smallFiles && (item.nFileSizeLow > 0) && (item.nFileSizeLow < MIN_FILE_SIZE) && (item.nFileSizeHigh == 0)) {
				logDebug(L"ignoring file, is too small.");
				delete fullPath;
				return;
			}

			// check if this is a system file
			if (!systemFiles && (item.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)) {
				logDebug(L"ignoring file, system attribute is set");
				delete fullPath;
				return;
			}

			// add the file only if it contains data!
			if ((item.nFileSizeLow > 0) || (item.nFileSizeHigh > 0)) {
				addFile(fullPath, item);
			}
		}
		delete fullPath;
	}

	/**
	* Links two files on hard disk by deleting one and linking the name of this to the other
	* @param file1 File name of the first file
	* @param file2 File name of the second file to link to
	* @return boolean value if operation was successful
	*/
	bool hardLinkFiles(LPCWSTR file1, LPCWSTR file2) {
		LPWSTR file2Backup = new wchar_t[MAX_PATH_LENGTH];

		logInfo(L"Linking %s and %s", file1, file2);

		// Step 1: precaution - rename original file
		wsprintf(file2Backup, L"%s_backup", file2);
		if (!MoveFile(file2, file2Backup)) {
			logError(L"Unable to move file to backup: %i", GetLastError());
			delete file2Backup;
			return false;
		}

		// Step 2: create hard link
		if (!MyCreateHardLink(file2, file1, NULL)) {
			logError(L"Unable to create hard link: %i", GetLastError());
			delete file2Backup;
			return false;
		}

		// Step 3: remove backup file (orphan)
		if (!DeleteFile(file2Backup)) {
			logError(L"Unable to delete file: %i, trying to change attribute...", GetLastError());

			if (!SetFileAttributes(file2Backup, FILE_ATTRIBUTE_NORMAL) ||
				!DeleteFile(file2Backup)) {

					logError(L"Finally unable to delete file: %i", GetLastError());
					delete file2Backup;
					return false;
			}
		}

		return true;
	}

public:

	/**
	* Constructor for the Object
	*/
	DuplicateFileHardLinker() {
		p = new Paths();
		f = new Files();
		d = new Duplicates();
		block1 = block2 = NULL;
		attributeMustMatch = false;
		hiddenFiles = false;
		followJunctions = false;
		smallFiles = false;
		recursive = false;
		systemFiles = false;
		dateTimeMustMatch = false;
	}

	~DuplicateFileHardLinker() {
		delete p;
		delete f;
		delete d;
		if (block1 != NULL) {
			delete block1;
		}
		if (block2 != NULL) {
			delete block2;
		}
	}

	/**
	* Setter for the attriute match flag
	*/
	void setAttributeMustMatch(bool newValue) {
		attributeMustMatch = newValue;
	}

	/**
	* Setter for the hidden files flag
	*/
	void setHiddenFiles(bool newValue) {
		hiddenFiles = newValue;
	}

	/**
	* Setter for the junctions follow flag
	*/
	void setFollowJunctions(bool newValue) {
		followJunctions = newValue;
	}

	/**
	* Setter for the small files flag
	*/
	void setSmallFiles(bool newValue) {
		smallFiles = newValue;
	}

	/**
	* Setter for the small files flag
	*/
	void setRecursive(bool newValue) {
		recursive = newValue;
	}

	/**
	* Setter for the system files flag
	*/
	void setSystemFiles(bool newValue) {
		systemFiles = newValue;
	}

	/**
	* Setter for the attriute match flag
	*/
	void setDateMatch(bool newValue) {
		dateTimeMustMatch = newValue;
	}

	/**
	* Adds a path to the collection of path's to process
	* @param path Path to add to the collection
	*/
	void addPath(LPCWSTR path) {
		p->add(path);
	}

	/**
	* Starts the search for duplicate files
	*/
	void findDuplicates() {
		// Step 1: Walk through the directory tree
		logInfo(L"Parsing Directory Tree...");
		LPWSTR folder = new wchar_t[MAX_PATH_LENGTH];
		while (p->pop(folder)) {
			logVerbose(L"Parsing Folder %s", folder);

			WIN32_FIND_DATA FindFileData;
			HANDLE hFind = INVALID_HANDLE_VALUE;
			wchar_t DirSpec[MAX_PATH_LENGTH];  // directory specification
			DWORD dwError;

			size_t len = wcslen(wcsncpy(DirSpec, folder, wcslen(folder)+1));
			// Do not append backslash if this is already the last character!
			if(DirSpec[len] != L'\\')
				wcsncat(DirSpec, L"\\", 2);
			wcsncat(DirSpec, L"*", 2);

			hFind = FindFirstFile(DirSpec, &FindFileData);

			if (hFind == INVALID_HANDLE_VALUE) {
				// Accessing "<drive>:\System Volume Information\*" gives an
				// ERROR_ACCESS_DENIED. So this has to be fixed to scan whole
				// volumes! Also can happen on folders with no access permissions.
				logError(GetLastError(), L"Unable to read folder content.");
			} else {
				addItem(folder, FindFileData);
				while (FindNextFile(hFind, &FindFileData) != 0) {
					addItem(folder, FindFileData);
				}

				dwError = GetLastError();
				FindClose(hFind);
				if (dwError != ERROR_NO_MORE_FILES) {
					LPWSTR buffer = new wchar_t[TEMP_BUFFER_LENGTH];
					wsprintf(buffer, L"FindNextFile error. Error is %u\n", dwError);
					throw buffer;
				}
			}
		}

		// Step 2: Walk over all relevant files
		logInfo(L"Found %i Files in folders, comparing relevant files.", f->getSize());
		LPWSTR file1 = new wchar_t[MAX_PATH_LENGTH];
		INT64 size1;
		LPWSTR file2 = new wchar_t[MAX_PATH_LENGTH];
		INT64 size2;
		bool duplicateChecked;
		while (f->pop(file1, size1)) {
			duplicateChecked = false;
			for (int i = 0; i < f->getSize() && !duplicateChecked; i++) {
				f->next(file2, size2);
				if (size1 == size2) {
					logVerbose(L"File \"%s\" and \"%s\" have both size of %I64i comparing...", file1, file2, size1);

					// Compare the both files with same size
					DWORD start = GetTickCount();
					DWORD time = 0;
					switch (compareFiles(file1, file2, size1))
					{
					case EQUAL:
						time = GetTickCount() - start;
						logDebug(L"file compare took %ims, %I64i KB/s", time, time>0?size1*2*1000 / time / 1024:0);

						// Files seem to be equal, marking them for later processing...
						addDuplicate(file1, file2, size1);
						duplicateChecked = true;
						break;
					case SAME:
						// In case the files are already hard linked, we skip further checks and check the second file later...
						duplicateChecked = true;
						break;
					case SKIP:
						// okay, it seems that this pair should not be processed...
						duplicateChecked = true;
						break;
					case DIFFERENT:
						// yeah, just do nothing.
						break;
					}
				}
			}
		}

		// Step 3: Show search results
		logInfo(L"Found %i duplicate files, savings of %I64i bytes possible.", d->getFileCount(), d->getByteSum());

		delete file2;
		delete file1;
		delete folder;
	}

	/**
	* Processes all duplicates and crestes hard links of the files
	*/
	void linkAllDuplicates() {
		LPWSTR file1 = new wchar_t[MAX_PATH_LENGTH];
		LPWSTR file2 = new wchar_t[MAX_PATH_LENGTH];
		INT64 size;
		INT64 sumSize = 0;

		if (d->getSize() > 0) {
			// Loop over all found files...
			logInfo(L"Hard linking %i duplicate files", d->getSize());
			while (d->pop(file1, file2, size)) {
				sumSize += size;
				if (!hardLinkFiles(file1, file2)) {
					logInfo(L"Unable to process links for \"%s\" and \"%s\"", file1, file2);
				}
			}
			logInfo(L"Hard linking done, %I64i bytes saved.", sumSize);
		} else {
			logInfo(L"No files found for linking");
		}

		delete file2;
		delete file1;
	}

	/**
	* Displays the result duplicate list to stdout
	*/
	void listDuplicates() {
		LPWSTR file1 = new wchar_t[MAX_PATH_LENGTH];
		LPWSTR file2 = new wchar_t[MAX_PATH_LENGTH];
		INT64 size;

		if (d->next(file1, file2, size)) {
			logInfo(L"Result of duplicate analysis:");
			do {
				logInfo(L"%I64i bytes: %s = %s", size, file1, file2);
			} while (d->next(file1, file2, size));
		} else {
			logInfo(L"No duplicates to list.");
		}

		delete file2;
		delete file1;
	}
};

/**
* Helper function to parse the command line
* @param argc Argument Counter
* @param argv Argument Vector
* @param prog Program Instance Reference to fill with options
*/
bool parseCommandLine(int argc, char* argv[], DuplicateFileHardLinker* prog) {
	bool pathAdded = false;

	// iterate over all arguments...
	for (int i=1; i<argc; i++) {

		// first check if command line option
		if (argv[i][0] == '-' || argv[i][0] == '/') {

			if (strlen(argv[i]) == 2) {
				switch (argv[i][1]) {
				case '?':
					// Show program usage
					wchar_t programName[MAX_PATH_LENGTH];
					mbstowcs(programName,argv[0],sizeof(programName));
					logInfo(PROGRAM_NAME);
					logInfo(L"Program to link duplicate files in several paths on one disk.");
					logInfo(L"%s - %s", PROGRAM_VERSION, PROGRAM_AUTHOR);
					logInfo(L"");
					logInfo(L"NOTE: Use this tool on your own risk!");
					logInfo(L"");
					logInfo(L"Usage:");
					logInfo(L"%s [options] [path] [...]", programName);
					logInfo(L"Options:");
					logInfo(L"/?\tShows this help screen");
					logInfo(L"/a\tFile attributes must match for linking");
					logInfo(L"/d\tDebug Mode");
					logInfo(L"/h\tProcess hidden files");
					logInfo(L"/j\tAlso follow junctions (=reparse points) in filesystem");
					logInfo(L"/l\tHard links for files. If not specified, tool will just read (test) for duplicates");
					logInfo(L"/m\tAlso Process small files <1024 bytes, they are skipped by default");
					logInfo(L"/o\tList duplicate file result to stdout");
					logInfo(L"/q\tSilent Mode");
					logInfo(L"/r\tRuns recursively through the given folder list");
					logInfo(L"/s\tProcess system files");
					logInfo(L"/t\tTime + Date of files must match");
					logInfo(L"/v\tVerbose Mode");
					throw L""; //just to terminate the program...
					break;
				case 'a':
					prog->setAttributeMustMatch(true);
					break;
				case 'd':
					logLevel = LOG_DEBUG;
					break;
				case 'h':
					prog->setHiddenFiles(true);
					break;
				case 'j':
					prog->setFollowJunctions(true);
					break;
				case 'l':
					reallyLink = true;
					break;
				case 'm':
					prog->setSmallFiles(true);
					break;
				case 'o':
					outputList = true;
					break;
				case 'q':
					logLevel = LOG_ERROR;
					break;
				case 'r':
					prog->setRecursive(true);
					break;
				case 's':
					prog->setSystemFiles(true);
					break;
				case 't':
					prog->setDateMatch(true);
					break;
				case 'v':
					logLevel = LOG_VERBOSE;
					break;
				default:
					logError(L"Illegal Command line option! Use /? to see valid options!");
					return false;
				}
			} else {
				logError(L"Illegal Command line option! Use /? to see valid options!");
				return false;
			}
		} else {
			// the command line options seems to be a path...
			wchar_t tmpPath[MAX_PATH_LENGTH];
			mbstowcs(tmpPath,argv[i],sizeof(tmpPath));

			// check if the path is existing!
			wchar_t DirSpec[MAX_PATH_LENGTH];  // directory specification
			wcsncpy(DirSpec, tmpPath, wcslen(tmpPath)+1);
			wcsncat(DirSpec, L"\\*", 3);
			WIN32_FIND_DATA FindFileData;
			HANDLE hFind = FindFirstFile(DirSpec, &FindFileData);
			if (hFind == INVALID_HANDLE_VALUE) {
				logError(L"Specified directory \"%s\" does not exist", tmpPath);
				return false;
			}

			prog->addPath(tmpPath);
			pathAdded = true;
		}
	}

	// check for parameters
	if (!pathAdded) {
		logError(L"You need to specify at least one folder to process!\nUse /? to see valid options!");
		return false;
	}

	return true;
}

/**
* Main runnable and entry point for executing the application
* @param argc Argument Counter
* @param argv Argument Vector
* @return Application Return code
*/
int __cdecl main(int argc, char* argv[])
{
	int result = 0;

	DuplicateFileHardLinker* prog = new DuplicateFileHardLinker();

	try {
		// parse the command line
		if (!parseCommandLine(argc, argv, prog)) {
			return -1;
		}

		// show desired option info
		logInfo(PROGRAM_NAME);
		logInfo(L"%s - %s", PROGRAM_VERSION, PROGRAM_AUTHOR);
		logInfo(L"");

		// find duplicates
		prog->findDuplicates();

		if (outputList) {
			prog->listDuplicates();
		}

		if (reallyLink) {
			// link duplicates
			prog->linkAllDuplicates();
		} else {
			logInfo(L"Skipping real linking. To really create hard links, use the /l switch.");
		}
	} catch (LPCWSTR err) {
		DWORD dwError = GetLastError();
		if (wcslen(err) > 0) {
			if (dwError != 0) {
				logError(dwError, err);
			} else {
				logError(err);
			}
		}
		result = -1;
	}

	delete prog;
	return result;
}