link_win32.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Pierre A. Joye <pierre@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef PHP_WIN32
  19. #include "php.h"
  20. #include "php_filestat.h"
  21. #include "php_globals.h"
  22. #include <WinBase.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #if HAVE_PWD_H
  26. #include "win32/pwd.h"
  27. #endif
  28. #if HAVE_GRP_H
  29. #include "win32/grp.h"
  30. #endif
  31. #include <errno.h>
  32. #include <ctype.h>
  33. #include "php_link.h"
  34. #include "php_string.h"
  35. /*
  36. TODO:
  37. - Create php_readlink (done), php_link and php_symlink in win32/link.c
  38. - Expose them (PHPAPI) so extensions developers can use them
  39. - define link/readlink/symlink to their php_ equivalent and use them in ext/standart/link.c
  40. - this file is then useless and we have a portable link API
  41. */
  42. #ifndef VOLUME_NAME_NT
  43. #define VOLUME_NAME_NT 0x2
  44. #endif
  45. #ifndef VOLUME_NAME_DOS
  46. #define VOLUME_NAME_DOS 0x0
  47. #endif
  48. /* {{{ proto string readlink(string filename)
  49. Return the target of a symbolic link */
  50. PHP_FUNCTION(readlink)
  51. {
  52. char *link;
  53. ssize_t link_len;
  54. char target[MAXPATHLEN];
  55. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &link, &link_len) == FAILURE) {
  56. return;
  57. }
  58. if (OPENBASEDIR_CHECKPATH(link)) {
  59. RETURN_FALSE;
  60. }
  61. link_len = php_sys_readlink(link, target, MAXPATHLEN);
  62. if (link_len == -1) {
  63. php_error_docref(NULL, E_WARNING, "readlink failed to read the symbolic link (%s), error %d)", link, GetLastError());
  64. RETURN_FALSE;
  65. }
  66. RETURN_STRING(target);
  67. }
  68. /* }}} */
  69. /* {{{ proto int linkinfo(string filename)
  70. Returns the st_dev field of the UNIX C stat structure describing the link */
  71. PHP_FUNCTION(linkinfo)
  72. {
  73. char *link;
  74. char *dirname;
  75. size_t link_len;
  76. zend_stat_t sb;
  77. int ret;
  78. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &link, &link_len) == FAILURE) {
  79. return;
  80. }
  81. dirname = estrndup(link, link_len);
  82. php_dirname(dirname, link_len);
  83. if (php_check_open_basedir(dirname)) {
  84. efree(dirname);
  85. RETURN_FALSE;
  86. }
  87. ret = VCWD_STAT(link, &sb);
  88. if (ret == -1) {
  89. php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  90. efree(dirname);
  91. RETURN_LONG(Z_L(-1));
  92. }
  93. efree(dirname);
  94. RETURN_LONG((zend_long) sb.st_dev);
  95. }
  96. /* }}} */
  97. /* {{{ proto int symlink(string target, string link)
  98. Create a symbolic link */
  99. PHP_FUNCTION(symlink)
  100. {
  101. char *topath, *frompath;
  102. size_t topath_len, frompath_len;
  103. BOOLEAN ret;
  104. char source_p[MAXPATHLEN];
  105. char dest_p[MAXPATHLEN];
  106. char dirname[MAXPATHLEN];
  107. size_t len;
  108. DWORD attr;
  109. wchar_t *dstw, *srcw;
  110. if (zend_parse_parameters(ZEND_NUM_ARGS(), "pp", &topath, &topath_len, &frompath, &frompath_len) == FAILURE) {
  111. return;
  112. }
  113. if (!expand_filepath(frompath, source_p)) {
  114. php_error_docref(NULL, E_WARNING, "No such file or directory");
  115. RETURN_FALSE;
  116. }
  117. memcpy(dirname, source_p, sizeof(source_p));
  118. len = php_dirname(dirname, strlen(dirname));
  119. if (!expand_filepath_ex(topath, dest_p, dirname, len)) {
  120. php_error_docref(NULL, E_WARNING, "No such file or directory");
  121. RETURN_FALSE;
  122. }
  123. if (php_stream_locate_url_wrapper(source_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY) ||
  124. php_stream_locate_url_wrapper(dest_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY) )
  125. {
  126. php_error_docref(NULL, E_WARNING, "Unable to symlink to a URL");
  127. RETURN_FALSE;
  128. }
  129. if (OPENBASEDIR_CHECKPATH(dest_p)) {
  130. RETURN_FALSE;
  131. }
  132. if (OPENBASEDIR_CHECKPATH(source_p)) {
  133. RETURN_FALSE;
  134. }
  135. dstw = php_win32_ioutil_any_to_w(topath);
  136. if (!dstw) {
  137. php_error_docref(NULL, E_WARNING, "UTF-16 conversion failed (error %d)", GetLastError());
  138. RETURN_FALSE;
  139. }
  140. if ((attr = GetFileAttributesW(dstw)) == INVALID_FILE_ATTRIBUTES) {
  141. free(dstw);
  142. php_error_docref(NULL, E_WARNING, "Could not fetch file information(error %d)", GetLastError());
  143. RETURN_FALSE;
  144. }
  145. srcw = php_win32_ioutil_any_to_w(source_p);
  146. if (!srcw) {
  147. free(dstw);
  148. php_error_docref(NULL, E_WARNING, "UTF-16 conversion failed (error %d)", GetLastError());
  149. RETURN_FALSE;
  150. }
  151. /* For the source, an expanded path must be used (in ZTS an other thread could have changed the CWD).
  152. * For the target the exact string given by the user must be used, relative or not, existing or not.
  153. * The target is relative to the link itself, not to the CWD. */
  154. ret = CreateSymbolicLinkW(srcw, dstw, (attr & FILE_ATTRIBUTE_DIRECTORY ? 1 : 0));
  155. if (!ret) {
  156. free(dstw);
  157. free(srcw);
  158. php_error_docref(NULL, E_WARNING, "Cannot create symlink, error code(%d)", GetLastError());
  159. RETURN_FALSE;
  160. }
  161. free(dstw);
  162. free(srcw);
  163. RETURN_TRUE;
  164. }
  165. /* }}} */
  166. /* {{{ proto int link(string target, string link)
  167. Create a hard link */
  168. PHP_FUNCTION(link)
  169. {
  170. char *topath, *frompath;
  171. size_t topath_len, frompath_len;
  172. int ret;
  173. char source_p[MAXPATHLEN];
  174. char dest_p[MAXPATHLEN];
  175. wchar_t *dstw, *srcw;
  176. /*First argument to link function is the target and hence should go to frompath
  177. Second argument to link function is the link itself and hence should go to topath */
  178. if (zend_parse_parameters(ZEND_NUM_ARGS(), "pp", &frompath, &frompath_len, &topath, &topath_len) == FAILURE) {
  179. return;
  180. }
  181. if (!expand_filepath(frompath, source_p) || !expand_filepath(topath, dest_p)) {
  182. php_error_docref(NULL, E_WARNING, "No such file or directory");
  183. RETURN_FALSE;
  184. }
  185. if (php_stream_locate_url_wrapper(source_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY) ||
  186. php_stream_locate_url_wrapper(dest_p, NULL, STREAM_LOCATE_WRAPPERS_ONLY) )
  187. {
  188. php_error_docref(NULL, E_WARNING, "Unable to link to a URL");
  189. RETURN_FALSE;
  190. }
  191. if (OPENBASEDIR_CHECKPATH(source_p)) {
  192. RETURN_FALSE;
  193. }
  194. if (OPENBASEDIR_CHECKPATH(dest_p)) {
  195. RETURN_FALSE;
  196. }
  197. #ifndef ZTS
  198. # define _TO_PATH topath
  199. # define _FROM_PATH frompath
  200. #else
  201. # define _TO_PATH dest_p
  202. # define _FROM_PATH source_p
  203. #endif
  204. dstw = php_win32_ioutil_any_to_w(_TO_PATH);
  205. if (!dstw) {
  206. php_error_docref(NULL, E_WARNING, "UTF-16 conversion failed (error %d)", GetLastError());
  207. RETURN_FALSE;
  208. }
  209. srcw = php_win32_ioutil_any_to_w(_FROM_PATH);
  210. if (!srcw) {
  211. free(dstw);
  212. php_error_docref(NULL, E_WARNING, "UTF-16 conversion failed (error %d)", GetLastError());
  213. RETURN_FALSE;
  214. }
  215. #undef _TO_PATH
  216. #undef _FROM_PATH
  217. ret = CreateHardLinkW(dstw, srcw, NULL);
  218. if (ret == 0) {
  219. free(dstw);
  220. free(srcw);
  221. php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  222. RETURN_FALSE;
  223. }
  224. free(dstw);
  225. free(srcw);
  226. RETURN_TRUE;
  227. }
  228. /* }}} */
  229. #endif
  230. /*
  231. * Local variables:
  232. * tab-width: 4
  233. * c-basic-offset: 4
  234. * End:
  235. * vim600: noet sw=4 ts=4 fdm=marker
  236. * vim<600: noet sw=4 ts=4
  237. */