zend_virtual_cwd.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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. | Authors: Andi Gutmans <andi@php.net> |
  16. | Sascha Schumann <sascha@schumann.cx> |
  17. | Pierre Joye <pierre@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <limits.h>
  25. #include <errno.h>
  26. #include <stdlib.h>
  27. #include <fcntl.h>
  28. #include <time.h>
  29. #include "zend.h"
  30. #include "zend_virtual_cwd.h"
  31. #ifdef ZEND_WIN32
  32. #include <io.h>
  33. #include "tsrm_win32.h"
  34. # ifndef IO_REPARSE_TAG_SYMLINK
  35. # define IO_REPARSE_TAG_SYMLINK 0xA000000C
  36. # endif
  37. # ifndef IO_REPARSE_TAG_DEDUP
  38. # define IO_REPARSE_TAG_DEDUP 0x80000013
  39. # endif
  40. # ifndef IO_REPARSE_TAG_CLOUD
  41. # define IO_REPARSE_TAG_CLOUD (0x9000001AL)
  42. # endif
  43. /* IO_REPARSE_TAG_CLOUD_1 through IO_REPARSE_TAG_CLOUD_F have values of 0x9000101AL
  44. to 0x9000F01AL, they can be checked against the mask. */
  45. #ifndef IO_REPARSE_TAG_CLOUD_MASK
  46. #define IO_REPARSE_TAG_CLOUD_MASK (0x0000F000L)
  47. #endif
  48. #ifndef IO_REPARSE_TAG_ONEDRIVE
  49. #define IO_REPARSE_TAG_ONEDRIVE (0x80000021L)
  50. #endif
  51. # ifndef IO_REPARSE_TAG_ACTIVISION_HSM
  52. # define IO_REPARSE_TAG_ACTIVISION_HSM (0x00000047L)
  53. # endif
  54. # ifndef IO_REPARSE_TAG_PROJFS
  55. # define IO_REPARSE_TAG_PROJFS (0x9000001CL)
  56. # endif
  57. # ifndef VOLUME_NAME_NT
  58. # define VOLUME_NAME_NT 0x2
  59. # endif
  60. # ifndef VOLUME_NAME_DOS
  61. # define VOLUME_NAME_DOS 0x0
  62. # endif
  63. #endif
  64. #ifndef HAVE_REALPATH
  65. #define realpath(x,y) strcpy(y,x)
  66. #endif
  67. #define VIRTUAL_CWD_DEBUG 0
  68. #include "TSRM.h"
  69. /* Only need mutex for popen() in Windows because it doesn't chdir() on UNIX */
  70. #if defined(ZEND_WIN32) && defined(ZTS)
  71. MUTEX_T cwd_mutex;
  72. #endif
  73. #ifdef ZTS
  74. ts_rsrc_id cwd_globals_id;
  75. #else
  76. virtual_cwd_globals cwd_globals;
  77. #endif
  78. static cwd_state main_cwd_state; /* True global */
  79. #ifndef ZEND_WIN32
  80. #include <unistd.h>
  81. #else
  82. #include <direct.h>
  83. #include "zend_globals.h"
  84. #include "zend_globals_macros.h"
  85. #endif
  86. #define CWD_STATE_COPY(d, s) \
  87. (d)->cwd_length = (s)->cwd_length; \
  88. (d)->cwd = (char *) emalloc((s)->cwd_length+1); \
  89. memcpy((d)->cwd, (s)->cwd, (s)->cwd_length+1);
  90. #define CWD_STATE_FREE(s) \
  91. efree((s)->cwd); \
  92. (s)->cwd_length = 0;
  93. #ifdef ZEND_WIN32
  94. # define CWD_STATE_FREE_ERR(state) do { \
  95. DWORD last_error = GetLastError(); \
  96. CWD_STATE_FREE(state); \
  97. SetLastError(last_error); \
  98. } while (0)
  99. #else
  100. # define CWD_STATE_FREE_ERR(state) CWD_STATE_FREE(state)
  101. #endif
  102. #ifdef ZEND_WIN32
  103. #ifdef CTL_CODE
  104. #undef CTL_CODE
  105. #endif
  106. #define CTL_CODE(DeviceType,Function,Method,Access) (((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
  107. #define FILE_DEVICE_FILE_SYSTEM 0x00000009
  108. #define METHOD_BUFFERED 0
  109. #define FILE_ANY_ACCESS 0
  110. #define FSCTL_GET_REPARSE_POINT CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 42, METHOD_BUFFERED, FILE_ANY_ACCESS)
  111. #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
  112. typedef struct {
  113. unsigned long ReparseTag;
  114. unsigned short ReparseDataLength;
  115. unsigned short Reserved;
  116. union {
  117. struct {
  118. unsigned short SubstituteNameOffset;
  119. unsigned short SubstituteNameLength;
  120. unsigned short PrintNameOffset;
  121. unsigned short PrintNameLength;
  122. unsigned long Flags;
  123. wchar_t ReparseTarget[1];
  124. } SymbolicLinkReparseBuffer;
  125. struct {
  126. unsigned short SubstituteNameOffset;
  127. unsigned short SubstituteNameLength;
  128. unsigned short PrintNameOffset;
  129. unsigned short PrintNameLength;
  130. wchar_t ReparseTarget[1];
  131. } MountPointReparseBuffer;
  132. struct {
  133. unsigned char ReparseTarget[1];
  134. } GenericReparseBuffer;
  135. };
  136. } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
  137. #define SECS_BETWEEN_EPOCHS (__int64)11644473600
  138. #define SECS_TO_100NS (__int64)10000000
  139. static inline time_t FileTimeToUnixTime(const FILETIME *FileTime)
  140. {
  141. __int64 UnixTime;
  142. SYSTEMTIME SystemTime;
  143. FileTimeToSystemTime(FileTime, &SystemTime);
  144. UnixTime = ((__int64)FileTime->dwHighDateTime << 32) +
  145. FileTime->dwLowDateTime;
  146. UnixTime -= (SECS_BETWEEN_EPOCHS * SECS_TO_100NS);
  147. UnixTime /= SECS_TO_100NS; /* now convert to seconds */
  148. if ((time_t)UnixTime != UnixTime) {
  149. UnixTime = 0;
  150. }
  151. return (time_t)UnixTime;
  152. }
  153. CWD_API ssize_t php_sys_readlink(const char *link, char *target, size_t target_len){ /* {{{ */
  154. HANDLE hFile;
  155. wchar_t *linkw = php_win32_ioutil_any_to_w(link), targetw[MAXPATHLEN];
  156. size_t ret_len, targetw_len, offset = 0;
  157. char *ret;
  158. if (!linkw) {
  159. return -1;
  160. }
  161. if (!target_len) {
  162. free(linkw);
  163. return -1;
  164. }
  165. hFile = CreateFileW(linkw, // file to open
  166. 0, // query possible attributes
  167. PHP_WIN32_IOUTIL_DEFAULT_SHARE_MODE,
  168. NULL, // default security
  169. OPEN_EXISTING, // existing file only
  170. FILE_FLAG_BACKUP_SEMANTICS, // normal file
  171. NULL); // no attr. template
  172. if( hFile == INVALID_HANDLE_VALUE) {
  173. free(linkw);
  174. return -1;
  175. }
  176. /* Despite MSDN has documented it won't to, the length returned by
  177. GetFinalPathNameByHandleA includes the length of the
  178. null terminator. This behavior is at least reproducible
  179. with VS2012 and earlier, and seems not to be fixed till
  180. now. Thus, correcting target_len so it's suddenly don't
  181. overflown. */
  182. targetw_len = GetFinalPathNameByHandleW(hFile, targetw, MAXPATHLEN, VOLUME_NAME_DOS);
  183. if(targetw_len >= target_len || targetw_len >= MAXPATHLEN || targetw_len == 0) {
  184. free(linkw);
  185. CloseHandle(hFile);
  186. return -1;
  187. }
  188. if(targetw_len > 4) {
  189. /* Skip first 4 characters if they are "\\?\" */
  190. if(targetw[0] == L'\\' && targetw[1] == L'\\' && targetw[2] == L'?' && targetw[3] == L'\\') {
  191. offset = 4;
  192. /* \\?\UNC\ */
  193. if (targetw_len > 7 && targetw[4] == L'U' && targetw[5] == L'N' && targetw[6] == L'C') {
  194. offset += 2;
  195. targetw[offset] = L'\\';
  196. }
  197. }
  198. }
  199. ret = php_win32_ioutil_conv_w_to_any(targetw + offset, targetw_len - offset, &ret_len);
  200. if (!ret || ret_len >= MAXPATHLEN) {
  201. CloseHandle(hFile);
  202. free(linkw);
  203. free(ret);
  204. return -1;
  205. }
  206. memcpy(target, ret, ret_len + 1);
  207. free(ret);
  208. CloseHandle(hFile);
  209. free(linkw);
  210. return (ssize_t)ret_len;
  211. }
  212. /* }}} */
  213. CWD_API int php_sys_stat_ex(const char *path, zend_stat_t *buf, int lstat) /* {{{ */
  214. {
  215. WIN32_FILE_ATTRIBUTE_DATA data;
  216. LARGE_INTEGER t;
  217. size_t pathw_len = 0;
  218. ALLOCA_FLAG(use_heap_large)
  219. wchar_t *pathw = php_win32_ioutil_conv_any_to_w(path, PHP_WIN32_CP_IGNORE_LEN, &pathw_len);
  220. if (!pathw) {
  221. return -1;
  222. }
  223. if (!GetFileAttributesExW(pathw, GetFileExInfoStandard, &data)) {
  224. int ret;
  225. #if ZEND_ENABLE_ZVAL_LONG64
  226. ret = _wstat64(pathw, buf);
  227. #else
  228. ret = _wstat(pathw, (struct _stat32 *)buf);
  229. #endif
  230. free(pathw);
  231. return ret;
  232. }
  233. if (pathw_len >= 1 && pathw[1] == L':') {
  234. if (pathw[0] >= L'A' && pathw[0] <= L'Z') {
  235. buf->st_dev = buf->st_rdev = pathw[0] - L'A';
  236. } else {
  237. buf->st_dev = buf->st_rdev = pathw[0] - L'a';
  238. }
  239. } else if (PHP_WIN32_IOUTIL_IS_UNC(pathw, pathw_len)) {
  240. buf->st_dev = buf->st_rdev = 0;
  241. } else {
  242. wchar_t cur_path[MAXPATHLEN+1];
  243. if (NULL != _wgetcwd(cur_path, sizeof(cur_path)/sizeof(wchar_t))) {
  244. if (cur_path[1] == L':') {
  245. if (pathw[0] >= L'A' && pathw[0] <= L'Z') {
  246. buf->st_dev = buf->st_rdev = pathw[0] - L'A';
  247. } else {
  248. buf->st_dev = buf->st_rdev = pathw[0] - L'a';
  249. }
  250. } else {
  251. buf->st_dev = buf->st_rdev = -1;
  252. }
  253. } else {
  254. buf->st_dev = buf->st_rdev = -1;
  255. }
  256. }
  257. buf->st_uid = buf->st_gid = buf->st_ino = 0;
  258. if (lstat && data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
  259. /* File is a reparse point. Get the target */
  260. HANDLE hLink = NULL;
  261. REPARSE_DATA_BUFFER * pbuffer;
  262. DWORD retlength = 0;
  263. hLink = CreateFileW(pathw,
  264. FILE_READ_ATTRIBUTES,
  265. PHP_WIN32_IOUTIL_DEFAULT_SHARE_MODE,
  266. NULL,
  267. OPEN_EXISTING,
  268. FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
  269. NULL);
  270. if(hLink == INVALID_HANDLE_VALUE) {
  271. free(pathw);
  272. return -1;
  273. }
  274. pbuffer = (REPARSE_DATA_BUFFER *)do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
  275. if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
  276. free_alloca(pbuffer, use_heap_large);
  277. CloseHandle(hLink);
  278. free(pathw);
  279. return -1;
  280. }
  281. CloseHandle(hLink);
  282. if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
  283. buf->st_mode = S_IFLNK;
  284. 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));
  285. }
  286. #if 0 /* Not used yet */
  287. else if(pbuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
  288. buf->st_mode |=;
  289. }
  290. #endif
  291. free_alloca(pbuffer, use_heap_large);
  292. } else {
  293. buf->st_mode = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? (S_IFDIR|S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6)) : S_IFREG;
  294. 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));
  295. }
  296. if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
  297. if (pathw_len >= 4 && pathw[pathw_len-4] == L'.') {
  298. if (_wcsnicmp(pathw+pathw_len-3, L"exe", 3) == 0 ||
  299. _wcsnicmp(pathw+pathw_len-3, L"com", 3) == 0 ||
  300. _wcsnicmp(pathw+pathw_len-3, L"bat", 3) == 0 ||
  301. _wcsnicmp(pathw+pathw_len-3, L"cmd", 3) == 0) {
  302. buf->st_mode |= (S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6));
  303. }
  304. }
  305. }
  306. buf->st_nlink = 1;
  307. t.HighPart = data.nFileSizeHigh;
  308. t.LowPart = data.nFileSizeLow;
  309. /* It's an overflow on 32 bit, however it won't fix as long
  310. as zend_long is 32 bit. */
  311. buf->st_size = (zend_long)t.QuadPart;
  312. buf->st_atime = FileTimeToUnixTime(&data.ftLastAccessTime);
  313. buf->st_ctime = FileTimeToUnixTime(&data.ftCreationTime);
  314. buf->st_mtime = FileTimeToUnixTime(&data.ftLastWriteTime);
  315. free(pathw);
  316. return 0;
  317. }
  318. /* }}} */
  319. #endif
  320. static int php_is_dir_ok(const cwd_state *state) /* {{{ */
  321. {
  322. zend_stat_t buf;
  323. if (php_sys_stat(state->cwd, &buf) == 0 && S_ISDIR(buf.st_mode))
  324. return (0);
  325. return (1);
  326. }
  327. /* }}} */
  328. static int php_is_file_ok(const cwd_state *state) /* {{{ */
  329. {
  330. zend_stat_t buf;
  331. if (php_sys_stat(state->cwd, &buf) == 0 && S_ISREG(buf.st_mode))
  332. return (0);
  333. return (1);
  334. }
  335. /* }}} */
  336. static void cwd_globals_ctor(virtual_cwd_globals *cwd_g) /* {{{ */
  337. {
  338. CWD_STATE_COPY(&cwd_g->cwd, &main_cwd_state);
  339. cwd_g->realpath_cache_size = 0;
  340. cwd_g->realpath_cache_size_limit = REALPATH_CACHE_SIZE;
  341. cwd_g->realpath_cache_ttl = REALPATH_CACHE_TTL;
  342. memset(cwd_g->realpath_cache, 0, sizeof(cwd_g->realpath_cache));
  343. }
  344. /* }}} */
  345. static void cwd_globals_dtor(virtual_cwd_globals *cwd_g) /* {{{ */
  346. {
  347. realpath_cache_clean();
  348. }
  349. /* }}} */
  350. void virtual_cwd_main_cwd_init(uint8_t reinit) /* {{{ */
  351. {
  352. char cwd[MAXPATHLEN];
  353. char *result;
  354. if (reinit) {
  355. free(main_cwd_state.cwd);
  356. }
  357. #ifdef ZEND_WIN32
  358. ZeroMemory(&cwd, sizeof(cwd));
  359. result = php_win32_ioutil_getcwd(cwd, sizeof(cwd));
  360. #else
  361. result = getcwd(cwd, sizeof(cwd));
  362. #endif
  363. if (!result) {
  364. cwd[0] = '\0';
  365. }
  366. main_cwd_state.cwd_length = strlen(cwd);
  367. #ifdef ZEND_WIN32
  368. if (main_cwd_state.cwd_length >= 2 && cwd[1] == ':') {
  369. cwd[0] = toupper(cwd[0]);
  370. }
  371. #endif
  372. main_cwd_state.cwd = strdup(cwd);
  373. }
  374. /* }}} */
  375. CWD_API void virtual_cwd_startup(void) /* {{{ */
  376. {
  377. virtual_cwd_main_cwd_init(0);
  378. #ifdef ZTS
  379. ts_allocate_id(&cwd_globals_id, sizeof(virtual_cwd_globals), (ts_allocate_ctor) cwd_globals_ctor, (ts_allocate_dtor) cwd_globals_dtor);
  380. #else
  381. cwd_globals_ctor(&cwd_globals);
  382. #endif
  383. #if (defined(ZEND_WIN32)) && defined(ZTS)
  384. cwd_mutex = tsrm_mutex_alloc();
  385. #endif
  386. }
  387. /* }}} */
  388. CWD_API void virtual_cwd_shutdown(void) /* {{{ */
  389. {
  390. #ifndef ZTS
  391. cwd_globals_dtor(&cwd_globals);
  392. #endif
  393. #if (defined(ZEND_WIN32)) && defined(ZTS)
  394. tsrm_mutex_free(cwd_mutex);
  395. #endif
  396. free(main_cwd_state.cwd); /* Don't use CWD_STATE_FREE because the non global states will probably use emalloc()/efree() */
  397. }
  398. /* }}} */
  399. CWD_API int virtual_cwd_activate(void) /* {{{ */
  400. {
  401. if (CWDG(cwd).cwd == NULL) {
  402. CWD_STATE_COPY(&CWDG(cwd), &main_cwd_state);
  403. }
  404. return 0;
  405. }
  406. /* }}} */
  407. CWD_API int virtual_cwd_deactivate(void) /* {{{ */
  408. {
  409. if (CWDG(cwd).cwd != NULL) {
  410. CWD_STATE_FREE(&CWDG(cwd));
  411. CWDG(cwd).cwd = NULL;
  412. }
  413. return 0;
  414. }
  415. /* }}} */
  416. CWD_API char *virtual_getcwd_ex(size_t *length) /* {{{ */
  417. {
  418. cwd_state *state;
  419. state = &CWDG(cwd);
  420. if (state->cwd_length == 0) {
  421. char *retval;
  422. *length = 1;
  423. retval = (char *) emalloc(2);
  424. retval[0] = DEFAULT_SLASH;
  425. retval[1] = '\0';
  426. return retval;
  427. }
  428. #ifdef ZEND_WIN32
  429. /* If we have something like C: */
  430. if (state->cwd_length == 2 && state->cwd[state->cwd_length-1] == ':') {
  431. char *retval;
  432. *length = state->cwd_length+1;
  433. retval = (char *) emalloc(*length+1);
  434. memcpy(retval, state->cwd, *length);
  435. retval[0] = toupper(retval[0]);
  436. retval[*length-1] = DEFAULT_SLASH;
  437. retval[*length] = '\0';
  438. return retval;
  439. }
  440. #endif
  441. if (!state->cwd) {
  442. *length = 0;
  443. return NULL;
  444. }
  445. *length = state->cwd_length;
  446. return estrdup(state->cwd);
  447. }
  448. /* }}} */
  449. /* Same semantics as UNIX getcwd() */
  450. CWD_API char *virtual_getcwd(char *buf, size_t size) /* {{{ */
  451. {
  452. size_t length;
  453. char *cwd;
  454. cwd = virtual_getcwd_ex(&length);
  455. if (buf == NULL) {
  456. return cwd;
  457. }
  458. if (length > size-1) {
  459. efree(cwd);
  460. errno = ERANGE; /* Is this OK? */
  461. return NULL;
  462. }
  463. if (!cwd) {
  464. return NULL;
  465. }
  466. memcpy(buf, cwd, length+1);
  467. efree(cwd);
  468. return buf;
  469. }
  470. /* }}} */
  471. #ifdef ZEND_WIN32
  472. static inline zend_ulong realpath_cache_key(const char *path, size_t path_len) /* {{{ */
  473. {
  474. register zend_ulong h;
  475. size_t bucket_key_len;
  476. char *bucket_key_start = tsrm_win32_get_path_sid_key(path, path_len, &bucket_key_len);
  477. char *bucket_key = (char *)bucket_key_start;
  478. const char *e;
  479. if (!bucket_key) {
  480. return 0;
  481. }
  482. e = bucket_key + bucket_key_len;
  483. for (h = Z_UL(2166136261); bucket_key < e;) {
  484. h *= Z_UL(16777619);
  485. h ^= *bucket_key++;
  486. }
  487. if (bucket_key_start != path) {
  488. HeapFree(GetProcessHeap(), 0, (LPVOID)bucket_key_start);
  489. }
  490. return h;
  491. }
  492. /* }}} */
  493. #else
  494. static inline zend_ulong realpath_cache_key(const char *path, size_t path_len) /* {{{ */
  495. {
  496. register zend_ulong h;
  497. const char *e = path + path_len;
  498. for (h = Z_UL(2166136261); path < e;) {
  499. h *= Z_UL(16777619);
  500. h ^= *path++;
  501. }
  502. return h;
  503. }
  504. /* }}} */
  505. #endif /* defined(ZEND_WIN32) */
  506. CWD_API void realpath_cache_clean(void) /* {{{ */
  507. {
  508. uint32_t i;
  509. for (i = 0; i < sizeof(CWDG(realpath_cache))/sizeof(CWDG(realpath_cache)[0]); i++) {
  510. realpath_cache_bucket *p = CWDG(realpath_cache)[i];
  511. while (p != NULL) {
  512. realpath_cache_bucket *r = p;
  513. p = p->next;
  514. free(r);
  515. }
  516. CWDG(realpath_cache)[i] = NULL;
  517. }
  518. CWDG(realpath_cache_size) = 0;
  519. }
  520. /* }}} */
  521. CWD_API void realpath_cache_del(const char *path, size_t path_len) /* {{{ */
  522. {
  523. zend_ulong key = realpath_cache_key(path, path_len);
  524. zend_ulong n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
  525. realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
  526. while (*bucket != NULL) {
  527. if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
  528. memcmp(path, (*bucket)->path, path_len) == 0) {
  529. realpath_cache_bucket *r = *bucket;
  530. *bucket = (*bucket)->next;
  531. /* if the pointers match then only subtract the length of the path */
  532. if(r->path == r->realpath) {
  533. CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1;
  534. } else {
  535. CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
  536. }
  537. free(r);
  538. return;
  539. } else {
  540. bucket = &(*bucket)->next;
  541. }
  542. }
  543. }
  544. /* }}} */
  545. static inline void realpath_cache_add(const char *path, size_t path_len, const char *realpath, size_t realpath_len, int is_dir, time_t t) /* {{{ */
  546. {
  547. zend_long size = sizeof(realpath_cache_bucket) + path_len + 1;
  548. int same = 1;
  549. if (realpath_len != path_len ||
  550. memcmp(path, realpath, path_len) != 0) {
  551. size += realpath_len + 1;
  552. same = 0;
  553. }
  554. if (CWDG(realpath_cache_size) + size <= CWDG(realpath_cache_size_limit)) {
  555. realpath_cache_bucket *bucket = malloc(size);
  556. zend_ulong n;
  557. if (bucket == NULL) {
  558. return;
  559. }
  560. bucket->key = realpath_cache_key(path, path_len);
  561. bucket->path = (char*)bucket + sizeof(realpath_cache_bucket);
  562. memcpy(bucket->path, path, path_len+1);
  563. bucket->path_len = path_len;
  564. if (same) {
  565. bucket->realpath = bucket->path;
  566. } else {
  567. bucket->realpath = bucket->path + (path_len + 1);
  568. memcpy(bucket->realpath, realpath, realpath_len+1);
  569. }
  570. bucket->realpath_len = realpath_len;
  571. bucket->is_dir = is_dir > 0;
  572. #ifdef ZEND_WIN32
  573. bucket->is_rvalid = 0;
  574. bucket->is_readable = 0;
  575. bucket->is_wvalid = 0;
  576. bucket->is_writable = 0;
  577. #endif
  578. bucket->expires = t + CWDG(realpath_cache_ttl);
  579. n = bucket->key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
  580. bucket->next = CWDG(realpath_cache)[n];
  581. CWDG(realpath_cache)[n] = bucket;
  582. CWDG(realpath_cache_size) += size;
  583. }
  584. }
  585. /* }}} */
  586. static inline realpath_cache_bucket* realpath_cache_find(const char *path, size_t path_len, time_t t) /* {{{ */
  587. {
  588. zend_ulong key = realpath_cache_key(path, path_len);
  589. zend_ulong n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
  590. realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
  591. while (*bucket != NULL) {
  592. if (CWDG(realpath_cache_ttl) && (*bucket)->expires < t) {
  593. realpath_cache_bucket *r = *bucket;
  594. *bucket = (*bucket)->next;
  595. /* if the pointers match then only subtract the length of the path */
  596. if(r->path == r->realpath) {
  597. CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1;
  598. } else {
  599. CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
  600. }
  601. free(r);
  602. } else if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
  603. memcmp(path, (*bucket)->path, path_len) == 0) {
  604. return *bucket;
  605. } else {
  606. bucket = &(*bucket)->next;
  607. }
  608. }
  609. return NULL;
  610. }
  611. /* }}} */
  612. CWD_API realpath_cache_bucket* realpath_cache_lookup(const char *path, size_t path_len, time_t t) /* {{{ */
  613. {
  614. return realpath_cache_find(path, path_len, t);
  615. }
  616. /* }}} */
  617. CWD_API zend_long realpath_cache_size(void)
  618. {
  619. return CWDG(realpath_cache_size);
  620. }
  621. CWD_API zend_long realpath_cache_max_buckets(void)
  622. {
  623. return (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
  624. }
  625. CWD_API realpath_cache_bucket** realpath_cache_get_buckets(void)
  626. {
  627. return CWDG(realpath_cache);
  628. }
  629. #undef LINK_MAX
  630. #define LINK_MAX 32
  631. static size_t tsrm_realpath_r(char *path, size_t start, size_t len, int *ll, time_t *t, int use_realpath, int is_dir, int *link_is_dir) /* {{{ */
  632. {
  633. size_t i, j;
  634. int directory = 0, save;
  635. #ifdef ZEND_WIN32
  636. WIN32_FIND_DATAW dataw;
  637. HANDLE hFind = INVALID_HANDLE_VALUE;
  638. ALLOCA_FLAG(use_heap_large)
  639. wchar_t *pathw = NULL;
  640. int may_retry_reparse_point;
  641. #define FREE_PATHW() \
  642. do { free(pathw); } while(0);
  643. #else
  644. zend_stat_t st;
  645. #endif
  646. realpath_cache_bucket *bucket;
  647. char *tmp;
  648. ALLOCA_FLAG(use_heap)
  649. while (1) {
  650. if (len <= start) {
  651. if (link_is_dir) {
  652. *link_is_dir = 1;
  653. }
  654. return start;
  655. }
  656. i = len;
  657. while (i > start && !IS_SLASH(path[i-1])) {
  658. i--;
  659. }
  660. assert(i < MAXPATHLEN);
  661. if (i == len ||
  662. (i + 1 == len && path[i] == '.')) {
  663. /* remove double slashes and '.' */
  664. len = EXPECTED(i > 0) ? i - 1 : 0;
  665. is_dir = 1;
  666. continue;
  667. } else if (i + 2 == len && path[i] == '.' && path[i+1] == '.') {
  668. /* remove '..' and previous directory */
  669. is_dir = 1;
  670. if (link_is_dir) {
  671. *link_is_dir = 1;
  672. }
  673. if (i <= start + 1) {
  674. return start ? start : len;
  675. }
  676. j = tsrm_realpath_r(path, start, i-1, ll, t, use_realpath, 1, NULL);
  677. if (j > start && j != (size_t)-1) {
  678. j--;
  679. assert(i < MAXPATHLEN);
  680. while (j > start && !IS_SLASH(path[j])) {
  681. j--;
  682. }
  683. assert(i < MAXPATHLEN);
  684. if (!start) {
  685. /* leading '..' must not be removed in case of relative path */
  686. if (j == 0 && path[0] == '.' && path[1] == '.' &&
  687. IS_SLASH(path[2])) {
  688. path[3] = '.';
  689. path[4] = '.';
  690. path[5] = DEFAULT_SLASH;
  691. j = 5;
  692. } else if (j > 0 &&
  693. path[j+1] == '.' && path[j+2] == '.' &&
  694. IS_SLASH(path[j+3])) {
  695. j += 4;
  696. path[j++] = '.';
  697. path[j++] = '.';
  698. path[j] = DEFAULT_SLASH;
  699. }
  700. }
  701. } else if (!start && !j) {
  702. /* leading '..' must not be removed in case of relative path */
  703. path[0] = '.';
  704. path[1] = '.';
  705. path[2] = DEFAULT_SLASH;
  706. j = 2;
  707. }
  708. return j;
  709. }
  710. path[len] = 0;
  711. save = (use_realpath != CWD_EXPAND);
  712. if (start && save && CWDG(realpath_cache_size_limit)) {
  713. /* cache lookup for absolute path */
  714. if (!*t) {
  715. *t = time(0);
  716. }
  717. if ((bucket = realpath_cache_find(path, len, *t)) != NULL) {
  718. if (is_dir && !bucket->is_dir) {
  719. /* not a directory */
  720. return (size_t)-1;
  721. } else {
  722. if (link_is_dir) {
  723. *link_is_dir = bucket->is_dir;
  724. }
  725. memcpy(path, bucket->realpath, bucket->realpath_len + 1);
  726. return bucket->realpath_len;
  727. }
  728. }
  729. }
  730. #ifdef ZEND_WIN32
  731. retry_reparse_point:
  732. may_retry_reparse_point = 0;
  733. if (save) {
  734. pathw = php_win32_ioutil_any_to_w(path);
  735. if (!pathw) {
  736. return (size_t)-1;
  737. }
  738. hFind = FindFirstFileExW(pathw, FindExInfoBasic, &dataw, FindExSearchNameMatch, NULL, 0);
  739. if (INVALID_HANDLE_VALUE == hFind) {
  740. if (use_realpath == CWD_REALPATH) {
  741. /* file not found */
  742. FREE_PATHW()
  743. return (size_t)-1;
  744. }
  745. /* continue resolution anyway but don't save result in the cache */
  746. save = 0;
  747. } else {
  748. FindClose(hFind);
  749. }
  750. }
  751. tmp = do_alloca(len+1, use_heap);
  752. memcpy(tmp, path, len+1);
  753. retry_reparse_tag_cloud:
  754. if(save &&
  755. !(IS_UNC_PATH(path, len) && len >= 3 && path[2] != '?') &&
  756. (dataw.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
  757. ) {
  758. /* File is a reparse point. Get the target */
  759. HANDLE hLink = NULL;
  760. REPARSE_DATA_BUFFER * pbuffer;
  761. DWORD retlength = 0;
  762. size_t bufindex = 0;
  763. uint8_t isabsolute = 0;
  764. wchar_t * reparsetarget;
  765. BOOL isVolume = FALSE;
  766. #if VIRTUAL_CWD_DEBUG
  767. char *printname = NULL;
  768. #endif
  769. char *substitutename = NULL;
  770. size_t substitutename_len;
  771. size_t substitutename_off = 0;
  772. wchar_t tmpsubstname[MAXPATHLEN];
  773. if(++(*ll) > LINK_MAX) {
  774. free_alloca(tmp, use_heap);
  775. FREE_PATHW()
  776. return (size_t)-1;
  777. }
  778. hLink = CreateFileW(pathw,
  779. 0,
  780. PHP_WIN32_IOUTIL_DEFAULT_SHARE_MODE,
  781. NULL,
  782. OPEN_EXISTING,
  783. FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
  784. NULL);
  785. if(hLink == INVALID_HANDLE_VALUE) {
  786. free_alloca(tmp, use_heap);
  787. FREE_PATHW()
  788. return (size_t)-1;
  789. }
  790. pbuffer = (REPARSE_DATA_BUFFER *)do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
  791. if (pbuffer == NULL) {
  792. CloseHandle(hLink);
  793. free_alloca(tmp, use_heap);
  794. FREE_PATHW()
  795. return (size_t)-1;
  796. }
  797. if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
  798. BY_HANDLE_FILE_INFORMATION fileInformation;
  799. free_alloca(pbuffer, use_heap_large);
  800. if ((dataw.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
  801. (dataw.dwReserved0 & ~IO_REPARSE_TAG_CLOUD_MASK) == IO_REPARSE_TAG_CLOUD &&
  802. EG(windows_version_info).dwMajorVersion >= 10 &&
  803. EG(windows_version_info).dwMinorVersion == 0 &&
  804. EG(windows_version_info).dwBuildNumber >= 18362 &&
  805. GetFileInformationByHandle(hLink, &fileInformation) &&
  806. !(fileInformation.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
  807. dataw.dwFileAttributes = fileInformation.dwFileAttributes;
  808. CloseHandle(hLink);
  809. (*ll)--;
  810. goto retry_reparse_tag_cloud;
  811. }
  812. free_alloca(tmp, use_heap);
  813. CloseHandle(hLink);
  814. FREE_PATHW()
  815. return (size_t)-1;
  816. }
  817. CloseHandle(hLink);
  818. if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
  819. may_retry_reparse_point = 1;
  820. reparsetarget = pbuffer->SymbolicLinkReparseBuffer.ReparseTarget;
  821. isabsolute = (pbuffer->SymbolicLinkReparseBuffer.Flags == 0) ? 1 : 0;
  822. #if VIRTUAL_CWD_DEBUG
  823. printname = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR));
  824. if (!printname) {
  825. free_alloca(pbuffer, use_heap_large);
  826. free_alloca(tmp, use_heap);
  827. FREE_PATHW()
  828. return (size_t)-1;
  829. }
  830. #endif
  831. substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
  832. if (substitutename_len >= MAXPATHLEN) {
  833. free_alloca(pbuffer, use_heap_large);
  834. free_alloca(tmp, use_heap);
  835. FREE_PATHW()
  836. return (size_t)-1;
  837. }
  838. memmove(tmpsubstname, reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR), pbuffer->MountPointReparseBuffer.SubstituteNameLength);
  839. tmpsubstname[substitutename_len] = L'\0';
  840. substitutename = php_win32_cp_conv_w_to_any(tmpsubstname, substitutename_len, &substitutename_len);
  841. if (!substitutename || substitutename_len >= MAXPATHLEN) {
  842. free_alloca(pbuffer, use_heap_large);
  843. free_alloca(tmp, use_heap);
  844. free(substitutename);
  845. #if VIRTUAL_CWD_DEBUG
  846. free(printname);
  847. #endif
  848. FREE_PATHW()
  849. return (size_t)-1;
  850. }
  851. }
  852. else if(pbuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
  853. isabsolute = 1;
  854. reparsetarget = pbuffer->MountPointReparseBuffer.ReparseTarget;
  855. #if VIRTUAL_CWD_DEBUG
  856. printname = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR));
  857. if (!printname) {
  858. free_alloca(pbuffer, use_heap_large);
  859. free_alloca(tmp, use_heap);
  860. FREE_PATHW()
  861. return (size_t)-1;
  862. }
  863. #endif
  864. substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
  865. if (substitutename_len >= MAXPATHLEN) {
  866. free_alloca(pbuffer, use_heap_large);
  867. free_alloca(tmp, use_heap);
  868. FREE_PATHW()
  869. return (size_t)-1;
  870. }
  871. memmove(tmpsubstname, reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR), pbuffer->MountPointReparseBuffer.SubstituteNameLength);
  872. tmpsubstname[substitutename_len] = L'\0';
  873. substitutename = php_win32_cp_conv_w_to_any(tmpsubstname, substitutename_len, &substitutename_len);
  874. if (!substitutename || substitutename_len >= MAXPATHLEN) {
  875. free_alloca(pbuffer, use_heap_large);
  876. free_alloca(tmp, use_heap);
  877. free(substitutename);
  878. #if VIRTUAL_CWD_DEBUG
  879. free(printname);
  880. #endif
  881. FREE_PATHW()
  882. return (size_t)-1;
  883. }
  884. }
  885. else if (pbuffer->ReparseTag == IO_REPARSE_TAG_DEDUP ||
  886. /* Starting with 1709. */
  887. (pbuffer->ReparseTag & ~IO_REPARSE_TAG_CLOUD_MASK) == IO_REPARSE_TAG_CLOUD ||
  888. IO_REPARSE_TAG_ONEDRIVE == pbuffer->ReparseTag ||
  889. IO_REPARSE_TAG_ACTIVISION_HSM == pbuffer->ReparseTag ||
  890. IO_REPARSE_TAG_PROJFS == pbuffer->ReparseTag) {
  891. isabsolute = 1;
  892. substitutename = malloc((len + 1) * sizeof(char));
  893. if (!substitutename) {
  894. free_alloca(pbuffer, use_heap_large);
  895. free_alloca(tmp, use_heap);
  896. FREE_PATHW()
  897. return (size_t)-1;
  898. }
  899. memcpy(substitutename, path, len + 1);
  900. substitutename_len = len;
  901. } else {
  902. /* XXX this might be not the end, restart handling with REPARSE_GUID_DATA_BUFFER should be implemented. */
  903. free_alloca(pbuffer, use_heap_large);
  904. free_alloca(tmp, use_heap);
  905. FREE_PATHW()
  906. return (size_t)-1;
  907. }
  908. if(isabsolute && substitutename_len > 4) {
  909. /* Do not resolve volumes (for now). A mounted point can
  910. target a volume without a drive, it is not certain that
  911. all IO functions we use in php and its deps support
  912. path with volume GUID instead of the DOS way, like:
  913. d:\test\mnt\foo
  914. \\?\Volume{62d1c3f8-83b9-11de-b108-806e6f6e6963}\foo
  915. */
  916. if (strncmp(substitutename, "\\??\\Volume{",11) == 0
  917. || strncmp(substitutename, "\\\\?\\Volume{",11) == 0
  918. || strncmp(substitutename, "\\??\\UNC\\", 8) == 0
  919. ) {
  920. isVolume = TRUE;
  921. substitutename_off = 0;
  922. } else
  923. /* do not use the \??\ and \\?\ prefix*/
  924. if (strncmp(substitutename, "\\??\\", 4) == 0
  925. || strncmp(substitutename, "\\\\?\\", 4) == 0) {
  926. substitutename_off = 4;
  927. }
  928. }
  929. if (!isVolume) {
  930. char * tmp2 = substitutename + substitutename_off;
  931. for (bufindex = 0; bufindex + substitutename_off < substitutename_len; bufindex++) {
  932. *(path + bufindex) = *(tmp2 + bufindex);
  933. }
  934. *(path + bufindex) = 0;
  935. j = bufindex;
  936. } else {
  937. j = len;
  938. }
  939. #if VIRTUAL_CWD_DEBUG
  940. fprintf(stderr, "reparse: print: %s ", printname);
  941. fprintf(stderr, "sub: %s ", substitutename);
  942. fprintf(stderr, "resolved: %s ", path);
  943. free(printname);
  944. #endif
  945. free_alloca(pbuffer, use_heap_large);
  946. free(substitutename);
  947. if (may_retry_reparse_point) {
  948. DWORD attrs;
  949. FREE_PATHW()
  950. pathw = php_win32_ioutil_any_to_w(path);
  951. if (!pathw) {
  952. return (size_t)-1;
  953. }
  954. attrs = GetFileAttributesW(pathw);
  955. if (!isVolume && attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_REPARSE_POINT)) {
  956. free_alloca(tmp, use_heap);
  957. FREE_PATHW()
  958. goto retry_reparse_point;
  959. }
  960. }
  961. if(isabsolute == 1) {
  962. if (!((j == 3) && (path[1] == ':') && (path[2] == '\\'))) {
  963. /* use_realpath is 0 in the call below coz path is absolute*/
  964. j = tsrm_realpath_r(path, 0, j, ll, t, 0, is_dir, &directory);
  965. if(j == (size_t)-1) {
  966. free_alloca(tmp, use_heap);
  967. FREE_PATHW()
  968. return (size_t)-1;
  969. }
  970. }
  971. }
  972. else {
  973. if(i + j >= MAXPATHLEN - 1) {
  974. free_alloca(tmp, use_heap);
  975. FREE_PATHW()
  976. return (size_t)-1;
  977. }
  978. memmove(path+i, path, j+1);
  979. memcpy(path, tmp, i-1);
  980. path[i-1] = DEFAULT_SLASH;
  981. j = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory);
  982. if(j == (size_t)-1) {
  983. free_alloca(tmp, use_heap);
  984. FREE_PATHW()
  985. return (size_t)-1;
  986. }
  987. }
  988. directory = (dataw.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  989. if(link_is_dir) {
  990. *link_is_dir = directory;
  991. }
  992. }
  993. else {
  994. if (save) {
  995. directory = (dataw.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
  996. if (is_dir && !directory) {
  997. /* not a directory */
  998. free_alloca(tmp, use_heap);
  999. FREE_PATHW()
  1000. return (size_t)-1;
  1001. }
  1002. }
  1003. #else
  1004. if (save && php_sys_lstat(path, &st) < 0) {
  1005. if (use_realpath == CWD_REALPATH) {
  1006. /* file not found */
  1007. return (size_t)-1;
  1008. }
  1009. /* continue resolution anyway but don't save result in the cache */
  1010. save = 0;
  1011. }
  1012. tmp = do_alloca(len+1, use_heap);
  1013. memcpy(tmp, path, len+1);
  1014. if (save && S_ISLNK(st.st_mode)) {
  1015. if (++(*ll) > LINK_MAX || (j = (size_t)php_sys_readlink(tmp, path, MAXPATHLEN)) == (size_t)-1) {
  1016. /* too many links or broken symlinks */
  1017. free_alloca(tmp, use_heap);
  1018. return (size_t)-1;
  1019. }
  1020. path[j] = 0;
  1021. if (IS_ABSOLUTE_PATH(path, j)) {
  1022. j = tsrm_realpath_r(path, 1, j, ll, t, use_realpath, is_dir, &directory);
  1023. if (j == (size_t)-1) {
  1024. free_alloca(tmp, use_heap);
  1025. return (size_t)-1;
  1026. }
  1027. } else {
  1028. if (i + j >= MAXPATHLEN-1) {
  1029. free_alloca(tmp, use_heap);
  1030. return (size_t)-1; /* buffer overflow */
  1031. }
  1032. memmove(path+i, path, j+1);
  1033. memcpy(path, tmp, i-1);
  1034. path[i-1] = DEFAULT_SLASH;
  1035. j = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory);
  1036. if (j == (size_t)-1) {
  1037. free_alloca(tmp, use_heap);
  1038. return (size_t)-1;
  1039. }
  1040. }
  1041. if (link_is_dir) {
  1042. *link_is_dir = directory;
  1043. }
  1044. } else {
  1045. if (save) {
  1046. directory = S_ISDIR(st.st_mode);
  1047. if (link_is_dir) {
  1048. *link_is_dir = directory;
  1049. }
  1050. if (is_dir && !directory) {
  1051. /* not a directory */
  1052. free_alloca(tmp, use_heap);
  1053. return (size_t)-1;
  1054. }
  1055. }
  1056. #endif
  1057. if (i <= start + 1) {
  1058. j = start;
  1059. } else {
  1060. /* some leading directories may be unaccessable */
  1061. j = tsrm_realpath_r(path, start, i-1, ll, t, save ? CWD_FILEPATH : use_realpath, 1, NULL);
  1062. if (j > start && j != (size_t)-1) {
  1063. path[j++] = DEFAULT_SLASH;
  1064. }
  1065. }
  1066. #ifdef ZEND_WIN32
  1067. if (j == (size_t)-1 || j + len >= MAXPATHLEN - 1 + i) {
  1068. free_alloca(tmp, use_heap);
  1069. FREE_PATHW()
  1070. return (size_t)-1;
  1071. }
  1072. if (save) {
  1073. size_t sz;
  1074. char *tmp_path = php_win32_ioutil_conv_w_to_any(dataw.cFileName, PHP_WIN32_CP_IGNORE_LEN, &sz);
  1075. if (!tmp_path) {
  1076. free_alloca(tmp, use_heap);
  1077. FREE_PATHW()
  1078. return (size_t)-1;
  1079. }
  1080. i = sz;
  1081. memcpy(path+j, tmp_path, i+1);
  1082. free(tmp_path);
  1083. j += i;
  1084. } else {
  1085. /* use the original file or directory name as it wasn't found */
  1086. memcpy(path+j, tmp+i, len-i+1);
  1087. j += (len-i);
  1088. }
  1089. }
  1090. #else
  1091. if (j == (size_t)-1 || j + len >= MAXPATHLEN - 1 + i) {
  1092. free_alloca(tmp, use_heap);
  1093. return (size_t)-1;
  1094. }
  1095. memcpy(path+j, tmp+i, len-i+1);
  1096. j += (len-i);
  1097. }
  1098. #endif
  1099. if (save && start && CWDG(realpath_cache_size_limit)) {
  1100. /* save absolute path in the cache */
  1101. realpath_cache_add(tmp, len, path, j, directory, *t);
  1102. }
  1103. free_alloca(tmp, use_heap);
  1104. #ifdef ZEND_WIN32
  1105. FREE_PATHW()
  1106. #undef FREE_PATHW
  1107. #endif
  1108. return j;
  1109. }
  1110. }
  1111. /* }}} */
  1112. /* Resolve path relatively to state and put the real path into state */
  1113. /* returns 0 for ok, 1 for error */
  1114. CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath) /* {{{ */
  1115. {
  1116. size_t path_length = strlen(path);
  1117. char resolved_path[MAXPATHLEN] = {0};
  1118. size_t start = 1;
  1119. int ll = 0;
  1120. time_t t;
  1121. int ret;
  1122. int add_slash;
  1123. void *tmp;
  1124. if (!path_length || path_length >= MAXPATHLEN-1) {
  1125. #ifdef ZEND_WIN32
  1126. _set_errno(EINVAL);
  1127. #else
  1128. errno = EINVAL;
  1129. #endif
  1130. return 1;
  1131. }
  1132. #if VIRTUAL_CWD_DEBUG
  1133. fprintf(stderr,"cwd = %s path = %s\n", state->cwd, path);
  1134. #endif
  1135. /* cwd_length can be 0 when getcwd() fails.
  1136. * This can happen under solaris when a dir does not have read permissions
  1137. * but *does* have execute permissions */
  1138. if (!IS_ABSOLUTE_PATH(path, path_length)) {
  1139. if (state->cwd_length == 0) {
  1140. /* resolve relative path */
  1141. start = 0;
  1142. memcpy(resolved_path , path, path_length + 1);
  1143. } else {
  1144. size_t state_cwd_length = state->cwd_length;
  1145. #ifdef ZEND_WIN32
  1146. if (IS_SLASH(path[0])) {
  1147. if (state->cwd[1] == ':') {
  1148. /* Copy only the drive name */
  1149. state_cwd_length = 2;
  1150. } else if (IS_UNC_PATH(state->cwd, state->cwd_length)) {
  1151. /* Copy only the share name */
  1152. state_cwd_length = 2;
  1153. while (IS_SLASH(state->cwd[state_cwd_length])) {
  1154. state_cwd_length++;
  1155. }
  1156. while (state->cwd[state_cwd_length] &&
  1157. !IS_SLASH(state->cwd[state_cwd_length])) {
  1158. state_cwd_length++;
  1159. }
  1160. while (IS_SLASH(state->cwd[state_cwd_length])) {
  1161. state_cwd_length++;
  1162. }
  1163. while (state->cwd[state_cwd_length] &&
  1164. !IS_SLASH(state->cwd[state_cwd_length])) {
  1165. state_cwd_length++;
  1166. }
  1167. }
  1168. }
  1169. #endif
  1170. if (path_length + state_cwd_length + 1 >= MAXPATHLEN-1) {
  1171. return 1;
  1172. }
  1173. memcpy(resolved_path, state->cwd, state_cwd_length);
  1174. if (resolved_path[state_cwd_length-1] == DEFAULT_SLASH) {
  1175. memcpy(resolved_path + state_cwd_length, path, path_length + 1);
  1176. path_length += state_cwd_length;
  1177. } else {
  1178. resolved_path[state_cwd_length] = DEFAULT_SLASH;
  1179. memcpy(resolved_path + state_cwd_length + 1, path, path_length + 1);
  1180. path_length += state_cwd_length + 1;
  1181. }
  1182. }
  1183. } else {
  1184. #ifdef ZEND_WIN32
  1185. if (path_length > 2 && path[1] == ':' && !IS_SLASH(path[2])) {
  1186. resolved_path[0] = path[0];
  1187. resolved_path[1] = ':';
  1188. resolved_path[2] = DEFAULT_SLASH;
  1189. memcpy(resolved_path + 3, path + 2, path_length - 1);
  1190. path_length++;
  1191. } else
  1192. #endif
  1193. memcpy(resolved_path, path, path_length + 1);
  1194. }
  1195. #ifdef ZEND_WIN32
  1196. if (memchr(resolved_path, '*', path_length) ||
  1197. memchr(resolved_path, '?', path_length)) {
  1198. return 1;
  1199. }
  1200. #endif
  1201. #ifdef ZEND_WIN32
  1202. if (IS_UNC_PATH(resolved_path, path_length)) {
  1203. /* skip UNC name */
  1204. resolved_path[0] = DEFAULT_SLASH;
  1205. resolved_path[1] = DEFAULT_SLASH;
  1206. start = 2;
  1207. while (!IS_SLASH(resolved_path[start])) {
  1208. if (resolved_path[start] == 0) {
  1209. goto verify;
  1210. }
  1211. resolved_path[start] = toupper(resolved_path[start]);
  1212. start++;
  1213. }
  1214. resolved_path[start++] = DEFAULT_SLASH;
  1215. while (!IS_SLASH(resolved_path[start])) {
  1216. if (resolved_path[start] == 0) {
  1217. goto verify;
  1218. }
  1219. resolved_path[start] = toupper(resolved_path[start]);
  1220. start++;
  1221. }
  1222. resolved_path[start++] = DEFAULT_SLASH;
  1223. } else if (IS_ABSOLUTE_PATH(resolved_path, path_length)) {
  1224. /* skip DRIVE name */
  1225. resolved_path[0] = toupper(resolved_path[0]);
  1226. resolved_path[2] = DEFAULT_SLASH;
  1227. start = 3;
  1228. }
  1229. #endif
  1230. add_slash = (use_realpath != CWD_REALPATH) && path_length > 0 && IS_SLASH(resolved_path[path_length-1]);
  1231. t = CWDG(realpath_cache_ttl) ? 0 : -1;
  1232. path_length = tsrm_realpath_r(resolved_path, start, path_length, &ll, &t, use_realpath, 0, NULL);
  1233. if (path_length == (size_t)-1) {
  1234. errno = ENOENT;
  1235. return 1;
  1236. }
  1237. if (!start && !path_length) {
  1238. resolved_path[path_length++] = '.';
  1239. }
  1240. if (add_slash && path_length && !IS_SLASH(resolved_path[path_length-1])) {
  1241. if (path_length >= MAXPATHLEN-1) {
  1242. return -1;
  1243. }
  1244. resolved_path[path_length++] = DEFAULT_SLASH;
  1245. }
  1246. resolved_path[path_length] = 0;
  1247. #ifdef ZEND_WIN32
  1248. verify:
  1249. #endif
  1250. if (verify_path) {
  1251. cwd_state old_state;
  1252. CWD_STATE_COPY(&old_state, state);
  1253. state->cwd_length = path_length;
  1254. tmp = erealloc(state->cwd, state->cwd_length+1);
  1255. state->cwd = (char *) tmp;
  1256. memcpy(state->cwd, resolved_path, state->cwd_length+1);
  1257. if (verify_path(state)) {
  1258. CWD_STATE_FREE(state);
  1259. *state = old_state;
  1260. ret = 1;
  1261. } else {
  1262. CWD_STATE_FREE(&old_state);
  1263. ret = 0;
  1264. }
  1265. } else {
  1266. state->cwd_length = path_length;
  1267. tmp = erealloc(state->cwd, state->cwd_length+1);
  1268. state->cwd = (char *) tmp;
  1269. memcpy(state->cwd, resolved_path, state->cwd_length+1);
  1270. ret = 0;
  1271. }
  1272. #if VIRTUAL_CWD_DEBUG
  1273. fprintf (stderr, "virtual_file_ex() = %s\n",state->cwd);
  1274. #endif
  1275. return (ret);
  1276. }
  1277. /* }}} */
  1278. CWD_API int virtual_chdir(const char *path) /* {{{ */
  1279. {
  1280. return virtual_file_ex(&CWDG(cwd), path, php_is_dir_ok, CWD_REALPATH)?-1:0;
  1281. }
  1282. /* }}} */
  1283. CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path)) /* {{{ */
  1284. {
  1285. size_t length = strlen(path);
  1286. char *temp;
  1287. int retval;
  1288. ALLOCA_FLAG(use_heap)
  1289. if (length == 0) {
  1290. return 1; /* Can't cd to empty string */
  1291. }
  1292. while(--length < SIZE_MAX && !IS_SLASH(path[length])) {
  1293. }
  1294. if (length == SIZE_MAX) {
  1295. /* No directory only file name */
  1296. errno = ENOENT;
  1297. return -1;
  1298. }
  1299. if (length == COPY_WHEN_ABSOLUTE(path) && IS_ABSOLUTE_PATH(path, length+1)) { /* Also use trailing slash if this is absolute */
  1300. length++;
  1301. }
  1302. temp = (char *) do_alloca(length+1, use_heap);
  1303. memcpy(temp, path, length);
  1304. temp[length] = 0;
  1305. #if VIRTUAL_CWD_DEBUG
  1306. fprintf (stderr, "Changing directory to %s\n", temp);
  1307. #endif
  1308. retval = p_chdir(temp);
  1309. free_alloca(temp, use_heap);
  1310. return retval;
  1311. }
  1312. /* }}} */
  1313. CWD_API char *virtual_realpath(const char *path, char *real_path) /* {{{ */
  1314. {
  1315. cwd_state new_state;
  1316. char *retval;
  1317. char cwd[MAXPATHLEN];
  1318. /* realpath("") returns CWD */
  1319. if (!*path) {
  1320. new_state.cwd = (char*)emalloc(1);
  1321. new_state.cwd[0] = '\0';
  1322. new_state.cwd_length = 0;
  1323. if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
  1324. path = cwd;
  1325. }
  1326. } else if (!IS_ABSOLUTE_PATH(path, strlen(path))) {
  1327. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1328. } else {
  1329. new_state.cwd = (char*)emalloc(1);
  1330. new_state.cwd[0] = '\0';
  1331. new_state.cwd_length = 0;
  1332. }
  1333. if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)==0) {
  1334. size_t len = new_state.cwd_length>MAXPATHLEN-1?MAXPATHLEN-1:new_state.cwd_length;
  1335. memcpy(real_path, new_state.cwd, len);
  1336. real_path[len] = '\0';
  1337. retval = real_path;
  1338. } else {
  1339. retval = NULL;
  1340. }
  1341. CWD_STATE_FREE(&new_state);
  1342. return retval;
  1343. }
  1344. /* }}} */
  1345. CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path) /* {{{ */
  1346. {
  1347. cwd_state new_state;
  1348. int retval;
  1349. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1350. retval = virtual_file_ex(&new_state, path, verify_path, CWD_FILEPATH);
  1351. *filepath = new_state.cwd;
  1352. return retval;
  1353. }
  1354. /* }}} */
  1355. CWD_API int virtual_filepath(const char *path, char **filepath) /* {{{ */
  1356. {
  1357. return virtual_filepath_ex(path, filepath, php_is_file_ok);
  1358. }
  1359. /* }}} */
  1360. CWD_API FILE *virtual_fopen(const char *path, const char *mode) /* {{{ */
  1361. {
  1362. cwd_state new_state;
  1363. FILE *f;
  1364. if (path[0] == '\0') { /* Fail to open empty path */
  1365. return NULL;
  1366. }
  1367. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1368. if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
  1369. CWD_STATE_FREE_ERR(&new_state);
  1370. return NULL;
  1371. }
  1372. #ifdef ZEND_WIN32
  1373. f = php_win32_ioutil_fopen(new_state.cwd, mode);
  1374. #else
  1375. f = fopen(new_state.cwd, mode);
  1376. #endif
  1377. CWD_STATE_FREE_ERR(&new_state);
  1378. return f;
  1379. }
  1380. /* }}} */
  1381. CWD_API int virtual_access(const char *pathname, int mode) /* {{{ */
  1382. {
  1383. cwd_state new_state;
  1384. int ret;
  1385. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1386. if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
  1387. CWD_STATE_FREE_ERR(&new_state);
  1388. return -1;
  1389. }
  1390. #if defined(ZEND_WIN32)
  1391. ret = tsrm_win32_access(new_state.cwd, mode);
  1392. #else
  1393. ret = access(new_state.cwd, mode);
  1394. #endif
  1395. CWD_STATE_FREE_ERR(&new_state);
  1396. return ret;
  1397. }
  1398. /* }}} */
  1399. #if HAVE_UTIME
  1400. CWD_API int virtual_utime(const char *filename, struct utimbuf *buf) /* {{{ */
  1401. {
  1402. cwd_state new_state;
  1403. int ret;
  1404. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1405. if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
  1406. CWD_STATE_FREE_ERR(&new_state);
  1407. return -1;
  1408. }
  1409. #ifdef ZEND_WIN32
  1410. ret = win32_utime(new_state.cwd, buf);
  1411. #else
  1412. ret = utime(new_state.cwd, buf);
  1413. #endif
  1414. CWD_STATE_FREE_ERR(&new_state);
  1415. return ret;
  1416. }
  1417. /* }}} */
  1418. #endif
  1419. CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */
  1420. {
  1421. cwd_state new_state;
  1422. int ret;
  1423. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1424. if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
  1425. CWD_STATE_FREE_ERR(&new_state);
  1426. return -1;
  1427. }
  1428. #ifdef ZEND_WIN32
  1429. {
  1430. mode_t _tmp = mode;
  1431. mode = 0;
  1432. if (_tmp & _S_IREAD) {
  1433. mode |= _S_IREAD;
  1434. }
  1435. if (_tmp & _S_IWRITE) {
  1436. mode |= _S_IWRITE;
  1437. }
  1438. ret = php_win32_ioutil_chmod(new_state.cwd, mode);
  1439. }
  1440. #else
  1441. ret = chmod(new_state.cwd, mode);
  1442. #endif
  1443. CWD_STATE_FREE_ERR(&new_state);
  1444. return ret;
  1445. }
  1446. /* }}} */
  1447. #if !defined(ZEND_WIN32)
  1448. CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link) /* {{{ */
  1449. {
  1450. cwd_state new_state;
  1451. int ret;
  1452. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1453. if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
  1454. CWD_STATE_FREE_ERR(&new_state);
  1455. return -1;
  1456. }
  1457. if (link) {
  1458. #if HAVE_LCHOWN
  1459. ret = lchown(new_state.cwd, owner, group);
  1460. #else
  1461. ret = -1;
  1462. #endif
  1463. } else {
  1464. ret = chown(new_state.cwd, owner, group);
  1465. }
  1466. CWD_STATE_FREE_ERR(&new_state);
  1467. return ret;
  1468. }
  1469. /* }}} */
  1470. #endif
  1471. CWD_API int virtual_open(const char *path, int flags, ...) /* {{{ */
  1472. {
  1473. cwd_state new_state;
  1474. int f;
  1475. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1476. if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
  1477. CWD_STATE_FREE_ERR(&new_state);
  1478. return -1;
  1479. }
  1480. if (flags & O_CREAT) {
  1481. mode_t mode;
  1482. va_list arg;
  1483. va_start(arg, flags);
  1484. mode = (mode_t) va_arg(arg, int);
  1485. va_end(arg);
  1486. #ifdef ZEND_WIN32
  1487. f = php_win32_ioutil_open(new_state.cwd, flags, mode);
  1488. #else
  1489. f = open(new_state.cwd, flags, mode);
  1490. #endif
  1491. } else {
  1492. #ifdef ZEND_WIN32
  1493. f = php_win32_ioutil_open(new_state.cwd, flags);
  1494. #else
  1495. f = open(new_state.cwd, flags);
  1496. #endif
  1497. }
  1498. CWD_STATE_FREE_ERR(&new_state);
  1499. return f;
  1500. }
  1501. /* }}} */
  1502. CWD_API int virtual_creat(const char *path, mode_t mode) /* {{{ */
  1503. {
  1504. cwd_state new_state;
  1505. int f;
  1506. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1507. if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
  1508. CWD_STATE_FREE_ERR(&new_state);
  1509. return -1;
  1510. }
  1511. f = creat(new_state.cwd, mode);
  1512. CWD_STATE_FREE_ERR(&new_state);
  1513. return f;
  1514. }
  1515. /* }}} */
  1516. CWD_API int virtual_rename(const char *oldname, const char *newname) /* {{{ */
  1517. {
  1518. cwd_state old_state;
  1519. cwd_state new_state;
  1520. int retval;
  1521. CWD_STATE_COPY(&old_state, &CWDG(cwd));
  1522. if (virtual_file_ex(&old_state, oldname, NULL, CWD_EXPAND)) {
  1523. CWD_STATE_FREE_ERR(&old_state);
  1524. return -1;
  1525. }
  1526. oldname = old_state.cwd;
  1527. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1528. if (virtual_file_ex(&new_state, newname, NULL, CWD_EXPAND)) {
  1529. CWD_STATE_FREE_ERR(&old_state);
  1530. CWD_STATE_FREE_ERR(&new_state);
  1531. return -1;
  1532. }
  1533. newname = new_state.cwd;
  1534. /* rename on windows will fail if newname already exists.
  1535. MoveFileEx has to be used */
  1536. #ifdef ZEND_WIN32
  1537. /* MoveFileEx returns 0 on failure, other way 'round for this function */
  1538. retval = php_win32_ioutil_rename(oldname, newname);
  1539. #else
  1540. retval = rename(oldname, newname);
  1541. #endif
  1542. CWD_STATE_FREE_ERR(&old_state);
  1543. CWD_STATE_FREE_ERR(&new_state);
  1544. return retval;
  1545. }
  1546. /* }}} */
  1547. CWD_API int virtual_stat(const char *path, zend_stat_t *buf) /* {{{ */
  1548. {
  1549. cwd_state new_state;
  1550. int retval;
  1551. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1552. if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
  1553. CWD_STATE_FREE_ERR(&new_state);
  1554. return -1;
  1555. }
  1556. retval = php_sys_stat(new_state.cwd, buf);
  1557. CWD_STATE_FREE_ERR(&new_state);
  1558. return retval;
  1559. }
  1560. /* }}} */
  1561. CWD_API int virtual_lstat(const char *path, zend_stat_t *buf) /* {{{ */
  1562. {
  1563. cwd_state new_state;
  1564. int retval;
  1565. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1566. if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
  1567. CWD_STATE_FREE_ERR(&new_state);
  1568. return -1;
  1569. }
  1570. retval = php_sys_lstat(new_state.cwd, buf);
  1571. CWD_STATE_FREE_ERR(&new_state);
  1572. return retval;
  1573. }
  1574. /* }}} */
  1575. CWD_API int virtual_unlink(const char *path) /* {{{ */
  1576. {
  1577. cwd_state new_state;
  1578. int retval;
  1579. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1580. if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
  1581. CWD_STATE_FREE_ERR(&new_state);
  1582. return -1;
  1583. }
  1584. #ifdef ZEND_WIN32
  1585. retval = php_win32_ioutil_unlink(new_state.cwd);
  1586. #else
  1587. retval = unlink(new_state.cwd);
  1588. #endif
  1589. CWD_STATE_FREE_ERR(&new_state);
  1590. return retval;
  1591. }
  1592. /* }}} */
  1593. CWD_API int virtual_mkdir(const char *pathname, mode_t mode) /* {{{ */
  1594. {
  1595. cwd_state new_state;
  1596. int retval;
  1597. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1598. if (virtual_file_ex(&new_state, pathname, NULL, CWD_FILEPATH)) {
  1599. CWD_STATE_FREE_ERR(&new_state);
  1600. return -1;
  1601. }
  1602. #ifdef ZEND_WIN32
  1603. retval = php_win32_ioutil_mkdir(new_state.cwd, mode);
  1604. #else
  1605. retval = mkdir(new_state.cwd, mode);
  1606. #endif
  1607. CWD_STATE_FREE_ERR(&new_state);
  1608. return retval;
  1609. }
  1610. /* }}} */
  1611. CWD_API int virtual_rmdir(const char *pathname) /* {{{ */
  1612. {
  1613. cwd_state new_state;
  1614. int retval;
  1615. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1616. if (virtual_file_ex(&new_state, pathname, NULL, CWD_EXPAND)) {
  1617. CWD_STATE_FREE_ERR(&new_state);
  1618. return -1;
  1619. }
  1620. #ifdef ZEND_WIN32
  1621. retval = php_win32_ioutil_rmdir(new_state.cwd);
  1622. #else
  1623. retval = rmdir(new_state.cwd);
  1624. #endif
  1625. CWD_STATE_FREE_ERR(&new_state);
  1626. return retval;
  1627. }
  1628. /* }}} */
  1629. #ifdef ZEND_WIN32
  1630. DIR *opendir(const char *name);
  1631. #endif
  1632. CWD_API DIR *virtual_opendir(const char *pathname) /* {{{ */
  1633. {
  1634. cwd_state new_state;
  1635. DIR *retval;
  1636. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1637. if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
  1638. CWD_STATE_FREE_ERR(&new_state);
  1639. return NULL;
  1640. }
  1641. retval = opendir(new_state.cwd);
  1642. CWD_STATE_FREE_ERR(&new_state);
  1643. return retval;
  1644. }
  1645. /* }}} */
  1646. #ifdef ZEND_WIN32
  1647. CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
  1648. {
  1649. return popen_ex(command, type, CWDG(cwd).cwd, NULL);
  1650. }
  1651. /* }}} */
  1652. #else /* Unix */
  1653. CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
  1654. {
  1655. size_t command_length;
  1656. int dir_length, extra = 0;
  1657. char *command_line;
  1658. char *ptr, *dir;
  1659. FILE *retval;
  1660. command_length = strlen(command);
  1661. dir_length = CWDG(cwd).cwd_length;
  1662. dir = CWDG(cwd).cwd;
  1663. while (dir_length > 0) {
  1664. if (*dir == '\'') extra+=3;
  1665. dir++;
  1666. dir_length--;
  1667. }
  1668. dir_length = CWDG(cwd).cwd_length;
  1669. dir = CWDG(cwd).cwd;
  1670. ptr = command_line = (char *) emalloc(command_length + sizeof("cd '' ; ") + dir_length + extra+1+1);
  1671. memcpy(ptr, "cd ", sizeof("cd ")-1);
  1672. ptr += sizeof("cd ")-1;
  1673. if (CWDG(cwd).cwd_length == 0) {
  1674. *ptr++ = DEFAULT_SLASH;
  1675. } else {
  1676. *ptr++ = '\'';
  1677. while (dir_length > 0) {
  1678. switch (*dir) {
  1679. case '\'':
  1680. *ptr++ = '\'';
  1681. *ptr++ = '\\';
  1682. *ptr++ = '\'';
  1683. /* fall-through */
  1684. default:
  1685. *ptr++ = *dir;
  1686. }
  1687. dir++;
  1688. dir_length--;
  1689. }
  1690. *ptr++ = '\'';
  1691. }
  1692. *ptr++ = ' ';
  1693. *ptr++ = ';';
  1694. *ptr++ = ' ';
  1695. memcpy(ptr, command, command_length+1);
  1696. retval = popen(command_line, type);
  1697. efree(command_line);
  1698. return retval;
  1699. }
  1700. /* }}} */
  1701. #endif
  1702. CWD_API char *tsrm_realpath(const char *path, char *real_path) /* {{{ */
  1703. {
  1704. cwd_state new_state;
  1705. char cwd[MAXPATHLEN];
  1706. /* realpath("") returns CWD */
  1707. if (!*path) {
  1708. new_state.cwd = (char*)emalloc(1);
  1709. new_state.cwd[0] = '\0';
  1710. new_state.cwd_length = 0;
  1711. if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
  1712. path = cwd;
  1713. }
  1714. } else if (!IS_ABSOLUTE_PATH(path, strlen(path)) &&
  1715. VCWD_GETCWD(cwd, MAXPATHLEN)) {
  1716. new_state.cwd = estrdup(cwd);
  1717. new_state.cwd_length = strlen(cwd);
  1718. } else {
  1719. new_state.cwd = (char*)emalloc(1);
  1720. new_state.cwd[0] = '\0';
  1721. new_state.cwd_length = 0;
  1722. }
  1723. if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
  1724. efree(new_state.cwd);
  1725. return NULL;
  1726. }
  1727. if (real_path) {
  1728. size_t copy_len = new_state.cwd_length>MAXPATHLEN-1 ? MAXPATHLEN-1 : new_state.cwd_length;
  1729. memcpy(real_path, new_state.cwd, copy_len);
  1730. real_path[copy_len] = '\0';
  1731. efree(new_state.cwd);
  1732. return real_path;
  1733. } else {
  1734. return new_state.cwd;
  1735. }
  1736. }
  1737. /* }}} */
  1738. /*
  1739. * Local variables:
  1740. * tab-width: 4
  1741. * c-basic-offset: 4
  1742. * End:
  1743. */