]> git.proxmox.com Git - libgit2.git/blob - src/path.c
Merge pull request #4260 from libgit2/ethomson/forced_checkout_2
[libgit2.git] / src / path.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 #include "common.h"
8 #include "path.h"
9 #include "posix.h"
10 #include "repository.h"
11 #ifdef GIT_WIN32
12 #include "win32/posix.h"
13 #include "win32/w32_buffer.h"
14 #include "win32/w32_util.h"
15 #include "win32/version.h"
16 #else
17 #include <dirent.h>
18 #endif
19 #include <stdio.h>
20 #include <ctype.h>
21
22 #define LOOKS_LIKE_DRIVE_PREFIX(S) (git__isalpha((S)[0]) && (S)[1] == ':')
23
24 #ifdef GIT_WIN32
25 static bool looks_like_network_computer_name(const char *path, int pos)
26 {
27 if (pos < 3)
28 return false;
29
30 if (path[0] != '/' || path[1] != '/')
31 return false;
32
33 while (pos-- > 2) {
34 if (path[pos] == '/')
35 return false;
36 }
37
38 return true;
39 }
40 #endif
41
42 /*
43 * Based on the Android implementation, BSD licensed.
44 * http://android.git.kernel.org/
45 *
46 * Copyright (C) 2008 The Android Open Source Project
47 * All rights reserved.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * * Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * * Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in
56 * the documentation and/or other materials provided with the
57 * distribution.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
60 * AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
61 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
62 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
63 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
64 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
65 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
66 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
67 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
68 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
69 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 */
72 int git_path_basename_r(git_buf *buffer, const char *path)
73 {
74 const char *endp, *startp;
75 int len, result;
76
77 /* Empty or NULL string gets treated as "." */
78 if (path == NULL || *path == '\0') {
79 startp = ".";
80 len = 1;
81 goto Exit;
82 }
83
84 /* Strip trailing slashes */
85 endp = path + strlen(path) - 1;
86 while (endp > path && *endp == '/')
87 endp--;
88
89 /* All slashes becomes "/" */
90 if (endp == path && *endp == '/') {
91 startp = "/";
92 len = 1;
93 goto Exit;
94 }
95
96 /* Find the start of the base */
97 startp = endp;
98 while (startp > path && *(startp - 1) != '/')
99 startp--;
100
101 /* Cast is safe because max path < max int */
102 len = (int)(endp - startp + 1);
103
104 Exit:
105 result = len;
106
107 if (buffer != NULL && git_buf_set(buffer, startp, len) < 0)
108 return -1;
109
110 return result;
111 }
112
113 /*
114 * Determine if the path is a Windows prefix and, if so, returns
115 * its actual lentgh. If it is not a prefix, returns -1.
116 */
117 static int win32_prefix_length(const char *path, int len)
118 {
119 #ifndef GIT_WIN32
120 GIT_UNUSED(path);
121 GIT_UNUSED(len);
122 #else
123 /*
124 * Mimic unix behavior where '/.git' returns '/': 'C:/.git' will return
125 * 'C:/' here
126 */
127 if (len == 2 && LOOKS_LIKE_DRIVE_PREFIX(path))
128 return 2;
129
130 /*
131 * Similarly checks if we're dealing with a network computer name
132 * '//computername/.git' will return '//computername/'
133 */
134 if (looks_like_network_computer_name(path, len))
135 return len;
136 #endif
137
138 return -1;
139 }
140
141 /*
142 * Based on the Android implementation, BSD licensed.
143 * Check http://android.git.kernel.org/
144 */
145 int git_path_dirname_r(git_buf *buffer, const char *path)
146 {
147 const char *endp;
148 int is_prefix = 0, len;
149
150 /* Empty or NULL string gets treated as "." */
151 if (path == NULL || *path == '\0') {
152 path = ".";
153 len = 1;
154 goto Exit;
155 }
156
157 /* Strip trailing slashes */
158 endp = path + strlen(path) - 1;
159 while (endp > path && *endp == '/')
160 endp--;
161
162 if ((len = win32_prefix_length(path, endp - path + 1)) > 0) {
163 is_prefix = 1;
164 goto Exit;
165 }
166
167 /* Find the start of the dir */
168 while (endp > path && *endp != '/')
169 endp--;
170
171 /* Either the dir is "/" or there are no slashes */
172 if (endp == path) {
173 path = (*endp == '/') ? "/" : ".";
174 len = 1;
175 goto Exit;
176 }
177
178 do {
179 endp--;
180 } while (endp > path && *endp == '/');
181
182 if ((len = win32_prefix_length(path, endp - path + 1)) > 0) {
183 is_prefix = 1;
184 goto Exit;
185 }
186
187 /* Cast is safe because max path < max int */
188 len = (int)(endp - path + 1);
189
190 Exit:
191 if (buffer) {
192 if (git_buf_set(buffer, path, len) < 0)
193 return -1;
194 if (is_prefix && git_buf_putc(buffer, '/') < 0)
195 return -1;
196 }
197
198 return len;
199 }
200
201
202 char *git_path_dirname(const char *path)
203 {
204 git_buf buf = GIT_BUF_INIT;
205 char *dirname;
206
207 git_path_dirname_r(&buf, path);
208 dirname = git_buf_detach(&buf);
209 git_buf_free(&buf); /* avoid memleak if error occurs */
210
211 return dirname;
212 }
213
214 char *git_path_basename(const char *path)
215 {
216 git_buf buf = GIT_BUF_INIT;
217 char *basename;
218
219 git_path_basename_r(&buf, path);
220 basename = git_buf_detach(&buf);
221 git_buf_free(&buf); /* avoid memleak if error occurs */
222
223 return basename;
224 }
225
226 size_t git_path_basename_offset(git_buf *buffer)
227 {
228 ssize_t slash;
229
230 if (!buffer || buffer->size <= 0)
231 return 0;
232
233 slash = git_buf_rfind_next(buffer, '/');
234
235 if (slash >= 0 && buffer->ptr[slash] == '/')
236 return (size_t)(slash + 1);
237
238 return 0;
239 }
240
241 const char *git_path_topdir(const char *path)
242 {
243 size_t len;
244 ssize_t i;
245
246 assert(path);
247 len = strlen(path);
248
249 if (!len || path[len - 1] != '/')
250 return NULL;
251
252 for (i = (ssize_t)len - 2; i >= 0; --i)
253 if (path[i] == '/')
254 break;
255
256 return &path[i + 1];
257 }
258
259 int git_path_root(const char *path)
260 {
261 int offset = 0;
262
263 /* Does the root of the path look like a windows drive ? */
264 if (LOOKS_LIKE_DRIVE_PREFIX(path))
265 offset += 2;
266
267 #ifdef GIT_WIN32
268 /* Are we dealing with a windows network path? */
269 else if ((path[0] == '/' && path[1] == '/' && path[2] != '/') ||
270 (path[0] == '\\' && path[1] == '\\' && path[2] != '\\'))
271 {
272 offset += 2;
273
274 /* Skip the computer name segment */
275 while (path[offset] && path[offset] != '/' && path[offset] != '\\')
276 offset++;
277 }
278 #endif
279
280 if (path[offset] == '/' || path[offset] == '\\')
281 return offset;
282
283 return -1; /* Not a real error - signals that path is not rooted */
284 }
285
286 void git_path_trim_slashes(git_buf *path)
287 {
288 int ceiling = git_path_root(path->ptr) + 1;
289 assert(ceiling >= 0);
290
291 while (path->size > (size_t)ceiling) {
292 if (path->ptr[path->size-1] != '/')
293 break;
294
295 path->ptr[path->size-1] = '\0';
296 path->size--;
297 }
298 }
299
300 int git_path_join_unrooted(
301 git_buf *path_out, const char *path, const char *base, ssize_t *root_at)
302 {
303 ssize_t root;
304
305 assert(path && path_out);
306
307 root = (ssize_t)git_path_root(path);
308
309 if (base != NULL && root < 0) {
310 if (git_buf_joinpath(path_out, base, path) < 0)
311 return -1;
312
313 root = (ssize_t)strlen(base);
314 } else {
315 if (git_buf_sets(path_out, path) < 0)
316 return -1;
317
318 if (root < 0)
319 root = 0;
320 else if (base)
321 git_path_equal_or_prefixed(base, path, &root);
322 }
323
324 if (root_at)
325 *root_at = root;
326
327 return 0;
328 }
329
330 void git_path_squash_slashes(git_buf *path)
331 {
332 char *p, *q;
333
334 if (path->size == 0)
335 return;
336
337 for (p = path->ptr, q = path->ptr; *q; p++, q++) {
338 *p = *q;
339
340 while (*q == '/' && *(q+1) == '/') {
341 path->size--;
342 q++;
343 }
344 }
345
346 *p = '\0';
347 }
348
349 int git_path_prettify(git_buf *path_out, const char *path, const char *base)
350 {
351 char buf[GIT_PATH_MAX];
352
353 assert(path && path_out);
354
355 /* construct path if needed */
356 if (base != NULL && git_path_root(path) < 0) {
357 if (git_buf_joinpath(path_out, base, path) < 0)
358 return -1;
359 path = path_out->ptr;
360 }
361
362 if (p_realpath(path, buf) == NULL) {
363 /* giterr_set resets the errno when dealing with a GITERR_OS kind of error */
364 int error = (errno == ENOENT || errno == ENOTDIR) ? GIT_ENOTFOUND : -1;
365 giterr_set(GITERR_OS, "failed to resolve path '%s'", path);
366
367 git_buf_clear(path_out);
368
369 return error;
370 }
371
372 return git_buf_sets(path_out, buf);
373 }
374
375 int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base)
376 {
377 int error = git_path_prettify(path_out, path, base);
378 return (error < 0) ? error : git_path_to_dir(path_out);
379 }
380
381 int git_path_to_dir(git_buf *path)
382 {
383 if (path->asize > 0 &&
384 git_buf_len(path) > 0 &&
385 path->ptr[git_buf_len(path) - 1] != '/')
386 git_buf_putc(path, '/');
387
388 return git_buf_oom(path) ? -1 : 0;
389 }
390
391 void git_path_string_to_dir(char* path, size_t size)
392 {
393 size_t end = strlen(path);
394
395 if (end && path[end - 1] != '/' && end < size) {
396 path[end] = '/';
397 path[end + 1] = '\0';
398 }
399 }
400
401 int git__percent_decode(git_buf *decoded_out, const char *input)
402 {
403 int len, hi, lo, i;
404 assert(decoded_out && input);
405
406 len = (int)strlen(input);
407 git_buf_clear(decoded_out);
408
409 for(i = 0; i < len; i++)
410 {
411 char c = input[i];
412
413 if (c != '%')
414 goto append;
415
416 if (i >= len - 2)
417 goto append;
418
419 hi = git__fromhex(input[i + 1]);
420 lo = git__fromhex(input[i + 2]);
421
422 if (hi < 0 || lo < 0)
423 goto append;
424
425 c = (char)(hi << 4 | lo);
426 i += 2;
427
428 append:
429 if (git_buf_putc(decoded_out, c) < 0)
430 return -1;
431 }
432
433 return 0;
434 }
435
436 static int error_invalid_local_file_uri(const char *uri)
437 {
438 giterr_set(GITERR_CONFIG, "'%s' is not a valid local file URI", uri);
439 return -1;
440 }
441
442 static int local_file_url_prefixlen(const char *file_url)
443 {
444 int len = -1;
445
446 if (git__prefixcmp(file_url, "file://") == 0) {
447 if (file_url[7] == '/')
448 len = 8;
449 else if (git__prefixcmp(file_url + 7, "localhost/") == 0)
450 len = 17;
451 }
452
453 return len;
454 }
455
456 bool git_path_is_local_file_url(const char *file_url)
457 {
458 return (local_file_url_prefixlen(file_url) > 0);
459 }
460
461 int git_path_fromurl(git_buf *local_path_out, const char *file_url)
462 {
463 int offset;
464
465 assert(local_path_out && file_url);
466
467 if ((offset = local_file_url_prefixlen(file_url)) < 0 ||
468 file_url[offset] == '\0' || file_url[offset] == '/')
469 return error_invalid_local_file_uri(file_url);
470
471 #ifndef GIT_WIN32
472 offset--; /* A *nix absolute path starts with a forward slash */
473 #endif
474
475 git_buf_clear(local_path_out);
476 return git__percent_decode(local_path_out, file_url + offset);
477 }
478
479 int git_path_walk_up(
480 git_buf *path,
481 const char *ceiling,
482 int (*cb)(void *data, const char *),
483 void *data)
484 {
485 int error = 0;
486 git_buf iter;
487 ssize_t stop = 0, scan;
488 char oldc = '\0';
489
490 assert(path && cb);
491
492 if (ceiling != NULL) {
493 if (git__prefixcmp(path->ptr, ceiling) == 0)
494 stop = (ssize_t)strlen(ceiling);
495 else
496 stop = git_buf_len(path);
497 }
498 scan = git_buf_len(path);
499
500 /* empty path: yield only once */
501 if (!scan) {
502 error = cb(data, "");
503 if (error)
504 giterr_set_after_callback(error);
505 return error;
506 }
507
508 iter.ptr = path->ptr;
509 iter.size = git_buf_len(path);
510 iter.asize = path->asize;
511
512 while (scan >= stop) {
513 error = cb(data, iter.ptr);
514 iter.ptr[scan] = oldc;
515
516 if (error) {
517 giterr_set_after_callback(error);
518 break;
519 }
520
521 scan = git_buf_rfind_next(&iter, '/');
522 if (scan >= 0) {
523 scan++;
524 oldc = iter.ptr[scan];
525 iter.size = scan;
526 iter.ptr[scan] = '\0';
527 }
528 }
529
530 if (scan >= 0)
531 iter.ptr[scan] = oldc;
532
533 /* relative path: yield for the last component */
534 if (!error && stop == 0 && iter.ptr[0] != '/') {
535 error = cb(data, "");
536 if (error)
537 giterr_set_after_callback(error);
538 }
539
540 return error;
541 }
542
543 bool git_path_exists(const char *path)
544 {
545 assert(path);
546 return p_access(path, F_OK) == 0;
547 }
548
549 bool git_path_isdir(const char *path)
550 {
551 struct stat st;
552 if (p_stat(path, &st) < 0)
553 return false;
554
555 return S_ISDIR(st.st_mode) != 0;
556 }
557
558 bool git_path_isfile(const char *path)
559 {
560 struct stat st;
561
562 assert(path);
563 if (p_stat(path, &st) < 0)
564 return false;
565
566 return S_ISREG(st.st_mode) != 0;
567 }
568
569 bool git_path_islink(const char *path)
570 {
571 struct stat st;
572
573 assert(path);
574 if (p_lstat(path, &st) < 0)
575 return false;
576
577 return S_ISLNK(st.st_mode) != 0;
578 }
579
580 #ifdef GIT_WIN32
581
582 bool git_path_is_empty_dir(const char *path)
583 {
584 git_win32_path filter_w;
585 bool empty = false;
586
587 if (git_win32__findfirstfile_filter(filter_w, path)) {
588 WIN32_FIND_DATAW findData;
589 HANDLE hFind = FindFirstFileW(filter_w, &findData);
590
591 /* FindFirstFile will fail if there are no children to the given
592 * path, which can happen if the given path is a file (and obviously
593 * has no children) or if the given path is an empty mount point.
594 * (Most directories have at least directory entries '.' and '..',
595 * but ridiculously another volume mounted in another drive letter's
596 * path space do not, and thus have nothing to enumerate.) If
597 * FindFirstFile fails, check if this is a directory-like thing
598 * (a mount point).
599 */
600 if (hFind == INVALID_HANDLE_VALUE)
601 return git_path_isdir(path);
602
603 /* If the find handle was created successfully, then it's a directory */
604 empty = true;
605
606 do {
607 /* Allow the enumeration to return . and .. and still be considered
608 * empty. In the special case of drive roots (i.e. C:\) where . and
609 * .. do not occur, we can still consider the path to be an empty
610 * directory if there's nothing there. */
611 if (!git_path_is_dot_or_dotdotW(findData.cFileName)) {
612 empty = false;
613 break;
614 }
615 } while (FindNextFileW(hFind, &findData));
616
617 FindClose(hFind);
618 }
619
620 return empty;
621 }
622
623 #else
624
625 static int path_found_entry(void *payload, git_buf *path)
626 {
627 GIT_UNUSED(payload);
628 return !git_path_is_dot_or_dotdot(path->ptr);
629 }
630
631 bool git_path_is_empty_dir(const char *path)
632 {
633 int error;
634 git_buf dir = GIT_BUF_INIT;
635
636 if (!git_path_isdir(path))
637 return false;
638
639 if ((error = git_buf_sets(&dir, path)) != 0)
640 giterr_clear();
641 else
642 error = git_path_direach(&dir, 0, path_found_entry, NULL);
643
644 git_buf_free(&dir);
645
646 return !error;
647 }
648
649 #endif
650
651 int git_path_set_error(int errno_value, const char *path, const char *action)
652 {
653 switch (errno_value) {
654 case ENOENT:
655 case ENOTDIR:
656 giterr_set(GITERR_OS, "could not find '%s' to %s", path, action);
657 return GIT_ENOTFOUND;
658
659 case EINVAL:
660 case ENAMETOOLONG:
661 giterr_set(GITERR_OS, "invalid path for filesystem '%s'", path);
662 return GIT_EINVALIDSPEC;
663
664 case EEXIST:
665 giterr_set(GITERR_OS, "failed %s - '%s' already exists", action, path);
666 return GIT_EEXISTS;
667
668 case EACCES:
669 giterr_set(GITERR_OS, "failed %s - '%s' is locked", action, path);
670 return GIT_ELOCKED;
671
672 default:
673 giterr_set(GITERR_OS, "could not %s '%s'", action, path);
674 return -1;
675 }
676 }
677
678 int git_path_lstat(const char *path, struct stat *st)
679 {
680 if (p_lstat(path, st) == 0)
681 return 0;
682
683 return git_path_set_error(errno, path, "stat");
684 }
685
686 static bool _check_dir_contents(
687 git_buf *dir,
688 const char *sub,
689 bool (*predicate)(const char *))
690 {
691 bool result;
692 size_t dir_size = git_buf_len(dir);
693 size_t sub_size = strlen(sub);
694 size_t alloc_size;
695
696 /* leave base valid even if we could not make space for subdir */
697 if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, dir_size, sub_size) ||
698 GIT_ADD_SIZET_OVERFLOW(&alloc_size, alloc_size, 2) ||
699 git_buf_try_grow(dir, alloc_size, false) < 0)
700 return false;
701
702 /* save excursion */
703 if (git_buf_joinpath(dir, dir->ptr, sub) < 0)
704 return false;
705
706 result = predicate(dir->ptr);
707
708 /* restore path */
709 git_buf_truncate(dir, dir_size);
710 return result;
711 }
712
713 bool git_path_contains(git_buf *dir, const char *item)
714 {
715 return _check_dir_contents(dir, item, &git_path_exists);
716 }
717
718 bool git_path_contains_dir(git_buf *base, const char *subdir)
719 {
720 return _check_dir_contents(base, subdir, &git_path_isdir);
721 }
722
723 bool git_path_contains_file(git_buf *base, const char *file)
724 {
725 return _check_dir_contents(base, file, &git_path_isfile);
726 }
727
728 int git_path_find_dir(git_buf *dir, const char *path, const char *base)
729 {
730 int error = git_path_join_unrooted(dir, path, base, NULL);
731
732 if (!error) {
733 char buf[GIT_PATH_MAX];
734 if (p_realpath(dir->ptr, buf) != NULL)
735 error = git_buf_sets(dir, buf);
736 }
737
738 /* call dirname if this is not a directory */
739 if (!error) /* && git_path_isdir(dir->ptr) == false) */
740 error = (git_path_dirname_r(dir, dir->ptr) < 0) ? -1 : 0;
741
742 if (!error)
743 error = git_path_to_dir(dir);
744
745 return error;
746 }
747
748 int git_path_resolve_relative(git_buf *path, size_t ceiling)
749 {
750 char *base, *to, *from, *next;
751 size_t len;
752
753 GITERR_CHECK_ALLOC_BUF(path);
754
755 if (ceiling > path->size)
756 ceiling = path->size;
757
758 /* recognize drive prefixes, etc. that should not be backed over */
759 if (ceiling == 0)
760 ceiling = git_path_root(path->ptr) + 1;
761
762 /* recognize URL prefixes that should not be backed over */
763 if (ceiling == 0) {
764 for (next = path->ptr; *next && git__isalpha(*next); ++next);
765 if (next[0] == ':' && next[1] == '/' && next[2] == '/')
766 ceiling = (next + 3) - path->ptr;
767 }
768
769 base = to = from = path->ptr + ceiling;
770
771 while (*from) {
772 for (next = from; *next && *next != '/'; ++next);
773
774 len = next - from;
775
776 if (len == 1 && from[0] == '.')
777 /* do nothing with singleton dot */;
778
779 else if (len == 2 && from[0] == '.' && from[1] == '.') {
780 /* error out if trying to up one from a hard base */
781 if (to == base && ceiling != 0) {
782 giterr_set(GITERR_INVALID,
783 "cannot strip root component off url");
784 return -1;
785 }
786
787 /* no more path segments to strip,
788 * use '../' as a new base path */
789 if (to == base) {
790 if (*next == '/')
791 len++;
792
793 if (to != from)
794 memmove(to, from, len);
795
796 to += len;
797 /* this is now the base, can't back up from a
798 * relative prefix */
799 base = to;
800 } else {
801 /* back up a path segment */
802 while (to > base && to[-1] == '/') to--;
803 while (to > base && to[-1] != '/') to--;
804 }
805 } else {
806 if (*next == '/' && *from != '/')
807 len++;
808
809 if (to != from)
810 memmove(to, from, len);
811
812 to += len;
813 }
814
815 from += len;
816
817 while (*from == '/') from++;
818 }
819
820 *to = '\0';
821
822 path->size = to - path->ptr;
823
824 return 0;
825 }
826
827 int git_path_apply_relative(git_buf *target, const char *relpath)
828 {
829 return git_buf_joinpath(target, git_buf_cstr(target), relpath) ||
830 git_path_resolve_relative(target, 0);
831 }
832
833 int git_path_cmp(
834 const char *name1, size_t len1, int isdir1,
835 const char *name2, size_t len2, int isdir2,
836 int (*compare)(const char *, const char *, size_t))
837 {
838 unsigned char c1, c2;
839 size_t len = len1 < len2 ? len1 : len2;
840 int cmp;
841
842 cmp = compare(name1, name2, len);
843 if (cmp)
844 return cmp;
845
846 c1 = name1[len];
847 c2 = name2[len];
848
849 if (c1 == '\0' && isdir1)
850 c1 = '/';
851
852 if (c2 == '\0' && isdir2)
853 c2 = '/';
854
855 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
856 }
857
858 size_t git_path_common_dirlen(const char *one, const char *two)
859 {
860 const char *p, *q, *dirsep = NULL;
861
862 for (p = one, q = two; *p && *q; p++, q++) {
863 if (*p == '/' && *q == '/')
864 dirsep = p;
865 else if (*p != *q)
866 break;
867 }
868
869 return dirsep ? (dirsep - one) + 1 : 0;
870 }
871
872 int git_path_make_relative(git_buf *path, const char *parent)
873 {
874 const char *p, *q, *p_dirsep, *q_dirsep;
875 size_t plen = path->size, newlen, alloclen, depth = 1, i, offset;
876
877 for (p_dirsep = p = path->ptr, q_dirsep = q = parent; *p && *q; p++, q++) {
878 if (*p == '/' && *q == '/') {
879 p_dirsep = p;
880 q_dirsep = q;
881 }
882 else if (*p != *q)
883 break;
884 }
885
886 /* need at least 1 common path segment */
887 if ((p_dirsep == path->ptr || q_dirsep == parent) &&
888 (*p_dirsep != '/' || *q_dirsep != '/')) {
889 giterr_set(GITERR_INVALID,
890 "%s is not a parent of %s", parent, path->ptr);
891 return GIT_ENOTFOUND;
892 }
893
894 if (*p == '/' && !*q)
895 p++;
896 else if (!*p && *q == '/')
897 q++;
898 else if (!*p && !*q)
899 return git_buf_clear(path), 0;
900 else {
901 p = p_dirsep + 1;
902 q = q_dirsep + 1;
903 }
904
905 plen -= (p - path->ptr);
906
907 if (!*q)
908 return git_buf_set(path, p, plen);
909
910 for (; (q = strchr(q, '/')) && *(q + 1); q++)
911 depth++;
912
913 GITERR_CHECK_ALLOC_MULTIPLY(&newlen, depth, 3);
914 GITERR_CHECK_ALLOC_ADD(&newlen, newlen, plen);
915
916 GITERR_CHECK_ALLOC_ADD(&alloclen, newlen, 1);
917
918 /* save the offset as we might realllocate the pointer */
919 offset = p - path->ptr;
920 if (git_buf_try_grow(path, alloclen, 1) < 0)
921 return -1;
922 p = path->ptr + offset;
923
924 memmove(path->ptr + (depth * 3), p, plen + 1);
925
926 for (i = 0; i < depth; i++)
927 memcpy(path->ptr + (i * 3), "../", 3);
928
929 path->size = newlen;
930 return 0;
931 }
932
933 bool git_path_has_non_ascii(const char *path, size_t pathlen)
934 {
935 const uint8_t *scan = (const uint8_t *)path, *end;
936
937 for (end = scan + pathlen; scan < end; ++scan)
938 if (*scan & 0x80)
939 return true;
940
941 return false;
942 }
943
944 #ifdef GIT_USE_ICONV
945
946 int git_path_iconv_init_precompose(git_path_iconv_t *ic)
947 {
948 git_buf_init(&ic->buf, 0);
949 ic->map = iconv_open(GIT_PATH_REPO_ENCODING, GIT_PATH_NATIVE_ENCODING);
950 return 0;
951 }
952
953 void git_path_iconv_clear(git_path_iconv_t *ic)
954 {
955 if (ic) {
956 if (ic->map != (iconv_t)-1)
957 iconv_close(ic->map);
958 git_buf_free(&ic->buf);
959 }
960 }
961
962 int git_path_iconv(git_path_iconv_t *ic, const char **in, size_t *inlen)
963 {
964 char *nfd = (char*)*in, *nfc;
965 size_t nfdlen = *inlen, nfclen, wantlen = nfdlen, alloclen, rv;
966 int retry = 1;
967
968 if (!ic || ic->map == (iconv_t)-1 ||
969 !git_path_has_non_ascii(*in, *inlen))
970 return 0;
971
972 git_buf_clear(&ic->buf);
973
974 while (1) {
975 GITERR_CHECK_ALLOC_ADD(&alloclen, wantlen, 1);
976 if (git_buf_grow(&ic->buf, alloclen) < 0)
977 return -1;
978
979 nfc = ic->buf.ptr + ic->buf.size;
980 nfclen = ic->buf.asize - ic->buf.size;
981
982 rv = iconv(ic->map, &nfd, &nfdlen, &nfc, &nfclen);
983
984 ic->buf.size = (nfc - ic->buf.ptr);
985
986 if (rv != (size_t)-1)
987 break;
988
989 /* if we cannot convert the data (probably because iconv thinks
990 * it is not valid UTF-8 source data), then use original data
991 */
992 if (errno != E2BIG)
993 return 0;
994
995 /* make space for 2x the remaining data to be converted
996 * (with per retry overhead to avoid infinite loops)
997 */
998 wantlen = ic->buf.size + max(nfclen, nfdlen) * 2 + (size_t)(retry * 4);
999
1000 if (retry++ > 4)
1001 goto fail;
1002 }
1003
1004 ic->buf.ptr[ic->buf.size] = '\0';
1005
1006 *in = ic->buf.ptr;
1007 *inlen = ic->buf.size;
1008
1009 return 0;
1010
1011 fail:
1012 giterr_set(GITERR_OS, "unable to convert unicode path data");
1013 return -1;
1014 }
1015
1016 static const char *nfc_file = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D.XXXXXX";
1017 static const char *nfd_file = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D.XXXXXX";
1018
1019 /* Check if the platform is decomposing unicode data for us. We will
1020 * emulate core Git and prefer to use precomposed unicode data internally
1021 * on these platforms, composing the decomposed unicode on the fly.
1022 *
1023 * This mainly happens on the Mac where HDFS stores filenames as
1024 * decomposed unicode. Even on VFAT and SAMBA file systems, the Mac will
1025 * return decomposed unicode from readdir() even when the actual
1026 * filesystem is storing precomposed unicode.
1027 */
1028 bool git_path_does_fs_decompose_unicode(const char *root)
1029 {
1030 git_buf path = GIT_BUF_INIT;
1031 int fd;
1032 bool found_decomposed = false;
1033 char tmp[6];
1034
1035 /* Create a file using a precomposed path and then try to find it
1036 * using the decomposed name. If the lookup fails, then we will mark
1037 * that we should precompose unicode for this repository.
1038 */
1039 if (git_buf_joinpath(&path, root, nfc_file) < 0 ||
1040 (fd = p_mkstemp(path.ptr)) < 0)
1041 goto done;
1042 p_close(fd);
1043
1044 /* record trailing digits generated by mkstemp */
1045 memcpy(tmp, path.ptr + path.size - sizeof(tmp), sizeof(tmp));
1046
1047 /* try to look up as NFD path */
1048 if (git_buf_joinpath(&path, root, nfd_file) < 0)
1049 goto done;
1050 memcpy(path.ptr + path.size - sizeof(tmp), tmp, sizeof(tmp));
1051
1052 found_decomposed = git_path_exists(path.ptr);
1053
1054 /* remove temporary file (using original precomposed path) */
1055 if (git_buf_joinpath(&path, root, nfc_file) < 0)
1056 goto done;
1057 memcpy(path.ptr + path.size - sizeof(tmp), tmp, sizeof(tmp));
1058
1059 (void)p_unlink(path.ptr);
1060
1061 done:
1062 git_buf_free(&path);
1063 return found_decomposed;
1064 }
1065
1066 #else
1067
1068 bool git_path_does_fs_decompose_unicode(const char *root)
1069 {
1070 GIT_UNUSED(root);
1071 return false;
1072 }
1073
1074 #endif
1075
1076 #if defined(__sun) || defined(__GNU__)
1077 typedef char path_dirent_data[sizeof(struct dirent) + FILENAME_MAX + 1];
1078 #else
1079 typedef struct dirent path_dirent_data;
1080 #endif
1081
1082 int git_path_direach(
1083 git_buf *path,
1084 uint32_t flags,
1085 int (*fn)(void *, git_buf *),
1086 void *arg)
1087 {
1088 int error = 0;
1089 ssize_t wd_len;
1090 DIR *dir;
1091 struct dirent *de;
1092
1093 #ifdef GIT_USE_ICONV
1094 git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
1095 #endif
1096
1097 GIT_UNUSED(flags);
1098
1099 if (git_path_to_dir(path) < 0)
1100 return -1;
1101
1102 wd_len = git_buf_len(path);
1103
1104 if ((dir = opendir(path->ptr)) == NULL) {
1105 giterr_set(GITERR_OS, "failed to open directory '%s'", path->ptr);
1106 if (errno == ENOENT)
1107 return GIT_ENOTFOUND;
1108
1109 return -1;
1110 }
1111
1112 #ifdef GIT_USE_ICONV
1113 if ((flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
1114 (void)git_path_iconv_init_precompose(&ic);
1115 #endif
1116
1117 while ((de = readdir(dir)) != NULL) {
1118 const char *de_path = de->d_name;
1119 size_t de_len = strlen(de_path);
1120
1121 if (git_path_is_dot_or_dotdot(de_path))
1122 continue;
1123
1124 #ifdef GIT_USE_ICONV
1125 if ((error = git_path_iconv(&ic, &de_path, &de_len)) < 0)
1126 break;
1127 #endif
1128
1129 if ((error = git_buf_put(path, de_path, de_len)) < 0)
1130 break;
1131
1132 giterr_clear();
1133 error = fn(arg, path);
1134
1135 git_buf_truncate(path, wd_len); /* restore path */
1136
1137 /* Only set our own error if the callback did not set one already */
1138 if (error != 0) {
1139 if (!giterr_last())
1140 giterr_set_after_callback(error);
1141
1142 break;
1143 }
1144 }
1145
1146 closedir(dir);
1147
1148 #ifdef GIT_USE_ICONV
1149 git_path_iconv_clear(&ic);
1150 #endif
1151
1152 return error;
1153 }
1154
1155 #if defined(GIT_WIN32) && !defined(__MINGW32__)
1156
1157 /* Using _FIND_FIRST_EX_LARGE_FETCH may increase performance in Windows 7
1158 * and better.
1159 */
1160 #ifndef FIND_FIRST_EX_LARGE_FETCH
1161 # define FIND_FIRST_EX_LARGE_FETCH 2
1162 #endif
1163
1164 int git_path_diriter_init(
1165 git_path_diriter *diriter,
1166 const char *path,
1167 unsigned int flags)
1168 {
1169 git_win32_path path_filter;
1170
1171 static int is_win7_or_later = -1;
1172 if (is_win7_or_later < 0)
1173 is_win7_or_later = git_has_win32_version(6, 1, 0);
1174
1175 assert(diriter && path);
1176
1177 memset(diriter, 0, sizeof(git_path_diriter));
1178 diriter->handle = INVALID_HANDLE_VALUE;
1179
1180 if (git_buf_puts(&diriter->path_utf8, path) < 0)
1181 return -1;
1182
1183 git_path_trim_slashes(&diriter->path_utf8);
1184
1185 if (diriter->path_utf8.size == 0) {
1186 giterr_set(GITERR_FILESYSTEM, "could not open directory '%s'", path);
1187 return -1;
1188 }
1189
1190 if ((diriter->parent_len = git_win32_path_from_utf8(diriter->path, diriter->path_utf8.ptr)) < 0 ||
1191 !git_win32__findfirstfile_filter(path_filter, diriter->path_utf8.ptr)) {
1192 giterr_set(GITERR_OS, "could not parse the directory path '%s'", path);
1193 return -1;
1194 }
1195
1196 diriter->handle = FindFirstFileExW(
1197 path_filter,
1198 is_win7_or_later ? FindExInfoBasic : FindExInfoStandard,
1199 &diriter->current,
1200 FindExSearchNameMatch,
1201 NULL,
1202 is_win7_or_later ? FIND_FIRST_EX_LARGE_FETCH : 0);
1203
1204 if (diriter->handle == INVALID_HANDLE_VALUE) {
1205 giterr_set(GITERR_OS, "could not open directory '%s'", path);
1206 return -1;
1207 }
1208
1209 diriter->parent_utf8_len = diriter->path_utf8.size;
1210 diriter->flags = flags;
1211 return 0;
1212 }
1213
1214 static int diriter_update_paths(git_path_diriter *diriter)
1215 {
1216 size_t filename_len, path_len;
1217
1218 filename_len = wcslen(diriter->current.cFileName);
1219
1220 if (GIT_ADD_SIZET_OVERFLOW(&path_len, diriter->parent_len, filename_len) ||
1221 GIT_ADD_SIZET_OVERFLOW(&path_len, path_len, 2))
1222 return -1;
1223
1224 if (path_len > GIT_WIN_PATH_UTF16) {
1225 giterr_set(GITERR_FILESYSTEM,
1226 "invalid path '%.*ls\\%ls' (path too long)",
1227 diriter->parent_len, diriter->path, diriter->current.cFileName);
1228 return -1;
1229 }
1230
1231 diriter->path[diriter->parent_len] = L'\\';
1232 memcpy(&diriter->path[diriter->parent_len+1],
1233 diriter->current.cFileName, filename_len * sizeof(wchar_t));
1234 diriter->path[path_len-1] = L'\0';
1235
1236 git_buf_truncate(&diriter->path_utf8, diriter->parent_utf8_len);
1237
1238 if (diriter->parent_utf8_len > 0 &&
1239 diriter->path_utf8.ptr[diriter->parent_utf8_len-1] != '/')
1240 git_buf_putc(&diriter->path_utf8, '/');
1241
1242 git_buf_put_w(&diriter->path_utf8, diriter->current.cFileName, filename_len);
1243
1244 if (git_buf_oom(&diriter->path_utf8))
1245 return -1;
1246
1247 return 0;
1248 }
1249
1250 int git_path_diriter_next(git_path_diriter *diriter)
1251 {
1252 bool skip_dot = !(diriter->flags & GIT_PATH_DIR_INCLUDE_DOT_AND_DOTDOT);
1253
1254 do {
1255 /* Our first time through, we already have the data from
1256 * FindFirstFileW. Use it, otherwise get the next file.
1257 */
1258 if (!diriter->needs_next)
1259 diriter->needs_next = 1;
1260 else if (!FindNextFileW(diriter->handle, &diriter->current))
1261 return GIT_ITEROVER;
1262 } while (skip_dot && git_path_is_dot_or_dotdotW(diriter->current.cFileName));
1263
1264 if (diriter_update_paths(diriter) < 0)
1265 return -1;
1266
1267 return 0;
1268 }
1269
1270 int git_path_diriter_filename(
1271 const char **out,
1272 size_t *out_len,
1273 git_path_diriter *diriter)
1274 {
1275 assert(out && out_len && diriter);
1276
1277 assert(diriter->path_utf8.size > diriter->parent_utf8_len);
1278
1279 *out = &diriter->path_utf8.ptr[diriter->parent_utf8_len+1];
1280 *out_len = diriter->path_utf8.size - diriter->parent_utf8_len - 1;
1281 return 0;
1282 }
1283
1284 int git_path_diriter_fullpath(
1285 const char **out,
1286 size_t *out_len,
1287 git_path_diriter *diriter)
1288 {
1289 assert(out && out_len && diriter);
1290
1291 *out = diriter->path_utf8.ptr;
1292 *out_len = diriter->path_utf8.size;
1293 return 0;
1294 }
1295
1296 int git_path_diriter_stat(struct stat *out, git_path_diriter *diriter)
1297 {
1298 assert(out && diriter);
1299
1300 return git_win32__file_attribute_to_stat(out,
1301 (WIN32_FILE_ATTRIBUTE_DATA *)&diriter->current,
1302 diriter->path);
1303 }
1304
1305 void git_path_diriter_free(git_path_diriter *diriter)
1306 {
1307 if (diriter == NULL)
1308 return;
1309
1310 git_buf_free(&diriter->path_utf8);
1311
1312 if (diriter->handle != INVALID_HANDLE_VALUE) {
1313 FindClose(diriter->handle);
1314 diriter->handle = INVALID_HANDLE_VALUE;
1315 }
1316 }
1317
1318 #else
1319
1320 int git_path_diriter_init(
1321 git_path_diriter *diriter,
1322 const char *path,
1323 unsigned int flags)
1324 {
1325 assert(diriter && path);
1326
1327 memset(diriter, 0, sizeof(git_path_diriter));
1328
1329 if (git_buf_puts(&diriter->path, path) < 0)
1330 return -1;
1331
1332 git_path_trim_slashes(&diriter->path);
1333
1334 if (diriter->path.size == 0) {
1335 giterr_set(GITERR_FILESYSTEM, "could not open directory '%s'", path);
1336 return -1;
1337 }
1338
1339 if ((diriter->dir = opendir(diriter->path.ptr)) == NULL) {
1340 git_buf_free(&diriter->path);
1341
1342 giterr_set(GITERR_OS, "failed to open directory '%s'", path);
1343 return -1;
1344 }
1345
1346 #ifdef GIT_USE_ICONV
1347 if ((flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
1348 (void)git_path_iconv_init_precompose(&diriter->ic);
1349 #endif
1350
1351 diriter->parent_len = diriter->path.size;
1352 diriter->flags = flags;
1353
1354 return 0;
1355 }
1356
1357 int git_path_diriter_next(git_path_diriter *diriter)
1358 {
1359 struct dirent *de;
1360 const char *filename;
1361 size_t filename_len;
1362 bool skip_dot = !(diriter->flags & GIT_PATH_DIR_INCLUDE_DOT_AND_DOTDOT);
1363 int error = 0;
1364
1365 assert(diriter);
1366
1367 errno = 0;
1368
1369 do {
1370 if ((de = readdir(diriter->dir)) == NULL) {
1371 if (!errno)
1372 return GIT_ITEROVER;
1373
1374 giterr_set(GITERR_OS,
1375 "could not read directory '%s'", diriter->path.ptr);
1376 return -1;
1377 }
1378 } while (skip_dot && git_path_is_dot_or_dotdot(de->d_name));
1379
1380 filename = de->d_name;
1381 filename_len = strlen(filename);
1382
1383 #ifdef GIT_USE_ICONV
1384 if ((diriter->flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0 &&
1385 (error = git_path_iconv(&diriter->ic, &filename, &filename_len)) < 0)
1386 return error;
1387 #endif
1388
1389 git_buf_truncate(&diriter->path, diriter->parent_len);
1390
1391 if (diriter->parent_len > 0 &&
1392 diriter->path.ptr[diriter->parent_len-1] != '/')
1393 git_buf_putc(&diriter->path, '/');
1394
1395 git_buf_put(&diriter->path, filename, filename_len);
1396
1397 if (git_buf_oom(&diriter->path))
1398 return -1;
1399
1400 return error;
1401 }
1402
1403 int git_path_diriter_filename(
1404 const char **out,
1405 size_t *out_len,
1406 git_path_diriter *diriter)
1407 {
1408 assert(out && out_len && diriter);
1409
1410 assert(diriter->path.size > diriter->parent_len);
1411
1412 *out = &diriter->path.ptr[diriter->parent_len+1];
1413 *out_len = diriter->path.size - diriter->parent_len - 1;
1414 return 0;
1415 }
1416
1417 int git_path_diriter_fullpath(
1418 const char **out,
1419 size_t *out_len,
1420 git_path_diriter *diriter)
1421 {
1422 assert(out && out_len && diriter);
1423
1424 *out = diriter->path.ptr;
1425 *out_len = diriter->path.size;
1426 return 0;
1427 }
1428
1429 int git_path_diriter_stat(struct stat *out, git_path_diriter *diriter)
1430 {
1431 assert(out && diriter);
1432
1433 return git_path_lstat(diriter->path.ptr, out);
1434 }
1435
1436 void git_path_diriter_free(git_path_diriter *diriter)
1437 {
1438 if (diriter == NULL)
1439 return;
1440
1441 if (diriter->dir) {
1442 closedir(diriter->dir);
1443 diriter->dir = NULL;
1444 }
1445
1446 #ifdef GIT_USE_ICONV
1447 git_path_iconv_clear(&diriter->ic);
1448 #endif
1449
1450 git_buf_free(&diriter->path);
1451 }
1452
1453 #endif
1454
1455 int git_path_dirload(
1456 git_vector *contents,
1457 const char *path,
1458 size_t prefix_len,
1459 uint32_t flags)
1460 {
1461 git_path_diriter iter = GIT_PATH_DIRITER_INIT;
1462 const char *name;
1463 size_t name_len;
1464 char *dup;
1465 int error;
1466
1467 assert(contents && path);
1468
1469 if ((error = git_path_diriter_init(&iter, path, flags)) < 0)
1470 return error;
1471
1472 while ((error = git_path_diriter_next(&iter)) == 0) {
1473 if ((error = git_path_diriter_fullpath(&name, &name_len, &iter)) < 0)
1474 break;
1475
1476 assert(name_len > prefix_len);
1477
1478 dup = git__strndup(name + prefix_len, name_len - prefix_len);
1479 GITERR_CHECK_ALLOC(dup);
1480
1481 if ((error = git_vector_insert(contents, dup)) < 0)
1482 break;
1483 }
1484
1485 if (error == GIT_ITEROVER)
1486 error = 0;
1487
1488 git_path_diriter_free(&iter);
1489 return error;
1490 }
1491
1492 int git_path_from_url_or_path(git_buf *local_path_out, const char *url_or_path)
1493 {
1494 if (git_path_is_local_file_url(url_or_path))
1495 return git_path_fromurl(local_path_out, url_or_path);
1496 else
1497 return git_buf_sets(local_path_out, url_or_path);
1498 }
1499
1500 /* Reject paths like AUX or COM1, or those versions that end in a dot or
1501 * colon. ("AUX." or "AUX:")
1502 */
1503 GIT_INLINE(bool) verify_dospath(
1504 const char *component,
1505 size_t len,
1506 const char dospath[3],
1507 bool trailing_num)
1508 {
1509 size_t last = trailing_num ? 4 : 3;
1510
1511 if (len < last || git__strncasecmp(component, dospath, 3) != 0)
1512 return true;
1513
1514 if (trailing_num && (component[3] < '1' || component[3] > '9'))
1515 return true;
1516
1517 return (len > last &&
1518 component[last] != '.' &&
1519 component[last] != ':');
1520 }
1521
1522 static int32_t next_hfs_char(const char **in, size_t *len)
1523 {
1524 while (*len) {
1525 int32_t codepoint;
1526 int cp_len = git__utf8_iterate((const uint8_t *)(*in), (int)(*len), &codepoint);
1527 if (cp_len < 0)
1528 return -1;
1529
1530 (*in) += cp_len;
1531 (*len) -= cp_len;
1532
1533 /* these code points are ignored completely */
1534 switch (codepoint) {
1535 case 0x200c: /* ZERO WIDTH NON-JOINER */
1536 case 0x200d: /* ZERO WIDTH JOINER */
1537 case 0x200e: /* LEFT-TO-RIGHT MARK */
1538 case 0x200f: /* RIGHT-TO-LEFT MARK */
1539 case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */
1540 case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */
1541 case 0x202c: /* POP DIRECTIONAL FORMATTING */
1542 case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */
1543 case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */
1544 case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */
1545 case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */
1546 case 0x206c: /* INHIBIT ARABIC FORM SHAPING */
1547 case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */
1548 case 0x206e: /* NATIONAL DIGIT SHAPES */
1549 case 0x206f: /* NOMINAL DIGIT SHAPES */
1550 case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */
1551 continue;
1552 }
1553
1554 /* fold into lowercase -- this will only fold characters in
1555 * the ASCII range, which is perfectly fine, because the
1556 * git folder name can only be composed of ascii characters
1557 */
1558 return git__tolower(codepoint);
1559 }
1560 return 0; /* NULL byte -- end of string */
1561 }
1562
1563 static bool verify_dotgit_hfs(const char *path, size_t len)
1564 {
1565 if (next_hfs_char(&path, &len) != '.' ||
1566 next_hfs_char(&path, &len) != 'g' ||
1567 next_hfs_char(&path, &len) != 'i' ||
1568 next_hfs_char(&path, &len) != 't' ||
1569 next_hfs_char(&path, &len) != 0)
1570 return true;
1571
1572 return false;
1573 }
1574
1575 GIT_INLINE(bool) verify_dotgit_ntfs(git_repository *repo, const char *path, size_t len)
1576 {
1577 git_buf *reserved = git_repository__reserved_names_win32;
1578 size_t reserved_len = git_repository__reserved_names_win32_len;
1579 size_t start = 0, i;
1580
1581 if (repo)
1582 git_repository__reserved_names(&reserved, &reserved_len, repo, true);
1583
1584 for (i = 0; i < reserved_len; i++) {
1585 git_buf *r = &reserved[i];
1586
1587 if (len >= r->size &&
1588 strncasecmp(path, r->ptr, r->size) == 0) {
1589 start = r->size;
1590 break;
1591 }
1592 }
1593
1594 if (!start)
1595 return true;
1596
1597 /* Reject paths like ".git\" */
1598 if (path[start] == '\\')
1599 return false;
1600
1601 /* Reject paths like '.git ' or '.git.' */
1602 for (i = start; i < len; i++) {
1603 if (path[i] != ' ' && path[i] != '.')
1604 return true;
1605 }
1606
1607 return false;
1608 }
1609
1610 GIT_INLINE(bool) verify_char(unsigned char c, unsigned int flags)
1611 {
1612 if ((flags & GIT_PATH_REJECT_BACKSLASH) && c == '\\')
1613 return false;
1614
1615 if ((flags & GIT_PATH_REJECT_SLASH) && c == '/')
1616 return false;
1617
1618 if (flags & GIT_PATH_REJECT_NT_CHARS) {
1619 if (c < 32)
1620 return false;
1621
1622 switch (c) {
1623 case '<':
1624 case '>':
1625 case ':':
1626 case '"':
1627 case '|':
1628 case '?':
1629 case '*':
1630 return false;
1631 }
1632 }
1633
1634 return true;
1635 }
1636
1637 /*
1638 * We fundamentally don't like some paths when dealing with user-inputted
1639 * strings (in checkout or ref names): we don't want dot or dot-dot
1640 * anywhere, we want to avoid writing weird paths on Windows that can't
1641 * be handled by tools that use the non-\\?\ APIs, we don't want slashes
1642 * or double slashes at the end of paths that can make them ambiguous.
1643 *
1644 * For checkout, we don't want to recurse into ".git" either.
1645 */
1646 static bool verify_component(
1647 git_repository *repo,
1648 const char *component,
1649 size_t len,
1650 unsigned int flags)
1651 {
1652 if (len == 0)
1653 return false;
1654
1655 if ((flags & GIT_PATH_REJECT_TRAVERSAL) &&
1656 len == 1 && component[0] == '.')
1657 return false;
1658
1659 if ((flags & GIT_PATH_REJECT_TRAVERSAL) &&
1660 len == 2 && component[0] == '.' && component[1] == '.')
1661 return false;
1662
1663 if ((flags & GIT_PATH_REJECT_TRAILING_DOT) && component[len-1] == '.')
1664 return false;
1665
1666 if ((flags & GIT_PATH_REJECT_TRAILING_SPACE) && component[len-1] == ' ')
1667 return false;
1668
1669 if ((flags & GIT_PATH_REJECT_TRAILING_COLON) && component[len-1] == ':')
1670 return false;
1671
1672 if (flags & GIT_PATH_REJECT_DOS_PATHS) {
1673 if (!verify_dospath(component, len, "CON", false) ||
1674 !verify_dospath(component, len, "PRN", false) ||
1675 !verify_dospath(component, len, "AUX", false) ||
1676 !verify_dospath(component, len, "NUL", false) ||
1677 !verify_dospath(component, len, "COM", true) ||
1678 !verify_dospath(component, len, "LPT", true))
1679 return false;
1680 }
1681
1682 if (flags & GIT_PATH_REJECT_DOT_GIT_HFS &&
1683 !verify_dotgit_hfs(component, len))
1684 return false;
1685
1686 if (flags & GIT_PATH_REJECT_DOT_GIT_NTFS &&
1687 !verify_dotgit_ntfs(repo, component, len))
1688 return false;
1689
1690 /* don't bother rerunning the `.git` test if we ran the HFS or NTFS
1691 * specific tests, they would have already rejected `.git`.
1692 */
1693 if ((flags & GIT_PATH_REJECT_DOT_GIT_HFS) == 0 &&
1694 (flags & GIT_PATH_REJECT_DOT_GIT_NTFS) == 0 &&
1695 (flags & GIT_PATH_REJECT_DOT_GIT_LITERAL) &&
1696 len == 4 &&
1697 component[0] == '.' &&
1698 (component[1] == 'g' || component[1] == 'G') &&
1699 (component[2] == 'i' || component[2] == 'I') &&
1700 (component[3] == 't' || component[3] == 'T'))
1701 return false;
1702
1703 return true;
1704 }
1705
1706 GIT_INLINE(unsigned int) dotgit_flags(
1707 git_repository *repo,
1708 unsigned int flags)
1709 {
1710 int protectHFS = 0, protectNTFS = 0;
1711 int error = 0;
1712
1713 flags |= GIT_PATH_REJECT_DOT_GIT_LITERAL;
1714
1715 #ifdef __APPLE__
1716 protectHFS = 1;
1717 #endif
1718
1719 #ifdef GIT_WIN32
1720 protectNTFS = 1;
1721 #endif
1722
1723 if (repo && !protectHFS)
1724 error = git_repository__cvar(&protectHFS, repo, GIT_CVAR_PROTECTHFS);
1725 if (!error && protectHFS)
1726 flags |= GIT_PATH_REJECT_DOT_GIT_HFS;
1727
1728 if (repo && !protectNTFS)
1729 error = git_repository__cvar(&protectNTFS, repo, GIT_CVAR_PROTECTNTFS);
1730 if (!error && protectNTFS)
1731 flags |= GIT_PATH_REJECT_DOT_GIT_NTFS;
1732
1733 return flags;
1734 }
1735
1736 bool git_path_isvalid(
1737 git_repository *repo,
1738 const char *path,
1739 unsigned int flags)
1740 {
1741 const char *start, *c;
1742
1743 /* Upgrade the ".git" checks based on platform */
1744 if ((flags & GIT_PATH_REJECT_DOT_GIT))
1745 flags = dotgit_flags(repo, flags);
1746
1747 for (start = c = path; *c; c++) {
1748 if (!verify_char(*c, flags))
1749 return false;
1750
1751 if (*c == '/') {
1752 if (!verify_component(repo, start, (c - start), flags))
1753 return false;
1754
1755 start = c+1;
1756 }
1757 }
1758
1759 return verify_component(repo, start, (c - start), flags);
1760 }
1761
1762 int git_path_normalize_slashes(git_buf *out, const char *path)
1763 {
1764 int error;
1765 char *p;
1766
1767 if ((error = git_buf_puts(out, path)) < 0)
1768 return error;
1769
1770 for (p = out->ptr; *p; p++) {
1771 if (*p == '\\')
1772 *p = '/';
1773 }
1774
1775 return 0;
1776 }