Commit 1744fafec05d8fa3036a43f5e390c790810b05a5

Russell Belfer 2012-01-17T15:49:47

Move path related functions from fileops to path This takes all of the functions that look up simple data about paths (such as `git_futils_isdir`) and moves them over to path.h (becoming `git_path_isdir`). This leaves fileops.h just with functions that actually manipulate the filesystem or look at the file contents in some way. As part of this, the dir.h header which is really just for win32 support was moved into win32 (with some minor changes).

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
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
diff --git a/src/attr.c b/src/attr.c
index dc42379..984b04f 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -230,7 +230,7 @@ int git_attr_cache__push_file(
 
 	/* either get attr_file from cache or read from disk */
 	file = git_hashtable_lookup(cache->files, filename);
-	if (file == NULL && git_futils_exists(filename) == GIT_SUCCESS) {
+	if (file == NULL && git_path_exists(filename) == GIT_SUCCESS) {
 		error = (*loader)(repo, filename, &file);
 		add_to_cache = (error == GIT_SUCCESS);
 	}
@@ -279,7 +279,7 @@ static int collect_attr_files(
 	if ((error = git_vector_init(files, 4, NULL)) < GIT_SUCCESS)
 		goto cleanup;
 
-	if ((error = git_futils_dir_for_path(&dir, path, workdir)) < GIT_SUCCESS)
+	if ((error = git_path_find_dir(&dir, path, workdir)) < GIT_SUCCESS)
 		goto cleanup;
 
 	/* in precendence order highest to lowest:
diff --git a/src/attr_file.c b/src/attr_file.c
index 5ea07c9..b38b35f 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -223,7 +223,7 @@ int git_attr_path__init(
 		info->basename++;
 	if (!info->basename || !*info->basename)
 		info->basename = path;
-	info->is_dir = (git_futils_isdir(path) == GIT_SUCCESS);
+	info->is_dir = (git_path_isdir(path) == GIT_SUCCESS);
 	return GIT_SUCCESS;
 }
 
diff --git a/src/dir.h b/src/dir.h
deleted file mode 100644
index 5d50692..0000000
--- a/src/dir.h
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (C) 2009-2011 the libgit2 contributors
- *
- * 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_dir_h__
-#define INCLUDE_dir_h__
-
-#include "common.h"
-
-#ifndef GIT_WIN32
-# include <dirent.h>
-#endif
-
-#ifdef GIT_WIN32
-
-struct git__dirent {
-	int d_ino;
-	char d_name[261];
-};
-
-typedef struct {
-	HANDLE h;
-	WIN32_FIND_DATAW f;
-	struct git__dirent entry;
-	char *dir;
-	int first;
-} git__DIR;
-
-extern git__DIR *git__opendir(const char *);
-extern struct git__dirent *git__readdir(git__DIR *);
-extern void git__rewinddir(git__DIR *);
-extern int git__closedir(git__DIR *);
-
-# ifndef GIT__WIN32_NO_WRAP_DIR
-#	define dirent git__dirent
-#	define DIR git__DIR
-#	define opendir	git__opendir
-#	define readdir	git__readdir
-#	define rewinddir git__rewinddir
-#	define closedir git__closedir
-# endif
-
-#endif
-
-#endif /* INCLUDE_dir_h__ */
diff --git a/src/filebuf.c b/src/filebuf.c
index aa47d5e..447d8a0 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -16,7 +16,7 @@ static const size_t WRITE_BUFFER_SIZE = (4096 * 2);
 
 static int lock_file(git_filebuf *file, int flags)
 {
-	if (git_futils_exists(file->path_lock) == 0) {
+	if (git_path_exists(file->path_lock) == 0) {
 		if (flags & GIT_FILEBUF_FORCE)
 			p_unlink(file->path_lock);
 		else
@@ -34,7 +34,7 @@ static int lock_file(git_filebuf *file, int flags)
 	if (file->fd < 0)
 		return git__throw(GIT_EOSERR, "Failed to create lock");
 
-	if ((flags & GIT_FILEBUF_APPEND) && git_futils_exists(file->path_original) == 0) {
+	if ((flags & GIT_FILEBUF_APPEND) && git_path_exists(file->path_original) == 0) {
 		git_file source;
 		char buffer[2048];
 		size_t read_bytes;
@@ -60,7 +60,7 @@ void git_filebuf_cleanup(git_filebuf *file)
 	if (file->fd >= 0)
 		p_close(file->fd);
 
-	if (file->fd >= 0 && file->path_lock && git_futils_exists(file->path_lock) == GIT_SUCCESS)
+	if (file->fd >= 0 && file->path_lock && git_path_exists(file->path_lock) == GIT_SUCCESS)
 		p_unlink(file->path_lock);
 
 	if (file->digest)
diff --git a/src/fileops.c b/src/fileops.c
index 3412a47..e2a6adf 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -23,7 +23,7 @@ int git_futils_mkpath2file(const char *file_path, const mode_t mode)
 	}
 
 	/* Does the containing folder exist? */
-	if (git_futils_isdir(target_folder.ptr) != GIT_SUCCESS)
+	if (git_path_isdir(target_folder.ptr) != GIT_SUCCESS)
 		/* Let's create the tree structure */
 		error = git_futils_mkdir_r(target_folder.ptr, NULL, mode);
 
@@ -70,47 +70,6 @@ int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, con
 	return git_futils_creat_locked(path, mode);
 }
 
-int git_futils_isdir(const char *path)
-{
-#ifdef GIT_WIN32
-	DWORD attr = GetFileAttributes(path);
-	if (attr == INVALID_FILE_ATTRIBUTES)
-		return GIT_ERROR;
-
-	return (attr & FILE_ATTRIBUTE_DIRECTORY) ? GIT_SUCCESS : GIT_ERROR;
-
-#else
-	struct stat st;
-	if (p_stat(path, &st) < GIT_SUCCESS)
-		return GIT_ERROR;
-
-	return S_ISDIR(st.st_mode) ? GIT_SUCCESS : GIT_ERROR;
-#endif
-}
-
-int git_futils_isfile(const char *path)
-{
-	struct stat st;
-	int stat_error;
-
-	assert(path);
-	stat_error = p_stat(path, &st);
-
-	if (stat_error < GIT_SUCCESS)
-		return -1;
-
-	if (!S_ISREG(st.st_mode))
-		return -1;
-
-	return 0;
-}
-
-int git_futils_exists(const char *path)
-{
-	assert(path);
-	return p_access(path, F_OK);
-}
-
 git_off_t git_futils_filesize(git_file fd)
 {
 	struct stat sb;
@@ -219,54 +178,6 @@ void git_futils_mmap_free(git_map *out)
 	p_munmap(out);
 }
 
-/* Taken from git.git */
-GIT_INLINE(int) is_dot_or_dotdot(const char *name)
-{
-	return (name[0] == '.' &&
-		(name[1] == '\0' ||
-		 (name[1] == '.' && name[2] == '\0')));
-}
-
-int git_futils_direach(
-	git_buf *path,
-	int (*fn)(void *, git_buf *),
-	void *arg)
-{
-	ssize_t wd_len;
-	DIR *dir;
-	struct dirent *de;
-
-	if (git_path_to_dir(path) < GIT_SUCCESS)
-		return git_buf_lasterror(path);
-
-	wd_len = path->size;
-	dir = opendir(path->ptr);
-	if (!dir)
-		return git__throw(GIT_EOSERR, "Failed to process `%s` tree structure. An error occured while opening the directory", path->ptr);
-
-	while ((de = readdir(dir)) != NULL) {
-		int result;
-
-		if (is_dot_or_dotdot(de->d_name))
-			continue;
-
-		if (git_buf_puts(path, de->d_name) < GIT_SUCCESS)
-			return git_buf_lasterror(path);
-
-		result = fn(arg, path);
-
-		git_buf_truncate(path, wd_len); /* restore path */
-
-		if (result != GIT_SUCCESS) {
-			closedir(dir);
-			return result;	/* The callee is reponsible for setting the correct error message */
-		}
-	}
-
-	closedir(dir);
-	return GIT_SUCCESS;
-}
-
 int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
 {
 	int error, root_path_offset;
@@ -291,7 +202,7 @@ int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
 		pp += root_path_offset; /* On Windows, will skip the drive name (eg. C: or D:) */
 
 	while (error == GIT_SUCCESS && (sp = strchr(pp, '/')) != NULL) {
-		if (sp != pp && git_futils_isdir(make_path.ptr) < GIT_SUCCESS) {
+		if (sp != pp && git_path_isdir(make_path.ptr) < GIT_SUCCESS) {
 			*sp = 0;
 			error = p_mkdir(make_path.ptr, mode);
 
@@ -324,8 +235,8 @@ static int _rmdir_recurs_foreach(void *opaque, git_buf *path)
 	int error = GIT_SUCCESS;
 	int force = *(int *)opaque;
 
-	if (git_futils_isdir(path->ptr) == GIT_SUCCESS) {
-		error = git_futils_direach(path, _rmdir_recurs_foreach, opaque);
+	if (git_path_isdir(path->ptr) == GIT_SUCCESS) {
+		error = git_path_direach(path, _rmdir_recurs_foreach, opaque);
 		if (error < GIT_SUCCESS)
 			return git__rethrow(error, "Failed to remove directory `%s`", path->ptr);
 		return p_rmdir(path->ptr);
@@ -349,60 +260,6 @@ int git_futils_rmdir_r(const char *path, int force)
 	return error;
 }
 
-int git_futils_cmp_path(const char *name1, int len1, int isdir1,
-		const char *name2, int len2, int isdir2)
-{
-	int len = len1 < len2 ? len1 : len2;
-	int cmp;
-
-	cmp = memcmp(name1, name2, len);
-	if (cmp)
-		return cmp;
-	if (len1 < len2)
-		return ((!isdir1 && !isdir2) ? -1 :
-						(isdir1 ? '/' - name2[len1] : name2[len1] - '/'));
-	if (len1 > len2)
-		return ((!isdir1 && !isdir2) ? 1 :
-						(isdir2 ? name1[len2] - '/' : '/' - name1[len2]));
-	return 0;
-}
-
-static int _check_dir_contents(
-	git_buf *dir,
-	const char *sub,
-	int append_on_success,
-	int (*predicate)(const char *))
-{
-	int error = GIT_SUCCESS;
-	size_t dir_size = dir->size;
-	size_t sub_size = strlen(sub);
-
-	/* leave base valid even if we could not make space for subdir */
-	if ((error = git_buf_try_grow(dir, dir_size + sub_size + 2)) < GIT_SUCCESS)
-		return error;
-
-	/* save excursion */
-	git_buf_joinpath(dir, dir->ptr, sub);
-
-	error = (*predicate)(dir->ptr);
-
-	/* restore excursion */
-	if (!append_on_success || error != GIT_SUCCESS)
-		git_buf_truncate(dir, dir_size);
-
-	return error;
-}
-
-int git_futils_contains_dir(git_buf *base, const char *subdir, int append_if_exists)
-{
-	return _check_dir_contents(base, subdir, append_if_exists, &git_futils_isdir);
-}
-
-int git_futils_contains_file(git_buf *base, const char *file, int append_if_exists)
-{
-	return _check_dir_contents(base, file, append_if_exists, &git_futils_isfile);
-}
-
 int git_futils_find_global_file(git_buf *path, const char *filename)
 {
 	int error;
@@ -420,7 +277,7 @@ int git_futils_find_global_file(git_buf *path, const char *filename)
 	if ((error = git_buf_joinpath(path, home, filename)) < GIT_SUCCESS)
 		return error;
 
-	if (git_futils_exists(path->ptr) < GIT_SUCCESS) {
+	if (git_path_exists(path->ptr) < GIT_SUCCESS) {
 		git_buf_clear(path);
 		return GIT_ENOTFOUND;
 	}
@@ -522,7 +379,7 @@ int git_futils_find_system_file(git_buf *path, const char *filename)
 	if (git_buf_joinpath(path, "/etc", filename) < GIT_SUCCESS)
 		return git_buf_lasterror(path);
 
-	if (git_futils_exists(path->ptr) == GIT_SUCCESS)
+	if (git_path_exists(path->ptr) == GIT_SUCCESS)
 		return GIT_SUCCESS;
 
 	git_buf_clear(path);
@@ -533,29 +390,3 @@ int git_futils_find_system_file(git_buf *path, const char *filename)
 	return GIT_ENOTFOUND;
 #endif
 }
-
-int git_futils_dir_for_path(git_buf *dir, const char *path, const char *base)
-{
-	int error = GIT_SUCCESS;
-
-	if (base != NULL && git_path_root(path) < 0)
-		error = git_buf_joinpath(dir, base, path);
-	else
-		error = git_buf_sets(dir, path);
-
-	if (error == GIT_SUCCESS) {
-		char buf[GIT_PATH_MAX];
-		if (p_realpath(dir->ptr, buf) != NULL)
-			error = git_buf_sets(dir, buf);
-	}
-
-	/* call dirname if this is not a directory */
-	if (error == GIT_SUCCESS && git_futils_isdir(dir->ptr) != GIT_SUCCESS)
-		if (git_path_dirname_r(dir, dir->ptr) < GIT_SUCCESS)
-			error = git_buf_lasterror(dir);
-
-	if (error == GIT_SUCCESS)
-		error = git_path_to_dir(dir);
-
-	return error;
-}
diff --git a/src/fileops.h b/src/fileops.h
index 91903a7..1ded0d3 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -9,7 +9,6 @@
 
 #include "common.h"
 #include "map.h"
-#include "dir.h"
 #include "posix.h"
 #include "path.h"
 
@@ -41,11 +40,6 @@ extern void git_futils_fbuffer_rtrim(git_fbuffer *obj);
  */
 
 /**
- * Check if a file exists and can be accessed.
- */
-extern int git_futils_exists(const char *path);
-
-/**
  * Create and open a file, while also
  * creating all the folders in its path
  */
@@ -63,32 +57,6 @@ 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);
 
 /**
- * Check if the given path points to a directory
- */
-extern int git_futils_isdir(const char *path);
-
-/**
- * Check if the given path points to a regular file
- */
-extern int git_futils_isfile(const char *path);
-
-/**
- * Check if the given path contains the given subdirectory.
- *
- * If `append_if_exists` is true, then the subdir will be appended to the
- * parent path if it does exists.
- */
-extern int git_futils_contains_dir(git_buf *parent, const char *subdir, int append_if_exists);
-
-/**
- * Check if the given path contains the given file
- *
- * If `append_if_exists` is true, then the filename will be appended to the
- * parent path if it does exists.
- */
-extern int git_futils_contains_file(git_buf *parent, const char *file, int append_if_exists);
-
-/**
  * Create a path recursively
  */
 extern int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode);
@@ -99,17 +67,10 @@ extern int git_futils_mkdir_r(const char *path, const char *base, const mode_t m
  */
 extern int git_futils_mkpath2file(const char *path, const mode_t mode);
 
-extern int git_futils_rmdir_r(const char *path, int force);
-
 /**
- * Get the directory for a path.
- *
- * If the path is a directory, this does nothing (save append a '/' as
- * needed).  If path is a normal file, this gets the directory containing
- * it.  If the path does not exist, then this treats it a filename and
- * returns the dirname of it.
+ * Remove path and any files and directories beneath it.
  */
-extern int git_futils_dir_for_path(git_buf *dir, const char *path, const char *base);
+extern int git_futils_rmdir_r(const char *path, int force);
 
 /**
  * Create and open a temporary file with a `_git2_` suffix.
@@ -158,24 +119,6 @@ extern int git_futils_mmap_ro(
 extern void git_futils_mmap_free(git_map *map);
 
 /**
- * Walk each directory entry, except '.' and '..', calling fn(state).
- *
- * @param pathbuf buffer the function reads the initial directory
- * 		path from, and updates with each successive entry's name.
- * @param fn function to invoke with each entry. The first arg is
- *		the input state and the second arg is pathbuf. The function
- *		may modify the pathbuf, but only by appending new text.
- * @param state to pass to fn as the first arg.
- */
-extern int git_futils_direach(
-	git_buf *pathbuf,
-	int (*fn)(void *, git_buf *),
-	void *state);
-
-extern int git_futils_cmp_path(const char *name1, int len1, int isdir1,
-		const char *name2, int len2, int isdir2);
-
-/**
  * Find a "global" file (i.e. one in a user's home directory).
  *
  * @param pathbuf buffer to write the full path into
diff --git a/src/ignore.c b/src/ignore.c
index 1040574..8e0b8a0 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -88,7 +88,7 @@ int git_ignore__for_path(git_repository *repo, const char *path, git_vector *sta
 	if ((error = git_attr_cache__init(repo)) < GIT_SUCCESS)
 		goto cleanup;
 
-	if ((error = git_futils_dir_for_path(&dir, path, workdir)) < GIT_SUCCESS)
+	if ((error = git_path_find_dir(&dir, path, workdir)) < GIT_SUCCESS)
 		goto cleanup;
 
 	/* insert internals */
diff --git a/src/index.c b/src/index.c
index 9e88012..66e7a81 100644
--- a/src/index.c
+++ b/src/index.c
@@ -150,7 +150,7 @@ int git_index_open(git_index **index_out, const char *index_path)
 	git_vector_init(&index->entries, 32, index_cmp);
 
 	/* Check if index file is stored on disk already */
-	if (git_futils_exists(index->index_file_path) == 0)
+	if (git_path_exists(index->index_file_path) == 0)
 		index->on_disk = 1;
 
 	*index_out = index;
@@ -221,7 +221,7 @@ int git_index_read(git_index *index)
 
 	assert(index->index_file_path);
 
-	if (!index->on_disk || git_futils_exists(index->index_file_path) < 0) {
+	if (!index->on_disk || git_path_exists(index->index_file_path) < 0) {
 		git_index_clear(index);
 		index->on_disk = 0;
 		return GIT_SUCCESS;
diff --git a/src/odb.c b/src/odb.c
index b52f870..8905c22 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -354,7 +354,7 @@ static int load_alternates(git_odb *odb, const char *objects_dir)
 	if (error < GIT_SUCCESS)
 		return error;
 
-	if (git_futils_exists(alternates_path.ptr) < GIT_SUCCESS) {
+	if (git_path_exists(alternates_path.ptr) < GIT_SUCCESS) {
 		git_buf_free(&alternates_path);
 		return GIT_SUCCESS;
 	}
diff --git a/src/odb_loose.c b/src/odb_loose.c
index f177af8..d958fce 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -466,7 +466,7 @@ static int locate_object(
 	int error = object_file_name(object_location, backend->objects_dir, oid);
 
 	if (error == GIT_SUCCESS)
-		error = git_futils_exists(git_buf_cstr(object_location));
+		error = git_path_exists(git_buf_cstr(object_location));
 
 	return error;
 }
@@ -480,7 +480,7 @@ static int fn_locate_object_short_oid(void *state, git_buf *pathbuf) {
 		return GIT_SUCCESS;
 	}
 
-	if (!git_futils_exists(pathbuf->ptr) && git_futils_isdir(pathbuf->ptr)) {
+	if (!git_path_exists(pathbuf->ptr) && git_path_isdir(pathbuf->ptr)) {
 		/* We are already in the directory matching the 2 first hex characters,
 		 * compare the first ncmp characters of the oids */
 		if (!memcmp(sstate->short_oid + 2,
@@ -533,8 +533,8 @@ static int locate_object_short_oid(
 		return git__rethrow(error, "Failed to locate object from short oid");
 
 	/* Check that directory exists */
-	if (git_futils_exists(object_location->ptr) ||
-		git_futils_isdir(object_location->ptr))
+	if (git_path_exists(object_location->ptr) ||
+		git_path_isdir(object_location->ptr))
 		return git__throw(GIT_ENOTFOUND, "Failed to locate object from short oid. Object not found");
 
 	state.dir_len = object_location->size;
@@ -542,7 +542,7 @@ static int locate_object_short_oid(
 	state.found = 0;
 
 	/* Explore directory to find a unique object matching short_oid */
-	error = git_futils_direach(object_location, fn_locate_object_short_oid, &state);
+	error = git_path_direach(object_location, fn_locate_object_short_oid, &state);
 	if (error)
 		return git__rethrow(error, "Failed to locate object from short oid");
 
@@ -716,7 +716,7 @@ static int loose_backend__stream_fwrite(git_oid *oid, git_odb_stream *_stream)
 	 * is what git does and allows us to sidestep the fact that
 	 * we're not allowed to overwrite a read-only file on Windows.
 	 */
-	if (git_futils_exists(final_path.ptr) == GIT_SUCCESS) {
+	if (git_path_exists(final_path.ptr) == GIT_SUCCESS) {
 		git_filebuf_cleanup(&stream->fbuf);
 		goto cleanup;
 	}
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 757d627..81168bf 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -254,7 +254,7 @@ static int packfile_refresh_all(struct pack_backend *backend)
 		git_buf_sets(&path, backend->pack_folder);
 
 		/* reload all packs */
-		error = git_futils_direach(&path, packfile_load__cb, (void *)backend);
+		error = git_path_direach(&path, packfile_load__cb, (void *)backend);
 
 		git_buf_free(&path);
 		if (error < GIT_SUCCESS)
@@ -469,7 +469,7 @@ int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
 	if (error < GIT_SUCCESS)
 		goto cleanup;
 
-	if (git_futils_isdir(git_buf_cstr(&path)) == GIT_SUCCESS) {
+	if (git_path_isdir(git_buf_cstr(&path)) == GIT_SUCCESS) {
 		backend->pack_folder = git_buf_detach(&path);
 		backend->pack_folder_mtime = 0;
 	}
diff --git a/src/pack.c b/src/pack.c
index 1510ded..cf64983 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -600,7 +600,7 @@ int git_packfile_check(struct git_pack_file **pack_out, const char *path)
 	memcpy(p->pack_name, path, path_len);
 
 	strcpy(p->pack_name + path_len, ".keep");
-	if (git_futils_exists(p->pack_name) == GIT_SUCCESS)
+	if (git_path_exists(p->pack_name) == GIT_SUCCESS)
 		p->pack_keep = 1;
 
 	strcpy(p->pack_name + path_len, ".pack");
diff --git a/src/path.c b/src/path.c
index 03ebfe0..5319ca6 100644
--- a/src/path.c
+++ b/src/path.c
@@ -7,7 +7,11 @@
 #include "common.h"
 #include "path.h"
 #include "posix.h"
-
+#ifdef GIT_WIN32
+#include "win32/dir.h"
+#else
+#include <dirent.h>
+#endif
 #include <stdarg.h>
 #include <stdio.h>
 #include <ctype.h>
@@ -349,3 +353,172 @@ int git_path_walk_up(
 
 	return error;
 }
+
+int git_path_exists(const char *path)
+{
+	assert(path);
+	return p_access(path, F_OK);
+}
+
+int git_path_isdir(const char *path)
+{
+#ifdef GIT_WIN32
+	DWORD attr = GetFileAttributes(path);
+	if (attr == INVALID_FILE_ATTRIBUTES)
+		return GIT_ERROR;
+
+	return (attr & FILE_ATTRIBUTE_DIRECTORY) ? GIT_SUCCESS : GIT_ERROR;
+
+#else
+	struct stat st;
+	if (p_stat(path, &st) < GIT_SUCCESS)
+		return GIT_ERROR;
+
+	return S_ISDIR(st.st_mode) ? GIT_SUCCESS : GIT_ERROR;
+#endif
+}
+
+int git_path_isfile(const char *path)
+{
+	struct stat st;
+	int stat_error;
+
+	assert(path);
+	stat_error = p_stat(path, &st);
+
+	if (stat_error < GIT_SUCCESS)
+		return -1;
+
+	if (!S_ISREG(st.st_mode))
+		return -1;
+
+	return 0;
+}
+
+static int _check_dir_contents(
+	git_buf *dir,
+	const char *sub,
+	int append_on_success,
+	int (*predicate)(const char *))
+{
+	int error = GIT_SUCCESS;
+	size_t dir_size = dir->size;
+	size_t sub_size = strlen(sub);
+
+	/* leave base valid even if we could not make space for subdir */
+	if ((error = git_buf_try_grow(dir, dir_size + sub_size + 2)) < GIT_SUCCESS)
+		return error;
+
+	/* save excursion */
+	git_buf_joinpath(dir, dir->ptr, sub);
+
+	error = (*predicate)(dir->ptr);
+
+	/* restore excursion */
+	if (!append_on_success || error != GIT_SUCCESS)
+		git_buf_truncate(dir, dir_size);
+
+	return error;
+}
+
+int git_path_contains_dir(git_buf *base, const char *subdir, int append_if_exists)
+{
+	return _check_dir_contents(base, subdir, append_if_exists, &git_path_isdir);
+}
+
+int git_path_contains_file(git_buf *base, const char *file, int append_if_exists)
+{
+	return _check_dir_contents(base, file, append_if_exists, &git_path_isfile);
+}
+
+int git_path_find_dir(git_buf *dir, const char *path, const char *base)
+{
+	int error = GIT_SUCCESS;
+
+	if (base != NULL && git_path_root(path) < 0)
+		error = git_buf_joinpath(dir, base, path);
+	else
+		error = git_buf_sets(dir, path);
+
+	if (error == GIT_SUCCESS) {
+		char buf[GIT_PATH_MAX];
+		if (p_realpath(dir->ptr, buf) != NULL)
+			error = git_buf_sets(dir, buf);
+	}
+
+	/* call dirname if this is not a directory */
+	if (error == GIT_SUCCESS && git_path_isdir(dir->ptr) != GIT_SUCCESS)
+		if (git_path_dirname_r(dir, dir->ptr) < GIT_SUCCESS)
+			error = git_buf_lasterror(dir);
+
+	if (error == GIT_SUCCESS)
+		error = git_path_to_dir(dir);
+
+	return error;
+}
+
+int git_path_cmp(const char *name1, int len1, int isdir1,
+		const char *name2, int len2, int isdir2)
+{
+	int len = len1 < len2 ? len1 : len2;
+	int cmp;
+
+	cmp = memcmp(name1, name2, len);
+	if (cmp)
+		return cmp;
+	if (len1 < len2)
+		return ((!isdir1 && !isdir2) ? -1 :
+						(isdir1 ? '/' - name2[len1] : name2[len1] - '/'));
+	if (len1 > len2)
+		return ((!isdir1 && !isdir2) ? 1 :
+						(isdir2 ? name1[len2] - '/' : '/' - name1[len2]));
+	return 0;
+}
+
+/* Taken from git.git */
+GIT_INLINE(int) is_dot_or_dotdot(const char *name)
+{
+	return (name[0] == '.' &&
+		(name[1] == '\0' ||
+		 (name[1] == '.' && name[2] == '\0')));
+}
+
+int git_path_direach(
+	git_buf *path,
+	int (*fn)(void *, git_buf *),
+	void *arg)
+{
+	ssize_t wd_len;
+	DIR *dir;
+	struct dirent *de;
+
+	if (git_path_to_dir(path) < GIT_SUCCESS)
+		return git_buf_lasterror(path);
+
+	wd_len = path->size;
+	dir = opendir(path->ptr);
+	if (!dir)
+		return git__throw(GIT_EOSERR, "Failed to process `%s` tree structure. An error occured while opening the directory", path->ptr);
+
+	while ((de = readdir(dir)) != NULL) {
+		int result;
+
+		if (is_dot_or_dotdot(de->d_name))
+			continue;
+
+		if (git_buf_puts(path, de->d_name) < GIT_SUCCESS)
+			return git_buf_lasterror(path);
+
+		result = fn(arg, path);
+
+		git_buf_truncate(path, wd_len); /* restore path */
+
+		if (result != GIT_SUCCESS) {
+			closedir(dir);
+			return result;	/* The callee is reponsible for setting the correct error message */
+		}
+	}
+
+	closedir(dir);
+	return GIT_SUCCESS;
+}
diff --git a/src/path.h b/src/path.h
index e59c19a..ee3607c 100644
--- a/src/path.h
+++ b/src/path.h
@@ -10,6 +10,13 @@
 #include "common.h"
 #include "buffer.h"
 
+/**
+ * Path manipulation utils
+ *
+ * These are path utilities that munge paths without actually
+ * looking at the real filesystem.
+ */
+
 /*
  * The dirname() function shall take a pointer to a character string
  * that contains a pathname, and return a pointer to a string that is a
@@ -52,15 +59,30 @@ extern int git_path_basename_r(git_buf *buffer, const char *path);
 
 extern const char *git_path_topdir(const char *path);
 
+/**
+ * Find offset to root of path if path has one.
+ *
+ * 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.
+ */
 extern int git_path_root(const char *path);
 
-extern int git_path_prettify(git_buf *path_out, const char *path, const char *base);
-extern int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base);
-
+/**
+ * Ensure path has a trailing '/'.
+ */
 extern int git_path_to_dir(git_buf *path);
+
+/**
+ * Ensure string has a trailing '/' if there is space for it.
+ */
 extern void git_path_string_to_dir(char* path, size_t size);
 
 #ifdef GIT_WIN32
+/**
+ * Convert backslashes in path to forward slashes.
+ */
 GIT_INLINE(void) git_path_mkposix(char *path)
 {
 	while (*path) {
@@ -75,20 +97,123 @@ GIT_INLINE(void) git_path_mkposix(char *path)
 #endif
 
 extern int git__percent_decode(git_buf *decoded_out, const char *input);
+
+/**
+ * Extract path from file:// URL.
+ */
 extern int git_path_fromurl(git_buf *local_path_out, const char *file_url);
 
+
+/**
+ * Path filesystem utils
+ *
+ * These are path utilities that actually access the filesystem.
+ */
+
+/**
+ * Check if a file exists and can be accessed.
+ * @return GIT_SUCCESS if file exists, < 0 otherwise.
+ */
+extern int git_path_exists(const char *path);
+
+/**
+ * Check if the given path points to a directory.
+ * @return GIT_SUCCESS if it is a directory, < 0 otherwise.
+ */
+extern int git_path_isdir(const char *path);
+
 /**
- * Invoke callback directory by directory up the path until the ceiling
- * is reached (inclusive of a final call at the root_path).
+ * Check if the given path points to a regular file.
+ * @return GIT_SUCCESS if it is a regular file, < 0 otherwise.
+ */
+extern int git_path_isfile(const char *path);
+
+/**
+ * Check if the given path contains the given subdirectory.
+ *
+ * @param parent Directory path that might contain subdir
+ * @param subdir Subdirectory name to look for in parent
+ * @param append_if_exists If true, then subdir will be appended to the parent path if it does exist
+ * @return GIT_SUCCESS if subdirectory exists, < 0 otherwise.
+ */
+extern int git_path_contains_dir(git_buf *parent, const char *subdir, int append_if_exists);
+
+/**
+ * Check if the given path contains the given file.
+ *
+ * @param dir Directory path that might contain file
+ * @param file File name to look for in parent
+ * @param append_if_exists If true, then file will be appended to the path if it does exist
+ * @return GIT_SUCCESS if file exists, < 0 otherwise.
+ */
+extern int git_path_contains_file(git_buf *dir, const char *file, int append_if_exists);
+
+/**
+ * Clean up path, prepending base if it is not already rooted.
+ */
+extern int git_path_prettify(git_buf *path_out, const char *path, const char *base);
+
+/**
+ * Clean up path, prepending base if it is not already rooted and
+ * appending a slash.
+ */
+extern int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base);
+
+/**
+ * Get a directory from a path.
+ *
+ * If path is a directory, this acts like `git_path_prettify_dir`
+ * (cleaning up path and appending a '/').  If path is a normal file,
+ * this prettifies it, then removed the filename a la dirname and
+ * appends the trailing '/'.  If the path does not exist, it is
+ * treated like a regular filename.
+ */
+extern int git_path_find_dir(git_buf *dir, const char *path, const char *base);
+
+/**
+ * Walk each directory entry, except '.' and '..', calling fn(state).
+ *
+ * @param pathbuf buffer the function reads the initial directory
+ * 		path from, and updates with each successive entry's name.
+ * @param fn function to invoke with each entry. The first arg is
+ *		the input state and the second arg is pathbuf. The function
+ *		may modify the pathbuf, but only by appending new text.
+ * @param state to pass to fn as the first arg.
+ */
+extern int git_path_direach(
+	git_buf *pathbuf,
+	int (*fn)(void *, git_buf *),
+	void *state);
+
+/**
+ * Sort function to order two paths.
+ */
+extern int git_path_cmp(
+	const char *name1, int len1, int isdir1,
+	const char *name2, int len2, int isdir2);
+
+/**
+ * Invoke callback up path directory by directory until the ceiling is
+ * reached (inclusive of a final call at the root_path).
+ *
+ * Returning anything other than GIT_SUCCESS from the callback function
+ * will stop the iteration and propogate the error to the caller.
  *
- * If the ceiling is NULL, this will walk all the way up to the root.
- * If the ceiling is not a prefix of the path, the callback will be
- * invoked a single time on the verbatim input path.  Returning anything
- * other than GIT_SUCCESS from the callback function will stop the
- * iteration and propogate the error to the caller.
+ * @param pathbuf Buffer the function reads the directory from and
+ *		and updates with each successive name.
+ * @param ceiling Prefix of path at which to stop walking up.  If NULL,
+ *      this will walk all the way up to the root.  If not a prefix of
+ *      pathbuf, the callback will be invoked a single time on the
+ *      original input path.
+ * @param fn Function to invoke on each path.  The first arg is the
+ *		input satte and the second arg is the pathbuf.  The function
+ *		should not modify the pathbuf.
+ * @param state Passed to fn as the first ath.
  */
 extern int git_path_walk_up(
-	git_buf *path, const char *ceiling,
-	int (*cb)(void *data, git_buf *), void *data);
+	git_buf *pathbuf,
+	const char *ceiling,
+	int (*fn)(void *state, git_buf *),
+	void *state);
 
 #endif
diff --git a/src/reflog.c b/src/reflog.c
index a327975..970e7c2 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -246,12 +246,12 @@ int git_reflog_write(git_reference *ref, const git_oid *oid_old,
 	if (error < GIT_SUCCESS)
 		goto cleanup;
 
-	if (git_futils_exists(log_path.ptr)) {
+	if (git_path_exists(log_path.ptr)) {
 		error = git_futils_mkpath2file(log_path.ptr, GIT_REFLOG_DIR_MODE);
 		if (error < GIT_SUCCESS)
 			git__rethrow(error,
 				"Failed to write reflog. Cannot create reflog directory");
-	} else if (git_futils_isfile(log_path.ptr)) {
+	} else if (git_path_isfile(log_path.ptr)) {
 		error = git__throw(GIT_ERROR,
 			"Failed to write reflog. `%s` is directory", log_path.ptr);
 	} else if (oid_old == NULL) {
@@ -302,7 +302,7 @@ int git_reflog_delete(git_reference *ref)
 	error = git_buf_join_n(&path, '/', 3,
 		ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
 
-	if (error == GIT_SUCCESS && git_futils_exists(path.ptr) == 0)
+	if (error == GIT_SUCCESS && git_path_exists(path.ptr) == 0)
 		error = p_unlink(path.ptr);
 
 	git_buf_free(&path);
diff --git a/src/refs.c b/src/refs.c
index 2842ada..340841c 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -525,8 +525,8 @@ static int _dirent_loose_listall(void *_data, git_buf *full_path)
 	struct dirent_list_data *data = (struct dirent_list_data *)_data;
 	const char *file_path = full_path->ptr + data->repo_path_len;
 
-	if (git_futils_isdir(full_path->ptr) == GIT_SUCCESS)
-		return git_futils_direach(full_path, _dirent_loose_listall, _data);
+	if (git_path_isdir(full_path->ptr) == GIT_SUCCESS)
+		return git_path_direach(full_path, _dirent_loose_listall, _data);
 
 	/* do not add twice a reference that exists already in the packfile */
 	if ((data->list_flags & GIT_REF_PACKED) != 0 &&
@@ -549,8 +549,8 @@ static int _dirent_loose_load(void *data, git_buf *full_path)
 	const char *file_path;
 	int error;
 
-	if (git_futils_isdir(full_path->ptr) == GIT_SUCCESS)
-		return git_futils_direach(full_path, _dirent_loose_load, repository);
+	if (git_path_isdir(full_path->ptr) == GIT_SUCCESS)
+		return git_path_direach(full_path, _dirent_loose_load, repository);
 
 	file_path = full_path->ptr + strlen(repository->path_repository);
 	error = loose_lookup_to_packfile(&ref, repository, file_path);
@@ -596,7 +596,7 @@ static int packed_loadloose(git_repository *repository)
 	 * This will overwrite any old packed entries with their
 	 * updated loose versions
 	 */
-	error = git_futils_direach(&refs_path, _dirent_loose_load, repository);
+	error = git_path_direach(&refs_path, _dirent_loose_load, repository);
 	git_buf_free(&refs_path);
 	return error;
 }
@@ -719,7 +719,7 @@ static int packed_remove_loose(git_repository *repo, git_vector *packing_list)
 		an_error = git_buf_joinpath(&full_path, repo->path_repository, ref->name);
 
 		if (an_error == GIT_SUCCESS &&
-			git_futils_exists(full_path.ptr) == GIT_SUCCESS &&
+			git_path_exists(full_path.ptr) == GIT_SUCCESS &&
 			p_unlink(full_path.ptr) < GIT_SUCCESS)
 			an_error = GIT_EOSERR;
 
@@ -902,7 +902,7 @@ static int reference_exists(int *exists, git_repository *repo, const char *ref_n
 	if (error < GIT_SUCCESS)
 		return git__rethrow(error, "Cannot resolve if a reference exists");
 
-	if (git_futils_isfile(ref_path.ptr) == GIT_SUCCESS ||
+	if (git_path_isfile(ref_path.ptr) == GIT_SUCCESS ||
 		git_hashtable_lookup(repo->references.packfile, ref_path.ptr) != NULL) {
 		*exists = 1;
 	} else {
@@ -1352,8 +1352,8 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
 	if ((error = reference_delete(ref)) < GIT_SUCCESS)
 		goto cleanup;
 
-	if (git_futils_exists(aux_path.ptr) == GIT_SUCCESS) {
-		if (git_futils_isdir(aux_path.ptr) == GIT_SUCCESS) {
+	if (git_path_exists(aux_path.ptr) == GIT_SUCCESS) {
+		if (git_path_isdir(aux_path.ptr) == GIT_SUCCESS) {
 			if ((error = git_futils_rmdir_r(aux_path.ptr, 0)) < GIT_SUCCESS)
 				goto rollback;
 		} else goto rollback;
@@ -1398,7 +1398,7 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
 	if (error < GIT_SUCCESS)
 		goto cleanup;
 
-	if (git_futils_exists(aux_path.ptr) == GIT_SUCCESS)
+	if (git_path_exists(aux_path.ptr) == GIT_SUCCESS)
 		error = git_reflog_rename(ref, new_name);
 
 	/*
@@ -1536,7 +1536,7 @@ int git_reference_foreach(
 		repo->path_repository, GIT_REFS_DIR)) < GIT_SUCCESS)
 		return git__rethrow(error, "Failed to alloc space for references");
 
-	error = git_futils_direach(&refs_path, _dirent_loose_listall, &data);
+	error = git_path_direach(&refs_path, _dirent_loose_listall, &data);
 
 	git_buf_free(&refs_path);
 
diff --git a/src/repository.c b/src/repository.c
index 67bfcc9..97d70c4 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -79,14 +79,14 @@ void git_repository_free(git_repository *repo)
 static int quickcheck_repository_dir(git_buf *repository_path)
 {
 	/* Check OBJECTS_DIR first, since it will generate the longest path name */
-	if (git_futils_contains_dir(repository_path, GIT_OBJECTS_DIR, 0) < 0)
+	if (git_path_contains_dir(repository_path, GIT_OBJECTS_DIR, 0) < 0)
 		return GIT_ERROR;
 
 	/* Ensure HEAD file exists */
-	if (git_futils_contains_file(repository_path, GIT_HEAD_FILE, 0) < 0)
+	if (git_path_contains_file(repository_path, GIT_HEAD_FILE, 0) < 0)
 		return GIT_ERROR;
 
-	if (git_futils_contains_dir(repository_path, GIT_REFS_DIR, 0) < 0)
+	if (git_path_contains_dir(repository_path, GIT_REFS_DIR, 0) < 0)
 		return GIT_ERROR;
 
 	return GIT_SUCCESS;
@@ -164,7 +164,7 @@ int git_repository_open(git_repository **repo_out, const char *path)
 	 * of the working dir, by testing if it contains a `.git`
 	 * folder inside of it.
 	 */
-	git_futils_contains_dir(&path_buf, DOT_GIT, 1); /* append on success */
+	git_path_contains_dir(&path_buf, DOT_GIT, 1); /* append on success */
 	/* ignore error, since it just means `path/.git` doesn't exist */
 
 	if (quickcheck_repository_dir(&path_buf) < GIT_SUCCESS) {
@@ -491,7 +491,7 @@ static int read_gitfile(git_buf *path_out, const char *file_path, const char *ba
 
 	git_futils_freebuffer(&file);
 
-	if (error == GIT_SUCCESS && git_futils_exists(path_out->ptr) == 0)
+	if (error == GIT_SUCCESS && git_path_exists(path_out->ptr) == 0)
 		return GIT_SUCCESS;
 
 	return git__throw(GIT_EOBJCORRUPTED, "The `.git` file points to a nonexistent path");
@@ -535,7 +535,7 @@ int git_repository_discover(
 		 * If the `.git` file is regular instead of
 		 * a directory, it should contain the path of the actual git repository
 		 */
-		if (git_futils_isfile(normal_path.ptr) == GIT_SUCCESS) {
+		if (git_path_isfile(normal_path.ptr) == GIT_SUCCESS) {
 			git_buf gitfile_path = GIT_BUF_INIT;
 
 			error = read_gitfile(&gitfile_path, normal_path.ptr, bare_path.ptr);
@@ -557,7 +557,7 @@ int git_repository_discover(
 		/**
 		 * If the `.git` file is a folder, we check inside of it
 		 */
-		if (git_futils_isdir(normal_path.ptr) == GIT_SUCCESS) {
+		if (git_path_isdir(normal_path.ptr) == GIT_SUCCESS) {
 			error = quickcheck_repository_dir(&normal_path);
 			if (error == GIT_SUCCESS) {
 				found_path = &normal_path;
@@ -733,7 +733,7 @@ int git_repository_init(git_repository **repo_out, const char *path, unsigned is
 	if (error < GIT_SUCCESS)
 		return error;
 
-	if (git_futils_isdir(repository_path.ptr) == GIT_SUCCESS) {
+	if (git_path_isdir(repository_path.ptr) == GIT_SUCCESS) {
 		if (quickcheck_repository_dir(&repository_path) == GIT_SUCCESS) {
 			error = repo_init_reinit(repository_path.ptr, is_bare);
 			git_buf_free(&repository_path);
diff --git a/src/status.c b/src/status.c
index 3ead15a..492edf5 100644
--- a/src/status.c
+++ b/src/status.c
@@ -489,7 +489,7 @@ int git_status_foreach(
 	dirent_st.index_position = 0;
 	dirent_st.is_dir = 1;
 
-	if (git_futils_isdir(workdir)) {
+	if (git_path_isdir(workdir)) {
 		error = git__throw(GIT_EINVALIDPATH,
 			"Failed to determine status of file '%s'. "
 			"The given path doesn't lead to a folder", workdir);
@@ -592,7 +592,7 @@ int git_status_file(unsigned int *status_flags, git_repository *repo, const char
 		return git__rethrow(error,
 			"Failed to determine status of file '%s'", path);
 
-	if (git_futils_isdir(temp_path.ptr) == GIT_SUCCESS) {
+	if (git_path_isdir(temp_path.ptr) == GIT_SUCCESS) {
 		git_buf_free(&temp_path);
 		return git__throw(GIT_EINVALIDPATH,
 			"Failed to determine status of file '%s'. "
@@ -606,7 +606,7 @@ int git_status_file(unsigned int *status_flags, git_repository *repo, const char
 	}
 
 	/* Find file in Workdir */
-	if (git_futils_exists(temp_path.ptr) == GIT_SUCCESS) {
+	if (git_path_exists(temp_path.ptr) == GIT_SUCCESS) {
 		if ((error = status_entry_update_from_workdir(e, temp_path.ptr)) < GIT_SUCCESS)
 			goto cleanup;	/* The callee has already set the error message */
 	}
@@ -672,8 +672,8 @@ cleanup:
 }
 
 /*
- * git_futils_direach is not supposed to return entries in an ordered manner.
- * alphasorted_futils_direach wraps git_futils_direach and invokes the callback
+ * git_path_direach is not supposed to return entries in an ordered manner.
+ * alphasorted_futils_direach wraps git_path_direach and invokes the callback
  * function by passing it alphabeticcally sorted paths parameters.
  *
  */
@@ -686,7 +686,7 @@ static char *alphasorted_dirent_info_new(const git_buf *path)
 
 	git_buf_copy_cstr(di, path->size + 1, path);
 
-	if (git_futils_isdir(path->ptr) == GIT_SUCCESS) {
+	if (git_path_isdir(path->ptr) == GIT_SUCCESS) {
 		/*
 		 * Append a forward slash to the name to force folders
 		 * to be ordered in a similar way than in a tree
@@ -734,7 +734,7 @@ static int alphasorted_futils_direach(
 	if (git_vector_init(&entry_names, 16, git__strcmp_cb) < GIT_SUCCESS)
 		return GIT_ENOMEM;
 
-	error = git_futils_direach(path, alphasorted_dirent_cb, &entry_names);
+	error = git_path_direach(path, alphasorted_dirent_cb, &entry_names);
 
 	git_vector_sort(&entry_names);
 
diff --git a/src/tree.c b/src/tree.c
index 8bc17d9..f214902 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -30,7 +30,7 @@ static int entry_sort_cmp(const void *a, const void *b)
 	const git_tree_entry *entry_a = (const git_tree_entry *)(a);
 	const git_tree_entry *entry_b = (const git_tree_entry *)(b);
 
-	return git_futils_cmp_path(
+	return git_path_cmp(
 		entry_a->filename, entry_a->filename_len, entry_is_tree(entry_a),
 		entry_b->filename, entry_b->filename_len, entry_is_tree(entry_b));
 }
diff --git a/src/win32/dir.h b/src/win32/dir.h
new file mode 100644
index 0000000..b16a3cf
--- /dev/null
+++ b/src/win32/dir.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2009-2011 the libgit2 contributors
+ *
+ * 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_dir_h__
+#define INCLUDE_dir_h__
+
+#include "common.h"
+
+struct git__dirent {
+	int d_ino;
+	char d_name[261];
+};
+
+typedef struct {
+	HANDLE h;
+	WIN32_FIND_DATAW f;
+	struct git__dirent entry;
+	char *dir;
+	int first;
+} git__DIR;
+
+extern git__DIR *git__opendir(const char *);
+extern struct git__dirent *git__readdir(git__DIR *);
+extern void git__rewinddir(git__DIR *);
+extern int git__closedir(git__DIR *);
+
+# ifndef GIT__WIN32_NO_WRAP_DIR
+#	define dirent git__dirent
+#	define DIR git__DIR
+#	define opendir	git__opendir
+#	define readdir	git__readdir
+#	define rewinddir git__rewinddir
+#	define closedir git__closedir
+# endif
+
+#endif /* INCLUDE_dir_h__ */
diff --git a/tests-clay/config/stress.c b/tests-clay/config/stress.c
index b48ed39..3d3729c 100644
--- a/tests-clay/config/stress.c
+++ b/tests-clay/config/stress.c
@@ -27,7 +27,7 @@ void test_config_stress__dont_break_on_invalid_input(void)
 	struct git_config_file *file;
 	git_config *config;
 
-	cl_git_pass(git_futils_exists("git-test-config"));
+	cl_git_pass(git_path_exists("git-test-config"));
 	cl_git_pass(git_config_file__ondisk(&file, "git-test-config"));
 	cl_git_pass(git_config_new(&config));
 	cl_git_pass(git_config_add_file(config, file, 0));
diff --git a/tests-clay/core/dirent.c b/tests-clay/core/dirent.c
index 4f55368..c9ab1c1 100644
--- a/tests-clay/core/dirent.c
+++ b/tests-clay/core/dirent.c
@@ -115,7 +115,7 @@ void test_core_dirent__dont_traverse_dot(void)
 	cl_set_cleanup(&dirent_cleanup__cb, &dot);
 	setup(&dot);
 
-	cl_git_pass(git_futils_direach(&dot.path,
+	cl_git_pass(git_path_direach(&dot.path,
 					one_entry,
 					&dot));
 
@@ -141,7 +141,7 @@ void test_core_dirent__traverse_subfolder(void)
 	cl_set_cleanup(&dirent_cleanup__cb, &sub);
 	setup(&sub);
 
-	cl_git_pass(git_futils_direach(&sub.path,
+	cl_git_pass(git_path_direach(&sub.path,
 					one_entry,
 					&sub));
 
@@ -161,7 +161,7 @@ void test_core_dirent__traverse_slash_terminated_folder(void)
 	cl_set_cleanup(&dirent_cleanup__cb, &sub_slash);
 	setup(&sub_slash);
 
-	cl_git_pass(git_futils_direach(&sub_slash.path,
+	cl_git_pass(git_path_direach(&sub_slash.path,
 					one_entry,
 					&sub_slash));
 
@@ -184,14 +184,14 @@ void test_core_dirent__dont_traverse_empty_folders(void)
 	cl_set_cleanup(&dirent_cleanup__cb, &empty);
 	setup(&empty);
 
-	cl_git_pass(git_futils_direach(&empty.path,
+	cl_git_pass(git_path_direach(&empty.path,
 					one_entry,
 					&empty));
 
 	check_counts(&empty);
 
 	/* make sure callback not called */
-	cl_git_pass(git_futils_direach(&empty.path,
+	cl_git_pass(git_path_direach(&empty.path,
 					dont_call_me,
 					&empty));
 }
@@ -216,7 +216,7 @@ void test_core_dirent__traverse_weird_filenames(void)
 	cl_set_cleanup(&dirent_cleanup__cb, &odd);
 	setup(&odd);
 
-	cl_git_pass(git_futils_direach(&odd.path,
+	cl_git_pass(git_path_direach(&odd.path,
 					one_entry,
 					&odd));
 
diff --git a/tests-clay/core/filebuf.c b/tests-clay/core/filebuf.c
index 5b233fe..6a87902 100644
--- a/tests-clay/core/filebuf.c
+++ b/tests-clay/core/filebuf.c
@@ -14,7 +14,7 @@ void test_core_filebuf__0(void)
 	cl_must_pass(p_close(fd));
 
 	cl_git_fail(git_filebuf_open(&file, test, 0));
-	cl_git_pass(git_futils_exists(testlock));
+	cl_git_pass(git_path_exists(testlock));
 
 	cl_must_pass(p_unlink(testlock));
 }
diff --git a/tests/t00-core.c b/tests/t00-core.c
index 708a889..58f048a 100644
--- a/tests/t00-core.c
+++ b/tests/t00-core.c
@@ -362,7 +362,7 @@ static walk_data dot = {
 BEGIN_TEST(dirent0, "make sure that the '.' folder is not traversed")
 	must_pass(setup(&dot));
 
-	must_pass(git_futils_direach(&dot.path,
+	must_pass(git_path_direach(&dot.path,
 			       one_entry,
 			       &dot));
 
@@ -387,7 +387,7 @@ BEGIN_TEST(dirent1, "traverse a subfolder")
 
 	must_pass(setup(&sub));
 
-	must_pass(git_futils_direach(&sub.path,
+	must_pass(git_path_direach(&sub.path,
 			       one_entry,
 			       &sub));
 
@@ -406,7 +406,7 @@ BEGIN_TEST(dirent2, "traverse a slash-terminated subfolder")
 
 	must_pass(setup(&sub_slash));
 
-	must_pass(git_futils_direach(&sub_slash.path,
+	must_pass(git_path_direach(&sub_slash.path,
 			       one_entry,
 			       &sub_slash));
 
@@ -435,14 +435,14 @@ BEGIN_TEST(dirent3, "make sure that empty folders are not traversed")
 
 	must_pass(setup(&empty));
 
-	must_pass(git_futils_direach(&empty.path,
+	must_pass(git_path_direach(&empty.path,
 			       one_entry,
 			       &empty));
 
 	must_pass(check_counts(&empty));
 
 	/* make sure callback not called */
-	must_pass(git_futils_direach(&empty.path,
+	must_pass(git_path_direach(&empty.path,
 			       dont_call_me,
 			       &empty));
 
@@ -467,7 +467,7 @@ BEGIN_TEST(dirent4, "make sure that strange looking filenames ('..c') are traver
 
 	must_pass(setup(&odd));
 
-	must_pass(git_futils_direach(&odd.path,
+	must_pass(git_path_direach(&odd.path,
 			       one_entry,
 			       &odd));
 
@@ -485,7 +485,7 @@ BEGIN_TEST(filebuf0, "make sure git_filebuf_open doesn't delete an existing lock
 	must_pass(fd);
 	must_pass(p_close(fd));
 	must_fail(git_filebuf_open(&file, test, 0));
-	must_pass(git_futils_exists(testlock));
+	must_pass(git_path_exists(testlock));
 	must_pass(p_unlink(testlock));
 END_TEST
 
diff --git a/tests/t03-objwrite.c b/tests/t03-objwrite.c
index 1fc0cac..1650b80 100644
--- a/tests/t03-objwrite.c
+++ b/tests/t03-objwrite.c
@@ -44,9 +44,9 @@ static int make_odb_dir(void)
 
 static int check_object_files(object_data *d)
 {
-	if (git_futils_exists(d->dir) < 0)
+	if (git_path_exists(d->dir) < 0)
 		return -1;
-	if (git_futils_exists(d->file) < 0)
+	if (git_path_exists(d->file) < 0)
 		return -1;
 	return 0;
 }
diff --git a/tests/t10-refs.c b/tests/t10-refs.c
index e8c7b7e..63d1cb7 100644
--- a/tests/t10-refs.c
+++ b/tests/t10-refs.c
@@ -530,7 +530,7 @@ BEGIN_TEST(pack1, "create a packfile from all the loose rn a repo")
 
 	/* Ensure the packed-refs file exists */
 	must_pass(git_buf_joinpath(&temp_path, repo->path_repository, GIT_PACKEDREFS_FILE));
-	must_pass(git_futils_exists(temp_path.ptr));
+	must_pass(git_path_exists(temp_path.ptr));
 
 	/* Ensure the known ref can still be looked up but is now packed */
 	must_pass(git_reference_lookup(&reference, repo, loose_tag_ref_name));
@@ -539,7 +539,7 @@ BEGIN_TEST(pack1, "create a packfile from all the loose rn a repo")
 
 	/* Ensure the known ref has been removed from the loose folder structure */
 	must_pass(git_buf_joinpath(&temp_path, repo->path_repository, loose_tag_ref_name));
-	must_pass(!git_futils_exists(temp_path.ptr));
+	must_pass(!git_path_exists(temp_path.ptr));
 
 	close_temp_repo(repo);
 
@@ -557,7 +557,7 @@ BEGIN_TEST(rename0, "rename a loose reference")
 
 	/* Ensure the ref doesn't exist on the file system */
 	must_pass(git_buf_joinpath(&temp_path, repo->path_repository, new_name));
-	must_pass(!git_futils_exists(temp_path.ptr));
+	must_pass(!git_path_exists(temp_path.ptr));
 
 	/* Retrieval of the reference to rename */
 	must_pass(git_reference_lookup(&looked_up_ref, repo, loose_tag_ref_name));
@@ -582,7 +582,7 @@ BEGIN_TEST(rename0, "rename a loose reference")
 
 	/* ...and the ref can be found in the file system */
 	must_pass(git_buf_joinpath(&temp_path, repo->path_repository, new_name));
-	must_pass(git_futils_exists(temp_path.ptr));
+	must_pass(git_path_exists(temp_path.ptr));
 
 	close_temp_repo(repo);
 
@@ -601,7 +601,7 @@ BEGIN_TEST(rename1, "rename a packed reference (should make it loose)")
 
 	/* Ensure the ref doesn't exist on the file system */
 	must_pass(git_buf_joinpath(&temp_path, repo->path_repository, packed_head_name));
-	must_pass(!git_futils_exists(temp_path.ptr));
+	must_pass(!git_path_exists(temp_path.ptr));
 
 	/* The reference can however be looked-up... */
 	must_pass(git_reference_lookup(&looked_up_ref, repo, packed_head_name));
@@ -626,7 +626,7 @@ BEGIN_TEST(rename1, "rename a packed reference (should make it loose)")
 
 	/* ...and the ref now happily lives in the file system */
 	must_pass(git_buf_joinpath(&temp_path, repo->path_repository, brand_new_name));
-	must_pass(git_futils_exists(temp_path.ptr));
+	must_pass(git_path_exists(temp_path.ptr));
 
 	close_temp_repo(repo);
 
@@ -645,7 +645,7 @@ BEGIN_TEST(rename2, "renaming a packed reference does not pack another reference
 
 	/* Ensure the other reference exists on the file system */
 	must_pass(git_buf_joinpath(&temp_path, repo->path_repository, packed_test_head_name));
-	must_pass(git_futils_exists(temp_path.ptr));
+	must_pass(git_path_exists(temp_path.ptr));
 
 	/* Lookup the other reference */
 	must_pass(git_reference_lookup(&another_looked_up_ref, repo, packed_test_head_name));
@@ -670,7 +670,7 @@ BEGIN_TEST(rename2, "renaming a packed reference does not pack another reference
 	must_be_true(git_reference_is_packed(another_looked_up_ref) == 0);
 
 	/* Ensure the other ref still exists on the file system */
-	must_pass(git_futils_exists(temp_path.ptr));
+	must_pass(git_path_exists(temp_path.ptr));
 
 	close_temp_repo(repo);
 
@@ -899,7 +899,7 @@ BEGIN_TEST(delete0, "deleting a ref which is both packed and loose should remove
 
 	/* Ensure the loose reference exists on the file system */
 	must_pass(git_buf_joinpath(&temp_path, repo->path_repository, packed_test_head_name));
-	must_pass(git_futils_exists(temp_path.ptr));
+	must_pass(git_path_exists(temp_path.ptr));
 
 	/* Lookup the reference */
 	must_pass(git_reference_lookup(&looked_up_ref, repo, packed_test_head_name));
@@ -914,7 +914,7 @@ BEGIN_TEST(delete0, "deleting a ref which is both packed and loose should remove
 	must_fail(git_reference_lookup(&another_looked_up_ref, repo, packed_test_head_name));
 
 	/* Ensure the loose reference doesn't exist any longer on the file system */
-	must_pass(!git_futils_exists(temp_path.ptr));
+	must_pass(!git_path_exists(temp_path.ptr));
 
 	close_temp_repo(repo);
 
diff --git a/tests/t12-repo.c b/tests/t12-repo.c
index 0b24565..6a080ec 100644
--- a/tests/t12-repo.c
+++ b/tests/t12-repo.c
@@ -68,7 +68,7 @@ static int write_file(const char *path, const char *content)
 	int error;
 	git_file file;
 
-	if (git_futils_exists(path) == GIT_SUCCESS) {
+	if (git_path_exists(path) == GIT_SUCCESS) {
 		error = p_unlink(path);
 
 		if (error < GIT_SUCCESS)
diff --git a/tests/test_helpers.c b/tests/test_helpers.c
index 40b3499..42c8031 100644
--- a/tests/test_helpers.c
+++ b/tests/test_helpers.c
@@ -238,8 +238,8 @@ static int copy_filesystem_element_recurs(void *_data, git_buf *source)
 	git_buf_truncate(&data->dst, data->dst_baselen);
 	git_buf_puts(&data->dst, source->ptr + data->src_baselen);
 
-	if (git_futils_isdir(source->ptr) == GIT_SUCCESS)
-		return git_futils_direach(source, copy_filesystem_element_recurs, _data);
+	if (git_path_isdir(source->ptr) == GIT_SUCCESS)
+		return git_path_direach(source, copy_filesystem_element_recurs, _data);
 	else
 		return copy_file(source->ptr, data->dst.ptr);
 }
@@ -252,8 +252,8 @@ int copydir_recurs(
 	copydir_data data = { GIT_BUF_INIT, 0, GIT_BUF_INIT, 0 };
 
 	/* Source has to exist, Destination hast to _not_ exist */
-	if (git_futils_isdir(source_directory_path) != GIT_SUCCESS ||
-		git_futils_isdir(destination_directory_path) == GIT_SUCCESS)
+	if (git_path_isdir(source_directory_path) != GIT_SUCCESS ||
+		git_path_isdir(destination_directory_path) == GIT_SUCCESS)
 		return GIT_EINVALIDPATH;
 
 	git_buf_joinpath(&data.src, source_directory_path, "");
@@ -298,8 +298,8 @@ static int remove_placeholders_recurs(void *_data, git_buf *path)
 	remove_data *data = (remove_data *)_data;
 	size_t pathlen;
 
-	if (!git_futils_isdir(path->ptr))
-		return git_futils_direach(path, remove_placeholders_recurs, data);
+	if (!git_path_isdir(path->ptr))
+		return git_path_direach(path, remove_placeholders_recurs, data);
 
 	pathlen = path->size;
 
@@ -321,7 +321,7 @@ int remove_placeholders(const char *directory_path, const char *filename)
 	remove_data data;
 	git_buf buffer = GIT_BUF_INIT;
 
-	if (git_futils_isdir(directory_path))
+	if (git_path_isdir(directory_path))
 		return GIT_EINVALIDPATH;
 
 	if ((error = git_buf_sets(&buffer, directory_path)) < GIT_SUCCESS)