Blame src/path.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "path.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
#include "win32/posix.h"
Packit Service 20376f
#include "win32/w32_buffer.h"
Packit Service 20376f
#include "win32/w32_util.h"
Packit Service 20376f
#include "win32/version.h"
Packit Service 20376f
#else
Packit Service 20376f
#include <dirent.h>
Packit Service 20376f
#endif
Packit Service 20376f
#include <stdio.h>
Packit Service 20376f
#include <ctype.h>
Packit Service 20376f
Packit Service 20376f
#define LOOKS_LIKE_DRIVE_PREFIX(S) (git__isalpha((S)[0]) && (S)[1] == ':')
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
static bool looks_like_network_computer_name(const char *path, int pos)
Packit Service 20376f
{
Packit Service 20376f
	if (pos < 3)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if (path[0] != '/' || path[1] != '/')
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	while (pos-- > 2) {
Packit Service 20376f
		if (path[pos] == '/')
Packit Service 20376f
			return false;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return true;
Packit Service 20376f
}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Based on the Android implementation, BSD licensed.
Packit Service 20376f
 * http://android.git.kernel.org/
Packit Service 20376f
 *
Packit Service 20376f
 * Copyright (C) 2008 The Android Open Source Project
Packit Service 20376f
 * All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * Redistribution and use in source and binary forms, with or without
Packit Service 20376f
 * modification, are permitted provided that the following conditions
Packit Service 20376f
 * are met:
Packit Service 20376f
 * * Redistributions of source code must retain the above copyright
Packit Service 20376f
 *   notice, this list of conditions and the following disclaimer.
Packit Service 20376f
 * * Redistributions in binary form must reproduce the above copyright
Packit Service 20376f
 *   notice, this list of conditions and the following disclaimer in
Packit Service 20376f
 *   the documentation and/or other materials provided with the
Packit Service 20376f
 *   distribution.
Packit Service 20376f
 *
Packit Service 20376f
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 20376f
 * AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 20376f
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit Service 20376f
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit Service 20376f
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 20376f
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
Packit Service 20376f
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Packit Service 20376f
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
Packit Service 20376f
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit Service 20376f
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
Packit Service 20376f
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit Service 20376f
 * SUCH DAMAGE.
Packit Service 20376f
 */
Packit Service 20376f
int git_path_basename_r(git_buf *buffer, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	const char *endp, *startp;
Packit Service 20376f
	int len, result;
Packit Service 20376f
Packit Service 20376f
	/* Empty or NULL string gets treated as "." */
Packit Service 20376f
	if (path == NULL || *path == '\0') {
Packit Service 20376f
		startp = ".";
Packit Service 20376f
		len		= 1;
Packit Service 20376f
		goto Exit;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Strip trailing slashes */
Packit Service 20376f
	endp = path + strlen(path) - 1;
Packit Service 20376f
	while (endp > path && *endp == '/')
Packit Service 20376f
		endp--;
Packit Service 20376f
Packit Service 20376f
	/* All slashes becomes "/" */
Packit Service 20376f
	if (endp == path && *endp == '/') {
Packit Service 20376f
		startp = "/";
Packit Service 20376f
		len	= 1;
Packit Service 20376f
		goto Exit;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Find the start of the base */
Packit Service 20376f
	startp = endp;
Packit Service 20376f
	while (startp > path && *(startp - 1) != '/')
Packit Service 20376f
		startp--;
Packit Service 20376f
Packit Service 20376f
	/* Cast is safe because max path < max int */
Packit Service 20376f
	len = (int)(endp - startp + 1);
Packit Service 20376f
Packit Service 20376f
Exit:
Packit Service 20376f
	result = len;
Packit Service 20376f
Packit Service 20376f
	if (buffer != NULL && git_buf_set(buffer, startp, len) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return result;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Determine if the path is a Windows prefix and, if so, returns
Packit Service 20376f
 * its actual lentgh. If it is not a prefix, returns -1.
Packit Service 20376f
 */
Packit Service 20376f
static int win32_prefix_length(const char *path, int len)
Packit Service 20376f
{
Packit Service 20376f
#ifndef GIT_WIN32
Packit Service 20376f
	GIT_UNUSED(path);
Packit Service 20376f
	GIT_UNUSED(len);
Packit Service 20376f
#else
Packit Service 20376f
	/*
Packit Service 20376f
	 * Mimic unix behavior where '/.git' returns '/': 'C:/.git' will return
Packit Service 20376f
	 * 'C:/' here
Packit Service 20376f
	 */
Packit Service 20376f
	if (len == 2 && LOOKS_LIKE_DRIVE_PREFIX(path))
Packit Service 20376f
		return 2;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * Similarly checks if we're dealing with a network computer name
Packit Service 20376f
	 * '//computername/.git' will return '//computername/'
Packit Service 20376f
	 */
Packit Service 20376f
	if (looks_like_network_computer_name(path, len))
Packit Service 20376f
		return len;
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Based on the Android implementation, BSD licensed.
Packit Service 20376f
 * Check http://android.git.kernel.org/
Packit Service 20376f
 */
Packit Service 20376f
int git_path_dirname_r(git_buf *buffer, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	const char *endp;
Packit Service 20376f
	int is_prefix = 0, len;
Packit Service 20376f
Packit Service 20376f
	/* Empty or NULL string gets treated as "." */
Packit Service 20376f
	if (path == NULL || *path == '\0') {
Packit Service 20376f
		path = ".";
Packit Service 20376f
		len = 1;
Packit Service 20376f
		goto Exit;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Strip trailing slashes */
Packit Service 20376f
	endp = path + strlen(path) - 1;
Packit Service 20376f
	while (endp > path && *endp == '/')
Packit Service 20376f
		endp--;
Packit Service 20376f
Packit Service 20376f
	if ((len = win32_prefix_length(path, endp - path + 1)) > 0) {
Packit Service 20376f
		is_prefix = 1;
Packit Service 20376f
		goto Exit;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Find the start of the dir */
Packit Service 20376f
	while (endp > path && *endp != '/')
Packit Service 20376f
		endp--;
Packit Service 20376f
Packit Service 20376f
	/* Either the dir is "/" or there are no slashes */
Packit Service 20376f
	if (endp == path) {
Packit Service 20376f
		path = (*endp == '/') ? "/" : ".";
Packit Service 20376f
		len = 1;
Packit Service 20376f
		goto Exit;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	do {
Packit Service 20376f
		endp--;
Packit Service 20376f
	} while (endp > path && *endp == '/');
Packit Service 20376f
Packit Service 20376f
	if ((len = win32_prefix_length(path, endp - path + 1)) > 0) {
Packit Service 20376f
		is_prefix = 1;
Packit Service 20376f
		goto Exit;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Cast is safe because max path < max int */
Packit Service 20376f
	len = (int)(endp - path + 1);
Packit Service 20376f
Packit Service 20376f
Exit:
Packit Service 20376f
	if (buffer) {
Packit Service 20376f
		if (git_buf_set(buffer, path, len) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
		if (is_prefix && git_buf_putc(buffer, '/') < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return len;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
char *git_path_dirname(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	char *dirname;
Packit Service 20376f
Packit Service 20376f
	git_path_dirname_r(&buf, path);
Packit Service 20376f
	dirname = git_buf_detach(&buf;;
Packit Service 20376f
	git_buf_free(&buf;; /* avoid memleak if error occurs */
Packit Service 20376f
Packit Service 20376f
	return dirname;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
char *git_path_basename(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	char *basename;
Packit Service 20376f
Packit Service 20376f
	git_path_basename_r(&buf, path);
Packit Service 20376f
	basename = git_buf_detach(&buf;;
Packit Service 20376f
	git_buf_free(&buf;; /* avoid memleak if error occurs */
Packit Service 20376f
Packit Service 20376f
	return basename;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_path_basename_offset(git_buf *buffer)
Packit Service 20376f
{
Packit Service 20376f
	ssize_t slash;
Packit Service 20376f
Packit Service 20376f
	if (!buffer || buffer->size <= 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	slash = git_buf_rfind_next(buffer, '/');
Packit Service 20376f
Packit Service 20376f
	if (slash >= 0 && buffer->ptr[slash] == '/')
Packit Service 20376f
		return (size_t)(slash + 1);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const char *git_path_topdir(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	size_t len;
Packit Service 20376f
	ssize_t i;
Packit Service 20376f
Packit Service 20376f
	assert(path);
Packit Service 20376f
	len = strlen(path);
Packit Service 20376f
Packit Service 20376f
	if (!len || path[len - 1] != '/')
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	for (i = (ssize_t)len - 2; i >= 0; --i)
Packit Service 20376f
		if (path[i] == '/')
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
	return &path[i + 1];
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_root(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	int offset = 0;
Packit Service 20376f
Packit Service 20376f
	/* Does the root of the path look like a windows drive ? */
Packit Service 20376f
	if (LOOKS_LIKE_DRIVE_PREFIX(path))
Packit Service 20376f
		offset += 2;
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	/* Are we dealing with a windows network path? */
Packit Service 20376f
	else if ((path[0] == '/' && path[1] == '/' && path[2] != '/') ||
Packit Service 20376f
		(path[0] == '\\' && path[1] == '\\' && path[2] != '\\'))
Packit Service 20376f
	{
Packit Service 20376f
		offset += 2;
Packit Service 20376f
Packit Service 20376f
		/* Skip the computer name segment */
Packit Service 20376f
		while (path[offset] && path[offset] != '/' && path[offset] != '\\')
Packit Service 20376f
			offset++;
Packit Service 20376f
	}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	if (path[offset] == '/' || path[offset] == '\\')
Packit Service 20376f
		return offset;
Packit Service 20376f
Packit Service 20376f
	return -1;	/* Not a real error - signals that path is not rooted */
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_path_trim_slashes(git_buf *path)
Packit Service 20376f
{
Packit Service 20376f
	int ceiling = git_path_root(path->ptr) + 1;
Packit Service 20376f
	assert(ceiling >= 0);
Packit Service 20376f
Packit Service 20376f
	while (path->size > (size_t)ceiling) {
Packit Service 20376f
		if (path->ptr[path->size-1] != '/')
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		path->ptr[path->size-1] = '\0';
Packit Service 20376f
		path->size--;
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_join_unrooted(
Packit Service 20376f
	git_buf *path_out, const char *path, const char *base, ssize_t *root_at)
Packit Service 20376f
{
Packit Service 20376f
	ssize_t root;
Packit Service 20376f
Packit Service 20376f
	assert(path && path_out);
Packit Service 20376f
Packit Service 20376f
	root = (ssize_t)git_path_root(path);
Packit Service 20376f
Packit Service 20376f
	if (base != NULL && root < 0) {
Packit Service 20376f
		if (git_buf_joinpath(path_out, base, path) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		root = (ssize_t)strlen(base);
Packit Service 20376f
	} else {
Packit Service 20376f
		if (git_buf_sets(path_out, path) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		if (root < 0)
Packit Service 20376f
			root = 0;
Packit Service 20376f
		else if (base)
Packit Service 20376f
			git_path_equal_or_prefixed(base, path, &root);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (root_at)
Packit Service 20376f
		*root_at = root;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_path_squash_slashes(git_buf *path)
Packit Service 20376f
{
Packit Service 20376f
	char *p, *q;
Packit Service 20376f
Packit Service 20376f
	if (path->size == 0)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	for (p = path->ptr, q = path->ptr; *q; p++, q++) {
Packit Service 20376f
		*p = *q;
Packit Service 20376f
Packit Service 20376f
		while (*q == '/' && *(q+1) == '/') {
Packit Service 20376f
			path->size--;
Packit Service 20376f
			q++;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*p = '\0';
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_prettify(git_buf *path_out, const char *path, const char *base)
Packit Service 20376f
{
Packit Service 20376f
	char buf[GIT_PATH_MAX];
Packit Service 20376f
Packit Service 20376f
	assert(path && path_out);
Packit Service 20376f
Packit Service 20376f
	/* construct path if needed */
Packit Service 20376f
	if (base != NULL && git_path_root(path) < 0) {
Packit Service 20376f
		if (git_buf_joinpath(path_out, base, path) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
		path = path_out->ptr;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (p_realpath(path, buf) == NULL) {
Packit Service 20376f
		/* giterr_set resets the errno when dealing with a GITERR_OS kind of error */
Packit Service 20376f
		int error = (errno == ENOENT || errno == ENOTDIR) ? GIT_ENOTFOUND : -1;
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to resolve path '%s'", path);
Packit Service 20376f
Packit Service 20376f
		git_buf_clear(path_out);
Packit Service 20376f
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return git_buf_sets(path_out, buf);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base)
Packit Service 20376f
{
Packit Service 20376f
	int error = git_path_prettify(path_out, path, base);
Packit Service 20376f
	return (error < 0) ? error : git_path_to_dir(path_out);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_to_dir(git_buf *path)
Packit Service 20376f
{
Packit Service 20376f
	if (path->asize > 0 &&
Packit Service 20376f
		git_buf_len(path) > 0 &&
Packit Service 20376f
		path->ptr[git_buf_len(path) - 1] != '/')
Packit Service 20376f
		git_buf_putc(path, '/');
Packit Service 20376f
Packit Service 20376f
	return git_buf_oom(path) ? -1 : 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_path_string_to_dir(char* path, size_t size)
Packit Service 20376f
{
Packit Service 20376f
	size_t end = strlen(path);
Packit Service 20376f
Packit Service 20376f
	if (end && path[end - 1] != '/' && end < size) {
Packit Service 20376f
		path[end] = '/';
Packit Service 20376f
		path[end + 1] = '\0';
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git__percent_decode(git_buf *decoded_out, const char *input)
Packit Service 20376f
{
Packit Service 20376f
	int len, hi, lo, i;
Packit Service 20376f
	assert(decoded_out && input);
Packit Service 20376f
Packit Service 20376f
	len = (int)strlen(input);
Packit Service 20376f
	git_buf_clear(decoded_out);
Packit Service 20376f
Packit Service 20376f
	for(i = 0; i < len; i++)
Packit Service 20376f
	{
Packit Service 20376f
		char c = input[i];
Packit Service 20376f
Packit Service 20376f
		if (c != '%')
Packit Service 20376f
			goto append;
Packit Service 20376f
Packit Service 20376f
		if (i >= len - 2)
Packit Service 20376f
			goto append;
Packit Service 20376f
Packit Service 20376f
		hi = git__fromhex(input[i + 1]);
Packit Service 20376f
		lo = git__fromhex(input[i + 2]);
Packit Service 20376f
Packit Service 20376f
		if (hi < 0 || lo < 0)
Packit Service 20376f
			goto append;
Packit Service 20376f
Packit Service 20376f
		c = (char)(hi << 4 | lo);
Packit Service 20376f
		i += 2;
Packit Service 20376f
Packit Service 20376f
append:
Packit Service 20376f
		if (git_buf_putc(decoded_out, c) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int error_invalid_local_file_uri(const char *uri)
Packit Service 20376f
{
Packit Service 20376f
	giterr_set(GITERR_CONFIG, "'%s' is not a valid local file URI", uri);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int local_file_url_prefixlen(const char *file_url)
Packit Service 20376f
{
Packit Service 20376f
	int len = -1;
Packit Service 20376f
Packit Service 20376f
	if (git__prefixcmp(file_url, "file://") == 0) {
Packit Service 20376f
		if (file_url[7] == '/')
Packit Service 20376f
			len = 8;
Packit Service 20376f
		else if (git__prefixcmp(file_url + 7, "localhost/") == 0)
Packit Service 20376f
			len = 17;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return len;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_path_is_local_file_url(const char *file_url)
Packit Service 20376f
{
Packit Service 20376f
	return (local_file_url_prefixlen(file_url) > 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_fromurl(git_buf *local_path_out, const char *file_url)
Packit Service 20376f
{
Packit Service 20376f
	int offset;
Packit Service 20376f
Packit Service 20376f
	assert(local_path_out && file_url);
Packit Service 20376f
Packit Service 20376f
	if ((offset = local_file_url_prefixlen(file_url)) < 0 ||
Packit Service 20376f
		file_url[offset] == '\0' || file_url[offset] == '/')
Packit Service 20376f
		return error_invalid_local_file_uri(file_url);
Packit Service 20376f
Packit Service 20376f
#ifndef GIT_WIN32
Packit Service 20376f
	offset--;	/* A *nix absolute path starts with a forward slash */
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(local_path_out);
Packit Service 20376f
	return git__percent_decode(local_path_out, file_url + offset);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_walk_up(
Packit Service 20376f
	git_buf *path,
Packit Service 20376f
	const char *ceiling,
Packit Service 20376f
	int (*cb)(void *data, const char *),
Packit Service 20376f
	void *data)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_buf iter;
Packit Service 20376f
	ssize_t stop = 0, scan;
Packit Service 20376f
	char oldc = '\0';
Packit Service 20376f
Packit Service 20376f
	assert(path && cb);
Packit Service 20376f
Packit Service 20376f
	if (ceiling != NULL) {
Packit Service 20376f
		if (git__prefixcmp(path->ptr, ceiling) == 0)
Packit Service 20376f
			stop = (ssize_t)strlen(ceiling);
Packit Service 20376f
		else
Packit Service 20376f
			stop = git_buf_len(path);
Packit Service 20376f
	}
Packit Service 20376f
	scan = git_buf_len(path);
Packit Service 20376f
Packit Service 20376f
	/* empty path: yield only once */
Packit Service 20376f
	if (!scan) {
Packit Service 20376f
		error = cb(data, "");
Packit Service 20376f
		if (error)
Packit Service 20376f
			giterr_set_after_callback(error);
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	iter.ptr = path->ptr;
Packit Service 20376f
	iter.size = git_buf_len(path);
Packit Service 20376f
	iter.asize = path->asize;
Packit Service 20376f
Packit Service 20376f
	while (scan >= stop) {
Packit Service 20376f
		error = cb(data, iter.ptr);
Packit Service 20376f
		iter.ptr[scan] = oldc;
Packit Service 20376f
Packit Service 20376f
		if (error) {
Packit Service 20376f
			giterr_set_after_callback(error);
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		scan = git_buf_rfind_next(&iter, '/');
Packit Service 20376f
		if (scan >= 0) {
Packit Service 20376f
			scan++;
Packit Service 20376f
			oldc = iter.ptr[scan];
Packit Service 20376f
			iter.size = scan;
Packit Service 20376f
			iter.ptr[scan] = '\0';
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (scan >= 0)
Packit Service 20376f
		iter.ptr[scan] = oldc;
Packit Service 20376f
Packit Service 20376f
	/* relative path: yield for the last component */
Packit Service 20376f
	if (!error && stop == 0 && iter.ptr[0] != '/') {
Packit Service 20376f
		error = cb(data, "");
Packit Service 20376f
		if (error)
Packit Service 20376f
			giterr_set_after_callback(error);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_path_exists(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	assert(path);
Packit Service 20376f
	return p_access(path, F_OK) == 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_path_isdir(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	if (p_stat(path, &st) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	return S_ISDIR(st.st_mode) != 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_path_isfile(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	struct stat st;
Packit Service 20376f
Packit Service 20376f
	assert(path);
Packit Service 20376f
	if (p_stat(path, &st) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	return S_ISREG(st.st_mode) != 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_path_islink(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	struct stat st;
Packit Service 20376f
Packit Service 20376f
	assert(path);
Packit Service 20376f
	if (p_lstat(path, &st) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	return S_ISLNK(st.st_mode) != 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
Packit Service 20376f
bool git_path_is_empty_dir(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_win32_path filter_w;
Packit Service 20376f
	bool empty = false;
Packit Service 20376f
Packit Service 20376f
	if (git_win32__findfirstfile_filter(filter_w, path)) {
Packit Service 20376f
		WIN32_FIND_DATAW findData;
Packit Service 20376f
		HANDLE hFind = FindFirstFileW(filter_w, &findData);
Packit Service 20376f
Packit Service 20376f
		/* FindFirstFile will fail if there are no children to the given
Packit Service 20376f
		 * path, which can happen if the given path is a file (and obviously
Packit Service 20376f
		 * has no children) or if the given path is an empty mount point.
Packit Service 20376f
		 * (Most directories have at least directory entries '.' and '..',
Packit Service 20376f
		 * but ridiculously another volume mounted in another drive letter's
Packit Service 20376f
		 * path space do not, and thus have nothing to enumerate.)  If
Packit Service 20376f
		 * FindFirstFile fails, check if this is a directory-like thing
Packit Service 20376f
		 * (a mount point).
Packit Service 20376f
		 */
Packit Service 20376f
		if (hFind == INVALID_HANDLE_VALUE)
Packit Service 20376f
			return git_path_isdir(path);
Packit Service 20376f
Packit Service 20376f
		/* If the find handle was created successfully, then it's a directory */
Packit Service 20376f
		empty = true;
Packit Service 20376f
Packit Service 20376f
		do {
Packit Service 20376f
			/* Allow the enumeration to return . and .. and still be considered
Packit Service 20376f
			 * empty. In the special case of drive roots (i.e. C:\) where . and
Packit Service 20376f
			 * .. do not occur, we can still consider the path to be an empty
Packit Service 20376f
			 * directory if there's nothing there. */
Packit Service 20376f
			if (!git_path_is_dot_or_dotdotW(findData.cFileName)) {
Packit Service 20376f
				empty = false;
Packit Service 20376f
				break;
Packit Service 20376f
			}
Packit Service 20376f
		} while (FindNextFileW(hFind, &findData));
Packit Service 20376f
Packit Service 20376f
		FindClose(hFind);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return empty;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#else
Packit Service 20376f
Packit Service 20376f
static int path_found_entry(void *payload, git_buf *path)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(payload);
Packit Service 20376f
	return !git_path_is_dot_or_dotdot(path->ptr);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_path_is_empty_dir(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_buf dir = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	if (!git_path_isdir(path))
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_sets(&dir, path)) != 0)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	else
Packit Service 20376f
		error = git_path_direach(&dir, 0, path_found_entry, NULL);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&dir;;
Packit Service 20376f
Packit Service 20376f
	return !error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
int git_path_set_error(int errno_value, const char *path, const char *action)
Packit Service 20376f
{
Packit Service 20376f
	switch (errno_value) {
Packit Service 20376f
	case ENOENT:
Packit Service 20376f
	case ENOTDIR:
Packit Service 20376f
		giterr_set(GITERR_OS, "could not find '%s' to %s", path, action);
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
Packit Service 20376f
	case EINVAL:
Packit Service 20376f
	case ENAMETOOLONG:
Packit Service 20376f
		giterr_set(GITERR_OS, "invalid path for filesystem '%s'", path);
Packit Service 20376f
		return GIT_EINVALIDSPEC;
Packit Service 20376f
Packit Service 20376f
	case EEXIST:
Packit Service 20376f
		giterr_set(GITERR_OS, "failed %s - '%s' already exists", action, path);
Packit Service 20376f
		return GIT_EEXISTS;
Packit Service 20376f
Packit Service 20376f
	case EACCES:
Packit Service 20376f
		giterr_set(GITERR_OS, "failed %s - '%s' is locked", action, path);
Packit Service 20376f
		return GIT_ELOCKED;
Packit Service 20376f
Packit Service 20376f
	default:
Packit Service 20376f
		giterr_set(GITERR_OS, "could not %s '%s'", action, path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_lstat(const char *path, struct stat *st)
Packit Service 20376f
{
Packit Service 20376f
	if (p_lstat(path, st) == 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	return git_path_set_error(errno, path, "stat");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool _check_dir_contents(
Packit Service 20376f
	git_buf *dir,
Packit Service 20376f
	const char *sub,
Packit Service 20376f
	bool (*predicate)(const char *))
Packit Service 20376f
{
Packit Service 20376f
	bool result;
Packit Service 20376f
	size_t dir_size = git_buf_len(dir);
Packit Service 20376f
	size_t sub_size = strlen(sub);
Packit Service 20376f
	size_t alloc_size;
Packit Service 20376f
Packit Service 20376f
	/* leave base valid even if we could not make space for subdir */
Packit Service 20376f
	if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, dir_size, sub_size) ||
Packit Service 20376f
		GIT_ADD_SIZET_OVERFLOW(&alloc_size, alloc_size, 2) ||
Packit Service 20376f
		git_buf_try_grow(dir, alloc_size, false) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	/* save excursion */
Packit Service 20376f
	if (git_buf_joinpath(dir, dir->ptr, sub) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	result = predicate(dir->ptr);
Packit Service 20376f
Packit Service 20376f
	/* restore path */
Packit Service 20376f
	git_buf_truncate(dir, dir_size);
Packit Service 20376f
	return result;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_path_contains(git_buf *dir, const char *item)
Packit Service 20376f
{
Packit Service 20376f
	return _check_dir_contents(dir, item, &git_path_exists);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_path_contains_dir(git_buf *base, const char *subdir)
Packit Service 20376f
{
Packit Service 20376f
	return _check_dir_contents(base, subdir, &git_path_isdir);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_path_contains_file(git_buf *base, const char *file)
Packit Service 20376f
{
Packit Service 20376f
	return _check_dir_contents(base, file, &git_path_isfile);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_find_dir(git_buf *dir, const char *path, const char *base)
Packit Service 20376f
{
Packit Service 20376f
	int error = git_path_join_unrooted(dir, path, base, NULL);
Packit Service 20376f
Packit Service 20376f
	if (!error) {
Packit Service 20376f
		char buf[GIT_PATH_MAX];
Packit Service 20376f
		if (p_realpath(dir->ptr, buf) != NULL)
Packit Service 20376f
			error = git_buf_sets(dir, buf);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* call dirname if this is not a directory */
Packit Service 20376f
	if (!error) /* && git_path_isdir(dir->ptr) == false) */
Packit Service 20376f
		error = (git_path_dirname_r(dir, dir->ptr) < 0) ? -1 : 0;
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		error = git_path_to_dir(dir);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_resolve_relative(git_buf *path, size_t ceiling)
Packit Service 20376f
{
Packit Service 20376f
	char *base, *to, *from, *next;
Packit Service 20376f
	size_t len;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_BUF(path);
Packit Service 20376f
Packit Service 20376f
	if (ceiling > path->size)
Packit Service 20376f
		ceiling = path->size;
Packit Service 20376f
Packit Service 20376f
	/* recognize drive prefixes, etc. that should not be backed over */
Packit Service 20376f
	if (ceiling == 0)
Packit Service 20376f
		ceiling = git_path_root(path->ptr) + 1;
Packit Service 20376f
Packit Service 20376f
	/* recognize URL prefixes that should not be backed over */
Packit Service 20376f
	if (ceiling == 0) {
Packit Service 20376f
		for (next = path->ptr; *next && git__isalpha(*next); ++next);
Packit Service 20376f
		if (next[0] == ':' && next[1] == '/' && next[2] == '/')
Packit Service 20376f
			ceiling = (next + 3) - path->ptr;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	base = to = from = path->ptr + ceiling;
Packit Service 20376f
Packit Service 20376f
	while (*from) {
Packit Service 20376f
		for (next = from; *next && *next != '/'; ++next);
Packit Service 20376f
Packit Service 20376f
		len = next - from;
Packit Service 20376f
Packit Service 20376f
		if (len == 1 && from[0] == '.')
Packit Service 20376f
			/* do nothing with singleton dot */;
Packit Service 20376f
Packit Service 20376f
		else if (len == 2 && from[0] == '.' && from[1] == '.') {
Packit Service 20376f
			/* error out if trying to up one from a hard base */
Packit Service 20376f
			if (to == base && ceiling != 0) {
Packit Service 20376f
				giterr_set(GITERR_INVALID,
Packit Service 20376f
					"cannot strip root component off url");
Packit Service 20376f
				return -1;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			/* no more path segments to strip,
Packit Service 20376f
			 * use '../' as a new base path */
Packit Service 20376f
			if (to == base) {
Packit Service 20376f
				if (*next == '/')
Packit Service 20376f
					len++;
Packit Service 20376f
Packit Service 20376f
				if (to != from)
Packit Service 20376f
					memmove(to, from, len);
Packit Service 20376f
Packit Service 20376f
				to += len;
Packit Service 20376f
				/* this is now the base, can't back up from a
Packit Service 20376f
				 * relative prefix */
Packit Service 20376f
				base = to;
Packit Service 20376f
			} else {
Packit Service 20376f
				/* back up a path segment */
Packit Service 20376f
				while (to > base && to[-1] == '/') to--;
Packit Service 20376f
				while (to > base && to[-1] != '/') to--;
Packit Service 20376f
			}
Packit Service 20376f
		} else {
Packit Service 20376f
			if (*next == '/' && *from != '/')
Packit Service 20376f
				len++;
Packit Service 20376f
Packit Service 20376f
			if (to != from)
Packit Service 20376f
				memmove(to, from, len);
Packit Service 20376f
Packit Service 20376f
			to += len;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		from += len;
Packit Service 20376f
Packit Service 20376f
		while (*from == '/') from++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*to = '\0';
Packit Service 20376f
Packit Service 20376f
	path->size = to - path->ptr;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_apply_relative(git_buf *target, const char *relpath)
Packit Service 20376f
{
Packit Service 20376f
	return git_buf_joinpath(target, git_buf_cstr(target), relpath) ||
Packit Service 20376f
	    git_path_resolve_relative(target, 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_cmp(
Packit Service 20376f
	const char *name1, size_t len1, int isdir1,
Packit Service 20376f
	const char *name2, size_t len2, int isdir2,
Packit Service 20376f
	int (*compare)(const char *, const char *, size_t))
Packit Service 20376f
{
Packit Service 20376f
	unsigned char c1, c2;
Packit Service 20376f
	size_t len = len1 < len2 ? len1 : len2;
Packit Service 20376f
	int cmp;
Packit Service 20376f
Packit Service 20376f
	cmp = compare(name1, name2, len);
Packit Service 20376f
	if (cmp)
Packit Service 20376f
		return cmp;
Packit Service 20376f
Packit Service 20376f
	c1 = name1[len];
Packit Service 20376f
	c2 = name2[len];
Packit Service 20376f
Packit Service 20376f
	if (c1 == '\0' && isdir1)
Packit Service 20376f
		c1 = '/';
Packit Service 20376f
Packit Service 20376f
	if (c2 == '\0' && isdir2)
Packit Service 20376f
		c2 = '/';
Packit Service 20376f
Packit Service 20376f
	return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_path_common_dirlen(const char *one, const char *two)
Packit Service 20376f
{
Packit Service 20376f
	const char *p, *q, *dirsep = NULL;
Packit Service 20376f
Packit Service 20376f
	for (p = one, q = two; *p && *q; p++, q++) {
Packit Service 20376f
		if (*p == '/' && *q == '/')
Packit Service 20376f
			dirsep = p;
Packit Service 20376f
		else if (*p != *q)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return dirsep ? (dirsep - one) + 1 : 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_make_relative(git_buf *path, const char *parent)
Packit Service 20376f
{
Packit Service 20376f
	const char *p, *q, *p_dirsep, *q_dirsep;
Packit Service 20376f
	size_t plen = path->size, newlen, alloclen, depth = 1, i, offset;
Packit Service 20376f
Packit Service 20376f
	for (p_dirsep = p = path->ptr, q_dirsep = q = parent; *p && *q; p++, q++) {
Packit Service 20376f
		if (*p == '/' && *q == '/') {
Packit Service 20376f
			p_dirsep = p;
Packit Service 20376f
			q_dirsep = q;
Packit Service 20376f
		}
Packit Service 20376f
		else if (*p != *q)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* need at least 1 common path segment */
Packit Service 20376f
	if ((p_dirsep == path->ptr || q_dirsep == parent) &&
Packit Service 20376f
		(*p_dirsep != '/' || *q_dirsep != '/')) {
Packit Service 20376f
		giterr_set(GITERR_INVALID,
Packit Service 20376f
			"%s is not a parent of %s", parent, path->ptr);
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (*p == '/' && !*q)
Packit Service 20376f
		p++;
Packit Service 20376f
	else if (!*p && *q == '/')
Packit Service 20376f
		q++;
Packit Service 20376f
	else if (!*p && !*q)
Packit Service 20376f
		return git_buf_clear(path), 0;
Packit Service 20376f
	else {
Packit Service 20376f
		p = p_dirsep + 1;
Packit Service 20376f
		q = q_dirsep + 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	plen -= (p - path->ptr);
Packit Service 20376f
Packit Service 20376f
	if (!*q)
Packit Service 20376f
		return git_buf_set(path, p, plen);
Packit Service 20376f
Packit Service 20376f
	for (; (q = strchr(q, '/')) && *(q + 1); q++)
Packit Service 20376f
		depth++;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_MULTIPLY(&newlen, depth, 3);
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&newlen, newlen, plen);
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, newlen, 1);
Packit Service 20376f
Packit Service 20376f
	/* save the offset as we might realllocate the pointer */
Packit Service 20376f
	offset = p - path->ptr;
Packit Service 20376f
	if (git_buf_try_grow(path, alloclen, 1) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
	p = path->ptr + offset;
Packit Service 20376f
Packit Service 20376f
	memmove(path->ptr + (depth * 3), p, plen + 1);
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < depth; i++)
Packit Service 20376f
		memcpy(path->ptr + (i * 3), "../", 3);
Packit Service 20376f
Packit Service 20376f
	path->size = newlen;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_path_has_non_ascii(const char *path, size_t pathlen)
Packit Service 20376f
{
Packit Service 20376f
	const uint8_t *scan = (const uint8_t *)path, *end;
Packit Service 20376f
Packit Service 20376f
	for (end = scan + pathlen; scan < end; ++scan)
Packit Service 20376f
		if (*scan & 0x80)
Packit Service 20376f
			return true;
Packit Service 20376f
Packit Service 20376f
	return false;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_USE_ICONV
Packit Service 20376f
Packit Service 20376f
int git_path_iconv_init_precompose(git_path_iconv_t *ic)
Packit Service 20376f
{
Packit Service 20376f
	git_buf_init(&ic->buf, 0);
Packit Service 20376f
	ic->map = iconv_open(GIT_PATH_REPO_ENCODING, GIT_PATH_NATIVE_ENCODING);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_path_iconv_clear(git_path_iconv_t *ic)
Packit Service 20376f
{
Packit Service 20376f
	if (ic) {
Packit Service 20376f
		if (ic->map != (iconv_t)-1)
Packit Service 20376f
			iconv_close(ic->map);
Packit Service 20376f
		git_buf_free(&ic->buf);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_iconv(git_path_iconv_t *ic, const char **in, size_t *inlen)
Packit Service 20376f
{
Packit Service 20376f
	char *nfd = (char*)*in, *nfc;
Packit Service 20376f
	size_t nfdlen = *inlen, nfclen, wantlen = nfdlen, alloclen, rv;
Packit Service 20376f
	int retry = 1;
Packit Service 20376f
Packit Service 20376f
	if (!ic || ic->map == (iconv_t)-1 ||
Packit Service 20376f
		!git_path_has_non_ascii(*in, *inlen))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(&ic->buf);
Packit Service 20376f
Packit Service 20376f
	while (1) {
Packit Service 20376f
		GITERR_CHECK_ALLOC_ADD(&alloclen, wantlen, 1);
Packit Service 20376f
		if (git_buf_grow(&ic->buf, alloclen) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		nfc    = ic->buf.ptr   + ic->buf.size;
Packit Service 20376f
		nfclen = ic->buf.asize - ic->buf.size;
Packit Service 20376f
Packit Service 20376f
		rv = iconv(ic->map, &nfd, &nfdlen, &nfc, &nfclen);
Packit Service 20376f
Packit Service 20376f
		ic->buf.size = (nfc - ic->buf.ptr);
Packit Service 20376f
Packit Service 20376f
		if (rv != (size_t)-1)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		/* if we cannot convert the data (probably because iconv thinks
Packit Service 20376f
		 * it is not valid UTF-8 source data), then use original data
Packit Service 20376f
		 */
Packit Service 20376f
		if (errno != E2BIG)
Packit Service 20376f
			return 0;
Packit Service 20376f
Packit Service 20376f
		/* make space for 2x the remaining data to be converted
Packit Service 20376f
		 * (with per retry overhead to avoid infinite loops)
Packit Service 20376f
		 */
Packit Service 20376f
		wantlen = ic->buf.size + max(nfclen, nfdlen) * 2 + (size_t)(retry * 4);
Packit Service 20376f
Packit Service 20376f
		if (retry++ > 4)
Packit Service 20376f
			goto fail;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	ic->buf.ptr[ic->buf.size] = '\0';
Packit Service 20376f
Packit Service 20376f
	*in    = ic->buf.ptr;
Packit Service 20376f
	*inlen = ic->buf.size;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
fail:
Packit Service 20376f
	giterr_set(GITERR_OS, "unable to convert unicode path data");
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static const char *nfc_file = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D.XXXXXX";
Packit Service 20376f
static const char *nfd_file = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D.XXXXXX";
Packit Service 20376f
Packit Service 20376f
/* Check if the platform is decomposing unicode data for us.  We will
Packit Service 20376f
 * emulate core Git and prefer to use precomposed unicode data internally
Packit Service 20376f
 * on these platforms, composing the decomposed unicode on the fly.
Packit Service 20376f
 *
Packit Service 20376f
 * This mainly happens on the Mac where HDFS stores filenames as
Packit Service 20376f
 * decomposed unicode.  Even on VFAT and SAMBA file systems, the Mac will
Packit Service 20376f
 * return decomposed unicode from readdir() even when the actual
Packit Service 20376f
 * filesystem is storing precomposed unicode.
Packit Service 20376f
 */
Packit Service 20376f
bool git_path_does_fs_decompose_unicode(const char *root)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	int fd;
Packit Service 20376f
	bool found_decomposed = false;
Packit Service 20376f
	char tmp[6];
Packit Service 20376f
Packit Service 20376f
	/* Create a file using a precomposed path and then try to find it
Packit Service 20376f
	 * using the decomposed name.  If the lookup fails, then we will mark
Packit Service 20376f
	 * that we should precompose unicode for this repository.
Packit Service 20376f
	 */
Packit Service 20376f
	if (git_buf_joinpath(&path, root, nfc_file) < 0 ||
Packit Service 20376f
		(fd = p_mkstemp(path.ptr)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
	p_close(fd);
Packit Service 20376f
Packit Service 20376f
	/* record trailing digits generated by mkstemp */
Packit Service 20376f
	memcpy(tmp, path.ptr + path.size - sizeof(tmp), sizeof(tmp));
Packit Service 20376f
Packit Service 20376f
	/* try to look up as NFD path */
Packit Service 20376f
	if (git_buf_joinpath(&path, root, nfd_file) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
	memcpy(path.ptr + path.size - sizeof(tmp), tmp, sizeof(tmp));
Packit Service 20376f
Packit Service 20376f
	found_decomposed = git_path_exists(path.ptr);
Packit Service 20376f
Packit Service 20376f
	/* remove temporary file (using original precomposed path) */
Packit Service 20376f
	if (git_buf_joinpath(&path, root, nfc_file) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
	memcpy(path.ptr + path.size - sizeof(tmp), tmp, sizeof(tmp));
Packit Service 20376f
Packit Service 20376f
	(void)p_unlink(path.ptr);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	return found_decomposed;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#else
Packit Service 20376f
Packit Service 20376f
bool git_path_does_fs_decompose_unicode(const char *root)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(root);
Packit Service 20376f
	return false;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#if defined(__sun) || defined(__GNU__)
Packit Service 20376f
typedef char path_dirent_data[sizeof(struct dirent) + FILENAME_MAX + 1];
Packit Service 20376f
#else
Packit Service 20376f
typedef struct dirent path_dirent_data;
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
int git_path_direach(
Packit Service 20376f
	git_buf *path,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	int (*fn)(void *, git_buf *),
Packit Service 20376f
	void *arg)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	ssize_t wd_len;
Packit Service 20376f
	DIR *dir;
Packit Service 20376f
	struct dirent *de;
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_USE_ICONV
Packit Service 20376f
	git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(flags);
Packit Service 20376f
Packit Service 20376f
	if (git_path_to_dir(path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	wd_len = git_buf_len(path);
Packit Service 20376f
Packit Service 20376f
	if ((dir = opendir(path->ptr)) == NULL) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to open directory '%s'", path->ptr);
Packit Service 20376f
		if (errno == ENOENT)
Packit Service 20376f
			return GIT_ENOTFOUND;
Packit Service 20376f
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_USE_ICONV
Packit Service 20376f
	if ((flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
Packit Service 20376f
		(void)git_path_iconv_init_precompose(&ic);
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	while ((de = readdir(dir)) != NULL) {
Packit Service 20376f
		const char *de_path = de->d_name;
Packit Service 20376f
		size_t de_len = strlen(de_path);
Packit Service 20376f
Packit Service 20376f
		if (git_path_is_dot_or_dotdot(de_path))
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_USE_ICONV
Packit Service 20376f
		if ((error = git_path_iconv(&ic, &de_path, &de_len)) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
		if ((error = git_buf_put(path, de_path, de_len)) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
		error = fn(arg, path);
Packit Service 20376f
Packit Service 20376f
		git_buf_truncate(path, wd_len); /* restore path */
Packit Service 20376f
Packit Service 20376f
		/* Only set our own error if the callback did not set one already */
Packit Service 20376f
		if (error != 0) {
Packit Service 20376f
			if (!giterr_last())
Packit Service 20376f
				giterr_set_after_callback(error);
Packit Service 20376f
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	closedir(dir);
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_USE_ICONV
Packit Service 20376f
	git_path_iconv_clear(&ic);
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#if defined(GIT_WIN32) && !defined(__MINGW32__)
Packit Service 20376f
Packit Service 20376f
/* Using _FIND_FIRST_EX_LARGE_FETCH may increase performance in Windows 7
Packit Service 20376f
 * and better.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef FIND_FIRST_EX_LARGE_FETCH
Packit Service 20376f
# define FIND_FIRST_EX_LARGE_FETCH 2
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
int git_path_diriter_init(
Packit Service 20376f
	git_path_diriter *diriter,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	unsigned int flags)
Packit Service 20376f
{
Packit Service 20376f
	git_win32_path path_filter;
Packit Service 20376f
Packit Service 20376f
	static int is_win7_or_later = -1;
Packit Service 20376f
	if (is_win7_or_later < 0)
Packit Service 20376f
		is_win7_or_later = git_has_win32_version(6, 1, 0);
Packit Service 20376f
Packit Service 20376f
	assert(diriter && path);
Packit Service 20376f
Packit Service 20376f
	memset(diriter, 0, sizeof(git_path_diriter));
Packit Service 20376f
	diriter->handle = INVALID_HANDLE_VALUE;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_puts(&diriter->path_utf8, path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	git_path_trim_slashes(&diriter->path_utf8);
Packit Service 20376f
Packit Service 20376f
	if (diriter->path_utf8.size == 0) {
Packit Service 20376f
		giterr_set(GITERR_FILESYSTEM, "could not open directory '%s'", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((diriter->parent_len = git_win32_path_from_utf8(diriter->path, diriter->path_utf8.ptr)) < 0 ||
Packit Service 20376f
			!git_win32__findfirstfile_filter(path_filter, diriter->path_utf8.ptr)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "could not parse the directory path '%s'", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	diriter->handle = FindFirstFileExW(
Packit Service 20376f
		path_filter,
Packit Service 20376f
		is_win7_or_later ? FindExInfoBasic : FindExInfoStandard,
Packit Service 20376f
		&diriter->current,
Packit Service 20376f
		FindExSearchNameMatch,
Packit Service 20376f
		NULL,
Packit Service 20376f
		is_win7_or_later ? FIND_FIRST_EX_LARGE_FETCH : 0);
Packit Service 20376f
Packit Service 20376f
	if (diriter->handle == INVALID_HANDLE_VALUE) {
Packit Service 20376f
		giterr_set(GITERR_OS, "could not open directory '%s'", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	diriter->parent_utf8_len = diriter->path_utf8.size;
Packit Service 20376f
	diriter->flags = flags;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int diriter_update_paths(git_path_diriter *diriter)
Packit Service 20376f
{
Packit Service 20376f
	size_t filename_len, path_len;
Packit Service 20376f
Packit Service 20376f
	filename_len = wcslen(diriter->current.cFileName);
Packit Service 20376f
Packit Service 20376f
	if (GIT_ADD_SIZET_OVERFLOW(&path_len, diriter->parent_len, filename_len) ||
Packit Service 20376f
		GIT_ADD_SIZET_OVERFLOW(&path_len, path_len, 2))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (path_len > GIT_WIN_PATH_UTF16) {
Packit Service 20376f
		giterr_set(GITERR_FILESYSTEM,
Packit Service 20376f
			"invalid path '%.*ls\\%ls' (path too long)",
Packit Service 20376f
			diriter->parent_len, diriter->path, diriter->current.cFileName);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	diriter->path[diriter->parent_len] = L'\\';
Packit Service 20376f
	memcpy(&diriter->path[diriter->parent_len+1],
Packit Service 20376f
		diriter->current.cFileName, filename_len * sizeof(wchar_t));
Packit Service 20376f
	diriter->path[path_len-1] = L'\0';
Packit Service 20376f
Packit Service 20376f
	git_buf_truncate(&diriter->path_utf8, diriter->parent_utf8_len);
Packit Service 20376f
Packit Service 20376f
	if (diriter->parent_utf8_len > 0 &&
Packit Service 20376f
		diriter->path_utf8.ptr[diriter->parent_utf8_len-1] != '/')
Packit Service 20376f
		git_buf_putc(&diriter->path_utf8, '/');
Packit Service 20376f
Packit Service 20376f
	git_buf_put_w(&diriter->path_utf8, diriter->current.cFileName, filename_len);
Packit Service 20376f
Packit Service 20376f
	if (git_buf_oom(&diriter->path_utf8))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_diriter_next(git_path_diriter *diriter)
Packit Service 20376f
{
Packit Service 20376f
	bool skip_dot = !(diriter->flags & GIT_PATH_DIR_INCLUDE_DOT_AND_DOTDOT);
Packit Service 20376f
Packit Service 20376f
	do {
Packit Service 20376f
		/* Our first time through, we already have the data from
Packit Service 20376f
		 * FindFirstFileW.  Use it, otherwise get the next file.
Packit Service 20376f
		 */
Packit Service 20376f
		if (!diriter->needs_next)
Packit Service 20376f
			diriter->needs_next = 1;
Packit Service 20376f
		else if (!FindNextFileW(diriter->handle, &diriter->current))
Packit Service 20376f
			return GIT_ITEROVER;
Packit Service 20376f
	} while (skip_dot && git_path_is_dot_or_dotdotW(diriter->current.cFileName));
Packit Service 20376f
Packit Service 20376f
	if (diriter_update_paths(diriter) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_diriter_filename(
Packit Service 20376f
	const char **out,
Packit Service 20376f
	size_t *out_len,
Packit Service 20376f
	git_path_diriter *diriter)
Packit Service 20376f
{
Packit Service 20376f
	assert(out && out_len && diriter);
Packit Service 20376f
Packit Service 20376f
	assert(diriter->path_utf8.size > diriter->parent_utf8_len);
Packit Service 20376f
Packit Service 20376f
	*out = &diriter->path_utf8.ptr[diriter->parent_utf8_len+1];
Packit Service 20376f
	*out_len = diriter->path_utf8.size - diriter->parent_utf8_len - 1;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_diriter_fullpath(
Packit Service 20376f
	const char **out,
Packit Service 20376f
	size_t *out_len,
Packit Service 20376f
	git_path_diriter *diriter)
Packit Service 20376f
{
Packit Service 20376f
	assert(out && out_len && diriter);
Packit Service 20376f
Packit Service 20376f
	*out = diriter->path_utf8.ptr;
Packit Service 20376f
	*out_len = diriter->path_utf8.size;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_diriter_stat(struct stat *out, git_path_diriter *diriter)
Packit Service 20376f
{
Packit Service 20376f
	assert(out && diriter);
Packit Service 20376f
Packit Service 20376f
	return git_win32__file_attribute_to_stat(out,
Packit Service 20376f
		(WIN32_FILE_ATTRIBUTE_DATA *)&diriter->current,
Packit Service 20376f
		diriter->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_path_diriter_free(git_path_diriter *diriter)
Packit Service 20376f
{
Packit Service 20376f
	if (diriter == NULL)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&diriter->path_utf8);
Packit Service 20376f
Packit Service 20376f
	if (diriter->handle != INVALID_HANDLE_VALUE) {
Packit Service 20376f
		FindClose(diriter->handle);
Packit Service 20376f
		diriter->handle = INVALID_HANDLE_VALUE;
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#else
Packit Service 20376f
Packit Service 20376f
int git_path_diriter_init(
Packit Service 20376f
	git_path_diriter *diriter,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	unsigned int flags)
Packit Service 20376f
{
Packit Service 20376f
	assert(diriter && path);
Packit Service 20376f
Packit Service 20376f
	memset(diriter, 0, sizeof(git_path_diriter));
Packit Service 20376f
Packit Service 20376f
	if (git_buf_puts(&diriter->path, path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	git_path_trim_slashes(&diriter->path);
Packit Service 20376f
Packit Service 20376f
	if (diriter->path.size == 0) {
Packit Service 20376f
		giterr_set(GITERR_FILESYSTEM, "could not open directory '%s'", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((diriter->dir = opendir(diriter->path.ptr)) == NULL) {
Packit Service 20376f
		git_buf_free(&diriter->path);
Packit Service 20376f
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to open directory '%s'", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_USE_ICONV
Packit Service 20376f
	if ((flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
Packit Service 20376f
		(void)git_path_iconv_init_precompose(&diriter->ic);
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	diriter->parent_len = diriter->path.size;
Packit Service 20376f
	diriter->flags = flags;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_diriter_next(git_path_diriter *diriter)
Packit Service 20376f
{
Packit Service 20376f
	struct dirent *de;
Packit Service 20376f
	const char *filename;
Packit Service 20376f
	size_t filename_len;
Packit Service 20376f
	bool skip_dot = !(diriter->flags & GIT_PATH_DIR_INCLUDE_DOT_AND_DOTDOT);
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(diriter);
Packit Service 20376f
Packit Service 20376f
	errno = 0;
Packit Service 20376f
Packit Service 20376f
	do {
Packit Service 20376f
		if ((de = readdir(diriter->dir)) == NULL) {
Packit Service 20376f
			if (!errno)
Packit Service 20376f
				return GIT_ITEROVER;
Packit Service 20376f
Packit Service 20376f
			giterr_set(GITERR_OS,
Packit Service 20376f
				"could not read directory '%s'", diriter->path.ptr);
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	} while (skip_dot && git_path_is_dot_or_dotdot(de->d_name));
Packit Service 20376f
Packit Service 20376f
	filename = de->d_name;
Packit Service 20376f
	filename_len = strlen(filename);
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_USE_ICONV
Packit Service 20376f
	if ((diriter->flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0 &&
Packit Service 20376f
		(error = git_path_iconv(&diriter->ic, &filename, &filename_len)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	git_buf_truncate(&diriter->path, diriter->parent_len);
Packit Service 20376f
Packit Service 20376f
	if (diriter->parent_len > 0 &&
Packit Service 20376f
		diriter->path.ptr[diriter->parent_len-1] != '/')
Packit Service 20376f
		git_buf_putc(&diriter->path, '/');
Packit Service 20376f
Packit Service 20376f
	git_buf_put(&diriter->path, filename, filename_len);
Packit Service 20376f
Packit Service 20376f
	if (git_buf_oom(&diriter->path))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_diriter_filename(
Packit Service 20376f
	const char **out,
Packit Service 20376f
	size_t *out_len,
Packit Service 20376f
	git_path_diriter *diriter)
Packit Service 20376f
{
Packit Service 20376f
	assert(out && out_len && diriter);
Packit Service 20376f
Packit Service 20376f
	assert(diriter->path.size > diriter->parent_len);
Packit Service 20376f
Packit Service 20376f
	*out = &diriter->path.ptr[diriter->parent_len+1];
Packit Service 20376f
	*out_len = diriter->path.size - diriter->parent_len - 1;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_diriter_fullpath(
Packit Service 20376f
	const char **out,
Packit Service 20376f
	size_t *out_len,
Packit Service 20376f
	git_path_diriter *diriter)
Packit Service 20376f
{
Packit Service 20376f
	assert(out && out_len && diriter);
Packit Service 20376f
Packit Service 20376f
	*out = diriter->path.ptr;
Packit Service 20376f
	*out_len = diriter->path.size;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_diriter_stat(struct stat *out, git_path_diriter *diriter)
Packit Service 20376f
{
Packit Service 20376f
	assert(out && diriter);
Packit Service 20376f
Packit Service 20376f
	return git_path_lstat(diriter->path.ptr, out);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_path_diriter_free(git_path_diriter *diriter)
Packit Service 20376f
{
Packit Service 20376f
	if (diriter == NULL)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	if (diriter->dir) {
Packit Service 20376f
		closedir(diriter->dir);
Packit Service 20376f
		diriter->dir = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_USE_ICONV
Packit Service 20376f
	git_path_iconv_clear(&diriter->ic);
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&diriter->path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
int git_path_dirload(
Packit Service 20376f
	git_vector *contents,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	size_t prefix_len,
Packit Service 20376f
	uint32_t flags)
Packit Service 20376f
{
Packit Service 20376f
	git_path_diriter iter = GIT_PATH_DIRITER_INIT;
Packit Service 20376f
	const char *name;
Packit Service 20376f
	size_t name_len;
Packit Service 20376f
	char *dup;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(contents && path);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_path_diriter_init(&iter, path, flags)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	while ((error = git_path_diriter_next(&iter)) == 0) {
Packit Service 20376f
		if ((error = git_path_diriter_fullpath(&name, &name_len, &iter)) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		assert(name_len > prefix_len);
Packit Service 20376f
Packit Service 20376f
		dup = git__strndup(name + prefix_len, name_len - prefix_len);
Packit Service 20376f
		GITERR_CHECK_ALLOC(dup);
Packit Service 20376f
Packit Service 20376f
		if ((error = git_vector_insert(contents, dup)) < 0)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ITEROVER)
Packit Service 20376f
		error = 0;
Packit Service 20376f
Packit Service 20376f
	git_path_diriter_free(&iter);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_from_url_or_path(git_buf *local_path_out, const char *url_or_path)
Packit Service 20376f
{
Packit Service 20376f
	if (git_path_is_local_file_url(url_or_path))
Packit Service 20376f
		return git_path_fromurl(local_path_out, url_or_path);
Packit Service 20376f
	else
Packit Service 20376f
		return git_buf_sets(local_path_out, url_or_path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Reject paths like AUX or COM1, or those versions that end in a dot or
Packit Service 20376f
 * colon.  ("AUX." or "AUX:")
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(bool) verify_dospath(
Packit Service 20376f
	const char *component,
Packit Service 20376f
	size_t len,
Packit Service 20376f
	const char dospath[3],
Packit Service 20376f
	bool trailing_num)
Packit Service 20376f
{
Packit Service 20376f
	size_t last = trailing_num ? 4 : 3;
Packit Service 20376f
Packit Service 20376f
	if (len < last || git__strncasecmp(component, dospath, 3) != 0)
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	if (trailing_num && (component[3] < '1' || component[3] > '9'))
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	return (len > last &&
Packit Service 20376f
		component[last] != '.' &&
Packit Service 20376f
		component[last] != ':');
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int32_t next_hfs_char(const char **in, size_t *len)
Packit Service 20376f
{
Packit Service 20376f
	while (*len) {
Packit Service 20376f
		int32_t codepoint;
Packit Service 20376f
		int cp_len = git__utf8_iterate((const uint8_t *)(*in), (int)(*len), &codepoint);
Packit Service 20376f
		if (cp_len < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		(*in) += cp_len;
Packit Service 20376f
		(*len) -= cp_len;
Packit Service 20376f
Packit Service 20376f
		/* these code points are ignored completely */
Packit Service 20376f
		switch (codepoint) {
Packit Service 20376f
		case 0x200c: /* ZERO WIDTH NON-JOINER */
Packit Service 20376f
		case 0x200d: /* ZERO WIDTH JOINER */
Packit Service 20376f
		case 0x200e: /* LEFT-TO-RIGHT MARK */
Packit Service 20376f
		case 0x200f: /* RIGHT-TO-LEFT MARK */
Packit Service 20376f
		case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */
Packit Service 20376f
		case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */
Packit Service 20376f
		case 0x202c: /* POP DIRECTIONAL FORMATTING */
Packit Service 20376f
		case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */
Packit Service 20376f
		case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */
Packit Service 20376f
		case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */
Packit Service 20376f
		case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */
Packit Service 20376f
		case 0x206c: /* INHIBIT ARABIC FORM SHAPING */
Packit Service 20376f
		case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */
Packit Service 20376f
		case 0x206e: /* NATIONAL DIGIT SHAPES */
Packit Service 20376f
		case 0x206f: /* NOMINAL DIGIT SHAPES */
Packit Service 20376f
		case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* fold into lowercase -- this will only fold characters in
Packit Service 20376f
		 * the ASCII range, which is perfectly fine, because the
Packit Service 20376f
		 * git folder name can only be composed of ascii characters
Packit Service 20376f
		 */
Packit Service 20376f
		return git__tolower(codepoint);
Packit Service 20376f
	}
Packit Service 20376f
	return 0; /* NULL byte -- end of string */
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool verify_dotgit_hfs_generic(const char *path, size_t len, const char *needle, size_t needle_len)
Packit Service 20376f
{
Packit Service 20376f
	size_t i;
Packit Service 20376f
	char c;
Packit Service 20376f
Packit Service 20376f
	if (next_hfs_char(&path, &len) != '.')
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < needle_len; i++) {
Packit Service 20376f
		c = next_hfs_char(&path, &len;;
Packit Service 20376f
		if (c != needle[i])
Packit Service 20376f
			return true;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (next_hfs_char(&path, &len) != '\0')
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	return false;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool verify_dotgit_hfs(const char *path, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	return verify_dotgit_hfs_generic(path, len, "git", CONST_STRLEN("git"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) verify_dotgit_ntfs(git_repository *repo, const char *path, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_buf *reserved = git_repository__reserved_names_win32;
Packit Service 20376f
	size_t reserved_len = git_repository__reserved_names_win32_len;
Packit Service 20376f
	size_t start = 0, i;
Packit Service 20376f
Packit Service 20376f
	if (repo)
Packit Service 20376f
		git_repository__reserved_names(&reserved, &reserved_len, repo, true);
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < reserved_len; i++) {
Packit Service 20376f
		git_buf *r = &reserved[i];
Packit Service 20376f
Packit Service 20376f
		if (len >= r->size &&
Packit Service 20376f
			strncasecmp(path, r->ptr, r->size) == 0) {
Packit Service 20376f
			start = r->size;
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!start)
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	/* Reject paths like ".git\" */
Packit Service 20376f
	if (path[start] == '\\')
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	/* Reject paths like '.git ' or '.git.' */
Packit Service 20376f
	for (i = start; i < len; i++) {
Packit Service 20376f
		if (path[i] != ' ' && path[i] != '.')
Packit Service 20376f
			return true;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return false;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) only_spaces_and_dots(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	const char *c = path;
Packit Service 20376f
Packit Service 20376f
	for (;; c++) {
Packit Service 20376f
		if (*c == '\0')
Packit Service 20376f
			return true;
Packit Service 20376f
		if (*c != ' ' && *c != '.')
Packit Service 20376f
			return false;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return true;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) verify_dotgit_ntfs_generic(const char *name, size_t len, const char *dotgit_name, size_t dotgit_len, const char *shortname_pfix)
Packit Service 20376f
{
Packit Service 20376f
	int i, saw_tilde;
Packit Service 20376f
Packit Service 20376f
	if (name[0] == '.' && len >= dotgit_len &&
Packit Service 20376f
	    !strncasecmp(name + 1, dotgit_name, dotgit_len)) {
Packit Service 20376f
		return !only_spaces_and_dots(name + dotgit_len + 1);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Detect the basic NTFS shortname with the first six chars */
Packit Service 20376f
	if (!strncasecmp(name, dotgit_name, 6) && name[6] == '~' &&
Packit Service 20376f
	    name[7] >= '1' && name[7] <= '4')
Packit Service 20376f
		return !only_spaces_and_dots(name + 8);
Packit Service 20376f
Packit Service 20376f
	/* Catch fallback names */
Packit Service 20376f
	for (i = 0, saw_tilde = 0; i < 8; i++) {
Packit Service 20376f
		if (name[i] == '\0') {
Packit Service 20376f
			return true;
Packit Service 20376f
		} else if (saw_tilde) {
Packit Service 20376f
			if (name[i] < '0' || name[i] > '9')
Packit Service 20376f
				return true;
Packit Service 20376f
		} else if (name[i] == '~') {
Packit Service 20376f
			if (name[i+1] < '1' || name[i+1]  > '9')
Packit Service 20376f
				return true;
Packit Service 20376f
			saw_tilde = 1;
Packit Service 20376f
		} else if (i >= 6) {
Packit Service 20376f
			return true;
Packit Service 20376f
		} else if (name[i] < 0) {
Packit Service 20376f
			return true;
Packit Service 20376f
		} else if (git__tolower(name[i]) != shortname_pfix[i]) {
Packit Service 20376f
			return true;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return !only_spaces_and_dots(name + i);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) verify_char(unsigned char c, unsigned int flags)
Packit Service 20376f
{
Packit Service 20376f
	if ((flags & GIT_PATH_REJECT_BACKSLASH) && c == '\\')
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if ((flags & GIT_PATH_REJECT_SLASH) && c == '/')
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if (flags & GIT_PATH_REJECT_NT_CHARS) {
Packit Service 20376f
		if (c < 32)
Packit Service 20376f
			return false;
Packit Service 20376f
Packit Service 20376f
		switch (c) {
Packit Service 20376f
		case '<':
Packit Service 20376f
		case '>':
Packit Service 20376f
		case ':':
Packit Service 20376f
		case '"':
Packit Service 20376f
		case '|':
Packit Service 20376f
		case '?':
Packit Service 20376f
		case '*':
Packit Service 20376f
			return false;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return true;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Return the length of the common prefix between str and prefix, comparing them
Packit Service 20376f
 * case-insensitively (must be ASCII to match).
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(size_t) common_prefix_icase(const char *str, size_t len, const char *prefix)
Packit Service 20376f
{
Packit Service 20376f
	size_t count = 0;
Packit Service 20376f
Packit Service 20376f
	while (len >0 && tolower(*str) == tolower(*prefix)) {
Packit Service 20376f
		count++;
Packit Service 20376f
		str++;
Packit Service 20376f
		prefix++;
Packit Service 20376f
		len--;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return count;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * We fundamentally don't like some paths when dealing with user-inputted
Packit Service 20376f
 * strings (in checkout or ref names): we don't want dot or dot-dot
Packit Service 20376f
 * anywhere, we want to avoid writing weird paths on Windows that can't
Packit Service 20376f
 * be handled by tools that use the non-\\?\ APIs, we don't want slashes
Packit Service 20376f
 * or double slashes at the end of paths that can make them ambiguous.
Packit Service 20376f
 *
Packit Service 20376f
 * For checkout, we don't want to recurse into ".git" either.
Packit Service 20376f
 */
Packit Service 20376f
static bool verify_component(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *component,
Packit Service 20376f
	size_t len,
Packit Service 20376f
	uint16_t mode,
Packit Service 20376f
	unsigned int flags)
Packit Service 20376f
{
Packit Service 20376f
	if (len == 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if ((flags & GIT_PATH_REJECT_TRAVERSAL) &&
Packit Service 20376f
		len == 1 && component[0] == '.')
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if ((flags & GIT_PATH_REJECT_TRAVERSAL) &&
Packit Service 20376f
		len == 2 && component[0] == '.' && component[1] == '.')
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if ((flags & GIT_PATH_REJECT_TRAILING_DOT) && component[len-1] == '.')
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if ((flags & GIT_PATH_REJECT_TRAILING_SPACE) && component[len-1] == ' ')
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if ((flags & GIT_PATH_REJECT_TRAILING_COLON) && component[len-1] == ':')
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if (flags & GIT_PATH_REJECT_DOS_PATHS) {
Packit Service 20376f
		if (!verify_dospath(component, len, "CON", false) ||
Packit Service 20376f
			!verify_dospath(component, len, "PRN", false) ||
Packit Service 20376f
			!verify_dospath(component, len, "AUX", false) ||
Packit Service 20376f
			!verify_dospath(component, len, "NUL", false) ||
Packit Service 20376f
			!verify_dospath(component, len, "COM", true)  ||
Packit Service 20376f
			!verify_dospath(component, len, "LPT", true))
Packit Service 20376f
			return false;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (flags & GIT_PATH_REJECT_DOT_GIT_HFS) {
Packit Service 20376f
		if (!verify_dotgit_hfs(component, len))
Packit Service 20376f
			return false;
Packit Service 20376f
		if (S_ISLNK(mode) && git_path_is_hfs_dotgit_modules(component, len))
Packit Service 20376f
			return false;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (flags & GIT_PATH_REJECT_DOT_GIT_NTFS) {
Packit Service 20376f
		if (!verify_dotgit_ntfs(repo, component, len))
Packit Service 20376f
			return false;
Packit Service 20376f
		if (S_ISLNK(mode) && git_path_is_ntfs_dotgit_modules(component, len))
Packit Service 20376f
			return false;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* don't bother rerunning the `.git` test if we ran the HFS or NTFS
Packit Service 20376f
	 * specific tests, they would have already rejected `.git`.
Packit Service 20376f
	 */
Packit Service 20376f
	if ((flags & GIT_PATH_REJECT_DOT_GIT_HFS) == 0 &&
Packit Service 20376f
	    (flags & GIT_PATH_REJECT_DOT_GIT_NTFS) == 0 &&
Packit Service 20376f
	    (flags & GIT_PATH_REJECT_DOT_GIT_LITERAL)) {
Packit Service 20376f
		if (len >= 4 &&
Packit Service 20376f
		    component[0] == '.' &&
Packit Service 20376f
		    (component[1] == 'g' || component[1] == 'G') &&
Packit Service 20376f
		    (component[2] == 'i' || component[2] == 'I') &&
Packit Service 20376f
		    (component[3] == 't' || component[3] == 'T')) {
Packit Service 20376f
			if (len == 4)
Packit Service 20376f
				return false;
Packit Service 20376f
Packit Service 20376f
			if (S_ISLNK(mode) && common_prefix_icase(component, len, ".gitmodules") == len)
Packit Service 20376f
				return false;
Packit Service 20376f
		}
Packit Service 20376f
	    }
Packit Service 20376f
Packit Service 20376f
	return true;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(unsigned int) dotgit_flags(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	unsigned int flags)
Packit Service 20376f
{
Packit Service 20376f
	int protectHFS = 0, protectNTFS = 0;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	flags |= GIT_PATH_REJECT_DOT_GIT_LITERAL;
Packit Service 20376f
Packit Service 20376f
#ifdef __APPLE__
Packit Service 20376f
	protectHFS = 1;
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	protectNTFS = 1;
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	if (repo && !protectHFS)
Packit Service 20376f
		error = git_repository__cvar(&protectHFS, repo, GIT_CVAR_PROTECTHFS);
Packit Service 20376f
	if (!error && protectHFS)
Packit Service 20376f
		flags |= GIT_PATH_REJECT_DOT_GIT_HFS;
Packit Service 20376f
Packit Service 20376f
	if (repo && !protectNTFS)
Packit Service 20376f
		error = git_repository__cvar(&protectNTFS, repo, GIT_CVAR_PROTECTNTFS);
Packit Service 20376f
	if (!error && protectNTFS)
Packit Service 20376f
		flags |= GIT_PATH_REJECT_DOT_GIT_NTFS;
Packit Service 20376f
Packit Service 20376f
	return flags;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_path_isvalid(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	uint16_t mode,
Packit Service 20376f
	unsigned int flags)
Packit Service 20376f
{
Packit Service 20376f
	const char *start, *c;
Packit Service 20376f
Packit Service 20376f
	/* Upgrade the ".git" checks based on platform */
Packit Service 20376f
	if ((flags & GIT_PATH_REJECT_DOT_GIT))
Packit Service 20376f
		flags = dotgit_flags(repo, flags);
Packit Service 20376f
Packit Service 20376f
	for (start = c = path; *c; c++) {
Packit Service 20376f
		if (!verify_char(*c, flags))
Packit Service 20376f
			return false;
Packit Service 20376f
Packit Service 20376f
		if (*c == '/') {
Packit Service 20376f
			if (!verify_component(repo, start, (c - start), mode, flags))
Packit Service 20376f
				return false;
Packit Service 20376f
Packit Service 20376f
			start = c+1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return verify_component(repo, start, (c - start), mode, flags);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_normalize_slashes(git_buf *out, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	char *p;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_puts(out, path)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	for (p = out->ptr; *p; p++) {
Packit Service 20376f
		if (*p == '\\')
Packit Service 20376f
			*p = '/';
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int verify_dotgit_generic(const char *name, size_t len, const char *dotgit_name, size_t dotgit_len, const char *shortname_pfix)
Packit Service 20376f
{
Packit Service 20376f
	if (!verify_dotgit_ntfs_generic(name, len, dotgit_name, dotgit_len, shortname_pfix))
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	return verify_dotgit_hfs_generic(name, len, dotgit_name, dotgit_len);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_is_ntfs_dotgit_modules(const char *name, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	return !verify_dotgit_ntfs_generic(name, len, "gitmodules", CONST_STRLEN("gitmodules"), "gi7eba");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_is_hfs_dotgit_modules(const char *name, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	return !verify_dotgit_hfs_generic(name, len, "gitmodules", CONST_STRLEN("gitmodules"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_is_dotgit_modules(const char *name, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	if (git_path_is_hfs_dotgit_modules(name, len))
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	return git_path_is_ntfs_dotgit_modules(name, len);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_is_ntfs_dotgit_ignore(const char *name, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	return !verify_dotgit_ntfs_generic(name, len, "gitignore", CONST_STRLEN("gitignore"), "gi250a");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_is_hfs_dotgit_ignore(const char *name, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	return !verify_dotgit_hfs_generic(name, len, "gitignore", CONST_STRLEN("gitignore"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_is_dotgit_ignore(const char *name, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	if (git_path_is_hfs_dotgit_ignore(name, len))
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	return git_path_is_ntfs_dotgit_ignore(name, len);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_is_hfs_dotgit_attributes(const char *name, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	return !verify_dotgit_hfs_generic(name, len, "gitattributes", CONST_STRLEN("gitattributes"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_is_ntfs_dotgit_attributes(const char *name, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	return !verify_dotgit_ntfs_generic(name, len, "gitattributes", CONST_STRLEN("gitattributes"), "gi7d29");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_path_is_dotgit_attributes(const char *name, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	if (git_path_is_hfs_dotgit_attributes(name, len))
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	return git_path_is_ntfs_dotgit_attributes(name, len);
Packit Service 20376f
}