Blame src/win32/w32_util.h

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
#ifndef INCLUDE_w32_util_h__
Packit ae9e2a
#define INCLUDE_w32_util_h__
Packit ae9e2a
Packit ae9e2a
#include "utf-conv.h"
Packit ae9e2a
#include "posix.h"
Packit ae9e2a
#include "path_w32.h"
Packit ae9e2a
Packit ae9e2a
/*
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include "path.h"
Packit ae9e2a
#include "path_w32.h"
Packit ae9e2a
#include "utf-conv.h"
Packit ae9e2a
#include "posix.h"
Packit ae9e2a
#include "reparse.h"
Packit ae9e2a
#include "dir.h"
Packit ae9e2a
*/
Packit ae9e2a
Packit ae9e2a
Packit ae9e2a
GIT_INLINE(bool) git_win32__isalpha(wchar_t c)
Packit ae9e2a
{
Packit ae9e2a
	return ((c >= L'A' && c <= L'Z') || (c >= L'a' && c <= L'z'));
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Creates a FindFirstFile(Ex) filter string from a UTF-8 path.
Packit ae9e2a
 * The filter string enumerates all items in the directory.
Packit ae9e2a
 *
Packit ae9e2a
 * @param dest The buffer to receive the filter string.
Packit ae9e2a
 * @param src The UTF-8 path of the directory to enumerate.
Packit ae9e2a
 * @return True if the filter string was created successfully; false otherwise
Packit ae9e2a
 */
Packit ae9e2a
bool git_win32__findfirstfile_filter(git_win32_path dest, const char *src);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Ensures the given path (file or folder) has the +H (hidden) attribute set
Packit ae9e2a
 * or unset.
Packit ae9e2a
 *
Packit ae9e2a
 * @param path The path that should receive the +H bit.
Packit ae9e2a
 * @param hidden true to set +H, false to unset it
Packit ae9e2a
 * @return 0 on success; -1 on failure
Packit ae9e2a
 */
Packit ae9e2a
extern int git_win32__set_hidden(const char *path, bool hidden);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Determines if the given file or folder has the hidden attribute set.
Packit ae9e2a
 * @param hidden pointer to store hidden value
Packit ae9e2a
 * @param path The path that should be queried for hiddenness.
Packit ae9e2a
 * @return 0 on success or an error code.
Packit ae9e2a
 */
Packit ae9e2a
extern int git_win32__hidden(bool *hidden, const char *path);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Removes any trailing backslashes from a path, except in the case of a drive
Packit ae9e2a
 * letter path (C:\, D:\, etc.). This function cannot fail.
Packit ae9e2a
 *
Packit ae9e2a
 * @param path The path which should be trimmed.
Packit ae9e2a
 * @return The length of the modified string (<= the input length)
Packit ae9e2a
 */
Packit ae9e2a
size_t git_win32__path_trim_end(wchar_t *str, size_t len);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Removes any of the following namespace prefixes from a path,
Packit ae9e2a
 * if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail.
Packit ae9e2a
 *
Packit ae9e2a
 * @param path The path which should be converted.
Packit ae9e2a
 * @return The length of the modified string (<= the input length)
Packit ae9e2a
 */
Packit ae9e2a
size_t git_win32__canonicalize_path(wchar_t *str, size_t len);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Converts a FILETIME structure to a struct timespec.
Packit ae9e2a
 *
Packit ae9e2a
 * @param FILETIME A pointer to a FILETIME
Packit ae9e2a
 * @param ts A pointer to the timespec structure to fill in
Packit ae9e2a
 */
Packit ae9e2a
GIT_INLINE(void) git_win32__filetime_to_timespec(
Packit ae9e2a
	const FILETIME *ft,
Packit ae9e2a
	struct timespec *ts)
Packit ae9e2a
{
Packit ae9e2a
	long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
Packit ae9e2a
	winTime -= 116444736000000000LL; /* Windows to Unix Epoch conversion */
Packit ae9e2a
	ts->tv_sec = (time_t)(winTime / 10000000);
Packit ae9e2a
#ifdef GIT_USE_NSEC
Packit ae9e2a
	ts->tv_nsec = (winTime % 10000000) * 100;
Packit ae9e2a
#else
Packit ae9e2a
	ts->tv_nsec = 0;
Packit ae9e2a
#endif
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
GIT_INLINE(void) git_win32__timeval_to_filetime(
Packit ae9e2a
	FILETIME *ft, const struct p_timeval tv)
Packit ae9e2a
{
Packit ae9e2a
	long long ticks = (tv.tv_sec * 10000000LL) +
Packit ae9e2a
		(tv.tv_usec * 10LL) + 116444736000000000LL;
Packit ae9e2a
Packit ae9e2a
	ft->dwHighDateTime = ((ticks >> 32) & 0xffffffffLL);
Packit ae9e2a
	ft->dwLowDateTime = (ticks & 0xffffffffLL);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
GIT_INLINE(void) git_win32__stat_init(
Packit ae9e2a
	struct stat *st,
Packit ae9e2a
	DWORD dwFileAttributes,
Packit ae9e2a
	DWORD nFileSizeHigh,
Packit ae9e2a
	DWORD nFileSizeLow,
Packit ae9e2a
	FILETIME ftCreationTime,
Packit ae9e2a
	FILETIME ftLastAccessTime,
Packit ae9e2a
	FILETIME ftLastWriteTime)
Packit ae9e2a
{
Packit ae9e2a
	mode_t mode = S_IREAD;
Packit ae9e2a
Packit ae9e2a
	memset(st, 0, sizeof(struct stat));
Packit ae9e2a
Packit ae9e2a
	if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
Packit ae9e2a
		mode |= S_IFDIR;
Packit ae9e2a
	else
Packit ae9e2a
		mode |= S_IFREG;
Packit ae9e2a
Packit ae9e2a
	if ((dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
Packit ae9e2a
		mode |= S_IWRITE;
Packit ae9e2a
Packit ae9e2a
	st->st_ino = 0;
Packit ae9e2a
	st->st_gid = 0;
Packit ae9e2a
	st->st_uid = 0;
Packit ae9e2a
	st->st_nlink = 1;
Packit ae9e2a
	st->st_mode = mode;
Packit ae9e2a
	st->st_size = ((git_off_t)nFileSizeHigh << 32) + nFileSizeLow;
Packit ae9e2a
	st->st_dev = _getdrive() - 1;
Packit ae9e2a
	st->st_rdev = st->st_dev;
Packit ae9e2a
	git_win32__filetime_to_timespec(&ftLastAccessTime, &(st->st_atim));
Packit ae9e2a
	git_win32__filetime_to_timespec(&ftLastWriteTime, &(st->st_mtim));
Packit ae9e2a
	git_win32__filetime_to_timespec(&ftCreationTime, &(st->st_ctim));
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
GIT_INLINE(void) git_win32__file_information_to_stat(
Packit ae9e2a
	struct stat *st,
Packit ae9e2a
	const BY_HANDLE_FILE_INFORMATION *fileinfo)
Packit ae9e2a
{
Packit ae9e2a
	git_win32__stat_init(st,
Packit ae9e2a
		fileinfo->dwFileAttributes,
Packit ae9e2a
		fileinfo->nFileSizeHigh,
Packit ae9e2a
		fileinfo->nFileSizeLow,
Packit ae9e2a
		fileinfo->ftCreationTime,
Packit ae9e2a
		fileinfo->ftLastAccessTime,
Packit ae9e2a
		fileinfo->ftLastWriteTime);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
GIT_INLINE(int) git_win32__file_attribute_to_stat(
Packit ae9e2a
	struct stat *st,
Packit ae9e2a
	const WIN32_FILE_ATTRIBUTE_DATA *attrdata,
Packit ae9e2a
	const wchar_t *path)
Packit ae9e2a
{
Packit ae9e2a
	git_win32__stat_init(st,
Packit ae9e2a
		attrdata->dwFileAttributes,
Packit ae9e2a
		attrdata->nFileSizeHigh,
Packit ae9e2a
		attrdata->nFileSizeLow,
Packit ae9e2a
		attrdata->ftCreationTime,
Packit ae9e2a
		attrdata->ftLastAccessTime,
Packit ae9e2a
		attrdata->ftLastWriteTime);
Packit ae9e2a
Packit ae9e2a
	if (attrdata->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT && path) {
Packit ae9e2a
		git_win32_path target;
Packit ae9e2a
Packit ae9e2a
		if (git_win32_path_readlink_w(target, path) >= 0) {
Packit ae9e2a
			st->st_mode = (st->st_mode & ~S_IFMT) | S_IFLNK;
Packit ae9e2a
Packit ae9e2a
			/* st_size gets the UTF-8 length of the target name, in bytes,
Packit ae9e2a
			 * not counting the NULL terminator */
Packit ae9e2a
			if ((st->st_size = git__utf16_to_8(NULL, 0, target)) < 0) {
Packit ae9e2a
				giterr_set(GITERR_OS, "could not convert reparse point name for '%ls'", path);
Packit ae9e2a
				return -1;
Packit ae9e2a
			}
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
#endif