dir.c 2.57 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
Vicent Marti committed
8
#include "posix.h"
9 10 11

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

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

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

	return 1;
}

25
git__DIR *git__opendir(const char *dir)
26
{
27
	git_win32_path_as_utf8 filter;
28
	git_win32_path filter_w;
29
	git__DIR *new = NULL;
30
	size_t dirlen;
31 32 33 34

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

35 36 37
	dirlen = strlen(dir);

	new = git__calloc(sizeof(*new) + dirlen + 1, 1);
38 39
	if (!new)
		return NULL;
40
	memcpy(new->dir, dir, dirlen);
41

42
	git_win32_path_from_c(filter_w, filter);
43 44
	new->h = FindFirstFileW(filter_w, &new->f);

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

51
	new->first = 1;
52 53 54
	return new;
}

55 56 57 58 59
int git__readdir_ext(
	git__DIR *d,
	struct git__dirent *entry,
	struct git__dirent **result,
	int *is_dir)
60
{
61 62
	if (!d || !entry || !result || d->h == INVALID_HANDLE_VALUE)
		return -1;
63

64 65
	*result = NULL;

66 67
	if (d->first)
		d->first = 0;
68
	else if (!FindNextFileW(d->h, &d->f)) {
69 70 71 72
		if (GetLastError() == ERROR_NO_MORE_FILES)
			return 0;
		giterr_set(GITERR_OS, "Could not read from directory '%s'", d->dir);
		return -1;
73 74
	}

75 76 77
	if (wcslen(d->f.cFileName) >= sizeof(entry->d_name))
		return -1;

78
	git_win32_path_to_c(entry->d_name, d->f.cFileName);
79
	entry->d_ino = 0;
80

81
	*result = entry;
82

83 84 85
	if (is_dir != NULL)
		*is_dir = ((d->f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);

86 87
	return 0;
}
88

89 90 91
struct git__dirent *git__readdir(git__DIR *d)
{
	struct git__dirent *result;
92
	if (git__readdir_ext(d, &d->entry, &result, NULL) < 0)
93 94
		return NULL;
	return result;
95 96
}

97
void git__rewinddir(git__DIR *d)
98
{
99
	git_win32_path_as_utf8 filter;
100
	git_win32_path filter_w;
101

102 103 104 105 106
	if (!d)
		return;

	if (d->h != INVALID_HANDLE_VALUE) {
		FindClose(d->h);
107 108
		d->h = INVALID_HANDLE_VALUE;
		d->first = 0;
109
	}
110

111
	if (!init_filter(filter, sizeof(filter), d->dir))
112
		return;
113

114
	git_win32_path_from_c(filter_w, filter);
115 116 117 118 119 120
	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;
121 122
}

123
int git__closedir(git__DIR *d)
124
{
125 126 127 128 129 130
	if (!d)
		return 0;

	if (d->h != INVALID_HANDLE_VALUE) {
		FindClose(d->h);
		d->h = INVALID_HANDLE_VALUE;
131
	}
132

133
	git__free(d);
134 135 136
	return 0;
}