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