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