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