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: 373 564 66.1 %
Date: 2015-04-14 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       78504 : PHPAPI int php_stream_parse_fopen_modes(const char *mode, int *open_flags)
      65             : {
      66             :         int flags;
      67             : 
      68       78504 :         switch (mode[0]) {
      69             :                 case 'r':
      70       37959 :                         flags = 0;
      71       37959 :                         break;
      72             :                 case 'w':
      73       37495 :                         flags = O_TRUNC|O_CREAT;
      74       37495 :                         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       78481 :         if (strchr(mode, '+')) {
      90        9543 :                 flags |= O_RDWR;
      91       68938 :         } else if (flags) {
      92       31661 :                 flags |= O_WRONLY;
      93             :         } else {
      94       37277 :                 flags |= O_RDONLY;
      95             :         }
      96             : 
      97             : #if defined(O_NONBLOCK)
      98       78481 :         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       78481 :         *open_flags = flags;
     112       78481 :         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             :         char *temp_file_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      272494 : static int do_fstat(php_stdio_stream_data *d, int force)
     147             : {
     148      272494 :         if (!d->cached_fstat || force) {
     149             :                 int fd;
     150             :                 int r;
     151             : 
     152      272494 :                 PHP_STDIOP_GET_FD(fd, d);
     153      272494 :                 r = zend_fstat(fd, &d->sb);
     154      272494 :                 d->cached_fstat = r == 0;
     155             : 
     156      272494 :                 return r;
     157             :         }
     158           0 :         return 0;
     159             : }
     160             : 
     161      154192 : 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      308384 :         self = pemalloc_rel_orig(sizeof(*self), persistent_id);
     166      154192 :         memset(self, 0, sizeof(*self));
     167      154192 :         self->file = NULL;
     168      154192 :         self->is_pipe = 0;
     169      154192 :         self->lock_flag = LOCK_UN;
     170      154192 :         self->is_process_pipe = 0;
     171      154192 :         self->temp_file_name = NULL;
     172      154192 :         self->fd = fd;
     173             : 
     174      154192 :         return php_stream_alloc_rel(&php_stream_stdio_ops, self, persistent_id, mode);
     175             : }
     176             : 
     177       61194 : 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       61194 :         self = emalloc_rel_orig(sizeof(*self));
     182       61194 :         memset(self, 0, sizeof(*self));
     183       61194 :         self->file = file;
     184       61194 :         self->is_pipe = 0;
     185       61194 :         self->lock_flag = LOCK_UN;
     186       61194 :         self->is_process_pipe = 0;
     187       61194 :         self->temp_file_name = NULL;
     188       61194 :         self->fd = fileno(file);
     189             : 
     190       61194 :         return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
     191             : }
     192             : 
     193       14089 : PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path_ptr STREAMS_DC)
     194             : {
     195       14089 :         char *opened_path = NULL;
     196             :         int fd;
     197             : 
     198       14089 :         fd = php_open_temporary_fd(dir, pfx, &opened_path);
     199       14089 :         if (fd != -1)   {
     200             :                 php_stream *stream;
     201             : 
     202       14089 :                 if (opened_path_ptr) {
     203           0 :                         *opened_path_ptr = opened_path;
     204             :                 }
     205             : 
     206       14089 :                 stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL);
     207       14089 :                 if (stream) {
     208       14089 :                         php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
     209       14089 :                         stream->wrapper = &php_plain_files_wrapper;
     210       14089 :                         stream->orig_path = estrdup(opened_path);
     211             : 
     212       14089 :                         self->temp_file_name = opened_path;
     213       14089 :                         self->lock_flag = LOCK_UN;
     214             : 
     215       14089 :                         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       14088 : PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC)
     227             : {
     228       14088 :         return php_stream_fopen_temporary_file(NULL, "php", NULL);
     229             : }
     230             : 
     231      131557 : PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const char *persistent_id STREAMS_DC)
     232             : {
     233      131557 :         php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);
     234             : 
     235      131557 :         if (stream) {
     236      131557 :                 php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
     237             : 
     238             : #ifdef S_ISFIFO
     239             :                 /* detect if this is a pipe */
     240      131557 :                 if (self->fd >= 0) {
     241      131557 :                         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      131557 :                 if (self->is_pipe) {
     254       62250 :                         stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
     255             :                 } else {
     256       69307 :                         stream->position = zend_lseek(self->fd, 0, SEEK_CUR);
     257             : #ifdef ESPIPE
     258       69307 :                         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      131557 :         return stream;
     268             : }
     269             : 
     270       61194 : PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC)
     271             : {
     272       61194 :         php_stream *stream = php_stream_fopen_from_file_int_rel(file, mode);
     273             : 
     274       61194 :         if (stream) {
     275       61194 :                 php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
     276             : 
     277             : #ifdef S_ISFIFO
     278             :                 /* detect if this is a pipe */
     279       61194 :                 if (self->fd >= 0) {
     280       61194 :                         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       61194 :                 if (self->is_pipe) {
     293       61186 :                         stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
     294             :                 } else {
     295           8 :                         stream->position = zend_ftell(file);
     296             :                 }
     297             :         }
     298             : 
     299       61194 :         return stream;
     300             : }
     301             : 
     302         158 : 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         158 :         self = emalloc_rel_orig(sizeof(*self));
     308         158 :         memset(self, 0, sizeof(*self));
     309         158 :         self->file = file;
     310         158 :         self->is_pipe = 1;
     311         158 :         self->lock_flag = LOCK_UN;
     312         158 :         self->is_process_pipe = 1;
     313         158 :         self->fd = fileno(file);
     314         158 :         self->temp_file_name = NULL;
     315             : 
     316         158 :         stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
     317         158 :         stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
     318         158 :         return stream;
     319             : }
     320             : 
     321      203438 : static size_t php_stdiop_write(php_stream *stream, const char *buf, size_t count)
     322             : {
     323      203438 :         php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
     324             : 
     325             :         assert(data != NULL);
     326             : 
     327      203438 :         if (data->fd >= 0) {
     328             : #ifdef PHP_WIN32
     329             :                 int bytes_written = write(data->fd, buf, (unsigned int)count);
     330             : #else
     331      203438 :                 int bytes_written = write(data->fd, buf, count);
     332             : #endif
     333      203438 :                 if (bytes_written < 0) return 0;
     334      203383 :                 return (size_t) bytes_written;
     335             :         } else {
     336             : 
     337             : #if HAVE_FLUSHIO
     338             :                 if (!data->is_pipe && data->last_op == 'r') {
     339             :                         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      966445 : static size_t php_stdiop_read(php_stream *stream, char *buf, size_t count)
     349             : {
     350      966445 :         php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
     351             :         size_t ret;
     352             : 
     353             :         assert(data != NULL);
     354             : 
     355      966445 :         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      966442 :                 ret = read(data->fd, buf,  PLAIN_WRAP_BUF_SIZE(count));
     385             : 
     386      966442 :                 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      966442 :                 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      966445 :         return ret;
     407             : }
     408             : 
     409      215661 : static int php_stdiop_close(php_stream *stream, int close_handle)
     410             : {
     411             :         int ret;
     412      215661 :         php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
     413             : 
     414             :         assert(data != NULL);
     415             : 
     416             : #if HAVE_MMAP
     417      215661 :         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      215661 :         if (close_handle) {
     433      215661 :                 if (data->file) {
     434       61557 :                         if (data->is_process_pipe) {
     435         158 :                                 errno = 0;
     436         158 :                                 ret = pclose(data->file);
     437             : 
     438             : #if HAVE_SYS_WAIT_H
     439         158 :                                 if (WIFEXITED(ret)) {
     440         158 :                                         ret = WEXITSTATUS(ret);
     441             :                                 }
     442             : #endif
     443             :                         } else {
     444       61399 :                                 ret = fclose(data->file);
     445       61399 :                                 data->file = NULL;
     446             :                         }
     447      154104 :                 } else if (data->fd != -1) {
     448      154104 :                         ret = close(data->fd);
     449      154104 :                         data->fd = -1;
     450             :                 } else {
     451           0 :                         return 0; /* everything should be closed already -> success */
     452             :                 }
     453      215661 :                 if (data->temp_file_name) {
     454       14089 :                         unlink(data->temp_file_name);
     455             :                         /* temporary streams are never persistent */
     456       14089 :                         efree(data->temp_file_name);
     457       14089 :                         data->temp_file_name = NULL;
     458             :                 }
     459             :         } else {
     460           0 :                 ret = 0;
     461           0 :                 data->file = NULL;
     462           0 :                 data->fd = -1;
     463             :         }
     464             : 
     465      215661 :         pefree(data, stream->is_persistent);
     466             : 
     467      215661 :         return ret;
     468             : }
     469             : 
     470      217819 : static int php_stdiop_flush(php_stream *stream)
     471             : {
     472      217819 :         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      217819 :         if (data->file) {
     482       62706 :                 return fflush(data->file);
     483             :         }
     484      155113 :         return 0;
     485             : }
     486             : 
     487       79318 : static int php_stdiop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset)
     488             : {
     489       79318 :         php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
     490             :         int ret;
     491             : 
     492             :         assert(data != NULL);
     493             : 
     494       79318 :         if (data->is_pipe) {
     495           0 :                 php_error_docref(NULL, E_WARNING, "cannot seek on a pipe");
     496           0 :                 return -1;
     497             :         }
     498             : 
     499       79318 :         if (data->fd >= 0) {
     500             :                 zend_off_t result;
     501             : 
     502       78291 :                 result = zend_lseek(data->fd, offset, whence);
     503       78291 :                 if (result == (zend_off_t)-1)
     504         189 :                         return -1;
     505             : 
     506       78102 :                 *newoffset = result;
     507       78102 :                 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     3395355 : static int php_stdiop_cast(php_stream *stream, int castas, void **ret)
     517             : {
     518             :         php_socket_t fd;
     519     3395355 :         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     3395355 :         switch (castas) {
     527             :                 case PHP_STREAM_AS_STDIO:
     528          89 :                         if (ret) {
     529             : 
     530          89 :                                 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          88 :                                         php_stream_mode_sanitize_fdopen_fopencookie(stream, fixed_mode);
     535          88 :                                         data->file = fdopen(data->fd, fixed_mode);
     536          88 :                                         if (data->file == NULL) {
     537           0 :                                                 return FAILURE;
     538             :                                         }
     539             :                                 }
     540             : 
     541          89 :                                 *(FILE**)ret = data->file;
     542          89 :                                 data->fd = SOCK_ERR;
     543             :                         }
     544          89 :                         return SUCCESS;
     545             : 
     546             :                 case PHP_STREAM_AS_FD_FOR_SELECT:
     547     3394856 :                         PHP_STDIOP_GET_FD(fd, data);
     548     3394856 :                         if (SOCK_ERR == fd) {
     549           0 :                                 return FAILURE;
     550             :                         }
     551     3394856 :                         if (ret) {
     552     3394853 :                                 *(php_socket_t *)ret = fd;
     553             :                         }
     554     3394856 :                         return SUCCESS;
     555             : 
     556             :                 case PHP_STREAM_AS_FD:
     557         409 :                         PHP_STDIOP_GET_FD(fd, data);
     558             : 
     559         409 :                         if (SOCK_ERR == fd) {
     560           0 :                                 return FAILURE;
     561             :                         }
     562         409 :                         if (data->file) {
     563         119 :                                 fflush(data->file);
     564             :                         }
     565         409 :                         if (ret) {
     566         409 :                                 *(php_socket_t *)ret = fd;
     567             :                         }
     568         409 :                         return SUCCESS;
     569             :                 default:
     570           1 :                         return FAILURE;
     571             :         }
     572             : }
     573             : 
     574       43435 : static int php_stdiop_stat(php_stream *stream, php_stream_statbuf *ssb)
     575             : {
     576             :         int ret;
     577       43435 :         php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
     578             : 
     579             :         assert(data != NULL);
     580       43435 :         if((ret = do_fstat(data, 1)) == 0) {
     581       43435 :                 memcpy(&ssb->sb, &data->sb, sizeof(ssb->sb));
     582             :         }
     583             : 
     584       43435 :         return ret;
     585             : }
     586             : 
     587      112991 : static int php_stdiop_set_option(php_stream *stream, int option, int value, void *ptrparam)
     588             : {
     589      112991 :         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      112991 :         PHP_STDIOP_GET_FD(fd, data);
     599             : 
     600      112991 :         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         343 :                         if (fd == -1) {
     647           0 :                                 return -1;
     648             :                         }
     649             : 
     650         343 :                         if ((zend_uintptr_t) ptrparam == PHP_STREAM_LOCK_SUPPORTED) {
     651          91 :                                 return 0;
     652             :                         }
     653             : 
     654         252 :                         if (!flock(fd, value)) {
     655         252 :                                 data->lock_flag = value;
     656         252 :                                 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       78584 :                                 php_stream_mmap_range *range = (php_stream_mmap_range*)ptrparam;
     666             :                                 int prot, flags;
     667             : 
     668       78584 :                                 switch (value) {
     669             :                                         case PHP_STREAM_MMAP_SUPPORTED:
     670       27762 :                                                 return fd == -1 ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
     671             : 
     672             :                                         case PHP_STREAM_MMAP_MAP_RANGE:
     673       27762 :                                                 if(do_fstat(data, 1) != 0) {
     674           0 :                                                         return PHP_STREAM_OPTION_RETURN_ERR;
     675             :                                                 }
     676       27762 :                                                 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       27762 :                                                 if (range->length == 0 || range->length > data->sb.st_size) {
     680        8815 :                                                         range->length = data->sb.st_size;
     681             :                                                 }
     682       27762 :                                                 if (range->offset >= data->sb.st_size) {
     683          89 :                                                         range->offset = data->sb.st_size;
     684          89 :                                                         range->length = 0;
     685             :                                                 }
     686       27762 :                                                 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       27762 :                                                                 prot = PROT_READ;
     697       27762 :                                                                 flags = MAP_SHARED;
     698       27762 :                                                                 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       27762 :                                                 range->mapped = (char*)mmap(NULL, range->length, prot, flags, fd, range->offset);
     707       27762 :                                                 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       23093 :                                                 data->last_mapped_addr = range->mapped;
     713       23093 :                                                 data->last_mapped_len = range->length;
     714       23093 :                                                 return PHP_STREAM_OPTION_RETURN_OK;
     715             : 
     716             :                                         case PHP_STREAM_MMAP_UNMAP:
     717       23060 :                                                 if (data->last_mapped_addr) {
     718       23060 :                                                         munmap(data->last_mapped_addr, data->last_mapped_len);
     719       23060 :                                                         data->last_mapped_addr = NULL;
     720             : 
     721       23060 :                                                         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       33271 :                         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       38856 : static size_t php_plain_files_dirstream_read(php_stream *stream, char *buf, size_t count)
     853             : {
     854       38856 :         DIR *dir = (DIR*)stream->abstract;
     855             :         /* avoid libc5 readdir problems */
     856             :         char entry[sizeof(struct dirent)+MAXPATHLEN];
     857       38856 :         struct dirent *result = (struct dirent *)&entry;
     858       38856 :         php_stream_dirent *ent = (php_stream_dirent*)buf;
     859             : 
     860             :         /* avoid problems if someone mis-uses the stream */
     861       38856 :         if (count != sizeof(php_stream_dirent))
     862           0 :                 return 0;
     863             : 
     864       38856 :         if (php_readdir_r(dir, (struct dirent *)entry, &result) == 0 && result) {
     865       38142 :                 PHP_STRLCPY(ent->d_name, result->d_name, sizeof(ent->d_name), strlen(result->d_name));
     866       38142 :                 return sizeof(php_stream_dirent);
     867             :         }
     868         714 :         return 0;
     869             : }
     870             : 
     871         849 : static int php_plain_files_dirstream_close(php_stream *stream, int close_handle)
     872             : {
     873         849 :         return closedir((DIR *)stream->abstract);
     874             : }
     875             : 
     876          55 : static int php_plain_files_dirstream_rewind(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
     877             : {
     878          55 :         rewinddir((DIR *)stream->abstract);
     879          55 :         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         934 : static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, const char *path, const char *mode,
     893             :                 int options, char **opened_path, php_stream_context *context STREAMS_DC)
     894             : {
     895         934 :         DIR *dir = NULL;
     896         934 :         php_stream *stream = NULL;
     897             : 
     898             : #ifdef HAVE_GLOB
     899         934 :         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         934 :         if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path)) {
     905          24 :                 return NULL;
     906             :         }
     907             : 
     908         910 :         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         910 :         if (dir) {
     921         849 :                 stream = php_stream_alloc(&php_plain_files_dirstream_ops, dir, 0, mode);
     922         849 :                 if (stream == NULL)
     923           0 :                         closedir(dir);
     924             :         }
     925             : 
     926         910 :         return stream;
     927             : }
     928             : /* }}} */
     929             : 
     930             : /* {{{ php_stream_fopen */
     931       78504 : PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, char **opened_path, int options STREAMS_DC)
     932             : {
     933       78504 :         char *realpath = NULL;
     934             :         int open_flags;
     935             :         int fd;
     936             :         php_stream *ret;
     937       78504 :         int persistent = options & STREAM_OPEN_PERSISTENT;
     938       78504 :         char *persistent_id = NULL;
     939             : 
     940       78504 :         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       78481 :         if (options & STREAM_ASSUME_REALPATH) {
     948        8699 :                 realpath = estrdup(filename);
     949             :         } else {
     950       69782 :                 if ((realpath = expand_filepath(filename, NULL)) == NULL) {
     951           0 :                         return NULL;
     952             :                 }
     953             :         }
     954             : 
     955       78481 :         if (persistent) {
     956          25 :                 spprintf(&persistent_id, 0, "streams_stdio_%d_%s", open_flags, realpath);
     957          25 :                 switch (php_stream_from_persistent_id(persistent_id, &ret)) {
     958             :                         case PHP_STREAM_PERSISTENT_SUCCESS:
     959           0 :                                 if (opened_path) {
     960           0 :                                         *opened_path = realpath;
     961           0 :                                         realpath = NULL;
     962             :                                 }
     963             :                                 /* fall through */
     964             : 
     965             :                         case PHP_STREAM_PERSISTENT_FAILURE:
     966           0 :                                 if (realpath) {
     967           0 :                                         efree(realpath);
     968             :                                 }
     969           0 :                                 efree(persistent_id);;
     970           0 :                                 return ret;
     971             :                 }
     972             :         }
     973             : 
     974       78481 :         fd = open(realpath, open_flags, 0666);
     975             : 
     976       78481 :         if (fd != -1)   {
     977             : 
     978       77853 :                 if (options & STREAM_OPEN_FOR_INCLUDE) {
     979        8546 :                         ret = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);
     980             :                 } else {
     981       69307 :                         ret = php_stream_fopen_from_fd_rel(fd, mode, persistent_id);
     982             :                 }
     983             : 
     984       77853 :                 if (ret)        {
     985       77853 :                         if (opened_path) {
     986       12294 :                                 *opened_path = realpath;
     987       12294 :                                 realpath = NULL;
     988             :                         }
     989       77853 :                         if (realpath) {
     990       65559 :                                 efree(realpath);
     991             :                         }
     992       77853 :                         if (persistent_id) {
     993          24 :                                 efree(persistent_id);
     994             :                         }
     995             : 
     996             :                         /* WIN32 always set ISREG flag */
     997             : #ifndef PHP_WIN32
     998             :                         /* sanity checks for include/require.
     999             :                          * We check these after opening the stream, so that we save
    1000             :                          * on fstat() syscalls */
    1001       77853 :                         if (options & STREAM_OPEN_FOR_INCLUDE) {
    1002        8546 :                                 php_stdio_stream_data *self = (php_stdio_stream_data*)ret->abstract;
    1003             :                                 int r;
    1004             : 
    1005        8546 :                                 r = do_fstat(self, 0);
    1006        8546 :                                 if ((r == 0 && !S_ISREG(self->sb.st_mode))) {
    1007           1 :                                         if (opened_path) {
    1008           1 :                                                 efree(*opened_path);
    1009           1 :                                                 *opened_path = NULL;
    1010             :                                         }
    1011           1 :                                         php_stream_close(ret);
    1012           1 :                                         return NULL;
    1013             :                                 }
    1014             :                         }
    1015             : #endif
    1016             : 
    1017       77852 :                         return ret;
    1018             :                 }
    1019           0 :                 close(fd);
    1020             :         }
    1021         628 :         efree(realpath);
    1022         628 :         if (persistent_id) {
    1023           1 :                 efree(persistent_id);
    1024             :         }
    1025         628 :         return NULL;
    1026             : }
    1027             : /* }}} */
    1028             : 
    1029             : 
    1030       78545 : static php_stream *php_plain_files_stream_opener(php_stream_wrapper *wrapper, const char *path, const char *mode,
    1031             :                 int options, char **opened_path, php_stream_context *context STREAMS_DC)
    1032             : {
    1033       78545 :         if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path)) {
    1034          41 :                 return NULL;
    1035             :         }
    1036             : 
    1037       78504 :         return php_stream_fopen_rel(path, mode, opened_path, options);
    1038             : }
    1039             : 
    1040       56583 : static int php_plain_files_url_stater(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context)
    1041             : {
    1042       56583 :         if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
    1043           0 :                 url += sizeof("file://") - 1;
    1044             :         }
    1045             : 
    1046       56583 :         if (php_check_open_basedir_ex(url, (flags & PHP_STREAM_URL_STAT_QUIET) ? 0 : 1)) {
    1047           9 :                 return -1;
    1048             :         }
    1049             : 
    1050             : #ifdef PHP_WIN32
    1051             :         if (EG(windows_version_info).dwMajorVersion >= 5) {
    1052             :                 if (flags & PHP_STREAM_URL_STAT_LINK) {
    1053             :                         return VCWD_LSTAT(url, &ssb->sb);
    1054             :                 }
    1055             :         }
    1056             : #else
    1057             : # ifdef HAVE_SYMLINK
    1058       56574 :         if (flags & PHP_STREAM_URL_STAT_LINK) {
    1059        2524 :                 return VCWD_LSTAT(url, &ssb->sb);
    1060             :         } else
    1061             : # endif
    1062             : #endif
    1063       54050 :                 return VCWD_STAT(url, &ssb->sb);
    1064             : }
    1065             : 
    1066      214397 : static int php_plain_files_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
    1067             : {
    1068             :         int ret;
    1069             : 
    1070      214397 :         if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
    1071           0 :                 url += sizeof("file://") - 1;
    1072             :         }
    1073             : 
    1074      214397 :         if (php_check_open_basedir(url)) {
    1075           6 :                 return 0;
    1076             :         }
    1077             : 
    1078      214391 :         ret = VCWD_UNLINK(url);
    1079      214391 :         if (ret == -1) {
    1080      187791 :                 if (options & REPORT_ERRORS) {
    1081      187791 :                         php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
    1082             :                 }
    1083      187791 :                 return 0;
    1084             :         }
    1085             : 
    1086             :         /* Clear stat cache (and realpath cache) */
    1087       26600 :         php_clear_stat_cache(1, NULL, 0);
    1088             : 
    1089       26600 :         return 1;
    1090             : }
    1091             : 
    1092          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)
    1093             : {
    1094             :         int ret;
    1095             : 
    1096          79 :         if (!url_from || !url_to) {
    1097           0 :                 return 0;
    1098             :         }
    1099             : 
    1100             : #ifdef PHP_WIN32
    1101             :         if (!php_win32_check_trailing_space(url_from, (int)strlen(url_from))) {
    1102             :                 php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to);
    1103             :                 return 0;
    1104             :         }
    1105             :         if (!php_win32_check_trailing_space(url_to, (int)strlen(url_to))) {
    1106             :                 php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to);
    1107             :                 return 0;
    1108             :         }
    1109             : #endif
    1110             : 
    1111          79 :         if (strncasecmp(url_from, "file://", sizeof("file://") - 1) == 0) {
    1112           0 :                 url_from += sizeof("file://") - 1;
    1113             :         }
    1114             : 
    1115          79 :         if (strncasecmp(url_to, "file://", sizeof("file://") - 1) == 0) {
    1116           0 :                 url_to += sizeof("file://") - 1;
    1117             :         }
    1118             : 
    1119          79 :         if (php_check_open_basedir(url_from) || php_check_open_basedir(url_to)) {
    1120           5 :                 return 0;
    1121             :         }
    1122             : 
    1123          74 :         ret = VCWD_RENAME(url_from, url_to);
    1124             : 
    1125          74 :         if (ret == -1) {
    1126             : #ifndef PHP_WIN32
    1127             : # ifdef EXDEV
    1128          24 :                 if (errno == EXDEV) {
    1129             :                         zend_stat_t sb;
    1130           0 :                         if (php_copy_file(url_from, url_to) == SUCCESS) {
    1131           0 :                                 if (VCWD_STAT(url_from, &sb) == 0) {
    1132             : #  if !defined(TSRM_WIN32) && !defined(NETWARE)
    1133           0 :                                         if (VCWD_CHMOD(url_to, sb.st_mode)) {
    1134           0 :                                                 if (errno == EPERM) {
    1135           0 :                                                         php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
    1136           0 :                                                         VCWD_UNLINK(url_from);
    1137           0 :                                                         return 1;
    1138             :                                                 }
    1139           0 :                                                 php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
    1140           0 :                                                 return 0;
    1141             :                                         }
    1142           0 :                                         if (VCWD_CHOWN(url_to, sb.st_uid, sb.st_gid)) {
    1143           0 :                                                 if (errno == EPERM) {
    1144           0 :                                                         php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
    1145           0 :                                                         VCWD_UNLINK(url_from);
    1146           0 :                                                         return 1;
    1147             :                                                 }
    1148           0 :                                                 php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
    1149           0 :                                                 return 0;
    1150             :                                         }
    1151             : #  endif
    1152           0 :                                         VCWD_UNLINK(url_from);
    1153           0 :                                         return 1;
    1154             :                                 }
    1155             :                         }
    1156           0 :                         php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
    1157           0 :                         return 0;
    1158             :                 }
    1159             : # endif
    1160             : #endif
    1161             : 
    1162             : #ifdef PHP_WIN32
    1163             :                 php_win32_docref2_from_error(GetLastError(), url_from, url_to);
    1164             : #else
    1165          24 :                 php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
    1166             : #endif
    1167          24 :                 return 0;
    1168             :         }
    1169             : 
    1170             :         /* Clear stat cache (and realpath cache) */
    1171          50 :         php_clear_stat_cache(1, NULL, 0);
    1172             : 
    1173          50 :         return 1;
    1174             : }
    1175             : 
    1176        1678 : static int php_plain_files_mkdir(php_stream_wrapper *wrapper, const char *dir, int mode, int options, php_stream_context *context)
    1177             : {
    1178        1678 :         int ret, recursive = options & PHP_STREAM_MKDIR_RECURSIVE;
    1179             :         char *p;
    1180             : 
    1181        1678 :         if (strncasecmp(dir, "file://", sizeof("file://") - 1) == 0) {
    1182           1 :                 dir += sizeof("file://") - 1;
    1183             :         }
    1184             : 
    1185        1678 :         if (!recursive) {
    1186        1115 :                 ret = php_mkdir(dir, mode);
    1187             :         } else {
    1188             :                 /* we look for directory separator from the end of string, thus hopefuly reducing our work load */
    1189             :                 char *e;
    1190             :                 zend_stat_t sb;
    1191         563 :                 int dir_len = (int)strlen(dir);
    1192         563 :                 int offset = 0;
    1193             :                 char buf[MAXPATHLEN];
    1194             : 
    1195         563 :                 if (!expand_filepath_with_mode(dir, buf, NULL, 0, CWD_EXPAND )) {
    1196           0 :                         php_error_docref(NULL, E_WARNING, "Invalid path");
    1197           0 :                         return 0;
    1198             :                 }
    1199             : 
    1200         563 :                 e = buf +  strlen(buf);
    1201             : 
    1202         563 :                 if ((p = memchr(buf, DEFAULT_SLASH, dir_len))) {
    1203         563 :                         offset = p - buf + 1;
    1204             :                 }
    1205             : 
    1206         563 :                 if (p && dir_len == 1) {
    1207             :                         /* buf == "DEFAULT_SLASH" */
    1208             :                 }
    1209             :                 else {
    1210             :                         /* find a top level directory we need to create */
    1211        1683 :                         while ( (p = strrchr(buf + offset, DEFAULT_SLASH)) || (offset != 1 && (p = strrchr(buf, DEFAULT_SLASH))) ) {
    1212        1120 :                                 int n = 0;
    1213             : 
    1214        1120 :                                 *p = '\0';
    1215        2240 :                                 while (p > buf && *(p-1) == DEFAULT_SLASH) {
    1216           0 :                                         ++n;
    1217           0 :                                         --p;
    1218           0 :                                         *p = '\0';
    1219             :                                 }
    1220        1120 :                                 if (VCWD_STAT(buf, &sb) == 0) {
    1221             :                                         while (1) {
    1222         563 :                                                 *p = DEFAULT_SLASH;
    1223         563 :                                                 if (!n) break;
    1224           0 :                                                 --n;
    1225           0 :                                                 ++p;
    1226           0 :                                         }
    1227         563 :                                         break;
    1228             :                                 }
    1229             :                         }
    1230             :                 }
    1231             : 
    1232         563 :                 if (p == buf) {
    1233           0 :                         ret = php_mkdir(dir, mode);
    1234         563 :                 } else if (!(ret = php_mkdir(buf, mode))) {
    1235         562 :                         if (!p) {
    1236           0 :                                 p = buf;
    1237             :                         }
    1238             :                         /* create any needed directories if the creation of the 1st directory worked */
    1239       10635 :                         while (++p != e) {
    1240        9511 :                                 if (*p == '\0') {
    1241         557 :                                         *p = DEFAULT_SLASH;
    1242         592 :                                         if ((*(p+1) != '\0') &&
    1243          35 :                                                 (ret = VCWD_MKDIR(buf, (mode_t)mode)) < 0) {
    1244           0 :                                                 if (options & REPORT_ERRORS) {
    1245           0 :                                                         php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
    1246             :                                                 }
    1247           0 :                                                 break;
    1248             :                                         }
    1249             :                                 }
    1250             :                         }
    1251             :                 }
    1252             :         }
    1253        1678 :         if (ret < 0) {
    1254             :                 /* Failure */
    1255          50 :                 return 0;
    1256             :         } else {
    1257             :                 /* Success */
    1258        1628 :                 return 1;
    1259             :         }
    1260             : }
    1261             : 
    1262        1697 : static int php_plain_files_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
    1263             : {
    1264        1697 :         if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
    1265           1 :                 url += sizeof("file://") - 1;
    1266             :         }
    1267             : 
    1268        1697 :         if (php_check_open_basedir(url)) {
    1269           5 :                 return 0;
    1270             :         }
    1271             : 
    1272             : #if PHP_WIN32
    1273             :         if (!php_win32_check_trailing_space(url, (int)strlen(url))) {
    1274             :                 php_error_docref1(NULL, url, E_WARNING, "%s", strerror(ENOENT));
    1275             :                 return 0;
    1276             :         }
    1277             : #endif
    1278             : 
    1279        1692 :         if (VCWD_RMDIR(url) < 0) {
    1280          29 :                 php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
    1281          29 :                 return 0;
    1282             :         }
    1283             : 
    1284             :         /* Clear stat cache (and realpath cache) */
    1285        1663 :         php_clear_stat_cache(1, NULL, 0);
    1286             : 
    1287        1663 :         return 1;
    1288             : }
    1289             : 
    1290           0 : static int php_plain_files_metadata(php_stream_wrapper *wrapper, const char *url, int option, void *value, php_stream_context *context)
    1291             : {
    1292             :         struct utimbuf *newtime;
    1293             : #if !defined(WINDOWS) && !defined(NETWARE)
    1294             :         uid_t uid;
    1295             :         gid_t gid;
    1296             : #endif
    1297             :         mode_t mode;
    1298           0 :         int ret = 0;
    1299             : #if PHP_WIN32
    1300             :         int url_len = (int)strlen(url);
    1301             : #endif
    1302             : 
    1303             : #if PHP_WIN32
    1304             :         if (!php_win32_check_trailing_space(url, url_len)) {
    1305             :                 php_error_docref1(NULL, url, E_WARNING, "%s", strerror(ENOENT));
    1306             :                 return 0;
    1307             :         }
    1308             : #endif
    1309             : 
    1310           0 :         if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
    1311           0 :                 url += sizeof("file://") - 1;
    1312             :         }
    1313             : 
    1314           0 :         if (php_check_open_basedir(url)) {
    1315           0 :                 return 0;
    1316             :         }
    1317             : 
    1318           0 :         switch(option) {
    1319             :                 case PHP_STREAM_META_TOUCH:
    1320           0 :                         newtime = (struct utimbuf *)value;
    1321           0 :                         if (VCWD_ACCESS(url, F_OK) != 0) {
    1322           0 :                                 FILE *file = VCWD_FOPEN(url, "w");
    1323           0 :                                 if (file == NULL) {
    1324           0 :                                         php_error_docref1(NULL, url, E_WARNING, "Unable to create file %s because %s", url, strerror(errno));
    1325           0 :                                         return 0;
    1326             :                                 }
    1327           0 :                                 fclose(file);
    1328             :                         }
    1329             : 
    1330           0 :                         ret = VCWD_UTIME(url, newtime);
    1331           0 :                         break;
    1332             : #if !defined(WINDOWS) && !defined(NETWARE)
    1333             :                 case PHP_STREAM_META_OWNER_NAME:
    1334             :                 case PHP_STREAM_META_OWNER:
    1335           0 :                         if(option == PHP_STREAM_META_OWNER_NAME) {
    1336           0 :                                 if(php_get_uid_by_name((char *)value, &uid) != SUCCESS) {
    1337           0 :                                         php_error_docref1(NULL, url, E_WARNING, "Unable to find uid for %s", (char *)value);
    1338           0 :                                         return 0;
    1339             :                                 }
    1340             :                         } else {
    1341           0 :                                 uid = (uid_t)*(long *)value;
    1342             :                         }
    1343           0 :                         ret = VCWD_CHOWN(url, uid, -1);
    1344           0 :                         break;
    1345             :                 case PHP_STREAM_META_GROUP:
    1346             :                 case PHP_STREAM_META_GROUP_NAME:
    1347           0 :                         if(option == PHP_STREAM_META_GROUP_NAME) {
    1348           0 :                                 if(php_get_gid_by_name((char *)value, &gid) != SUCCESS) {
    1349           0 :                                         php_error_docref1(NULL, url, E_WARNING, "Unable to find gid for %s", (char *)value);
    1350           0 :                                         return 0;
    1351             :                                 }
    1352             :                         } else {
    1353           0 :                                 gid = (gid_t)*(long *)value;
    1354             :                         }
    1355           0 :                         ret = VCWD_CHOWN(url, -1, gid);
    1356           0 :                         break;
    1357             : #endif
    1358             :                 case PHP_STREAM_META_ACCESS:
    1359           0 :                         mode = (mode_t)*(zend_long *)value;
    1360           0 :                         ret = VCWD_CHMOD(url, mode);
    1361           0 :                         break;
    1362             :                 default:
    1363           0 :                         php_error_docref1(NULL, url, E_WARNING, "Unknown option %d for stream_metadata", option);
    1364           0 :                         return 0;
    1365             :         }
    1366           0 :         if (ret == -1) {
    1367           0 :                 php_error_docref1(NULL, url, E_WARNING, "Operation failed: %s", strerror(errno));
    1368           0 :                 return 0;
    1369             :         }
    1370           0 :         php_clear_stat_cache(0, NULL, 0);
    1371           0 :         return 1;
    1372             : }
    1373             : 
    1374             : 
    1375             : static php_stream_wrapper_ops php_plain_files_wrapper_ops = {
    1376             :         php_plain_files_stream_opener,
    1377             :         NULL,
    1378             :         NULL,
    1379             :         php_plain_files_url_stater,
    1380             :         php_plain_files_dir_opener,
    1381             :         "plainfile",
    1382             :         php_plain_files_unlink,
    1383             :         php_plain_files_rename,
    1384             :         php_plain_files_mkdir,
    1385             :         php_plain_files_rmdir,
    1386             :         php_plain_files_metadata
    1387             : };
    1388             : 
    1389             : PHPAPI php_stream_wrapper php_plain_files_wrapper = {
    1390             :         &php_plain_files_wrapper_ops,
    1391             :         NULL,
    1392             :         0
    1393             : };
    1394             : 
    1395             : /* {{{ php_stream_fopen_with_path */
    1396           0 : PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path, int options STREAMS_DC)
    1397             : {
    1398             :         /* code ripped off from fopen_wrappers.c */
    1399             :         char *pathbuf, *end;
    1400             :         const char *ptr;
    1401             :         const char *exec_fname;
    1402             :         char trypath[MAXPATHLEN];
    1403             :         php_stream *stream;
    1404             :         int path_length;
    1405             :         int filename_length;
    1406             :         int exec_fname_length;
    1407             : 
    1408           0 :         if (opened_path) {
    1409           0 :                 *opened_path = NULL;
    1410             :         }
    1411             : 
    1412           0 :         if(!filename) {
    1413           0 :                 return NULL;
    1414             :         }
    1415             : 
    1416           0 :         filename_length = (int)strlen(filename);
    1417             : #ifndef PHP_WIN32
    1418             :         (void) filename_length;
    1419             : #endif
    1420             : 
    1421             :         /* Relative path open */
    1422           0 :         if (*filename == '.' && (IS_SLASH(filename[1]) || filename[1] == '.')) {
    1423             :                 /* further checks, we could have ....... filenames */
    1424           0 :                 ptr = filename + 1;
    1425           0 :                 if (*ptr == '.') {
    1426           0 :                         while (*(++ptr) == '.');
    1427           0 :                         if (!IS_SLASH(*ptr)) { /* not a relative path after all */
    1428           0 :                                 goto not_relative_path;
    1429             :                         }
    1430             :                 }
    1431             : 
    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             : not_relative_path:
    1441             : 
    1442             :         /* Absolute path open */
    1443           0 :         if (IS_ABSOLUTE_PATH(filename, filename_length)) {
    1444             : 
    1445           0 :                 if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename)) {
    1446           0 :                         return NULL;
    1447             :                 }
    1448             : 
    1449           0 :                 return php_stream_fopen_rel(filename, mode, opened_path, options);
    1450             :         }
    1451             : 
    1452             : #ifdef PHP_WIN32
    1453             :         if (IS_SLASH(filename[0])) {
    1454             :                 size_t cwd_len;
    1455             :                 char *cwd;
    1456             :                 cwd = virtual_getcwd_ex(&cwd_len);
    1457             :                 /* getcwd() will return always return [DRIVE_LETTER]:/) on windows. */
    1458             :                 *(cwd+3) = '\0';
    1459             : 
    1460             :                 if (snprintf(trypath, MAXPATHLEN, "%s%s", cwd, filename) >= MAXPATHLEN) {
    1461             :                         php_error_docref(NULL, E_NOTICE, "%s/%s path was truncated to %d", cwd, filename, MAXPATHLEN);
    1462             :                 }
    1463             : 
    1464             :                 efree(cwd);
    1465             : 
    1466             :                 if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(trypath)) {
    1467             :                         return NULL;
    1468             :                 }
    1469             : 
    1470             :                 return php_stream_fopen_rel(trypath, mode, opened_path, options);
    1471             :         }
    1472             : #endif
    1473             : 
    1474           0 :         if (!path || (path && !*path)) {
    1475           0 :                 return php_stream_fopen_rel(filename, mode, opened_path, options);
    1476             :         }
    1477             : 
    1478             :         /* check in provided path */
    1479             :         /* append the calling scripts' current working directory
    1480             :          * as a fall back case
    1481             :          */
    1482           0 :         if (zend_is_executing()) {
    1483           0 :                 exec_fname = zend_get_executed_filename();
    1484           0 :                 exec_fname_length = (int)strlen(exec_fname);
    1485           0 :                 path_length = (int)strlen(path);
    1486             : 
    1487           0 :                 while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
    1488           0 :                 if ((exec_fname && exec_fname[0] == '[')
    1489             :                                 || exec_fname_length<=0) {
    1490             :                         /* [no active file] or no path */
    1491           0 :                         pathbuf = estrdup(path);
    1492             :                 } else {
    1493           0 :                         pathbuf = (char *) emalloc(exec_fname_length + path_length +1 +1);
    1494           0 :                         memcpy(pathbuf, path, path_length);
    1495           0 :                         pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;
    1496           0 :                         memcpy(pathbuf+path_length+1, exec_fname, exec_fname_length);
    1497           0 :                         pathbuf[path_length + exec_fname_length +1] = '\0';
    1498             :                 }
    1499             :         } else {
    1500           0 :                 pathbuf = estrdup(path);
    1501             :         }
    1502             : 
    1503           0 :         ptr = pathbuf;
    1504             : 
    1505           0 :         while (ptr && *ptr) {
    1506           0 :                 end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
    1507           0 :                 if (end != NULL) {
    1508           0 :                         *end = '\0';
    1509           0 :                         end++;
    1510             :                 }
    1511           0 :                 if (*ptr == '\0') {
    1512           0 :                         goto stream_skip;
    1513             :                 }
    1514           0 :                 if (snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename) >= MAXPATHLEN) {
    1515           0 :                         php_error_docref(NULL, E_NOTICE, "%s/%s path was truncated to %d", ptr, filename, MAXPATHLEN);
    1516             :                 }
    1517             : 
    1518           0 :                 if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir_ex(trypath, 0)) {
    1519           0 :                         goto stream_skip;
    1520             :                 }
    1521             : 
    1522           0 :                 stream = php_stream_fopen_rel(trypath, mode, opened_path, options);
    1523           0 :                 if (stream) {
    1524           0 :                         efree(pathbuf);
    1525           0 :                         return stream;
    1526             :                 }
    1527             : stream_skip:
    1528           0 :                 ptr = end;
    1529             :         } /* end provided path */
    1530             : 
    1531           0 :         efree(pathbuf);
    1532           0 :         return NULL;
    1533             : 
    1534             : }
    1535             : /* }}} */
    1536             : 
    1537             : /*
    1538             :  * Local variables:
    1539             :  * tab-width: 4
    1540             :  * c-basic-offset: 4
    1541             :  * End:
    1542             :  * vim600: noet sw=4 ts=4 fdm=marker
    1543             :  * vim<600: noet sw=4 ts=4
    1544             :  */

Generated by: LCOV version 1.10

Generated at Tue, 14 Apr 2015 11:48:57 +0000 (3 days ago)

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