path_w32.c 15.2 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "path_w32.h"
9

10
#include "fs_path.h"
11
#include "utf-conv.h"
12 13 14
#include "posix.h"
#include "reparse.h"
#include "dir.h"
15 16 17 18 19 20 21 22 23 24 25 26 27

#define PATH__NT_NAMESPACE     L"\\\\?\\"
#define PATH__NT_NAMESPACE_LEN 4

#define PATH__ABSOLUTE_LEN     3

#define path__is_nt_namespace(p) \
	(((p)[0] == '\\' && (p)[1] == '\\' && (p)[2] == '?' && (p)[3] == '\\') || \
	 ((p)[0] == '/' && (p)[1] == '/' && (p)[2] == '?' && (p)[3] == '/'))

#define path__is_unc(p) \
	(((p)[0] == '\\' && (p)[1] == '\\') || ((p)[0] == '/' && (p)[1] == '/'))

28 29 30
#define path__startswith_slash(p) \
	((p)[0] == '\\' || (p)[0] == '/')

31 32 33 34 35 36 37 38 39 40 41 42 43
GIT_INLINE(int) path__cwd(wchar_t *path, int size)
{
	int len;

	if ((len = GetCurrentDirectoryW(size, path)) == 0) {
		errno = GetLastError() == ERROR_ACCESS_DENIED ? EACCES : ENOENT;
		return -1;
	} else if (len > size) {
		errno = ENAMETOOLONG;
		return -1;
	}

	/* The Win32 APIs may return "\\?\" once you've used it first.
Dimitris Apostolou committed
44
	 * But it may not.  What a gloriously predictable API!
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	 */
	if (wcsncmp(path, PATH__NT_NAMESPACE, PATH__NT_NAMESPACE_LEN))
		return len;

	len -= PATH__NT_NAMESPACE_LEN;

	memmove(path, path + PATH__NT_NAMESPACE_LEN, sizeof(wchar_t) * len);
	return len;
}

static wchar_t *path__skip_server(wchar_t *path)
{
	wchar_t *c;

	for (c = path; *c; c++) {
60
		if (git_fs_path_is_dirsep(*c))
61 62 63 64 65 66 67 68 69 70 71 72 73
			return c + 1;
	}

	return c;
}

static wchar_t *path__skip_prefix(wchar_t *path)
{
	if (path__is_nt_namespace(path)) {
		path += PATH__NT_NAMESPACE_LEN;

		if (wcsncmp(path, L"UNC\\", 4) == 0)
			path = path__skip_server(path + 4);
74
		else if (git_fs_path_is_absolute(path))
75
			path += PATH__ABSOLUTE_LEN;
76
	} else if (git_fs_path_is_absolute(path)) {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
		path += PATH__ABSOLUTE_LEN;
	} else if (path__is_unc(path)) {
		path = path__skip_server(path + 2);
	}

	return path;
}

int git_win32_path_canonicalize(git_win32_path path)
{
	wchar_t *base, *from, *to, *next;
	size_t len;

	base = to = path__skip_prefix(path);

	/* Unposixify if the prefix */
	for (from = path; from < to; from++) {
		if (*from == L'/')
			*from = L'\\';
	}

	while (*from) {
		for (next = from; *next; ++next) {
			if (*next == L'/') {
				*next = L'\\';
				break;
			}

			if (*next == L'\\')
				break;
		}

		len = next - from;

		if (len == 1 && from[0] == L'.')
			/* do nothing with singleton dot */;

		else if (len == 2 && from[0] == L'.' && from[1] == L'.') {
			if (to == base) {
				/* no more path segments to strip, eat the "../" */
				if (*next == L'\\')
					len++;

				base = to;
			} else {
				/* back up a path segment */
				while (to > base && to[-1] == L'\\') to--;
				while (to > base && to[-1] != L'\\') to--;
			}
		} else {
			if (*next == L'\\' && *from != L'\\')
				len++;

			if (to != from)
				memmove(to, from, sizeof(wchar_t) * len);

			to += len;
		}

		from += len;

		while (*from == L'\\') from++;
	}

	/* Strip trailing backslashes */
	while (to > base && to[-1] == L'\\') to--;

	*to = L'\0';

146 147 148 149 150 151
	if ((to - path) > INT_MAX) {
		SetLastError(ERROR_FILENAME_EXCED_RANGE);
		return -1;
	}

	return (int)(to - path);
152 153
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
static int git_win32_path_join(
	git_win32_path dest,
	const wchar_t *one,
	size_t one_len,
	const wchar_t *two,
	size_t two_len)
{
	size_t backslash = 0;

	if (one_len && two_len && one[one_len - 1] != L'\\')
		backslash = 1;

	if (one_len + two_len + backslash > MAX_PATH) {
		git_error_set(GIT_ERROR_INVALID, "path too long");
		return -1;
	}

	memmove(dest, one, one_len * sizeof(wchar_t));

	if (backslash)
		dest[one_len] = L'\\';

	memcpy(dest + one_len + backslash, two, two_len * sizeof(wchar_t));
	dest[one_len + backslash + two_len] = L'\0';

	return 0;
}

struct win32_path_iter {
	wchar_t *env;
	const wchar_t *current_dir;
};

static int win32_path_iter_init(struct win32_path_iter *iter)
{
	DWORD len = GetEnvironmentVariableW(L"PATH", NULL, 0);

	if (!len && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
		iter->env = NULL;
		iter->current_dir = NULL;
		return 0;
	} else if (!len) {
		git_error_set(GIT_ERROR_OS, "could not load PATH");
		return -1;
	}

	iter->env = git__malloc(len * sizeof(wchar_t));
	GIT_ERROR_CHECK_ALLOC(iter->env);

	len = GetEnvironmentVariableW(L"PATH", iter->env, len);

	if (len == 0) {
		git_error_set(GIT_ERROR_OS, "could not load PATH");
		return -1;
	}

	iter->current_dir = iter->env;
	return 0;
}

static int win32_path_iter_next(
	const wchar_t **out,
	size_t *out_len,
	struct win32_path_iter *iter)
{
	const wchar_t *start;
	wchar_t term;
	size_t len = 0;

	if (!iter->current_dir || !*iter->current_dir)
		return GIT_ITEROVER;

	term = (*iter->current_dir == L'"') ? *iter->current_dir++ : L';';
	start = iter->current_dir;

	while (*iter->current_dir && *iter->current_dir != term) {
		iter->current_dir++;
		len++;
	}

	*out = start;
	*out_len = len;

	if (term == L'"' && *iter->current_dir)
		iter->current_dir++;

	while (*iter->current_dir == L';')
		iter->current_dir++;

	return 0;
}

static void win32_path_iter_dispose(struct win32_path_iter *iter)
{
	if (!iter)
		return;

	git__free(iter->env);
	iter->env = NULL;
	iter->current_dir = NULL;
}

int git_win32_path_find_executable(git_win32_path fullpath, wchar_t *exe)
{
	struct win32_path_iter path_iter;
	const wchar_t *dir;
	size_t dir_len, exe_len = wcslen(exe);
	bool found = false;

	if (win32_path_iter_init(&path_iter) < 0)
		return -1;

	while (win32_path_iter_next(&dir, &dir_len, &path_iter) != GIT_ITEROVER) {
		if (git_win32_path_join(fullpath, dir, dir_len, exe, exe_len) < 0)
			continue;

		if (_waccess(fullpath, 0) == 0) {
			found = true;
			break;
		}
	}

	win32_path_iter_dispose(&path_iter);

	if (found)
		return 0;

	fullpath[0] = L'\0';
	return GIT_ENOTFOUND;
}

285
static int win32_path_cwd(wchar_t *out, size_t len)
286 287 288
{
	int cwd_len;

289 290 291 292 293 294
	if (len > INT_MAX) {
		errno = ENAMETOOLONG;
		return -1;
	}

	if ((cwd_len = path__cwd(out, (int)len)) < 0)
295 296 297 298 299 300 301 302 303
		return -1;

	/* UNC paths */
	if (wcsncmp(L"\\\\", out, 2) == 0) {
		/* Our buffer must be at least 5 characters larger than the
		 * current working directory:  we swallow one of the leading
		 * '\'s, but we we add a 'UNC' specifier to the path, plus
		 * a trailing directory separator, plus a NUL.
		 */
304
		if (cwd_len > GIT_WIN_PATH_MAX - 4) {
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
			errno = ENAMETOOLONG;
			return -1;
		}

		memmove(out+2, out, sizeof(wchar_t) * cwd_len);
		out[0] = L'U';
		out[1] = L'N';
		out[2] = L'C';

		cwd_len += 2;
	}

	/* Our buffer must be at least 2 characters larger than the current
	 * working directory.  (One character for the directory separator,
	 * one for the null.
	 */
321
	else if (cwd_len > GIT_WIN_PATH_MAX - 2) {
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
		errno = ENAMETOOLONG;
		return -1;
	}

	return cwd_len;
}

int git_win32_path_from_utf8(git_win32_path out, const char *src)
{
	wchar_t *dest = out;

	/* All win32 paths are in NT-prefixed format, beginning with "\\?\". */
	memcpy(dest, PATH__NT_NAMESPACE, sizeof(wchar_t) * PATH__NT_NAMESPACE_LEN);
	dest += PATH__NT_NAMESPACE_LEN;

	/* See if this is an absolute path (beginning with a drive letter) */
338
	if (git_fs_path_is_absolute(src)) {
339
		if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src) < 0)
340
			goto on_error;
341 342 343 344
	}
	/* File-prefixed NT-style paths beginning with \\?\ */
	else if (path__is_nt_namespace(src)) {
		/* Skip the NT prefix, the destination already contains it */
345
		if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src + PATH__NT_NAMESPACE_LEN) < 0)
346
			goto on_error;
347 348 349 350 351 352 353
	}
	/* UNC paths */
	else if (path__is_unc(src)) {
		memcpy(dest, L"UNC\\", sizeof(wchar_t) * 4);
		dest += 4;

		/* Skip the leading "\\" */
354
		if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX - 2, src + 2) < 0)
355
			goto on_error;
356 357
	}
	/* Absolute paths omitting the drive letter */
358
	else if (path__startswith_slash(src)) {
359
		if (path__cwd(dest, GIT_WIN_PATH_MAX) < 0)
360
			goto on_error;
361

362
		if (!git_fs_path_is_absolute(dest)) {
363
			errno = ENOENT;
364
			goto on_error;
365 366
		}

367
		/* Skip the drive letter specification ("C:") */
368
		if (git__utf8_to_16(dest + 2, GIT_WIN_PATH_MAX - 2, src) < 0)
369
			goto on_error;
370 371 372 373 374
	}
	/* Relative paths */
	else {
		int cwd_len;

375
		if ((cwd_len = win32_path_cwd(dest, GIT_WIN_PATH_MAX)) < 0)
376
			goto on_error;
377 378 379

		dest[cwd_len++] = L'\\';

380
		if (git__utf8_to_16(dest + cwd_len, GIT_WIN_PATH_MAX - cwd_len, src) < 0)
381
			goto on_error;
382 383 384
	}

	return git_win32_path_canonicalize(out);
385 386 387 388 389 390 391

on_error:
	/* set windows error code so we can use its error message */
	if (errno == ENAMETOOLONG)
		SetLastError(ERROR_FILENAME_EXCED_RANGE);

	return -1;
392 393
}

394 395 396 397 398 399
int git_win32_path_relative_from_utf8(git_win32_path out, const char *src)
{
	wchar_t *dest = out, *p;
	int len;

	/* Handle absolute paths */
400
	if (git_fs_path_is_absolute(src) ||
401 402 403 404 405 406
	    path__is_nt_namespace(src) ||
	    path__is_unc(src) ||
	    path__startswith_slash(src)) {
		return git_win32_path_from_utf8(out, src);
	}

407
	if ((len = git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src)) < 0)
408 409 410 411 412 413 414 415 416 417
		return -1;

	for (p = dest; p < (dest + len); p++) {
		if (*p == L'/')
			*p = L'\\';
	}

	return len;
}

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
int git_win32_path_to_utf8(git_win32_utf8_path dest, const wchar_t *src)
{
	char *out = dest;
	int len;

	/* Strip NT namespacing "\\?\" */
	if (path__is_nt_namespace(src)) {
		src += 4;

		/* "\\?\UNC\server\share" -> "\\server\share" */
		if (wcsncmp(src, L"UNC\\", 4) == 0) {
			src += 4;

			memcpy(dest, "\\\\", 2);
			out = dest + 2;
		}
	}

	if ((len = git__utf16_to_8(out, GIT_WIN_PATH_UTF8, src)) < 0)
		return len;

439
	git_fs_path_mkposix(dest);
440 441 442

	return len;
}
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

char *git_win32_path_8dot3_name(const char *path)
{
	git_win32_path longpath, shortpath;
	wchar_t *start;
	char *shortname;
	int len, namelen = 1;

	if (git_win32_path_from_utf8(longpath, path) < 0)
		return NULL;

	len = GetShortPathNameW(longpath, shortpath, GIT_WIN_PATH_UTF16);

	while (len && shortpath[len-1] == L'\\')
		shortpath[--len] = L'\0';

	if (len == 0 || len >= GIT_WIN_PATH_UTF16)
		return NULL;

	for (start = shortpath + (len - 1);
		start > shortpath && *(start-1) != '/' && *(start-1) != '\\';
		start--)
		namelen++;

	/* We may not have actually been given a short name.  But if we have,
	 * it will be in the ASCII byte range, so we don't need to worry about
	 * multi-byte sequences and can allocate naively.
	 */
	if (namelen > 12 || (shortname = git__malloc(namelen + 1)) == NULL)
		return NULL;

	if ((len = git__utf16_to_8(shortname, namelen + 1, start)) < 0)
		return NULL;

	return shortname;
}
479 480 481 482 483 484 485

static bool path_is_volume(wchar_t *target, size_t target_len)
{
	return (target_len && wcsncmp(target, L"\\??\\Volume{", 11) == 0);
}

/* On success, returns the length, in characters, of the path stored in dest.
486
 * On failure, returns a negative value. */
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
{
	BYTE buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
	GIT_REPARSE_DATA_BUFFER *reparse_buf = (GIT_REPARSE_DATA_BUFFER *)buf;
	HANDLE handle = NULL;
	DWORD ioctl_ret;
	wchar_t *target;
	size_t target_len;

	int error = -1;

	handle = CreateFileW(path, GENERIC_READ,
		FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
		FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);

	if (handle == INVALID_HANDLE_VALUE) {
		errno = ENOENT;
		return -1;
	}

	if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0,
		reparse_buf, sizeof(buf), &ioctl_ret, NULL)) {
		errno = EINVAL;
		goto on_error;
	}

	switch (reparse_buf->ReparseTag) {
	case IO_REPARSE_TAG_SYMLINK:
515 516 517
		target = reparse_buf->ReparseBuffer.SymbolicLink.PathBuffer +
			(reparse_buf->ReparseBuffer.SymbolicLink.SubstituteNameOffset / sizeof(WCHAR));
		target_len = reparse_buf->ReparseBuffer.SymbolicLink.SubstituteNameLength / sizeof(WCHAR);
518 519
	break;
	case IO_REPARSE_TAG_MOUNT_POINT:
520 521 522
		target = reparse_buf->ReparseBuffer.MountPoint.PathBuffer +
			(reparse_buf->ReparseBuffer.MountPoint.SubstituteNameOffset / sizeof(WCHAR));
		target_len = reparse_buf->ReparseBuffer.MountPoint.SubstituteNameLength / sizeof(WCHAR);
523 524 525 526 527 528 529 530
	break;
	default:
		errno = EINVAL;
		goto on_error;
	}

	if (path_is_volume(target, target_len)) {
		/* This path is a reparse point that represents another volume mounted
531 532
		 * at this location, it is not a symbolic link our input was canonical.
		 */
533 534 535
		errno = EINVAL;
		error = -1;
	} else if (target_len) {
536 537
		/* The path may need to have a namespace prefix removed. */
		target_len = git_win32_path_remove_namespace(target, target_len);
538 539

		/* Need one additional character in the target buffer
540
		 * for the terminating NULL. */
541 542 543 544 545 546 547 548 549 550
		if (GIT_WIN_PATH_UTF16 > target_len) {
			wcscpy(dest, target);
			error = (int)target_len;
		}
	}

on_error:
	CloseHandle(handle);
	return error;
}
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

/**
 * Removes any trailing backslashes from a path, except in the case of a drive
 * letter path (C:\, D:\, etc.). This function cannot fail.
 *
 * @param path The path which should be trimmed.
 * @return The length of the modified string (<= the input length)
 */
size_t git_win32_path_trim_end(wchar_t *str, size_t len)
{
	while (1) {
		if (!len || str[len - 1] != L'\\')
			break;

		/*
		 * Don't trim backslashes from drive letter paths, which
		 * are 3 characters long and of the form C:\, D:\, etc.
		 */
		if (len == 3 && git_win32__isalpha(str[0]) && str[1] == ':')
			break;

		len--;
	}

	str[len] = L'\0';

	return len;
}

/**
 * Removes any of the following namespace prefixes from a path,
 * if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail.
 *
 * @param path The path which should be converted.
 * @return The length of the modified string (<= the input length)
 */
size_t git_win32_path_remove_namespace(wchar_t *str, size_t len)
{
589 590 591 592
	static const wchar_t dosdevices_namespace[] = L"\\\?\?\\";
	static const wchar_t nt_namespace[] = L"\\\\?\\";
	static const wchar_t unc_namespace_remainder[] = L"UNC\\";
	static const wchar_t unc_prefix[] = L"\\\\";
593

594 595
	const wchar_t *prefix = NULL, *remainder = NULL;
	size_t prefix_len = 0, remainder_len = 0;
596 597

	/* "\??\" -- DOS Devices prefix */
598 599 600 601
	if (len >= CONST_STRLEN(dosdevices_namespace) &&
		!wcsncmp(str, dosdevices_namespace, CONST_STRLEN(dosdevices_namespace))) {
		remainder = str + CONST_STRLEN(dosdevices_namespace);
		remainder_len = len - CONST_STRLEN(dosdevices_namespace);
602 603
	}
	/* "\\?\" -- NT namespace prefix */
604 605 606 607
	else if (len >= CONST_STRLEN(nt_namespace) &&
		!wcsncmp(str, nt_namespace, CONST_STRLEN(nt_namespace))) {
		remainder = str + CONST_STRLEN(nt_namespace);
		remainder_len = len - CONST_STRLEN(nt_namespace);
608 609 610
	}

	/* "\??\UNC\", "\\?\UNC\" -- UNC prefix */
611 612
	if (remainder_len >= CONST_STRLEN(unc_namespace_remainder) &&
		!wcsncmp(remainder, unc_namespace_remainder, CONST_STRLEN(unc_namespace_remainder))) {
613 614 615

		/*
		 * The proper Win32 path for a UNC share has "\\" at beginning of it
616 617
		 * and looks like "\\server\share\<folderStructure>".  So remove the
		 * UNC namespace and add a prefix of "\\" in its place.
618
		 */
619 620
		remainder += CONST_STRLEN(unc_namespace_remainder);
		remainder_len -= CONST_STRLEN(unc_namespace_remainder);
621

622 623
		prefix = unc_prefix;
		prefix_len = CONST_STRLEN(unc_prefix);
624 625
	}

Edward Thomson committed
626 627 628 629 630 631
	/*
	 * Sanity check that the new string isn't longer than the old one.
	 * (This could only happen due to programmer error introducing a
	 * prefix longer than the namespace it replaces.)
	 */
	if (remainder && len >= remainder_len + prefix_len) {
632 633 634 635 636 637
		if (prefix)
			memmove(str, prefix, prefix_len * sizeof(wchar_t));

		memmove(str + prefix_len, remainder, remainder_len * sizeof(wchar_t));

		len = remainder_len + prefix_len;
638 639 640 641 642
		str[len] = L'\0';
	}

	return git_win32_path_trim_end(str, len);
}