PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/runtime/ext_zend_compat/php-src/Zend/zend_virtual_cwd.cpp

https://gitlab.com/Blueprint-Marketing/hhvm
C++ | 209 lines | 147 code | 22 blank | 40 comment | 13 complexity | 236ee269addb768665670cac58dbdbd5 MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
  6. | Copyright (c) 1997-2013 The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.01 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_01.txt |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include "hphp/runtime/ext_zend_compat/php-src/Zend/zend.h"
  18. #include "hphp/runtime/ext_zend_compat/php-src/TSRM/tsrm_virtual_cwd.h"
  19. #include "hphp/runtime/ext_zend_compat/php-src/TSRM/TSRM.h"
  20. #include "hphp/runtime/base/execution-context.h"
  21. #include "hphp/runtime/base/file.h"
  22. #include "hphp/runtime/ext/std/ext_std_file.h"
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <unistd.h>
  27. // Unimplemented:
  28. // CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath TSRMLS_DC)
  29. // CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path TSRMLS_DC) TSRMLS_DC)
  30. // CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path TSRMLS_DC)
  31. // CWD_API int virtual_filepath(const char *path, char **filepath TSRMLS_DC)
  32. // CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC)
  33. CWD_API char *virtual_getcwd_ex(size_t *length TSRMLS_DC) /* {{{ */
  34. {
  35. *length = HPHP::g_context->getCwd().size();
  36. return estrdup(HPHP::g_context->getCwd().c_str());
  37. }
  38. /* }}} */
  39. CWD_API char *virtual_getcwd(char *buf, size_t size TSRMLS_DC) /* {{{ */
  40. {
  41. int cwd_size = HPHP::g_context->getCwd().size();
  42. if (cwd_size >= size) {
  43. errno = ERANGE;
  44. return NULL;
  45. } else {
  46. memcpy(buf, HPHP::g_context->getCwd().data(), cwd_size);
  47. buf[cwd_size] = '\0';
  48. return buf;
  49. }
  50. }
  51. /* }}} */
  52. CWD_API int virtual_chdir(const char *path TSRMLS_DC) /* {{{ */
  53. {
  54. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(path, HPHP::CopyString));
  55. struct stat s;
  56. if (stat(translated.c_str(), &s) != 0) {
  57. return -1; // failure
  58. }
  59. if (!S_ISDIR(s.st_mode)) {
  60. errno = ENOTDIR;
  61. return -1; // failure
  62. }
  63. HPHP::g_context->setCwd(translated);
  64. return 0;
  65. }
  66. /* }}} */
  67. CWD_API char *virtual_realpath(const char *path, char *real_path TSRMLS_DC) /* {{{ */
  68. {
  69. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(path, HPHP::CopyString));
  70. if (translated.empty()) {
  71. return NULL;
  72. }
  73. if (!realpath(translated.c_str(), real_path)) {
  74. return NULL;
  75. }
  76. return real_path;
  77. }
  78. /* }}} */
  79. CWD_API FILE *virtual_fopen(const char *path, const char *mode TSRMLS_DC) /* {{{ */
  80. {
  81. if (path[0] == '\0') { /* Fail to open empty path */
  82. return NULL;
  83. }
  84. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(path, HPHP::CopyString));
  85. return fopen(translated.c_str(), mode);
  86. }
  87. /* }}} */
  88. CWD_API int virtual_access(const char *pathname, int mode TSRMLS_DC) /* {{{ */
  89. {
  90. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(pathname, HPHP::CopyString));
  91. return access(translated.c_str(), mode);
  92. }
  93. /* }}} */
  94. CWD_API int virtual_utime(const char *filename, struct utimbuf *buf TSRMLS_DC) /* {{{ */
  95. {
  96. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(filename, HPHP::CopyString));
  97. return utime(translated.c_str(), buf);
  98. }
  99. /* }}} */
  100. CWD_API int virtual_chmod(const char *filename, mode_t mode TSRMLS_DC) /* {{{ */
  101. {
  102. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(filename, HPHP::CopyString));
  103. return chmod(translated.c_str(), mode);
  104. }
  105. /* }}} */
  106. CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link TSRMLS_DC) /* {{{ */
  107. {
  108. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(filename, HPHP::CopyString));
  109. int ret;
  110. if (link) {
  111. #if HAVE_LCHOWN
  112. ret = lchown(translated.c_str(), owner, group);
  113. #else
  114. ret = -1;
  115. #endif
  116. } else {
  117. ret = chown(translated.c_str(), owner, group);
  118. }
  119. return ret;
  120. }
  121. /* }}} */
  122. CWD_API int virtual_open(const char *path TSRMLS_DC, int flags, ...) /* {{{ */
  123. {
  124. int f;
  125. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(path, HPHP::CopyString));
  126. if (flags & O_CREAT) {
  127. mode_t mode;
  128. va_list arg;
  129. va_start(arg, flags);
  130. mode = (mode_t) va_arg(arg, int);
  131. va_end(arg);
  132. f = open(translated.c_str(), flags, mode);
  133. } else {
  134. f = open(translated.c_str(), flags & ~O_CREAT);
  135. }
  136. return f;
  137. }
  138. /* }}} */
  139. CWD_API int virtual_creat(const char *path, mode_t mode TSRMLS_DC) /* {{{ */
  140. {
  141. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(path, HPHP::CopyString));
  142. return creat(translated.c_str(), mode);
  143. }
  144. /* }}} */
  145. CWD_API int virtual_rename(const char *oldname, const char *newname TSRMLS_DC) /* {{{ */
  146. {
  147. HPHP::String oldTrans = HPHP::File::TranslatePath(HPHP::String(oldname, HPHP::CopyString));
  148. HPHP::String newTrans = HPHP::File::TranslatePath(HPHP::String(newname, HPHP::CopyString));
  149. return rename(oldTrans.c_str(), newTrans.c_str());
  150. }
  151. /* }}} */
  152. CWD_API int virtual_stat(const char *path, struct stat *buf TSRMLS_DC) /* {{{ */
  153. {
  154. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(path, HPHP::CopyString));
  155. return stat(translated.c_str(), buf);
  156. }
  157. /* }}} */
  158. CWD_API int virtual_lstat(const char *path, struct stat *buf TSRMLS_DC) /* {{{ */
  159. {
  160. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(path, HPHP::CopyString));
  161. return lstat(translated.c_str(), buf);
  162. }
  163. /* }}} */
  164. CWD_API int virtual_unlink(const char *path TSRMLS_DC) /* {{{ */
  165. {
  166. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(path, HPHP::CopyString));
  167. return unlink(translated.c_str());
  168. }
  169. /* }}} */
  170. CWD_API int virtual_mkdir(const char *pathname, mode_t mode TSRMLS_DC) /* {{{ */
  171. {
  172. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(pathname, HPHP::CopyString));
  173. return mkdir(translated.c_str(), mode);
  174. }
  175. /* }}} */
  176. CWD_API int virtual_rmdir(const char *pathname TSRMLS_DC) /* {{{ */
  177. {
  178. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(pathname, HPHP::CopyString));
  179. return rmdir(translated.c_str());
  180. }
  181. /* }}} */
  182. CWD_API DIR *virtual_opendir(const char *pathname TSRMLS_DC) /* {{{ */
  183. {
  184. HPHP::String translated = HPHP::File::TranslatePath(HPHP::String(pathname, HPHP::CopyString));
  185. return opendir(translated.c_str());
  186. }
  187. /* }}} */