readdir.c 5.0 KB

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