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

Generated by: LCOV version 1.10

Generated at Wed, 02 Sep 2015 17:19:25 +0000 (36 hours ago)

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