Annotation of embedaddon/php/main/streams/plain_wrapper.c, revision 1.1.1.3

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>