deps/libgit2/src/libgit2/sysdir.c

filter: Statementstmt 47.6% 70 / 147 Branchbran 31.3% 32 / 102 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 40.9% 102 / 249 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/xdiff/xutils.c ↑ Summary deps/libgit2/src/libgit2/proxy.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 "runtime.h"
11 #include "str.h"
12 #include "fs_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 87 static int git_sysdir_guess_programdata_dirs(git_str *out)
22 {
23 #ifdef GIT_WIN32
24 return git_win32__find_programdata_dirs(out);
25 #else
26 87 git_str_clear(out);
27 87 return 0;
28 #endif
29 }
30  
31 87 static int git_sysdir_guess_system_dirs(git_str *out)
32 {
33 #ifdef GIT_WIN32
34 return git_win32__find_system_dirs(out, "etc");
35 #else
36 87 return git_str_sets(out, "/etc");
37 #endif
38 }
39  
40 #ifndef GIT_WIN32
41 0 static int get_passwd_home(git_str *out, uid_t uid)
42 {
43 struct passwd pwd, *pwdptr;
44 0 char *buf = NULL;
45 long buflen;
46 int error;
47  
48 0 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 0 0
49  
50 0 ▶ if ((buflen = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ((buflen = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) 0 0 0
51 0 buflen = 1024;
52  
53 do {
54 0 buf = git__realloc(buf, buflen);
55 0 error = getpwuid_r(uid, &pwd, buf, buflen, &pwdptr);
56 0 buflen *= 2;
57 0 ▶ } while (error == ERANGE && buflen <= 8192);
Branch: 2 branches
BranchTrueFalseTotal
} while (error == ERANGE && buflen <= 8192); 0 0 0
} while (error == ERANGE && buflen <= 8192); 0 0 0
58  
59 0 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 0 0
60 0 git_error_set(GIT_ERROR_OS, "failed to get passwd entry");
61 0 goto out;
62 }
63  
64 0 ▶ if (!pwdptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pwdptr) { 0 0 0
65 0 git_error_set(GIT_ERROR_OS, "no passwd entry found for user");
66 0 goto out;
67 }
68  
69 0 ▶ if ((error = git_str_puts(out, pwdptr->pw_dir)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_puts(out, pwdptr->pw_dir)) < 0) 0 0 0
70 0 goto out;
71  
72 0 out:
73 0 git__free(buf);
74 0 return error;
75 }
76 #endif
77  
78 87 static int git_sysdir_guess_global_dirs(git_str *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 const char *sandbox_id;
86  
87 87 uid = getuid();
88 87 euid = geteuid();
89  
90 /**
91 * If APP_SANDBOX_CONTAINER_ID is set, we are running in a
92 * sandboxed environment on macOS.
93 */
94 87 sandbox_id = getenv("APP_SANDBOX_CONTAINER_ID");
95  
96 /*
97 * In case we are running setuid, use the configuration
98 * of the effective user.
99 *
100 * If we are running in a sandboxed environment on macOS,
101 * we have to get the HOME dir from the password entry file.
102 */
103 87 ▶ if (!sandbox_id && uid == euid)
Branch: 2 branches
BranchTrueFalseTotal
if (!sandbox_id && uid == euid) 87 0 87
if (!sandbox_id && uid == euid) 87 0 87
104 87 error = git__getenv(out, "HOME");
105 else
106 0 error = get_passwd_home(out, euid);
107  
108 87 ▶ if (error == GIT_ENOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) { 0 87 87
109 0 git_error_clear();
110 0 error = 0;
111 }
112  
113 87 return error;
114 #endif
115 }
116  
117 87 static int git_sysdir_guess_xdg_dirs(git_str *out)
118 {
119 #ifdef GIT_WIN32
120 return git_win32__find_xdg_dirs(out);
121 #else
122 87 git_str env = GIT_STR_INIT;
123 int error;
124 uid_t uid, euid;
125  
126 87 uid = getuid();
127 87 euid = geteuid();
128  
129 /*
130 * In case we are running setuid, only look up passwd
131 * directory of the effective user.
132 */
133 87 ▶ if (uid == euid) {
Branch: 1 branch
BranchTrueFalseTotal
if (uid == euid) { 87 0 87
134 87 ▶ if ((error = git__getenv(&env, "XDG_CONFIG_HOME")) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git__getenv(&env, "XDG_CONFIG_HOME")) == 0) 0 87 87
135 0 error = git_str_joinpath(out, env.ptr, "git");
136  
137 87 ▶ if (error == GIT_ENOTFOUND && (error = git__getenv(&env, "HOME")) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND && (error = git__getenv(&env, "HOME")) == 0) 87 0 87
if (error == GIT_ENOTFOUND && (error = git__getenv(&env, "HOME")) == 0) 87 0 87
138 87 error = git_str_joinpath(out, env.ptr, ".config/git");
139 } else {
140 0 ▶ if ((error = get_passwd_home(&env, euid)) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = get_passwd_home(&env, euid)) == 0) 0 0 0
141 0 error = git_str_joinpath(out, env.ptr, ".config/git");
142 }
143  
144 87 ▶ if (error == GIT_ENOTFOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) { 0 87 87
145 0 git_error_clear();
146 0 error = 0;
147 }
148  
149 87 git_str_dispose(&env);
150 87 return error;
151 #endif
152 }
153  
154 87 static int git_sysdir_guess_template_dirs(git_str *out)
155 {
156 #ifdef GIT_WIN32
157 return git_win32__find_system_dirs(out, "share/git-core/templates");
158 #else
159 87 return git_str_sets(out, "/usr/share/git-core/templates");
160 #endif
161 }
162  
163 struct git_sysdir__dir {
164 git_str buf;
165 int (*guess)(git_str *out);
166 };
167  
168 static struct git_sysdir__dir git_sysdir__dirs[] = {
169 { GIT_STR_INIT, git_sysdir_guess_system_dirs },
170 { GIT_STR_INIT, git_sysdir_guess_global_dirs },
171 { GIT_STR_INIT, git_sysdir_guess_xdg_dirs },
172 { GIT_STR_INIT, git_sysdir_guess_programdata_dirs },
173 { GIT_STR_INIT, git_sysdir_guess_template_dirs },
174 };
175  
176 0 static void git_sysdir_global_shutdown(void)
177 {
178 size_t i;
179  
180 0 ▶ for (i = 0; i < ARRAY_SIZE(git_sysdir__dirs); ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ARRAY_SIZE(git_sysdir__dirs); ++i) 0 0 0
181 0 git_str_dispose(&git_sysdir__dirs[i].buf);
182 0 }
183  
184 87 int git_sysdir_global_init(void)
185 {
186 size_t i;
187 87 int error = 0;
188  
189 522 ▶ for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++) 522 0 522
for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++) 435 87 522
190 435 error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf);
191  
192 87 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 0 87 87
193 0 return error;
194  
195 87 return git_runtime_shutdown_register(git_sysdir_global_shutdown);
196 }
197  
198 0 int git_sysdir_reset(void)
199 {
200 size_t i;
201 0 int error = 0;
202  
203 0 ▶ for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); ++i) { 0 0 0
for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); ++i) { 0 0 0
204 0 git_str_dispose(&git_sysdir__dirs[i].buf);
205 0 error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf);
206 }
207  
208 0 return error;
209 }
210  
211 1329 static int git_sysdir_check_selector(git_sysdir_t which)
212 {
213 1329 ▶ if (which < ARRAY_SIZE(git_sysdir__dirs))
Branch: 1 branch
BranchTrueFalseTotal
if (which < ARRAY_SIZE(git_sysdir__dirs)) 1329 0 1329
214 1329 return 0;
215  
216 0 git_error_set(GIT_ERROR_INVALID, "config directory selector out of range");
217 0 return -1;
218 }
219  
220  
221 1329 int git_sysdir_get(const git_str **out, git_sysdir_t which)
222 {
223 1329 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 1329 1329
224  
225 1329 *out = NULL;
226  
227 1329 ▶ GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which)); 0 1329 1329
228  
229 1329 *out = &git_sysdir__dirs[which].buf;
230 1329 return 0;
231 }
232  
233 #define PATH_MAGIC "$PATH"
234  
235 0 int git_sysdir_set(git_sysdir_t which, const char *search_path)
236 {
237 0 const char *expand_path = NULL;
238 0 git_str merge = GIT_STR_INIT;
239  
240 0 ▶ GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ERROR(git_sysdir_check_selector(which)); 0 0 0
241  
242 0 ▶ if (search_path != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (search_path != NULL) 0 0 0
243 0 expand_path = strstr(search_path, PATH_MAGIC);
244  
245 /* reset the default if this path has been cleared */
246 0 ▶ if (!search_path)
Branch: 1 branch
BranchTrueFalseTotal
if (!search_path) 0 0 0
247 0 git_sysdir__dirs[which].guess(&git_sysdir__dirs[which].buf);
248  
249 /* if $PATH is not referenced, then just set the path */
250 0 ▶ if (!expand_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (!expand_path) { 0 0 0
251 0 ▶ if (search_path)
Branch: 1 branch
BranchTrueFalseTotal
if (search_path) 0 0 0
252 0 git_str_sets(&git_sysdir__dirs[which].buf, search_path);
253  
254 0 goto done;
255 }
256  
257 /* otherwise set to join(before $PATH, old value, after $PATH) */
258 0 ▶ if (expand_path > search_path)
Branch: 1 branch
BranchTrueFalseTotal
if (expand_path > search_path) 0 0 0
259 0 git_str_set(&merge, search_path, expand_path - search_path);
260  
261 0 ▶ if (git_str_len(&git_sysdir__dirs[which].buf))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_len(&git_sysdir__dirs[which].buf)) 0 0 0
262 0 git_str_join(&merge, GIT_PATH_LIST_SEPARATOR,
263 0 merge.ptr, git_sysdir__dirs[which].buf.ptr);
264  
265 0 expand_path += strlen(PATH_MAGIC);
266 0 ▶ if (*expand_path)
Branch: 1 branch
BranchTrueFalseTotal
if (*expand_path) 0 0 0
267 0 git_str_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path);
268  
269 0 git_str_swap(&git_sysdir__dirs[which].buf, &merge);
270 0 git_str_dispose(&merge);
271  
272 0 done:
273 0 ▶ if (git_str_oom(&git_sysdir__dirs[which].buf))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(&git_sysdir__dirs[which].buf)) 0 0 0
274 0 return -1;
275  
276 0 return 0;
277 }
278  
279 1328 static int git_sysdir_find_in_dirlist(
280 git_str *path,
281 const char *name,
282 git_sysdir_t which,
283 const char *label)
284 {
285 size_t len;
286 1328 const char *scan, *next = NULL;
287 const git_str *syspath;
288  
289 1328 ▶ GIT_ERROR_CHECK_ERROR(git_sysdir_get(&syspath, which));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ERROR(git_sysdir_get(&syspath, which)); 0 1328 1328
290 1328 ▶ if (!syspath || !git_str_len(syspath))
Branch: 2 branches
BranchTrueFalseTotal
if (!syspath || !git_str_len(syspath)) 1328 0 1328
if (!syspath || !git_str_len(syspath)) 70 1258 1328
291 70 goto done;
292  
293 2447 ▶ for (scan = git_str_cstr(syspath); scan; scan = next) {
Branch: 1 branch
BranchTrueFalseTotal
for (scan = git_str_cstr(syspath); scan; scan = next) { 1258 1189 2447
294 /* find unescaped separator or end of string */
295 9406 ▶ for (next = scan; *next; ++next) {
Branch: 1 branch
BranchTrueFalseTotal
for (next = scan; *next; ++next) { 8148 1258 9406
296 8148 ▶ if (*next == GIT_PATH_LIST_SEPARATOR &&
Branch: 2 branches
BranchTrueFalseTotal
if (*next == GIT_PATH_LIST_SEPARATOR && 0 8148 8148
if (*next == GIT_PATH_LIST_SEPARATOR && 0 0 0
297 0 ▶ (next <= scan || next[-1] != '\\'))
Branch: 1 branch
BranchTrueFalseTotal
(next <= scan || next[-1] != '\\')) 0 0 0
298 break;
299 }
300  
301 1258 len = (size_t)(next - scan);
302 1258 ▶ next = (*next ? next + 1 : NULL);
Branch: 1 branch
BranchTrueFalseTotal
next = (*next ? next + 1 : NULL); 0 1258 1258
303 1258 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 0 1258 1258
304 0 continue;
305  
306 1258 ▶ GIT_ERROR_CHECK_ERROR(git_str_set(path, scan, len));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ERROR(git_str_set(path, scan, len)); 0 1258 1258
307 1258 ▶ if (name)
Branch: 1 branch
BranchTrueFalseTotal
if (name) 1258 0 1258
308 1258 ▶ GIT_ERROR_CHECK_ERROR(git_str_joinpath(path, path->ptr, name));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ERROR(git_str_joinpath(path, path->ptr, name)); 0 1258 1258
309  
310 1258 ▶ if (git_fs_path_exists(path->ptr))
Branch: 1 branch
BranchTrueFalseTotal
if (git_fs_path_exists(path->ptr)) 69 1189 1258
311 69 return 0;
312 }
313  
314 1189 done:
315 1259 ▶ if (name)
Branch: 1 branch
BranchTrueFalseTotal
if (name) 1259 0 1259
316 1259 git_error_set(GIT_ERROR_OS, "the %s file '%s' doesn't exist", label, name);
317 else
318 0 git_error_set(GIT_ERROR_OS, "the %s directory doesn't exist", label);
319 1259 git_str_dispose(path);
320 1259 return GIT_ENOTFOUND;
321 }
322  
323 1086 int git_sysdir_find_system_file(git_str *path, const char *filename)
324 {
325 1086 return git_sysdir_find_in_dirlist(
326 path, filename, GIT_SYSDIR_SYSTEM, "system");
327 }
328  
329 70 int git_sysdir_find_global_file(git_str *path, const char *filename)
330 {
331 70 return git_sysdir_find_in_dirlist(
332 path, filename, GIT_SYSDIR_GLOBAL, "global");
333 }
334  
335 102 int git_sysdir_find_xdg_file(git_str *path, const char *filename)
336 {
337 102 return git_sysdir_find_in_dirlist(
338 path, filename, GIT_SYSDIR_XDG, "global/xdg");
339 }
340  
341 70 int git_sysdir_find_programdata_file(git_str *path, const char *filename)
342 {
343 70 return git_sysdir_find_in_dirlist(
344 path, filename, GIT_SYSDIR_PROGRAMDATA, "ProgramData");
345 }
346  
347 0 int git_sysdir_find_template_dir(git_str *path)
348 {
349 0 return git_sysdir_find_in_dirlist(
350 path, NULL, GIT_SYSDIR_TEMPLATE, "template");
351 }
352  
353 0 int git_sysdir_expand_global_file(git_str *path, const char *filename)
354 {
355 int error;
356  
357 0 ▶ if ((error = git_sysdir_find_global_file(path, NULL)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_sysdir_find_global_file(path, NULL)) == 0) { 0 0 0
358 0 ▶ if (filename)
Branch: 1 branch
BranchTrueFalseTotal
if (filename) 0 0 0
359 0 error = git_str_joinpath(path, path->ptr, filename);
360 }
361  
362 0 return error;
363 }
« deps/libgit2/src/libgit2/xdiff/xutils.c ↑ Summary deps/libgit2/src/libgit2/proxy.c »