1 #include "php.h"
2
3 #include <malloc.h>
4 #include <string.h>
5 #include <errno.h>
6
7 #include "readdir.h"
8 #include "win32/ioutil.h"
9
10 /**********************************************************************
11 * Implement dirent-style opendir/readdir/rewinddir/closedir on Win32
12 *
13 * Functions defined are opendir(), readdir(), rewinddir() and
14 * closedir() with the same prototypes as the normal dirent.h
15 * implementation.
16 *
17 * Does not implement telldir(), seekdir(), or scandir(). The dirent
18 * struct is compatible with Unix, except that d_ino is always 1 and
19 * d_off is made up as we go along.
20 *
21 * The DIR typedef is not compatible with Unix.
22 **********************************************************************/
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
opendir(const char * dir)28 DIR *opendir(const char *dir)
29 {/*{{{*/
30 DIR *dp;
31 wchar_t *filespecw, *resolvedw;
32 HANDLE handle;
33 char resolved_path_buff[MAXPATHLEN];
34 size_t resolvedw_len, filespecw_len, index;
35 zend_bool might_need_prefix;
36
37 if (!VCWD_REALPATH(dir, resolved_path_buff)) {
38 return NULL;
39 }
40
41 resolvedw = php_win32_ioutil_conv_any_to_w(resolved_path_buff, PHP_WIN32_CP_IGNORE_LEN, &resolvedw_len);
42 if (!resolvedw) {
43 return NULL;
44 }
45
46 might_need_prefix = resolvedw_len >= 3 && PHP_WIN32_IOUTIL_IS_LETTERW(resolvedw[0]) && L':' == resolvedw[1] && PHP_WIN32_IOUTIL_IS_SLASHW(resolvedw[2]);
47
48 filespecw_len = resolvedw_len + 2;
49 if (filespecw_len >= _MAX_PATH && might_need_prefix) {
50 filespecw_len += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
51 }
52 filespecw = (wchar_t *)malloc((filespecw_len + 1)*sizeof(wchar_t));
53 if (filespecw == NULL) {
54 free(resolvedw);
55 return NULL;
56 }
57
58 if (filespecw_len >= _MAX_PATH && might_need_prefix) {
59 wcscpy(filespecw, PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW);
60 wcscpy(filespecw + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, resolvedw);
61 index = resolvedw_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW - 1;
62 } else {
63 wcscpy(filespecw, resolvedw);
64 index = resolvedw_len - 1;
65 }
66 if (index >= 0 && filespecw[index] == L'/' || index == 0 && filespecw[index] == L'\\')
67 filespecw[index] = L'\0';
68 wcscat(filespecw, L"\\*");
69
70 dp = (DIR *) calloc(1, sizeof(DIR) + (_MAX_FNAME*5+1)*sizeof(char));
71 if (dp == NULL) {
72 free(filespecw);
73 free(resolvedw);
74 return NULL;
75 }
76
77 if ((handle = FindFirstFileExW(filespecw, FindExInfoBasic, &(dp->fileinfo), FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH)) == INVALID_HANDLE_VALUE) {
78 DWORD err = GetLastError();
79 if (err == ERROR_NO_MORE_FILES || err == ERROR_FILE_NOT_FOUND) {
80 dp->finished = 1;
81 } else {
82 free(dp);
83 free(filespecw);
84 free(resolvedw);
85 return NULL;
86 }
87 }
88 dp->dirw = _wcsdup(resolvedw);
89 dp->handle = handle;
90 dp->offset = 0;
91 dp->finished = 0;
92
93 free(filespecw);
94 free(resolvedw);
95
96 return dp;
97 }/*}}}*/
98
readdir(DIR * dp)99 struct dirent *readdir(DIR *dp)
100 {/*{{{*/
101 char *_tmp;
102 size_t reclen;
103
104 if (!dp || dp->finished)
105 return NULL;
106
107 if (dp->offset != 0) {
108 if (FindNextFileW(dp->handle, &(dp->fileinfo)) == 0) {
109 dp->finished = 1;
110 return NULL;
111 }
112 }
113
114 _tmp = php_win32_cp_conv_w_to_any(dp->fileinfo.cFileName, PHP_WIN32_CP_IGNORE_LEN, &reclen);
115 if (!_tmp) {
116 /* wide to utf8 failed, should never happen. */
117 return NULL;
118 }
119 memmove(dp->dent.d_name, _tmp, reclen + 1);
120 free(_tmp);
121 dp->dent.d_reclen = (unsigned short)reclen;
122
123 dp->offset++;
124
125 dp->dent.d_ino = 1;
126 dp->dent.d_off = dp->offset;
127
128 return &(dp->dent);
129 }/*}}}*/
130
closedir(DIR * dp)131 int closedir(DIR *dp)
132 {/*{{{*/
133 if (!dp)
134 return 0;
135 /* It is valid to scan an empty directory but we have an invalid
136 handle in this case (no first file found). */
137 if (dp->handle != INVALID_HANDLE_VALUE) {
138 FindClose(dp->handle);
139 }
140 if (dp->dirw)
141 free(dp->dirw);
142 if (dp)
143 free(dp);
144
145 return 0;
146 }/*}}}*/
147
rewinddir(DIR * dp)148 int rewinddir(DIR *dp)
149 {/*{{{*/
150 /* Re-set to the beginning */
151 wchar_t *filespecw;
152 HANDLE handle;
153 size_t dirw_len, filespecw_len, index;
154 zend_bool might_need_prefix;
155
156 FindClose(dp->handle);
157
158 dp->offset = 0;
159 dp->finished = 0;
160
161 /* XXX save the dir len into the struct. */
162 dirw_len = wcslen((wchar_t *)dp->dirw);
163
164 might_need_prefix = dirw_len >= 3 && PHP_WIN32_IOUTIL_IS_LETTERW(dp->dirw[0]) && L':' == dp->dirw[1] && PHP_WIN32_IOUTIL_IS_SLASHW(dp->dirw[2]);
165
166 filespecw_len = dirw_len + 2;
167 if (filespecw_len >= _MAX_PATH && might_need_prefix) {
168 filespecw_len += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
169 }
170
171 filespecw = (wchar_t *)malloc((filespecw_len + 1)*sizeof(wchar_t));
172 if (filespecw == NULL) {
173 return -1;
174 }
175
176 if (filespecw_len >= _MAX_PATH && might_need_prefix) {
177 wcscpy(filespecw, PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW);
178 wcscpy(filespecw + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, dp->dirw);
179 index = dirw_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW - 1;
180 } else {
181 wcscpy(filespecw, dp->dirw);
182 index = dirw_len - 1;
183 }
184
185 if (index >= 0 && (filespecw[index] == L'/' ||
186 (filespecw[index] == L'\\' && index == 0)))
187 filespecw[index] = L'\0';
188 wcscat(filespecw, L"\\*");
189
190 if ((handle = FindFirstFileExW(filespecw, FindExInfoBasic, &(dp->fileinfo), FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH)) == INVALID_HANDLE_VALUE) {
191 dp->finished = 1;
192 }
193
194 free(filespecw);
195 dp->handle = handle;
196
197 return 0;
198 }/*}}}*/
199
200 #ifdef __cplusplus
201 }
202 #endif
203