xref: /php-src/win32/readdir.c (revision 0b2e6bc2)
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 	if (dp->fileinfo.dwFileAttributes & (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DEVICE)) {
124 		dp->dent.d_type = DT_UNKNOWN; /* conservative */
125 	} else if (dp->fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
126 		dp->dent.d_type = DT_DIR;
127 	} else {
128 		dp->dent.d_type = DT_REG;
129 	}
130 
131 	return &(dp->dent);
132 }/*}}}*/
133 
closedir(DIR * dp)134 int closedir(DIR *dp)
135 {/*{{{*/
136 	if (!dp)
137 		return 0;
138 	/* It is valid to scan an empty directory but we have an invalid
139 	   handle in this case (no first file found). */
140 	if (dp->handle != INVALID_HANDLE_VALUE) {
141 		FindClose(dp->handle);
142 	}
143 	if (dp->dirw)
144 		free(dp->dirw);
145 	if (dp)
146 		free(dp);
147 
148 	return 0;
149 }/*}}}*/
150 
rewinddir(DIR * dp)151 int rewinddir(DIR *dp)
152 {/*{{{*/
153 	/* Re-set to the beginning */
154 	wchar_t *filespecw;
155 	HANDLE handle;
156 	size_t dirw_len, filespecw_len, index;
157 	bool might_need_prefix;
158 
159 	FindClose(dp->handle);
160 
161 	dp->offset = 0;
162 	dp->finished = 0;
163 
164 	/* XXX save the dir len into the struct. */
165 	dirw_len = wcslen((wchar_t *)dp->dirw);
166 
167 	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]);
168 
169 	filespecw_len = dirw_len + 2;
170 	if (filespecw_len >= _MAX_PATH && might_need_prefix) {
171 		filespecw_len += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
172 	}
173 
174 	filespecw = (wchar_t *)malloc((filespecw_len + 1)*sizeof(wchar_t));
175 	if (filespecw == NULL) {
176 		return -1;
177 	}
178 
179 	if (filespecw_len >= _MAX_PATH && might_need_prefix) {
180 		wcscpy(filespecw, PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW);
181 		wcscpy(filespecw + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, dp->dirw);
182 		index = dirw_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW - 1;
183 	} else {
184 		wcscpy(filespecw, dp->dirw);
185 		index = dirw_len - 1;
186 	}
187 
188 	if (index >= 0 && (filespecw[index] == L'/' ||
189 	   (filespecw[index] == L'\\' && index == 0)))
190 		filespecw[index] = L'\0';
191 	wcscat(filespecw, L"\\*");
192 
193 	if ((handle = FindFirstFileExW(filespecw, FindExInfoBasic, &(dp->fileinfo), FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH)) == INVALID_HANDLE_VALUE) {
194 		dp->finished = 1;
195 	}
196 
197 	free(filespecw);
198 	dp->handle = handle;
199 
200 	return 0;
201 }/*}}}*/
202