PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - main/streams - plain_wrapper.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 370 560 66.1 %
Date: 2016-07-26 Functions: 28 30 93.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 26 Jul 2016 17:07:50 +0000 (2 days ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.