dir.c 2.85 KB
Newer Older
Vicent Marti committed
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
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 10
#include "utf-conv.h"
#include "git2/windows.h"
11 12 13

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

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

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

	return 1;
}

27
git__DIR *git__opendir(const char *dir)
28 29
{
	char filter[4096];
30 31
	wchar_t* filter_w = NULL;
	git__DIR *new = NULL;
32 33 34 35 36 37 38 39

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

	new = git__malloc(sizeof(*new));
	if (!new)
		return NULL;

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

44
	filter_w = gitwin_to_utf16(filter);
45 46 47
	if (!filter_w)
		goto fail;

48
	new->h = FindFirstFileW(filter_w, &new->f);
49
	git__free(filter_w);
50

51
	if (new->h == INVALID_HANDLE_VALUE) {
52 53
		giterr_set(GITERR_OS, "Could not open directory '%s'", dir);
		goto fail;
54 55
	}

56
	new->first = 1;
57
	return new;
58 59 60 61 62

fail:
	git__free(new->dir);
	git__free(new);
	return NULL;
63 64
}

65 66 67 68 69
int git__readdir_ext(
	git__DIR *d,
	struct git__dirent *entry,
	struct git__dirent **result,
	int *is_dir)
70
{
71 72
	if (!d || !entry || !result || d->h == INVALID_HANDLE_VALUE)
		return -1;
73

74 75
	*result = NULL;

76 77
	if (d->first)
		d->first = 0;
78
	else if (!FindNextFileW(d->h, &d->f)) {
79 80 81 82
		if (GetLastError() == ERROR_NO_MORE_FILES)
			return 0;
		giterr_set(GITERR_OS, "Could not read from directory '%s'", d->dir);
		return -1;
83 84
	}

85 86 87 88
	if (wcslen(d->f.cFileName) >= sizeof(entry->d_name))
		return -1;

	entry->d_ino = 0;
89 90

	if (WideCharToMultiByte(
91
		gitwin_get_codepage(), 0, d->f.cFileName, -1,
92 93 94 95 96
		entry->d_name, GIT_PATH_MAX, NULL, NULL) == 0)
	{
		giterr_set(GITERR_OS, "Could not convert filename to UTF-8");
		return -1;
	}
97

98
	*result = entry;
99

100 101 102
	if (is_dir != NULL)
		*is_dir = ((d->f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);

103 104
	return 0;
}
105

106 107 108
struct git__dirent *git__readdir(git__DIR *d)
{
	struct git__dirent *result;
109
	if (git__readdir_ext(d, &d->entry, &result, NULL) < 0)
110 111
		return NULL;
	return result;
112 113
}

114
void git__rewinddir(git__DIR *d)
115 116
{
	char filter[4096];
117
	wchar_t* filter_w;
118

119 120 121 122 123
	if (!d)
		return;

	if (d->h != INVALID_HANDLE_VALUE) {
		FindClose(d->h);
124 125
		d->h = INVALID_HANDLE_VALUE;
		d->first = 0;
126
	}
127

128 129 130
	if (!init_filter(filter, sizeof(filter), d->dir) ||
		(filter_w = gitwin_to_utf16(filter)) == NULL)
		return;
131

132 133 134 135 136 137 138
	d->h = FindFirstFileW(filter_w, &d->f);
	git__free(filter_w);

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

141
int git__closedir(git__DIR *d)
142
{
143 144 145 146 147 148
	if (!d)
		return 0;

	if (d->h != INVALID_HANDLE_VALUE) {
		FindClose(d->h);
		d->h = INVALID_HANDLE_VALUE;
149
	}
150 151 152
	git__free(d->dir);
	d->dir = NULL;
	git__free(d);
153 154 155
	return 0;
}