]> git.proxmox.com Git - libgit2.git/blob - src/sysdir.c
e07ba7199d8ad77f615760966af5353bf9c1d598
[libgit2.git] / src / sysdir.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "sysdir.h"
9
10 #include "global.h"
11 #include "buffer.h"
12 #include "path.h"
13 #include <ctype.h>
14 #if GIT_WIN32
15 #include "win32/findfile.h"
16 #else
17 #include <unistd.h>
18 #include <pwd.h>
19 #endif
20
21 static int git_sysdir_guess_programdata_dirs(git_buf *out)
22 {
23 #ifdef GIT_WIN32
24 return git_win32__find_programdata_dirs(out);
25 #else
26 git_buf_clear(out);
27 return 0;
28 #endif
29 }
30
31 static int git_sysdir_guess_system_dirs(git_buf *out)
32 {
33 #ifdef GIT_WIN32
34 return git_win32__find_system_dirs(out, L"etc\\");
35 #else
36 return git_buf_sets(out, "/etc");
37 #endif
38 }
39
40 #ifndef GIT_WIN32
41 static int get_passwd_home(git_buf *out, uid_t uid)
42 {
43 struct passwd pwd, *pwdptr;
44 char *buf = NULL;
45 long buflen;
46 int error;
47
48 assert(out);
49
50 if ((buflen = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1)
51 buflen = 1024;
52
53 do {
54 buf = git__realloc(buf, buflen);
55 error = getpwuid_r(uid, &pwd, buf, buflen, &pwdptr);
56 buflen *= 2;
57 } while (error == ERANGE && buflen <= 8192);
58
59 if (error) {
60 git_error_set(GIT_ERROR_OS, "failed to get passwd entry");
61 goto out;
62 }
63
64 if (!pwdptr) {
65 git_error_set(GIT_ERROR_OS, "no passwd entry found for user");
66 goto out;
67 }
68
69 if ((error = git_buf_puts(out, pwdptr->pw_dir)) < 0)
70 goto out;
71
72 out:
73 git__free(buf);
74 return error;
75 }
76 #endif
77
78 static int git_sysdir_guess_global_dirs(git_buf *out)
79 {
80 #ifdef GIT_WIN32
81 return git_win32__find_global_dirs(out);
82 #else
83 int error;
84 uid_t uid, euid;
85
86 uid = getuid();
87 euid = geteuid();
88
89 /*
90 * In case we are running setuid, use the configuration
91 * of the effective user.
92 */
93 if (uid == euid)
94 error = git__getenv(out, "HOME");
95 else
96 error = get_passwd_home(out, euid);
97
98 if (error == GIT_ENOTFOUND) {
99 git_error_clear();
100 error = 0;
101 }
102
103 return error;
104 #endif
105 }
106
107 static int git_sysdir_guess_xdg_dirs(git_buf *out)
108 {
109 #ifdef GIT_WIN32
110 return git_win32__find_xdg_dirs(out);
111 #else
112 git_buf env = GIT_BUF_INIT;
113 int error;
114 uid_t uid, euid;
115
116 uid = getuid();
117 euid = geteuid();
118
119 /*
120 * In case we are running setuid, only look up passwd
121 * directory of the effective user.
122 */
123 if (uid == euid) {
124 if ((error = git__getenv(&env, "XDG_CONFIG_HOME")) == 0)
125 error = git_buf_joinpath(out, env.ptr, "git");
126
127 if (error == GIT_ENOTFOUND && (error = git__getenv(&env, "HOME")) == 0)
128 error = git_buf_joinpath(out, env.ptr, ".config/git");
129 } else {
130 if ((error = get_passwd_home(&env, euid)) == 0)
131 error = git_buf_joinpath(out, env.ptr, ".config/git");
132 }
133
134 if (error == GIT_ENOTFOUND) {
135 git_error_clear();
136 error = 0;
137 }
138
139 git_buf_dispose(&env);
140 return error;
141 #endif
142 }
143
144 static int git_sysdir_guess_template_dirs(git_buf *out)
145 {
146 #ifdef GIT_WIN32
147 return git_win32__find_system_dirs(out, L"share\\git-core\\templates");
148 #else
149 return git_buf_sets(out, "/usr/share/git-core/templates");
150 #endif
151 }
152
153 struct git_sysdir__dir {
154 git_buf buf;
155 int (*guess)(git_buf *out);
156 };
157
158 static struct git_sysdir__dir git_sysdir__dirs[] = {
159 { GIT_BUF_INIT, git_sysdir_guess_system_dirs },
160 { GIT_BUF_INIT, git_sysdir_guess_global_dirs },
161 { GIT_BUF_INIT, git_sysdir_guess_xdg_dirs },
162 { GIT_BUF_INIT, git_sysdir_guess_programdata_dirs },
163 { GIT_BUF_INIT, git_sysdir_guess_template_dirs },
164 };
165
166 static void git_sysdir_global_shutdown(void)
167 {
168 size_t i;
169
170 for (i = 0; i < ARRAY_SIZE(git_sysdir__dirs); ++i)
171 git_buf_dispose(&git_sysdir__dirs[i].buf);
172 }
173
174 int git_sysdir_global_init(void)
175 {
176 size_t i;
177 int error = 0;
178
179 for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++)
180 error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf);
181
182 git__on_shutdown(git_sysdir_global_shutdown);
183
184 return error;
185 }
186
187 static int git_sysdir_check_selector(git_sysdir_t which)
188 {
189 if (which < ARRAY_SIZE(git_sysdir__dirs))
190 return 0;
191
192 git_error_set(GIT_ERROR_INVALID, "config directory selector out of range");
193 return -1;
194 }
195
196
197 int git_sysdir_get(const git_buf **out, git_sysdir_t which)
198 {
199 assert(out);
200
201 *out = NULL;
202
203 GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which));
204
205 *out = &git_sysdir__dirs[which].buf;
206 return 0;
207 }
208
209 int git_sysdir_get_str(
210 char *out,
211 size_t outlen,
212 git_sysdir_t which)
213 {
214 const git_buf *path = NULL;
215
216 GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which));
217 GIT_ERROR_CHECK_ERROR(git_sysdir_get(&path, which));
218
219 if (!out || path->size >= outlen) {
220 git_error_set(GIT_ERROR_NOMEMORY, "buffer is too short for the path");
221 return GIT_EBUFS;
222 }
223
224 git_buf_copy_cstr(out, outlen, path);
225 return 0;
226 }
227
228 #define PATH_MAGIC "$PATH"
229
230 int git_sysdir_set(git_sysdir_t which, const char *search_path)
231 {
232 const char *expand_path = NULL;
233 git_buf merge = GIT_BUF_INIT;
234
235 GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which));
236
237 if (search_path != NULL)
238 expand_path = strstr(search_path, PATH_MAGIC);
239
240 /* reset the default if this path has been cleared */
241 if (!search_path)
242 git_sysdir__dirs[which].guess(&git_sysdir__dirs[which].buf);
243
244 /* if $PATH is not referenced, then just set the path */
245 if (!expand_path) {
246 if (search_path)
247 git_buf_sets(&git_sysdir__dirs[which].buf, search_path);
248
249 goto done;
250 }
251
252 /* otherwise set to join(before $PATH, old value, after $PATH) */
253 if (expand_path > search_path)
254 git_buf_set(&merge, search_path, expand_path - search_path);
255
256 if (git_buf_len(&git_sysdir__dirs[which].buf))
257 git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR,
258 merge.ptr, git_sysdir__dirs[which].buf.ptr);
259
260 expand_path += strlen(PATH_MAGIC);
261 if (*expand_path)
262 git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path);
263
264 git_buf_swap(&git_sysdir__dirs[which].buf, &merge);
265 git_buf_dispose(&merge);
266
267 done:
268 if (git_buf_oom(&git_sysdir__dirs[which].buf))
269 return -1;
270
271 return 0;
272 }
273
274 static int git_sysdir_find_in_dirlist(
275 git_buf *path,
276 const char *name,
277 git_sysdir_t which,
278 const char *label)
279 {
280 size_t len;
281 const char *scan, *next = NULL;
282 const git_buf *syspath;
283
284 GIT_ERROR_CHECK_ERROR(git_sysdir_get(&syspath, which));
285 if (!syspath || !git_buf_len(syspath))
286 goto done;
287
288 for (scan = git_buf_cstr(syspath); scan; scan = next) {
289 /* find unescaped separator or end of string */
290 for (next = scan; *next; ++next) {
291 if (*next == GIT_PATH_LIST_SEPARATOR &&
292 (next <= scan || next[-1] != '\\'))
293 break;
294 }
295
296 len = (size_t)(next - scan);
297 next = (*next ? next + 1 : NULL);
298 if (!len)
299 continue;
300
301 GIT_ERROR_CHECK_ERROR(git_buf_set(path, scan, len));
302 if (name)
303 GIT_ERROR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));
304
305 if (git_path_exists(path->ptr))
306 return 0;
307 }
308
309 done:
310 git_buf_dispose(path);
311 git_error_set(GIT_ERROR_OS, "the %s file '%s' doesn't exist", label, name);
312 return GIT_ENOTFOUND;
313 }
314
315 int git_sysdir_find_system_file(git_buf *path, const char *filename)
316 {
317 return git_sysdir_find_in_dirlist(
318 path, filename, GIT_SYSDIR_SYSTEM, "system");
319 }
320
321 int git_sysdir_find_global_file(git_buf *path, const char *filename)
322 {
323 return git_sysdir_find_in_dirlist(
324 path, filename, GIT_SYSDIR_GLOBAL, "global");
325 }
326
327 int git_sysdir_find_xdg_file(git_buf *path, const char *filename)
328 {
329 return git_sysdir_find_in_dirlist(
330 path, filename, GIT_SYSDIR_XDG, "global/xdg");
331 }
332
333 int git_sysdir_find_programdata_file(git_buf *path, const char *filename)
334 {
335 return git_sysdir_find_in_dirlist(
336 path, filename, GIT_SYSDIR_PROGRAMDATA, "ProgramData");
337 }
338
339 int git_sysdir_find_template_dir(git_buf *path)
340 {
341 return git_sysdir_find_in_dirlist(
342 path, NULL, GIT_SYSDIR_TEMPLATE, "template");
343 }
344
345 int git_sysdir_expand_global_file(git_buf *path, const char *filename)
346 {
347 int error;
348
349 if ((error = git_sysdir_find_global_file(path, NULL)) == 0) {
350 if (filename)
351 error = git_buf_joinpath(path, path->ptr, filename);
352 }
353
354 return error;
355 }