dir.c 2.66 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7
#define GIT__WIN32_NO_WRAP_DIR
8
#include "dir.h"
9
#include "utf-conv.h"
10 11 12

static int init_filter(char *filter, size_t n, const char *dir)
{
13
	size_t len = strlen(dir);
14 15 16 17 18 19 20 21 22 23 24 25

	if (len+3 >= n)
		return 0;

	strcpy(filter, dir);
	if (len && dir[len-1] != '/')
		strcat(filter, "/");
	strcat(filter, "*");

	return 1;
}

26
git__DIR *git__opendir(const char *dir)
27
{
28 29
	char filter[GIT_WIN_PATH];
	wchar_t filter_w[GIT_WIN_PATH];
30
	git__DIR *new = NULL;
31 32 33 34

	if (!dir || !init_filter(filter, sizeof(filter), dir))
		return NULL;

Linquize committed
35
	new = git__calloc(1, sizeof(*new));
36 37 38
	if (!new)
		return NULL;

39 40 41
	new->dir = git__strdup(dir);
	if (!new->dir)
		goto fail;
42

43
	git__utf8_to_16(filter_w, GIT_WIN_PATH, filter);
44 45
	new->h = FindFirstFileW(filter_w, &new->f);

46
	if (new->h == INVALID_HANDLE_VALUE) {
47 48
		giterr_set(GITERR_OS, "Could not open directory '%s'", dir);
		goto fail;
49 50
	}

51
	new->first = 1;
52
	return new;
53 54 55 56 57

fail:
	git__free(new->dir);
	git__free(new);
	return NULL;
58 59
}

60 61 62 63 64
int git__readdir_ext(
	git__DIR *d,
	struct git__dirent *entry,
	struct git__dirent **result,
	int *is_dir)
65
{
66 67
	if (!d || !entry || !result || d->h == INVALID_HANDLE_VALUE)
		return -1;
68

69 70
	*result = NULL;

71 72
	if (d->first)
		d->first = 0;
73
	else if (!FindNextFileW(d->h, &d->f)) {
74 75 76 77
		if (GetLastError() == ERROR_NO_MORE_FILES)
			return 0;
		giterr_set(GITERR_OS, "Could not read from directory '%s'", d->dir);
		return -1;
78 79
	}

80 81 82
	if (wcslen(d->f.cFileName) >= sizeof(entry->d_name))
		return -1;

83
	git__utf16_to_8(entry->d_name, d->f.cFileName);
84
	entry->d_ino = 0;
85

86
	*result = entry;
87

88 89 90
	if (is_dir != NULL)
		*is_dir = ((d->f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);

91 92
	return 0;
}
93

94 95 96
struct git__dirent *git__readdir(git__DIR *d)
{
	struct git__dirent *result;
97
	if (git__readdir_ext(d, &d->entry, &result, NULL) < 0)
98 99
		return NULL;
	return result;
100 101
}

102
void git__rewinddir(git__DIR *d)
103
{
104 105
	char filter[GIT_WIN_PATH];
	wchar_t filter_w[GIT_WIN_PATH];
106

107 108 109 110 111
	if (!d)
		return;

	if (d->h != INVALID_HANDLE_VALUE) {
		FindClose(d->h);
112 113
		d->h = INVALID_HANDLE_VALUE;
		d->first = 0;
114
	}
115

116
	if (!init_filter(filter, sizeof(filter), d->dir))
117
		return;
118

119
	git__utf8_to_16(filter_w, GIT_WIN_PATH, filter);
120 121 122 123 124 125
	d->h = FindFirstFileW(filter_w, &d->f);

	if (d->h == INVALID_HANDLE_VALUE)
		giterr_set(GITERR_OS, "Could not open directory '%s'", d->dir);
	else
		d->first = 1;
126 127
}

128
int git__closedir(git__DIR *d)
129
{
130 131 132 133 134 135
	if (!d)
		return 0;

	if (d->h != INVALID_HANDLE_VALUE) {
		FindClose(d->h);
		d->h = INVALID_HANDLE_VALUE;
136
	}
137 138 139
	git__free(d->dir);
	d->dir = NULL;
	git__free(d);
140 141 142
	return 0;
}