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

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:59:10 +0000 (8 days ago)

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