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