Commit 65477db1660273c453c590b8e3b97a4f7c41df61

Edward Thomson 2014-04-21T23:32:31

Handle win32 reparse points properly

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
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
diff --git a/src/win32/posix.h b/src/win32/posix.h
index e5a32b5..7f9d57c 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -30,7 +30,7 @@ GIT_INLINE(int) p_link(const char *old, const char *new)
 extern int p_mkdir(const char *path, mode_t mode);
 extern int p_unlink(const char *path);
 extern int p_lstat(const char *file_name, struct stat *buf);
-extern int p_readlink(const char *link, char *target, size_t target_len);
+extern int p_readlink(const char *path, char *buf, size_t bufsiz);
 extern int p_symlink(const char *old, const char *new);
 extern char *p_realpath(const char *orig_path, char *buffer);
 extern int p_vsnprintf(char *buffer, size_t count, const char *format, va_list argptr);
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 868be60..bef65a3 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -9,12 +9,13 @@
 #include "path.h"
 #include "utf-conv.h"
 #include "repository.h"
+#include "reparse.h"
 #include <errno.h>
 #include <io.h>
 #include <fcntl.h>
 #include <ws2tcpip.h>
 
-#if defined(__MINGW32__)
+#ifndef FILE_NAME_NORMALIZED
 # define FILE_NAME_NORMALIZED 0
 #endif
 
@@ -100,29 +101,79 @@ GIT_INLINE(time_t) filetime_to_time_t(const FILETIME *ft)
 	return (time_t)winTime;
 }
 
-#define WIN32_IS_WSEP(CH) ((CH) == L'/' || (CH) == L'\\')
-
-static int do_lstat(
-	const char *file_name, struct stat *buf, int posix_enotdir)
+/* On success, returns the length, in characters, of the path stored in dest.
+ * On failure, returns a negative value. */
+static int readlink_w(
+	git_win32_path dest,
+	const git_win32_path path)
 {
-	WIN32_FILE_ATTRIBUTE_DATA fdata;
-	git_win32_path fbuf;
-	wchar_t lastch;
-	int flen;
+	BYTE buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
+	GIT_REPARSE_DATA_BUFFER *reparse_buf = (GIT_REPARSE_DATA_BUFFER *)buf;
+	HANDLE handle = NULL;
+	DWORD ioctl_ret;
+	wchar_t *target;
+	size_t target_len;
+
+	int error = -1;
 
-	if ((flen = utf8_to_16_with_errno(fbuf, file_name)) < 0)
+	handle = CreateFileW(path, GENERIC_READ,
+		FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
+		FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
+
+	if (INVALID_HANDLE_VALUE == handle) {
+		errno = ENOENT;
 		return -1;
+	}
 
-	/* truncate trailing slashes */
-	for (; flen > 0; --flen) {
-		lastch = fbuf[flen - 1];
-		if (WIN32_IS_WSEP(lastch))
-			fbuf[flen - 1] = L'\0';
-		else if (lastch != L'\0')
-			break;
+	if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0,
+		reparse_buf, sizeof(buf), &ioctl_ret, NULL)) {
+		errno = EINVAL;
+		goto on_error;
 	}
 
-	if (GetFileAttributesExW(fbuf, GetFileExInfoStandard, &fdata)) {
+	switch (reparse_buf->ReparseTag) {
+	case IO_REPARSE_TAG_SYMLINK:
+		target = reparse_buf->SymbolicLinkReparseBuffer.PathBuffer +
+			(reparse_buf->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(WCHAR));
+		target_len = reparse_buf->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
+		break;
+	case IO_REPARSE_TAG_MOUNT_POINT:
+		target = reparse_buf->MountPointReparseBuffer.PathBuffer +
+			(reparse_buf->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR));
+		target_len = reparse_buf->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
+		break;
+	default:
+		errno = EINVAL;
+		goto on_error;
+	}
+
+	if (target_len) {
+		/* The path may need to have a prefix removed. */
+		target_len = git_win32__to_dos(target, target_len);
+
+		/* Need one additional character in the target buffer
+		 * for the terminating NULL. */
+		if (GIT_WIN_PATH_UTF16 > target_len) {
+			wcscpy(dest, target);
+			error = (int)target_len;
+		}
+	}
+
+on_error:
+	CloseHandle(handle);
+	return error;
+}
+
+#define WIN32_IS_WSEP(CH) ((CH) == L'/' || (CH) == L'\\')
+
+static int lstat_w(
+	wchar_t *path,
+	struct stat *buf,
+	bool posix_enotdir)
+{
+	WIN32_FILE_ATTRIBUTE_DATA fdata;
+
+	if (GetFileAttributesExW(path, GetFileExInfoStandard, &fdata)) {
 		int fMode = S_IREAD;
 
 		if (!buf)
@@ -136,12 +187,6 @@ static int do_lstat(
 		if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
 			fMode |= S_IWRITE;
 
-		if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
-			fMode |= S_IFLNK;
-
-		if ((fMode & (S_IFDIR | S_IFLNK)) == (S_IFDIR | S_IFLNK)) // junction
-			fMode ^= S_IFLNK;
-
 		buf->st_ino = 0;
 		buf->st_gid = 0;
 		buf->st_uid = 0;
@@ -153,16 +198,17 @@ static int do_lstat(
 		buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
 		buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
 
-		/* Windows symlinks have zero file size, call readlink to determine
-		 * the length of the path pointed to, which we expect everywhere else
-		 */
-		if (S_ISLNK(fMode)) {
-			git_win32_utf8_path target;
+		if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
+			git_win32_path target;
 
-			if (p_readlink(file_name, target, GIT_WIN_PATH_UTF8) == -1)
-				return -1;
+			if (readlink_w(target, path) >= 0) {
+				buf->st_mode = (buf->st_mode & ~S_IFMT) | S_IFLNK;
 
-			buf->st_size = strlen(target);
+				/* st_size gets the UTF-8 length of the target name, in bytes,
+				 * not counting the NULL terminator */
+				if ((buf->st_size = git__utf16_to_8(NULL, 0, target)) < 0)
+					return -1;
+			}
 		}
 
 		return 0;
@@ -174,18 +220,23 @@ static int do_lstat(
 	 * file path is a regular file, otherwise set ENOENT.
 	 */
 	if (posix_enotdir) {
+		size_t path_len = wcslen(path);
+
 		/* scan up path until we find an existing item */
 		while (1) {
+			DWORD attrs;
+
 			/* remove last directory component */
-			for (--flen; flen > 0 && !WIN32_IS_WSEP(fbuf[flen]); --flen);
+			for (path_len--; path_len > 0 && !WIN32_IS_WSEP(path[path_len]); path_len--);
 
-			if (flen <= 0)
+			if (path_len <= 0)
 				break;
 
-			fbuf[flen] = L'\0';
+			path[path_len] = L'\0';
+			attrs = GetFileAttributesW(path);
 
-			if (GetFileAttributesExW(fbuf, GetFileExInfoStandard, &fdata)) {
-				if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
+			if (INVALID_FILE_ATTRIBUTES != attrs) {
+				if (!(attrs & FILE_ATTRIBUTE_DIRECTORY))
 					errno = ENOTDIR;
 				break;
 			}
@@ -195,105 +246,51 @@ static int do_lstat(
 	return -1;
 }
 
-int p_lstat(const char *filename, struct stat *buf)
+static int do_lstat(const char *path, struct stat *buf, bool posixly_correct)
 {
-	return do_lstat(filename, buf, 0);
+	git_win32_path path_w;
+	int len;
+
+	if ((len = utf8_to_16_with_errno(path_w, path)) < 0)
+		return -1;
+
+	git_win32__path_trim_end(path_w, len);
+
+	return lstat_w(path_w, buf, posixly_correct);
 }
 
-int p_lstat_posixly(const char *filename, struct stat *buf)
+int p_lstat(const char *filename, struct stat *buf)
 {
-	return do_lstat(filename, buf, 1);
+	return do_lstat(filename, buf, false);
 }
 
-/*
- * Returns the address of the GetFinalPathNameByHandleW function.
- * This function is available on Windows Vista and higher.
- */
-static PFGetFinalPathNameByHandleW get_fpnbyhandle(void)
+int p_lstat_posixly(const char *filename, struct stat *buf)
 {
-	static PFGetFinalPathNameByHandleW pFunc = NULL;
-	PFGetFinalPathNameByHandleW toReturn = pFunc;
-
-	if (!toReturn) {
-		HMODULE hModule = GetModuleHandleW(L"kernel32");
-
-		if (hModule)
-			toReturn = (PFGetFinalPathNameByHandleW)GetProcAddress(hModule, "GetFinalPathNameByHandleW");
-
-		pFunc = toReturn;
-	}
-
-	assert(toReturn);
-
-	return toReturn;
+	return do_lstat(filename, buf, true);
 }
 
-/*
- * Parts of the The p_readlink function are heavily inspired by the php 
- * readlink function in link_win32.c
- *
- * Copyright (c) 1999 - 2012 The PHP Group. All rights reserved.
- *
- * For details of the PHP license see http://www.php.net/license/3_01.txt
- */
-int p_readlink(const char *link, char *target, size_t target_len)
+int p_readlink(const char *path, char *buf, size_t bufsiz)
 {
-	static const wchar_t prefix[] = L"\\\\?\\";
-	PFGetFinalPathNameByHandleW pgfp = get_fpnbyhandle();
-	HANDLE hFile = NULL;
-	wchar_t *target_w = NULL;
-	bool trim_prefix;
-	git_win32_path link_w;
-	DWORD dwChars, dwLastError;
-	int error = -1;
-
-	/* Check that we found the function, and convert to UTF-16 */
-	if (!pgfp || utf8_to_16_with_errno(link_w, link) < 0)
-		goto on_error;
-
-	/* Use FILE_FLAG_BACKUP_SEMANTICS so we can open a directory. Do not
-	 * specify FILE_FLAG_OPEN_REPARSE_POINT; we want to open a handle to the
-	 * target of the link. */
-	hFile = CreateFileW(link_w, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE,
-		NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
-
-	if (hFile == INVALID_HANDLE_VALUE)
-		goto on_error;
-
-	/* Find out how large the buffer should be to hold the result */
-	if (!(dwChars = pgfp(hFile, NULL, 0, FILE_NAME_NORMALIZED)))
-		goto on_error;
-
-	if (!(target_w = git__malloc(dwChars * sizeof(wchar_t))))
-		goto on_error;
-
-	/* Call a second time */
-	dwChars = pgfp(hFile, target_w, dwChars, FILE_NAME_NORMALIZED);
-
-	if (!dwChars)
-		goto on_error;
-
-	/* Do we need to trim off a \\?\ from the start of the path? */
-	trim_prefix = (dwChars >= ARRAY_SIZE(prefix)) &&
-		!wcsncmp(prefix, target_w, ARRAY_SIZE(prefix));
-
-	/* Convert the result to UTF-8 */
-	if (git__utf16_to_8(target, target_len, trim_prefix ? target_w + 4 : target_w) < 0)
-		goto on_error;
+	git_win32_path path_w, target_w;
+	git_win32_utf8_path target;
+	int len;
 
-	error = 0;
+	/* readlink(2) does not NULL-terminate the string written
+	 * to the target buffer. Furthermore, the target buffer need
+	 * not be large enough to hold the entire result. A truncated
+	 * result should be written in this case. Since this truncation
+	 * could occur in the middle of the encoding of a code point,
+	 * we need to buffer the result on the stack. */
 
-on_error:
-	dwLastError = GetLastError();
-
-	if (hFile && INVALID_HANDLE_VALUE != hFile)
-		CloseHandle(hFile);
+	if (utf8_to_16_with_errno(path_w, path) < 0 ||
+		readlink_w(target_w, path_w) < 0 ||
+		(len = git_win32_path_to_utf8(target, target_w)) < 0)
+		return -1;
 
-	if (target_w)
-		git__free(target_w);
+	bufsiz = min((size_t)len, bufsiz);
+	memcpy(buf, target, bufsiz);
 
-	SetLastError(dwLastError);
-	return error;
+	return (int)bufsiz;
 }
 
 int p_symlink(const char *old, const char *new)
@@ -356,20 +353,94 @@ int p_getcwd(char *buffer_out, size_t size)
 	return 0;
 }
 
+/*
+ * Returns the address of the GetFinalPathNameByHandleW function.
+ * This function is available on Windows Vista and higher.
+ */
+static PFGetFinalPathNameByHandleW get_fpnbyhandle(void)
+{
+	static PFGetFinalPathNameByHandleW pFunc = NULL;
+	PFGetFinalPathNameByHandleW toReturn = pFunc;
+
+	if (!toReturn) {
+		HMODULE hModule = GetModuleHandleW(L"kernel32");
+
+		if (hModule)
+			toReturn = (PFGetFinalPathNameByHandleW)GetProcAddress(hModule, "GetFinalPathNameByHandleW");
+
+		pFunc = toReturn;
+	}
+
+	assert(toReturn);
+
+	return toReturn;
+}
+
+static int getfinalpath_w(
+	git_win32_path dest,
+	const wchar_t *path)
+{
+	PFGetFinalPathNameByHandleW pgfp = get_fpnbyhandle();
+	HANDLE hFile;
+	DWORD dwChars;
+
+	if (!pgfp)
+		return -1;
+
+	/* Use FILE_FLAG_BACKUP_SEMANTICS so we can open a directory. Do not
+	* specify FILE_FLAG_OPEN_REPARSE_POINT; we want to open a handle to the
+	* target of the link. */
+	hFile = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE,
+		NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
+
+	if (hFile == INVALID_HANDLE_VALUE)
+		return -1;
+
+	/* Call GetFinalPathNameByHandle */
+	dwChars = pgfp(hFile, dest, GIT_WIN_PATH_UTF16, FILE_NAME_NORMALIZED);
+
+	if (!dwChars || dwChars >= GIT_WIN_PATH_UTF16) {
+		DWORD error = GetLastError();
+		CloseHandle(hFile);
+		SetLastError(error);
+		return -1;
+	}
+
+	CloseHandle(hFile);
+
+	/* The path may be delivered to us with a prefix; canonicalize */
+	return (int)git_win32__to_dos(dest, dwChars);
+}
+
+static int follow_and_lstat_link(git_win32_path path, struct stat* buf)
+{
+	git_win32_path target_w;
+
+	if (getfinalpath_w(target_w, path) < 0)
+		return -1;
+
+	return lstat_w(target_w, buf, false);
+}
+
 int p_stat(const char* path, struct stat* buf)
 {
-	git_win32_utf8_path target;
-	int error = 0;
+	git_win32_path path_w;
+	int len;
 
-	error = do_lstat(path, buf, 0);
+	if ((len = utf8_to_16_with_errno(path_w, path)) < 0)
+		return -1;
 
-	/* We need not do this in a loop to unwind chains of symlinks since
-	 * p_readlink calls GetFinalPathNameByHandle which does it for us. */
-	if (error >= 0 && S_ISLNK(buf->st_mode) &&
-		(error = p_readlink(path, target, GIT_WIN_PATH_UTF8)) >= 0)
-		error = do_lstat(target, buf, 0);
+	git_win32__path_trim_end(path_w, len);
 
-	return error;
+	if (lstat_w(path_w, buf, false) < 0)
+		return -1;
+
+	/* The item is a symbolic link or mount point. No need to iterate
+	 * to follow multiple links; use GetFinalPathNameFromHandle. */
+	if (S_ISLNK(buf->st_mode))
+		return follow_and_lstat_link(path_w, buf);
+
+	return 0;
 }
 
 int p_chdir(const char* path)
diff --git a/src/win32/reparse.h b/src/win32/reparse.h
new file mode 100644
index 0000000..4f56ed0
--- /dev/null
+++ b/src/win32/reparse.h
@@ -0,0 +1,57 @@
+/*
+* Copyright (C) the libgit2 contributors. All rights reserved.
+*
+* This file is part of libgit2, distributed under the GNU GPL v2 with
+* a Linking Exception. For full terms see the included COPYING file.
+*/
+
+#ifndef INCLUDE_git_win32_reparse_h__
+#define INCLUDE_git_win32_reparse_h__
+
+/* This structure is defined on MSDN at
+* http://msdn.microsoft.com/en-us/library/windows/hardware/ff552012(v=vs.85).aspx
+*
+* It was formerly included in the Windows 2000 SDK and remains defined in
+* MinGW, so we must define it with a silly name to avoid conflicting.
+*/
+typedef struct _GIT_REPARSE_DATA_BUFFER {
+	ULONG  ReparseTag;
+	USHORT ReparseDataLength;
+	USHORT Reserved;
+	union {
+		struct {
+			USHORT SubstituteNameOffset;
+			USHORT SubstituteNameLength;
+			USHORT PrintNameOffset;
+			USHORT PrintNameLength;
+			ULONG  Flags;
+			WCHAR  PathBuffer[1];
+		} SymbolicLinkReparseBuffer;
+		struct {
+			USHORT SubstituteNameOffset;
+			USHORT SubstituteNameLength;
+			USHORT PrintNameOffset;
+			USHORT PrintNameLength;
+			WCHAR  PathBuffer[1];
+		} MountPointReparseBuffer;
+		struct {
+			UCHAR DataBuffer[1];
+		} GenericReparseBuffer;
+	};
+} GIT_REPARSE_DATA_BUFFER;
+
+#define REPARSE_DATA_HEADER_SIZE			8
+#define REPARSE_DATA_MOUNTPOINT_HEADER_SIZE	8
+#define REPARSE_DATA_UNION_SIZE				12
+
+/* Missing in MinGW */
+#ifndef FSCTL_GET_REPARSE_POINT
+# define FSCTL_GET_REPARSE_POINT			0x000900a8
+#endif
+
+/* Missing in MinGW */
+#ifndef FSCTL_SET_REPARSE_POINT
+# define FSCTL_SET_REPARSE_POINT			0x000900a4
+#endif
+
+#endif
\ No newline at end of file
diff --git a/src/win32/w32_util.c b/src/win32/w32_util.c
index 246b30a..50b85a3 100644
--- a/src/win32/w32_util.c
+++ b/src/win32/w32_util.c
@@ -7,6 +7,8 @@
 
 #include "w32_util.h"
 
+#define CONST_STRLEN(x) ((sizeof(x)/sizeof(x[0])) - 1)
+
 /**
  * Creates a FindFirstFile(Ex) filter string from a UTF-8 path.
  * The filter string enumerates all items in the directory.
@@ -67,3 +69,71 @@ int git_win32__sethidden(const char *path)
 
 	return 0;
 }
+
+/**
+ * Removes any trailing backslashes from a path, except in the case of a drive
+ * letter path (C:\, D:\, etc.). This function cannot fail.
+ *
+ * @param path The path which should be trimmed.
+ * @return The length of the modified string (<= the input length)
+ */
+size_t git_win32__path_trim_end(wchar_t *str, size_t len)
+{
+	while (1) {
+		if (!len || str[len - 1] != L'\\')
+			break;
+
+		/* Don't trim backslashes from drive letter paths, which
+		 * are 3 characters long and of the form C:\, D:\, etc. */
+		if (3 == len && git_win32__isalpha(str[0]) && str[1] == ':')
+			break;
+
+		len--;
+	}
+
+	str[len] = L'\0';
+
+	return len;
+}
+
+/**
+ * Removes any of the following namespace prefixes from a path,
+ * if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail.
+ *
+ * @param path The path which should be converted.
+ * @return The length of the modified string (<= the input length)
+ */
+size_t git_win32__to_dos(wchar_t *str, size_t len)
+{
+	static const wchar_t dosdevices_prefix[] = L"\\\?\?\\";
+	static const wchar_t nt_prefix[] = L"\\\\?\\";
+	static const wchar_t unc_prefix[] = L"UNC\\";
+	size_t to_advance = 0;
+
+	/* "\??\" -- DOS Devices prefix */
+	if (len >= CONST_STRLEN(dosdevices_prefix) &&
+		!wcsncmp(str, dosdevices_prefix, CONST_STRLEN(dosdevices_prefix))) {
+		to_advance += CONST_STRLEN(dosdevices_prefix);
+		len -= CONST_STRLEN(dosdevices_prefix);
+	}
+	/* "\\?\" -- NT namespace prefix */
+	else if (len >= CONST_STRLEN(nt_prefix) &&
+		!wcsncmp(str, nt_prefix, CONST_STRLEN(nt_prefix))) {
+		to_advance += CONST_STRLEN(nt_prefix);
+		len -= CONST_STRLEN(nt_prefix);
+	}
+
+	/* "\??\UNC\", "\\?\UNC\" -- UNC prefix */
+	if (to_advance && len >= CONST_STRLEN(unc_prefix) &&
+		!wcsncmp(str + to_advance, unc_prefix, CONST_STRLEN(unc_prefix))) {
+		to_advance += CONST_STRLEN(unc_prefix);
+		len -= CONST_STRLEN(unc_prefix);
+	}
+
+	if (to_advance) {
+		memmove(str, str + to_advance, len * sizeof(wchar_t));
+		str[len] = L'\0';
+	}
+
+	return git_win32__path_trim_end(str, len);
+}
\ No newline at end of file
diff --git a/src/win32/w32_util.h b/src/win32/w32_util.h
index cd02bd7..acdee3d 100644
--- a/src/win32/w32_util.h
+++ b/src/win32/w32_util.h
@@ -10,6 +10,11 @@
 
 #include "utf-conv.h"
 
+GIT_INLINE(bool) git_win32__isalpha(wchar_t c)
+{
+	return ((c >= L'A' && c <= L'Z') || (c >= L'a' && c <= L'z'));
+}
+
 /**
  * Creates a FindFirstFile(Ex) filter string from a UTF-8 path.
  * The filter string enumerates all items in the directory.
@@ -28,4 +33,22 @@ bool git_win32__findfirstfile_filter(git_win32_path dest, const char *src);
  */
 int git_win32__sethidden(const char *path);
 
+/**
+ * Removes any trailing backslashes from a path, except in the case of a drive
+ * letter path (C:\, D:\, etc.). This function cannot fail.
+ *
+ * @param path The path which should be trimmed.
+ * @return The length of the modified string (<= the input length)
+ */
+size_t git_win32__path_trim_end(wchar_t *str, size_t len);
+
+/**
+ * Removes any of the following namespace prefixes from a path,
+ * if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail.
+ *
+ * @param path The path which should be converted.
+ * @return The length of the modified string (<= the input length)
+ */
+size_t git_win32__to_dos(wchar_t *str, size_t len);
+
 #endif
diff --git a/tests/clar_libgit2.h b/tests/clar_libgit2.h
index d395bd6..082fa9f 100644
--- a/tests/clar_libgit2.h
+++ b/tests/clar_libgit2.h
@@ -29,6 +29,17 @@
 
 #define cl_git_fail_with(expr, error) cl_assert_equal_i(error,expr)
 
+/**
+ * Like cl_git_pass, only for Win32 error code conventions
+ */
+#define cl_win32_pass(expr) do { \
+	int _win32_res; \
+	if ((_win32_res = (expr)) == 0) { \
+		giterr_set(GITERR_OS, "Returned: %d, system error code: %d", _win32_res, GetLastError()); \
+		cl_git_report_failure(_win32_res, __FILE__, __LINE__, "System call failed: " #expr); \
+	} \
+	} while(0)
+
 void cl_git_report_failure(int, const char *, int, const char *);
 
 #define cl_assert_at_line(expr,file,line) \
diff --git a/tests/core/link.c b/tests/core/link.c
new file mode 100644
index 0000000..20d2706
--- /dev/null
+++ b/tests/core/link.c
@@ -0,0 +1,602 @@
+#include "clar_libgit2.h"
+#include "posix.h"
+#include "buffer.h"
+#include "path.h"
+
+#ifdef GIT_WIN32
+# include "win32/reparse.h"
+#endif
+
+void test_core_link__cleanup(void)
+{
+#ifdef GIT_WIN32
+	RemoveDirectory("lstat_junction");
+	RemoveDirectory("lstat_dangling");
+	RemoveDirectory("lstat_dangling_dir");
+	RemoveDirectory("lstat_dangling_junction");
+
+	RemoveDirectory("stat_junction");
+	RemoveDirectory("stat_dangling");
+	RemoveDirectory("stat_dangling_dir");
+	RemoveDirectory("stat_dangling_junction");
+#endif
+}
+
+#ifdef GIT_WIN32
+static bool is_administrator(void)
+{
+	static SID_IDENTIFIER_AUTHORITY authority = { SECURITY_NT_AUTHORITY };
+	PSID admin_sid;
+	BOOL is_admin;
+
+	cl_win32_pass(AllocateAndInitializeSid(&authority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &admin_sid));
+	cl_win32_pass(CheckTokenMembership(NULL, admin_sid, &is_admin));
+	FreeSid(admin_sid);
+
+	return is_admin ? true : false;
+}
+#endif
+
+static void do_symlink(const char *old, const char *new, int is_dir)
+{
+#ifndef GIT_WIN32
+	GIT_UNUSED(is_dir);
+
+	cl_must_pass(symlink(old, new));
+#else
+	typedef DWORD (WINAPI *create_symlink_func)(LPCTSTR, LPCTSTR, DWORD);
+	HMODULE module;
+	create_symlink_func pCreateSymbolicLink;
+
+	if (!is_administrator())
+		clar__skip();
+
+	cl_assert(module = GetModuleHandle("kernel32"));
+	cl_assert(pCreateSymbolicLink = (create_symlink_func)GetProcAddress(module, "CreateSymbolicLinkA"));
+
+	cl_win32_pass(pCreateSymbolicLink(new, old, is_dir));
+#endif
+}
+
+static void do_hardlink(const char *old, const char *new)
+{
+#ifndef GIT_WIN32
+	cl_must_pass(link(old, new));
+#else
+	typedef DWORD (WINAPI *create_hardlink_func)(LPCTSTR, LPCTSTR, LPSECURITY_ATTRIBUTES);
+	HMODULE module;
+	create_hardlink_func pCreateHardLink;
+
+	if (!is_administrator())
+		clar__skip();
+
+	cl_assert(module = GetModuleHandle("kernel32"));
+	cl_assert(pCreateHardLink = (create_hardlink_func)GetProcAddress(module, "CreateHardLinkA"));
+
+	cl_win32_pass(pCreateHardLink(new, old, 0));
+#endif
+}
+
+#ifdef GIT_WIN32
+
+static void do_junction(const char *old, const char *new)
+{
+	GIT_REPARSE_DATA_BUFFER *reparse_buf;
+	HANDLE handle;
+	git_buf unparsed_buf = GIT_BUF_INIT;
+	wchar_t *subst_utf16, *print_utf16;
+	DWORD ioctl_ret;
+	int subst_utf16_len, subst_byte_len, print_utf16_len, print_byte_len, ret;
+	USHORT reparse_buflen;
+	size_t i;
+
+	/* Junction targets must be the unparsed name, starting with \??\, using
+	 * backslashes instead of forward, and end in a trailing backslash.
+	 * eg: \??\C:\Foo\
+	 */
+	git_buf_puts(&unparsed_buf, "\\??\\");
+
+	for (i = 0; i < strlen(old); i++)
+		git_buf_putc(&unparsed_buf, old[i] == '/' ? '\\' : old[i]);
+
+	git_buf_putc(&unparsed_buf, '\\');
+
+	subst_utf16_len = git__utf8_to_16(NULL, 0, git_buf_cstr(&unparsed_buf));
+	subst_byte_len = subst_utf16_len * sizeof(WCHAR);
+
+	print_utf16_len = subst_utf16_len - 4;
+	print_byte_len = subst_byte_len - (4 * sizeof(WCHAR));
+
+	/* The junction must be an empty directory before the junction attribute
+	 * can be added.
+	 */
+	cl_win32_pass(CreateDirectoryA(new, NULL));
+
+	handle = CreateFileA(new, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
+		FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
+	cl_win32_pass(handle != INVALID_HANDLE_VALUE);
+
+	reparse_buflen = (USHORT)(REPARSE_DATA_HEADER_SIZE +
+		REPARSE_DATA_MOUNTPOINT_HEADER_SIZE +
+		subst_byte_len + sizeof(WCHAR) +
+		print_byte_len + sizeof(WCHAR));
+
+	reparse_buf = LocalAlloc(LMEM_FIXED|LMEM_ZEROINIT, reparse_buflen);
+	cl_assert(reparse_buf);
+
+	subst_utf16 = reparse_buf->MountPointReparseBuffer.PathBuffer;
+	print_utf16 = subst_utf16 + subst_utf16_len + 1;
+
+	ret = git__utf8_to_16(subst_utf16, subst_utf16_len + 1,
+		git_buf_cstr(&unparsed_buf));
+	cl_assert_equal_i(subst_utf16_len, ret);
+
+	ret = git__utf8_to_16(print_utf16,
+		print_utf16_len + 1, git_buf_cstr(&unparsed_buf) + 4);
+	cl_assert_equal_i(print_utf16_len, ret);
+
+	reparse_buf->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
+	reparse_buf->MountPointReparseBuffer.SubstituteNameOffset = 0;
+	reparse_buf->MountPointReparseBuffer.SubstituteNameLength =	subst_byte_len;
+	reparse_buf->MountPointReparseBuffer.PrintNameOffset = (USHORT)(subst_byte_len + sizeof(WCHAR));
+	reparse_buf->MountPointReparseBuffer.PrintNameLength = print_byte_len;
+	reparse_buf->ReparseDataLength = reparse_buflen - REPARSE_DATA_HEADER_SIZE;
+
+	cl_win32_pass(DeviceIoControl(handle, FSCTL_SET_REPARSE_POINT,
+		reparse_buf, reparse_buflen, NULL, 0, &ioctl_ret, NULL));
+
+	CloseHandle(handle);
+	LocalFree(reparse_buf);
+}
+
+static void do_custom_reparse(const char *path)
+{
+	REPARSE_GUID_DATA_BUFFER *reparse_buf;
+	HANDLE handle;
+	DWORD ioctl_ret;
+
+	const char *reparse_data = "Reparse points are silly.";
+	size_t reparse_buflen = REPARSE_GUID_DATA_BUFFER_HEADER_SIZE +
+		strlen(reparse_data) + 1;
+
+	reparse_buf = LocalAlloc(LMEM_FIXED|LMEM_ZEROINIT, reparse_buflen);
+	cl_assert(reparse_buf);
+
+	reparse_buf->ReparseTag = 42;
+	reparse_buf->ReparseDataLength = (WORD)(strlen(reparse_data) + 1);
+
+	reparse_buf->ReparseGuid.Data1 = 0xdeadbeef;
+	reparse_buf->ReparseGuid.Data2 = 0xdead;
+	reparse_buf->ReparseGuid.Data3 = 0xbeef;
+	reparse_buf->ReparseGuid.Data4[0] = 42;
+	reparse_buf->ReparseGuid.Data4[1] = 42;
+	reparse_buf->ReparseGuid.Data4[2] = 42;
+	reparse_buf->ReparseGuid.Data4[3] = 42;
+	reparse_buf->ReparseGuid.Data4[4] = 42;
+	reparse_buf->ReparseGuid.Data4[5] = 42;
+	reparse_buf->ReparseGuid.Data4[6] = 42;
+	reparse_buf->ReparseGuid.Data4[7] = 42;
+	reparse_buf->ReparseGuid.Data4[8] = 42;
+
+	memcpy(reparse_buf->GenericReparseBuffer.DataBuffer,
+		reparse_data, strlen(reparse_data) + 1);
+
+	handle = CreateFileA(path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
+		FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
+	cl_win32_pass(handle != INVALID_HANDLE_VALUE);
+
+	cl_win32_pass(DeviceIoControl(handle, FSCTL_SET_REPARSE_POINT,
+		reparse_buf,
+		reparse_buf->ReparseDataLength + REPARSE_GUID_DATA_BUFFER_HEADER_SIZE,
+		NULL, 0, &ioctl_ret, NULL));
+
+	CloseHandle(handle);
+	LocalFree(reparse_buf);
+}
+
+#endif
+
+git_buf *unslashify(git_buf *buf)
+{
+#ifdef GIT_WIN32
+	size_t i;
+
+	for (i = 0; i < buf->size; i++)
+		if (buf->ptr[i] == '/')
+			buf->ptr[i] = '\\';
+#endif
+
+	return buf;
+}
+
+void test_core_link__stat_regular_file(void)
+{
+	struct stat st;
+
+	cl_git_rewritefile("stat_regfile", "This is a regular file!\n");
+
+	cl_must_pass(p_stat("stat_regfile", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(24, st.st_size);
+}
+
+void test_core_link__lstat_regular_file(void)
+{
+	struct stat st;
+
+	cl_git_rewritefile("lstat_regfile", "This is a regular file!\n");
+
+	cl_must_pass(p_stat("lstat_regfile", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(24, st.st_size);
+}
+
+void test_core_link__stat_symlink(void)
+{
+	struct stat st;
+
+	cl_git_rewritefile("stat_target", "This is the target of a symbolic link.\n");
+	do_symlink("stat_target", "stat_symlink", 0);
+
+	cl_must_pass(p_stat("stat_target", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(39, st.st_size);
+
+	cl_must_pass(p_stat("stat_symlink", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(39, st.st_size);
+}
+
+void test_core_link__stat_symlink_directory(void)
+{
+	struct stat st;
+
+	p_mkdir("stat_dirtarget", 0777);
+	do_symlink("stat_dirtarget", "stat_dirlink", 1);
+
+	cl_must_pass(p_stat("stat_dirtarget", &st));
+	cl_assert(S_ISDIR(st.st_mode));
+
+	cl_must_pass(p_stat("stat_dirlink", &st));
+	cl_assert(S_ISDIR(st.st_mode));
+}
+
+void test_core_link__stat_symlink_chain(void)
+{
+	struct stat st;
+
+	cl_git_rewritefile("stat_final_target", "Final target of some symbolic links...\n");
+	do_symlink("stat_final_target", "stat_chain_3", 0);
+	do_symlink("stat_chain_3", "stat_chain_2", 0);
+	do_symlink("stat_chain_2", "stat_chain_1", 0);
+
+	cl_must_pass(p_stat("stat_chain_1", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(39, st.st_size);
+}
+
+void test_core_link__stat_dangling_symlink(void)
+{
+	struct stat st;
+
+	do_symlink("stat_nonexistent", "stat_dangling", 0);
+
+	cl_must_fail(p_stat("stat_nonexistent", &st));
+	cl_must_fail(p_stat("stat_dangling", &st));
+}
+
+void test_core_link__stat_dangling_symlink_directory(void)
+{
+	struct stat st;
+
+	do_symlink("stat_nonexistent", "stat_dangling_dir", 1);
+
+	cl_must_fail(p_stat("stat_nonexistent_dir", &st));
+	cl_must_fail(p_stat("stat_dangling", &st));
+}
+
+void test_core_link__lstat_symlink(void)
+{
+	git_buf target_path = GIT_BUF_INIT;
+	struct stat st;
+
+	/* Windows always writes the canonical path as the link target, so
+	 * write the full path on all platforms.
+	 */
+	git_buf_join(&target_path, '/', clar_sandbox_path(), "lstat_target");
+
+	cl_git_rewritefile("lstat_target", "This is the target of a symbolic link.\n");
+	do_symlink(git_buf_cstr(&target_path), "lstat_symlink", 0);
+
+	cl_must_pass(p_lstat("lstat_target", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(39, st.st_size);
+
+	cl_must_pass(p_lstat("lstat_symlink", &st));
+	cl_assert(S_ISLNK(st.st_mode));
+	cl_assert_equal_i(git_buf_len(&target_path), st.st_size);
+
+	git_buf_free(&target_path);
+}
+
+void test_core_link__lstat_symlink_directory(void)
+{
+	git_buf target_path = GIT_BUF_INIT;
+	struct stat st;
+
+	git_buf_join(&target_path, '/', clar_sandbox_path(), "lstat_dirtarget");
+
+	p_mkdir("lstat_dirtarget", 0777);
+	do_symlink(git_buf_cstr(&target_path), "lstat_dirlink", 1);
+
+	cl_must_pass(p_lstat("lstat_dirtarget", &st));
+	cl_assert(S_ISDIR(st.st_mode));
+
+	cl_must_pass(p_lstat("lstat_dirlink", &st));
+	cl_assert(S_ISLNK(st.st_mode));
+	cl_assert_equal_i(git_buf_len(&target_path), st.st_size);
+
+	git_buf_free(&target_path);
+}
+
+void test_core_link__lstat_dangling_symlink(void)
+{
+	struct stat st;
+
+	do_symlink("lstat_nonexistent", "lstat_dangling", 0);
+
+	cl_must_fail(p_lstat("lstat_nonexistent", &st));
+
+	cl_must_pass(p_lstat("lstat_dangling", &st));
+	cl_assert(S_ISLNK(st.st_mode));
+	cl_assert_equal_i(strlen("lstat_nonexistent"), st.st_size);
+}
+
+void test_core_link__lstat_dangling_symlink_directory(void)
+{
+	struct stat st;
+
+	do_symlink("lstat_nonexistent", "lstat_dangling_dir", 1);
+
+	cl_must_fail(p_lstat("lstat_nonexistent", &st));
+
+	cl_must_pass(p_lstat("lstat_dangling_dir", &st));
+	cl_assert(S_ISLNK(st.st_mode));
+	cl_assert_equal_i(strlen("lstat_nonexistent"), st.st_size);
+}
+
+void test_core_link__stat_junction(void)
+{
+#ifdef GIT_WIN32
+	git_buf target_path = GIT_BUF_INIT;
+	struct stat st;
+
+	git_buf_join(&target_path, '/', clar_sandbox_path(), "stat_junctarget");
+
+	p_mkdir("stat_junctarget", 0777);
+	do_junction(git_buf_cstr(&target_path), "stat_junction");
+
+	cl_must_pass(p_stat("stat_junctarget", &st));
+	cl_assert(S_ISDIR(st.st_mode));
+
+	cl_must_pass(p_stat("stat_junction", &st));
+	cl_assert(S_ISDIR(st.st_mode));
+
+	git_buf_free(&target_path);
+#endif
+}
+
+void test_core_link__stat_dangling_junction(void)
+{
+#ifdef GIT_WIN32
+	git_buf target_path = GIT_BUF_INIT;
+	struct stat st;
+
+	git_buf_join(&target_path, '/', clar_sandbox_path(), "stat_nonexistent_junctarget");
+
+	p_mkdir("stat_nonexistent_junctarget", 0777);
+	do_junction(git_buf_cstr(&target_path), "stat_dangling_junction");
+
+	RemoveDirectory("stat_nonexistent_junctarget");
+
+	cl_must_fail(p_stat("stat_nonexistent_junctarget", &st));
+	cl_must_fail(p_stat("stat_dangling_junction", &st));
+
+	git_buf_free(&target_path);
+#endif
+}
+
+void test_core_link__lstat_junction(void)
+{
+#ifdef GIT_WIN32
+	git_buf target_path = GIT_BUF_INIT;
+	struct stat st;
+
+	git_buf_join(&target_path, '/', clar_sandbox_path(), "lstat_junctarget");
+
+	p_mkdir("lstat_junctarget", 0777);
+	do_junction(git_buf_cstr(&target_path), "lstat_junction");
+
+	cl_must_pass(p_lstat("lstat_junctarget", &st));
+	cl_assert(S_ISDIR(st.st_mode));
+
+	cl_must_pass(p_lstat("lstat_junction", &st));
+	cl_assert(S_ISLNK(st.st_mode));
+
+	git_buf_free(&target_path);
+#endif
+}
+
+void test_core_link__lstat_dangling_junction(void)
+{
+#ifdef GIT_WIN32
+	git_buf target_path = GIT_BUF_INIT;
+	struct stat st;
+
+	git_buf_join(&target_path, '/', clar_sandbox_path(), "lstat_nonexistent_junctarget");
+
+	p_mkdir("lstat_nonexistent_junctarget", 0777);
+	do_junction(git_buf_cstr(&target_path), "lstat_dangling_junction");
+
+	RemoveDirectory("lstat_nonexistent_junctarget");
+
+	cl_must_fail(p_lstat("lstat_nonexistent_junctarget", &st));
+
+	cl_must_pass(p_lstat("lstat_dangling_junction", &st));
+	cl_assert(S_ISLNK(st.st_mode));
+	cl_assert_equal_i(git_buf_len(&target_path), st.st_size);
+
+	git_buf_free(&target_path);
+#endif
+}
+
+void test_core_link__stat_hardlink(void)
+{
+	struct stat st;
+
+	cl_git_rewritefile("stat_hardlink1", "This file has many names!\n");
+	do_hardlink("stat_hardlink1", "stat_hardlink2");
+
+	cl_must_pass(p_stat("stat_hardlink1", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(26, st.st_size);
+
+	cl_must_pass(p_stat("stat_hardlink2", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(26, st.st_size);
+}
+
+void test_core_link__lstat_hardlink(void)
+{
+	struct stat st;
+
+	cl_git_rewritefile("lstat_hardlink1", "This file has many names!\n");
+	do_hardlink("lstat_hardlink1", "lstat_hardlink2");
+
+	cl_must_pass(p_lstat("lstat_hardlink1", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(26, st.st_size);
+
+	cl_must_pass(p_lstat("lstat_hardlink2", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(26, st.st_size);
+}
+
+void test_core_link__stat_reparse_point(void)
+{
+#ifdef GIT_WIN32
+	struct stat st;
+
+	/* Generic reparse points should be treated as regular files, only
+	 * symlinks and junctions should be treated as links.
+	 */
+
+	cl_git_rewritefile("stat_reparse", "This is a reparse point!\n");
+	do_custom_reparse("stat_reparse");
+
+	cl_must_pass(p_lstat("stat_reparse", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(25, st.st_size);
+#endif
+}
+
+void test_core_link__lstat_reparse_point(void)
+{
+#ifdef GIT_WIN32
+	struct stat st;
+
+	cl_git_rewritefile("lstat_reparse", "This is a reparse point!\n");
+	do_custom_reparse("lstat_reparse");
+
+	cl_must_pass(p_lstat("lstat_reparse", &st));
+	cl_assert(S_ISREG(st.st_mode));
+	cl_assert_equal_i(25, st.st_size);
+#endif
+}
+
+void test_core_link__readlink_nonexistent_file(void)
+{
+	char buf[2048];
+
+	cl_must_fail(p_readlink("readlink_nonexistent", buf, 2048));
+	cl_assert_equal_i(ENOENT, errno);
+}
+
+void test_core_link__readlink_normal_file(void)
+{
+	char buf[2048];
+
+	cl_git_rewritefile("readlink_regfile", "This is a regular file!\n");
+	cl_must_fail(p_readlink("readlink_regfile", buf, 2048));
+	cl_assert_equal_i(EINVAL, errno);
+}
+
+void test_core_link__readlink_symlink(void)
+{
+	git_buf target_path = GIT_BUF_INIT;
+	int len;
+	char buf[2048];
+
+	git_buf_join(&target_path, '/', clar_sandbox_path(), "readlink_target");
+
+	cl_git_rewritefile("readlink_target", "This is the target of a symlink\n");
+	do_symlink(git_buf_cstr(&target_path), "readlink_link", 0);
+
+	len = p_readlink("readlink_link", buf, 2048);
+	cl_must_pass(len);
+
+	buf[len] = 0;
+
+	cl_assert_equal_s(git_buf_cstr(unslashify(&target_path)), buf);
+
+	git_buf_free(&target_path);
+}
+
+void test_core_link__readlink_dangling(void)
+{
+	git_buf target_path = GIT_BUF_INIT;
+	int len;
+	char buf[2048];
+
+	git_buf_join(&target_path, '/', clar_sandbox_path(), "readlink_nonexistent");
+
+	do_symlink(git_buf_cstr(&target_path), "readlink_dangling", 0);
+
+	len = p_readlink("readlink_dangling", buf, 2048);
+	cl_must_pass(len);
+
+	buf[len] = 0;
+
+	cl_assert_equal_s(git_buf_cstr(unslashify(&target_path)), buf);
+
+	git_buf_free(&target_path);
+}
+
+void test_core_link__readlink_multiple(void)
+{
+	git_buf target_path = GIT_BUF_INIT,
+		path3 = GIT_BUF_INIT, path2 = GIT_BUF_INIT, path1 = GIT_BUF_INIT;
+	int len;
+	char buf[2048];
+
+	git_buf_join(&target_path, '/', clar_sandbox_path(), "readlink_final");
+	git_buf_join(&path3, '/', clar_sandbox_path(), "readlink_3");
+	git_buf_join(&path2, '/', clar_sandbox_path(), "readlink_2");
+	git_buf_join(&path1, '/', clar_sandbox_path(), "readlink_1");
+
+	do_symlink(git_buf_cstr(&target_path), git_buf_cstr(&path3), 0);
+	do_symlink(git_buf_cstr(&path3), git_buf_cstr(&path2), 0);
+	do_symlink(git_buf_cstr(&path2), git_buf_cstr(&path1), 0);
+
+	len = p_readlink("readlink_1", buf, 2048);
+	cl_must_pass(len);
+
+	buf[len] = 0;
+
+	cl_assert_equal_s(git_buf_cstr(unslashify(&path2)), buf);
+
+	git_buf_free(&path1);
+	git_buf_free(&path2);
+	git_buf_free(&path3);
+	git_buf_free(&target_path);
+}