plain_wrapper.c 43 KB

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