plain_wrapper.c 39 KB

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