deps/libgit2/src/util/fs_path.c

filter: Statementstmt 55.4% 388 / 700 Branchbran 37.3% 239 / 640 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 46.7% 627 / 1340 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/repository.c ↑ Summary deps/libgit2/src/util/str.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 "fs_path.h"
9  
10 #include "git2_util.h"
11 #include "futils.h"
12 #include "posix.h"
13 #ifdef GIT_WIN32
14 #include "win32/posix.h"
15 #include "win32/w32_buffer.h"
16 #include "win32/w32_util.h"
17 #include "win32/version.h"
18 #include <aclapi.h>
19 #else
20 #include <dirent.h>
21 #endif
22 #include <stdio.h>
23 #include <ctype.h>
24  
25 #define ensure_error_set(code) do { \
26 const git_error *e = git_error_last(); \
27 if (!e || !e->message) \
28 git_error_set(e ? e->klass : GIT_ERROR_CALLBACK, \
29 "filesystem callback returned %d", code); \
30 } while(0)
31  
32 10097 static int dos_drive_prefix_length(const char *path)
33 {
34 int i;
35  
36 /*
37 * Does it start with an ASCII letter (i.e. highest bit not set),
38 * followed by a colon?
39 */
40 10097 ▶ if (!(0x80 & (unsigned char)*path))
Branch: 1 branch
BranchTrueFalseTotal
if (!(0x80 & (unsigned char)*path)) 10097 0 10097
41 10097 ▶ return *path && path[1] == ':' ? 2 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return *path && path[1] == ':' ? 2 : 0; 9914 183 10097
return *path && path[1] == ':' ? 2 : 0; 0 9914 9914
42  
43 /*
44 * While drive letters must be letters of the English alphabet, it is
45 * possible to assign virtually _any_ Unicode character via `subst` as
46 * a drive letter to "virtual drives". Even `1`, or `ä`. Or fun stuff
47 * like this:
48 *
49 * subst ֍: %USERPROFILE%\Desktop
50 */
51 0 ▶ for (i = 1; i < 4 && (0x80 & (unsigned char)path[i]); i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 1; i < 4 && (0x80 & (unsigned char)path[i]); i++) 0 0 0
for (i = 1; i < 4 && (0x80 & (unsigned char)path[i]); i++) 0 0 0
52 ; /* skip first UTF-8 character */
53 0 ▶ return path[i] == ':' ? i + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return path[i] == ':' ? i + 1 : 0; 0 0 0
54 }
55  
56 #ifdef GIT_WIN32
57 static bool looks_like_network_computer_name(const char *path, int pos)
58 {
59 if (pos < 3)
60 return false;
61  
62 if (path[0] != '/' || path[1] != '/')
63 return false;
64  
65 while (pos-- > 2) {
66 if (path[pos] == '/')
67 return false;
68 }
69  
70 return true;
71 }
72 #endif
73  
74 /*
75 * Based on the Android implementation, BSD licensed.
76 * http://android.git.kernel.org/
77 *
78 * Copyright (C) 2008 The Android Open Source Project
79 * All rights reserved.
80 *
81 * Redistribution and use in source and binary forms, with or without
82 * modification, are permitted provided that the following conditions
83 * are met:
84 * * Redistributions of source code must retain the above copyright
85 * notice, this list of conditions and the following disclaimer.
86 * * Redistributions in binary form must reproduce the above copyright
87 * notice, this list of conditions and the following disclaimer in
88 * the documentation and/or other materials provided with the
89 * distribution.
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
92 * AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
93 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
94 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
95 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
96 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
97 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
98 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
99 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
100 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
101 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
102 * SUCH DAMAGE.
103 */
104 0 int git_fs_path_basename_r(git_str *buffer, const char *path)
105 {
106 const char *endp, *startp;
107 int len, result;
108  
109 /* Empty or NULL string gets treated as "." */
110 0 ▶ if (path == NULL || *path == '\0') {
Branch: 2 branches
BranchTrueFalseTotal
if (path == NULL || *path == '\0') { 0 0 0
if (path == NULL || *path == '\0') { 0 0 0
111 0 startp = ".";
112 0 len = 1;
113 0 goto Exit;
114 }
115  
116 /* Strip trailing slashes */
117 0 endp = path + strlen(path) - 1;
118 0 ▶ while (endp > path && *endp == '/')
Branch: 2 branches
BranchTrueFalseTotal
while (endp > path && *endp == '/') 0 0 0
while (endp > path && *endp == '/') 0 0 0
119 0 endp--;
120  
121 /* All slashes becomes "/" */
122 0 ▶ if (endp == path && *endp == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (endp == path && *endp == '/') { 0 0 0
if (endp == path && *endp == '/') { 0 0 0
123 0 startp = "/";
124 0 len = 1;
125 0 goto Exit;
126 }
127  
128 /* Find the start of the base */
129 0 startp = endp;
130 0 ▶ while (startp > path && *(startp - 1) != '/')
Branch: 2 branches
BranchTrueFalseTotal
while (startp > path && *(startp - 1) != '/') 0 0 0
while (startp > path && *(startp - 1) != '/') 0 0 0
131 0 startp--;
132  
133 /* Cast is safe because max path < max int */
134 0 len = (int)(endp - startp + 1);
135  
136 0 Exit:
137 0 result = len;
138  
139 0 ▶ if (buffer != NULL && git_str_set(buffer, startp, len) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (buffer != NULL && git_str_set(buffer, startp, len) < 0) 0 0 0
if (buffer != NULL && git_str_set(buffer, startp, len) < 0) 0 0 0
140 0 return -1;
141  
142 0 return result;
143 }
144  
145 /*
146 * Determine if the path is a Windows prefix and, if so, returns
147 * its actual length. If it is not a prefix, returns -1.
148 */
149 2672 static int win32_prefix_length(const char *path, int len)
150 {
151 #ifndef GIT_WIN32
152 2672 GIT_UNUSED(path);
153 2672 GIT_UNUSED(len);
154 #else
155 /*
156 * Mimic unix behavior where '/.git' returns '/': 'C:/.git'
157 * will return 'C:/' here
158 */
159 if (dos_drive_prefix_length(path) == len)
160 return len;
161  
162 /*
163 * Similarly checks if we're dealing with a network computer name
164 * '//computername/.git' will return '//computername/'
165 */
166 if (looks_like_network_computer_name(path, len))
167 return len;
168 #endif
169  
170 2672 return -1;
171 }
172  
173 /*
174 * Based on the Android implementation, BSD licensed.
175 * Check http://android.git.kernel.org/
176 */
177 1429 int git_fs_path_dirname_r(git_str *buffer, const char *path)
178 {
179 const char *endp;
180 1429 int is_prefix = 0, len;
181  
182 /* Empty or NULL string gets treated as "." */
183 1429 ▶ if (path == NULL || *path == '\0') {
Branch: 2 branches
BranchTrueFalseTotal
if (path == NULL || *path == '\0') { 1429 0 1429
if (path == NULL || *path == '\0') { 0 1429 1429
184 0 path = ".";
185 0 len = 1;
186 0 goto Exit;
187 }
188  
189 /* Strip trailing slashes */
190 1429 endp = path + strlen(path) - 1;
191 1490 ▶ while (endp > path && *endp == '/')
Branch: 2 branches
BranchTrueFalseTotal
while (endp > path && *endp == '/') 1490 0 1490
while (endp > path && *endp == '/') 61 1429 1490
192 61 endp--;
193  
194 1429 ▶ if (endp - path + 1 > INT_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (endp - path + 1 > INT_MAX) { 0 1429 1429
195 0 git_error_set(GIT_ERROR_INVALID, "path too long");
196 0 return -1;
197 }
198  
199 1429 ▶ if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) { 0 1429 1429
200 0 is_prefix = 1;
201 0 goto Exit;
202 }
203  
204 /* Find the start of the dir */
205 16935 ▶ while (endp > path && *endp != '/')
Branch: 2 branches
BranchTrueFalseTotal
while (endp > path && *endp != '/') 16749 186 16935
while (endp > path && *endp != '/') 15506 1243 16749
206 15506 endp--;
207  
208 /* Either the dir is "/" or there are no slashes */
209 1429 ▶ if (endp == path) {
Branch: 1 branch
BranchTrueFalseTotal
if (endp == path) { 186 1243 1429
210 186 ▶ path = (*endp == '/') ? "/" : ".";
Branch: 1 branch
BranchTrueFalseTotal
path = (*endp == '/') ? "/" : "."; 0 186 186
211 186 len = 1;
212 186 goto Exit;
213 }
214  
215 do {
216 1243 endp--;
217 1243 ▶ } while (endp > path && *endp == '/');
Branch: 2 branches
BranchTrueFalseTotal
} while (endp > path && *endp == '/'); 1243 0 1243
} while (endp > path && *endp == '/'); 0 1243 1243
218  
219 1243 ▶ if (endp - path + 1 > INT_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (endp - path + 1 > INT_MAX) { 0 1243 1243
220 0 git_error_set(GIT_ERROR_INVALID, "path too long");
221 0 return -1;
222 }
223  
224 1243 ▶ if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((len = win32_prefix_length(path, (int)(endp - path + 1))) > 0) { 0 1243 1243
225 0 is_prefix = 1;
226 0 goto Exit;
227 }
228  
229 /* Cast is safe because max path < max int */
230 1243 len = (int)(endp - path + 1);
231  
232 1429 Exit:
233 1429 ▶ if (buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (buffer) { 1429 0 1429
234 1429 ▶ if (git_str_set(buffer, path, len) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_set(buffer, path, len) < 0) 0 1429 1429
235 0 return -1;
236 1429 ▶ if (is_prefix && git_str_putc(buffer, '/') < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (is_prefix && git_str_putc(buffer, '/') < 0) 0 1429 1429
if (is_prefix && git_str_putc(buffer, '/') < 0) 0 0 0
237 0 return -1;
238 }
239  
240 1429 return len;
241 }
242  
243  
244 6 char *git_fs_path_dirname(const char *path)
245 {
246 6 git_str buf = GIT_STR_INIT;
247 char *dirname;
248  
249 6 git_fs_path_dirname_r(&buf, path);
250 6 dirname = git_str_detach(&buf);
251 6 git_str_dispose(&buf); /* avoid memleak if error occurs */
252  
253 6 return dirname;
254 }
255  
256 0 char *git_fs_path_basename(const char *path)
257 {
258 0 git_str buf = GIT_STR_INIT;
259 char *basename;
260  
261 0 git_fs_path_basename_r(&buf, path);
262 0 basename = git_str_detach(&buf);
263 0 git_str_dispose(&buf); /* avoid memleak if error occurs */
264  
265 0 return basename;
266 }
267  
268 0 size_t git_fs_path_basename_offset(git_str *buffer)
269 {
270 ssize_t slash;
271  
272 0 ▶ if (!buffer || buffer->size <= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!buffer || buffer->size <= 0) 0 0 0
if (!buffer || buffer->size <= 0) 0 0 0
273 0 return 0;
274  
275 0 slash = git_str_rfind_next(buffer, '/');
276  
277 0 ▶ if (slash >= 0 && buffer->ptr[slash] == '/')
Branch: 2 branches
BranchTrueFalseTotal
if (slash >= 0 && buffer->ptr[slash] == '/') 0 0 0
if (slash >= 0 && buffer->ptr[slash] == '/') 0 0 0
278 0 return (size_t)(slash + 1);
279  
280 0 return 0;
281 }
282  
283 10097 int git_fs_path_root(const char *path)
284 {
285 10097 int offset = 0, prefix_len;
286  
287 /* Does the root of the path look like a windows drive ? */
288 10097 ▶ if ((prefix_len = dos_drive_prefix_length(path)))
Branch: 1 branch
BranchTrueFalseTotal
if ((prefix_len = dos_drive_prefix_length(path))) 0 10097 10097
289 0 offset += prefix_len;
290  
291 #ifdef GIT_WIN32
292 /* Are we dealing with a windows network path? */
293 else if ((path[0] == '/' && path[1] == '/' && path[2] != '/') ||
294 (path[0] == '\\' && path[1] == '\\' && path[2] != '\\'))
295 {
296 offset += 2;
297  
298 /* Skip the computer name segment */
299 while (path[offset] && path[offset] != '/' && path[offset] != '\\')
300 offset++;
301 }
302  
303 if (path[offset] == '\\')
304 return offset;
305 #endif
306  
307 10097 ▶ if (path[offset] == '/')
Branch: 1 branch
BranchTrueFalseTotal
if (path[offset] == '/') 1562 8535 10097
308 1562 return offset;
309  
310 8535 return -1; /* Not a real error - signals that path is not rooted */
311 }
312  
313 452 static void path_trim_slashes(git_str *path)
314 {
315 452 int ceiling = git_fs_path_root(path->ptr) + 1;
316  
317 452 ▶ if (ceiling < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ceiling < 0) 0 452 452
318 0 return;
319  
320 904 ▶ while (path->size > (size_t)ceiling) {
Branch: 1 branch
BranchTrueFalseTotal
while (path->size > (size_t)ceiling) { 904 0 904
321 904 ▶ if (path->ptr[path->size-1] != '/')
Branch: 1 branch
BranchTrueFalseTotal
if (path->ptr[path->size-1] != '/') 452 452 904
322 452 break;
323  
324 452 path->ptr[path->size-1] = '\0';
325 452 path->size--;
326 }
327 }
328  
329 1815 int git_fs_path_join_unrooted(
330 git_str *path_out, const char *path, const char *base, ssize_t *root_at)
331 {
332 ssize_t root;
333  
334 1815 ▶ GIT_ASSERT_ARG(path_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path_out); 0 1815 1815
335 1815 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 1815 1815
336  
337 1815 root = (ssize_t)git_fs_path_root(path);
338  
339 1815 ▶ if (base != NULL && root < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (base != NULL && root < 0) { 1804 11 1815
if (base != NULL && root < 0) { 1753 51 1804
340 1753 ▶ if (git_str_joinpath(path_out, base, path) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_joinpath(path_out, base, path) < 0) 0 1753 1753
341 0 return -1;
342  
343 1753 root = (ssize_t)strlen(base);
344 } else {
345 62 ▶ if (git_str_sets(path_out, path) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_sets(path_out, path) < 0) 0 62 62
346 0 return -1;
347  
348 62 ▶ if (root < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (root < 0) 0 62 62
349 0 root = 0;
350 62 ▶ else if (base)
Branch: 1 branch
BranchTrueFalseTotal
else if (base) 51 11 62
351 51 git_fs_path_equal_or_prefixed(base, path, &root);
352 }
353  
354 1815 ▶ if (root_at)
Branch: 1 branch
BranchTrueFalseTotal
if (root_at) 1651 164 1815
355 1651 *root_at = root;
356  
357 1815 return 0;
358 }
359  
360 19 void git_fs_path_squash_slashes(git_str *path)
361 {
362 char *p, *q;
363  
364 19 ▶ if (path->size == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (path->size == 0) 0 19 19
365 0 return;
366  
367 287 ▶ for (p = path->ptr, q = path->ptr; *q; p++, q++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = path->ptr, q = path->ptr; *q; p++, q++) { 268 19 287
368 268 *p = *q;
369  
370 268 ▶ while (*q == '/' && *(q+1) == '/') {
Branch: 2 branches
BranchTrueFalseTotal
while (*q == '/' && *(q+1) == '/') { 27 241 268
while (*q == '/' && *(q+1) == '/') { 0 27 27
371 0 path->size--;
372 0 q++;
373 }
374 }
375  
376 19 *p = '\0';
377 }
378  
379 94 int git_fs_path_prettify(git_str *path_out, const char *path, const char *base)
380 {
381 char buf[GIT_PATH_MAX];
382  
383 94 ▶ GIT_ASSERT_ARG(path_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path_out); 0 94 94
384 94 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 94 94
385  
386 /* construct path if needed */
387 94 ▶ if (base != NULL && git_fs_path_root(path) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (base != NULL && git_fs_path_root(path) < 0) { 5 89 94
if (base != NULL && git_fs_path_root(path) < 0) { 0 5 5
388 0 ▶ if (git_str_joinpath(path_out, base, path) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_joinpath(path_out, base, path) < 0) 0 0 0
389 0 return -1;
390 0 path = path_out->ptr;
391 }
392  
393 94 ▶ if (p_realpath(path, buf) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_realpath(path, buf) == NULL) { 0 94 94
394 /* git_error_set resets the errno when dealing with a GIT_ERROR_OS kind of error */
395 0 ▶ int error = (errno == ENOENT || errno == ENOTDIR) ? GIT_ENOTFOUND : -1;
Branch: 2 branches
BranchTrueFalseTotal
int error = (errno == ENOENT || errno == ENOTDIR) ? GIT_ENOTFOUND : -1; 0 0 0
int error = (errno == ENOENT || errno == ENOTDIR) ? GIT_ENOTFOUND : -1; 0 0 0
396 0 git_error_set(GIT_ERROR_OS, "failed to resolve path '%s'", path);
397  
398 0 git_str_clear(path_out);
399  
400 0 return error;
401 }
402  
403 94 return git_str_sets(path_out, buf);
404 }
405  
406 30 int git_fs_path_prettify_dir(git_str *path_out, const char *path, const char *base)
407 {
408 30 int error = git_fs_path_prettify(path_out, path, base);
409 30 ▶ return (error < 0) ? error : git_fs_path_to_dir(path_out);
Branch: 1 branch
BranchTrueFalseTotal
return (error < 0) ? error : git_fs_path_to_dir(path_out); 30 0 30
410 }
411  
412 4620 int git_fs_path_to_dir(git_str *path)
413 {
414 9240 if (path->asize > 0 &&
415 4620 git_str_len(path) > 0 &&
416 4434 ▶ path->ptr[git_str_len(path) - 1] != '/')
Branch: 1 branch
BranchTrueFalseTotal
path->ptr[git_str_len(path) - 1] != '/') 2767 1667 4434
417 2767 git_str_putc(path, '/');
418  
419 4620 ▶ return git_str_oom(path) ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return git_str_oom(path) ? -1 : 0; 0 4620 4620
420 }
421  
422 0 void git_fs_path_string_to_dir(char *path, size_t size)
423 {
424 0 size_t end = strlen(path);
425  
426 0 ▶ if (end && path[end - 1] != '/' && end < size) {
Branch: 3 branches
BranchTrueFalseTotal
if (end && path[end - 1] != '/' && end < size) { 0 0 0
if (end && path[end - 1] != '/' && end < size) { 0 0 0
if (end && path[end - 1] != '/' && end < size) { 0 0 0
427 0 path[end] = '/';
428 0 path[end + 1] = '\0';
429 }
430 0 }
431  
432 3 int git__percent_decode(git_str *decoded_out, const char *input)
433 {
434 int len, hi, lo, i;
435  
436 3 ▶ GIT_ASSERT_ARG(decoded_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(decoded_out); 0 3 3
437 3 ▶ GIT_ASSERT_ARG(input);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(input); 0 3 3
438  
439 3 len = (int)strlen(input);
440 3 git_str_clear(decoded_out);
441  
442 183 ▶ for(i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < len; i++) 180 3 183
443 {
444 180 char c = input[i];
445  
446 180 ▶ if (c != '%')
Branch: 1 branch
BranchTrueFalseTotal
if (c != '%') 180 0 180
447 180 goto append;
448  
449 0 ▶ if (i >= len - 2)
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len - 2) 0 0 0
450 0 goto append;
451  
452 0 hi = git__fromhex(input[i + 1]);
453 0 lo = git__fromhex(input[i + 2]);
454  
455 0 ▶ if (hi < 0 || lo < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (hi < 0 || lo < 0) 0 0 0
if (hi < 0 || lo < 0) 0 0 0
456 0 goto append;
457  
458 0 c = (char)(hi << 4 | lo);
459 0 i += 2;
460  
461 180 append:
462 180 ▶ if (git_str_putc(decoded_out, c) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_putc(decoded_out, c) < 0) 0 180 180
463 0 return -1;
464 }
465  
466 3 return 0;
467 }
468  
469 0 static int error_invalid_local_file_uri(const char *uri)
470 {
471 0 git_error_set(GIT_ERROR_CONFIG, "'%s' is not a valid local file URI", uri);
472 0 return -1;
473 }
474  
475 9 static int local_file_url_prefixlen(const char *file_url)
476 {
477 9 int len = -1;
478  
479 9 ▶ if (git__prefixcmp(file_url, "file://") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git__prefixcmp(file_url, "file://") == 0) { 6 3 9
480 6 ▶ if (file_url[7] == '/')
Branch: 1 branch
BranchTrueFalseTotal
if (file_url[7] == '/') 6 0 6
481 6 len = 8;
482 0 ▶ else if (git__prefixcmp(file_url + 7, "localhost/") == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (git__prefixcmp(file_url + 7, "localhost/") == 0) 0 0 0
483 0 len = 17;
484 }
485  
486 9 return len;
487 }
488  
489 6 bool git_fs_path_is_local_file_url(const char *file_url)
490 {
491 6 return (local_file_url_prefixlen(file_url) > 0);
492 }
493  
494 3 int git_fs_path_fromurl(git_str *local_path_out, const char *file_url)
495 {
496 int offset;
497  
498 3 ▶ GIT_ASSERT_ARG(local_path_out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(local_path_out); 0 3 3
499 3 ▶ GIT_ASSERT_ARG(file_url);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(file_url); 0 3 3
500  
501 3 ▶ if ((offset = local_file_url_prefixlen(file_url)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if ((offset = local_file_url_prefixlen(file_url)) < 0 || 3 0 3
502 3 ▶ file_url[offset] == '\0' || file_url[offset] == '/')
Branch: 2 branches
BranchTrueFalseTotal
file_url[offset] == '\0' || file_url[offset] == '/') 3 0 3
file_url[offset] == '\0' || file_url[offset] == '/') 0 3 3
503 0 return error_invalid_local_file_uri(file_url);
504  
505 #ifndef GIT_WIN32
506 3 offset--; /* A *nix absolute path starts with a forward slash */
507 #endif
508  
509 3 git_str_clear(local_path_out);
510 3 return git__percent_decode(local_path_out, file_url + offset);
511 }
512  
513 861 int git_fs_path_walk_up(
514 git_str *path,
515 const char *ceiling,
516 int (*cb)(void *data, const char *),
517 void *data)
518 {
519 861 int error = 0;
520 git_str iter;
521 861 ssize_t stop = 0, scan;
522 861 char oldc = '\0';
523  
524 861 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 861 861
525 861 ▶ GIT_ASSERT_ARG(cb);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(cb); 0 861 861
526  
527 861 ▶ if (ceiling != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ceiling != NULL) { 861 0 861
528 861 ▶ if (git__prefixcmp(path->ptr, ceiling) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git__prefixcmp(path->ptr, ceiling) == 0) 861 0 861
529 861 stop = (ssize_t)strlen(ceiling);
530 else
531 0 stop = git_str_len(path);
532 }
533 861 scan = git_str_len(path);
534  
535 /* empty path: yield only once */
536 861 ▶ if (!scan) {
Branch: 1 branch
BranchTrueFalseTotal
if (!scan) { 0 861 861
537 0 error = cb(data, "");
538 0 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 0 0 0
539 0 ▶ ensure_error_set(error);
Branch: 3 branches
BranchTrueFalseTotal
ensure_error_set(error); 0 0 0
ensure_error_set(error); 0 0 0
ensure_error_set(error); 0 0 0
540 0 return error;
541 }
542  
543 861 iter.ptr = path->ptr;
544 861 iter.size = git_str_len(path);
545 861 iter.asize = path->asize;
546  
547 1850 ▶ while (scan >= stop) {
Branch: 1 branch
BranchTrueFalseTotal
while (scan >= stop) { 1015 835 1850
548 1015 error = cb(data, iter.ptr);
549 1015 iter.ptr[scan] = oldc;
550  
551 1015 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 26 989 1015
552 26 ▶ ensure_error_set(error);
Branch: 3 branches
BranchTrueFalseTotal
ensure_error_set(error); 12 14 26
ensure_error_set(error); 0 12 12
ensure_error_set(error); 0 14 14
553 26 break;
554 }
555  
556 989 scan = git_str_rfind_next(&iter, '/');
557 989 ▶ if (scan >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scan >= 0) { 989 0 989
558 989 scan++;
559 989 oldc = iter.ptr[scan];
560 989 iter.size = scan;
561 989 iter.ptr[scan] = '\0';
562 }
563 }
564  
565 861 ▶ if (scan >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scan >= 0) 861 0 861
566 861 iter.ptr[scan] = oldc;
567  
568 /* relative path: yield for the last component */
569 861 ▶ if (!error && stop == 0 && iter.ptr[0] != '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (!error && stop == 0 && iter.ptr[0] != '/') { 835 26 861
if (!error && stop == 0 && iter.ptr[0] != '/') { 0 835 835
if (!error && stop == 0 && iter.ptr[0] != '/') { 0 0 0
570 0 error = cb(data, "");
571 0 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 0 0 0
572 0 ▶ ensure_error_set(error);
Branch: 3 branches
BranchTrueFalseTotal
ensure_error_set(error); 0 0 0
ensure_error_set(error); 0 0 0
ensure_error_set(error); 0 0 0
573 }
574  
575 861 return error;
576 }
577  
578 3173 bool git_fs_path_exists(const char *path)
579 {
580 3173 ▶ GIT_ASSERT_ARG_WITH_RETVAL(path, false);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(path, false); 0 3173 3173
581 3173 return p_access(path, F_OK) == 0;
582 }
583  
584 2048 bool git_fs_path_isdir(const char *path)
585 {
586 struct stat st;
587 2048 ▶ if (p_stat(path, &st) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p_stat(path, &st) < 0) 339 1709 2048
588 339 return false;
589  
590 1709 return S_ISDIR(st.st_mode) != 0;
591 }
592  
593 690 bool git_fs_path_isfile(const char *path)
594 {
595 struct stat st;
596  
597 690 ▶ GIT_ASSERT_ARG_WITH_RETVAL(path, false);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(path, false); 0 690 690
598 690 ▶ if (p_stat(path, &st) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p_stat(path, &st) < 0) 473 217 690
599 473 return false;
600  
601 217 return S_ISREG(st.st_mode) != 0;
602 }
603  
604 0 bool git_fs_path_islink(const char *path)
605 {
606 struct stat st;
607  
608 0 ▶ GIT_ASSERT_ARG_WITH_RETVAL(path, false);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG_WITH_RETVAL(path, false); 0 0 0
609 0 ▶ if (p_lstat(path, &st) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p_lstat(path, &st) < 0) 0 0 0
610 0 return false;
611  
612 0 return S_ISLNK(st.st_mode) != 0;
613 }
614  
615 #ifdef GIT_WIN32
616  
617 bool git_fs_path_is_empty_dir(const char *path)
618 {
619 git_win32_path filter_w;
620 bool empty = false;
621  
622 if (git_win32__findfirstfile_filter(filter_w, path)) {
623 WIN32_FIND_DATAW findData;
624 HANDLE hFind = FindFirstFileW(filter_w, &findData);
625  
626 /* FindFirstFile will fail if there are no children to the given
627 * path, which can happen if the given path is a file (and obviously
628 * has no children) or if the given path is an empty mount point.
629 * (Most directories have at least directory entries '.' and '..',
630 * but ridiculously another volume mounted in another drive letter's
631 * path space do not, and thus have nothing to enumerate.) If
632 * FindFirstFile fails, check if this is a directory-like thing
633 * (a mount point).
634 */
635 if (hFind == INVALID_HANDLE_VALUE)
636 return git_fs_path_isdir(path);
637  
638 /* If the find handle was created successfully, then it's a directory */
639 empty = true;
640  
641 do {
642 /* Allow the enumeration to return . and .. and still be considered
643 * empty. In the special case of drive roots (i.e. C:\) where . and
644 * .. do not occur, we can still consider the path to be an empty
645 * directory if there's nothing there. */
646 if (!git_fs_path_is_dot_or_dotdotW(findData.cFileName)) {
647 empty = false;
648 break;
649 }
650 } while (FindNextFileW(hFind, &findData));
651  
652 FindClose(hFind);
653 }
654  
655 return empty;
656 }
657  
658 #else
659  
660 2 static int path_found_entry(void *payload, git_str *path)
661 {
662 2 GIT_UNUSED(payload);
663 2 return !git_fs_path_is_dot_or_dotdot(path->ptr);
664 }
665  
666 2 bool git_fs_path_is_empty_dir(const char *path)
667 {
668 int error;
669 2 git_str dir = GIT_STR_INIT;
670  
671 2 ▶ if (!git_fs_path_isdir(path))
Branch: 1 branch
BranchTrueFalseTotal
if (!git_fs_path_isdir(path)) 0 2 2
672 0 return false;
673  
674 2 ▶ if ((error = git_str_sets(&dir, path)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_sets(&dir, path)) != 0) 0 2 2
675 0 git_error_clear();
676 else
677 2 error = git_fs_path_direach(&dir, 0, path_found_entry, NULL);
678  
679 2 git_str_dispose(&dir);
680  
681 2 return !error;
682 }
683  
684 #endif
685  
686 783 int git_fs_path_set_error(int errno_value, const char *path, const char *action)
687 {
688 783 switch (errno_value) {
689 783 case ENOENT:
690 case ENOTDIR:
691 783 git_error_set(GIT_ERROR_OS, "could not find '%s' to %s", path, action);
692 783 return GIT_ENOTFOUND;
693  
694 0 case EINVAL:
695 case ENAMETOOLONG:
696 0 git_error_set(GIT_ERROR_OS, "invalid path for filesystem '%s'", path);
697 0 return GIT_EINVALIDSPEC;
698  
699 0 case EEXIST:
700 0 git_error_set(GIT_ERROR_OS, "failed %s - '%s' already exists", action, path);
701 0 return GIT_EEXISTS;
702  
703 0 case EACCES:
704 0 git_error_set(GIT_ERROR_OS, "failed %s - '%s' is locked", action, path);
705 0 return GIT_ELOCKED;
706  
707 0 default:
708 0 git_error_set(GIT_ERROR_OS, "could not %s '%s'", action, path);
709 0 return -1;
710 }
711 }
712  
713 1212 int git_fs_path_lstat(const char *path, struct stat *st)
714 {
715 1212 ▶ if (p_lstat(path, st) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p_lstat(path, st) == 0) 1212 0 1212
716 1212 return 0;
717  
718 0 return git_fs_path_set_error(errno, path, "stat");
719 }
720  
721 588 static bool _check_dir_contents(
722 git_str *dir,
723 const char *sub,
724 bool (*predicate)(const char *))
725 {
726 bool result;
727 588 size_t dir_size = git_str_len(dir);
728 588 size_t sub_size = strlen(sub);
729 size_t alloc_size;
730  
731 /* leave base valid even if we could not make space for subdir */
732 588 ▶ if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, dir_size, sub_size) ||
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, dir_size, sub_size) || 0 588 588
733 1176 ▶ GIT_ADD_SIZET_OVERFLOW(&alloc_size, alloc_size, 2) ||
Branch: 2 branches
BranchTrueFalseTotal
GIT_ADD_SIZET_OVERFLOW(&alloc_size, alloc_size, 2) || 0 588 588
GIT_ADD_SIZET_OVERFLOW(&alloc_size, alloc_size, 2) || 0 588 588
734 588 git_str_try_grow(dir, alloc_size, false) < 0)
735 0 return false;
736  
737 /* save excursion */
738 588 ▶ if (git_str_joinpath(dir, dir->ptr, sub) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_joinpath(dir, dir->ptr, sub) < 0) 0 588 588
739 0 return false;
740  
741 588 result = predicate(dir->ptr);
742  
743 /* restore path */
744 588 git_str_truncate(dir, dir_size);
745 588 return result;
746 }
747  
748 13 bool git_fs_path_contains(git_str *dir, const char *item)
749 {
750 13 return _check_dir_contents(dir, item, &git_fs_path_exists);
751 }
752  
753 195 bool git_fs_path_contains_dir(git_str *base, const char *subdir)
754 {
755 195 return _check_dir_contents(base, subdir, &git_fs_path_isdir);
756 }
757  
758 380 bool git_fs_path_contains_file(git_str *base, const char *file)
759 {
760 380 return _check_dir_contents(base, file, &git_fs_path_isfile);
761 }
762  
763 649 int git_fs_path_find_dir(git_str *dir)
764 {
765 649 int error = 0;
766 char buf[GIT_PATH_MAX];
767  
768 649 ▶ if (p_realpath(dir->ptr, buf) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (p_realpath(dir->ptr, buf) != NULL) 631 18 649
769 631 error = git_str_sets(dir, buf);
770  
771 /* call dirname if this is not a directory */
772 649 ▶ if (!error) /* && git_fs_path_isdir(dir->ptr) == false) */
Branch: 1 branch
BranchTrueFalseTotal
if (!error) /* && git_fs_path_isdir(dir->ptr) == false) */ 649 0 649
773 649 ▶ error = (git_fs_path_dirname_r(dir, dir->ptr) < 0) ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
error = (git_fs_path_dirname_r(dir, dir->ptr) < 0) ? -1 : 0; 0 649 649
774  
775 649 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 649 0 649
776 649 error = git_fs_path_to_dir(dir);
777  
778 649 return error;
779 }
780  
781 186 int git_fs_path_resolve_relative(git_str *path, size_t ceiling)
782 {
783 char *base, *to, *from, *next;
784 size_t len;
785  
786 186 ▶ GIT_ERROR_CHECK_ALLOC_STR(path);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_STR(path); 186 0 186
GIT_ERROR_CHECK_ALLOC_STR(path); 0 186 186
787  
788 186 ▶ if (ceiling > path->size)
Branch: 1 branch
BranchTrueFalseTotal
if (ceiling > path->size) 0 186 186
789 0 ceiling = path->size;
790  
791 /* recognize drive prefixes, etc. that should not be backed over */
792 186 ▶ if (ceiling == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ceiling == 0) 186 0 186
793 186 ceiling = git_fs_path_root(path->ptr) + 1;
794  
795 /* recognize URL prefixes that should not be backed over */
796 186 ▶ if (ceiling == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ceiling == 0) { 186 0 186
797 186 ▶ for (next = path->ptr; *next && git__isalpha(*next); ++next);
Branch: 2 branches
BranchTrueFalseTotal
for (next = path->ptr; *next && git__isalpha(*next); ++next); 186 0 186
for (next = path->ptr; *next && git__isalpha(*next); ++next); 0 186 186
798 186 ▶ if (next[0] == ':' && next[1] == '/' && next[2] == '/')
Branch: 3 branches
BranchTrueFalseTotal
if (next[0] == ':' && next[1] == '/' && next[2] == '/') 0 186 186
if (next[0] == ':' && next[1] == '/' && next[2] == '/') 0 0 0
if (next[0] == ':' && next[1] == '/' && next[2] == '/') 0 0 0
799 0 ceiling = (next + 3) - path->ptr;
800 }
801  
802 186 base = to = from = path->ptr + ceiling;
803  
804 372 ▶ while (*from) {
Branch: 1 branch
BranchTrueFalseTotal
while (*from) { 186 186 372
805 372 ▶ for (next = from; *next && *next != '/'; ++next);
Branch: 2 branches
BranchTrueFalseTotal
for (next = from; *next && *next != '/'; ++next); 186 186 372
for (next = from; *next && *next != '/'; ++next); 186 0 186
806  
807 186 len = next - from;
808  
809 186 ▶ if (len == 1 && from[0] == '.')
Branch: 2 branches
BranchTrueFalseTotal
if (len == 1 && from[0] == '.') 186 0 186
if (len == 1 && from[0] == '.') 186 0 186
810 /* do nothing with singleton dot */;
811  
812 0 ▶ else if (len == 2 && from[0] == '.' && from[1] == '.') {
Branch: 3 branches
BranchTrueFalseTotal
else if (len == 2 && from[0] == '.' && from[1] == '.') { 0 0 0
else if (len == 2 && from[0] == '.' && from[1] == '.') { 0 0 0
else if (len == 2 && from[0] == '.' && from[1] == '.') { 0 0 0
813 /* error out if trying to up one from a hard base */
814 0 ▶ if (to == base && ceiling != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (to == base && ceiling != 0) { 0 0 0
if (to == base && ceiling != 0) { 0 0 0
815 0 git_error_set(GIT_ERROR_INVALID,
816 "cannot strip root component off url");
817 0 return -1;
818 }
819  
820 /* no more path segments to strip,
821 * use '../' as a new base path */
822 0 ▶ if (to == base) {
Branch: 1 branch
BranchTrueFalseTotal
if (to == base) { 0 0 0
823 0 ▶ if (*next == '/')
Branch: 1 branch
BranchTrueFalseTotal
if (*next == '/') 0 0 0
824 0 len++;
825  
826 0 ▶ if (to != from)
Branch: 1 branch
BranchTrueFalseTotal
if (to != from) 0 0 0
827 0 memmove(to, from, len);
828  
829 0 to += len;
830 /* this is now the base, can't back up from a
831 * relative prefix */
832 0 base = to;
833 } else {
834 /* back up a path segment */
835 0 ▶ while (to > base && to[-1] == '/') to--;
Branch: 2 branches
BranchTrueFalseTotal
while (to > base && to[-1] == '/') to--; 0 0 0
while (to > base && to[-1] == '/') to--; 0 0 0
836 0 ▶ while (to > base && to[-1] != '/') to--;
Branch: 2 branches
BranchTrueFalseTotal
while (to > base && to[-1] != '/') to--; 0 0 0
while (to > base && to[-1] != '/') to--; 0 0 0
837 }
838 } else {
839 0 ▶ if (*next == '/' && *from != '/')
Branch: 2 branches
BranchTrueFalseTotal
if (*next == '/' && *from != '/') 0 0 0
if (*next == '/' && *from != '/') 0 0 0
840 0 len++;
841  
842 0 ▶ if (to != from)
Branch: 1 branch
BranchTrueFalseTotal
if (to != from) 0 0 0
843 0 memmove(to, from, len);
844  
845 0 to += len;
846 }
847  
848 186 from += len;
849  
850 186 ▶ while (*from == '/') from++;
Branch: 1 branch
BranchTrueFalseTotal
while (*from == '/') from++; 0 186 186
851 }
852  
853 186 *to = '\0';
854  
855 186 path->size = to - path->ptr;
856  
857 186 return 0;
858 }
859  
860 0 int git_fs_path_apply_relative(git_str *target, const char *relpath)
861 {
862 0 return git_str_joinpath(target, git_str_cstr(target), relpath) ||
863 0 git_fs_path_resolve_relative(target, 0);
864 }
865  
866 220 int git_fs_path_cmp(
867 const char *name1, size_t len1, int isdir1,
868 const char *name2, size_t len2, int isdir2,
869 int (*compare)(const char *, const char *, size_t))
870 {
871 unsigned char c1, c2;
872 220 size_t len = len1 < len2 ? len1 : len2;
873 int cmp;
874  
875 220 cmp = compare(name1, name2, len);
876 220 ▶ if (cmp)
Branch: 1 branch
BranchTrueFalseTotal
if (cmp) 137 83 220
877 137 return cmp;
878  
879 83 c1 = name1[len];
880 83 c2 = name2[len];
881  
882 83 ▶ if (c1 == '\0' && isdir1)
Branch: 2 branches
BranchTrueFalseTotal
if (c1 == '\0' && isdir1) 34 49 83
if (c1 == '\0' && isdir1) 1 33 34
883 1 c1 = '/';
884  
885 83 ▶ if (c2 == '\0' && isdir2)
Branch: 2 branches
BranchTrueFalseTotal
if (c2 == '\0' && isdir2) 49 34 83
if (c2 == '\0' && isdir2) 2 47 49
886 2 c2 = '/';
887  
888 83 ▶ return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; 48 35 83
889 }
890  
891 22 size_t git_fs_path_common_dirlen(const char *one, const char *two)
892 {
893 22 const char *p, *q, *dirsep = NULL;
894  
895 174 ▶ for (p = one, q = two; *p && *q; p++, q++) {
Branch: 2 branches
BranchTrueFalseTotal
for (p = one, q = two; *p && *q; p++, q++) { 166 8 174
for (p = one, q = two; *p && *q; p++, q++) { 166 0 166
896 166 ▶ if (*p == '/' && *q == '/')
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '/' && *q == '/') 28 138 166
if (*p == '/' && *q == '/') 28 0 28
897 28 dirsep = p;
898 138 ▶ else if (*p != *q)
Branch: 1 branch
BranchTrueFalseTotal
else if (*p != *q) 14 124 138
899 14 break;
900 }
901  
902 22 ▶ return dirsep ? (dirsep - one) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return dirsep ? (dirsep - one) + 1 : 0; 21 1 22
903 }
904  
905 0 int git_fs_path_make_relative(git_str *path, const char *parent)
906 {
907 const char *p, *q, *p_dirsep, *q_dirsep;
908 0 size_t plen = path->size, newlen, alloclen, depth = 1, i, offset;
909  
910 0 ▶ for (p_dirsep = p = path->ptr, q_dirsep = q = parent; *p && *q; p++, q++) {
Branch: 2 branches
BranchTrueFalseTotal
for (p_dirsep = p = path->ptr, q_dirsep = q = parent; *p && *q; p++, q++) { 0 0 0
for (p_dirsep = p = path->ptr, q_dirsep = q = parent; *p && *q; p++, q++) { 0 0 0
911 0 ▶ if (*p == '/' && *q == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '/' && *q == '/') { 0 0 0
if (*p == '/' && *q == '/') { 0 0 0
912 0 p_dirsep = p;
913 0 q_dirsep = q;
914 }
915 0 ▶ else if (*p != *q)
Branch: 1 branch
BranchTrueFalseTotal
else if (*p != *q) 0 0 0
916 0 break;
917 }
918  
919 /* need at least 1 common path segment */
920 0 ▶ if ((p_dirsep == path->ptr || q_dirsep == parent) &&
Branch: 2 branches
BranchTrueFalseTotal
if ((p_dirsep == path->ptr || q_dirsep == parent) && 0 0 0
if ((p_dirsep == path->ptr || q_dirsep == parent) && 0 0 0
921 0 ▶ (*p_dirsep != '/' || *q_dirsep != '/')) {
Branch: 2 branches
BranchTrueFalseTotal
(*p_dirsep != '/' || *q_dirsep != '/')) { 0 0 0
(*p_dirsep != '/' || *q_dirsep != '/')) { 0 0 0
922 0 git_error_set(GIT_ERROR_INVALID,
923 "%s is not a parent of %s", parent, path->ptr);
924 0 return GIT_ENOTFOUND;
925 }
926  
927 0 ▶ if (*p == '/' && !*q)
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '/' && !*q) 0 0 0
if (*p == '/' && !*q) 0 0 0
928 0 p++;
929 0 ▶ else if (!*p && *q == '/')
Branch: 2 branches
BranchTrueFalseTotal
else if (!*p && *q == '/') 0 0 0
else if (!*p && *q == '/') 0 0 0
930 0 q++;
931 0 ▶ else if (!*p && !*q)
Branch: 2 branches
BranchTrueFalseTotal
else if (!*p && !*q) 0 0 0
else if (!*p && !*q) 0 0 0
932 0 return git_str_clear(path), 0;
933 else {
934 0 p = p_dirsep + 1;
935 0 q = q_dirsep + 1;
936 }
937  
938 0 plen -= (p - path->ptr);
939  
940 0 ▶ if (!*q)
Branch: 1 branch
BranchTrueFalseTotal
if (!*q) 0 0 0
941 0 return git_str_set(path, p, plen);
942  
943 0 ▶ for (; (q = strchr(q, '/')) && *(q + 1); q++)
Branch: 2 branches
BranchTrueFalseTotal
for (; (q = strchr(q, '/')) && *(q + 1); q++) 0 0 0
for (; (q = strchr(q, '/')) && *(q + 1); q++) 0 0 0
944 0 depth++;
945  
946 0 ▶ GIT_ERROR_CHECK_ALLOC_MULTIPLY(&newlen, depth, 3);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_MULTIPLY(&newlen, depth, 3); 0 0 0
GIT_ERROR_CHECK_ALLOC_MULTIPLY(&newlen, depth, 3); 0 0 0
947 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&newlen, newlen, plen);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&newlen, newlen, plen); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&newlen, newlen, plen); 0 0 0
948  
949 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, newlen, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, newlen, 1); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, newlen, 1); 0 0 0
950  
951 /* save the offset as we might realllocate the pointer */
952 0 offset = p - path->ptr;
953 0 ▶ if (git_str_try_grow(path, alloclen, 1) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_try_grow(path, alloclen, 1) < 0) 0 0 0
954 0 return -1;
955 0 p = path->ptr + offset;
956  
957 0 memmove(path->ptr + (depth * 3), p, plen + 1);
958  
959 0 ▶ for (i = 0; i < depth; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < depth; i++) 0 0 0
960 0 memcpy(path->ptr + (i * 3), "../", 3);
961  
962 0 path->size = newlen;
963 0 return 0;
964 }
965  
966 0 bool git_fs_path_has_non_ascii(const char *path, size_t pathlen)
967 {
968 0 const uint8_t *scan = (const uint8_t *)path, *end;
969  
970 0 ▶ for (end = scan + pathlen; scan < end; ++scan)
Branch: 1 branch
BranchTrueFalseTotal
for (end = scan + pathlen; scan < end; ++scan) 0 0 0
971 0 ▶ if (*scan & 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (*scan & 0x80) 0 0 0
972 0 return true;
973  
974 0 return false;
975 }
976  
977 #ifdef GIT_USE_ICONV
978  
979 int git_fs_path_iconv_init_precompose(git_fs_path_iconv_t *ic)
980 {
981 git_str_init(&ic->buf, 0);
982 ic->map = iconv_open(GIT_PATH_REPO_ENCODING, GIT_PATH_NATIVE_ENCODING);
983 return 0;
984 }
985  
986 void git_fs_path_iconv_clear(git_fs_path_iconv_t *ic)
987 {
988 if (ic) {
989 if (ic->map != (iconv_t)-1)
990 iconv_close(ic->map);
991 git_str_dispose(&ic->buf);
992 }
993 }
994  
995 int git_fs_path_iconv(git_fs_path_iconv_t *ic, const char **in, size_t *inlen)
996 {
997 char *nfd = (char*)*in, *nfc;
998 size_t nfdlen = *inlen, nfclen, wantlen = nfdlen, alloclen, rv;
999 int retry = 1;
1000  
1001 if (!ic || ic->map == (iconv_t)-1 ||
1002 !git_fs_path_has_non_ascii(*in, *inlen))
1003 return 0;
1004  
1005 git_str_clear(&ic->buf);
1006  
1007 while (1) {
1008 GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, wantlen, 1);
1009 if (git_str_grow(&ic->buf, alloclen) < 0)
1010 return -1;
1011  
1012 nfc = ic->buf.ptr + ic->buf.size;
1013 nfclen = ic->buf.asize - ic->buf.size;
1014  
1015 rv = iconv(ic->map, &nfd, &nfdlen, &nfc, &nfclen);
1016  
1017 ic->buf.size = (nfc - ic->buf.ptr);
1018  
1019 if (rv != (size_t)-1)
1020 break;
1021  
1022 /* if we cannot convert the data (probably because iconv thinks
1023 * it is not valid UTF-8 source data), then use original data
1024 */
1025 if (errno != E2BIG)
1026 return 0;
1027  
1028 /* make space for 2x the remaining data to be converted
1029 * (with per retry overhead to avoid infinite loops)
1030 */
1031 wantlen = ic->buf.size + max(nfclen, nfdlen) * 2 + (size_t)(retry * 4);
1032  
1033 if (retry++ > 4)
1034 goto fail;
1035 }
1036  
1037 ic->buf.ptr[ic->buf.size] = '\0';
1038  
1039 *in = ic->buf.ptr;
1040 *inlen = ic->buf.size;
1041  
1042 return 0;
1043  
1044 fail:
1045 git_error_set(GIT_ERROR_OS, "unable to convert unicode path data");
1046 return -1;
1047 }
1048  
1049 static const char *nfc_file = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D";
1050 static const char *nfd_file = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
1051  
1052 /* Check if the platform is decomposing unicode data for us. We will
1053 * emulate core Git and prefer to use precomposed unicode data internally
1054 * on these platforms, composing the decomposed unicode on the fly.
1055 *
1056 * This mainly happens on the Mac where HDFS stores filenames as
1057 * decomposed unicode. Even on VFAT and SAMBA file systems, the Mac will
1058 * return decomposed unicode from readdir() even when the actual
1059 * filesystem is storing precomposed unicode.
1060 */
1061 bool git_fs_path_does_decompose_unicode(const char *root)
1062 {
1063 git_str nfc_path = GIT_STR_INIT;
1064 git_str nfd_path = GIT_STR_INIT;
1065 int fd;
1066 bool found_decomposed = false;
1067 size_t orig_len;
1068 const char *trailer;
1069  
1070 /* Create a file using a precomposed path and then try to find it
1071 * using the decomposed name. If the lookup fails, then we will mark
1072 * that we should precompose unicode for this repository.
1073 */
1074 if (git_str_joinpath(&nfc_path, root, nfc_file) < 0)
1075 goto done;
1076  
1077 /* record original path length before trailer */
1078 orig_len = nfc_path.size;
1079  
1080 if ((fd = git_futils_mktmp(&nfc_path, nfc_path.ptr, 0666)) < 0)
1081 goto done;
1082 p_close(fd);
1083  
1084 trailer = nfc_path.ptr + orig_len;
1085  
1086 /* try to look up as NFD path */
1087 if (git_str_joinpath(&nfd_path, root, nfd_file) < 0 ||
1088 git_str_puts(&nfd_path, trailer) < 0)
1089 goto done;
1090  
1091 found_decomposed = git_fs_path_exists(nfd_path.ptr);
1092  
1093 /* remove temporary file (using original precomposed path) */
1094 (void)p_unlink(nfc_path.ptr);
1095  
1096 done:
1097 git_str_dispose(&nfc_path);
1098 git_str_dispose(&nfd_path);
1099 return found_decomposed;
1100 }
1101  
1102 #else
1103  
1104 0 bool git_fs_path_does_decompose_unicode(const char *root)
1105 {
1106 0 GIT_UNUSED(root);
1107 0 return false;
1108 }
1109  
1110 #endif
1111  
1112 #if defined(__sun) || defined(__GNU__)
1113 typedef char path_dirent_data[sizeof(struct dirent) + FILENAME_MAX + 1];
1114 #else
1115 typedef struct dirent path_dirent_data;
1116 #endif
1117  
1118 335 int git_fs_path_direach(
1119 git_str *path,
1120 uint32_t flags,
1121 int (*fn)(void *, git_str *),
1122 void *arg)
1123 {
1124 335 int error = 0;
1125 ssize_t wd_len;
1126 DIR *dir;
1127 struct dirent *de;
1128  
1129 #ifdef GIT_USE_ICONV
1130 git_fs_path_iconv_t ic = GIT_PATH_ICONV_INIT;
1131 #endif
1132  
1133 335 GIT_UNUSED(flags);
1134  
1135 335 ▶ if (git_fs_path_to_dir(path) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_fs_path_to_dir(path) < 0) 0 335 335
1136 0 return -1;
1137  
1138 335 wd_len = git_str_len(path);
1139  
1140 335 ▶ if ((dir = opendir(path->ptr)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((dir = opendir(path->ptr)) == NULL) { 0 335 335
1141 0 git_error_set(GIT_ERROR_OS, "failed to open directory '%s'", path->ptr);
1142 0 ▶ if (errno == ENOENT)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == ENOENT) 0 0 0
1143 0 return GIT_ENOTFOUND;
1144  
1145 0 return -1;
1146 }
1147  
1148 #ifdef GIT_USE_ICONV
1149 if ((flags & GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
1150 (void)git_fs_path_iconv_init_precompose(&ic);
1151 #endif
1152  
1153 1274 ▶ while ((de = readdir(dir)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((de = readdir(dir)) != NULL) { 941 333 1274
1154 941 const char *de_path = de->d_name;
1155 941 size_t de_len = strlen(de_path);
1156  
1157 941 ▶ if (git_fs_path_is_dot_or_dotdot(de_path))
Branch: 1 branch
BranchTrueFalseTotal
if (git_fs_path_is_dot_or_dotdot(de_path)) 670 271 941
1158 670 continue;
1159  
1160 #ifdef GIT_USE_ICONV
1161 if ((error = git_fs_path_iconv(&ic, &de_path, &de_len)) < 0)
1162 break;
1163 #endif
1164  
1165 271 ▶ if ((error = git_str_put(path, de_path, de_len)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_put(path, de_path, de_len)) < 0) 0 271 271
1166 0 break;
1167  
1168 271 git_error_clear();
1169 271 error = fn(arg, path);
1170  
1171 271 git_str_truncate(path, wd_len); /* restore path */
1172  
1173 /* Only set our own error if the callback did not set one already */
1174 271 ▶ if (error != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (error != 0) { 2 269 271
1175 2 ▶ if (!git_error_last())
Branch: 1 branch
BranchTrueFalseTotal
if (!git_error_last()) 2 0 2
1176 2 ▶ ensure_error_set(error);
Branch: 3 branches
BranchTrueFalseTotal
ensure_error_set(error); 0 2 2
ensure_error_set(error); 0 0 0
ensure_error_set(error); 0 2 2
1177  
1178 2 break;
1179 }
1180 }
1181  
1182 335 closedir(dir);
1183  
1184 #ifdef GIT_USE_ICONV
1185 git_fs_path_iconv_clear(&ic);
1186 #endif
1187  
1188 335 return error;
1189 }
1190  
1191 #if defined(GIT_WIN32) && !defined(__MINGW32__)
1192  
1193 /* Using _FIND_FIRST_EX_LARGE_FETCH may increase performance in Windows 7
1194 * and better.
1195 */
1196 #ifndef FIND_FIRST_EX_LARGE_FETCH
1197 # define FIND_FIRST_EX_LARGE_FETCH 2
1198 #endif
1199  
1200 int git_fs_path_diriter_init(
1201 git_fs_path_diriter *diriter,
1202 const char *path,
1203 unsigned int flags)
1204 {
1205 git_win32_path path_filter;
1206  
1207 static int is_win7_or_later = -1;
1208 if (is_win7_or_later < 0)
1209 is_win7_or_later = git_has_win32_version(6, 1, 0);
1210  
1211 GIT_ASSERT_ARG(diriter);
1212 GIT_ASSERT_ARG(path);
1213  
1214 memset(diriter, 0, sizeof(git_fs_path_diriter));
1215 diriter->handle = INVALID_HANDLE_VALUE;
1216  
1217 if (git_str_puts(&diriter->path_utf8, path) < 0)
1218 return -1;
1219  
1220 path_trim_slashes(&diriter->path_utf8);
1221  
1222 if (diriter->path_utf8.size == 0) {
1223 git_error_set(GIT_ERROR_FILESYSTEM, "could not open directory '%s'", path);
1224 return -1;
1225 }
1226  
1227 if ((diriter->parent_len = git_win32_path_from_utf8(diriter->path, diriter->path_utf8.ptr)) < 0 ||
1228 !git_win32__findfirstfile_filter(path_filter, diriter->path_utf8.ptr)) {
1229 git_error_set(GIT_ERROR_OS, "could not parse the directory path '%s'", path);
1230 return -1;
1231 }
1232  
1233 diriter->handle = FindFirstFileExW(
1234 path_filter,
1235 is_win7_or_later ? FindExInfoBasic : FindExInfoStandard,
1236 &diriter->current,
1237 FindExSearchNameMatch,
1238 NULL,
1239 is_win7_or_later ? FIND_FIRST_EX_LARGE_FETCH : 0);
1240  
1241 if (diriter->handle == INVALID_HANDLE_VALUE) {
1242 git_error_set(GIT_ERROR_OS, "could not open directory '%s'", path);
1243 return -1;
1244 }
1245  
1246 diriter->parent_utf8_len = diriter->path_utf8.size;
1247 diriter->flags = flags;
1248 return 0;
1249 }
1250  
1251 static int diriter_update_paths(git_fs_path_diriter *diriter)
1252 {
1253 size_t filename_len, path_len;
1254  
1255 filename_len = wcslen(diriter->current.cFileName);
1256  
1257 if (GIT_ADD_SIZET_OVERFLOW(&path_len, diriter->parent_len, filename_len) ||
1258 GIT_ADD_SIZET_OVERFLOW(&path_len, path_len, 2))
1259 return -1;
1260  
1261 if (path_len > GIT_WIN_PATH_UTF16) {
1262 git_error_set(GIT_ERROR_FILESYSTEM,
1263 "invalid path '%.*ls\\%ls' (path too long)",
1264 diriter->parent_len, diriter->path, diriter->current.cFileName);
1265 return -1;
1266 }
1267  
1268 diriter->path[diriter->parent_len] = L'\\';
1269 memcpy(&diriter->path[diriter->parent_len+1],
1270 diriter->current.cFileName, filename_len * sizeof(wchar_t));
1271 diriter->path[path_len-1] = L'\0';
1272  
1273 git_str_truncate(&diriter->path_utf8, diriter->parent_utf8_len);
1274  
1275 if (diriter->parent_utf8_len > 0 &&
1276 diriter->path_utf8.ptr[diriter->parent_utf8_len-1] != '/')
1277 git_str_putc(&diriter->path_utf8, '/');
1278  
1279 git_str_put_w(&diriter->path_utf8, diriter->current.cFileName, filename_len);
1280  
1281 if (git_str_oom(&diriter->path_utf8))
1282 return -1;
1283  
1284 return 0;
1285 }
1286  
1287 int git_fs_path_diriter_next(git_fs_path_diriter *diriter)
1288 {
1289 bool skip_dot = !(diriter->flags & GIT_FS_PATH_DIR_INCLUDE_DOT_AND_DOTDOT);
1290  
1291 do {
1292 /* Our first time through, we already have the data from
1293 * FindFirstFileW. Use it, otherwise get the next file.
1294 */
1295 if (!diriter->needs_next)
1296 diriter->needs_next = 1;
1297 else if (!FindNextFileW(diriter->handle, &diriter->current))
1298 return GIT_ITEROVER;
1299 } while (skip_dot && git_fs_path_is_dot_or_dotdotW(diriter->current.cFileName));
1300  
1301 if (diriter_update_paths(diriter) < 0)
1302 return -1;
1303  
1304 return 0;
1305 }
1306  
1307 int git_fs_path_diriter_filename(
1308 const char **out,
1309 size_t *out_len,
1310 git_fs_path_diriter *diriter)
1311 {
1312 GIT_ASSERT_ARG(out);
1313 GIT_ASSERT_ARG(out_len);
1314 GIT_ASSERT_ARG(diriter);
1315 GIT_ASSERT(diriter->path_utf8.size > diriter->parent_utf8_len);
1316  
1317 *out = &diriter->path_utf8.ptr[diriter->parent_utf8_len+1];
1318 *out_len = diriter->path_utf8.size - diriter->parent_utf8_len - 1;
1319 return 0;
1320 }
1321  
1322 int git_fs_path_diriter_fullpath(
1323 const char **out,
1324 size_t *out_len,
1325 git_fs_path_diriter *diriter)
1326 {
1327 GIT_ASSERT_ARG(out);
1328 GIT_ASSERT_ARG(out_len);
1329 GIT_ASSERT_ARG(diriter);
1330  
1331 *out = diriter->path_utf8.ptr;
1332 *out_len = diriter->path_utf8.size;
1333 return 0;
1334 }
1335  
1336 int git_fs_path_diriter_stat(struct stat *out, git_fs_path_diriter *diriter)
1337 {
1338 GIT_ASSERT_ARG(out);
1339 GIT_ASSERT_ARG(diriter);
1340  
1341 return git_win32__file_attribute_to_stat(out,
1342 (WIN32_FILE_ATTRIBUTE_DATA *)&diriter->current,
1343 diriter->path);
1344 }
1345  
1346 void git_fs_path_diriter_free(git_fs_path_diriter *diriter)
1347 {
1348 if (diriter == NULL)
1349 return;
1350  
1351 git_str_dispose(&diriter->path_utf8);
1352  
1353 if (diriter->handle != INVALID_HANDLE_VALUE) {
1354 FindClose(diriter->handle);
1355 diriter->handle = INVALID_HANDLE_VALUE;
1356 }
1357 }
1358  
1359 #else
1360  
1361 452 int git_fs_path_diriter_init(
1362 git_fs_path_diriter *diriter,
1363 const char *path,
1364 unsigned int flags)
1365 {
1366 452 ▶ GIT_ASSERT_ARG(diriter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(diriter); 0 452 452
1367 452 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 452 452
1368  
1369 452 memset(diriter, 0, sizeof(git_fs_path_diriter));
1370  
1371 452 ▶ if (git_str_puts(&diriter->path, path) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_puts(&diriter->path, path) < 0) 0 452 452
1372 0 return -1;
1373  
1374 452 path_trim_slashes(&diriter->path);
1375  
1376 452 ▶ if (diriter->path.size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (diriter->path.size == 0) { 0 452 452
1377 0 git_error_set(GIT_ERROR_FILESYSTEM, "could not open directory '%s'", path);
1378 0 return -1;
1379 }
1380  
1381 452 ▶ if ((diriter->dir = opendir(diriter->path.ptr)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((diriter->dir = opendir(diriter->path.ptr)) == NULL) { 2 450 452
1382 2 git_str_dispose(&diriter->path);
1383  
1384 2 git_error_set(GIT_ERROR_OS, "failed to open directory '%s'", path);
1385 2 return -1;
1386 }
1387  
1388 #ifdef GIT_USE_ICONV
1389 if ((flags & GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
1390 (void)git_fs_path_iconv_init_precompose(&diriter->ic);
1391 #endif
1392  
1393 450 diriter->parent_len = diriter->path.size;
1394 450 diriter->flags = flags;
1395  
1396 450 return 0;
1397 }
1398  
1399 1683 int git_fs_path_diriter_next(git_fs_path_diriter *diriter)
1400 {
1401 struct dirent *de;
1402 const char *filename;
1403 size_t filename_len;
1404 1683 bool skip_dot = !(diriter->flags & GIT_FS_PATH_DIR_INCLUDE_DOT_AND_DOTDOT);
1405 1683 int error = 0;
1406  
1407 1683 ▶ GIT_ASSERT_ARG(diriter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(diriter); 0 1683 1683
1408  
1409 1683 errno = 0;
1410  
1411 do {
1412 2583 ▶ if ((de = readdir(diriter->dir)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((de = readdir(diriter->dir)) == NULL) { 450 2133 2583
1413 450 ▶ if (!errno)
Branch: 1 branch
BranchTrueFalseTotal
if (!errno) 450 0 450
1414 450 return GIT_ITEROVER;
1415  
1416 0 git_error_set(GIT_ERROR_OS,
1417 "could not read directory '%s'", diriter->path.ptr);
1418 0 return -1;
1419 }
1420 2133 ▶ } while (skip_dot && git_fs_path_is_dot_or_dotdot(de->d_name));
Branch: 2 branches
BranchTrueFalseTotal
} while (skip_dot && git_fs_path_is_dot_or_dotdot(de->d_name)); 2133 0 2133
} while (skip_dot && git_fs_path_is_dot_or_dotdot(de->d_name)); 900 1233 2133
1421  
1422 1233 filename = de->d_name;
1423 1233 filename_len = strlen(filename);
1424  
1425 #ifdef GIT_USE_ICONV
1426 if ((diriter->flags & GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE) != 0 &&
1427 (error = git_fs_path_iconv(&diriter->ic, &filename, &filename_len)) < 0)
1428 return error;
1429 #endif
1430  
1431 1233 git_str_truncate(&diriter->path, diriter->parent_len);
1432  
1433 1233 ▶ if (diriter->parent_len > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (diriter->parent_len > 0 && 1233 0 1233
1434 1233 ▶ diriter->path.ptr[diriter->parent_len-1] != '/')
Branch: 1 branch
BranchTrueFalseTotal
diriter->path.ptr[diriter->parent_len-1] != '/') 1233 0 1233
1435 1233 git_str_putc(&diriter->path, '/');
1436  
1437 1233 git_str_put(&diriter->path, filename, filename_len);
1438  
1439 1233 ▶ if (git_str_oom(&diriter->path))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(&diriter->path)) 0 1233 1233
1440 0 return -1;
1441  
1442 1233 return error;
1443 }
1444  
1445 0 int git_fs_path_diriter_filename(
1446 const char **out,
1447 size_t *out_len,
1448 git_fs_path_diriter *diriter)
1449 {
1450 0 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 0 0
1451 0 ▶ GIT_ASSERT_ARG(out_len);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out_len); 0 0 0
1452 0 ▶ GIT_ASSERT_ARG(diriter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(diriter); 0 0 0
1453 0 ▶ GIT_ASSERT(diriter->path.size > diriter->parent_len);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(diriter->path.size > diriter->parent_len); 0 0 0
1454  
1455 0 *out = &diriter->path.ptr[diriter->parent_len+1];
1456 0 *out_len = diriter->path.size - diriter->parent_len - 1;
1457 0 return 0;
1458 }
1459  
1460 1233 int git_fs_path_diriter_fullpath(
1461 const char **out,
1462 size_t *out_len,
1463 git_fs_path_diriter *diriter)
1464 {
1465 1233 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 1233 1233
1466 1233 ▶ GIT_ASSERT_ARG(out_len);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out_len); 0 1233 1233
1467 1233 ▶ GIT_ASSERT_ARG(diriter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(diriter); 0 1233 1233
1468  
1469 1233 *out = diriter->path.ptr;
1470 1233 *out_len = diriter->path.size;
1471 1233 return 0;
1472 }
1473  
1474 1096 int git_fs_path_diriter_stat(struct stat *out, git_fs_path_diriter *diriter)
1475 {
1476 1096 ▶ GIT_ASSERT_ARG(out);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(out); 0 1096 1096
1477 1096 ▶ GIT_ASSERT_ARG(diriter);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(diriter); 0 1096 1096
1478  
1479 1096 return git_fs_path_lstat(diriter->path.ptr, out);
1480 }
1481  
1482 452 void git_fs_path_diriter_free(git_fs_path_diriter *diriter)
1483 {
1484 452 ▶ if (diriter == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (diriter == NULL) 0 452 452
1485 0 return;
1486  
1487 452 ▶ if (diriter->dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (diriter->dir) { 450 2 452
1488 450 closedir(diriter->dir);
1489 450 diriter->dir = NULL;
1490 }
1491  
1492 #ifdef GIT_USE_ICONV
1493 git_fs_path_iconv_clear(&diriter->ic);
1494 #endif
1495  
1496 452 git_str_dispose(&diriter->path);
1497 }
1498  
1499 #endif
1500  
1501 2 int git_fs_path_dirload(
1502 git_vector *contents,
1503 const char *path,
1504 size_t prefix_len,
1505 uint32_t flags)
1506 {
1507 2 git_fs_path_diriter iter = GIT_FS_PATH_DIRITER_INIT;
1508 const char *name;
1509 size_t name_len;
1510 char *dup;
1511 int error;
1512  
1513 2 ▶ GIT_ASSERT_ARG(contents);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(contents); 0 2 2
1514 2 ▶ GIT_ASSERT_ARG(path);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(path); 0 2 2
1515  
1516 2 ▶ if ((error = git_fs_path_diriter_init(&iter, path, flags)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_fs_path_diriter_init(&iter, path, flags)) < 0) 0 2 2
1517 0 return error;
1518  
1519 6 ▶ while ((error = git_fs_path_diriter_next(&iter)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((error = git_fs_path_diriter_next(&iter)) == 0) { 4 2 6
1520 4 ▶ if ((error = git_fs_path_diriter_fullpath(&name, &name_len, &iter)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_fs_path_diriter_fullpath(&name, &name_len, &iter)) < 0) 0 4 4
1521 0 break;
1522  
1523 4 ▶ GIT_ASSERT(name_len > prefix_len);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(name_len > prefix_len); 0 4 4
1524  
1525 4 dup = git__strndup(name + prefix_len, name_len - prefix_len);
1526 4 ▶ GIT_ERROR_CHECK_ALLOC(dup);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(dup); 0 4 4
1527  
1528 4 ▶ if ((error = git_vector_insert(contents, dup)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_insert(contents, dup)) < 0) 0 4 4
1529 0 break;
1530 }
1531  
1532 2 ▶ if (error == GIT_ITEROVER)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ITEROVER) 2 0 2
1533 2 error = 0;
1534  
1535 2 git_fs_path_diriter_free(&iter);
1536 2 return error;
1537 }
1538  
1539 6 int git_fs_path_from_url_or_path(git_str *local_path_out, const char *url_or_path)
1540 {
1541 6 ▶ if (git_fs_path_is_local_file_url(url_or_path))
Branch: 1 branch
BranchTrueFalseTotal
if (git_fs_path_is_local_file_url(url_or_path)) 3 3 6
1542 3 return git_fs_path_fromurl(local_path_out, url_or_path);
1543 else
1544 3 return git_str_sets(local_path_out, url_or_path);
1545 }
1546  
1547 /* Reject paths like AUX or COM1, or those versions that end in a dot or
1548 * colon. ("AUX." or "AUX:")
1549 */
1550 0 GIT_INLINE(bool) validate_dospath(
1551 const char *component,
1552 size_t len,
1553 const char dospath[3],
1554 bool trailing_num)
1555 {
1556 0 ▶ size_t last = trailing_num ? 4 : 3;
Branch: 1 branch
BranchTrueFalseTotal
size_t last = trailing_num ? 4 : 3; 0 0 0
1557  
1558 0 ▶ if (len < last || git__strncasecmp(component, dospath, 3) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (len < last || git__strncasecmp(component, dospath, 3) != 0) 0 0 0
if (len < last || git__strncasecmp(component, dospath, 3) != 0) 0 0 0
1559 0 return true;
1560  
1561 0 ▶ if (trailing_num && (component[3] < '1' || component[3] > '9'))
Branch: 3 branches
BranchTrueFalseTotal
if (trailing_num && (component[3] < '1' || component[3] > '9')) 0 0 0
if (trailing_num && (component[3] < '1' || component[3] > '9')) 0 0 0
if (trailing_num && (component[3] < '1' || component[3] > '9')) 0 0 0
1562 0 return true;
1563  
1564 0 return (len > last &&
1565 0 ▶ component[last] != '.' &&
Branch: 2 branches
BranchTrueFalseTotal
component[last] != '.' && 0 0 0
component[last] != '.' && 0 0 0
1566 0 ▶ component[last] != ':');
Branch: 1 branch
BranchTrueFalseTotal
component[last] != ':'); 0 0 0
1567 }
1568  
1569 4887 GIT_INLINE(bool) validate_char(unsigned char c, unsigned int flags)
1570 {
1571 4887 ▶ if ((flags & GIT_FS_PATH_REJECT_BACKSLASH) && c == '\\')
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & GIT_FS_PATH_REJECT_BACKSLASH) && c == '\\') 0 4887 4887
if ((flags & GIT_FS_PATH_REJECT_BACKSLASH) && c == '\\') 0 0 0
1572 0 return false;
1573  
1574 4887 ▶ if ((flags & GIT_FS_PATH_REJECT_SLASH) && c == '/')
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & GIT_FS_PATH_REJECT_SLASH) && c == '/') 1170 3717 4887
if ((flags & GIT_FS_PATH_REJECT_SLASH) && c == '/') 1 1169 1170
1575 1 return false;
1576  
1577 4886 ▶ if (flags & GIT_FS_PATH_REJECT_NT_CHARS) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & GIT_FS_PATH_REJECT_NT_CHARS) { 0 4886 4886
1578 0 ▶ if (c < 32)
Branch: 1 branch
BranchTrueFalseTotal
if (c < 32) 0 0 0
1579 0 return false;
1580  
1581 0 ▶ switch (c) {
Branch: 1 branch
BranchTrueFalseTotal
switch (c) { 0 0 0
1582 0 case '<':
1583 case '>':
1584 case ':':
1585 case '"':
1586 case '|':
1587 case '?':
1588 case '*':
1589 0 return false;
1590 }
1591 }
1592  
1593 4886 return true;
1594 }
1595  
1596 /*
1597 * We fundamentally don't like some paths when dealing with user-inputted
1598 * strings (to avoid escaping a sandbox): we don't want dot or dot-dot
1599 * anywhere, we want to avoid writing weird paths on Windows that can't
1600 * be handled by tools that use the non-\\?\ APIs, we don't want slashes
1601 * or double slashes at the end of paths that can make them ambiguous.
1602 *
1603 * For checkout, we don't want to recurse into ".git" either.
1604 */
1605 803 static bool validate_component(
1606 const char *component,
1607 size_t len,
1608 unsigned int flags)
1609 {
1610 803 ▶ if (len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) 0 803 803
1611 0 return !(flags & GIT_FS_PATH_REJECT_EMPTY_COMPONENT);
1612  
1613 803 ▶ if ((flags & GIT_FS_PATH_REJECT_TRAVERSAL) &&
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & GIT_FS_PATH_REJECT_TRAVERSAL) && 803 0 803
if ((flags & GIT_FS_PATH_REJECT_TRAVERSAL) && 7 796 803
1614 7 ▶ len == 1 && component[0] == '.')
Branch: 1 branch
BranchTrueFalseTotal
len == 1 && component[0] == '.') 0 7 7
1615 0 return false;
1616  
1617 803 ▶ if ((flags & GIT_FS_PATH_REJECT_TRAVERSAL) &&
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & GIT_FS_PATH_REJECT_TRAVERSAL) && 803 0 803
if ((flags & GIT_FS_PATH_REJECT_TRAVERSAL) && 0 803 803
1618 0 ▶ len == 2 && component[0] == '.' && component[1] == '.')
Branch: 2 branches
BranchTrueFalseTotal
len == 2 && component[0] == '.' && component[1] == '.') 0 0 0
len == 2 && component[0] == '.' && component[1] == '.') 0 0 0
1619 0 return false;
1620  
1621 803 ▶ if ((flags & GIT_FS_PATH_REJECT_TRAILING_DOT) &&
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & GIT_FS_PATH_REJECT_TRAILING_DOT) && 0 803 803
1622 0 ▶ component[len - 1] == '.')
Branch: 1 branch
BranchTrueFalseTotal
component[len - 1] == '.') 0 0 0
1623 0 return false;
1624  
1625 803 ▶ if ((flags & GIT_FS_PATH_REJECT_TRAILING_SPACE) &&
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & GIT_FS_PATH_REJECT_TRAILING_SPACE) && 0 803 803
1626 0 ▶ component[len - 1] == ' ')
Branch: 1 branch
BranchTrueFalseTotal
component[len - 1] == ' ') 0 0 0
1627 0 return false;
1628  
1629 803 ▶ if ((flags & GIT_FS_PATH_REJECT_TRAILING_COLON) &&
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & GIT_FS_PATH_REJECT_TRAILING_COLON) && 0 803 803
1630 0 ▶ component[len - 1] == ':')
Branch: 1 branch
BranchTrueFalseTotal
component[len - 1] == ':') 0 0 0
1631 0 return false;
1632  
1633 803 ▶ if (flags & GIT_FS_PATH_REJECT_DOS_PATHS) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & GIT_FS_PATH_REJECT_DOS_PATHS) { 0 803 803
1634 0 ▶ if (!validate_dospath(component, len, "CON", false) ||
Branch: 1 branch
BranchTrueFalseTotal
if (!validate_dospath(component, len, "CON", false) || 0 0 0
1635 0 ▶ !validate_dospath(component, len, "PRN", false) ||
Branch: 1 branch
BranchTrueFalseTotal
!validate_dospath(component, len, "PRN", false) || 0 0 0
1636 0 ▶ !validate_dospath(component, len, "AUX", false) ||
Branch: 1 branch
BranchTrueFalseTotal
!validate_dospath(component, len, "AUX", false) || 0 0 0
1637 0 ▶ !validate_dospath(component, len, "NUL", false) ||
Branch: 1 branch
BranchTrueFalseTotal
!validate_dospath(component, len, "NUL", false) || 0 0 0
1638 0 ▶ !validate_dospath(component, len, "COM", true) ||
Branch: 1 branch
BranchTrueFalseTotal
!validate_dospath(component, len, "COM", true) || 0 0 0
1639 0 ▶ !validate_dospath(component, len, "LPT", true))
Branch: 1 branch
BranchTrueFalseTotal
!validate_dospath(component, len, "LPT", true)) 0 0 0
1640 0 return false;
1641 }
1642  
1643 803 return true;
1644 }
1645  
1646 #ifdef GIT_WIN32
1647 GIT_INLINE(bool) validate_length(
1648 const char *path,
1649 size_t len,
1650 size_t utf8_char_len)
1651 {
1652 GIT_UNUSED(path);
1653 GIT_UNUSED(len);
1654  
1655 return (utf8_char_len <= MAX_PATH);
1656 }
1657 #endif
1658  
1659 7380 bool git_fs_path_str_is_valid_ext(
1660 const git_str *path,
1661 unsigned int flags,
1662 bool (*validate_char_cb)(char ch, void *payload),
1663 bool (*validate_component_cb)(const char *component, size_t len, void *payload),
1664 bool (*validate_length_cb)(const char *path, size_t len, size_t utf8_char_len),
1665 void *payload)
1666 {
1667 const char *start, *c;
1668 7380 size_t len = 0;
1669  
1670 7380 ▶ if (!flags)
Branch: 1 branch
BranchTrueFalseTotal
if (!flags) 6802 578 7380
1671 6802 return true;
1672  
1673 5464 ▶ for (start = c = path->ptr; *c && len < path->size; c++, len++) {
Branch: 2 branches
BranchTrueFalseTotal
for (start = c = path->ptr; *c && len < path->size; c++, len++) { 4887 577 5464
for (start = c = path->ptr; *c && len < path->size; c++, len++) { 4887 0 4887
1674 4887 ▶ if (!validate_char(*c, flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!validate_char(*c, flags)) 1 4886 4887
1675 1 return false;
1676  
1677 4886 ▶ if (validate_char_cb && !validate_char_cb(*c, payload))
Branch: 2 branches
BranchTrueFalseTotal
if (validate_char_cb && !validate_char_cb(*c, payload)) 0 4886 4886
if (validate_char_cb && !validate_char_cb(*c, payload)) 0 0 0
1678 0 return false;
1679  
1680 4886 ▶ if (*c != '/')
Branch: 1 branch
BranchTrueFalseTotal
if (*c != '/') 4660 226 4886
1681 4660 continue;
1682  
1683 226 ▶ if (!validate_component(start, (c - start), flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!validate_component(start, (c - start), flags)) 0 226 226
1684 0 return false;
1685  
1686 226 ▶ if (validate_component_cb &&
Branch: 1 branch
BranchTrueFalseTotal
if (validate_component_cb && 226 0 226
1687 226 ▶ !validate_component_cb(start, (c - start), payload))
Branch: 1 branch
BranchTrueFalseTotal
!validate_component_cb(start, (c - start), payload)) 0 226 226
1688 0 return false;
1689  
1690 226 start = c + 1;
1691 }
1692  
1693 /*
1694 * We want to support paths specified as either `const char *`
1695 * or `git_str *`; we pass size as `SIZE_MAX` when we use a
1696 * `const char *` to avoid a `strlen`. Ensure that we didn't
1697 * have a NUL in the buffer if there was a non-SIZE_MAX length.
1698 */
1699 577 ▶ if (path->size != SIZE_MAX && len != path->size)
Branch: 2 branches
BranchTrueFalseTotal
if (path->size != SIZE_MAX && len != path->size) 0 577 577
if (path->size != SIZE_MAX && len != path->size) 0 0 0
1700 0 return false;
1701  
1702 577 ▶ if (!validate_component(start, (c - start), flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!validate_component(start, (c - start), flags)) 0 577 577
1703 0 return false;
1704  
1705 577 ▶ if (validate_component_cb &&
Branch: 1 branch
BranchTrueFalseTotal
if (validate_component_cb && 577 0 577
1706 577 ▶ !validate_component_cb(start, (c - start), payload))
Branch: 1 branch
BranchTrueFalseTotal
!validate_component_cb(start, (c - start), payload)) 0 577 577
1707 0 return false;
1708  
1709 #ifdef GIT_WIN32
1710 if ((flags & GIT_FS_PATH_REJECT_LONG_PATHS) != 0) {
1711 size_t utf8_len = git_utf8_char_length(path->ptr, len);
1712  
1713 if (!validate_length(path->ptr, len, utf8_len))
1714 return false;
1715  
1716 if (validate_length_cb &&
1717 !validate_length_cb(path->ptr, len, utf8_len))
1718 return false;
1719 }
1720 #else
1721 577 GIT_UNUSED(validate_length_cb);
1722 #endif
1723  
1724 577 return true;
1725 }
1726  
1727 1894 int git_fs_path_validate_str_length_with_suffix(
1728 git_str *path,
1729 size_t suffix_len)
1730 {
1731 #ifdef GIT_WIN32
1732 size_t utf8_len = git_utf8_char_length(path->ptr, path->size);
1733 size_t total_len;
1734  
1735 if (GIT_ADD_SIZET_OVERFLOW(&total_len, utf8_len, suffix_len) ||
1736 total_len > MAX_PATH) {
1737  
1738 git_error_set(GIT_ERROR_FILESYSTEM, "path too long: '%.*s'",
1739 (int)path->size, path->ptr);
1740 return -1;
1741 }
1742 #else
1743 1894 GIT_UNUSED(path);
1744 1894 GIT_UNUSED(suffix_len);
1745 #endif
1746  
1747 1894 return 0;
1748 }
1749  
1750 0 int git_fs_path_normalize_slashes(git_str *out, const char *path)
1751 {
1752 int error;
1753 char *p;
1754  
1755 0 ▶ if ((error = git_str_puts(out, path)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_str_puts(out, path)) < 0) 0 0 0
1756 0 return error;
1757  
1758 0 ▶ for (p = out->ptr; *p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = out->ptr; *p; p++) { 0 0 0
1759 0 ▶ if (*p == '\\')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\\') 0 0 0
1760 0 *p = '/';
1761 }
1762  
1763 0 return 0;
1764 }
1765  
1766 5 bool git_fs_path_supports_symlinks(const char *dir)
1767 {
1768 5 git_str path = GIT_STR_INIT;
1769 5 bool supported = false;
1770 struct stat st;
1771 int fd;
1772  
1773 10 if ((fd = git_futils_mktmp(&path, dir, 0666)) < 0 ||
1774 10 ▶ p_close(fd) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
p_close(fd) < 0 || 5 0 5
1775 10 ▶ p_unlink(path.ptr) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
p_unlink(path.ptr) < 0 || 5 0 5
1776 10 ▶ p_symlink("testing", path.ptr) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
p_symlink("testing", path.ptr) < 0 || 0 5 5
1777 5 p_lstat(path.ptr, &st) < 0)
1778 0 goto done;
1779  
1780 5 supported = (S_ISLNK(st.st_mode) != 0);
1781 5 done:
1782 5 ▶ if (path.size)
Branch: 1 branch
BranchTrueFalseTotal
if (path.size) 5 0 5
1783 5 (void)p_unlink(path.ptr);
1784 5 git_str_dispose(&path);
1785 5 return supported;
1786 }
1787  
1788 static git_fs_path_owner_t mock_owner = GIT_FS_PATH_OWNER_NONE;
1789  
1790 0 void git_fs_path__set_owner(git_fs_path_owner_t owner)
1791 {
1792 0 mock_owner = owner;
1793 0 }
1794  
1795 #ifdef GIT_WIN32
1796 static PSID *sid_dup(PSID sid)
1797 {
1798 DWORD len;
1799 PSID dup;
1800  
1801 len = GetLengthSid(sid);
1802  
1803 if ((dup = git__malloc(len)) == NULL)
1804 return NULL;
1805  
1806 if (!CopySid(len, dup, sid)) {
1807 git_error_set(GIT_ERROR_OS, "could not duplicate sid");
1808 git__free(dup);
1809 return NULL;
1810 }
1811  
1812 return dup;
1813 }
1814  
1815 static int current_user_sid(PSID *out)
1816 {
1817 TOKEN_USER *info = NULL;
1818 HANDLE token = NULL;
1819 DWORD len = 0;
1820 int error = -1;
1821  
1822 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) {
1823 git_error_set(GIT_ERROR_OS, "could not lookup process information");
1824 goto done;
1825 }
1826  
1827 if (GetTokenInformation(token, TokenUser, NULL, 0, &len) ||
1828 GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
1829 git_error_set(GIT_ERROR_OS, "could not lookup token metadata");
1830 goto done;
1831 }
1832  
1833 info = git__malloc(len);
1834 GIT_ERROR_CHECK_ALLOC(info);
1835  
1836 if (!GetTokenInformation(token, TokenUser, info, len, &len)) {
1837 git_error_set(GIT_ERROR_OS, "could not lookup current user");
1838 goto done;
1839 }
1840  
1841 if ((*out = sid_dup(info->User.Sid)))
1842 error = 0;
1843  
1844 done:
1845 if (token)
1846 CloseHandle(token);
1847  
1848 git__free(info);
1849 return error;
1850 }
1851  
1852 static int file_owner_sid(PSID *out, const char *path)
1853 {
1854 git_win32_path path_w32;
1855 PSECURITY_DESCRIPTOR descriptor = NULL;
1856 PSID owner_sid;
1857 DWORD ret;
1858 int error = -1;
1859  
1860 if (git_win32_path_from_utf8(path_w32, path) < 0)
1861 return -1;
1862  
1863 ret = GetNamedSecurityInfoW(path_w32, SE_FILE_OBJECT,
1864 OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
1865 &owner_sid, NULL, NULL, NULL, &descriptor);
1866  
1867 if (ret == ERROR_FILE_NOT_FOUND || ret == ERROR_PATH_NOT_FOUND)
1868 error = GIT_ENOTFOUND;
1869 else if (ret != ERROR_SUCCESS)
1870 git_error_set(GIT_ERROR_OS, "failed to get security information");
1871 else if (!IsValidSid(owner_sid))
1872 git_error_set(GIT_ERROR_OS, "file owner is not valid");
1873 else if ((*out = sid_dup(owner_sid)))
1874 error = 0;
1875  
1876 if (descriptor)
1877 LocalFree(descriptor);
1878  
1879 return error;
1880 }
1881  
1882 int git_fs_path_owner_is(
1883 bool *out,
1884 const char *path,
1885 git_fs_path_owner_t owner_type)
1886 {
1887 PSID owner_sid = NULL, user_sid = NULL;
1888 BOOL is_admin, admin_owned;
1889 int error;
1890  
1891 if (mock_owner) {
1892 *out = ((mock_owner & owner_type) != 0);
1893 return 0;
1894 }
1895  
1896 if ((error = file_owner_sid(&owner_sid, path)) < 0)
1897 goto done;
1898  
1899 if ((owner_type & GIT_FS_PATH_OWNER_CURRENT_USER) != 0) {
1900 if ((error = current_user_sid(&user_sid)) < 0)
1901 goto done;
1902  
1903 if (EqualSid(owner_sid, user_sid)) {
1904 *out = true;
1905 goto done;
1906 }
1907 }
1908  
1909 admin_owned =
1910 IsWellKnownSid(owner_sid, WinBuiltinAdministratorsSid) ||
1911 IsWellKnownSid(owner_sid, WinLocalSystemSid);
1912  
1913 if (admin_owned &&
1914 (owner_type & GIT_FS_PATH_OWNER_ADMINISTRATOR) != 0) {
1915 *out = true;
1916 goto done;
1917 }
1918  
1919 if (admin_owned &&
1920 (owner_type & GIT_FS_PATH_USER_IS_ADMINISTRATOR) != 0 &&
1921 CheckTokenMembership(NULL, owner_sid, &is_admin) &&
1922 is_admin) {
1923 *out = true;
1924 goto done;
1925 }
1926  
1927 *out = false;
1928  
1929 done:
1930 git__free(owner_sid);
1931 git__free(user_sid);
1932 return error;
1933 }
1934  
1935 #else
1936  
1937 0 static int sudo_uid_lookup(uid_t *out)
1938 {
1939 0 git_str uid_str = GIT_STR_INIT;
1940 int64_t uid;
1941 int error;
1942  
1943 0 if ((error = git__getenv(&uid_str, "SUDO_UID")) == 0 &&
1944 0 (error = git__strntol64(&uid, uid_str.ptr, uid_str.size, NULL, 10)) == 0 &&
1945 0 ▶ uid == (int64_t)((uid_t)uid)) {
Branch: 1 branch
BranchTrueFalseTotal
uid == (int64_t)((uid_t)uid)) { 0 0 0
1946 0 *out = (uid_t)uid;
1947 }
1948  
1949 0 git_str_dispose(&uid_str);
1950 0 return error;
1951 }
1952  
1953 123 int git_fs_path_owner_is(
1954 bool *out,
1955 const char *path,
1956 git_fs_path_owner_t owner_type)
1957 {
1958 struct stat st;
1959 uid_t euid, sudo_uid;
1960  
1961 123 ▶ if (mock_owner) {
Branch: 1 branch
BranchTrueFalseTotal
if (mock_owner) { 0 123 123
1962 0 *out = ((mock_owner & owner_type) != 0);
1963 0 return 0;
1964 }
1965  
1966 123 euid = geteuid();
1967  
1968 123 ▶ if (p_lstat(path, &st) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_lstat(path, &st) != 0) { 0 123 123
1969 0 ▶ if (errno == ENOENT)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == ENOENT) 0 0 0
1970 0 return GIT_ENOTFOUND;
1971  
1972 0 git_error_set(GIT_ERROR_OS, "could not stat '%s'", path);
1973 0 return -1;
1974 }
1975  
1976 123 ▶ if ((owner_type & GIT_FS_PATH_OWNER_CURRENT_USER) != 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((owner_type & GIT_FS_PATH_OWNER_CURRENT_USER) != 0 && 123 0 123
1977 123 ▶ st.st_uid == euid) {
Branch: 1 branch
BranchTrueFalseTotal
st.st_uid == euid) { 123 0 123
1978 123 *out = true;
1979 123 return 0;
1980 }
1981  
1982 0 ▶ if ((owner_type & GIT_FS_PATH_OWNER_ADMINISTRATOR) != 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((owner_type & GIT_FS_PATH_OWNER_ADMINISTRATOR) != 0 && 0 0 0
1983 0 ▶ st.st_uid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
st.st_uid == 0) { 0 0 0
1984 0 *out = true;
1985 0 return 0;
1986 }
1987  
1988 0 ▶ if ((owner_type & GIT_FS_PATH_OWNER_RUNNING_SUDO) != 0 &&
Branch: 2 branches
BranchTrueFalseTotal
if ((owner_type & GIT_FS_PATH_OWNER_RUNNING_SUDO) != 0 && 0 0 0
if ((owner_type & GIT_FS_PATH_OWNER_RUNNING_SUDO) != 0 && 0 0 0
1989 0 ▶ euid == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
euid == 0 && 0 0 0
1990 0 sudo_uid_lookup(&sudo_uid) == 0 &&
1991 0 ▶ st.st_uid == sudo_uid) {
Branch: 1 branch
BranchTrueFalseTotal
st.st_uid == sudo_uid) { 0 0 0
1992 0 *out = true;
1993 0 return 0;
1994 }
1995  
1996 0 *out = false;
1997 0 return 0;
1998 }
1999  
2000 #endif
2001  
2002 0 int git_fs_path_owner_is_current_user(bool *out, const char *path)
2003 {
2004 0 return git_fs_path_owner_is(out, path, GIT_FS_PATH_OWNER_CURRENT_USER);
2005 }
2006  
2007 0 int git_fs_path_owner_is_system(bool *out, const char *path)
2008 {
2009 0 return git_fs_path_owner_is(out, path, GIT_FS_PATH_OWNER_ADMINISTRATOR);
2010 }
2011  
2012 0 int git_fs_path_find_executable(git_str *fullpath, const char *executable)
2013 {
2014 #ifdef GIT_WIN32
2015 git_win32_path fullpath_w, executable_w;
2016 int error;
2017  
2018 if (git__utf8_to_16(executable_w, GIT_WIN_PATH_MAX, executable) < 0)
2019 return -1;
2020  
2021 error = git_win32_path_find_executable(fullpath_w, executable_w);
2022  
2023 if (error == 0)
2024 error = git_str_put_w(fullpath, fullpath_w, wcslen(fullpath_w));
2025  
2026 return error;
2027 #else
2028 0 git_str path = GIT_STR_INIT;
2029 const char *current_dir, *term;
2030 0 bool found = false;
2031  
2032 0 ▶ if (git__getenv(&path, "PATH") < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git__getenv(&path, "PATH") < 0) 0 0 0
2033 0 return -1;
2034  
2035 0 current_dir = path.ptr;
2036  
2037 0 ▶ while (*current_dir) {
Branch: 1 branch
BranchTrueFalseTotal
while (*current_dir) { 0 0 0
2038 0 ▶ if (! (term = strchr(current_dir, GIT_PATH_LIST_SEPARATOR)))
Branch: 1 branch
BranchTrueFalseTotal
if (! (term = strchr(current_dir, GIT_PATH_LIST_SEPARATOR))) 0 0 0
2039 0 term = strchr(current_dir, '\0');
2040  
2041 0 git_str_clear(fullpath);
2042 0 if (git_str_put(fullpath, current_dir, (term - current_dir)) < 0 ||
2043 0 ▶ git_str_putc(fullpath, '/') < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
git_str_putc(fullpath, '/') < 0 || 0 0 0
2044 0 git_str_puts(fullpath, executable) < 0)
2045 0 return -1;
2046  
2047 0 ▶ if (git_fs_path_isfile(fullpath->ptr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_fs_path_isfile(fullpath->ptr)) { 0 0 0
2048 0 found = true;
2049 0 break;
2050 }
2051  
2052 0 current_dir = term;
2053  
2054 0 ▶ while (*current_dir == GIT_PATH_LIST_SEPARATOR)
Branch: 1 branch
BranchTrueFalseTotal
while (*current_dir == GIT_PATH_LIST_SEPARATOR) 0 0 0
2055 0 current_dir++;
2056 }
2057  
2058 0 git_str_dispose(&path);
2059  
2060 0 ▶ if (found)
Branch: 1 branch
BranchTrueFalseTotal
if (found) 0 0 0
2061 0 return 0;
2062  
2063 0 git_str_clear(fullpath);
2064 0 return GIT_ENOTFOUND;
2065 #endif
2066 }
« deps/libgit2/src/libgit2/repository.c ↑ Summary deps/libgit2/src/util/str.c »