xref: /PHP-8.1/win32/readdir.c (revision 9066d695)
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 
opendir(const char * dir)24 DIR *opendir(const char *dir)
25 {/*{{{*/
26 	DIR *dp;
27 	wchar_t *filespecw, *resolvedw;
28 	HANDLE handle;
29 	char resolved_path_buff[MAXPATHLEN];
30 	size_t resolvedw_len, filespecw_len, index;
31 	bool might_need_prefix;
32 
33 	if (!VCWD_REALPATH(dir, resolved_path_buff)) {
34 		return NULL;
35 	}
36 
37 	resolvedw = php_win32_ioutil_conv_any_to_w(resolved_path_buff, PHP_WIN32_CP_IGNORE_LEN, &resolvedw_len);
38 	if (!resolvedw) {
39 		return NULL;
40 	}
41 
42 	might_need_prefix = resolvedw_len >= 3 && PHP_WIN32_IOUTIL_IS_LETTERW(resolvedw[0]) && L':' == resolvedw[1] && PHP_WIN32_IOUTIL_IS_SLASHW(resolvedw[2]);
43 
44 	filespecw_len = resolvedw_len + 2;
45 	if (filespecw_len >= _MAX_PATH && might_need_prefix) {
46 		filespecw_len += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
47 	}
48 	filespecw = (wchar_t *)malloc((filespecw_len + 1)*sizeof(wchar_t));
49 	if (filespecw == NULL) {
50 		free(resolvedw);
51 		return NULL;
52 	}
53 
54 	if (filespecw_len >= _MAX_PATH && might_need_prefix) {
55 		wcscpy(filespecw, PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW);
56 		wcscpy(filespecw + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, resolvedw);
57 		index = resolvedw_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW - 1;
58 	} else {
59 		wcscpy(filespecw, resolvedw);
60 		index = resolvedw_len - 1;
61 	}
62 	if (index >= 0 && filespecw[index] == L'/' || index == 0 && filespecw[index] == L'\\')
63 		filespecw[index] = L'\0';
64 	wcscat(filespecw, L"\\*");
65 
66 	dp = (DIR *) calloc(1, sizeof(DIR) + (_MAX_FNAME*5+1)*sizeof(char));
67 	if (dp == NULL) {
68 		free(filespecw);
69 		free(resolvedw);
70 		return NULL;
71 	}
72 
73 	if ((handle = FindFirstFileExW(filespecw, FindExInfoBasic, &(dp->fileinfo), FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH)) == INVALID_HANDLE_VALUE) {
74 		DWORD err = GetLastError();
75 		if (err == ERROR_NO_MORE_FILES || err == ERROR_FILE_NOT_FOUND) {
76 			dp->finished = 1;
77 		} else {
78 			free(dp);
79 			free(filespecw);
80 			free(resolvedw);
81 			return NULL;
82 		}
83 	}
84 	dp->dirw = _wcsdup(resolvedw);
85 	dp->handle = handle;
86 	dp->offset = 0;
87 	dp->finished = 0;
88 
89 	free(filespecw);
90 	free(resolvedw);
91 
92 	return dp;
93 }/*}}}*/
94 
readdir(DIR * dp)95 struct dirent *readdir(DIR *dp)
96 {/*{{{*/
97 	char *_tmp;
98 	size_t reclen;
99 
100 	if (!dp || dp->finished)
101 		return NULL;
102 
103 	if (dp->offset != 0) {
104 		if (FindNextFileW(dp->handle, &(dp->fileinfo)) == 0) {
105 			dp->finished = 1;
106 			return NULL;
107 		}
108 	}
109 
110 	_tmp = php_win32_cp_conv_w_to_any(dp->fileinfo.cFileName, PHP_WIN32_CP_IGNORE_LEN, &reclen);
111 	if (!_tmp) {
112 		/* wide to utf8 failed, should never happen. */
113 		return NULL;
114 	}
115 	memmove(dp->dent.d_name, _tmp, reclen + 1);
116 	free(_tmp);
117 	dp->dent.d_reclen = (unsigned short)reclen;
118 
119 	dp->offset++;
120 
121 	dp->dent.d_ino = 1;
122 	dp->dent.d_off = dp->offset;
123 
124 	return &(dp->dent);
125 }/*}}}*/
126 
closedir(DIR * dp)127 int closedir(DIR *dp)
128 {/*{{{*/
129 	if (!dp)
130 		return 0;
131 	/* It is valid to scan an empty directory but we have an invalid
132 	   handle in this case (no first file found). */
133 	if (dp->handle != INVALID_HANDLE_VALUE) {
134 		FindClose(dp->handle);
135 	}
136 	if (dp->dirw)
137 		free(dp->dirw);
138 	if (dp)
139 		free(dp);
140 
141 	return 0;
142 }/*}}}*/
143 
rewinddir(DIR * dp)144 int rewinddir(DIR *dp)
145 {/*{{{*/
146 	/* Re-set to the beginning */
147 	wchar_t *filespecw;
148 	HANDLE handle;
149 	size_t dirw_len, filespecw_len, index;
150 	bool might_need_prefix;
151 
152 	FindClose(dp->handle);
153 
154 	dp->offset = 0;
155 	dp->finished = 0;
156 
157 	/* XXX save the dir len into the struct. */
158 	dirw_len = wcslen((wchar_t *)dp->dirw);
159 
160 	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]);
161 
162 	filespecw_len = dirw_len + 2;
163 	if (filespecw_len >= _MAX_PATH && might_need_prefix) {
164 		filespecw_len += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
165 	}
166 
167 	filespecw = (wchar_t *)malloc((filespecw_len + 1)*sizeof(wchar_t));
168 	if (filespecw == NULL) {
169 		return -1;
170 	}
171 
172 	if (filespecw_len >= _MAX_PATH && might_need_prefix) {
173 		wcscpy(filespecw, PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW);
174 		wcscpy(filespecw + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, dp->dirw);
175 		index = dirw_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW - 1;
176 	} else {
177 		wcscpy(filespecw, dp->dirw);
178 		index = dirw_len - 1;
179 	}
180 
181 	if (index >= 0 && (filespecw[index] == L'/' ||
182 	   (filespecw[index] == L'\\' && index == 0)))
183 		filespecw[index] = L'\0';
184 	wcscat(filespecw, L"\\*");
185 
186 	if ((handle = FindFirstFileExW(filespecw, FindExInfoBasic, &(dp->fileinfo), FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH)) == INVALID_HANDLE_VALUE) {
187 		dp->finished = 1;
188 	}
189 
190 	free(filespecw);
191 	dp->handle = handle;
192 
193 	return 0;
194 }/*}}}*/
195