plain_wrapper.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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: Wez Furlong <wez@thebrainroom.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include "php_globals.h"
  20. #include "php_network.h"
  21. #include "php_open_temporary_file.h"
  22. #include "ext/standard/file.h"
  23. #include "ext/standard/flock_compat.h"
  24. #include "ext/standard/php_filestat.h"
  25. #include <stddef.h>
  26. #include <fcntl.h>
  27. #if HAVE_SYS_WAIT_H
  28. #include <sys/wait.h>
  29. #endif
  30. #if HAVE_SYS_FILE_H
  31. #include <sys/file.h>
  32. #endif
  33. #ifdef HAVE_SYS_MMAN_H
  34. #include <sys/mman.h>
  35. #endif
  36. #include "SAPI.h"
  37. #include "php_streams_int.h"
  38. #ifdef PHP_WIN32
  39. # include "win32/winutil.h"
  40. # include "win32/time.h"
  41. # include "win32/ioutil.h"
  42. # include "win32/readdir.h"
  43. #endif
  44. #define php_stream_fopen_from_fd_int(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_CC)
  45. #define php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_REL_CC)
  46. #define php_stream_fopen_from_file_int(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_CC)
  47. #define php_stream_fopen_from_file_int_rel(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_REL_CC)
  48. #ifndef PHP_WIN32
  49. extern int php_get_uid_by_name(const char *name, uid_t *uid);
  50. extern int php_get_gid_by_name(const char *name, gid_t *gid);
  51. #endif
  52. #if defined(PHP_WIN32)
  53. # define PLAIN_WRAP_BUF_SIZE(st) (((st) > UINT_MAX) ? UINT_MAX : (unsigned int)(st))
  54. #else
  55. # define PLAIN_WRAP_BUF_SIZE(st) (st)
  56. #endif
  57. /* parse standard "fopen" modes into open() flags */
  58. PHPAPI int php_stream_parse_fopen_modes(const char *mode, int *open_flags)
  59. {
  60. int flags;
  61. switch (mode[0]) {
  62. case 'r':
  63. flags = 0;
  64. break;
  65. case 'w':
  66. flags = O_TRUNC|O_CREAT;
  67. break;
  68. case 'a':
  69. flags = O_CREAT|O_APPEND;
  70. break;
  71. case 'x':
  72. flags = O_CREAT|O_EXCL;
  73. break;
  74. case 'c':
  75. flags = O_CREAT;
  76. break;
  77. default:
  78. /* unknown mode */
  79. return FAILURE;
  80. }
  81. if (strchr(mode, '+')) {
  82. flags |= O_RDWR;
  83. } else if (flags) {
  84. flags |= O_WRONLY;
  85. } else {
  86. flags |= O_RDONLY;
  87. }
  88. #if defined(O_CLOEXEC)
  89. if (strchr(mode, 'e')) {
  90. flags |= O_CLOEXEC;
  91. }
  92. #endif
  93. #if defined(O_NONBLOCK)
  94. if (strchr(mode, 'n')) {
  95. flags |= O_NONBLOCK;
  96. }
  97. #endif
  98. #if defined(_O_TEXT) && defined(O_BINARY)
  99. if (strchr(mode, 't')) {
  100. flags |= _O_TEXT;
  101. } else {
  102. flags |= O_BINARY;
  103. }
  104. #endif
  105. *open_flags = flags;
  106. return SUCCESS;
  107. }
  108. /* {{{ ------- STDIO stream implementation -------*/
  109. typedef struct {
  110. FILE *file;
  111. int fd; /* underlying file descriptor */
  112. unsigned is_process_pipe:1; /* use pclose instead of fclose */
  113. unsigned is_pipe:1; /* don't try and seek */
  114. unsigned cached_fstat:1; /* sb is valid */
  115. unsigned is_pipe_blocking:1; /* allow blocking read() on pipes, currently Windows only */
  116. unsigned _reserved:28;
  117. int lock_flag; /* stores the lock state */
  118. zend_string *temp_name; /* if non-null, this is the path to a temporary file that
  119. * is to be deleted when the stream is closed */
  120. #if HAVE_FLUSHIO
  121. char last_op;
  122. #endif
  123. #if HAVE_MMAP
  124. char *last_mapped_addr;
  125. size_t last_mapped_len;
  126. #endif
  127. #ifdef PHP_WIN32
  128. char *last_mapped_addr;
  129. HANDLE file_mapping;
  130. #endif
  131. zend_stat_t sb;
  132. } php_stdio_stream_data;
  133. #define PHP_STDIOP_GET_FD(anfd, data) anfd = (data)->file ? fileno((data)->file) : (data)->fd
  134. static int do_fstat(php_stdio_stream_data *d, int force)
  135. {
  136. if (!d->cached_fstat || force) {
  137. int fd;
  138. int r;
  139. PHP_STDIOP_GET_FD(fd, d);
  140. r = zend_fstat(fd, &d->sb);
  141. d->cached_fstat = r == 0;
  142. return r;
  143. }
  144. return 0;
  145. }
  146. static php_stream *_php_stream_fopen_from_fd_int(int fd, const char *mode, const char *persistent_id STREAMS_DC)
  147. {
  148. php_stdio_stream_data *self;
  149. self = pemalloc_rel_orig(sizeof(*self), persistent_id);
  150. memset(self, 0, sizeof(*self));
  151. self->file = NULL;
  152. self->is_pipe = 0;
  153. self->lock_flag = LOCK_UN;
  154. self->is_process_pipe = 0;
  155. self->temp_name = NULL;
  156. self->fd = fd;
  157. #ifdef PHP_WIN32
  158. self->is_pipe_blocking = 0;
  159. #endif
  160. return php_stream_alloc_rel(&php_stream_stdio_ops, self, persistent_id, mode);
  161. }
  162. static php_stream *_php_stream_fopen_from_file_int(FILE *file, const char *mode STREAMS_DC)
  163. {
  164. php_stdio_stream_data *self;
  165. self = emalloc_rel_orig(sizeof(*self));
  166. memset(self, 0, sizeof(*self));
  167. self->file = file;
  168. self->is_pipe = 0;
  169. self->lock_flag = LOCK_UN;
  170. self->is_process_pipe = 0;
  171. self->temp_name = NULL;
  172. self->fd = fileno(file);
  173. #ifdef PHP_WIN32
  174. self->is_pipe_blocking = 0;
  175. #endif
  176. return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
  177. }
  178. PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_ptr STREAMS_DC)
  179. {
  180. zend_string *opened_path = NULL;
  181. int fd;
  182. fd = php_open_temporary_fd(dir, pfx, &opened_path);
  183. if (fd != -1) {
  184. php_stream *stream;
  185. if (opened_path_ptr) {
  186. *opened_path_ptr = opened_path;
  187. }
  188. stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL);
  189. if (stream) {
  190. php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
  191. stream->wrapper = (php_stream_wrapper*)&php_plain_files_wrapper;
  192. stream->orig_path = estrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path));
  193. self->temp_name = opened_path;
  194. self->lock_flag = LOCK_UN;
  195. return stream;
  196. }
  197. close(fd);
  198. php_error_docref(NULL, E_WARNING, "unable to allocate stream");
  199. return NULL;
  200. }
  201. return NULL;
  202. }
  203. PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC)
  204. {
  205. return php_stream_fopen_temporary_file(NULL, "php", NULL);
  206. }
  207. PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const char *persistent_id STREAMS_DC)
  208. {
  209. php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);
  210. if (stream) {
  211. php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
  212. #ifdef S_ISFIFO
  213. /* detect if this is a pipe */
  214. if (self->fd >= 0) {
  215. self->is_pipe = (do_fstat(self, 0) == 0 && S_ISFIFO(self->sb.st_mode)) ? 1 : 0;
  216. }
  217. #elif defined(PHP_WIN32)
  218. {
  219. zend_uintptr_t handle = _get_osfhandle(self->fd);
  220. if (handle != (zend_uintptr_t)INVALID_HANDLE_VALUE) {
  221. self->is_pipe = GetFileType((HANDLE)handle) == FILE_TYPE_PIPE;
  222. }
  223. }
  224. #endif
  225. if (self->is_pipe) {
  226. stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
  227. } else {
  228. stream->position = zend_lseek(self->fd, 0, SEEK_CUR);
  229. #ifdef ESPIPE
  230. if (stream->position == (zend_off_t)-1 && errno == ESPIPE) {
  231. stream->position = 0;
  232. stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
  233. self->is_pipe = 1;
  234. }
  235. #endif
  236. }
  237. }
  238. return stream;
  239. }
  240. PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC)
  241. {
  242. php_stream *stream = php_stream_fopen_from_file_int_rel(file, mode);
  243. if (stream) {
  244. php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
  245. #ifdef S_ISFIFO
  246. /* detect if this is a pipe */
  247. if (self->fd >= 0) {
  248. self->is_pipe = (do_fstat(self, 0) == 0 && S_ISFIFO(self->sb.st_mode)) ? 1 : 0;
  249. }
  250. #elif defined(PHP_WIN32)
  251. {
  252. zend_uintptr_t handle = _get_osfhandle(self->fd);
  253. if (handle != (zend_uintptr_t)INVALID_HANDLE_VALUE) {
  254. self->is_pipe = GetFileType((HANDLE)handle) == FILE_TYPE_PIPE;
  255. }
  256. }
  257. #endif
  258. if (self->is_pipe) {
  259. stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
  260. } else {
  261. stream->position = zend_ftell(file);
  262. }
  263. }
  264. return stream;
  265. }
  266. PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STREAMS_DC)
  267. {
  268. php_stdio_stream_data *self;
  269. php_stream *stream;
  270. self = emalloc_rel_orig(sizeof(*self));
  271. memset(self, 0, sizeof(*self));
  272. self->file = file;
  273. self->is_pipe = 1;
  274. self->lock_flag = LOCK_UN;
  275. self->is_process_pipe = 1;
  276. self->fd = fileno(file);
  277. self->temp_name = NULL;
  278. #ifdef PHP_WIN32
  279. self->is_pipe_blocking = 0;
  280. #endif
  281. stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
  282. stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
  283. return stream;
  284. }
  285. static size_t php_stdiop_write(php_stream *stream, const char *buf, size_t count)
  286. {
  287. php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
  288. assert(data != NULL);
  289. if (data->fd >= 0) {
  290. #ifdef PHP_WIN32
  291. int bytes_written;
  292. if (ZEND_SIZE_T_UINT_OVFL(count)) {
  293. count = UINT_MAX;
  294. }
  295. bytes_written = _write(data->fd, buf, (unsigned int)count);
  296. #else
  297. int bytes_written = write(data->fd, buf, count);
  298. #endif
  299. if (bytes_written < 0) return 0;
  300. return (size_t) bytes_written;
  301. } else {
  302. #if HAVE_FLUSHIO
  303. if (!data->is_pipe && data->last_op == 'r') {
  304. zend_fseek(data->file, 0, SEEK_CUR);
  305. }
  306. data->last_op = 'w';
  307. #endif
  308. return fwrite(buf, 1, count, data->file);
  309. }
  310. }
  311. static size_t php_stdiop_read(php_stream *stream, char *buf, size_t count)
  312. {
  313. php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
  314. size_t ret;
  315. assert(data != NULL);
  316. if (data->fd >= 0) {
  317. #ifdef PHP_WIN32
  318. php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
  319. if ((self->is_pipe || self->is_process_pipe) && !self->is_pipe_blocking) {
  320. HANDLE ph = (HANDLE)_get_osfhandle(data->fd);
  321. int retry = 0;
  322. DWORD avail_read = 0;
  323. do {
  324. /* Look ahead to get the available data amount to read. Do the same
  325. as read() does, however not blocking forever. In case it failed,
  326. no data will be read (better than block). */
  327. if (!PeekNamedPipe(ph, NULL, 0, NULL, &avail_read, NULL)) {
  328. break;
  329. }
  330. /* If there's nothing to read, wait in 10ms periods. */
  331. if (0 == avail_read) {
  332. usleep(10);
  333. }
  334. } while (0 == avail_read && retry++ < 3200000);
  335. /* Reduce the required data amount to what is available, otherwise read()
  336. will block.*/
  337. if (avail_read < count) {
  338. count = avail_read;
  339. }
  340. }
  341. #endif
  342. ret = read(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
  343. if (ret == (size_t)-1 && errno == EINTR) {
  344. /* Read was interrupted, retry once,
  345. If read still fails, giveup with feof==0
  346. so script can retry if desired */
  347. ret = read(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
  348. }
  349. stream->eof = (ret == 0 || (ret == (size_t)-1 && errno != EWOULDBLOCK && errno != EINTR && errno != EBADF));
  350. } else {
  351. #if HAVE_FLUSHIO
  352. if (!data->is_pipe && data->last_op == 'w')
  353. zend_fseek(data->file, 0, SEEK_CUR);
  354. data->last_op = 'r';
  355. #endif
  356. ret = fread(buf, 1, count, data->file);
  357. stream->eof = feof(data->file);
  358. }
  359. return ret;
  360. }
  361. static int php_stdiop_close(php_stream *stream, int close_handle)
  362. {
  363. int ret;
  364. php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
  365. assert(data != NULL);
  366. #if HAVE_MMAP
  367. if (data->last_mapped_addr) {
  368. munmap(data->last_mapped_addr, data->last_mapped_len);
  369. data->last_mapped_addr = NULL;
  370. }
  371. #elif defined(PHP_WIN32)
  372. if (data->last_mapped_addr) {
  373. UnmapViewOfFile(data->last_mapped_addr);
  374. data->last_mapped_addr = NULL;
  375. }
  376. if (data->file_mapping) {
  377. CloseHandle(data->file_mapping);
  378. data->file_mapping = NULL;
  379. }
  380. #endif
  381. if (close_handle) {
  382. if (data->file) {
  383. if (data->is_process_pipe) {
  384. errno = 0;
  385. ret = pclose(data->file);
  386. #if HAVE_SYS_WAIT_H
  387. if (WIFEXITED(ret)) {
  388. ret = WEXITSTATUS(ret);
  389. }
  390. #endif
  391. } else {
  392. ret = fclose(data->file);
  393. data->file = NULL;
  394. }
  395. } else if (data->fd != -1) {
  396. ret = close(data->fd);
  397. data->fd = -1;
  398. } else {
  399. return 0; /* everything should be closed already -> success */
  400. }
  401. if (data->temp_name) {
  402. #ifdef PHP_WIN32
  403. php_win32_ioutil_unlink(ZSTR_VAL(data->temp_name));
  404. #else
  405. unlink(ZSTR_VAL(data->temp_name));
  406. #endif
  407. /* temporary streams are never persistent */
  408. zend_string_release_ex(data->temp_name, 0);
  409. data->temp_name = NULL;
  410. }
  411. } else {
  412. ret = 0;
  413. data->file = NULL;
  414. data->fd = -1;
  415. }
  416. pefree(data, stream->is_persistent);
  417. return ret;
  418. }
  419. static int php_stdiop_flush(php_stream *stream)
  420. {
  421. php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
  422. assert(data != NULL);
  423. /*
  424. * stdio buffers data in user land. By calling fflush(3), this
  425. * data is send to the kernel using write(2). fsync'ing is
  426. * something completely different.
  427. */
  428. if (data->file) {
  429. return fflush(data->file);
  430. }
  431. return 0;
  432. }
  433. static int php_stdiop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset)
  434. {
  435. php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
  436. int ret;
  437. assert(data != NULL);
  438. if (data->is_pipe) {
  439. php_error_docref(NULL, E_WARNING, "cannot seek on a pipe");
  440. return -1;
  441. }
  442. if (data->fd >= 0) {
  443. zend_off_t result;
  444. result = zend_lseek(data->fd, offset, whence);
  445. if (result == (zend_off_t)-1)
  446. return -1;
  447. *newoffset = result;
  448. return 0;
  449. } else {
  450. ret = zend_fseek(data->file, offset, whence);
  451. *newoffset = zend_ftell(data->file);
  452. return ret;
  453. }
  454. }
  455. static int php_stdiop_cast(php_stream *stream, int castas, void **ret)
  456. {
  457. php_socket_t fd;
  458. php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
  459. assert(data != NULL);
  460. /* as soon as someone touches the stdio layer, buffering may ensue,
  461. * so we need to stop using the fd directly in that case */
  462. switch (castas) {
  463. case PHP_STREAM_AS_STDIO:
  464. if (ret) {
  465. if (data->file == NULL) {
  466. /* we were opened as a plain file descriptor, so we
  467. * need fdopen now */
  468. char fixed_mode[5];
  469. php_stream_mode_sanitize_fdopen_fopencookie(stream, fixed_mode);
  470. data->file = fdopen(data->fd, fixed_mode);
  471. if (data->file == NULL) {
  472. return FAILURE;
  473. }
  474. }
  475. *(FILE**)ret = data->file;
  476. data->fd = SOCK_ERR;
  477. }
  478. return SUCCESS;
  479. case PHP_STREAM_AS_FD_FOR_SELECT:
  480. PHP_STDIOP_GET_FD(fd, data);
  481. if (SOCK_ERR == fd) {
  482. return FAILURE;
  483. }
  484. if (ret) {
  485. *(php_socket_t *)ret = fd;
  486. }
  487. return SUCCESS;
  488. case PHP_STREAM_AS_FD:
  489. PHP_STDIOP_GET_FD(fd, data);
  490. if (SOCK_ERR == fd) {
  491. return FAILURE;
  492. }
  493. if (data->file) {
  494. fflush(data->file);
  495. }
  496. if (ret) {
  497. *(php_socket_t *)ret = fd;
  498. }
  499. return SUCCESS;
  500. default:
  501. return FAILURE;
  502. }
  503. }
  504. static int php_stdiop_stat(php_stream *stream, php_stream_statbuf *ssb)
  505. {
  506. int ret;
  507. php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
  508. assert(data != NULL);
  509. if((ret = do_fstat(data, 1)) == 0) {
  510. memcpy(&ssb->sb, &data->sb, sizeof(ssb->sb));
  511. }
  512. return ret;
  513. }
  514. static int php_stdiop_set_option(php_stream *stream, int option, int value, void *ptrparam)
  515. {
  516. php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
  517. size_t size;
  518. int fd;
  519. #ifdef O_NONBLOCK
  520. /* FIXME: make this work for win32 */
  521. int flags;
  522. int oldval;
  523. #endif
  524. PHP_STDIOP_GET_FD(fd, data);
  525. switch(option) {
  526. case PHP_STREAM_OPTION_BLOCKING:
  527. if (fd == -1)
  528. return -1;
  529. #ifdef O_NONBLOCK
  530. flags = fcntl(fd, F_GETFL, 0);
  531. oldval = (flags & O_NONBLOCK) ? 0 : 1;
  532. if (value)
  533. flags &= ~O_NONBLOCK;
  534. else
  535. flags |= O_NONBLOCK;
  536. if (-1 == fcntl(fd, F_SETFL, flags))
  537. return -1;
  538. return oldval;
  539. #else
  540. return -1; /* not yet implemented */
  541. #endif
  542. case PHP_STREAM_OPTION_WRITE_BUFFER:
  543. if (data->file == NULL) {
  544. return -1;
  545. }
  546. if (ptrparam)
  547. size = *(size_t *)ptrparam;
  548. else
  549. size = BUFSIZ;
  550. switch(value) {
  551. case PHP_STREAM_BUFFER_NONE:
  552. return setvbuf(data->file, NULL, _IONBF, 0);
  553. case PHP_STREAM_BUFFER_LINE:
  554. return setvbuf(data->file, NULL, _IOLBF, size);
  555. case PHP_STREAM_BUFFER_FULL:
  556. return setvbuf(data->file, NULL, _IOFBF, size);
  557. default:
  558. return -1;
  559. }
  560. break;
  561. case PHP_STREAM_OPTION_LOCKING:
  562. if (fd == -1) {
  563. return -1;
  564. }
  565. if ((zend_uintptr_t) ptrparam == PHP_STREAM_LOCK_SUPPORTED) {
  566. return 0;
  567. }
  568. if (!flock(fd, value)) {
  569. data->lock_flag = value;
  570. return 0;
  571. } else {
  572. return -1;
  573. }
  574. break;
  575. case PHP_STREAM_OPTION_MMAP_API:
  576. #if HAVE_MMAP
  577. {
  578. php_stream_mmap_range *range = (php_stream_mmap_range*)ptrparam;
  579. int prot, flags;
  580. switch (value) {
  581. case PHP_STREAM_MMAP_SUPPORTED:
  582. return fd == -1 ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
  583. case PHP_STREAM_MMAP_MAP_RANGE:
  584. if (do_fstat(data, 1) != 0) {
  585. return PHP_STREAM_OPTION_RETURN_ERR;
  586. }
  587. if (range->offset > data->sb.st_size) {
  588. range->offset = data->sb.st_size;
  589. }
  590. if (range->length == 0 ||
  591. range->length > data->sb.st_size - range->offset) {
  592. range->length = data->sb.st_size - range->offset;
  593. }
  594. switch (range->mode) {
  595. case PHP_STREAM_MAP_MODE_READONLY:
  596. prot = PROT_READ;
  597. flags = MAP_PRIVATE;
  598. break;
  599. case PHP_STREAM_MAP_MODE_READWRITE:
  600. prot = PROT_READ | PROT_WRITE;
  601. flags = MAP_PRIVATE;
  602. break;
  603. case PHP_STREAM_MAP_MODE_SHARED_READONLY:
  604. prot = PROT_READ;
  605. flags = MAP_SHARED;
  606. break;
  607. case PHP_STREAM_MAP_MODE_SHARED_READWRITE:
  608. prot = PROT_READ | PROT_WRITE;
  609. flags = MAP_SHARED;
  610. break;
  611. default:
  612. return PHP_STREAM_OPTION_RETURN_ERR;
  613. }
  614. range->mapped = (char*)mmap(NULL, range->length, prot, flags, fd, range->offset);
  615. if (range->mapped == (char*)MAP_FAILED) {
  616. range->mapped = NULL;
  617. return PHP_STREAM_OPTION_RETURN_ERR;
  618. }
  619. /* remember the mapping */
  620. data->last_mapped_addr = range->mapped;
  621. data->last_mapped_len = range->length;
  622. return PHP_STREAM_OPTION_RETURN_OK;
  623. case PHP_STREAM_MMAP_UNMAP:
  624. if (data->last_mapped_addr) {
  625. munmap(data->last_mapped_addr, data->last_mapped_len);
  626. data->last_mapped_addr = NULL;
  627. return PHP_STREAM_OPTION_RETURN_OK;
  628. }
  629. return PHP_STREAM_OPTION_RETURN_ERR;
  630. }
  631. }
  632. #elif defined(PHP_WIN32)
  633. {
  634. php_stream_mmap_range *range = (php_stream_mmap_range*)ptrparam;
  635. HANDLE hfile = (HANDLE)_get_osfhandle(fd);
  636. DWORD prot, acc, loffs = 0, delta = 0;
  637. LARGE_INTEGER file_size;
  638. switch (value) {
  639. case PHP_STREAM_MMAP_SUPPORTED:
  640. return hfile == INVALID_HANDLE_VALUE ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
  641. case PHP_STREAM_MMAP_MAP_RANGE:
  642. switch (range->mode) {
  643. case PHP_STREAM_MAP_MODE_READONLY:
  644. prot = PAGE_READONLY;
  645. acc = FILE_MAP_READ;
  646. break;
  647. case PHP_STREAM_MAP_MODE_READWRITE:
  648. prot = PAGE_READWRITE;
  649. acc = FILE_MAP_READ | FILE_MAP_WRITE;
  650. break;
  651. case PHP_STREAM_MAP_MODE_SHARED_READONLY:
  652. prot = PAGE_READONLY;
  653. acc = FILE_MAP_READ;
  654. /* TODO: we should assign a name for the mapping */
  655. break;
  656. case PHP_STREAM_MAP_MODE_SHARED_READWRITE:
  657. prot = PAGE_READWRITE;
  658. acc = FILE_MAP_READ | FILE_MAP_WRITE;
  659. /* TODO: we should assign a name for the mapping */
  660. break;
  661. default:
  662. return PHP_STREAM_OPTION_RETURN_ERR;
  663. }
  664. /* create a mapping capable of viewing the whole file (this costs no real resources) */
  665. data->file_mapping = CreateFileMapping(hfile, NULL, prot, 0, 0, NULL);
  666. if (data->file_mapping == NULL) {
  667. return PHP_STREAM_OPTION_RETURN_ERR;
  668. }
  669. if (!GetFileSizeEx(hfile, &file_size)) {
  670. CloseHandle(data->file_mapping);
  671. data->file_mapping = NULL;
  672. return PHP_STREAM_OPTION_RETURN_ERR;
  673. }
  674. # if defined(_WIN64)
  675. size = file_size.QuadPart;
  676. # else
  677. if (file_size.HighPart) {
  678. CloseHandle(data->file_mapping);
  679. data->file_mapping = NULL;
  680. return PHP_STREAM_OPTION_RETURN_ERR;
  681. } else {
  682. size = file_size.LowPart;
  683. }
  684. # endif
  685. if (range->offset > size) {
  686. range->offset = size;
  687. }
  688. if (range->length == 0 || range->length > size - range->offset) {
  689. range->length = size - range->offset;
  690. }
  691. /* figure out how big a chunk to map to be able to view the part that we need */
  692. if (range->offset != 0) {
  693. SYSTEM_INFO info;
  694. DWORD gran;
  695. GetSystemInfo(&info);
  696. gran = info.dwAllocationGranularity;
  697. loffs = ((DWORD)range->offset / gran) * gran;
  698. delta = (DWORD)range->offset - loffs;
  699. }
  700. data->last_mapped_addr = MapViewOfFile(data->file_mapping, acc, 0, loffs, range->length + delta);
  701. if (data->last_mapped_addr) {
  702. /* give them back the address of the start offset they requested */
  703. range->mapped = data->last_mapped_addr + delta;
  704. return PHP_STREAM_OPTION_RETURN_OK;
  705. }
  706. CloseHandle(data->file_mapping);
  707. data->file_mapping = NULL;
  708. return PHP_STREAM_OPTION_RETURN_ERR;
  709. case PHP_STREAM_MMAP_UNMAP:
  710. if (data->last_mapped_addr) {
  711. UnmapViewOfFile(data->last_mapped_addr);
  712. data->last_mapped_addr = NULL;
  713. CloseHandle(data->file_mapping);
  714. data->file_mapping = NULL;
  715. return PHP_STREAM_OPTION_RETURN_OK;
  716. }
  717. return PHP_STREAM_OPTION_RETURN_ERR;
  718. default:
  719. return PHP_STREAM_OPTION_RETURN_ERR;
  720. }
  721. }
  722. #endif
  723. return PHP_STREAM_OPTION_RETURN_NOTIMPL;
  724. case PHP_STREAM_OPTION_TRUNCATE_API:
  725. switch (value) {
  726. case PHP_STREAM_TRUNCATE_SUPPORTED:
  727. return fd == -1 ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
  728. case PHP_STREAM_TRUNCATE_SET_SIZE: {
  729. ptrdiff_t new_size = *(ptrdiff_t*)ptrparam;
  730. if (new_size < 0) {
  731. return PHP_STREAM_OPTION_RETURN_ERR;
  732. }
  733. #ifdef PHP_WIN32
  734. HANDLE h = (HANDLE) _get_osfhandle(fd);
  735. if (INVALID_HANDLE_VALUE == h) {
  736. return PHP_STREAM_OPTION_RETURN_ERR;
  737. }
  738. LARGE_INTEGER sz, old_sz;
  739. sz.QuadPart = 0;
  740. if (!SetFilePointerEx(h, sz, &old_sz, FILE_CURRENT)) {
  741. return PHP_STREAM_OPTION_RETURN_ERR;
  742. }
  743. #if defined(_WIN64)
  744. sz.QuadPart = new_size;
  745. #else
  746. sz.HighPart = 0;
  747. sz.LowPart = new_size;
  748. #endif
  749. if (!SetFilePointerEx(h, sz, NULL, FILE_BEGIN)) {
  750. return PHP_STREAM_OPTION_RETURN_ERR;
  751. }
  752. if (0 == SetEndOfFile(h)) {
  753. return PHP_STREAM_OPTION_RETURN_ERR;
  754. }
  755. if (!SetFilePointerEx(h, old_sz, NULL, FILE_BEGIN)) {
  756. return PHP_STREAM_OPTION_RETURN_ERR;
  757. }
  758. return PHP_STREAM_OPTION_RETURN_OK;
  759. #else
  760. return ftruncate(fd, new_size) == 0 ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
  761. #endif
  762. }
  763. }
  764. #ifdef PHP_WIN32
  765. case PHP_STREAM_OPTION_PIPE_BLOCKING:
  766. data->is_pipe_blocking = value;
  767. return PHP_STREAM_OPTION_RETURN_OK;
  768. #endif
  769. case PHP_STREAM_OPTION_META_DATA_API:
  770. if (fd == -1)
  771. return -1;
  772. #ifdef O_NONBLOCK
  773. flags = fcntl(fd, F_GETFL, 0);
  774. add_assoc_bool((zval*)ptrparam, "timed_out", 0);
  775. add_assoc_bool((zval*)ptrparam, "blocked", (flags & O_NONBLOCK)? 0 : 1);
  776. add_assoc_bool((zval*)ptrparam, "eof", stream->eof);
  777. return PHP_STREAM_OPTION_RETURN_OK;
  778. #endif
  779. return -1;
  780. default:
  781. return PHP_STREAM_OPTION_RETURN_NOTIMPL;
  782. }
  783. }
  784. /* This should be "const", but phpdbg overwrite it */
  785. PHPAPI php_stream_ops php_stream_stdio_ops = {
  786. php_stdiop_write, php_stdiop_read,
  787. php_stdiop_close, php_stdiop_flush,
  788. "STDIO",
  789. php_stdiop_seek,
  790. php_stdiop_cast,
  791. php_stdiop_stat,
  792. php_stdiop_set_option
  793. };
  794. /* }}} */
  795. /* {{{ plain files opendir/readdir implementation */
  796. static size_t php_plain_files_dirstream_read(php_stream *stream, char *buf, size_t count)
  797. {
  798. DIR *dir = (DIR*)stream->abstract;
  799. /* avoid libc5 readdir problems */
  800. char entry[sizeof(struct dirent)+MAXPATHLEN];
  801. struct dirent *result = (struct dirent *)&entry;
  802. php_stream_dirent *ent = (php_stream_dirent*)buf;
  803. /* avoid problems if someone mis-uses the stream */
  804. if (count != sizeof(php_stream_dirent))
  805. return 0;
  806. if (php_readdir_r(dir, (struct dirent *)entry, &result) == 0 && result) {
  807. PHP_STRLCPY(ent->d_name, result->d_name, sizeof(ent->d_name), strlen(result->d_name));
  808. return sizeof(php_stream_dirent);
  809. }
  810. return 0;
  811. }
  812. static int php_plain_files_dirstream_close(php_stream *stream, int close_handle)
  813. {
  814. return closedir((DIR *)stream->abstract);
  815. }
  816. static int php_plain_files_dirstream_rewind(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
  817. {
  818. rewinddir((DIR *)stream->abstract);
  819. return 0;
  820. }
  821. static const php_stream_ops php_plain_files_dirstream_ops = {
  822. NULL, php_plain_files_dirstream_read,
  823. php_plain_files_dirstream_close, NULL,
  824. "dir",
  825. php_plain_files_dirstream_rewind,
  826. NULL, /* cast */
  827. NULL, /* stat */
  828. NULL /* set_option */
  829. };
  830. static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, const char *path, const char *mode,
  831. int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
  832. {
  833. DIR *dir = NULL;
  834. php_stream *stream = NULL;
  835. #ifdef HAVE_GLOB
  836. if (options & STREAM_USE_GLOB_DIR_OPEN) {
  837. return php_glob_stream_wrapper.wops->dir_opener((php_stream_wrapper*)&php_glob_stream_wrapper, path, mode, options, opened_path, context STREAMS_REL_CC);
  838. }
  839. #endif
  840. if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path)) {
  841. return NULL;
  842. }
  843. dir = VCWD_OPENDIR(path);
  844. #ifdef PHP_WIN32
  845. if (!dir) {
  846. php_win32_docref2_from_error(GetLastError(), path, path);
  847. }
  848. if (dir && dir->finished) {
  849. closedir(dir);
  850. dir = NULL;
  851. }
  852. #endif
  853. if (dir) {
  854. stream = php_stream_alloc(&php_plain_files_dirstream_ops, dir, 0, mode);
  855. if (stream == NULL)
  856. closedir(dir);
  857. }
  858. return stream;
  859. }
  860. /* }}} */
  861. /* {{{ php_stream_fopen */
  862. PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, zend_string **opened_path, int options STREAMS_DC)
  863. {
  864. char realpath[MAXPATHLEN];
  865. int open_flags;
  866. int fd;
  867. php_stream *ret;
  868. int persistent = options & STREAM_OPEN_PERSISTENT;
  869. char *persistent_id = NULL;
  870. if (FAILURE == php_stream_parse_fopen_modes(mode, &open_flags)) {
  871. php_stream_wrapper_log_error(&php_plain_files_wrapper, options, "`%s' is not a valid mode for fopen", mode);
  872. return NULL;
  873. }
  874. if (options & STREAM_ASSUME_REALPATH) {
  875. strlcpy(realpath, filename, sizeof(realpath));
  876. } else {
  877. if (expand_filepath(filename, realpath) == NULL) {
  878. return NULL;
  879. }
  880. }
  881. if (persistent) {
  882. spprintf(&persistent_id, 0, "streams_stdio_%d_%s", open_flags, realpath);
  883. switch (php_stream_from_persistent_id(persistent_id, &ret)) {
  884. case PHP_STREAM_PERSISTENT_SUCCESS:
  885. if (opened_path) {
  886. //TODO: avoid reallocation???
  887. *opened_path = zend_string_init(realpath, strlen(realpath), 0);
  888. }
  889. /* fall through */
  890. case PHP_STREAM_PERSISTENT_FAILURE:
  891. efree(persistent_id);
  892. return ret;
  893. }
  894. }
  895. #ifdef PHP_WIN32
  896. fd = php_win32_ioutil_open(realpath, open_flags, 0666);
  897. #else
  898. fd = open(realpath, open_flags, 0666);
  899. #endif
  900. if (fd != -1) {
  901. if (options & STREAM_OPEN_FOR_INCLUDE) {
  902. ret = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);
  903. } else {
  904. ret = php_stream_fopen_from_fd_rel(fd, mode, persistent_id);
  905. }
  906. if (ret) {
  907. if (opened_path) {
  908. *opened_path = zend_string_init(realpath, strlen(realpath), 0);
  909. }
  910. if (persistent_id) {
  911. efree(persistent_id);
  912. }
  913. /* WIN32 always set ISREG flag */
  914. #ifndef PHP_WIN32
  915. /* sanity checks for include/require.
  916. * We check these after opening the stream, so that we save
  917. * on fstat() syscalls */
  918. if (options & STREAM_OPEN_FOR_INCLUDE) {
  919. php_stdio_stream_data *self = (php_stdio_stream_data*)ret->abstract;
  920. int r;
  921. r = do_fstat(self, 0);
  922. if ((r == 0 && !S_ISREG(self->sb.st_mode))) {
  923. if (opened_path) {
  924. zend_string_release_ex(*opened_path, 0);
  925. *opened_path = NULL;
  926. }
  927. php_stream_close(ret);
  928. return NULL;
  929. }
  930. }
  931. if (options & STREAM_USE_BLOCKING_PIPE) {
  932. php_stdio_stream_data *self = (php_stdio_stream_data*)ret->abstract;
  933. self->is_pipe_blocking = 1;
  934. }
  935. #endif
  936. return ret;
  937. }
  938. close(fd);
  939. }
  940. if (persistent_id) {
  941. efree(persistent_id);
  942. }
  943. return NULL;
  944. }
  945. /* }}} */
  946. static php_stream *php_plain_files_stream_opener(php_stream_wrapper *wrapper, const char *path, const char *mode,
  947. int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
  948. {
  949. if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path)) {
  950. return NULL;
  951. }
  952. return php_stream_fopen_rel(path, mode, opened_path, options);
  953. }
  954. static int php_plain_files_url_stater(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context)
  955. {
  956. if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
  957. url += sizeof("file://") - 1;
  958. }
  959. if (php_check_open_basedir_ex(url, (flags & PHP_STREAM_URL_STAT_QUIET) ? 0 : 1)) {
  960. return -1;
  961. }
  962. #ifdef PHP_WIN32
  963. if (flags & PHP_STREAM_URL_STAT_LINK) {
  964. return VCWD_LSTAT(url, &ssb->sb);
  965. }
  966. #else
  967. # ifdef HAVE_SYMLINK
  968. if (flags & PHP_STREAM_URL_STAT_LINK) {
  969. return VCWD_LSTAT(url, &ssb->sb);
  970. } else
  971. # endif
  972. #endif
  973. return VCWD_STAT(url, &ssb->sb);
  974. }
  975. static int php_plain_files_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
  976. {
  977. int ret;
  978. if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
  979. url += sizeof("file://") - 1;
  980. }
  981. if (php_check_open_basedir(url)) {
  982. return 0;
  983. }
  984. ret = VCWD_UNLINK(url);
  985. if (ret == -1) {
  986. if (options & REPORT_ERRORS) {
  987. php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
  988. }
  989. return 0;
  990. }
  991. /* Clear stat cache (and realpath cache) */
  992. php_clear_stat_cache(1, NULL, 0);
  993. return 1;
  994. }
  995. static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_from, const char *url_to, int options, php_stream_context *context)
  996. {
  997. int ret;
  998. if (!url_from || !url_to) {
  999. return 0;
  1000. }
  1001. #ifdef PHP_WIN32
  1002. if (!php_win32_check_trailing_space(url_from, strlen(url_from))) {
  1003. php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to);
  1004. return 0;
  1005. }
  1006. if (!php_win32_check_trailing_space(url_to, strlen(url_to))) {
  1007. php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to);
  1008. return 0;
  1009. }
  1010. #endif
  1011. if (strncasecmp(url_from, "file://", sizeof("file://") - 1) == 0) {
  1012. url_from += sizeof("file://") - 1;
  1013. }
  1014. if (strncasecmp(url_to, "file://", sizeof("file://") - 1) == 0) {
  1015. url_to += sizeof("file://") - 1;
  1016. }
  1017. if (php_check_open_basedir(url_from) || php_check_open_basedir(url_to)) {
  1018. return 0;
  1019. }
  1020. ret = VCWD_RENAME(url_from, url_to);
  1021. if (ret == -1) {
  1022. #ifndef PHP_WIN32
  1023. # ifdef EXDEV
  1024. if (errno == EXDEV) {
  1025. zend_stat_t sb;
  1026. # if !defined(ZTS) && !defined(TSRM_WIN32)
  1027. /* not sure what to do in ZTS case, umask is not thread-safe */
  1028. int oldmask = umask(077);
  1029. # endif
  1030. int success = 0;
  1031. if (php_copy_file(url_from, url_to) == SUCCESS) {
  1032. if (VCWD_STAT(url_from, &sb) == 0) {
  1033. success = 1;
  1034. # if !defined(TSRM_WIN32)
  1035. /*
  1036. * Try to set user and permission info on the target.
  1037. * If we're not root, then some of these may fail.
  1038. * We try chown first, to set proper group info, relying
  1039. * on the system environment to have proper umask to not allow
  1040. * access to the file in the meantime.
  1041. */
  1042. if (VCWD_CHOWN(url_to, sb.st_uid, sb.st_gid)) {
  1043. php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
  1044. if (errno != EPERM) {
  1045. success = 0;
  1046. }
  1047. }
  1048. if (success) {
  1049. if (VCWD_CHMOD(url_to, sb.st_mode)) {
  1050. php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
  1051. if (errno != EPERM) {
  1052. success = 0;
  1053. }
  1054. }
  1055. }
  1056. # endif
  1057. if (success) {
  1058. VCWD_UNLINK(url_from);
  1059. }
  1060. } else {
  1061. php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
  1062. }
  1063. } else {
  1064. php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
  1065. }
  1066. # if !defined(ZTS) && !defined(TSRM_WIN32)
  1067. umask(oldmask);
  1068. # endif
  1069. return success;
  1070. }
  1071. # endif
  1072. #endif
  1073. #ifdef PHP_WIN32
  1074. php_win32_docref2_from_error(GetLastError(), url_from, url_to);
  1075. #else
  1076. php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
  1077. #endif
  1078. return 0;
  1079. }
  1080. /* Clear stat cache (and realpath cache) */
  1081. php_clear_stat_cache(1, NULL, 0);
  1082. return 1;
  1083. }
  1084. static int php_plain_files_mkdir(php_stream_wrapper *wrapper, const char *dir, int mode, int options, php_stream_context *context)
  1085. {
  1086. int ret, recursive = options & PHP_STREAM_MKDIR_RECURSIVE;
  1087. char *p;
  1088. if (strncasecmp(dir, "file://", sizeof("file://") - 1) == 0) {
  1089. dir += sizeof("file://") - 1;
  1090. }
  1091. if (!recursive) {
  1092. ret = php_mkdir(dir, mode);
  1093. } else {
  1094. /* we look for directory separator from the end of string, thus hopefuly reducing our work load */
  1095. char *e;
  1096. zend_stat_t sb;
  1097. size_t dir_len = strlen(dir), offset = 0;
  1098. char buf[MAXPATHLEN];
  1099. if (!expand_filepath_with_mode(dir, buf, NULL, 0, CWD_EXPAND )) {
  1100. php_error_docref(NULL, E_WARNING, "Invalid path");
  1101. return 0;
  1102. }
  1103. e = buf + strlen(buf);
  1104. if ((p = memchr(buf, DEFAULT_SLASH, dir_len))) {
  1105. offset = p - buf + 1;
  1106. }
  1107. if (p && dir_len == 1) {
  1108. /* buf == "DEFAULT_SLASH" */
  1109. }
  1110. else {
  1111. /* find a top level directory we need to create */
  1112. while ( (p = strrchr(buf + offset, DEFAULT_SLASH)) || (offset != 1 && (p = strrchr(buf, DEFAULT_SLASH))) ) {
  1113. int n = 0;
  1114. *p = '\0';
  1115. while (p > buf && *(p-1) == DEFAULT_SLASH) {
  1116. ++n;
  1117. --p;
  1118. *p = '\0';
  1119. }
  1120. if (VCWD_STAT(buf, &sb) == 0) {
  1121. while (1) {
  1122. *p = DEFAULT_SLASH;
  1123. if (!n) break;
  1124. --n;
  1125. ++p;
  1126. }
  1127. break;
  1128. }
  1129. }
  1130. }
  1131. if (p == buf) {
  1132. ret = php_mkdir(dir, mode);
  1133. } else if (!(ret = php_mkdir(buf, mode))) {
  1134. if (!p) {
  1135. p = buf;
  1136. }
  1137. /* create any needed directories if the creation of the 1st directory worked */
  1138. while (++p != e) {
  1139. if (*p == '\0') {
  1140. *p = DEFAULT_SLASH;
  1141. if ((*(p+1) != '\0') &&
  1142. (ret = VCWD_MKDIR(buf, (mode_t)mode)) < 0) {
  1143. if (options & REPORT_ERRORS) {
  1144. php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  1145. }
  1146. break;
  1147. }
  1148. }
  1149. }
  1150. }
  1151. }
  1152. if (ret < 0) {
  1153. /* Failure */
  1154. return 0;
  1155. } else {
  1156. /* Success */
  1157. return 1;
  1158. }
  1159. }
  1160. static int php_plain_files_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
  1161. {
  1162. if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
  1163. url += sizeof("file://") - 1;
  1164. }
  1165. if (php_check_open_basedir(url)) {
  1166. return 0;
  1167. }
  1168. #ifdef PHP_WIN32
  1169. if (!php_win32_check_trailing_space(url, strlen(url))) {
  1170. php_error_docref1(NULL, url, E_WARNING, "%s", strerror(ENOENT));
  1171. return 0;
  1172. }
  1173. #endif
  1174. if (VCWD_RMDIR(url) < 0) {
  1175. php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
  1176. return 0;
  1177. }
  1178. /* Clear stat cache (and realpath cache) */
  1179. php_clear_stat_cache(1, NULL, 0);
  1180. return 1;
  1181. }
  1182. static int php_plain_files_metadata(php_stream_wrapper *wrapper, const char *url, int option, void *value, php_stream_context *context)
  1183. {
  1184. struct utimbuf *newtime;
  1185. #ifndef PHP_WIN32
  1186. uid_t uid;
  1187. gid_t gid;
  1188. #endif
  1189. mode_t mode;
  1190. int ret = 0;
  1191. #ifdef PHP_WIN32
  1192. if (!php_win32_check_trailing_space(url, strlen(url))) {
  1193. php_error_docref1(NULL, url, E_WARNING, "%s", strerror(ENOENT));
  1194. return 0;
  1195. }
  1196. #endif
  1197. if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
  1198. url += sizeof("file://") - 1;
  1199. }
  1200. if (php_check_open_basedir(url)) {
  1201. return 0;
  1202. }
  1203. switch(option) {
  1204. case PHP_STREAM_META_TOUCH:
  1205. newtime = (struct utimbuf *)value;
  1206. if (VCWD_ACCESS(url, F_OK) != 0) {
  1207. FILE *file = VCWD_FOPEN(url, "w");
  1208. if (file == NULL) {
  1209. php_error_docref1(NULL, url, E_WARNING, "Unable to create file %s because %s", url, strerror(errno));
  1210. return 0;
  1211. }
  1212. fclose(file);
  1213. }
  1214. ret = VCWD_UTIME(url, newtime);
  1215. break;
  1216. #ifndef PHP_WIN32
  1217. case PHP_STREAM_META_OWNER_NAME:
  1218. case PHP_STREAM_META_OWNER:
  1219. if(option == PHP_STREAM_META_OWNER_NAME) {
  1220. if(php_get_uid_by_name((char *)value, &uid) != SUCCESS) {
  1221. php_error_docref1(NULL, url, E_WARNING, "Unable to find uid for %s", (char *)value);
  1222. return 0;
  1223. }
  1224. } else {
  1225. uid = (uid_t)*(long *)value;
  1226. }
  1227. ret = VCWD_CHOWN(url, uid, -1);
  1228. break;
  1229. case PHP_STREAM_META_GROUP:
  1230. case PHP_STREAM_META_GROUP_NAME:
  1231. if(option == PHP_STREAM_META_GROUP_NAME) {
  1232. if(php_get_gid_by_name((char *)value, &gid) != SUCCESS) {
  1233. php_error_docref1(NULL, url, E_WARNING, "Unable to find gid for %s", (char *)value);
  1234. return 0;
  1235. }
  1236. } else {
  1237. gid = (gid_t)*(long *)value;
  1238. }
  1239. ret = VCWD_CHOWN(url, -1, gid);
  1240. break;
  1241. #endif
  1242. case PHP_STREAM_META_ACCESS:
  1243. mode = (mode_t)*(zend_long *)value;
  1244. ret = VCWD_CHMOD(url, mode);
  1245. break;
  1246. default:
  1247. php_error_docref1(NULL, url, E_WARNING, "Unknown option %d for stream_metadata", option);
  1248. return 0;
  1249. }
  1250. if (ret == -1) {
  1251. php_error_docref1(NULL, url, E_WARNING, "Operation failed: %s", strerror(errno));
  1252. return 0;
  1253. }
  1254. php_clear_stat_cache(0, NULL, 0);
  1255. return 1;
  1256. }
  1257. static const php_stream_wrapper_ops php_plain_files_wrapper_ops = {
  1258. php_plain_files_stream_opener,
  1259. NULL,
  1260. NULL,
  1261. php_plain_files_url_stater,
  1262. php_plain_files_dir_opener,
  1263. "plainfile",
  1264. php_plain_files_unlink,
  1265. php_plain_files_rename,
  1266. php_plain_files_mkdir,
  1267. php_plain_files_rmdir,
  1268. php_plain_files_metadata
  1269. };
  1270. /* TODO: We have to make php_plain_files_wrapper writable to support SWOOLE */
  1271. PHPAPI /*const*/ php_stream_wrapper php_plain_files_wrapper = {
  1272. &php_plain_files_wrapper_ops,
  1273. NULL,
  1274. 0
  1275. };
  1276. /* {{{ php_stream_fopen_with_path */
  1277. PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char *mode, const char *path, zend_string **opened_path, int options STREAMS_DC)
  1278. {
  1279. /* code ripped off from fopen_wrappers.c */
  1280. char *pathbuf, *end;
  1281. const char *ptr;
  1282. char trypath[MAXPATHLEN];
  1283. php_stream *stream;
  1284. size_t filename_length;
  1285. zend_string *exec_filename;
  1286. if (opened_path) {
  1287. *opened_path = NULL;
  1288. }
  1289. if(!filename) {
  1290. return NULL;
  1291. }
  1292. filename_length = strlen(filename);
  1293. #ifndef PHP_WIN32
  1294. (void) filename_length;
  1295. #endif
  1296. /* Relative path open */
  1297. if (*filename == '.' && (IS_SLASH(filename[1]) || filename[1] == '.')) {
  1298. /* further checks, we could have ....... filenames */
  1299. ptr = filename + 1;
  1300. if (*ptr == '.') {
  1301. while (*(++ptr) == '.');
  1302. if (!IS_SLASH(*ptr)) { /* not a relative path after all */
  1303. goto not_relative_path;
  1304. }
  1305. }
  1306. if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename)) {
  1307. return NULL;
  1308. }
  1309. return php_stream_fopen_rel(filename, mode, opened_path, options);
  1310. }
  1311. not_relative_path:
  1312. /* Absolute path open */
  1313. if (IS_ABSOLUTE_PATH(filename, filename_length)) {
  1314. if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename)) {
  1315. return NULL;
  1316. }
  1317. return php_stream_fopen_rel(filename, mode, opened_path, options);
  1318. }
  1319. #ifdef PHP_WIN32
  1320. if (IS_SLASH(filename[0])) {
  1321. size_t cwd_len;
  1322. char *cwd;
  1323. cwd = virtual_getcwd_ex(&cwd_len);
  1324. /* getcwd() will return always return [DRIVE_LETTER]:/) on windows. */
  1325. *(cwd+3) = '\0';
  1326. if (snprintf(trypath, MAXPATHLEN, "%s%s", cwd, filename) >= MAXPATHLEN) {
  1327. php_error_docref(NULL, E_NOTICE, "%s/%s path was truncated to %d", cwd, filename, MAXPATHLEN);
  1328. }
  1329. efree(cwd);
  1330. if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(trypath)) {
  1331. return NULL;
  1332. }
  1333. return php_stream_fopen_rel(trypath, mode, opened_path, options);
  1334. }
  1335. #endif
  1336. if (!path || !*path) {
  1337. return php_stream_fopen_rel(filename, mode, opened_path, options);
  1338. }
  1339. /* check in provided path */
  1340. /* append the calling scripts' current working directory
  1341. * as a fall back case
  1342. */
  1343. if (zend_is_executing() &&
  1344. (exec_filename = zend_get_executed_filename_ex()) != NULL) {
  1345. const char *exec_fname = ZSTR_VAL(exec_filename);
  1346. size_t exec_fname_length = ZSTR_LEN(exec_filename);
  1347. while ((--exec_fname_length < SIZE_MAX) && !IS_SLASH(exec_fname[exec_fname_length]));
  1348. if (exec_fname_length<=0) {
  1349. /* no path */
  1350. pathbuf = estrdup(path);
  1351. } else {
  1352. size_t path_length = strlen(path);
  1353. pathbuf = (char *) emalloc(exec_fname_length + path_length +1 +1);
  1354. memcpy(pathbuf, path, path_length);
  1355. pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;
  1356. memcpy(pathbuf+path_length+1, exec_fname, exec_fname_length);
  1357. pathbuf[path_length + exec_fname_length +1] = '\0';
  1358. }
  1359. } else {
  1360. pathbuf = estrdup(path);
  1361. }
  1362. ptr = pathbuf;
  1363. while (ptr && *ptr) {
  1364. end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
  1365. if (end != NULL) {
  1366. *end = '\0';
  1367. end++;
  1368. }
  1369. if (*ptr == '\0') {
  1370. goto stream_skip;
  1371. }
  1372. if (snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename) >= MAXPATHLEN) {
  1373. php_error_docref(NULL, E_NOTICE, "%s/%s path was truncated to %d", ptr, filename, MAXPATHLEN);
  1374. }
  1375. if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir_ex(trypath, 0)) {
  1376. goto stream_skip;
  1377. }
  1378. stream = php_stream_fopen_rel(trypath, mode, opened_path, options);
  1379. if (stream) {
  1380. efree(pathbuf);
  1381. return stream;
  1382. }
  1383. stream_skip:
  1384. ptr = end;
  1385. } /* end provided path */
  1386. efree(pathbuf);
  1387. return NULL;
  1388. }
  1389. /* }}} */
  1390. /*
  1391. * Local variables:
  1392. * tab-width: 4
  1393. * c-basic-offset: 4
  1394. * End:
  1395. * vim600: noet sw=4 ts=4 fdm=marker
  1396. * vim<600: noet sw=4 ts=4
  1397. */