Blame src/win32/w32_util.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
Packit Service 20376f
#include "w32_util.h"
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Creates a FindFirstFile(Ex) filter string from a UTF-8 path.
Packit Service 20376f
 * The filter string enumerates all items in the directory.
Packit Service 20376f
 *
Packit Service 20376f
 * @param dest The buffer to receive the filter string.
Packit Service 20376f
 * @param src The UTF-8 path of the directory to enumerate.
Packit Service 20376f
 * @return True if the filter string was created successfully; false otherwise
Packit Service 20376f
 */
Packit Service 20376f
bool git_win32__findfirstfile_filter(git_win32_path dest, const char *src)
Packit Service 20376f
{
Packit Service 20376f
	static const wchar_t suffix[] = L"\\*";
Packit Service 20376f
	int len = git_win32_path_from_utf8(dest, src);
Packit Service 20376f
Packit Service 20376f
	/* Ensure the path was converted */
Packit Service 20376f
	if (len < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	/* Ensure that the path does not end with a trailing slash,
Packit Service 20376f
	 * because we're about to add one. Don't rely our trim_end
Packit Service 20376f
	 * helper, because we want to remove the backslash even for
Packit Service 20376f
	 * drive letter paths, in this case. */
Packit Service 20376f
	if (len > 0 &&
Packit Service 20376f
		(dest[len - 1] == L'/' || dest[len - 1] == L'\\')) {
Packit Service 20376f
		dest[len - 1] = L'\0';
Packit Service 20376f
		len--;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Ensure we have enough room to add the suffix */
Packit Service 20376f
	if ((size_t)len >= GIT_WIN_PATH_UTF16 - CONST_STRLEN(suffix))
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	wcscat(dest, suffix);
Packit Service 20376f
	return true;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Ensures the given path (file or folder) has the +H (hidden) attribute set.
Packit Service 20376f
 *
Packit Service 20376f
 * @param path The path which should receive the +H bit.
Packit Service 20376f
 * @return 0 on success; -1 on failure
Packit Service 20376f
 */
Packit Service 20376f
int git_win32__set_hidden(const char *path, bool hidden)
Packit Service 20376f
{
Packit Service 20376f
	git_win32_path buf;
Packit Service 20376f
	DWORD attrs, newattrs;
Packit Service 20376f
Packit Service 20376f
	if (git_win32_path_from_utf8(buf, path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	attrs = GetFileAttributesW(buf);
Packit Service 20376f
Packit Service 20376f
	/* Ensure the path exists */
Packit Service 20376f
	if (attrs == INVALID_FILE_ATTRIBUTES)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (hidden)
Packit Service 20376f
		newattrs = attrs | FILE_ATTRIBUTE_HIDDEN;
Packit Service 20376f
	else
Packit Service 20376f
		newattrs = attrs & ~FILE_ATTRIBUTE_HIDDEN;
Packit Service 20376f
Packit Service 20376f
	if (attrs != newattrs && !SetFileAttributesW(buf, newattrs)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to %s hidden bit for '%s'",
Packit Service 20376f
			hidden ? "set" : "unset", path);
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
int git_win32__hidden(bool *out, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_win32_path buf;
Packit Service 20376f
	DWORD attrs;
Packit Service 20376f
Packit Service 20376f
	if (git_win32_path_from_utf8(buf, path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	attrs = GetFileAttributesW(buf);
Packit Service 20376f
Packit Service 20376f
	/* Ensure the path exists */
Packit Service 20376f
	if (attrs == INVALID_FILE_ATTRIBUTES)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	*out = (attrs & FILE_ATTRIBUTE_HIDDEN) ? true : false;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Removes any trailing backslashes from a path, except in the case of a drive
Packit Service 20376f
 * letter path (C:\, D:\, etc.). This function cannot fail.
Packit Service 20376f
 *
Packit Service 20376f
 * @param path The path which should be trimmed.
Packit Service 20376f
 * @return The length of the modified string (<= the input length)
Packit Service 20376f
 */
Packit Service 20376f
size_t git_win32__path_trim_end(wchar_t *str, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	while (1) {
Packit Service 20376f
		if (!len || str[len - 1] != L'\\')
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		/* Don't trim backslashes from drive letter paths, which
Packit Service 20376f
		 * are 3 characters long and of the form C:\, D:\, etc. */
Packit Service 20376f
		if (len == 3 && git_win32__isalpha(str[0]) && str[1] == ':')
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		len--;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	str[len] = L'\0';
Packit Service 20376f
Packit Service 20376f
	return len;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Removes any of the following namespace prefixes from a path,
Packit Service 20376f
 * if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail.
Packit Service 20376f
 *
Packit Service 20376f
 * @param path The path which should be converted.
Packit Service 20376f
 * @return The length of the modified string (<= the input length)
Packit Service 20376f
 */
Packit Service 20376f
size_t git_win32__canonicalize_path(wchar_t *str, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	static const wchar_t dosdevices_prefix[] = L"\\\?\?\\";
Packit Service 20376f
	static const wchar_t nt_prefix[] = L"\\\\?\\";
Packit Service 20376f
	static const wchar_t unc_prefix[] = L"UNC\\";
Packit Service 20376f
	size_t to_advance = 0;
Packit Service 20376f
Packit Service 20376f
	/* "\??\" -- DOS Devices prefix */
Packit Service 20376f
	if (len >= CONST_STRLEN(dosdevices_prefix) &&
Packit Service 20376f
		!wcsncmp(str, dosdevices_prefix, CONST_STRLEN(dosdevices_prefix))) {
Packit Service 20376f
		to_advance += CONST_STRLEN(dosdevices_prefix);
Packit Service 20376f
		len -= CONST_STRLEN(dosdevices_prefix);
Packit Service 20376f
	}
Packit Service 20376f
	/* "\\?\" -- NT namespace prefix */
Packit Service 20376f
	else if (len >= CONST_STRLEN(nt_prefix) &&
Packit Service 20376f
		!wcsncmp(str, nt_prefix, CONST_STRLEN(nt_prefix))) {
Packit Service 20376f
		to_advance += CONST_STRLEN(nt_prefix);
Packit Service 20376f
		len -= CONST_STRLEN(nt_prefix);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* "\??\UNC\", "\\?\UNC\" -- UNC prefix */
Packit Service 20376f
	if (to_advance && len >= CONST_STRLEN(unc_prefix) &&
Packit Service 20376f
		!wcsncmp(str + to_advance, unc_prefix, CONST_STRLEN(unc_prefix))) {
Packit Service 20376f
		to_advance += CONST_STRLEN(unc_prefix);
Packit Service 20376f
		len -= CONST_STRLEN(unc_prefix);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (to_advance) {
Packit Service 20376f
		memmove(str, str + to_advance, len * sizeof(wchar_t));
Packit Service 20376f
		str[len] = L'\0';
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return git_win32__path_trim_end(str, len);
Packit Service 20376f
}