1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
|
/*
* 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.
*/
#define GIT__WIN32_NO_WRAP_DIR
#include "posix.h"
static int init_filter(char *filter, size_t n, const char *dir)
{
size_t len = strlen(dir);
if (len+3 >= n)
return 0;
strcpy(filter, dir);
if (len && dir[len-1] != '/')
strcat(filter, "/");
strcat(filter, "*");
return 1;
}
git__DIR *git__opendir(const char *dir)
{
git_win32_path_as_utf8 filter;
git_win32_path filter_w;
git__DIR *new = NULL;
size_t dirlen;
if (!dir || !init_filter(filter, sizeof(filter), dir))
return NULL;
dirlen = strlen(dir);
new = git__calloc(sizeof(*new) + dirlen + 1, 1);
if (!new)
return NULL;
memcpy(new->dir, dir, dirlen);
git_win32_path_from_c(filter_w, filter);
new->h = FindFirstFileW(filter_w, &new->f);
if (new->h == INVALID_HANDLE_VALUE) {
giterr_set(GITERR_OS, "Could not open directory '%s'", dir);
git__free(new);
return NULL;
}
new->first = 1;
return new;
}
int git__readdir_ext(
git__DIR *d,
struct git__dirent *entry,
struct git__dirent **result,
int *is_dir)
{
if (!d || !entry || !result || d->h == INVALID_HANDLE_VALUE)
return -1;
*result = NULL;
if (d->first)
d->first = 0;
else if (!FindNextFileW(d->h, &d->f)) {
if (GetLastError() == ERROR_NO_MORE_FILES)
return 0;
giterr_set(GITERR_OS, "Could not read from directory '%s'", d->dir);
return -1;
}
if (wcslen(d->f.cFileName) >= sizeof(entry->d_name))
return -1;
git_win32_path_to_c(entry->d_name, d->f.cFileName);
entry->d_ino = 0;
*result = entry;
if (is_dir != NULL)
*is_dir = ((d->f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
return 0;
}
struct git__dirent *git__readdir(git__DIR *d)
{
struct git__dirent *result;
if (git__readdir_ext(d, &d->entry, &result, NULL) < 0)
return NULL;
return result;
}
void git__rewinddir(git__DIR *d)
{
git_win32_path_as_utf8 filter;
git_win32_path filter_w;
if (!d)
return;
if (d->h != INVALID_HANDLE_VALUE) {
FindClose(d->h);
d->h = INVALID_HANDLE_VALUE;
d->first = 0;
}
if (!init_filter(filter, sizeof(filter), d->dir))
return;
git_win32_path_from_c(filter_w, filter);
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;
}
int git__closedir(git__DIR *d)
{
if (!d)
return 0;
if (d->h != INVALID_HANDLE_VALUE) {
FindClose(d->h);
d->h = INVALID_HANDLE_VALUE;
}
git__free(d);
return 0;
}
|