Commit 83634d38be2d0a1ac006d912216cd6787c2b1542

Edward Thomson 2014-02-24T17:43:10

Move system directory cache out of utils

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
diff --git a/src/attr.c b/src/attr.c
index 15ed5c9..d8a171d 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -1,6 +1,6 @@
 #include "common.h"
 #include "repository.h"
-#include "fileops.h"
+#include "sysdir.h"
 #include "config.h"
 #include "attr.h"
 #include "ignore.h"
@@ -589,7 +589,7 @@ static int collect_attr_files(
 	}
 
 	if ((flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) {
-		error = git_futils_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM);
+		error = git_sysdir_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM);
 		if (!error)
 			error = push_attr_file(repo, files, NULL, dir.ptr);
 		else if (error == GIT_ENOTFOUND) {
@@ -623,13 +623,13 @@ static int attr_cache__lookup_path(
 
 		/* expand leading ~/ as needed */
 		if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' &&
-			!git_futils_find_global_file(&buf, &cfgval[2]))
+			!git_sysdir_find_global_file(&buf, &cfgval[2]))
 			*out = git_buf_detach(&buf);
 		else if (cfgval)
 			*out = git__strdup(cfgval);
 
 	}
-	else if (!git_futils_find_xdg_file(&buf, fallback))
+	else if (!git_sysdir_find_xdg_file(&buf, fallback))
 		*out = git_buf_detach(&buf);
 
 	git_buf_free(&buf);
diff --git a/src/config.c b/src/config.c
index 6aa7146..ae093ed 100644
--- a/src/config.c
+++ b/src/config.c
@@ -6,7 +6,7 @@
  */
 
 #include "common.h"
-#include "fileops.h"
+#include "sysdir.h"
 #include "config.h"
 #include "git2/config.h"
 #include "git2/sys/config.h"
@@ -937,17 +937,17 @@ void git_config_iterator_free(git_config_iterator *iter)
 
 int git_config_find_global(git_buf *path)
 {
-	return git_futils_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL);
+	return git_sysdir_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL);
 }
 
 int git_config_find_xdg(git_buf *path)
 {
-	return git_futils_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG);
+	return git_sysdir_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG);
 }
 
 int git_config_find_system(git_buf *path)
 {
-	return git_futils_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM);
+	return git_sysdir_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM);
 }
 
 int git_config__global_location(git_buf *buf)
@@ -956,7 +956,7 @@ int git_config__global_location(git_buf *buf)
 	const char *sep, *start;
 	size_t len;
 
-	if (git_futils_dirs_get(&paths, GIT_FUTILS_DIR_GLOBAL) < 0)
+	if (git_sysdir_get(&paths, GIT_SYSDIR_GLOBAL) < 0)
 		return -1;
 
 	/* no paths, so give up */
diff --git a/src/config_file.c b/src/config_file.c
index c7727c0..aedf2cb 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -7,8 +7,8 @@
 
 #include "common.h"
 #include "config.h"
-#include "fileops.h"
 #include "filebuf.h"
+#include "sysdir.h"
 #include "buffer.h"
 #include "buf_text.h"
 #include "git2/config.h"
@@ -1003,7 +1003,7 @@ static int included_path(git_buf *out, const char *dir, const char *path)
 {
 	/* From the user's home */
 	if (path[0] == '~' && path[1] == '/')
-		return git_futils_find_global_file(out, &path[1]);
+		return git_sysdir_find_global_file(out, &path[1]);
 
 	return git_path_join_unrooted(out, path, dir, NULL);
 }
diff --git a/src/fileops.c b/src/fileops.c
index a60689f..5709499 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -558,226 +558,6 @@ int git_futils_rmdir_r(
 	return error;
 }
 
-
-static int git_futils_guess_system_dirs(git_buf *out)
-{
-#ifdef GIT_WIN32
-	return git_win32__find_system_dirs(out, L"etc\\");
-#else
-	return git_buf_sets(out, "/etc");
-#endif
-}
-
-static int git_futils_guess_global_dirs(git_buf *out)
-{
-#ifdef GIT_WIN32
-	return git_win32__find_global_dirs(out);
-#else
-	return git_buf_sets(out, getenv("HOME"));
-#endif
-}
-
-static int git_futils_guess_xdg_dirs(git_buf *out)
-{
-#ifdef GIT_WIN32
-	return git_win32__find_xdg_dirs(out);
-#else
-	const char *env = NULL;
-
-	if ((env = getenv("XDG_CONFIG_HOME")) != NULL)
-		return git_buf_joinpath(out, env, "git");
-	else if ((env = getenv("HOME")) != NULL)
-		return git_buf_joinpath(out, env, ".config/git");
-
-	git_buf_clear(out);
-	return 0;
-#endif
-}
-
-static int git_futils_guess_template_dirs(git_buf *out)
-{
-#ifdef GIT_WIN32
-	return git_win32__find_system_dirs(out, L"share\\git-core\\templates");
-#else
-	return git_buf_sets(out, "/usr/share/git-core/templates");
-#endif
-}
-
-typedef int (*git_futils_dirs_guess_cb)(git_buf *out);
-
-static git_buf git_futils__dirs[GIT_FUTILS_DIR__MAX] =
-	{ GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT };
-
-static git_futils_dirs_guess_cb git_futils__dir_guess[GIT_FUTILS_DIR__MAX] = {
-	git_futils_guess_system_dirs,
-	git_futils_guess_global_dirs,
-	git_futils_guess_xdg_dirs,
-	git_futils_guess_template_dirs,
-};
-
-static int git_futils__dirs_shutdown_set = 0;
-
-void git_futils_dirs_global_shutdown(void)
-{
-	int i;
-	for (i = 0; i < GIT_FUTILS_DIR__MAX; ++i)
-		git_buf_free(&git_futils__dirs[i]);
-}
-
-int git_futils_dirs_global_init(void)
-{
-	git_futils_dir_t i;
-	const git_buf *path;
-	int error = 0;
-
-	for (i = 0; !error && i < GIT_FUTILS_DIR__MAX; i++)
-		error = git_futils_dirs_get(&path, i);
-
-	return error;
-}
-
-static int git_futils_check_selector(git_futils_dir_t which)
-{
-	if (which < GIT_FUTILS_DIR__MAX)
-		return 0;
-	giterr_set(GITERR_INVALID, "config directory selector out of range");
-	return -1;
-}
-
-int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which)
-{
-	assert(out);
-
-	*out = NULL;
-
-	GITERR_CHECK_ERROR(git_futils_check_selector(which));
-
-	if (!git_buf_len(&git_futils__dirs[which])) {
-		/* prepare shutdown if we're going to need it */
-		if (!git_futils__dirs_shutdown_set) {
-			git__on_shutdown(git_futils_dirs_global_shutdown);
-			git_futils__dirs_shutdown_set = 1;
-		}
-
-		GITERR_CHECK_ERROR(
-			git_futils__dir_guess[which](&git_futils__dirs[which]));
-	}
-
-	*out = &git_futils__dirs[which];
-	return 0;
-}
-
-int git_futils_dirs_get_str(char *out, size_t outlen, git_futils_dir_t which)
-{
-	const git_buf *path = NULL;
-
-	GITERR_CHECK_ERROR(git_futils_check_selector(which));
-	GITERR_CHECK_ERROR(git_futils_dirs_get(&path, which));
-
-	if (!out || path->size >= outlen) {
-		giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path");
-		return GIT_EBUFS;
-	}
-
-	git_buf_copy_cstr(out, outlen, path);
-	return 0;
-}
-
-#define PATH_MAGIC "$PATH"
-
-int git_futils_dirs_set(git_futils_dir_t which, const char *search_path)
-{
-	const char *expand_path = NULL;
-	git_buf merge = GIT_BUF_INIT;
-
-	GITERR_CHECK_ERROR(git_futils_check_selector(which));
-
-	if (search_path != NULL)
-		expand_path = strstr(search_path, PATH_MAGIC);
-
-	/* init with default if not yet done and needed (ignoring error) */
-	if ((!search_path || expand_path) &&
-		!git_buf_len(&git_futils__dirs[which]))
-		git_futils__dir_guess[which](&git_futils__dirs[which]);
-
-	/* if $PATH is not referenced, then just set the path */
-	if (!expand_path)
-		return git_buf_sets(&git_futils__dirs[which], search_path);
-
-	/* otherwise set to join(before $PATH, old value, after $PATH) */
-	if (expand_path > search_path)
-		git_buf_set(&merge, search_path, expand_path - search_path);
-
-	if (git_buf_len(&git_futils__dirs[which]))
-		git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR,
-			merge.ptr, git_futils__dirs[which].ptr);
-
-	expand_path += strlen(PATH_MAGIC);
-	if (*expand_path)
-		git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path);
-
-	git_buf_swap(&git_futils__dirs[which], &merge);
-	git_buf_free(&merge);
-
-	return git_buf_oom(&git_futils__dirs[which]) ? -1 : 0;
-}
-
-static int git_futils_find_in_dirlist(
-	git_buf *path, const char *name, git_futils_dir_t which, const char *label)
-{
-	size_t len;
-	const char *scan, *next = NULL;
-	const git_buf *syspath;
-
-	GITERR_CHECK_ERROR(git_futils_dirs_get(&syspath, which));
-
-	for (scan = git_buf_cstr(syspath); scan; scan = next) {
-		for (next = strchr(scan, GIT_PATH_LIST_SEPARATOR);
-			 next && next > scan && next[-1] == '\\';
-			 next = strchr(next + 1, GIT_PATH_LIST_SEPARATOR))
-			/* find unescaped separator or end of string */;
-
-		len = next ? (size_t)(next++ - scan) : strlen(scan);
-		if (!len)
-			continue;
-
-		GITERR_CHECK_ERROR(git_buf_set(path, scan, len));
-		if (name)
-			GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));
-
-		if (git_path_exists(path->ptr))
-			return 0;
-	}
-
-	git_buf_clear(path);
-	giterr_set(GITERR_OS, "The %s file '%s' doesn't exist", label, name);
-	return GIT_ENOTFOUND;
-}
-
-int git_futils_find_system_file(git_buf *path, const char *filename)
-{
-	return git_futils_find_in_dirlist(
-		path, filename, GIT_FUTILS_DIR_SYSTEM, "system");
-}
-
-int git_futils_find_global_file(git_buf *path, const char *filename)
-{
-	return git_futils_find_in_dirlist(
-		path, filename, GIT_FUTILS_DIR_GLOBAL, "global");
-}
-
-int git_futils_find_xdg_file(git_buf *path, const char *filename)
-{
-	return git_futils_find_in_dirlist(
-		path, filename, GIT_FUTILS_DIR_XDG, "global/xdg");
-}
-
-int git_futils_find_template_dir(git_buf *path)
-{
-	return git_futils_find_in_dirlist(
-		path, NULL, GIT_FUTILS_DIR_TEMPLATE, "template");
-}
-
 int git_futils_fake_symlink(const char *old, const char *new)
 {
 	int retcode = GIT_ERROR;
diff --git a/src/fileops.h b/src/fileops.h
index 636c9b6..6a65235 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -268,89 +268,6 @@ extern int git_futils_mmap_ro_file(
 extern void git_futils_mmap_free(git_map *map);
 
 /**
- * Find a "global" file (i.e. one in a user's home directory).
- *
- * @param path buffer to write the full path into
- * @param filename name of file to find in the home directory
- * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
- */
-extern int git_futils_find_global_file(git_buf *path, const char *filename);
-
-/**
- * Find an "XDG" file (i.e. one in user's XDG config path).
- *
- * @param path buffer to write the full path into
- * @param filename name of file to find in the home directory
- * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
- */
-extern int git_futils_find_xdg_file(git_buf *path, const char *filename);
-
-/**
- * Find a "system" file (i.e. one shared for all users of the system).
- *
- * @param path buffer to write the full path into
- * @param filename name of file to find in the home directory
- * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
- */
-extern int git_futils_find_system_file(git_buf *path, const char *filename);
-
-/**
- * Find template directory.
- *
- * @param path buffer to write the full path into
- * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
- */
-extern int git_futils_find_template_dir(git_buf *path);
-
-typedef enum {
-	GIT_FUTILS_DIR_SYSTEM = 0,
-	GIT_FUTILS_DIR_GLOBAL = 1,
-	GIT_FUTILS_DIR_XDG    = 2,
-	GIT_FUTILS_DIR_TEMPLATE = 3,
-	GIT_FUTILS_DIR__MAX   = 4,
-} git_futils_dir_t;
-
-/**
- * Configures global data for configuration file search paths.
- *
- * @return 0 on success, <0 on failure
- */
-extern int git_futils_dirs_global_init(void);
-
-/**
- * Get the search path for global/system/xdg files
- *
- * @param out pointer to git_buf containing search path
- * @param which which list of paths to return
- * @return 0 on success, <0 on failure
- */
-extern int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which);
-
-/**
- * Get search path into a preallocated buffer
- *
- * @param out String buffer to write into
- * @param outlen Size of string buffer
- * @param which Which search path to return
- * @return 0 on success, GIT_EBUFS if out is too small, <0 on other failure
- */
-
-extern int git_futils_dirs_get_str(
-	char *out, size_t outlen, git_futils_dir_t which);
-
-/**
- * Set search paths for global/system/xdg files
- *
- * The first occurrence of the magic string "$PATH" in the new value will
- * be replaced with the old value of the search path.
- *
- * @param which Which search path to modify
- * @param paths New search path (separated by GIT_PATH_LIST_SEPARATOR)
- * @return 0 on success, <0 on failure (allocation error)
- */
-extern int git_futils_dirs_set(git_futils_dir_t which, const char *paths);
-
-/**
  * Create a "fake" symlink (text file containing the target path).
  *
  * @param new symlink file to be created
@@ -399,9 +316,4 @@ extern int git_futils_filestamp_check(
 extern void git_futils_filestamp_set(
 	git_futils_filestamp *tgt, const git_futils_filestamp *src);
 
-/**
- * Free the configuration file search paths.
- */
-extern void git_futils_dirs_global_shutdown(void);
-
 #endif /* INCLUDE_fileops_h__ */
diff --git a/src/global.c b/src/global.c
index 7d39c6f..8c8f55a 100644
--- a/src/global.c
+++ b/src/global.c
@@ -7,7 +7,7 @@
 #include "common.h"
 #include "global.h"
 #include "hash.h"
-#include "fileops.h"
+#include "sysdir.h"
 #include "git2/threads.h"
 #include "thread-utils.h"
 
@@ -86,7 +86,7 @@ static int synchronized_threads_init()
 
 	/* Initialize any other subsystems that have global state */
 	if ((error = git_hash_global_init()) >= 0)
-		error = git_futils_dirs_global_init();
+		error = git_sysdir_global_init();
 
 	win32_pthread_initialize();
 
@@ -169,7 +169,7 @@ static void init_once(void)
 
 	/* Initialize any other subsystems that have global state */
 	if ((init_error = git_hash_global_init()) >= 0)
-		init_error = git_futils_dirs_global_init();
+		init_error = git_sysdir_global_init();
 
 	GIT_MEMORY_BARRIER;
 }
diff --git a/src/repository.c b/src/repository.c
index 96ed306..6107366 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -17,6 +17,7 @@
 #include "tag.h"
 #include "blob.h"
 #include "fileops.h"
+#include "sysdir.h"
 #include "filebuf.h"
 #include "index.h"
 #include "config.h"
@@ -1265,7 +1266,7 @@ static int repo_init_structure(
 		}
 
 		if (!tdir) {
-			if (!(error = git_futils_find_template_dir(&template_buf)))
+			if (!(error = git_sysdir_find_template_dir(&template_buf)))
 				tdir = template_buf.ptr;
 			default_template = true;
 		}
diff --git a/src/settings.c b/src/settings.c
index 748f765..92fc314 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -9,7 +9,7 @@
 
 #include <git2.h>
 #include "common.h"
-#include "fileops.h"
+#include "sysdir.h"
 #include "cache.h"
 
 void git_libgit2_version(int *major, int *minor, int *rev)
@@ -38,14 +38,14 @@ int git_libgit2_capabilities()
 extern size_t git_mwindow__window_size;
 extern size_t git_mwindow__mapped_limit;
 
-static int config_level_to_futils_dir(int config_level)
+static int config_level_to_sysdir(int config_level)
 {
 	int val = -1;
 
 	switch (config_level) {
-	case GIT_CONFIG_LEVEL_SYSTEM: val = GIT_FUTILS_DIR_SYSTEM; break;
-	case GIT_CONFIG_LEVEL_XDG:    val = GIT_FUTILS_DIR_XDG; break;
-	case GIT_CONFIG_LEVEL_GLOBAL: val = GIT_FUTILS_DIR_GLOBAL; break;
+	case GIT_CONFIG_LEVEL_SYSTEM: val = GIT_SYSDIR_SYSTEM; break;
+	case GIT_CONFIG_LEVEL_XDG:    val = GIT_SYSDIR_XDG; break;
+	case GIT_CONFIG_LEVEL_GLOBAL: val = GIT_SYSDIR_GLOBAL; break;
 	default:
 		giterr_set(
 			GITERR_INVALID, "Invalid config path selector %d", config_level);
@@ -79,17 +79,17 @@ int git_libgit2_opts(int key, ...)
 		break;
 
 	case GIT_OPT_GET_SEARCH_PATH:
-		if ((error = config_level_to_futils_dir(va_arg(ap, int))) >= 0) {
+		if ((error = config_level_to_sysdir(va_arg(ap, int))) >= 0) {
 			char *out = va_arg(ap, char *);
 			size_t outlen = va_arg(ap, size_t);
 
-			error = git_futils_dirs_get_str(out, outlen, error);
+			error = git_sysdir_get_str(out, outlen, error);
 		}
 		break;
 
 	case GIT_OPT_SET_SEARCH_PATH:
-		if ((error = config_level_to_futils_dir(va_arg(ap, int))) >= 0)
-			error = git_futils_dirs_set(error, va_arg(ap, const char *));
+		if ((error = config_level_to_sysdir(va_arg(ap, int))) >= 0)
+			error = git_sysdir_set(error, va_arg(ap, const char *));
 		break;
 
 	case GIT_OPT_SET_CACHE_OBJECT_LIMIT:
@@ -118,12 +118,12 @@ int git_libgit2_opts(int key, ...)
 			char *out = va_arg(ap, char *);
 			size_t outlen = va_arg(ap, size_t);
 
-			error = git_futils_dirs_get_str(out, outlen, GIT_FUTILS_DIR_TEMPLATE);
+			error = git_sysdir_get_str(out, outlen, GIT_SYSDIR_TEMPLATE);
 		}
 		break;
 
 	case GIT_OPT_SET_TEMPLATE_PATH:
-		error = git_futils_dirs_set(GIT_FUTILS_DIR_TEMPLATE, va_arg(ap, const char *));
+		error = git_sysdir_set(GIT_SYSDIR_TEMPLATE, va_arg(ap, const char *));
 		break;
 	}
 
diff --git a/src/sysdir.c b/src/sysdir.c
new file mode 100644
index 0000000..2e6304e
--- /dev/null
+++ b/src/sysdir.c
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "common.h"
+#include "sysdir.h"
+#include "global.h"
+#include "buffer.h"
+#include "path.h"
+#include <ctype.h>
+#if GIT_WIN32
+#include "win32/findfile.h"
+#endif
+
+static int git_sysdir_guess_system_dirs(git_buf *out)
+{
+#ifdef GIT_WIN32
+	return git_win32__find_system_dirs(out, L"etc\\");
+#else
+	return git_buf_sets(out, "/etc");
+#endif
+}
+
+static int git_sysdir_guess_global_dirs(git_buf *out)
+{
+#ifdef GIT_WIN32
+	return git_win32__find_global_dirs(out);
+#else
+	return git_buf_sets(out, getenv("HOME"));
+#endif
+}
+
+static int git_sysdir_guess_xdg_dirs(git_buf *out)
+{
+#ifdef GIT_WIN32
+	return git_win32__find_xdg_dirs(out);
+#else
+	const char *env = NULL;
+
+	if ((env = getenv("XDG_CONFIG_HOME")) != NULL)
+		return git_buf_joinpath(out, env, "git");
+	else if ((env = getenv("HOME")) != NULL)
+		return git_buf_joinpath(out, env, ".config/git");
+
+	git_buf_clear(out);
+	return 0;
+#endif
+}
+
+static int git_sysdir_guess_template_dirs(git_buf *out)
+{
+#ifdef GIT_WIN32
+	return git_win32__find_system_dirs(out, L"share\\git-core\\templates");
+#else
+	return git_buf_sets(out, "/usr/share/git-core/templates");
+#endif
+}
+
+typedef int (*git_sysdir_guess_cb)(git_buf *out);
+
+static git_buf git_sysdir__dirs[GIT_SYSDIR__MAX] =
+	{ GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT };
+
+static git_sysdir_guess_cb git_sysdir__dir_guess[GIT_SYSDIR__MAX] = {
+	git_sysdir_guess_system_dirs,
+	git_sysdir_guess_global_dirs,
+	git_sysdir_guess_xdg_dirs,
+	git_sysdir_guess_template_dirs,
+};
+
+static int git_sysdir__dirs_shutdown_set = 0;
+
+int git_sysdir_global_init(void)
+{
+	git_sysdir_t i;
+	const git_buf *path;
+	int error = 0;
+
+	for (i = 0; !error && i < GIT_SYSDIR__MAX; i++)
+		error = git_sysdir_get(&path, i);
+
+	return error;
+}
+
+void git_sysdir_global_shutdown(void)
+{
+	int i;
+	for (i = 0; i < GIT_SYSDIR__MAX; ++i)
+		git_buf_free(&git_sysdir__dirs[i]);
+}
+
+static int git_sysdir_check_selector(git_sysdir_t which)
+{
+	if (which < GIT_SYSDIR__MAX)
+		return 0;
+
+	giterr_set(GITERR_INVALID, "config directory selector out of range");
+	return -1;
+}
+
+
+int git_sysdir_get(const git_buf **out, git_sysdir_t which)
+{
+	assert(out);
+
+	*out = NULL;
+
+	GITERR_CHECK_ERROR(git_sysdir_check_selector(which));
+
+	if (!git_buf_len(&git_sysdir__dirs[which])) {
+		/* prepare shutdown if we're going to need it */
+		if (!git_sysdir__dirs_shutdown_set) {
+			git__on_shutdown(git_sysdir_global_shutdown);
+			git_sysdir__dirs_shutdown_set = 1;
+		}
+
+		GITERR_CHECK_ERROR(
+			git_sysdir__dir_guess[which](&git_sysdir__dirs[which]));
+	}
+
+	*out = &git_sysdir__dirs[which];
+	return 0;
+}
+
+int git_sysdir_get_str(
+	char *out,
+	size_t outlen,
+	git_sysdir_t which)
+{
+	const git_buf *path = NULL;
+
+	GITERR_CHECK_ERROR(git_sysdir_check_selector(which));
+	GITERR_CHECK_ERROR(git_sysdir_get(&path, which));
+
+	if (!out || path->size >= outlen) {
+		giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path");
+		return GIT_EBUFS;
+	}
+
+	git_buf_copy_cstr(out, outlen, path);
+	return 0;
+}
+
+#define PATH_MAGIC "$PATH"
+
+int git_sysdir_set(git_sysdir_t which, const char *search_path)
+{
+	const char *expand_path = NULL;
+	git_buf merge = GIT_BUF_INIT;
+
+	GITERR_CHECK_ERROR(git_sysdir_check_selector(which));
+
+	if (search_path != NULL)
+		expand_path = strstr(search_path, PATH_MAGIC);
+
+	/* init with default if not yet done and needed (ignoring error) */
+	if ((!search_path || expand_path) &&
+		!git_buf_len(&git_sysdir__dirs[which]))
+		git_sysdir__dir_guess[which](&git_sysdir__dirs[which]);
+
+	/* if $PATH is not referenced, then just set the path */
+	if (!expand_path)
+		return git_buf_sets(&git_sysdir__dirs[which], search_path);
+
+	/* otherwise set to join(before $PATH, old value, after $PATH) */
+	if (expand_path > search_path)
+		git_buf_set(&merge, search_path, expand_path - search_path);
+
+	if (git_buf_len(&git_sysdir__dirs[which]))
+		git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR,
+			merge.ptr, git_sysdir__dirs[which].ptr);
+
+	expand_path += strlen(PATH_MAGIC);
+	if (*expand_path)
+		git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path);
+
+	git_buf_swap(&git_sysdir__dirs[which], &merge);
+	git_buf_free(&merge);
+
+	return git_buf_oom(&git_sysdir__dirs[which]) ? -1 : 0;
+}
+
+static int git_sysdir_find_in_dirlist(
+	git_buf *path,
+	const char *name,
+	git_sysdir_t which,
+	const char *label)
+{
+	size_t len;
+	const char *scan, *next = NULL;
+	const git_buf *syspath;
+
+	GITERR_CHECK_ERROR(git_sysdir_get(&syspath, which));
+
+	for (scan = git_buf_cstr(syspath); scan; scan = next) {
+		for (next = strchr(scan, GIT_PATH_LIST_SEPARATOR);
+			 next && next > scan && next[-1] == '\\';
+			 next = strchr(next + 1, GIT_PATH_LIST_SEPARATOR))
+			/* find unescaped separator or end of string */;
+
+		len = next ? (size_t)(next++ - scan) : strlen(scan);
+		if (!len)
+			continue;
+
+		GITERR_CHECK_ERROR(git_buf_set(path, scan, len));
+		if (name)
+			GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));
+
+		if (git_path_exists(path->ptr))
+			return 0;
+	}
+
+	git_buf_clear(path);
+	giterr_set(GITERR_OS, "The %s file '%s' doesn't exist", label, name);
+	return GIT_ENOTFOUND;
+}
+
+int git_sysdir_find_system_file(git_buf *path, const char *filename)
+{
+	return git_sysdir_find_in_dirlist(
+		path, filename, GIT_SYSDIR_SYSTEM, "system");
+}
+
+int git_sysdir_find_global_file(git_buf *path, const char *filename)
+{
+	return git_sysdir_find_in_dirlist(
+		path, filename, GIT_SYSDIR_GLOBAL, "global");
+}
+
+int git_sysdir_find_xdg_file(git_buf *path, const char *filename)
+{
+	return git_sysdir_find_in_dirlist(
+		path, filename, GIT_SYSDIR_XDG, "global/xdg");
+}
+
+int git_sysdir_find_template_dir(git_buf *path)
+{
+	return git_sysdir_find_in_dirlist(
+		path, NULL, GIT_SYSDIR_TEMPLATE, "template");
+}
+
diff --git a/src/sysdir.h b/src/sysdir.h
new file mode 100644
index 0000000..f1bbf0b
--- /dev/null
+++ b/src/sysdir.h
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_sysdir_h__
+#define INCLUDE_sysdir_h__
+
+#include "common.h"
+#include "posix.h"
+#include "buffer.h"
+
+/**
+ * Find a "global" file (i.e. one in a user's home directory).
+ *
+ * @param path buffer to write the full path into
+ * @param filename name of file to find in the home directory
+ * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
+ */
+extern int git_sysdir_find_global_file(git_buf *path, const char *filename);
+
+/**
+ * Find an "XDG" file (i.e. one in user's XDG config path).
+ *
+ * @param path buffer to write the full path into
+ * @param filename name of file to find in the home directory
+ * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
+ */
+extern int git_sysdir_find_xdg_file(git_buf *path, const char *filename);
+
+/**
+ * Find a "system" file (i.e. one shared for all users of the system).
+ *
+ * @param path buffer to write the full path into
+ * @param filename name of file to find in the home directory
+ * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
+ */
+extern int git_sysdir_find_system_file(git_buf *path, const char *filename);
+
+/**
+ * Find template directory.
+ *
+ * @param path buffer to write the full path into
+ * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
+ */
+extern int git_sysdir_find_template_dir(git_buf *path);
+
+typedef enum {
+	GIT_SYSDIR_SYSTEM = 0,
+	GIT_SYSDIR_GLOBAL = 1,
+	GIT_SYSDIR_XDG    = 2,
+	GIT_SYSDIR_TEMPLATE = 3,
+	GIT_SYSDIR__MAX   = 4,
+} git_sysdir_t;
+
+/**
+ * Configures global data for configuration file search paths.
+ *
+ * @return 0 on success, <0 on failure
+ */
+extern int git_sysdir_global_init(void);
+
+/**
+ * Get the search path for global/system/xdg files
+ *
+ * @param out pointer to git_buf containing search path
+ * @param which which list of paths to return
+ * @return 0 on success, <0 on failure
+ */
+extern int git_sysdir_get(const git_buf **out, git_sysdir_t which);
+
+/**
+ * Get search path into a preallocated buffer
+ *
+ * @param out String buffer to write into
+ * @param outlen Size of string buffer
+ * @param which Which search path to return
+ * @return 0 on success, GIT_EBUFS if out is too small, <0 on other failure
+ */
+
+extern int git_sysdir_get_str(char *out, size_t outlen, git_sysdir_t which);
+
+/**
+ * Set search paths for global/system/xdg files
+ *
+ * The first occurrence of the magic string "$PATH" in the new value will
+ * be replaced with the old value of the search path.
+ *
+ * @param which Which search path to modify
+ * @param paths New search path (separated by GIT_PATH_LIST_SEPARATOR)
+ * @return 0 on success, <0 on failure (allocation error)
+ */
+extern int git_sysdir_set(git_sysdir_t which, const char *paths);
+
+/**
+ * Free the configuration file search paths.
+ */
+extern void git_sysdir_global_shutdown(void);
+
+#endif /* INCLUDE_sysdir_h__ */
diff --git a/tests/core/env.c b/tests/core/env.c
index 0fa6472..a32f5ed 100644
--- a/tests/core/env.c
+++ b/tests/core/env.c
@@ -1,5 +1,6 @@
 #include "clar_libgit2.h"
 #include "fileops.h"
+#include "sysdir.h"
 #include "path.h"
 
 #ifdef GIT_WIN32
@@ -41,12 +42,12 @@ void test_core_env__initialize(void)
 
 static void reset_global_search_path(void)
 {
-	cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL));
+	cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL, NULL));
 }
 
 static void reset_system_search_path(void)
 {
-	cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL));
+	cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, NULL));
 }
 
 void test_core_env__cleanup(void)
@@ -120,18 +121,18 @@ void test_core_env__0(void)
 		git_buf_rtruncate_at_char(&path, '/');
 
 		cl_assert_equal_i(
-			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
+			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
 
 		setenv_and_check("HOME", path.ptr);
 		reset_global_search_path();
 
-		cl_git_pass(git_futils_find_global_file(&found, testfile));
+		cl_git_pass(git_sysdir_find_global_file(&found, testfile));
 
 		cl_setenv("HOME", env_save[0]);
 		reset_global_search_path();
 
 		cl_assert_equal_i(
-			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
+			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
 
 #ifdef GIT_WIN32
 		setenv_and_check("HOMEDRIVE", NULL);
@@ -139,7 +140,7 @@ void test_core_env__0(void)
 		setenv_and_check("USERPROFILE", path.ptr);
 		reset_global_search_path();
 
-		cl_git_pass(git_futils_find_global_file(&found, testfile));
+		cl_git_pass(git_sysdir_find_global_file(&found, testfile));
 
 		{
 			int root = git_path_root(path.ptr);
@@ -150,7 +151,7 @@ void test_core_env__0(void)
 				reset_global_search_path();
 
 				cl_assert_equal_i(
-					GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
+					GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
 
 				old = path.ptr[root];
 				path.ptr[root] = '\0';
@@ -159,7 +160,7 @@ void test_core_env__0(void)
 				setenv_and_check("HOMEPATH", &path.ptr[root]);
 				reset_global_search_path();
 
-				cl_git_pass(git_futils_find_global_file(&found, testfile));
+				cl_git_pass(git_sysdir_find_global_file(&found, testfile));
 			}
 		}
 #endif
@@ -177,7 +178,7 @@ void test_core_env__1(void)
 	git_buf path = GIT_BUF_INIT;
 
 	cl_assert_equal_i(
-		GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
+		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
 
 	cl_git_pass(cl_setenv("HOME", "doesnotexist"));
 #ifdef GIT_WIN32
@@ -187,7 +188,7 @@ void test_core_env__1(void)
 	reset_global_search_path();
 
 	cl_assert_equal_i(
-		GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
+		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
 
 	cl_git_pass(cl_setenv("HOME", NULL));
 #ifdef GIT_WIN32
@@ -198,17 +199,17 @@ void test_core_env__1(void)
 	reset_system_search_path();
 
 	cl_assert_equal_i(
-		GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
+		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
 
 	cl_assert_equal_i(
-		GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile"));
+		GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
 
 #ifdef GIT_WIN32
 	cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
 	reset_system_search_path();
 
 	cl_assert_equal_i(
-		GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile"));
+		GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
 #endif
 
 	git_buf_free(&path);
@@ -242,13 +243,13 @@ static void check_global_searchpath(
 		cl_assert_equal_s(out, path);
 
 	/* find file using new path */
-	cl_git_pass(git_futils_find_global_file(temp, file));
+	cl_git_pass(git_sysdir_find_global_file(temp, file));
 
 	/* reset path and confirm file not found */
 	cl_git_pass(git_libgit2_opts(
 		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
 	cl_assert_equal_i(
-		GIT_ENOTFOUND, git_futils_find_global_file(temp, file));
+		GIT_ENOTFOUND, git_sysdir_find_global_file(temp, file));
 }
 
 void test_core_env__2(void)
@@ -285,7 +286,7 @@ void test_core_env__2(void)
 
 		/* default should be NOTFOUND */
 		cl_assert_equal_i(
-			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
+			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
 
 		/* try plain, append $PATH, and prepend $PATH */
 		check_global_searchpath(path.ptr,  0, testfile, &found);
diff --git a/tests/repo/config.c b/tests/repo/config.c
index e77acc8..2e7be37 100644
--- a/tests/repo/config.c
+++ b/tests/repo/config.c
@@ -1,4 +1,5 @@
 #include "clar_libgit2.h"
+#include "sysdir.h"
 #include "fileops.h"
 #include <ctype.h>
 
@@ -47,7 +48,7 @@ void test_repo_config__open_missing_global(void)
 	git_config_free(config);
 	git_repository_free(repo);
 
-	git_futils_dirs_global_shutdown();
+	git_sysdir_global_shutdown();
 }
 
 void test_repo_config__open_missing_global_with_separators(void)
@@ -76,7 +77,7 @@ void test_repo_config__open_missing_global_with_separators(void)
 	git_config_free(config);
 	git_repository_free(repo);
 
-	git_futils_dirs_global_shutdown();
+	git_sysdir_global_shutdown();
 }
 
 #include "repository.h"
@@ -105,7 +106,7 @@ void test_repo_config__read_no_configs(void)
 	cl_assert_equal_i(GIT_ABBREV_DEFAULT, val);
 	git_repository_free(repo);
 
-	git_futils_dirs_global_shutdown();
+	git_sysdir_global_shutdown();
 
 	/* with just system */
 
@@ -204,5 +205,5 @@ void test_repo_config__read_no_configs(void)
 	cl_assert(!git_path_exists("empty_standard_repo/.git/config"));
 	cl_assert(!git_path_exists("alternate/3/.gitconfig"));
 
-	git_futils_dirs_global_shutdown();
+	git_sysdir_global_shutdown();
 }
diff --git a/tests/repo/open.c b/tests/repo/open.c
index 7cfe041..f7420bd 100644
--- a/tests/repo/open.c
+++ b/tests/repo/open.c
@@ -1,5 +1,6 @@
 #include "clar_libgit2.h"
 #include "fileops.h"
+#include "sysdir.h"
 #include <ctype.h>
 
 void test_repo_open__cleanup(void)
@@ -323,7 +324,7 @@ void test_repo_open__no_config(void)
 	git_repository_free(repo);
 	cl_fixture_cleanup("empty_standard_repo");
 
-	git_futils_dirs_global_shutdown();
+	git_sysdir_global_shutdown();
 }
 
 void test_repo_open__force_bare(void)