Commit 8d6052df46fcb653f37349967d43bca0ce3e7cdf

Carlos Martín Nieto 2015-09-17T22:03:26

Merge pull request #3432 from ethomson/mkdir `mkdir`: split into `mkdir` and `mkdir_relative`

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
diff --git a/src/checkout.c b/src/checkout.c
index 8c06b33..2a8bfd5 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -1366,7 +1366,7 @@ static int checkout_mkdir(
 	mkdir_opts.dir_map = data->mkdir_map;
 	mkdir_opts.pool = &data->pool;
 
-	error = git_futils_mkdir_ext(
+	error = git_futils_mkdir_relative(
 		path, base, mode, flags, &mkdir_opts);
 
 	data->perfdata.mkdir_calls += mkdir_opts.perfdata.mkdir_calls;
diff --git a/src/fileops.c b/src/fileops.c
index b7b5515..57d2ce9 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -18,7 +18,7 @@ GIT__USE_STRMAP
 int git_futils_mkpath2file(const char *file_path, const mode_t mode)
 {
 	return git_futils_mkdir(
-		file_path, NULL, mode,
+		file_path, mode,
 		GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
 }
 
@@ -289,97 +289,230 @@ void git_futils_mmap_free(git_map *out)
 	p_munmap(out);
 }
 
-GIT_INLINE(int) validate_existing(
-	const char *make_path,
+GIT_INLINE(int) mkdir_validate_dir(
+	const char *path,
 	struct stat *st,
 	mode_t mode,
 	uint32_t flags,
-	struct git_futils_mkdir_perfdata *perfdata)
+	struct git_futils_mkdir_options *opts)
 {
+	/* with exclusive create, existing dir is an error */
+	if ((flags & GIT_MKDIR_EXCL) != 0) {
+		giterr_set(GITERR_FILESYSTEM,
+			"Failed to make directory '%s': directory exists", path);
+		return GIT_EEXISTS;
+	}
+
 	if ((S_ISREG(st->st_mode) && (flags & GIT_MKDIR_REMOVE_FILES)) ||
 		(S_ISLNK(st->st_mode) && (flags & GIT_MKDIR_REMOVE_SYMLINKS))) {
-		if (p_unlink(make_path) < 0) {
+		if (p_unlink(path) < 0) {
 			giterr_set(GITERR_OS, "Failed to remove %s '%s'",
-				S_ISLNK(st->st_mode) ? "symlink" : "file", make_path);
+				S_ISLNK(st->st_mode) ? "symlink" : "file", path);
 			return GIT_EEXISTS;
 		}
 
-		perfdata->mkdir_calls++;
+		opts->perfdata.mkdir_calls++;
 
-		if (p_mkdir(make_path, mode) < 0) {
-			giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path);
+		if (p_mkdir(path, mode) < 0) {
+			giterr_set(GITERR_OS, "Failed to make directory '%s'", path);
 			return GIT_EEXISTS;
 		}
 	}
 
 	else if (S_ISLNK(st->st_mode)) {
 		/* Re-stat the target, make sure it's a directory */
-		perfdata->stat_calls++;
+		opts->perfdata.stat_calls++;
 
-		if (p_stat(make_path, st) < 0) {
-			giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path);
+		if (p_stat(path, st) < 0) {
+			giterr_set(GITERR_OS, "Failed to make directory '%s'", path);
 			return GIT_EEXISTS;
 		}
 	}
 
 	else if (!S_ISDIR(st->st_mode)) {
 		giterr_set(GITERR_FILESYSTEM,
-			"Failed to make directory '%s': directory exists", make_path);
+			"Failed to make directory '%s': directory exists", path);
 		return GIT_EEXISTS;
 	}
 
 	return 0;
 }
 
-int git_futils_mkdir_ext(
+GIT_INLINE(int) mkdir_validate_mode(
 	const char *path,
-	const char *base,
+	struct stat *st,
+	bool terminal_path,
 	mode_t mode,
 	uint32_t flags,
 	struct git_futils_mkdir_options *opts)
 {
-	int error = -1;
-	git_buf make_path = GIT_BUF_INIT;
-	ssize_t root = 0, min_root_len, root_len;
-	char lastch = '/', *tail;
-	struct stat st;
+	if (((terminal_path && (flags & GIT_MKDIR_CHMOD) != 0) ||
+		(flags & GIT_MKDIR_CHMOD_PATH) != 0) && st->st_mode != mode) {
 
-	/* build path and find "root" where we should start calling mkdir */
-	if (git_path_join_unrooted(&make_path, path, base, &root) < 0)
-		return -1;
+		opts->perfdata.chmod_calls++;
 
-	if (make_path.size == 0) {
-		giterr_set(GITERR_OS, "Attempt to create empty path");
-		goto done;
+		if (p_chmod(path, mode) < 0) {
+			giterr_set(GITERR_OS, "failed to set permissions on '%s'", path);
+			return -1;
+		}
+	}
+
+	return 0;
+}
+	
+GIT_INLINE(int) mkdir_canonicalize(
+	git_buf *path,
+	uint32_t flags)
+{
+	ssize_t root_len;
+
+	if (path->size == 0) {
+		giterr_set(GITERR_OS, "attempt to create empty path");
+		return -1;
 	}
 
 	/* Trim trailing slashes (except the root) */
-	if ((root_len = git_path_root(make_path.ptr)) < 0)
+	if ((root_len = git_path_root(path->ptr)) < 0)
 		root_len = 0;
 	else
 		root_len++;
 
-	while (make_path.size > (size_t)root_len &&
-		make_path.ptr[make_path.size - 1] == '/')
-		make_path.ptr[--make_path.size] = '\0';
+	while (path->size > (size_t)root_len && path->ptr[path->size - 1] == '/')
+		path->ptr[--path->size] = '\0';
 
 	/* if we are not supposed to made the last element, truncate it */
 	if ((flags & GIT_MKDIR_SKIP_LAST2) != 0) {
-		git_path_dirname_r(&make_path, make_path.ptr);
+		git_path_dirname_r(path, path->ptr);
 		flags |= GIT_MKDIR_SKIP_LAST;
 	}
 	if ((flags & GIT_MKDIR_SKIP_LAST) != 0) {
-		git_path_dirname_r(&make_path, make_path.ptr);
+		git_path_dirname_r(path, path->ptr);
 	}
 
 	/* We were either given the root path (or trimmed it to
-	 * the root), we don't have anything to do.
+	* the root), we don't have anything to do.
+	*/
+	if (path->size <= (size_t)root_len)
+		git_buf_clear(path);
+
+	return 0;
+}
+
+int git_futils_mkdir(
+	const char *path,
+	mode_t mode,
+	uint32_t flags)
+{
+	git_buf make_path = GIT_BUF_INIT, parent_path = GIT_BUF_INIT;
+	const char *relative;
+	struct git_futils_mkdir_options opts = { 0 };
+	struct stat st;
+	size_t depth = 0;
+	int len = 0, root_len, error;
+
+	if ((error = git_buf_puts(&make_path, path)) < 0 ||
+		(error = mkdir_canonicalize(&make_path, flags)) < 0 ||
+		(error = git_buf_puts(&parent_path, make_path.ptr)) < 0 ||
+		make_path.size == 0)
+		goto done;
+
+	root_len = git_path_root(make_path.ptr);
+
+	/* find the first parent directory that exists.  this will be used
+	 * as the base to dirname_relative.
 	 */
-	if (make_path.size <= (size_t)root_len) {
-		error = 0;
+	for (relative = make_path.ptr; parent_path.size; ) {
+		error = p_lstat(parent_path.ptr, &st);
+
+		if (error == 0) {
+			break;
+		} else if (errno != ENOENT) {
+			giterr_set(GITERR_OS, "failed to stat '%s'", parent_path.ptr);
+			goto done;
+		}
+
+		depth++;
+
+		/* examine the parent of the current path */
+		if ((len = git_path_dirname_r(&parent_path, parent_path.ptr)) < 0) {
+			error = len;
+			goto done;
+		}
+
+		assert(len);
+
+		/* we've walked all the given path's parents and it's either relative
+		 * or rooted.  either way, give up and make the entire path.
+		 */
+		if ((len == 1 && parent_path.ptr[0] == '.') || len == root_len+1) {
+			relative = make_path.ptr;
+			break;
+		}
+
+		relative = make_path.ptr + len + 1;
+
+		/* not recursive? just make this directory relative to its parent. */
+		if ((flags & GIT_MKDIR_PATH) == 0)
+			break;
+	}
+
+	/* we found an item at the location we're trying to create,
+	 * validate it.
+	 */
+	if (depth == 0) {
+		error = mkdir_validate_dir(make_path.ptr, &st, mode, flags, &opts);
+
+		if (!error)
+			error = mkdir_validate_mode(
+				make_path.ptr, &st, true, mode, flags, &opts);
+
 		goto done;
 	}
 
+	/* we already took `SKIP_LAST` and `SKIP_LAST2` into account when
+	 * canonicalizing `make_path`.
+	 */
+	flags &= ~(GIT_MKDIR_SKIP_LAST2 | GIT_MKDIR_SKIP_LAST);
+
+	error = git_futils_mkdir_relative(relative,
+		parent_path.size ? parent_path.ptr : NULL, mode, flags, &opts);
+
+done:
+	git_buf_free(&make_path);
+	git_buf_free(&parent_path);
+	return error;
+}
+
+int git_futils_mkdir_r(const char *path, const mode_t mode)
+{
+	return git_futils_mkdir(path, mode, GIT_MKDIR_PATH);
+}
+
+int git_futils_mkdir_relative(
+	const char *relative_path,
+	const char *base,
+	mode_t mode,
+	uint32_t flags,
+	struct git_futils_mkdir_options *opts)
+{
+	git_buf make_path = GIT_BUF_INIT;
+	ssize_t root = 0, min_root_len;
+	char lastch = '/', *tail;
+	struct stat st;
+	struct git_futils_mkdir_options empty_opts = {0};
+	int error;
+
+	if (!opts)
+		opts = &empty_opts;
+
+	/* build path and find "root" where we should start calling mkdir */
+	if (git_path_join_unrooted(&make_path, relative_path, base, &root) < 0)
+		return -1;
+
+	if ((error = mkdir_canonicalize(&make_path, flags)) < 0 ||
+		make_path.size == 0)
+		goto done;
+
 	/* if we are not supposed to make the whole path, reset root */
 	if ((flags & GIT_MKDIR_PATH) == 0)
 		root = git_buf_rfind(&make_path, '/');
@@ -437,32 +570,15 @@ retry_lstat:
 				goto done;
 			}
 		} else {
-			/* with exclusive create, existing dir is an error */
-			if ((flags & GIT_MKDIR_EXCL) != 0) {
-				giterr_set(GITERR_FILESYSTEM, "Failed to make directory '%s': directory exists", make_path.ptr);
-				error = GIT_EEXISTS;
+			if ((error = mkdir_validate_dir(
+				make_path.ptr, &st, mode, flags, opts)) < 0)
 				goto done;
-			}
-
-			if ((error = validate_existing(
-				make_path.ptr, &st, mode, flags, &opts->perfdata)) < 0)
-					goto done;
 		}
 
 		/* chmod if requested and necessary */
-		if (((flags & GIT_MKDIR_CHMOD_PATH) != 0 ||
-			 (lastch == '\0' && (flags & GIT_MKDIR_CHMOD) != 0)) &&
-			st.st_mode != mode) {
-
-			opts->perfdata.chmod_calls++;
-
-			if ((error = p_chmod(make_path.ptr, mode)) < 0 &&
-				lastch == '\0') {
-				giterr_set(GITERR_OS, "Failed to set permissions on '%s'",
-					make_path.ptr);
-				goto done;
-			}
-		}
+		if ((error = mkdir_validate_mode(
+			make_path.ptr, &st, (lastch == '\0'), mode, flags, opts)) < 0)
+			goto done;
 
 		if (opts->dir_map && opts->pool) {
 			char *cache_path;
@@ -501,21 +617,6 @@ done:
 	return error;
 }
 
-int git_futils_mkdir(
-	const char *path,
-	const char *base,
-	mode_t mode,
-	uint32_t flags)
-{
-	struct git_futils_mkdir_options options = {0};
-	return git_futils_mkdir_ext(path, base, mode, flags, &options);
-}
-
-int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
-{
-	return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH);
-}
-
 typedef struct {
 	const char *base;
 	size_t baselen;
@@ -777,7 +878,7 @@ static int _cp_r_mkdir(cp_r_info *info, git_buf *from)
 	/* create root directory the first time we need to create a directory */
 	if ((info->flags & GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT) == 0) {
 		error = git_futils_mkdir(
-			info->to_root, NULL, info->dirmode,
+			info->to_root, info->dirmode,
 			(info->flags & GIT_CPDIR_CHMOD_DIRS) ? GIT_MKDIR_CHMOD : 0);
 
 		info->flags |= GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT;
@@ -785,9 +886,9 @@ static int _cp_r_mkdir(cp_r_info *info, git_buf *from)
 
 	/* create directory with root as base to prevent excess chmods */
 	if (!error)
-		error = git_futils_mkdir(
+		error = git_futils_mkdir_relative(
 			from->ptr + info->from_prefix, info->to_root,
-			info->dirmode, info->mkdir_flags);
+			info->dirmode, info->mkdir_flags, NULL);
 
 	return error;
 }
diff --git a/src/fileops.h b/src/fileops.h
index 0f6466c..572ff01 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -55,12 +55,9 @@ extern int git_futils_creat_locked(const char *path, const mode_t mode);
 extern int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode);
 
 /**
- * Create a path recursively
- *
- * If a base parameter is being passed, it's expected to be valued with a
- * path pointing to an already existing directory.
+ * Create a path recursively.
  */
-extern int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode);
+extern int git_futils_mkdir_r(const char *path, const mode_t mode);
 
 /**
  * Flags to pass to `git_futils_mkdir`.
@@ -111,20 +108,20 @@ struct git_futils_mkdir_options
  * and optionally chmods the directory immediately after (or each part of the
  * path if requested).
  *
- * @param path The path to create.
+ * @param path The path to create, relative to base.
  * @param base Root for relative path.  These directories will never be made.
  * @param mode The mode to use for created directories.
  * @param flags Combination of the mkdir flags above.
- * @param opts Extended options, use `git_futils_mkdir` if you are not interested.
+ * @param opts Extended options, or null.
  * @return 0 on success, else error code
  */
-extern int git_futils_mkdir_ext(const char *path, const char *base, mode_t mode, uint32_t flags, struct git_futils_mkdir_options *opts);
+extern int git_futils_mkdir_relative(const char *path, const char *base, mode_t mode, uint32_t flags, struct git_futils_mkdir_options *opts);
 
 /**
- * Create a directory or entire path.  Similar to `git_futils_mkdir_withperf`
+ * Create a directory or entire path.  Similar to `git_futils_mkdir_relative`
  * without performance data.
  */
-extern int git_futils_mkdir(const char *path, const char *base, mode_t mode, uint32_t flags);
+extern int git_futils_mkdir(const char *path, mode_t mode, uint32_t flags);
 
 /**
  * Create all the folders required to contain
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 99b8f7c..730c4b1 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -84,9 +84,9 @@ static int object_file_name(
 
 static int object_mkdir(const git_buf *name, const loose_backend *be)
 {
-	return git_futils_mkdir(
+	return git_futils_mkdir_relative(
 		name->ptr + be->objects_dirlen, be->objects_dir, be->object_dir_mode,
-		GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
+		GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR, NULL);
 }
 
 static size_t get_binary_object_header(obj_hdr *hdr, git_buf *obj)
diff --git a/src/path.c b/src/path.c
index cb11ace..72cb289 100644
--- a/src/path.c
+++ b/src/path.c
@@ -526,6 +526,17 @@ bool git_path_isfile(const char *path)
 	return S_ISREG(st.st_mode) != 0;
 }
 
+bool git_path_islink(const char *path)
+{
+	struct stat st;
+
+	assert(path);
+	if (p_lstat(path, &st) < 0)
+		return false;
+
+	return S_ISLNK(st.st_mode) != 0;
+}
+
 #ifdef GIT_WIN32
 
 bool git_path_is_empty_dir(const char *path)
diff --git a/src/path.h b/src/path.h
index 971603e..7e156fc 100644
--- a/src/path.h
+++ b/src/path.h
@@ -72,7 +72,7 @@ extern const char *git_path_topdir(const char *path);
  * This will return a number >= 0 which is the offset to the start of the
  * path, if the path is rooted (i.e. "/rooted/path" returns 0 and
  * "c:/windows/rooted/path" returns 2).  If the path is not rooted, this
- * returns < 0.
+ * returns -1.
  */
 extern int git_path_root(const char *path);
 
@@ -169,6 +169,12 @@ extern bool git_path_isdir(const char *path);
 extern bool git_path_isfile(const char *path);
 
 /**
+ * Check if the given path points to a symbolic link.
+ * @return true or false
+ */
+extern bool git_path_islink(const char *path);
+
+/**
  * Check if the given path is a directory, and is empty.
  */
 extern bool git_path_is_empty_dir(const char *path);
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 1ddce46..921f786 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -1413,7 +1413,8 @@ static int setup_namespace(git_buf *path, git_repository *repo)
 	git__free(parts);
 
 	/* Make sure that the folder with the namespace exists */
-	if (git_futils_mkdir_r(git_buf_cstr(path), repo->path_repository, 0777) < 0)
+	if (git_futils_mkdir_relative(git_buf_cstr(path), repo->path_repository,
+			0777, GIT_MKDIR_PATH, NULL) < 0)
 		return -1;
 
 	/* Return root of the namespaced path, i.e. without the trailing '/refs' */
diff --git a/src/repository.c b/src/repository.c
index 0f37cfb..d0bf7dc 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -1441,8 +1441,8 @@ static int repo_init_structure(
 			if (chmod)
 				mkdir_flags |= GIT_MKDIR_CHMOD;
 
-			error = git_futils_mkdir(
-				tpl->path, repo_dir, dmode, mkdir_flags);
+			error = git_futils_mkdir_relative(
+				tpl->path, repo_dir, dmode, mkdir_flags, NULL);
 		}
 		else if (!external_tpl) {
 			const char *content = tpl->content;
@@ -1464,7 +1464,7 @@ static int mkdir_parent(git_buf *buf, uint32_t mode, bool skip2)
 	 * don't try to set gid or grant world write access
 	 */
 	return git_futils_mkdir(
-		buf->ptr, NULL, mode & ~(S_ISGID | 0002),
+		buf->ptr, mode & ~(S_ISGID | 0002),
 		GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR |
 		(skip2 ? GIT_MKDIR_SKIP_LAST2 : GIT_MKDIR_SKIP_LAST));
 }
@@ -1568,14 +1568,14 @@ static int repo_init_directories(
 		/* create path #4 */
 		if (wd_path->size > 0 &&
 			(error = git_futils_mkdir(
-				wd_path->ptr, NULL, dirmode & ~S_ISGID,
+				wd_path->ptr, dirmode & ~S_ISGID,
 				GIT_MKDIR_VERIFY_DIR)) < 0)
 			return error;
 
 		/* create path #2 (if not the same as #4) */
 		if (!natural_wd &&
 			(error = git_futils_mkdir(
-				repo_path->ptr, NULL, dirmode & ~S_ISGID,
+				repo_path->ptr, dirmode & ~S_ISGID,
 				GIT_MKDIR_VERIFY_DIR | GIT_MKDIR_SKIP_LAST)) < 0)
 			return error;
 	}
@@ -1585,7 +1585,7 @@ static int repo_init_directories(
 		has_dotgit)
 	{
 		/* create path #1 */
-		error = git_futils_mkdir(repo_path->ptr, NULL, dirmode,
+		error = git_futils_mkdir(repo_path->ptr, dirmode,
 			GIT_MKDIR_VERIFY_DIR | ((dirmode & S_ISGID) ? GIT_MKDIR_CHMOD : 0));
 	}
 
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index c909af6..414cb47 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -148,12 +148,19 @@ static int lstat_w(
 		return git_win32__file_attribute_to_stat(buf, &fdata, path);
 	}
 
-	errno = ENOENT;
+	switch (GetLastError()) {
+	case ERROR_ACCESS_DENIED:
+		errno = EACCES;
+		break;
+	default:
+		errno = ENOENT;
+		break;
+	}
 
 	/* To match POSIX behavior, set ENOTDIR when any of the folders in the
 	 * file path is a regular file, otherwise set ENOENT.
 	 */
-	if (posix_enotdir) {
+	if (errno == ENOENT && posix_enotdir) {
 		size_t path_len = wcslen(path);
 
 		/* scan up path until we find an existing item */
diff --git a/tests/checkout/index.c b/tests/checkout/index.c
index 0d220e1..9fa9018 100644
--- a/tests/checkout/index.c
+++ b/tests/checkout/index.c
@@ -63,7 +63,7 @@ void test_checkout_index__can_remove_untracked_files(void)
 {
 	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
 
-	git_futils_mkdir("./testrepo/dir/subdir/subsubdir", NULL, 0755, GIT_MKDIR_PATH);
+	git_futils_mkdir("./testrepo/dir/subdir/subsubdir", 0755, GIT_MKDIR_PATH);
 	cl_git_mkfile("./testrepo/dir/one", "one\n");
 	cl_git_mkfile("./testrepo/dir/subdir/two", "two\n");
 
diff --git a/tests/config/global.c b/tests/config/global.c
index 4481308..b5e83fe 100644
--- a/tests/config/global.c
+++ b/tests/config/global.c
@@ -6,17 +6,17 @@ void test_config_global__initialize(void)
 {
 	git_buf path = GIT_BUF_INIT;
 
-	cl_git_pass(git_futils_mkdir_r("home", NULL, 0777));
+	cl_git_pass(git_futils_mkdir_r("home", 0777));
 	cl_git_pass(git_path_prettify(&path, "home", NULL));
 	cl_git_pass(git_libgit2_opts(
 		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
 
-	cl_git_pass(git_futils_mkdir_r("xdg/git", NULL, 0777));
+	cl_git_pass(git_futils_mkdir_r("xdg/git", 0777));
 	cl_git_pass(git_path_prettify(&path, "xdg/git", NULL));
 	cl_git_pass(git_libgit2_opts(
 		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
 
-	cl_git_pass(git_futils_mkdir_r("etc", NULL, 0777));
+	cl_git_pass(git_futils_mkdir_r("etc", 0777));
 	cl_git_pass(git_path_prettify(&path, "etc", NULL));
 	cl_git_pass(git_libgit2_opts(
 		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
diff --git a/tests/core/buffer.c b/tests/core/buffer.c
index 0e7026a..9872af7 100644
--- a/tests/core/buffer.c
+++ b/tests/core/buffer.c
@@ -929,7 +929,7 @@ void test_core_buffer__similarity_metric(void)
 	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
 	cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
 
-	cl_git_pass(git_futils_mkdir("scratch", NULL, 0755, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir("scratch", 0755, GIT_MKDIR_PATH));
 	cl_git_mkfile("scratch/testdata", SIMILARITY_TEST_DATA_1);
 	cl_git_pass(git_hashsig_create_fromfile(
 		&b, "scratch/testdata", GIT_HASHSIG_NORMAL));
diff --git a/tests/core/copy.c b/tests/core/copy.c
index 04b2dfa..967748c 100644
--- a/tests/core/copy.c
+++ b/tests/core/copy.c
@@ -25,7 +25,7 @@ void test_core_copy__file_in_dir(void)
 	struct stat st;
 	const char *content = "This is some other stuff to copy\n";
 
-	cl_git_pass(git_futils_mkdir("an_dir/in_a_dir", NULL, 0775, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir("an_dir/in_a_dir", 0775, GIT_MKDIR_PATH));
 	cl_git_mkfile("an_dir/in_a_dir/copy_me", content);
 	cl_assert(git_path_isdir("an_dir"));
 
@@ -60,9 +60,9 @@ void test_core_copy__tree(void)
 	struct stat st;
 	const char *content = "File content\n";
 
-	cl_git_pass(git_futils_mkdir("src/b", NULL, 0775, GIT_MKDIR_PATH));
-	cl_git_pass(git_futils_mkdir("src/c/d", NULL, 0775, GIT_MKDIR_PATH));
-	cl_git_pass(git_futils_mkdir("src/c/e", NULL, 0775, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir("src/b", 0775, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir("src/c/d", 0775, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir("src/c/e", 0775, GIT_MKDIR_PATH));
 
 	cl_git_mkfile("src/f1", content);
 	cl_git_mkfile("src/b/f2", content);
diff --git a/tests/core/mkdir.c b/tests/core/mkdir.c
index f76fe1d..5e6a060 100644
--- a/tests/core/mkdir.c
+++ b/tests/core/mkdir.c
@@ -13,43 +13,80 @@ static void cleanup_basic_dirs(void *ref)
 	git_futils_rmdir_r("d4", NULL, GIT_RMDIR_EMPTY_HIERARCHY);
 }
 
+void test_core_mkdir__absolute(void)
+{
+	git_buf path = GIT_BUF_INIT;
+
+	cl_set_cleanup(cleanup_basic_dirs, NULL);
+
+	git_buf_joinpath(&path, clar_sandbox_path(), "d0");
+
+	/* make a directory */
+	cl_assert(!git_path_isdir(path.ptr));
+	cl_git_pass(git_futils_mkdir(path.ptr, 0755, 0));
+	cl_assert(git_path_isdir(path.ptr));
+
+	git_buf_joinpath(&path, path.ptr, "subdir");
+	cl_assert(!git_path_isdir(path.ptr));
+	cl_git_pass(git_futils_mkdir(path.ptr, 0755, 0));
+	cl_assert(git_path_isdir(path.ptr));
+
+	/* ensure mkdir_r works for a single subdir */
+	git_buf_joinpath(&path, path.ptr, "another");
+	cl_assert(!git_path_isdir(path.ptr));
+	cl_git_pass(git_futils_mkdir_r(path.ptr, 0755));
+	cl_assert(git_path_isdir(path.ptr));
+
+	/* ensure mkdir_r works */
+	git_buf_joinpath(&path, clar_sandbox_path(), "d1/foo/bar/asdf");
+	cl_assert(!git_path_isdir(path.ptr));
+	cl_git_pass(git_futils_mkdir_r(path.ptr, 0755));
+	cl_assert(git_path_isdir(path.ptr));
+
+	/* ensure we don't imply recursive */
+	git_buf_joinpath(&path, clar_sandbox_path(), "d2/foo/bar/asdf");
+	cl_assert(!git_path_isdir(path.ptr));
+	cl_git_fail(git_futils_mkdir(path.ptr, 0755, 0));
+	cl_assert(!git_path_isdir(path.ptr));
+}
+
 void test_core_mkdir__basic(void)
 {
 	cl_set_cleanup(cleanup_basic_dirs, NULL);
 
 	/* make a directory */
 	cl_assert(!git_path_isdir("d0"));
-	cl_git_pass(git_futils_mkdir("d0", NULL, 0755, 0));
+	cl_git_pass(git_futils_mkdir("d0", 0755, 0));
 	cl_assert(git_path_isdir("d0"));
 
 	/* make a path */
 	cl_assert(!git_path_isdir("d1"));
-	cl_git_pass(git_futils_mkdir("d1/d1.1/d1.2", NULL, 0755, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir("d1/d1.1/d1.2", 0755, GIT_MKDIR_PATH));
 	cl_assert(git_path_isdir("d1"));
 	cl_assert(git_path_isdir("d1/d1.1"));
 	cl_assert(git_path_isdir("d1/d1.1/d1.2"));
 
 	/* make a dir exclusively */
 	cl_assert(!git_path_isdir("d2"));
-	cl_git_pass(git_futils_mkdir("d2", NULL, 0755, GIT_MKDIR_EXCL));
+	cl_git_pass(git_futils_mkdir("d2", 0755, GIT_MKDIR_EXCL));
 	cl_assert(git_path_isdir("d2"));
 
 	/* make exclusive failure */
-	cl_git_fail(git_futils_mkdir("d2", NULL, 0755, GIT_MKDIR_EXCL));
+	cl_git_fail(git_futils_mkdir("d2", 0755, GIT_MKDIR_EXCL));
 
 	/* make a path exclusively */
 	cl_assert(!git_path_isdir("d3"));
-	cl_git_pass(git_futils_mkdir("d3/d3.1/d3.2", NULL, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL));
+	cl_git_pass(git_futils_mkdir("d3/d3.1/d3.2", 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL));
 	cl_assert(git_path_isdir("d3"));
 	cl_assert(git_path_isdir("d3/d3.1/d3.2"));
 
 	/* make exclusive path failure */
-	cl_git_fail(git_futils_mkdir("d3/d3.1/d3.2", NULL, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL));
+	cl_git_fail(git_futils_mkdir("d3/d3.1/d3.2", 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL));
 	/* ??? Should EXCL only apply to the last item in the path? */
 
 	/* path with trailing slash? */
 	cl_assert(!git_path_isdir("d4"));
-	cl_git_pass(git_futils_mkdir("d4/d4.1/", NULL, 0755, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir("d4/d4.1/", 0755, GIT_MKDIR_PATH));
 	cl_assert(git_path_isdir("d4/d4.1"));
 }
 
@@ -65,38 +102,38 @@ void test_core_mkdir__with_base(void)
 
 	cl_set_cleanup(cleanup_basedir, NULL);
 
-	cl_git_pass(git_futils_mkdir(BASEDIR, NULL, 0755, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir(BASEDIR, 0755, GIT_MKDIR_PATH));
 
-	cl_git_pass(git_futils_mkdir("a", BASEDIR, 0755, 0));
+	cl_git_pass(git_futils_mkdir_relative("a", BASEDIR, 0755, 0, NULL));
 	cl_assert(git_path_isdir(BASEDIR "/a"));
 
-	cl_git_pass(git_futils_mkdir("b/b1/b2", BASEDIR, 0755, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir_relative("b/b1/b2", BASEDIR, 0755, GIT_MKDIR_PATH, NULL));
 	cl_assert(git_path_isdir(BASEDIR "/b/b1/b2"));
 
 	/* exclusive with existing base */
-	cl_git_pass(git_futils_mkdir("c/c1/c2", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL));
+	cl_git_pass(git_futils_mkdir_relative("c/c1/c2", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL, NULL));
 
 	/* fail: exclusive with duplicated suffix */
-	cl_git_fail(git_futils_mkdir("c/c1/c3", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL));
+	cl_git_fail(git_futils_mkdir_relative("c/c1/c3", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL, NULL));
 
 	/* fail: exclusive with any duplicated component */
-	cl_git_fail(git_futils_mkdir("c/cz/cz", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL));
+	cl_git_fail(git_futils_mkdir_relative("c/cz/cz", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL, NULL));
 
 	/* success: exclusive without path */
-	cl_git_pass(git_futils_mkdir("c/c1/c3", BASEDIR, 0755, GIT_MKDIR_EXCL));
+	cl_git_pass(git_futils_mkdir_relative("c/c1/c3", BASEDIR, 0755, GIT_MKDIR_EXCL, NULL));
 
 	/* path with shorter base and existing dirs */
-	cl_git_pass(git_futils_mkdir("dir/here/d/", "base", 0755, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir_relative("dir/here/d/", "base", 0755, GIT_MKDIR_PATH, NULL));
 	cl_assert(git_path_isdir("base/dir/here/d"));
 
 	/* fail: path with shorter base and existing dirs */
-	cl_git_fail(git_futils_mkdir("dir/here/e/", "base", 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL));
+	cl_git_fail(git_futils_mkdir_relative("dir/here/e/", "base", 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL, NULL));
 
 	/* fail: base with missing components */
-	cl_git_fail(git_futils_mkdir("f/", "base/missing", 0755, GIT_MKDIR_PATH));
+	cl_git_fail(git_futils_mkdir_relative("f/", "base/missing", 0755, GIT_MKDIR_PATH, NULL));
 
 	/* success: shift missing component to path */
-	cl_git_pass(git_futils_mkdir("missing/f/", "base/", 0755, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir_relative("missing/f/", "base/", 0755, GIT_MKDIR_PATH, NULL));
 }
 
 static void cleanup_chmod_root(void *ref)
@@ -135,9 +172,9 @@ void test_core_mkdir__chmods(void)
 
 	cl_set_cleanup(cleanup_chmod_root, old);
 
-	cl_git_pass(git_futils_mkdir("r", NULL, 0777, 0));
+	cl_git_pass(git_futils_mkdir("r", 0777, 0));
 
-	cl_git_pass(git_futils_mkdir("mode/is/important", "r", 0777, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir_relative("mode/is/important", "r", 0777, GIT_MKDIR_PATH, NULL));
 
 	cl_git_pass(git_path_lstat("r/mode", &st));
 	check_mode(0755, st.st_mode);
@@ -146,7 +183,7 @@ void test_core_mkdir__chmods(void)
 	cl_git_pass(git_path_lstat("r/mode/is/important", &st));
 	check_mode(0755, st.st_mode);
 
-	cl_git_pass(git_futils_mkdir("mode2/is2/important2", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD));
+	cl_git_pass(git_futils_mkdir_relative("mode2/is2/important2", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD, NULL));
 
 	cl_git_pass(git_path_lstat("r/mode2", &st));
 	check_mode(0755, st.st_mode);
@@ -155,7 +192,7 @@ void test_core_mkdir__chmods(void)
 	cl_git_pass(git_path_lstat("r/mode2/is2/important2", &st));
 	check_mode(0777, st.st_mode);
 
-	cl_git_pass(git_futils_mkdir("mode3/is3/important3", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD_PATH));
+	cl_git_pass(git_futils_mkdir_relative("mode3/is3/important3", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD_PATH, NULL));
 
 	cl_git_pass(git_path_lstat("r/mode3", &st));
 	check_mode(0777, st.st_mode);
@@ -166,7 +203,7 @@ void test_core_mkdir__chmods(void)
 
 	/* test that we chmod existing dir */
 
-	cl_git_pass(git_futils_mkdir("mode/is/important", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD));
+	cl_git_pass(git_futils_mkdir_relative("mode/is/important", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD, NULL));
 
 	cl_git_pass(git_path_lstat("r/mode", &st));
 	check_mode(0755, st.st_mode);
@@ -177,7 +214,7 @@ void test_core_mkdir__chmods(void)
 
 	/* test that we chmod even existing dirs if CHMOD_PATH is set */
 
-	cl_git_pass(git_futils_mkdir("mode2/is2/important2.1", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD_PATH));
+	cl_git_pass(git_futils_mkdir_relative("mode2/is2/important2.1", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD_PATH, NULL));
 
 	cl_git_pass(git_path_lstat("r/mode2", &st));
 	check_mode(0777, st.st_mode);
@@ -187,6 +224,40 @@ void test_core_mkdir__chmods(void)
 	check_mode(0777, st.st_mode);
 }
 
+void test_core_mkdir__keeps_parent_symlinks(void)
+{
+#ifndef GIT_WIN32
+	git_buf path = GIT_BUF_INIT;
+
+	cl_set_cleanup(cleanup_basic_dirs, NULL);
+
+	/* make a directory */
+	cl_assert(!git_path_isdir("d0"));
+	cl_git_pass(git_futils_mkdir("d0", 0755, 0));
+	cl_assert(git_path_isdir("d0"));
+
+	cl_must_pass(symlink("d0", "d1"));
+	cl_assert(git_path_islink("d1"));
+
+	cl_git_pass(git_futils_mkdir("d1/foo/bar", 0755, GIT_MKDIR_PATH|GIT_MKDIR_REMOVE_SYMLINKS));
+	cl_assert(git_path_islink("d1"));
+	cl_assert(git_path_isdir("d1/foo/bar"));
+	cl_assert(git_path_isdir("d0/foo/bar"));
+
+	cl_must_pass(symlink("d0", "d2"));
+	cl_assert(git_path_islink("d2"));
+
+	git_buf_joinpath(&path, clar_sandbox_path(), "d2/other/dir");
+
+	cl_git_pass(git_futils_mkdir(path.ptr, 0755, GIT_MKDIR_PATH|GIT_MKDIR_REMOVE_SYMLINKS));
+	cl_assert(git_path_islink("d2"));
+	cl_assert(git_path_isdir("d2/other/dir"));
+	cl_assert(git_path_isdir("d0/other/dir"));
+
+	git_buf_free(&path);
+#endif
+}
+
 void test_core_mkdir__mkdir_path_inside_unwriteable_parent(void)
 {
 	struct stat st;
@@ -200,8 +271,8 @@ void test_core_mkdir__mkdir_path_inside_unwriteable_parent(void)
 	*old = p_umask(022);
 	cl_set_cleanup(cleanup_chmod_root, old);
 
-	cl_git_pass(git_futils_mkdir("r", NULL, 0777, 0));
-	cl_git_pass(git_futils_mkdir("mode/is/important", "r", 0777, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir("r", 0777, 0));
+	cl_git_pass(git_futils_mkdir_relative("mode/is/important", "r", 0777, GIT_MKDIR_PATH, NULL));
 	cl_git_pass(git_path_lstat("r/mode", &st));
 	check_mode(0755, st.st_mode);
 
@@ -210,10 +281,9 @@ void test_core_mkdir__mkdir_path_inside_unwriteable_parent(void)
 	check_mode(0111, st.st_mode);
 
 	cl_git_pass(
-		git_futils_mkdir("mode/is/okay/inside", "r", 0777, GIT_MKDIR_PATH));
+		git_futils_mkdir_relative("mode/is/okay/inside", "r", 0777, GIT_MKDIR_PATH, NULL));
 	cl_git_pass(git_path_lstat("r/mode/is/okay/inside", &st));
 	check_mode(0755, st.st_mode);
 
 	cl_must_pass(p_chmod("r/mode", 0777));
 }
-
diff --git a/tests/core/stat.c b/tests/core/stat.c
index bd9b990..ef2e45a 100644
--- a/tests/core/stat.c
+++ b/tests/core/stat.c
@@ -5,7 +5,7 @@
 
 void test_core_stat__initialize(void)
 {
-	cl_git_pass(git_futils_mkdir("root/d1/d2", NULL, 0755, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir("root/d1/d2", 0755, GIT_MKDIR_PATH));
 	cl_git_mkfile("root/file", "whatever\n");
 	cl_git_mkfile("root/d1/file", "whatever\n");
 }
diff --git a/tests/index/tests.c b/tests/index/tests.c
index 2a416fc..1498196 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -752,7 +752,7 @@ void test_index_tests__reload_from_disk(void)
 
 	cl_set_cleanup(&cleanup_myrepo, NULL);
 
-	cl_git_pass(git_futils_mkdir("./myrepo", NULL, 0777, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir("./myrepo", 0777, GIT_MKDIR_PATH));
 	cl_git_mkfile("./myrepo/a.txt", "a\n");
 	cl_git_mkfile("./myrepo/b.txt", "b\n");
 
diff --git a/tests/odb/alternates.c b/tests/odb/alternates.c
index c75f6fe..b5c0e79 100644
--- a/tests/odb/alternates.c
+++ b/tests/odb/alternates.c
@@ -29,7 +29,7 @@ static void init_linked_repo(const char *path, const char *alternate)
 	cl_git_pass(git_path_prettify(&destpath, alternate, NULL));
 	cl_git_pass(git_buf_joinpath(&destpath, destpath.ptr, "objects"));
 	cl_git_pass(git_buf_joinpath(&filepath, git_repository_path(repo), "objects/info"));
-	cl_git_pass(git_futils_mkdir(filepath.ptr, NULL, 0755, GIT_MKDIR_PATH));
+	cl_git_pass(git_futils_mkdir(filepath.ptr, 0755, GIT_MKDIR_PATH));
 	cl_git_pass(git_buf_joinpath(&filepath, filepath.ptr , "alternates"));
 
 	cl_git_pass(git_filebuf_open(&file, git_buf_cstr(&filepath), 0, 0666));
diff --git a/tests/refs/pack.c b/tests/refs/pack.c
index 7dfaf6d..bda86f6 100644
--- a/tests/refs/pack.c
+++ b/tests/refs/pack.c
@@ -36,7 +36,7 @@ void test_refs_pack__empty(void)
 	git_buf temp_path = GIT_BUF_INIT;
 
 	cl_git_pass(git_buf_join_n(&temp_path, '/', 3, git_repository_path(g_repo), GIT_REFS_HEADS_DIR, "empty_dir"));
-	cl_git_pass(git_futils_mkdir_r(temp_path.ptr, NULL, GIT_REFS_DIR_MODE));
+	cl_git_pass(git_futils_mkdir_r(temp_path.ptr, GIT_REFS_DIR_MODE));
 	git_buf_free(&temp_path);
 
 	packall();
diff --git a/tests/repo/discover.c b/tests/repo/discover.c
index 7904b64..86bd745 100644
--- a/tests/repo/discover.c
+++ b/tests/repo/discover.c
@@ -77,7 +77,7 @@ void test_repo_discover__0(void)
 	const char *ceiling_dirs;
 	const mode_t mode = 0777;
 
-	git_futils_mkdir_r(DISCOVER_FOLDER, NULL, mode);
+	git_futils_mkdir_r(DISCOVER_FOLDER, mode);
 	append_ceiling_dir(&ceiling_dirs_buf, TEMP_REPO_FOLDER);
 	ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf);
 
@@ -88,15 +88,15 @@ void test_repo_discover__0(void)
 	git_repository_free(repo);
 
 	cl_git_pass(git_repository_init(&repo, SUB_REPOSITORY_FOLDER, 0));
-	cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode));
+	cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, mode));
 	cl_git_pass(git_repository_discover(&sub_repository_path, SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
 
-	cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode));
+	cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, mode));
 	ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, &sub_repository_path);
 	ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path);
 	ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, &sub_repository_path);
 
-	cl_git_pass(git_futils_mkdir_r(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, NULL, mode));
+	cl_git_pass(git_futils_mkdir_r(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, mode));
 	write_file(REPOSITORY_ALTERNATE_FOLDER "/" DOT_GIT, "gitdir: ../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT);
 	write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/" DOT_GIT, "gitdir: ../../../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT);
 	write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB "/" DOT_GIT, "gitdir: ../../../../");
@@ -105,13 +105,13 @@ void test_repo_discover__0(void)
 	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, &sub_repository_path);
 	ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, &repository_path);
 
-	cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER1, NULL, mode));
+	cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER1, mode));
 	write_file(ALTERNATE_MALFORMED_FOLDER1 "/" DOT_GIT, "Anything but not gitdir:");
-	cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER2, NULL, mode));
+	cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER2, mode));
 	write_file(ALTERNATE_MALFORMED_FOLDER2 "/" DOT_GIT, "gitdir:");
-	cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER3, NULL, mode));
+	cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER3, mode));
 	write_file(ALTERNATE_MALFORMED_FOLDER3 "/" DOT_GIT, "gitdir: \n\n\n");
-	cl_git_pass(git_futils_mkdir_r(ALTERNATE_NOT_FOUND_FOLDER, NULL, mode));
+	cl_git_pass(git_futils_mkdir_r(ALTERNATE_NOT_FOUND_FOLDER, mode));
 	write_file(ALTERNATE_NOT_FOUND_FOLDER "/" DOT_GIT, "gitdir: a_repository_that_surely_does_not_exist");
 	cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs));
 	cl_git_fail(git_repository_discover(&found_path, ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs));
diff --git a/tests/repo/init.c b/tests/repo/init.c
index 929d741..7a9ec20 100644
--- a/tests/repo/init.c
+++ b/tests/repo/init.c
@@ -99,7 +99,7 @@ void test_repo_init__bare_repo_escaping_current_workdir(void)
 	cl_git_pass(git_path_prettify_dir(&path_current_workdir, ".", NULL));
 
 	cl_git_pass(git_buf_joinpath(&path_repository, git_buf_cstr(&path_current_workdir), "a/b/c"));
-	cl_git_pass(git_futils_mkdir_r(git_buf_cstr(&path_repository), NULL, GIT_DIR_MODE));
+	cl_git_pass(git_futils_mkdir_r(git_buf_cstr(&path_repository), GIT_DIR_MODE));
 
 	/* Change the current working directory */
 	cl_git_pass(chdir(git_buf_cstr(&path_repository)));
@@ -312,7 +312,7 @@ void test_repo_init__extended_0(void)
 	cl_git_fail(git_repository_init_ext(&_repo, "extended", &opts));
 
 	/* make the directory first, then it should succeed */
-	cl_git_pass(git_futils_mkdir("extended", NULL, 0775, 0));
+	cl_git_pass(git_futils_mkdir("extended", 0775, 0));
 	cl_git_pass(git_repository_init_ext(&_repo, "extended", &opts));
 
 	cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "/extended/"));
@@ -631,7 +631,7 @@ void test_repo_init__can_reinit_an_initialized_repository(void)
 
 	cl_set_cleanup(&cleanup_repository, "extended");
 
-	cl_git_pass(git_futils_mkdir("extended", NULL, 0775, 0));
+	cl_git_pass(git_futils_mkdir("extended", 0775, 0));
 	cl_git_pass(git_repository_init(&_repo, "extended", false));
 
 	cl_git_pass(git_repository_init(&reinit, "extended", false));
diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c
index 9c4cc9e..83b8246 100644
--- a/tests/repo/iterator.c
+++ b/tests/repo/iterator.c
@@ -848,14 +848,14 @@ static void build_workdir_tree(const char *root, int dirs, int subs)
 	for (i = 0; i < dirs; ++i) {
 		if (i % 2 == 0) {
 			p_snprintf(buf, sizeof(buf), "%s/dir%02d", root, i);
-			cl_git_pass(git_futils_mkdir(buf, NULL, 0775, GIT_MKDIR_PATH));
+			cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH));
 
 			p_snprintf(buf, sizeof(buf), "%s/dir%02d/file", root, i);
 			cl_git_mkfile(buf, buf);
 			buf[strlen(buf) - 5] = '\0';
 		} else {
 			p_snprintf(buf, sizeof(buf), "%s/DIR%02d", root, i);
-			cl_git_pass(git_futils_mkdir(buf, NULL, 0775, GIT_MKDIR_PATH));
+			cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH));
 		}
 
 		for (j = 0; j < subs; ++j) {
@@ -865,7 +865,7 @@ static void build_workdir_tree(const char *root, int dirs, int subs)
 			case 2: p_snprintf(sub, sizeof(sub), "%s/Sub%02d", buf, j); break;
 			case 3: p_snprintf(sub, sizeof(sub), "%s/SUB%02d", buf, j); break;
 			}
-			cl_git_pass(git_futils_mkdir(sub, NULL, 0775, GIT_MKDIR_PATH));
+			cl_git_pass(git_futils_mkdir(sub, 0775, GIT_MKDIR_PATH));
 
 			if (j % 2 == 0) {
 				size_t sublen = strlen(sub);
diff --git a/tests/repo/open.c b/tests/repo/open.c
index eb459e5..d3d0872 100644
--- a/tests/repo/open.c
+++ b/tests/repo/open.c
@@ -91,7 +91,7 @@ static void make_gitlink_dir(const char *dir, const char *linktext)
 {
 	git_buf path = GIT_BUF_INIT;
 
-	cl_git_pass(git_futils_mkdir(dir, NULL, 0777, GIT_MKDIR_VERIFY_DIR));
+	cl_git_pass(git_futils_mkdir(dir, 0777, GIT_MKDIR_VERIFY_DIR));
 	cl_git_pass(git_buf_joinpath(&path, dir, ".git"));
 	cl_git_rewritefile(path.ptr, linktext);
 	git_buf_free(&path);
@@ -222,7 +222,7 @@ void test_repo_open__bad_gitlinks(void)
 	cl_git_sandbox_init("attr");
 
 	cl_git_pass(p_mkdir("invalid", 0777));
-	cl_git_pass(git_futils_mkdir_r("invalid2/.git", NULL, 0777));
+	cl_git_pass(git_futils_mkdir_r("invalid2/.git", 0777));
 
 	for (scan = bad_links; *scan != NULL; scan++) {
 		make_gitlink_dir("alternate", *scan);
diff --git a/tests/status/ignore.c b/tests/status/ignore.c
index bbf8f49..c318046 100644
--- a/tests/status/ignore.c
+++ b/tests/status/ignore.c
@@ -148,7 +148,7 @@ void test_status_ignore__ignore_pattern_contains_space(void)
 	cl_git_pass(git_status_file(&flags, g_repo, "foo bar.txt"));
 	cl_assert(flags == GIT_STATUS_IGNORED);
 
-	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/foo", NULL, mode));
+	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/foo", mode));
 	cl_git_mkfile("empty_standard_repo/foo/look-ma.txt", "I'm not going to be ignored!");
 
 	cl_git_pass(git_status_file(&flags, g_repo, "foo/look-ma.txt"));
@@ -206,7 +206,7 @@ void test_status_ignore__subdirectories(void)
 	 * used a rooted path for an ignore, so I changed this behavior.
 	 */
 	cl_git_pass(git_futils_mkdir_r(
-		"empty_standard_repo/test/ignore_me", NULL, 0775));
+		"empty_standard_repo/test/ignore_me", 0775));
 	cl_git_mkfile(
 		"empty_standard_repo/test/ignore_me/file", "I'm going to be ignored!");
 	cl_git_mkfile(
@@ -230,9 +230,9 @@ static void make_test_data(const char *reponame, const char **files)
 	g_repo = cl_git_sandbox_init(reponame);
 
 	for (scan = files; *scan != NULL; ++scan) {
-		cl_git_pass(git_futils_mkdir(
+		cl_git_pass(git_futils_mkdir_relative(
 			*scan + repolen, reponame,
-			0777, GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST));
+			0777, GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST, NULL));
 		cl_git_mkfile(*scan, "contents");
 	}
 }
@@ -612,7 +612,7 @@ void test_status_ignore__issue_1766_negated_ignores(void)
 	g_repo = cl_git_sandbox_init("empty_standard_repo");
 
 	cl_git_pass(git_futils_mkdir_r(
-		"empty_standard_repo/a", NULL, 0775));
+		"empty_standard_repo/a", 0775));
 	cl_git_mkfile(
 		"empty_standard_repo/a/.gitignore", "*\n!.gitignore\n");
 	cl_git_mkfile(
@@ -622,7 +622,7 @@ void test_status_ignore__issue_1766_negated_ignores(void)
 	assert_is_ignored("a/ignoreme");
 
 	cl_git_pass(git_futils_mkdir_r(
-		"empty_standard_repo/b", NULL, 0775));
+		"empty_standard_repo/b", 0775));
 	cl_git_mkfile(
 		"empty_standard_repo/b/.gitignore", "*\n!.gitignore\n");
 	cl_git_mkfile(
@@ -1033,7 +1033,7 @@ void test_status_ignore__negate_starstar(void)
               "code/projects/**/packages/*\n"
               "!code/projects/**/packages/repositories.config");
 
-    cl_git_pass(git_futils_mkdir_r("code/projects/foo/bar/packages", "empty_standard_repo", 0777));
+    cl_git_pass(git_futils_mkdir_r("empty_standard_repo/code/projects/foo/bar/packages", 0777));
     cl_git_mkfile("empty_standard_repo/code/projects/foo/bar/packages/repositories.config", "");
 
     cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "code/projects/foo/bar/packages/repositories.config"));
diff --git a/tests/status/worktree.c b/tests/status/worktree.c
index 75c7b71..fc4afc6 100644
--- a/tests/status/worktree.c
+++ b/tests/status/worktree.c
@@ -195,7 +195,7 @@ void test_status_worktree__swap_subdir_with_recurse_and_pathspec(void)
 	cl_git_pass(p_rename("status/subdir", "status/current_file"));
 	cl_git_pass(p_rename("status/swap", "status/subdir"));
 	cl_git_mkfile("status/.new_file", "dummy");
-	cl_git_pass(git_futils_mkdir_r("status/zzz_new_dir", NULL, 0777));
+	cl_git_pass(git_futils_mkdir_r("status/zzz_new_dir", 0777));
 	cl_git_mkfile("status/zzz_new_dir/new_file", "dummy");
 	cl_git_mkfile("status/zzz_new_file", "dummy");
 
@@ -917,7 +917,7 @@ void test_status_worktree__long_filenames(void)
 
 	// Create directory with amazingly long filename
 	sprintf(path, "empty_standard_repo/%s", longname);
-	cl_git_pass(git_futils_mkdir_r(path, NULL, 0777));
+	cl_git_pass(git_futils_mkdir_r(path, 0777));
 	sprintf(path, "empty_standard_repo/%s/foo", longname);
 	cl_git_mkfile(path, "dummy");
 
@@ -1007,7 +1007,7 @@ void test_status_worktree__unreadable(void)
 	status_entry_counts counts = {0};
 
 	/* Create directory with no read permission */
-	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/no_permission", NULL, 0777));
+	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/no_permission", 0777));
 	cl_git_mkfile("empty_standard_repo/no_permission/foo", "dummy");
 	p_chmod("empty_standard_repo/no_permission", 0644);
 
@@ -1041,7 +1041,7 @@ void test_status_worktree__unreadable_not_included(void)
 	status_entry_counts counts = {0};
 
 	/* Create directory with no read permission */
-	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/no_permission", NULL, 0777));
+	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/no_permission", 0777));
 	cl_git_mkfile("empty_standard_repo/no_permission/foo", "dummy");
 	p_chmod("empty_standard_repo/no_permission", 0644);
 
@@ -1074,7 +1074,7 @@ void test_status_worktree__unreadable_as_untracked(void)
 	status_entry_counts counts = {0};
 
 	/* Create directory with no read permission */
-	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/no_permission", NULL, 0777));
+	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/no_permission", 0777));
 	cl_git_mkfile("empty_standard_repo/no_permission/foo", "dummy");
 	p_chmod("empty_standard_repo/no_permission", 0644);
 
diff --git a/tests/submodule/status.c b/tests/submodule/status.c
index 5f4e620..10f385c 100644
--- a/tests/submodule/status.c
+++ b/tests/submodule/status.c
@@ -92,7 +92,7 @@ void test_submodule_status__ignore_none(void)
 	cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0);
 
 	/* now mkdir sm_unchanged to test uninitialized */
-	cl_git_pass(git_futils_mkdir("sm_unchanged", "submod2", 0755, 0));
+	cl_git_pass(git_futils_mkdir_relative("sm_unchanged", "submod2", 0755, 0, NULL));
 	status = get_submodule_status(g_repo, "sm_unchanged");
 	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0);
 
@@ -141,7 +141,7 @@ void test_submodule_status__ignore_untracked(void)
 	cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0);
 
 	/* now mkdir sm_unchanged to test uninitialized */
-	cl_git_pass(git_futils_mkdir("sm_unchanged", "submod2", 0755, 0));
+	cl_git_pass(git_futils_mkdir_relative("sm_unchanged", "submod2", 0755, 0, NULL));
 	cl_git_pass(git_submodule_status(&status, g_repo,"sm_unchanged", ign));
 	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0);
 
@@ -185,7 +185,7 @@ void test_submodule_status__ignore_dirty(void)
 	cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0);
 
 	/* now mkdir sm_unchanged to test uninitialized */
-	cl_git_pass(git_futils_mkdir("sm_unchanged", "submod2", 0755, 0));
+	cl_git_pass(git_futils_mkdir_relative("sm_unchanged", "submod2", 0755, 0, NULL));
 	cl_git_pass(git_submodule_status(&status, g_repo,"sm_unchanged", ign));
 	cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0);
 
@@ -229,7 +229,7 @@ void test_submodule_status__ignore_all(void)
 	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
 
 	/* now mkdir sm_unchanged to test uninitialized */
-	cl_git_pass(git_futils_mkdir("sm_unchanged", "submod2", 0755, 0));
+	cl_git_pass(git_futils_mkdir_relative("sm_unchanged", "submod2", 0755, 0, NULL));
 	cl_git_pass(git_submodule_status(&status, g_repo,"sm_unchanged", ign));
 	cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status));
 
@@ -338,7 +338,7 @@ void test_submodule_status__untracked_dirs_containing_ignored_files(void)
 		"submod2/.git/modules/sm_unchanged/info/exclude", "\n*.ignored\n");
 
 	cl_git_pass(
-		git_futils_mkdir("sm_unchanged/directory", "submod2", 0755, 0));
+		git_futils_mkdir_relative("sm_unchanged/directory", "submod2", 0755, 0, NULL));
 	cl_git_mkfile(
 		"submod2/sm_unchanged/directory/i_am.ignored",
 		"ignore this file, please\n");