Blame src/win32/dir.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
#define GIT__WIN32_NO_WRAP_DIR
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
Packit Service 20376f
git__DIR *git__opendir(const char *dir)
Packit Service 20376f
{
Packit Service 20376f
	git_win32_path filter_w;
Packit Service 20376f
	git__DIR *new = NULL;
Packit Service 20376f
	size_t dirlen, alloclen;
Packit Service 20376f
Packit Service 20376f
	if (!dir || !git_win32__findfirstfile_filter(filter_w, dir))
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	dirlen = strlen(dir);
Packit Service 20376f
Packit Service 20376f
	if (GIT_ADD_SIZET_OVERFLOW(&alloclen, sizeof(*new), dirlen) ||
Packit Service 20376f
		GIT_ADD_SIZET_OVERFLOW(&alloclen, alloclen, 1) ||
Packit Service 20376f
		!(new = git__calloc(1, alloclen)))
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	memcpy(new->dir, dir, dirlen);
Packit Service 20376f
Packit Service 20376f
	new->h = FindFirstFileW(filter_w, &new->f);
Packit Service 20376f
Packit Service 20376f
	if (new->h == INVALID_HANDLE_VALUE) {
Packit Service 20376f
		giterr_set(GITERR_OS, "could not open directory '%s'", dir);
Packit Service 20376f
		git__free(new);
Packit Service 20376f
		return NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	new->first = 1;
Packit Service 20376f
	return new;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git__readdir_ext(
Packit Service 20376f
	git__DIR *d,
Packit Service 20376f
	struct git__dirent *entry,
Packit Service 20376f
	struct git__dirent **result,
Packit Service 20376f
	int *is_dir)
Packit Service 20376f
{
Packit Service 20376f
	if (!d || !entry || !result || d->h == INVALID_HANDLE_VALUE)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	*result = NULL;
Packit Service 20376f
Packit Service 20376f
	if (d->first)
Packit Service 20376f
		d->first = 0;
Packit Service 20376f
	else if (!FindNextFileW(d->h, &d->f)) {
Packit Service 20376f
		if (GetLastError() == ERROR_NO_MORE_FILES)
Packit Service 20376f
			return 0;
Packit Service 20376f
		giterr_set(GITERR_OS, "could not read from directory '%s'", d->dir);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Convert the path to UTF-8 */
Packit Service 20376f
	if (git_win32_path_to_utf8(entry->d_name, d->f.cFileName) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	entry->d_ino = 0;
Packit Service 20376f
Packit Service 20376f
	*result = entry;
Packit Service 20376f
Packit Service 20376f
	if (is_dir != NULL)
Packit Service 20376f
		*is_dir = ((d->f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
struct git__dirent *git__readdir(git__DIR *d)
Packit Service 20376f
{
Packit Service 20376f
	struct git__dirent *result;
Packit Service 20376f
	if (git__readdir_ext(d, &d->entry, &result, NULL) < 0)
Packit Service 20376f
		return NULL;
Packit Service 20376f
	return result;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git__rewinddir(git__DIR *d)
Packit Service 20376f
{
Packit Service 20376f
	git_win32_path filter_w;
Packit Service 20376f
Packit Service 20376f
	if (!d)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	if (d->h != INVALID_HANDLE_VALUE) {
Packit Service 20376f
		FindClose(d->h);
Packit Service 20376f
		d->h = INVALID_HANDLE_VALUE;
Packit Service 20376f
		d->first = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!git_win32__findfirstfile_filter(filter_w, d->dir))
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	d->h = FindFirstFileW(filter_w, &d->f);
Packit Service 20376f
Packit Service 20376f
	if (d->h == INVALID_HANDLE_VALUE)
Packit Service 20376f
		giterr_set(GITERR_OS, "could not open directory '%s'", d->dir);
Packit Service 20376f
	else
Packit Service 20376f
		d->first = 1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git__closedir(git__DIR *d)
Packit Service 20376f
{
Packit Service 20376f
	if (!d)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (d->h != INVALID_HANDLE_VALUE) {
Packit Service 20376f
		FindClose(d->h);
Packit Service 20376f
		d->h = INVALID_HANDLE_VALUE;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git__free(d);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f