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

Generated by: LCOV version 1.10

Generated at Sat, 25 Jun 2016 07:09:11 +0000 (44 hours ago)

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