-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkGui.cpp
More file actions
1630 lines (1375 loc) · 52.7 KB
/
LinkGui.cpp
File metadata and controls
1630 lines (1375 loc) · 52.7 KB
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
// LinkGui.cpp - pure WinAPI app with a Win10+ backend and classic UI (no mklink.exe).
// Query opens a separate window with a structured GUI (checkboxes for booleans/flags).
//
// Build (VS2022):
// cl /EHsc /DUNICODE /D_UNICODE /DWIN32_LEAN_AND_MEAN /D_CRT_SECURE_NO_WARNINGS LinkGui.cpp user32.lib gdi32.lib comctl32.lib
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0A00
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <commctrl.h>
#include <string>
#include <vector>
#include <cstdarg>
#pragma comment(lib, "comctl32.lib")
// ---- Compatibility defines ----
#ifndef FILE_FLAG_OPEN_REPARSE_POINT
#define FILE_FLAG_OPEN_REPARSE_POINT 0x00200000
#endif
#ifndef SYMBOLIC_LINK_FLAG_DIRECTORY
#define SYMBOLIC_LINK_FLAG_DIRECTORY 0x1
#endif
#ifndef SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
#define SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE 0x2
#endif
#ifndef IO_REPARSE_TAG_MOUNT_POINT
#define IO_REPARSE_TAG_MOUNT_POINT 0xA0000003L
#endif
#ifndef IO_REPARSE_TAG_SYMLINK
#define IO_REPARSE_TAG_SYMLINK 0xA000000CL
#endif
#ifndef IO_REPARSE_TAG_APPEXECLINK
#define IO_REPARSE_TAG_APPEXECLINK 0x8000001BL
#endif
#ifndef FSCTL_GET_REPARSE_POINT
#define FSCTL_GET_REPARSE_POINT 0x000900A8
#endif
#ifndef FSCTL_SET_REPARSE_POINT
#define FSCTL_SET_REPARSE_POINT 0x000900A4
#endif
#ifndef FSCTL_DELETE_REPARSE_POINT
#define FSCTL_DELETE_REPARSE_POINT 0x000900AC
#endif
#ifndef MAXIMUM_REPARSE_DATA_BUFFER_SIZE
#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE (16 * 1024)
#endif
#ifndef REPARSE_DATA_BUFFER_HEADER_SIZE
#define REPARSE_DATA_BUFFER_HEADER_SIZE 8
#endif
#ifndef SYMLINK_FLAG_RELATIVE
#define SYMLINK_FLAG_RELATIVE 0x00000001
#endif
#ifndef FILE_SUPPORTS_REPARSE_POINTS
#define FILE_SUPPORTS_REPARSE_POINTS 0x00000080
#endif
#ifndef FILE_SUPPORTS_HARD_LINKS
#define FILE_SUPPORTS_HARD_LINKS 0x00400000
#endif
// Minimal REPARSE_DATA_BUFFER (mount point + symlink)
typedef struct _REPARSE_DATA_BUFFER {
ULONG ReparseTag;
USHORT ReparseDataLength;
USHORT Reserved;
union {
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
WCHAR PathBuffer[1];
} MountPointReparseBuffer;
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
ULONG Flags;
WCHAR PathBuffer[1];
} SymbolicLinkReparseBuffer;
struct {
UCHAR DataBuffer[1];
} GenericReparseBuffer;
};
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
// ---- IDs (main window) ----
enum {
IDC_TYPE = 1001,
IDC_LINK = 1002,
IDC_TARGET = 1003,
IDC_CREATE = 1004,
IDC_DELETE = 1005,
IDC_QUERY = 1006,
IDC_UPDATE = 1007,
IDC_MAINLOG = 1008,
IDC_LBL_TYPE = 1101,
IDC_LBL_LINK = 1102,
IDC_LBL_TGT = 1103
};
// ---- IDs (query window) ----
enum {
QIDC_G_LINK = 2001,
QIDC_G_REP = 2002,
QIDC_G_TGT = 2003,
QIDC_G_EXTRA = 2004,
// link labels/edits
QIDC_S_PATH = 2100,
QIDC_E_PATH = 2101,
QIDC_S_FID = 2102,
QIDC_E_FID = 2103,
QIDC_S_VSER = 2104,
QIDC_E_VSER = 2105,
QIDC_S_SIZE = 2106,
QIDC_E_SIZE = 2107,
QIDC_S_CTIME = 2108,
QIDC_E_CTIME = 2109,
QIDC_S_MTIME = 2110,
QIDC_E_MTIME = 2111,
QIDC_S_ATIME = 2112,
QIDC_E_ATIME = 2113,
QIDC_CB_ISDIR = 2120,
QIDC_CB_ISREPARSE = 2121,
QIDC_CB_LV_REPARSE = 2130,
QIDC_CB_LV_HARDLINK = 2131,
// attribute flags checkboxes
QIDC_CB_A_READONLY = 2160,
QIDC_CB_A_HIDDEN = 2161,
QIDC_CB_A_SYSTEM = 2162,
QIDC_CB_A_ARCHIVE = 2163,
QIDC_CB_A_TEMP = 2164,
QIDC_CB_A_SPARSE = 2165,
QIDC_CB_A_COMP = 2166,
QIDC_CB_A_ENCRYPT = 2167,
QIDC_CB_A_OFFLINE = 2168,
QIDC_CB_A_REPARSE = 2169,
// reparse labels/edits
QIDC_S_RTAG = 2200,
QIDC_E_RTAG = 2201,
QIDC_CB_R_SYMLINK = 2202,
QIDC_CB_R_JUNCTION = 2203,
QIDC_CB_R_OTHER = 2204,
QIDC_CB_R_RELATIVE = 2205,
QIDC_S_SUB = 2210,
QIDC_E_SUB = 2211,
QIDC_S_PRN = 2212,
QIDC_E_PRN = 2213,
QIDC_S_IMM = 2214,
QIDC_E_IMM = 2215,
QIDC_CB_IMM_EXISTS = 2216,
// target labels/edits
QIDC_CB_FOLLOW_OK = 2250,
QIDC_S_FINAL = 2251,
QIDC_E_FINAL = 2252,
QIDC_S_TFID = 2253,
QIDC_E_TFID = 2254,
QIDC_S_TVSER = 2255,
QIDC_E_TVSER = 2256,
QIDC_S_TSIZE = 2257,
QIDC_E_TSIZE = 2258,
QIDC_S_TCTIME = 2259,
QIDC_E_TCTIME = 2260,
QIDC_S_TMTIME = 2261,
QIDC_E_TMTIME = 2262,
QIDC_S_TATIME = 2263,
QIDC_E_TATIME = 2264,
QIDC_CB_TV_REPARSE = 2270,
QIDC_CB_TV_HARDLINK = 2271,
// extra
QIDC_E_EXTRA = 2401
};
static HINSTANCE g_hInst = NULL;
static HFONT g_hFont = NULL;
// ---- Helpers ----
static void SetCtlFont(HWND h) { if (g_hFont) SendMessageW(h, WM_SETFONT, (WPARAM)g_hFont, TRUE); }
static std::wstring GetTextW(HWND h)
{
int len = GetWindowTextLengthW(h);
std::wstring s;
s.resize(len);
if (len > 0) GetWindowTextW(h, &s[0], len + 1);
return s;
}
static void SetTextW(HWND h, const std::wstring& s) { SetWindowTextW(h, s.c_str()); }
static void AppendTextW(HWND h, const std::wstring& s)
{
int len = GetWindowTextLengthW(h);
SendMessageW(h, EM_SETSEL, (WPARAM)len, (LPARAM)len);
SendMessageW(h, EM_REPLACESEL, FALSE, (LPARAM)s.c_str());
}
static std::wstring WinErrW(DWORD err)
{
WCHAR buf[512] = {0};
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, 0, buf, (DWORD)(sizeof(buf)/sizeof(buf[0]) - 1), NULL);
if (buf[0] == 0) {
WCHAR tmp[64];
swprintf_s(tmp, L"Error %lu", err);
return tmp;
}
return buf;
}
static std::wstring Fmt(const wchar_t* fmt, ...)
{
wchar_t buf[2048];
buf[0] = 0;
va_list ap;
va_start(ap, fmt);
_vsnwprintf_s(buf, _countof(buf), _TRUNCATE, fmt, ap);
va_end(ap);
return std::wstring(buf);
}
static std::wstring GetFullPathW(const std::wstring& p)
{
DWORD need = GetFullPathNameW(p.c_str(), 0, NULL, NULL);
if (!need) return p;
std::wstring out;
out.resize(need);
DWORD got = GetFullPathNameW(p.c_str(), need, &out[0], NULL);
if (!got) return p;
while (!out.empty() && out.back() == L'\0') out.pop_back();
return out;
}
static std::wstring DirOfPathW(const std::wstring& p)
{
size_t pos = p.find_last_of(L"\\/");
if (pos == std::wstring::npos) return L"";
return p.substr(0, pos);
}
static std::wstring JoinPathW(const std::wstring& a, const std::wstring& b)
{
if (a.empty()) return b;
if (b.empty()) return a;
if (a.back() == L'\\' || a.back() == L'/') return a + b;
return a + L"\\" + b;
}
static std::wstring StripNtPrefixW(const std::wstring& p)
{
if (p.rfind(L"\\??\\", 0) == 0) return p.substr(4);
if (p.rfind(L"\\\\?\\", 0) == 0) return p.substr(4);
return p;
}
static std::wstring FileTimeToTextLocalW(const FILETIME& ft)
{
FILETIME lft;
SYSTEMTIME st;
if (!FileTimeToLocalFileTime(&ft, &lft)) return L"(n/a)";
if (!FileTimeToSystemTime(&lft, &st)) return L"(n/a)";
return Fmt(L"%04u-%02u-%02u %02u:%02u:%02u",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
}
static void SetCheck(HWND h, bool on)
{
SendMessageW(h, BM_SETCHECK, on ? BST_CHECKED : BST_UNCHECKED, 0);
}
static const wchar_t* ReparseTagNameW(ULONG tag)
{
switch (tag) {
case IO_REPARSE_TAG_SYMLINK: return L"IO_REPARSE_TAG_SYMLINK";
case IO_REPARSE_TAG_MOUNT_POINT: return L"IO_REPARSE_TAG_MOUNT_POINT";
case IO_REPARSE_TAG_APPEXECLINK: return L"IO_REPARSE_TAG_APPEXECLINK";
default: return L"(unknown)";
}
}
// ---- Data structures for Query window ----
struct VolumeInfo {
bool ok = false;
std::wstring root;
std::wstring volName;
std::wstring fsName;
DWORD serial = 0;
DWORD maxComp = 0;
DWORD fsFlags = 0;
};
struct ReparseInfo {
bool has = false;
ULONG tag = 0;
bool isSymlink = false;
bool isMountPoint = false;
ULONG symlinkFlags = 0;
std::wstring substituteName;
std::wstring printName;
std::wstring err;
};
struct HandleInfo {
bool ok = false;
BY_HANDLE_FILE_INFORMATION fi{};
std::wstring finalPath; // optional
std::wstring err;
};
struct QueryData {
std::wstring inputPath;
std::wstring fullPath;
DWORD attr = 0;
std::wstring attrErr;
VolumeInfo linkVol;
VolumeInfo targetVol;
HandleInfo linkHandle; // OPEN_REPARSE_POINT (metadata link object)
ReparseInfo reparse; // FSCTL_GET_REPARSE_POINT parsed
std::wstring immediateTarget;
bool immediateExists = false;
HandleInfo followHandle; // follow (resolved)
std::vector<std::wstring> hardlinkNames;
std::wstring hardlinkErr;
std::wstring extraText; // text for entries that cannot be mapped to dedicated GUI fields
};
static bool FillVolumeInfoFromPathW(const std::wstring& anyPath, VolumeInfo& out, std::wstring& err)
{
out = VolumeInfo();
err.clear();
WCHAR root[MAX_PATH] = {0};
if (!GetVolumePathNameW(anyPath.c_str(), root, MAX_PATH)) {
err = L"GetVolumePathName failed: " + WinErrW(GetLastError());
return false;
}
WCHAR volName[MAX_PATH] = {0};
WCHAR fsName[MAX_PATH] = {0};
DWORD serial = 0, maxComp = 0, fsFlags = 0;
if (!GetVolumeInformationW(root, volName, MAX_PATH, &serial, &maxComp, &fsFlags, fsName, MAX_PATH)) {
err = L"GetVolumeInformation failed: " + WinErrW(GetLastError());
return false;
}
out.ok = true;
out.root = root;
out.volName = volName;
out.fsName = fsName;
out.serial = serial;
out.maxComp = maxComp;
out.fsFlags = fsFlags;
return true;
}
static bool FillHandleInfoW(const std::wstring& path, DWORD createFlags, HandleInfo& out)
{
out = HandleInfo();
HANDLE h = CreateFileW(path.c_str(),
0,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
createFlags,
NULL);
if (h == INVALID_HANDLE_VALUE) {
out.err = WinErrW(GetLastError());
return false;
}
if (!GetFileInformationByHandle(h, &out.fi)) {
out.err = WinErrW(GetLastError());
CloseHandle(h);
return false;
}
DWORD need = GetFinalPathNameByHandleW(h, NULL, 0, 0);
if (need) {
std::wstring buf;
buf.resize(need + 2);
DWORD got = GetFinalPathNameByHandleW(h, &buf[0], (DWORD)buf.size(), 0);
if (got && got < buf.size()) {
buf.resize(got);
out.finalPath = buf;
}
}
out.ok = true;
CloseHandle(h);
return true;
}
static std::wstring ReadNameFromPathBuffer(const BYTE* base, USHORT offBytes, USHORT lenBytes)
{
if (!base || lenBytes == 0) return L"";
const WCHAR* w = (const WCHAR*)(base + offBytes);
int chars = (int)(lenBytes / sizeof(WCHAR));
return std::wstring(w, w + chars);
}
static bool FillReparseInfoW(const std::wstring& path, ReparseInfo& out)
{
out = ReparseInfo();
HANDLE h = CreateFileW(path.c_str(),
0,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (h == INVALID_HANDLE_VALUE) {
out.err = L"Open failed: " + WinErrW(GetLastError());
return false;
}
BYTE buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
DWORD bytes = 0;
BOOL ok = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, buf, (DWORD)sizeof(buf), &bytes, NULL);
CloseHandle(h);
if (!ok) {
out.err = L"FSCTL_GET_REPARSE_POINT failed: " + WinErrW(GetLastError());
return false;
}
PREPARSE_DATA_BUFFER rdb = (PREPARSE_DATA_BUFFER)buf;
out.has = true;
out.tag = rdb->ReparseTag;
if (out.tag == IO_REPARSE_TAG_MOUNT_POINT) {
out.isMountPoint = true;
const auto& mp = rdb->MountPointReparseBuffer;
const BYTE* base = (const BYTE*)mp.PathBuffer;
out.substituteName = ReadNameFromPathBuffer(base, mp.SubstituteNameOffset, mp.SubstituteNameLength);
out.printName = ReadNameFromPathBuffer(base, mp.PrintNameOffset, mp.PrintNameLength);
return true;
}
if (out.tag == IO_REPARSE_TAG_SYMLINK) {
out.isSymlink = true;
const auto& sl = rdb->SymbolicLinkReparseBuffer;
const BYTE* base = (const BYTE*)sl.PathBuffer;
out.symlinkFlags = sl.Flags;
out.substituteName = ReadNameFromPathBuffer(base, sl.SubstituteNameOffset, sl.SubstituteNameLength);
out.printName = ReadNameFromPathBuffer(base, sl.PrintNameOffset, sl.PrintNameLength);
return true;
}
return true;
}
static std::wstring ResolveImmediateTargetHeuristicW(const std::wstring& linkPath, const ReparseInfo& ri)
{
std::wstring cand = !ri.printName.empty() ? ri.printName : ri.substituteName;
if (cand.empty()) return L"";
std::wstring stripped = StripNtPrefixW(cand);
if (stripped.rfind(L"\\\\", 0) == 0) return stripped;
if (stripped.size() >= 3 && stripped[1] == L':' && (stripped[2] == L'\\' || stripped[2] == L'/')) return stripped;
if (ri.isSymlink && (ri.symlinkFlags & SYMLINK_FLAG_RELATIVE)) {
std::wstring baseDir = DirOfPathW(GetFullPathW(linkPath));
if (!baseDir.empty()) return JoinPathW(baseDir, stripped);
}
return stripped;
}
static void EnumHardlinksW(const std::wstring& filePath, std::vector<std::wstring>& out, std::wstring& errOut)
{
out.clear();
errOut.clear();
DWORD attr = GetFileAttributesW(filePath.c_str());
if (attr == INVALID_FILE_ATTRIBUTES) {
errOut = L"GetFileAttributes: " + WinErrW(GetLastError());
return;
}
if (attr & FILE_ATTRIBUTE_DIRECTORY) return;
WCHAR volPath[MAX_PATH] = {0};
if (!GetVolumePathNameW(filePath.c_str(), volPath, MAX_PATH)) {
errOut = L"GetVolumePathName: " + WinErrW(GetLastError());
return;
}
DWORD len = 1024;
std::vector<WCHAR> buf(len);
HANDLE hFind = FindFirstFileNameW(filePath.c_str(), 0, &len, &buf[0]);
if (hFind == INVALID_HANDLE_VALUE) {
DWORD e = GetLastError();
if (e == ERROR_MORE_DATA) {
buf.resize(len + 2);
DWORD len2 = len;
hFind = FindFirstFileNameW(filePath.c_str(), 0, &len2, &buf[0]);
if (hFind == INVALID_HANDLE_VALUE) {
errOut = L"FindFirstFileName: " + WinErrW(GetLastError());
return;
}
len = len2;
} else {
errOut = L"FindFirstFileName: " + WinErrW(e);
return;
}
}
auto addOne = [&](const WCHAR* nameFromRoot) {
std::wstring full = volPath;
if (nameFromRoot && nameFromRoot[0] == L'\\') full += (nameFromRoot + 1);
else if (nameFromRoot) full += nameFromRoot;
out.push_back(full);
};
addOne(&buf[0]);
for (;;) {
DWORD l = (DWORD)buf.size();
BOOL ok = FindNextFileNameW(hFind, &l, &buf[0]);
if (!ok) {
DWORD e = GetLastError();
if (e == ERROR_MORE_DATA) {
buf.resize(l + 2);
DWORD l2 = l;
ok = FindNextFileNameW(hFind, &l2, &buf[0]);
if (!ok) break;
addOne(&buf[0]);
continue;
}
if (e == ERROR_HANDLE_EOF) break;
break;
}
addOne(&buf[0]);
}
FindClose(hFind);
}
static std::wstring FileIdTextW(const BY_HANDLE_FILE_INFORMATION& fi)
{
return Fmt(L"0x%08lX%08lX", fi.nFileIndexHigh, fi.nFileIndexLow);
}
static std::wstring VolumeSerialTextW(const BY_HANDLE_FILE_INFORMATION& fi)
{
return Fmt(L"0x%08lX", fi.dwVolumeSerialNumber);
}
static std::wstring SizeTextW(const BY_HANDLE_FILE_INFORMATION& fi)
{
ULONGLONG size = ((ULONGLONG)fi.nFileSizeHigh << 32) | (ULONGLONG)fi.nFileSizeLow;
return Fmt(L"%llu", (unsigned long long)size);
}
static void CollectQueryData(const std::wstring& inputPath, QueryData& q)
{
q = QueryData();
q.inputPath = inputPath;
q.fullPath = GetFullPathW(inputPath);
DWORD attr = GetFileAttributesW(q.fullPath.c_str());
if (attr == INVALID_FILE_ATTRIBUTES) {
q.attrErr = WinErrW(GetLastError());
return;
}
q.attr = attr;
// volumes
{
std::wstring e;
FillVolumeInfoFromPathW(q.fullPath, q.linkVol, e);
if (!e.empty()) q.extraText += L"Link volume: " + e + L"\r\n";
}
// link handle info (no-follow, OPEN_REPARSE_POINT)
{
DWORD flags = FILE_FLAG_OPEN_REPARSE_POINT;
if (attr & FILE_ATTRIBUTE_DIRECTORY) flags |= FILE_FLAG_BACKUP_SEMANTICS;
if (!FillHandleInfoW(q.fullPath, flags, q.linkHandle)) {
q.extraText += L"Link handle open failed: " + q.linkHandle.err + L"\r\n";
}
}
// reparse info
if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
if (!FillReparseInfoW(q.fullPath, q.reparse)) {
q.extraText += L"Reparse: " + q.reparse.err + L"\r\n";
} else {
if (q.reparse.isSymlink || q.reparse.isMountPoint) {
q.immediateTarget = ResolveImmediateTargetHeuristicW(q.fullPath, q.reparse);
if (!q.immediateTarget.empty()) {
DWORD a2 = GetFileAttributesW(q.immediateTarget.c_str());
q.immediateExists = (a2 != INVALID_FILE_ATTRIBUTES);
}
}
}
}
// follow handle info (resolved)
{
DWORD flags = 0;
if (attr & FILE_ATTRIBUTE_DIRECTORY) flags |= FILE_FLAG_BACKUP_SEMANTICS;
if (!FillHandleInfoW(q.fullPath, flags, q.followHandle)) {
q.extraText += L"Follow open failed: " + q.followHandle.err + L"\r\n";
} else {
std::wstring final = StripNtPrefixW(q.followHandle.finalPath);
if (!final.empty()) {
std::wstring e;
FillVolumeInfoFromPathW(final, q.targetVol, e);
if (!e.empty()) q.extraText += L"Target volume: " + e + L"\r\n";
}
}
}
// hardlinks list for files
if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) {
EnumHardlinksW(q.fullPath, q.hardlinkNames, q.hardlinkErr);
if (!q.hardlinkErr.empty()) q.extraText += L"Hardlink enum: " + q.hardlinkErr + L"\r\n";
}
// extra dumps
if (!q.hardlinkNames.empty()) {
q.extraText += L"\r\nHardlink names (same file ID):\r\n";
for (const auto& s : q.hardlinkNames) q.extraText += L" " + s + L"\r\n";
}
if (q.reparse.has) {
if (!q.reparse.substituteName.empty() || !q.reparse.printName.empty()) {
q.extraText += L"\r\nRaw reparse strings:\r\n";
if (!q.reparse.substituteName.empty()) q.extraText += L" SubstituteName: " + q.reparse.substituteName + L"\r\n";
if (!q.reparse.printName.empty()) q.extraText += L" PrintName: " + q.reparse.printName + L"\r\n";
}
}
}
// ---- Backend create/delete/update ----
static void MainLog(HWND hwnd, const std::wstring& s, bool clear)
{
HWND h = GetDlgItem(hwnd, IDC_MAINLOG);
if (clear) SetTextW(h, L"");
AppendTextW(h, s);
}
static bool SetJunctionW(const std::wstring& linkDir, const std::wstring& targetDir, std::wstring& errOut)
{
errOut.clear();
DWORD attr = GetFileAttributesW(linkDir.c_str());
if (attr == INVALID_FILE_ATTRIBUTES) {
if (!CreateDirectoryW(linkDir.c_str(), NULL)) {
errOut = L"CreateDirectory failed: " + WinErrW(GetLastError()) + L"\r\n";
return false;
}
} else if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) {
errOut = L"Link path exists but is not a directory.\r\n";
return false;
}
std::wstring fullTarget = GetFullPathW(targetDir);
DWORD tAttr = GetFileAttributesW(fullTarget.c_str());
if (tAttr == INVALID_FILE_ATTRIBUTES || !(tAttr & FILE_ATTRIBUTE_DIRECTORY)) {
errOut = L"Target for junction should be an existing directory.\r\n";
return false;
}
std::wstring subst = L"\\??\\";
subst += fullTarget;
std::wstring print = fullTarget;
const USHORT substBytes = (USHORT)(subst.size() * sizeof(WCHAR));
const USHORT printBytes = (USHORT)(print.size() * sizeof(WCHAR));
const USHORT pathBytes = (USHORT)(substBytes + sizeof(WCHAR) + printBytes + sizeof(WCHAR));
const DWORD totalSize =
(DWORD)FIELD_OFFSET(REPARSE_DATA_BUFFER, MountPointReparseBuffer.PathBuffer) + pathBytes;
std::string storage;
storage.resize(totalSize);
PREPARSE_DATA_BUFFER rdb = (PREPARSE_DATA_BUFFER)&storage[0];
ZeroMemory(rdb, totalSize);
rdb->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
rdb->ReparseDataLength = (USHORT)(8 + pathBytes);
rdb->Reserved = 0;
rdb->MountPointReparseBuffer.SubstituteNameOffset = 0;
rdb->MountPointReparseBuffer.SubstituteNameLength = substBytes;
rdb->MountPointReparseBuffer.PrintNameOffset = substBytes + sizeof(WCHAR);
rdb->MountPointReparseBuffer.PrintNameLength = printBytes;
BYTE* pb = (BYTE*)rdb->MountPointReparseBuffer.PathBuffer;
memcpy(pb, subst.c_str(), substBytes);
*(WCHAR*)(pb + substBytes) = L'\0';
memcpy(pb + substBytes + sizeof(WCHAR), print.c_str(), printBytes);
*(WCHAR*)(pb + substBytes + sizeof(WCHAR) + printBytes) = L'\0';
HANDLE h = CreateFileW(linkDir.c_str(),
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (h == INVALID_HANDLE_VALUE) {
errOut = L"Open link dir failed: " + WinErrW(GetLastError()) + L"\r\n";
return false;
}
DWORD bytesReturned = 0;
BOOL ok = DeviceIoControl(
h,
FSCTL_SET_REPARSE_POINT,
rdb,
(DWORD)(rdb->ReparseDataLength + REPARSE_DATA_BUFFER_HEADER_SIZE),
NULL,
0,
&bytesReturned,
NULL
);
CloseHandle(h);
if (!ok) {
errOut = L"FSCTL_SET_REPARSE_POINT failed: " + WinErrW(GetLastError()) + L"\r\n";
return false;
}
return true;
}
static bool DeleteReparsePointIfAnyW(const std::wstring& path, std::wstring& errOut)
{
errOut.clear();
HANDLE h = CreateFileW(path.c_str(),
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (h == INVALID_HANDLE_VALUE) {
errOut = L"Open failed: " + WinErrW(GetLastError()) + L"\r\n";
return false;
}
BYTE buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
DWORD bytes = 0;
if (!DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, buf, (DWORD)sizeof(buf), &bytes, NULL)) {
CloseHandle(h);
return true;
}
PREPARSE_DATA_BUFFER rdb = (PREPARSE_DATA_BUFFER)buf;
struct {
ULONG ReparseTag;
USHORT ReparseDataLength;
USHORT Reserved;
} del = { rdb->ReparseTag, 0, 0 };
DWORD br = 0;
BOOL ok = DeviceIoControl(h, FSCTL_DELETE_REPARSE_POINT, &del, sizeof(del), NULL, 0, &br, NULL);
CloseHandle(h);
if (!ok) {
errOut = L"FSCTL_DELETE_REPARSE_POINT failed: " + WinErrW(GetLastError()) + L"\r\n";
return false;
}
return true;
}
static bool CreateSymlinkW(const std::wstring& link, const std::wstring& target, bool isDir, std::wstring& errOut)
{
errOut.clear();
DWORD flags = isDir ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0;
DWORD flagsTry = flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
if (CreateSymbolicLinkW(link.c_str(), target.c_str(), flagsTry)) return true;
DWORD e = GetLastError();
if (e == ERROR_INVALID_PARAMETER) {
if (CreateSymbolicLinkW(link.c_str(), target.c_str(), flags)) return true;
e = GetLastError();
}
errOut = L"CreateSymbolicLink failed: " + WinErrW(e) + L"\r\n";
errOut += L"Note: symlinks often require Administrator rights or Developer Mode.\r\n";
return false;
}
// ---- Query result window ----
static const wchar_t* kQueryWndClass = L"LinkGui_QueryResult";
struct QueryWndState {
QueryData* q = nullptr;
// groups
HWND gLink=nullptr, gRep=nullptr, gTgt=nullptr, gExtra=nullptr;
// link labels/edits
HWND sPath=nullptr, ePath=nullptr;
HWND sFid=nullptr, eFid=nullptr;
HWND sVser=nullptr, eVser=nullptr;
HWND sSize=nullptr, eSize=nullptr;
HWND sCT=nullptr, eCT=nullptr;
HWND sMT=nullptr, eMT=nullptr;
HWND sAT=nullptr, eAT=nullptr;
HWND cbIsDir=nullptr, cbIsReparse=nullptr;
HWND cbLvRep=nullptr, cbLvHl=nullptr;
// attributes (checkboxes)
HWND cbARo=nullptr, cbAHid=nullptr, cbASys=nullptr, cbAArc=nullptr, cbATmp=nullptr, cbASparse=nullptr, cbAComp=nullptr, cbAEnc=nullptr, cbAOff=nullptr, cbARep=nullptr;
// reparse labels/edits + checkboxes
HWND sRTag=nullptr, eRTag=nullptr;
HWND cbRSym=nullptr, cbRJunc=nullptr, cbROther=nullptr, cbRRel=nullptr;
HWND sSub=nullptr, eSub=nullptr;
HWND sPrn=nullptr, ePrn=nullptr;
HWND sImm=nullptr, eImm=nullptr;
HWND cbImmExists=nullptr;
// target labels/edits + checkboxes
HWND cbFollowOk=nullptr;
HWND sFinal=nullptr, eFinal=nullptr;
HWND sTFid=nullptr, eTFid=nullptr;
HWND sTVser=nullptr, eTVser=nullptr;
HWND sTSize=nullptr, eTSize=nullptr;
HWND sTCT=nullptr, eTCT=nullptr;
HWND sTMT=nullptr, eTMT=nullptr;
HWND sTAT=nullptr, eTAT=nullptr;
HWND cbTvRep=nullptr, cbTvHl=nullptr;
// extra
HWND eExtra=nullptr;
};
static HWND MakeStatic(HWND parent, const wchar_t* text, int id)
{
HWND h = CreateWindowExW(0, L"STATIC", text, WS_CHILD|WS_VISIBLE, 0,0,0,0, parent, (HMENU)(INT_PTR)id, g_hInst, NULL);
SetCtlFont(h);
return h;
}
static HWND MakeGroup(HWND parent, const wchar_t* text, int id)
{
HWND h = CreateWindowExW(0, L"BUTTON", text, WS_CHILD|WS_VISIBLE|BS_GROUPBOX, 0,0,0,0, parent, (HMENU)(INT_PTR)id, g_hInst, NULL);
SetCtlFont(h);
return h;
}
static HWND MakeEditRO(HWND parent, int id, bool multiLine)
{
DWORD style = WS_CHILD|WS_VISIBLE|ES_READONLY|WS_TABSTOP;
if (multiLine) style |= ES_MULTILINE|ES_AUTOVSCROLL|WS_VSCROLL;
else style |= ES_AUTOHSCROLL;
HWND h = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", L"", style, 0,0,0,0, parent, (HMENU)(INT_PTR)id, g_hInst, NULL);
SetCtlFont(h);
return h;
}
static HWND MakeCheck(HWND parent, const wchar_t* text, int id)
{
HWND h = CreateWindowExW(0, L"BUTTON", text, WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX, 0,0,0,0, parent, (HMENU)(INT_PTR)id, g_hInst, NULL);
SetCtlFont(h);
return h;
}
static void DisableOutputCheckbox(HWND h)
{
if (h) EnableWindow(h, FALSE);
}
static void PopulateQueryGui(QueryWndState* st)
{
const QueryData& q = *st->q;
SetTextW(st->ePath, q.fullPath);
bool isDir = (q.attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
bool isRep = (q.attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
SetCheck(st->cbIsDir, isDir);
SetCheck(st->cbIsReparse, isRep);
// attribute subset
SetCheck(st->cbARo, (q.attr & FILE_ATTRIBUTE_READONLY) != 0);
SetCheck(st->cbAHid, (q.attr & FILE_ATTRIBUTE_HIDDEN) != 0);
SetCheck(st->cbASys, (q.attr & FILE_ATTRIBUTE_SYSTEM) != 0);
SetCheck(st->cbAArc, (q.attr & FILE_ATTRIBUTE_ARCHIVE) != 0);
SetCheck(st->cbATmp, (q.attr & FILE_ATTRIBUTE_TEMPORARY) != 0);
SetCheck(st->cbASparse,(q.attr & FILE_ATTRIBUTE_SPARSE_FILE) != 0);
SetCheck(st->cbAComp, (q.attr & FILE_ATTRIBUTE_COMPRESSED) != 0);
SetCheck(st->cbAEnc, (q.attr & FILE_ATTRIBUTE_ENCRYPTED) != 0);
SetCheck(st->cbAOff, (q.attr & FILE_ATTRIBUTE_OFFLINE) != 0);
SetCheck(st->cbARep, (q.attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0);
// link handle
if (q.linkHandle.ok) {
const auto& fi = q.linkHandle.fi;
SetTextW(st->eFid, FileIdTextW(fi));
SetTextW(st->eVser, VolumeSerialTextW(fi));
SetTextW(st->eSize, SizeTextW(fi));
SetTextW(st->eCT, FileTimeToTextLocalW(fi.ftCreationTime));
SetTextW(st->eMT, FileTimeToTextLocalW(fi.ftLastWriteTime));
SetTextW(st->eAT, FileTimeToTextLocalW(fi.ftLastAccessTime));
} else {
SetTextW(st->eFid, L"(n/a)");
SetTextW(st->eVser, L"(n/a)");
SetTextW(st->eSize, L"(n/a)");
SetTextW(st->eCT, L"(n/a)");
SetTextW(st->eMT, L"(n/a)");
SetTextW(st->eAT, L"(n/a)");
}
// volume feature checkboxes
if (q.linkVol.ok) {
SetCheck(st->cbLvRep, (q.linkVol.fsFlags & FILE_SUPPORTS_REPARSE_POINTS) != 0);
SetCheck(st->cbLvHl, (q.linkVol.fsFlags & FILE_SUPPORTS_HARD_LINKS) != 0);
} else {
SetCheck(st->cbLvRep, false);
SetCheck(st->cbLvHl, false);
}
// reparse
if (q.reparse.has) {
SetTextW(st->eRTag, Fmt(L"0x%08lX (%s)", q.reparse.tag, ReparseTagNameW(q.reparse.tag)));
SetCheck(st->cbRSym, q.reparse.isSymlink);
SetCheck(st->cbRJunc, q.reparse.isMountPoint);
SetCheck(st->cbROther, (!q.reparse.isSymlink && !q.reparse.isMountPoint));
SetCheck(st->cbRRel, (q.reparse.isSymlink && ((q.reparse.symlinkFlags & SYMLINK_FLAG_RELATIVE) != 0)));
SetTextW(st->eSub, StripNtPrefixW(q.reparse.substituteName));
SetTextW(st->ePrn, StripNtPrefixW(q.reparse.printName));
SetTextW(st->eImm, q.immediateTarget);
SetCheck(st->cbImmExists, q.immediateExists);
} else {
SetTextW(st->eRTag, L"(none)");
SetCheck(st->cbRSym, false);
SetCheck(st->cbRJunc, false);
SetCheck(st->cbROther, false);
SetCheck(st->cbRRel, false);
SetTextW(st->eSub, L"");
SetTextW(st->ePrn, L"");
SetTextW(st->eImm, L"");
SetCheck(st->cbImmExists, false);
}
// target/follow
SetCheck(st->cbFollowOk, q.followHandle.ok);
if (q.followHandle.ok) {
const auto& fi = q.followHandle.fi;
SetTextW(st->eFinal, StripNtPrefixW(q.followHandle.finalPath));
SetTextW(st->eTFid, FileIdTextW(fi));
SetTextW(st->eTVser, VolumeSerialTextW(fi));
SetTextW(st->eTSize, SizeTextW(fi));
SetTextW(st->eTCT, FileTimeToTextLocalW(fi.ftCreationTime));
SetTextW(st->eTMT, FileTimeToTextLocalW(fi.ftLastWriteTime));
SetTextW(st->eTAT, FileTimeToTextLocalW(fi.ftLastAccessTime));
} else {
SetTextW(st->eFinal, L"(open failed)");
SetTextW(st->eTFid, L"(n/a)");
SetTextW(st->eTVser, L"(n/a)");
SetTextW(st->eTSize, L"(n/a)");
SetTextW(st->eTCT, L"(n/a)");
SetTextW(st->eTMT, L"(n/a)");
SetTextW(st->eTAT, L"(n/a)");
}
if (q.targetVol.ok) {
SetCheck(st->cbTvRep, (q.targetVol.fsFlags & FILE_SUPPORTS_REPARSE_POINTS) != 0);
SetCheck(st->cbTvHl, (q.targetVol.fsFlags & FILE_SUPPORTS_HARD_LINKS) != 0);
} else {