ioutil.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Anatol Belski <ab@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. /* This file integrates several modified parts from the libuv project, which
  17. * is copyrighted to
  18. *
  19. * Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  20. *
  21. * Permission is hereby granted, free of charge, to any person obtaining a copy
  22. * of this software and associated documentation files (the "Software"), to
  23. * deal in the Software without restriction, including without limitation the
  24. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  25. * sell copies of the Software, and to permit persons to whom the Software is
  26. * furnished to do so, subject to the following conditions:
  27. *
  28. * The above copyright notice and this permission notice shall be included in
  29. * all copies or substantial portions of the Software.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  34. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  35. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  36. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  37. * IN THE SOFTWARE.
  38. */
  39. #include <assert.h>
  40. #include <stdlib.h>
  41. #include <direct.h>
  42. #include <errno.h>
  43. #include <fcntl.h>
  44. #include <io.h>
  45. #include <limits.h>
  46. #include <sys/stat.h>
  47. #include <sys/utime.h>
  48. #include <stdio.h>
  49. #include "php.h"
  50. #include "SAPI.h"
  51. #include "win32/winutil.h"
  52. #include "win32/time.h"
  53. #include "win32/ioutil.h"
  54. #include "win32/codepage.h"
  55. #include "main/streams/php_stream_plain_wrapper.h"
  56. #include <pathcch.h>
  57. #include <winioctl.h>
  58. #include <winnt.h>
  59. /*
  60. #undef NONLS
  61. #undef _WINNLS_
  62. #include <winnls.h>
  63. */
  64. typedef HRESULT (__stdcall *MyPathCchCanonicalizeEx)(wchar_t *pszPathOut, size_t cchPathOut, const wchar_t *pszPathIn, unsigned long dwFlags);
  65. static MyPathCchCanonicalizeEx canonicalize_path_w = NULL;
  66. PW32IO BOOL php_win32_ioutil_posix_to_open_opts(int flags, mode_t mode, php_ioutil_open_opts *opts)
  67. {/*{{{*/
  68. int current_umask;
  69. opts->attributes = 0;
  70. /* Obtain the active umask. umask() never fails and returns the previous */
  71. /* umask. */
  72. current_umask = umask(0);
  73. umask(current_umask);
  74. /* convert flags and mode to CreateFile parameters */
  75. switch (flags & (_O_RDONLY | _O_WRONLY | _O_RDWR)) {
  76. case _O_RDONLY:
  77. opts->access = FILE_GENERIC_READ;
  78. /* XXX not opening dirs yet, see also at the bottom of this function. Should be evaluated properly. */
  79. /*opts->attributes |= FILE_FLAG_BACKUP_SEMANTICS;*/
  80. break;
  81. case _O_WRONLY:
  82. opts->access = FILE_GENERIC_WRITE;
  83. break;
  84. case _O_RDWR:
  85. opts->access = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
  86. break;
  87. default:
  88. goto einval;
  89. }
  90. if (flags & _O_APPEND) {
  91. /* XXX this might look wrong, but i just leave it here. Disabling FILE_WRITE_DATA prevents the current truncate behaviors for files opened with "a". */
  92. /* access &= ~FILE_WRITE_DATA;*/
  93. opts->access |= FILE_APPEND_DATA;
  94. opts->attributes &= ~FILE_FLAG_BACKUP_SEMANTICS;
  95. }
  96. /*
  97. * Here is where we deviate significantly from what CRT's _open()
  98. * does. We indiscriminately use all the sharing modes, to match
  99. * UNIX semantics. In particular, this ensures that the file can
  100. * be deleted even whilst it's open.
  101. */
  102. opts->share = PHP_WIN32_IOUTIL_DEFAULT_SHARE_MODE;
  103. switch (flags & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
  104. case 0:
  105. case _O_EXCL:
  106. opts->disposition = OPEN_EXISTING;
  107. break;
  108. case _O_CREAT:
  109. opts->disposition = OPEN_ALWAYS;
  110. break;
  111. case _O_CREAT | _O_EXCL:
  112. case _O_CREAT | _O_TRUNC | _O_EXCL:
  113. opts->disposition = CREATE_NEW;
  114. break;
  115. case _O_TRUNC:
  116. case _O_TRUNC | _O_EXCL:
  117. opts->disposition = TRUNCATE_EXISTING;
  118. break;
  119. case _O_CREAT | _O_TRUNC:
  120. opts->disposition = CREATE_ALWAYS;
  121. break;
  122. default:
  123. goto einval;
  124. }
  125. opts->attributes |= FILE_ATTRIBUTE_NORMAL;
  126. if (flags & _O_CREAT) {
  127. if (!((mode & ~current_umask) & _S_IWRITE)) {
  128. opts->attributes |= FILE_ATTRIBUTE_READONLY;
  129. }
  130. }
  131. if (flags & _O_TEMPORARY ) {
  132. opts->attributes |= FILE_FLAG_DELETE_ON_CLOSE | FILE_ATTRIBUTE_TEMPORARY;
  133. opts->access |= DELETE;
  134. }
  135. if (flags & _O_SHORT_LIVED) {
  136. opts->attributes |= FILE_ATTRIBUTE_TEMPORARY;
  137. }
  138. switch (flags & (_O_SEQUENTIAL | _O_RANDOM)) {
  139. case 0:
  140. break;
  141. case _O_SEQUENTIAL:
  142. opts->attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
  143. break;
  144. case _O_RANDOM:
  145. opts->attributes |= FILE_FLAG_RANDOM_ACCESS;
  146. break;
  147. default:
  148. goto einval;
  149. }
  150. /* Very compat options */
  151. /*if (flags & O_ASYNC) {
  152. opts->attributes |= FILE_FLAG_OVERLAPPED;
  153. } else if (flags & O_SYNC) {
  154. opts->attributes &= ~FILE_FLAG_OVERLAPPED;
  155. }*/
  156. /* Setting this flag makes it possible to open a directory. */
  157. /* XXX not being done as this means a behavior change. Should be evaluated properly. */
  158. /* opts->attributes |= FILE_FLAG_BACKUP_SEMANTICS; */
  159. return 1;
  160. einval:
  161. _set_errno(EINVAL);
  162. return 0;
  163. }/*}}}*/
  164. PW32IO int php_win32_ioutil_open_w(const wchar_t *path, int flags, ...)
  165. {/*{{{*/
  166. php_ioutil_open_opts open_opts;
  167. HANDLE file;
  168. int fd;
  169. mode_t mode = 0;
  170. PHP_WIN32_IOUTIL_CHECK_PATH_W(path, -1, 0)
  171. if (flags & O_CREAT) {
  172. va_list arg;
  173. va_start(arg, flags);
  174. mode = (mode_t) va_arg(arg, int);
  175. va_end(arg);
  176. }
  177. if (!php_win32_ioutil_posix_to_open_opts(flags, mode, &open_opts)) {
  178. goto einval;
  179. }
  180. /* XXX care about security attributes here if needed, see tsrm_win32_access() */
  181. file = CreateFileW(path,
  182. open_opts.access,
  183. open_opts.share,
  184. NULL,
  185. open_opts.disposition,
  186. open_opts.attributes,
  187. NULL);
  188. if (file == INVALID_HANDLE_VALUE) {
  189. DWORD error = GetLastError();
  190. if (error == ERROR_FILE_EXISTS && (flags & _O_CREAT) &&
  191. !(flags & _O_EXCL)) {
  192. /* Special case: when ERROR_FILE_EXISTS happens and O_CREAT was */
  193. /* specified, it means the path referred to a directory. */
  194. _set_errno(EISDIR);
  195. } else {
  196. SET_ERRNO_FROM_WIN32_CODE(error);
  197. }
  198. return -1;
  199. }
  200. fd = _open_osfhandle((intptr_t) file, flags);
  201. if (fd < 0) {
  202. DWORD error = GetLastError();
  203. /* The only known failure mode for _open_osfhandle() is EMFILE, in which
  204. * case GetLastError() will return zero. However we'll try to handle other
  205. * errors as well, should they ever occur.
  206. */
  207. if (errno == EMFILE) {
  208. _set_errno(EMFILE);
  209. } else if (error != ERROR_SUCCESS) {
  210. SET_ERRNO_FROM_WIN32_CODE(error);
  211. }
  212. CloseHandle(file);
  213. return -1;
  214. }
  215. if (flags & _O_TEXT) {
  216. _setmode(fd, _O_TEXT);
  217. } else if (flags & _O_BINARY) {
  218. _setmode(fd, _O_BINARY);
  219. }
  220. return fd;
  221. einval:
  222. _set_errno(EINVAL);
  223. return -1;
  224. }/*}}}*/
  225. PW32IO int php_win32_ioutil_close(int fd)
  226. {/*{{{*/
  227. int result = -1;
  228. if (-1 == fd) {
  229. _set_errno(EBADF);
  230. return result;
  231. }
  232. if (fd > 2) {
  233. result = _close(fd);
  234. } else {
  235. result = 0;
  236. }
  237. /* _close doesn't set _doserrno on failure, but it does always set errno
  238. * to EBADF on failure.
  239. */
  240. if (result == -1) {
  241. _set_errno(EBADF);
  242. }
  243. return result;
  244. }/*}}}*/
  245. PW32IO int php_win32_ioutil_mkdir_w(const wchar_t *path, mode_t mode)
  246. {/*{{{*/
  247. size_t path_len;
  248. const wchar_t *my_path;
  249. if (!path) {
  250. SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
  251. return -1;
  252. }
  253. PHP_WIN32_IOUTIL_CHECK_PATH_W(path, -1, 0)
  254. path_len = wcslen(path);
  255. if (path_len < _MAX_PATH && path_len >= _MAX_PATH - 12) {
  256. /* Special case here. From the doc:
  257. "When using an API to create a directory, the specified path cannot be
  258. so long that you cannot append an 8.3 file name ..."
  259. Thus, if the directory name length happens to be in this range, it
  260. already needs to be a long path. The given path is already normalized
  261. and prepared, need only to prefix it.
  262. */
  263. wchar_t *tmp = (wchar_t *) malloc((path_len + 1) * sizeof(wchar_t));
  264. if (!tmp) {
  265. SET_ERRNO_FROM_WIN32_CODE(ERROR_NOT_ENOUGH_MEMORY);
  266. return -1;
  267. }
  268. memmove(tmp, path, (path_len + 1) * sizeof(wchar_t));
  269. if (PHP_WIN32_IOUTIL_NORM_FAIL == php_win32_ioutil_normalize_path_w(&tmp, path_len, &path_len)) {
  270. free(tmp);
  271. return -1;
  272. }
  273. if (!PHP_WIN32_IOUTIL_IS_LONG_PATHW(tmp, path_len)) {
  274. wchar_t *_tmp = (wchar_t *) malloc((path_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW + 1) * sizeof(wchar_t));
  275. wchar_t *src, *dst;
  276. if (!_tmp) {
  277. SET_ERRNO_FROM_WIN32_CODE(ERROR_NOT_ENOUGH_MEMORY);
  278. free(tmp);
  279. return -1;
  280. }
  281. memmove(_tmp, PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW, PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW * sizeof(wchar_t));
  282. src = tmp;
  283. dst = _tmp + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
  284. while (src < tmp + path_len) {
  285. if (*src == PHP_WIN32_IOUTIL_FW_SLASHW) {
  286. *dst++ = PHP_WIN32_IOUTIL_DEFAULT_SLASHW;
  287. src++;
  288. } else {
  289. *dst++ = *src++;
  290. }
  291. }
  292. path_len += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
  293. _tmp[path_len] = L'\0';
  294. free(tmp);
  295. tmp = _tmp;
  296. }
  297. my_path = tmp;
  298. } else {
  299. my_path = path;
  300. }
  301. if (!CreateDirectoryW(my_path, NULL)) {
  302. DWORD err = GetLastError();
  303. if (my_path != path) {
  304. free((void *)my_path);
  305. }
  306. SET_ERRNO_FROM_WIN32_CODE(err);
  307. return -1;
  308. }
  309. if (my_path != path) {
  310. free((void *)my_path);
  311. }
  312. return 0;
  313. }/*}}}*/
  314. PW32IO int php_win32_ioutil_unlink_w(const wchar_t *path)
  315. {/*{{{*/
  316. DWORD err = 0;
  317. HANDLE h;
  318. BY_HANDLE_FILE_INFORMATION info;
  319. FILE_DISPOSITION_INFO disposition;
  320. BOOL status;
  321. PHP_WIN32_IOUTIL_CHECK_PATH_W(path, -1, 0)
  322. h = CreateFileW(path,
  323. FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | DELETE,
  324. PHP_WIN32_IOUTIL_DEFAULT_SHARE_MODE,
  325. NULL,
  326. OPEN_EXISTING,
  327. FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
  328. NULL);
  329. if (INVALID_HANDLE_VALUE == h) {
  330. err = GetLastError();
  331. SET_ERRNO_FROM_WIN32_CODE(err);
  332. return -1;
  333. }
  334. if (!GetFileInformationByHandle(h, &info)) {
  335. err = GetLastError();
  336. CloseHandle(h);
  337. SET_ERRNO_FROM_WIN32_CODE(err);
  338. return -1;
  339. }
  340. if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  341. /* TODO Handle possible reparse point. */
  342. CloseHandle(h);
  343. SET_ERRNO_FROM_WIN32_CODE(ERROR_DIRECTORY_NOT_SUPPORTED);
  344. return -1;
  345. }
  346. #if 0
  347. /* XXX BC breach! */
  348. if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
  349. /* Remove read-only attribute */
  350. FILE_BASIC_INFO basic = { 0 };
  351. basic.FileAttributes = info.dwFileAttributes & ~(FILE_ATTRIBUTE_READONLY);
  352. status = SetFileInformationByHandle(h, FileBasicInfo, &basic, sizeof basic);
  353. if (!status) {
  354. err = GetLastError();
  355. SET_ERRNO_FROM_WIN32_CODE(err);
  356. CloseHandle(h);
  357. return -1;
  358. }
  359. }
  360. #endif
  361. /* Try to set the delete flag. */
  362. disposition.DeleteFile = TRUE;
  363. status = SetFileInformationByHandle(h, FileDispositionInfo, &disposition, sizeof disposition);
  364. if (!status) {
  365. err = GetLastError();
  366. CloseHandle(h);
  367. SET_ERRNO_FROM_WIN32_CODE(err);
  368. return -1;
  369. }
  370. CloseHandle(h);
  371. return 0;
  372. }/*}}}*/
  373. PW32IO int php_win32_ioutil_rmdir_w(const wchar_t *path)
  374. {/*{{{*/
  375. int ret = 0;
  376. PHP_WIN32_IOUTIL_CHECK_PATH_W(path, -1, 0)
  377. if (!RemoveDirectoryW(path)) {
  378. DWORD err = GetLastError();
  379. ret = -1;
  380. SET_ERRNO_FROM_WIN32_CODE(err);
  381. }
  382. return ret;
  383. }/*}}}*/
  384. PW32IO int php_win32_ioutil_chdir_w(const wchar_t *path)
  385. {/*{{{*/
  386. int ret = 0;
  387. if (!SetCurrentDirectoryW(path)) {
  388. DWORD err = GetLastError();
  389. ret = -1;
  390. SET_ERRNO_FROM_WIN32_CODE(err);
  391. }
  392. return ret;
  393. }/*}}}*/
  394. PW32IO int php_win32_ioutil_rename_w(const wchar_t *oldname, const wchar_t *newname)
  395. {/*{{{*/
  396. int ret = 0;
  397. PHP_WIN32_IOUTIL_CHECK_PATH_W(oldname, -1, 0)
  398. PHP_WIN32_IOUTIL_CHECK_PATH_W(newname, -1, 0)
  399. if (!MoveFileExW(oldname, newname, MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED)) {
  400. DWORD err = GetLastError();
  401. ret = -1;
  402. SET_ERRNO_FROM_WIN32_CODE(err);
  403. }
  404. return ret;
  405. }/*}}}*/
  406. PW32IO wchar_t *php_win32_ioutil_getcwd_w(wchar_t *buf, size_t len)
  407. {/*{{{*/
  408. DWORD err = 0;
  409. wchar_t *tmp_buf = NULL;
  410. DWORD tmp_len = (DWORD)len;
  411. /* If buf was NULL, the result has to be freed outside here. */
  412. if (!buf) {
  413. tmp_len = GetCurrentDirectoryW(0, NULL) + 1;
  414. if (!tmp_len) {
  415. err = GetLastError();
  416. SET_ERRNO_FROM_WIN32_CODE(err);
  417. return NULL;
  418. } else if (tmp_len > len) {
  419. SET_ERRNO_FROM_WIN32_CODE(ERROR_INSUFFICIENT_BUFFER);
  420. return NULL;
  421. }
  422. tmp_buf = (wchar_t *)malloc((tmp_len)*sizeof(wchar_t));
  423. if (!tmp_buf) {
  424. SET_ERRNO_FROM_WIN32_CODE(ERROR_NOT_ENOUGH_MEMORY);
  425. return NULL;
  426. }
  427. buf = tmp_buf;
  428. }
  429. if (!GetCurrentDirectoryW(tmp_len, buf)) {
  430. err = GetLastError();
  431. SET_ERRNO_FROM_WIN32_CODE(err);
  432. free(tmp_buf);
  433. return NULL;
  434. }
  435. return (wchar_t *)buf;
  436. }/*}}}*/
  437. /* based on zend_dirname(). */
  438. PW32IO size_t php_win32_ioutil_dirname(char *path, size_t len)
  439. {/*{{{*/
  440. char *ret = NULL, *start;
  441. size_t ret_len, len_adjust = 0, pathw_len;
  442. wchar_t *endw, *pathw, *startw;
  443. if (len == 0) {
  444. return 0;
  445. }
  446. start = path;
  447. /* Don't really care about the path normalization, pure parsing here. */
  448. startw = pathw = php_win32_cp_conv_any_to_w(path, len, &pathw_len);
  449. if (!pathw) {
  450. return 0;
  451. }
  452. endw = pathw + pathw_len - 1;
  453. if ((2 <= pathw_len) && iswalpha((wint_t)(pathw)[0]) && (L':' == pathw[1])) {
  454. pathw += 2;
  455. path += 2;
  456. len_adjust += 2;
  457. if (2 == len) {
  458. free(startw);
  459. return len;
  460. }
  461. }
  462. /* Strip trailing slashes */
  463. while (endw >= pathw && PHP_WIN32_IOUTIL_IS_SLASHW(*endw)) {
  464. endw--;
  465. }
  466. if (endw < pathw) {
  467. free(startw);
  468. /* The path only contained slashes */
  469. path[0] = PHP_WIN32_IOUTIL_DEFAULT_SLASH;
  470. path[1] = '\0';
  471. return 1 + len_adjust;
  472. }
  473. /* Strip filename */
  474. while (endw >= pathw && !PHP_WIN32_IOUTIL_IS_SLASHW(*endw)) {
  475. endw--;
  476. }
  477. if (endw < pathw) {
  478. free(startw);
  479. path[0] = '.';
  480. path[1] = '\0';
  481. return 1 + len_adjust;
  482. }
  483. /* Strip slashes which came before the file name */
  484. while (endw >= pathw && PHP_WIN32_IOUTIL_IS_SLASHW(*endw)) {
  485. endw--;
  486. }
  487. if (endw < pathw) {
  488. free(startw);
  489. path[0] = PHP_WIN32_IOUTIL_DEFAULT_SLASH;
  490. path[1] = '\0';
  491. return 1 + len_adjust;
  492. }
  493. *(endw+1) = L'\0';
  494. ret_len = (endw + 1 - startw);
  495. if (PHP_WIN32_IOUTIL_IS_LONG_PATHW(startw, ret_len)) {
  496. ret = php_win32_ioutil_conv_w_to_any(startw + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, ret_len - PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, &ret_len);
  497. } else {
  498. ret = php_win32_ioutil_conv_w_to_any(startw, ret_len, &ret_len);
  499. }
  500. memmove(start, ret, ret_len+1);
  501. assert(start[ret_len] == '\0');
  502. free(ret);
  503. free(startw);
  504. return ret_len;
  505. }/*}}}*/
  506. /* Partial normalization can still be acceptable, explicit fail has to be caught. */
  507. PW32IO php_win32_ioutil_normalization_result php_win32_ioutil_normalize_path_w(wchar_t **buf, size_t len, size_t *new_len)
  508. {/*{{{*/
  509. wchar_t *idx = *buf, canonicalw[MAXPATHLEN], _tmp[MAXPATHLEN], *pos = _tmp;
  510. size_t ret_len = len;
  511. if (len >= MAXPATHLEN) {
  512. SET_ERRNO_FROM_WIN32_CODE(ERROR_BAD_LENGTH);
  513. *new_len = 0;
  514. return PHP_WIN32_IOUTIL_NORM_FAIL;
  515. }
  516. for (; (size_t)(idx - *buf) <= len; idx++, pos++) {
  517. *pos = *idx;
  518. if (PHP_WIN32_IOUTIL_FW_SLASHW == *pos) {
  519. *pos = PHP_WIN32_IOUTIL_DEFAULT_SLASHW;
  520. }
  521. while (PHP_WIN32_IOUTIL_IS_SLASHW(*idx) && PHP_WIN32_IOUTIL_IS_SLASHW(*(idx+1))) {
  522. idx++;
  523. }
  524. }
  525. if (S_OK != canonicalize_path_w(canonicalw, MAXPATHLEN, _tmp, PATHCCH_ALLOW_LONG_PATHS)) {
  526. /* Length unchanged. */
  527. *new_len = len;
  528. return PHP_WIN32_IOUTIL_NORM_PARTIAL;
  529. }
  530. ret_len = wcslen(canonicalw);
  531. if (ret_len != len) {
  532. if (ret_len > len) {
  533. wchar_t *tmp = realloc(*buf, (ret_len + 1) * sizeof(wchar_t));
  534. if (!tmp) {
  535. SET_ERRNO_FROM_WIN32_CODE(ERROR_NOT_ENOUGH_MEMORY);
  536. /* Length unchanged. */
  537. *new_len = len;
  538. return PHP_WIN32_IOUTIL_NORM_PARTIAL;
  539. }
  540. *buf = tmp;
  541. }
  542. memmove(*buf, canonicalw, (ret_len + 1) * sizeof(wchar_t));
  543. }
  544. *new_len = ret_len;
  545. return PHP_WIN32_IOUTIL_NORM_OK;
  546. }/*}}}*/
  547. static HRESULT __stdcall MyPathCchCanonicalizeExFallback(wchar_t *pszPathOut, size_t cchPathOut, const wchar_t *pszPathIn, unsigned long dwFlags)
  548. {/*{{{*/
  549. return -42;
  550. }/*}}}*/
  551. BOOL php_win32_ioutil_init(void)
  552. {/*{{{*/
  553. HMODULE hMod = GetModuleHandle("api-ms-win-core-path-l1-1-0");
  554. if (hMod) {
  555. canonicalize_path_w = (MyPathCchCanonicalizeEx)GetProcAddress(hMod, "PathCchCanonicalizeEx");
  556. if (!canonicalize_path_w) {
  557. canonicalize_path_w = (MyPathCchCanonicalizeEx)MyPathCchCanonicalizeExFallback;
  558. }
  559. } else {
  560. canonicalize_path_w = (MyPathCchCanonicalizeEx)MyPathCchCanonicalizeExFallback;
  561. }
  562. return TRUE;
  563. }/*}}}*/
  564. PW32IO int php_win32_ioutil_access_w(const wchar_t *path, mode_t mode)
  565. {/*{{{*/
  566. DWORD attr;
  567. if ((mode & X_OK) == X_OK) {
  568. DWORD type;
  569. return GetBinaryTypeW(path, &type) ? 0 : -1;
  570. }
  571. attr = GetFileAttributesW(path);
  572. if (attr == INVALID_FILE_ATTRIBUTES) {
  573. DWORD err = GetLastError();
  574. SET_ERRNO_FROM_WIN32_CODE(err);
  575. return -1;
  576. }
  577. if (F_OK == mode) {
  578. return 0;
  579. }
  580. if ((mode &W_OK) == W_OK && (attr & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY) {
  581. SET_ERRNO_FROM_WIN32_CODE(ERROR_ACCESS_DENIED);
  582. return -1;
  583. }
  584. return 0;
  585. }/*}}}*/
  586. PW32IO FILE *php_win32_ioutil_fopen_w(const wchar_t *path, const wchar_t *mode)
  587. {/*{{{*/
  588. FILE *ret;
  589. char modea[16] = {0};
  590. int err = 0, fd, flags, i = 0;
  591. PHP_WIN32_IOUTIL_CHECK_PATH_W(path, NULL, 0)
  592. /* Using the converter from streams, char only. */
  593. while (i < sizeof(modea)-1 && mode[i]) {
  594. modea[i] = (char)mode[i];
  595. i++;
  596. }
  597. if (SUCCESS != php_stream_parse_fopen_modes(modea, &flags)) {
  598. SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
  599. return NULL;
  600. }
  601. fd = php_win32_ioutil_open_w(path, flags, 0666);
  602. if (0 > fd) {
  603. err = GetLastError();
  604. SET_ERRNO_FROM_WIN32_CODE(err);
  605. return NULL;
  606. }
  607. ret = _wfdopen(fd, mode);
  608. if (!ret) {
  609. err = GetLastError();
  610. php_win32_ioutil_close(fd);
  611. SET_ERRNO_FROM_WIN32_CODE(err);
  612. return NULL;
  613. }
  614. return ret;
  615. }/*}}}*/
  616. static size_t php_win32_ioutil_realpath_h(HANDLE *h, wchar_t **resolved)
  617. {/*{{{*/
  618. wchar_t ret[PHP_WIN32_IOUTIL_MAXPATHLEN], *ret_real;
  619. DWORD ret_len, ret_real_len;
  620. ret_len = GetFinalPathNameByHandleW(h, ret, PHP_WIN32_IOUTIL_MAXPATHLEN-1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
  621. if (0 == ret_len) {
  622. DWORD err = GetLastError();
  623. SET_ERRNO_FROM_WIN32_CODE(err);
  624. return (size_t)-1;
  625. } else if (ret_len > PHP_WIN32_IOUTIL_MAXPATHLEN) {
  626. SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
  627. return (size_t)-1;
  628. }
  629. if (NULL == *resolved) {
  630. /* ret is expected to be either NULL or a buffer of capable size. */
  631. *resolved = (wchar_t *) malloc((ret_len + 1)*sizeof(wchar_t));
  632. if (!*resolved) {
  633. SET_ERRNO_FROM_WIN32_CODE(ERROR_NOT_ENOUGH_MEMORY);
  634. return (size_t)-1;
  635. }
  636. }
  637. ret_real = ret;
  638. ret_real_len = ret_len;
  639. if (0 == wcsncmp(ret, PHP_WIN32_IOUTIL_UNC_PATH_PREFIXW, PHP_WIN32_IOUTIL_UNC_PATH_PREFIX_LENW)) {
  640. ret_real += (PHP_WIN32_IOUTIL_UNC_PATH_PREFIX_LENW - 2);
  641. ret_real[0] = L'\\';
  642. ret_real_len -= (PHP_WIN32_IOUTIL_UNC_PATH_PREFIX_LENW - 2);
  643. } else if (PHP_WIN32_IOUTIL_IS_LONG_PATHW(ret, ret_len)) {
  644. ret_real += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
  645. ret_real_len -= PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
  646. }
  647. memmove(*resolved, ret_real, (ret_real_len+1)*sizeof(wchar_t));
  648. return ret_real_len;
  649. }/*}}}*/
  650. PW32IO wchar_t *php_win32_ioutil_realpath_w(const wchar_t *path, wchar_t *resolved)
  651. {/*{{{*/
  652. return php_win32_ioutil_realpath_w_ex0(path, resolved, NULL);
  653. }/*}}}*/
  654. PW32IO wchar_t *php_win32_ioutil_realpath_w_ex0(const wchar_t *path, wchar_t *resolved, PBY_HANDLE_FILE_INFORMATION info)
  655. {/*{{{*/
  656. HANDLE h;
  657. size_t ret_len;
  658. PHP_WIN32_IOUTIL_CHECK_PATH_W(path, NULL, 0)
  659. h = CreateFileW(path,
  660. 0,
  661. 0,
  662. NULL,
  663. OPEN_EXISTING,
  664. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
  665. NULL);
  666. if (INVALID_HANDLE_VALUE == h) {
  667. DWORD err = GetLastError();
  668. SET_ERRNO_FROM_WIN32_CODE(err);
  669. return NULL;
  670. }
  671. ret_len = php_win32_ioutil_realpath_h(h, &resolved);
  672. if ((size_t)-1 == ret_len) {
  673. DWORD err = GetLastError();
  674. CloseHandle(h);
  675. SET_ERRNO_FROM_WIN32_CODE(err);
  676. return NULL;
  677. }
  678. if (NULL != info && !GetFileInformationByHandle(h, info)) {
  679. DWORD err = GetLastError();
  680. CloseHandle(h);
  681. SET_ERRNO_FROM_WIN32_CODE(err);
  682. return NULL;
  683. }
  684. CloseHandle(h);
  685. return resolved;
  686. }/*}}}*/
  687. PW32IO char *realpath(const char *path, char *resolved)
  688. {/*{{{*/
  689. return php_win32_ioutil_realpath(path, resolved);
  690. }/*}}}*/
  691. PW32IO int php_win32_ioutil_symlink_w(const wchar_t *target, const wchar_t *link)
  692. {/*{{{*/
  693. DWORD attr;
  694. BOOLEAN res;
  695. if ((attr = GetFileAttributesW(target)) == INVALID_FILE_ATTRIBUTES) {
  696. SET_ERRNO_FROM_WIN32_CODE(GetLastError());
  697. return -1;
  698. }
  699. res = CreateSymbolicLinkW(link, target, (attr & FILE_ATTRIBUTE_DIRECTORY ? 1 : 0));
  700. if (!res) {
  701. SET_ERRNO_FROM_WIN32_CODE(GetLastError());
  702. return -1;
  703. }
  704. return 0;
  705. }/*}}}*/
  706. PW32IO int php_win32_ioutil_link_w(const wchar_t *target, const wchar_t *link)
  707. {/*{{{*/
  708. BOOL res;
  709. res = CreateHardLinkW(link, target, NULL);
  710. if (!res) {
  711. SET_ERRNO_FROM_WIN32_CODE(GetLastError());
  712. return -1;
  713. }
  714. return 0;
  715. }/*}}}*/
  716. #define FILETIME_TO_UINT(filetime) \
  717. (*((uint64_t*) &(filetime)) - 116444736000000000ULL)
  718. #define FILETIME_TO_TIME_T(filetime) \
  719. (time_t)(FILETIME_TO_UINT(filetime) / 10000000ULL)
  720. static int php_win32_ioutil_fstat_int(HANDLE h, php_win32_ioutil_stat_t *buf, const wchar_t *pathw, size_t pathw_len, PBY_HANDLE_FILE_INFORMATION dp)
  721. {/*{{{*/
  722. BY_HANDLE_FILE_INFORMATION d;
  723. PBY_HANDLE_FILE_INFORMATION data;
  724. LARGE_INTEGER t;
  725. uint8_t is_dir;
  726. data = !dp ? &d : dp;
  727. if(!GetFileInformationByHandle(h, data)) {
  728. if (INVALID_HANDLE_VALUE != h) {
  729. /* Perhaps it's a fileless stream like stdio, reuse the normal stat info. */
  730. struct __stat64 _buf;
  731. if (_fstat64(_open_osfhandle((intptr_t)h, 0), &_buf)) {
  732. return -1;
  733. }
  734. buf->st_dev = _buf.st_dev;
  735. buf->st_ino = _buf.st_ino;
  736. buf->st_mode = _buf.st_mode;
  737. buf->st_nlink = _buf.st_nlink;
  738. buf->st_uid = _buf.st_uid;
  739. buf->st_gid = _buf.st_gid;
  740. buf->st_rdev = _buf.st_rdev;
  741. buf->st_size = _buf.st_size;
  742. buf->st_atime = _buf.st_atime;
  743. buf->st_mtime = _buf.st_mtime;
  744. buf->st_ctime = _buf.st_ctime;
  745. return 0;
  746. } else if(h == INVALID_HANDLE_VALUE && pathw_len > 0) {
  747. /* An abnormal situation it is. For example, the user is the file
  748. owner, but the file has an empty DACL. In that case, it is
  749. possible CreateFile would fail, but the attributes still can
  750. be read. Some info is still going to be missing. */
  751. WIN32_FILE_ATTRIBUTE_DATA _data;
  752. if (!GetFileAttributesExW(pathw, GetFileExInfoStandard, &_data)) {
  753. DWORD err = GetLastError();
  754. SET_ERRNO_FROM_WIN32_CODE(err);
  755. return -1;
  756. }
  757. data->dwFileAttributes = _data.dwFileAttributes;
  758. data->ftCreationTime = _data.ftCreationTime;
  759. data->ftLastAccessTime = _data.ftLastAccessTime;
  760. data->ftLastWriteTime = _data.ftLastWriteTime;
  761. data->nFileSizeHigh = _data.nFileSizeHigh;
  762. data->nFileSizeLow = _data.nFileSizeLow;
  763. data->dwVolumeSerialNumber = 0;
  764. data->nNumberOfLinks = 1;
  765. data->nFileIndexHigh = 0;
  766. data->nFileIndexLow = 0;
  767. } else {
  768. DWORD err = GetLastError();
  769. SET_ERRNO_FROM_WIN32_CODE(err);
  770. return -1;
  771. }
  772. }
  773. is_dir = (data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY;
  774. buf->st_dev = data->dwVolumeSerialNumber;
  775. buf->st_rdev = buf->st_uid = buf->st_gid = 0;
  776. buf->st_ino = (((uint64_t)data->nFileIndexHigh) << 32) + data->nFileIndexLow;
  777. buf->st_mode = 0;
  778. if (!is_dir) {
  779. if (pathw_len >= 4 &&
  780. pathw[pathw_len-4] == L'.') {
  781. if (_wcsnicmp(pathw+pathw_len-3, L"exe", 3) == 0 ||
  782. _wcsnicmp(pathw+pathw_len-3, L"com", 3) == 0) {
  783. DWORD type;
  784. if (GetBinaryTypeW(pathw, &type)) {
  785. buf->st_mode |= (S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6));
  786. }
  787. /* The below is actually incorrect, but keep for BC. */
  788. } else if (_wcsnicmp(pathw+pathw_len-3, L"bat", 3) == 0 ||
  789. _wcsnicmp(pathw+pathw_len-3, L"cmd", 3) == 0) {
  790. buf->st_mode |= (S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6));
  791. }
  792. }
  793. }
  794. if ((data->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
  795. if (is_dir) {
  796. buf->st_mode |= (S_IFDIR|S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6));
  797. } else {
  798. switch (GetFileType(h)) {
  799. case FILE_TYPE_CHAR:
  800. buf->st_mode |= S_IFCHR;
  801. break;
  802. case FILE_TYPE_PIPE:
  803. buf->st_mode |= S_IFIFO;
  804. break;
  805. default:
  806. buf->st_mode |= S_IFREG;
  807. }
  808. }
  809. buf->st_mode |= (data->dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)) : (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)|S_IWRITE|(S_IWRITE>>3)|(S_IWRITE>>6));
  810. }
  811. buf->st_nlink = data->nNumberOfLinks;
  812. t.HighPart = data->nFileSizeHigh;
  813. t.LowPart = data->nFileSizeLow;
  814. /* It's an overflow on 32 bit, however it won't fix as long
  815. as zend_long is 32 bit. */
  816. buf->st_size = (zend_long)t.QuadPart;
  817. buf->st_atime = FILETIME_TO_TIME_T(data->ftLastAccessTime);
  818. buf->st_ctime = FILETIME_TO_TIME_T(data->ftCreationTime);
  819. buf->st_mtime = FILETIME_TO_TIME_T(data->ftLastWriteTime);
  820. return 0;
  821. }/*}}}*/
  822. PW32IO int php_win32_ioutil_stat_ex_w(const wchar_t *path, size_t path_len, php_win32_ioutil_stat_t *buf, int lstat)
  823. {/*{{{*/
  824. BY_HANDLE_FILE_INFORMATION data;
  825. HANDLE hLink = NULL;
  826. DWORD flags_and_attrs = lstat ? FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT : FILE_FLAG_BACKUP_SEMANTICS;
  827. int ret;
  828. ALLOCA_FLAG(use_heap_large)
  829. hLink = CreateFileW(path,
  830. FILE_READ_ATTRIBUTES,
  831. PHP_WIN32_IOUTIL_DEFAULT_SHARE_MODE,
  832. NULL,
  833. OPEN_EXISTING,
  834. flags_and_attrs,
  835. NULL
  836. );
  837. ret = php_win32_ioutil_fstat_int(hLink, buf, path, path_len, &data);
  838. if (lstat && data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
  839. /* File is a reparse point. Get the target */
  840. PHP_WIN32_IOUTIL_REPARSE_DATA_BUFFER * pbuffer;
  841. DWORD retlength = 0;
  842. pbuffer = (PHP_WIN32_IOUTIL_REPARSE_DATA_BUFFER *)do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
  843. if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
  844. free_alloca(pbuffer, use_heap_large);
  845. CloseHandle(hLink);
  846. return -1;
  847. }
  848. if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
  849. buf->st_mode = S_IFLNK;
  850. buf->st_mode |= (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)) : (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)|S_IWRITE|(S_IWRITE>>3)|(S_IWRITE>>6));
  851. }
  852. #if 0 /* Not used yet */
  853. else if(pbuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
  854. buf->st_mode |=;
  855. }
  856. #endif
  857. free_alloca(pbuffer, use_heap_large);
  858. }
  859. CloseHandle(hLink);
  860. return ret;
  861. }/*}}}*/
  862. PW32IO int php_win32_ioutil_fstat(int fd, php_win32_ioutil_stat_t *buf)
  863. {/*{{{*/
  864. return php_win32_ioutil_fstat_int((HANDLE)_get_osfhandle(fd), buf, NULL, 0, NULL);
  865. }/*}}}*/
  866. static ssize_t php_win32_ioutil_readlink_int(HANDLE h, wchar_t *buf, size_t buf_len)
  867. {/*{{{*/
  868. char buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
  869. PHP_WIN32_IOUTIL_REPARSE_DATA_BUFFER *reparse_data = (PHP_WIN32_IOUTIL_REPARSE_DATA_BUFFER*) buffer;
  870. wchar_t* reparse_target;
  871. DWORD reparse_target_len;
  872. DWORD bytes;
  873. if (!DeviceIoControl(h,
  874. FSCTL_GET_REPARSE_POINT,
  875. NULL,
  876. 0,
  877. buffer,
  878. sizeof buffer,
  879. &bytes,
  880. NULL)) {
  881. SET_ERRNO_FROM_WIN32_CODE(GetLastError());
  882. return -1;
  883. }
  884. if (reparse_data->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
  885. /* Real symlink */
  886. /* BC - relative links are shown as absolute */
  887. if (reparse_data->SymbolicLinkReparseBuffer.Flags & SYMLINK_FLAG_RELATIVE) {
  888. SET_ERRNO_FROM_WIN32_CODE(ERROR_SYMLINK_NOT_SUPPORTED);
  889. return -1;
  890. }
  891. reparse_target = reparse_data->SymbolicLinkReparseBuffer.ReparseTarget +
  892. (reparse_data->SymbolicLinkReparseBuffer.SubstituteNameOffset /
  893. sizeof(wchar_t));
  894. reparse_target_len =
  895. reparse_data->SymbolicLinkReparseBuffer.SubstituteNameLength /
  896. sizeof(wchar_t);
  897. /* Real symlinks can contain pretty much everything, but the only thing we
  898. * really care about is undoing the implicit conversion to an NT namespaced
  899. * path that CreateSymbolicLink will perform on absolute paths. If the path
  900. * is win32-namespaced then the user must have explicitly made it so, and
  901. * we better just return the unmodified reparse data. */
  902. if (reparse_target_len >= 4 &&
  903. reparse_target[0] == L'\\' &&
  904. reparse_target[1] == L'?' &&
  905. reparse_target[2] == L'?' &&
  906. reparse_target[3] == L'\\') {
  907. /* Starts with \??\ */
  908. if (reparse_target_len >= 6 &&
  909. ((reparse_target[4] >= L'A' && reparse_target[4] <= L'Z') ||
  910. (reparse_target[4] >= L'a' && reparse_target[4] <= L'z')) &&
  911. reparse_target[5] == L':' &&
  912. (reparse_target_len == 6 || reparse_target[6] == L'\\')) {
  913. /* \??\<drive>:\ */
  914. reparse_target += 4;
  915. reparse_target_len -= 4;
  916. } else if (reparse_target_len >= 8 &&
  917. (reparse_target[4] == L'U' || reparse_target[4] == L'u') &&
  918. (reparse_target[5] == L'N' || reparse_target[5] == L'n') &&
  919. (reparse_target[6] == L'C' || reparse_target[6] == L'c') &&
  920. reparse_target[7] == L'\\') {
  921. /* \??\UNC\<server>\<share>\ - make sure the final path looks like
  922. * \\<server>\<share>\ */
  923. reparse_target += 6;
  924. reparse_target[0] = L'\\';
  925. reparse_target_len -= 6;
  926. }
  927. }
  928. } else if (reparse_data->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
  929. /* Junction. */
  930. reparse_target = reparse_data->MountPointReparseBuffer.ReparseTarget +
  931. (reparse_data->MountPointReparseBuffer.SubstituteNameOffset /
  932. sizeof(wchar_t));
  933. reparse_target_len = reparse_data->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
  934. /* Only treat junctions that look like \??\<drive>:\ as symlink. Junctions
  935. * can also be used as mount points, like \??\Volume{<guid>}, but that's
  936. * confusing for programs since they wouldn't be able to actually
  937. * understand such a path when returned by uv_readlink(). UNC paths are
  938. * never valid for junctions so we don't care about them. */
  939. if (!(reparse_target_len >= 6 &&
  940. reparse_target[0] == L'\\' &&
  941. reparse_target[1] == L'?' &&
  942. reparse_target[2] == L'?' &&
  943. reparse_target[3] == L'\\' &&
  944. ((reparse_target[4] >= L'A' && reparse_target[4] <= L'Z') ||
  945. (reparse_target[4] >= L'a' && reparse_target[4] <= L'z')) &&
  946. reparse_target[5] == L':' &&
  947. (reparse_target_len == 6 || reparse_target[6] == L'\\'))) {
  948. SET_ERRNO_FROM_WIN32_CODE(ERROR_SYMLINK_NOT_SUPPORTED);
  949. return -1;
  950. }
  951. /* Remove leading \??\ */
  952. reparse_target += 4;
  953. reparse_target_len -= 4;
  954. } else {
  955. /* Reparse tag does not indicate a symlink. */
  956. SET_ERRNO_FROM_WIN32_CODE(ERROR_SYMLINK_NOT_SUPPORTED);
  957. return -1;
  958. }
  959. if (reparse_target_len >= buf_len) {
  960. SET_ERRNO_FROM_WIN32_CODE(ERROR_NOT_ENOUGH_MEMORY);
  961. return -1;
  962. }
  963. memcpy(buf, reparse_target, reparse_target_len*sizeof(wchar_t));
  964. buf[reparse_target_len] = L'\0';
  965. return reparse_target_len;
  966. }/*}}}*/
  967. PW32IO ssize_t php_win32_ioutil_readlink_w(const wchar_t *path, wchar_t *buf, size_t buf_len)
  968. {/*{{{*/
  969. HANDLE h;
  970. ssize_t ret;
  971. /* Get a handle to the symbolic link (if path is a symbolic link) */
  972. h = CreateFileW(path,
  973. 0,
  974. 0,
  975. NULL,
  976. OPEN_EXISTING,
  977. FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
  978. NULL);
  979. if (h == INVALID_HANDLE_VALUE) {
  980. SET_ERRNO_FROM_WIN32_CODE(GetLastError());
  981. return -1;
  982. }
  983. ret = php_win32_ioutil_readlink_int(h, buf, buf_len);
  984. if (ret < 0) {
  985. wchar_t target[PHP_WIN32_IOUTIL_MAXPATHLEN];
  986. size_t target_len;
  987. size_t offset = 0;
  988. /* BC - get a handle to the target (if path is a symbolic link) */
  989. CloseHandle(h);
  990. h = CreateFileW(path,
  991. 0,
  992. 0,
  993. NULL,
  994. OPEN_EXISTING,
  995. FILE_FLAG_BACKUP_SEMANTICS,
  996. NULL);
  997. if (h == INVALID_HANDLE_VALUE) {
  998. SET_ERRNO_FROM_WIN32_CODE(GetLastError());
  999. return -1;
  1000. }
  1001. target_len = GetFinalPathNameByHandleW(h, target, PHP_WIN32_IOUTIL_MAXPATHLEN, VOLUME_NAME_DOS);
  1002. if(target_len >= buf_len || target_len >= PHP_WIN32_IOUTIL_MAXPATHLEN || target_len == 0) {
  1003. CloseHandle(h);
  1004. return -1;
  1005. }
  1006. if(target_len > 4) {
  1007. /* Skip first 4 characters if they are "\\?\" */
  1008. if(target[0] == L'\\' && target[1] == L'\\' && target[2] == L'?' && target[3] == L'\\') {
  1009. offset = 4;
  1010. /* \\?\UNC\ */
  1011. if (target_len > 7 && target[4] == L'U' && target[5] == L'N' && target[6] == L'C') {
  1012. offset += 2;
  1013. target[offset] = L'\\';
  1014. }
  1015. }
  1016. }
  1017. ret = target_len - offset;
  1018. memcpy(buf, target + offset, (ret + 1)*sizeof(wchar_t));
  1019. }
  1020. CloseHandle(h);
  1021. return ret;
  1022. }/*}}}*/