2003-02-16 22:19:28 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2014-09-19 16:33:14 +00:00
|
|
|
| PHP Version 7 |
|
2003-02-16 22:19:28 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2017-01-02 15:30:12 +00:00
|
|
|
| Copyright (c) 1997-2017 The PHP Group |
|
2003-02-16 22:19:28 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 12:51:34 +00:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2003-02-16 22:19:28 +00:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-10 20:04:29 +00:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 12:51:34 +00:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2003-02-16 22:19:28 +00:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
2003-02-19 08:40:19 +00:00
|
|
|
| Authors: Wez Furlong <wez@thebrainroom.com> |
|
2003-02-16 22:19:28 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "php_globals.h"
|
|
|
|
#include "php_network.h"
|
|
|
|
#include "php_open_temporary_file.h"
|
|
|
|
#include "ext/standard/file.h"
|
2003-02-25 01:39:06 +00:00
|
|
|
#include "ext/standard/flock_compat.h"
|
2007-10-31 13:22:45 +00:00
|
|
|
#include "ext/standard/php_filestat.h"
|
2003-02-16 22:19:28 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#if HAVE_SYS_WAIT_H
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
2003-02-25 01:44:57 +00:00
|
|
|
#if HAVE_SYS_FILE_H
|
|
|
|
#include <sys/file.h>
|
|
|
|
#endif
|
2003-02-27 17:43:38 +00:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
2004-05-25 13:23:20 +00:00
|
|
|
#include "SAPI.h"
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2010-02-07 20:15:58 +00:00
|
|
|
#include "php_streams_int.h"
|
2010-01-27 01:18:43 +00:00
|
|
|
#ifdef PHP_WIN32
|
2010-02-07 20:15:58 +00:00
|
|
|
# include "win32/winutil.h"
|
2014-10-23 14:49:47 +00:00
|
|
|
# include "win32/time.h"
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
# include "win32/ioutil.h"
|
|
|
|
# include "win32/readdir.h"
|
2010-01-27 01:18:43 +00:00
|
|
|
#endif
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
#define php_stream_fopen_from_fd_int(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_CC)
|
|
|
|
#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)
|
|
|
|
#define php_stream_fopen_from_file_int(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_CC)
|
|
|
|
#define php_stream_fopen_from_file_int_rel(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_REL_CC)
|
2006-11-10 13:18:35 +00:00
|
|
|
|
2016-11-12 10:20:01 +00:00
|
|
|
#ifndef PHP_WIN32
|
2014-12-13 22:06:14 +00:00
|
|
|
extern int php_get_uid_by_name(const char *name, uid_t *uid);
|
|
|
|
extern int php_get_gid_by_name(const char *name, gid_t *gid);
|
2011-05-25 21:03:55 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-16 09:16:11 +00:00
|
|
|
#if defined(PHP_WIN32)
|
|
|
|
# define PLAIN_WRAP_BUF_SIZE(st) (((st) > UINT_MAX) ? UINT_MAX : (unsigned int)(st))
|
|
|
|
#else
|
|
|
|
# define PLAIN_WRAP_BUF_SIZE(st) (st)
|
|
|
|
#endif
|
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
/* parse standard "fopen" modes into open() flags */
|
|
|
|
PHPAPI int php_stream_parse_fopen_modes(const char *mode, int *open_flags)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
switch (mode[0]) {
|
|
|
|
case 'r':
|
|
|
|
flags = 0;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
flags = O_TRUNC|O_CREAT;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
flags = O_CREAT|O_APPEND;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
flags = O_CREAT|O_EXCL;
|
|
|
|
break;
|
2007-11-12 18:44:18 +00:00
|
|
|
case 'c':
|
|
|
|
flags = O_CREAT;
|
|
|
|
break;
|
2003-02-16 22:19:28 +00:00
|
|
|
default:
|
|
|
|
/* unknown mode */
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2013-12-06 09:29:24 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
if (strchr(mode, '+')) {
|
|
|
|
flags |= O_RDWR;
|
|
|
|
} else if (flags) {
|
|
|
|
flags |= O_WRONLY;
|
|
|
|
} else {
|
|
|
|
flags |= O_RDONLY;
|
|
|
|
}
|
|
|
|
|
2015-10-02 19:51:58 +00:00
|
|
|
#if defined(O_CLOEXEC)
|
|
|
|
if (strchr(mode, 'e')) {
|
|
|
|
flags |= O_CLOEXEC;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-06 09:29:24 +00:00
|
|
|
#if defined(O_NONBLOCK)
|
|
|
|
if (strchr(mode, 'n')) {
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-07-05 08:25:25 +00:00
|
|
|
#if defined(_O_TEXT) && defined(O_BINARY)
|
2003-05-21 13:33:55 +00:00
|
|
|
if (strchr(mode, 't')) {
|
|
|
|
flags |= _O_TEXT;
|
2003-07-05 08:25:25 +00:00
|
|
|
} else {
|
|
|
|
flags |= O_BINARY;
|
2003-05-21 13:33:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
2003-07-05 08:25:25 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
*open_flags = flags;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* {{{ ------- STDIO stream implementation -------*/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FILE *file;
|
|
|
|
int fd; /* underlying file descriptor */
|
2004-07-31 10:09:25 +00:00
|
|
|
unsigned is_process_pipe:1; /* use pclose instead of fclose */
|
|
|
|
unsigned is_pipe:1; /* don't try and seek */
|
|
|
|
unsigned cached_fstat:1; /* sb is valid */
|
2015-07-02 10:16:41 +00:00
|
|
|
unsigned is_pipe_blocking:1; /* allow blocking read() on pipes, currently Windows only */
|
|
|
|
unsigned _reserved:28;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-27 17:43:38 +00:00
|
|
|
int lock_flag; /* stores the lock state */
|
2015-03-03 23:05:28 +00:00
|
|
|
zend_string *temp_name; /* if non-null, this is the path to a temporary file that
|
2003-02-16 22:19:28 +00:00
|
|
|
* is to be deleted when the stream is closed */
|
|
|
|
#if HAVE_FLUSHIO
|
|
|
|
char last_op;
|
|
|
|
#endif
|
2003-02-27 17:43:38 +00:00
|
|
|
|
|
|
|
#if HAVE_MMAP
|
|
|
|
char *last_mapped_addr;
|
|
|
|
size_t last_mapped_len;
|
|
|
|
#endif
|
2003-02-28 01:47:28 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
char *last_mapped_addr;
|
|
|
|
HANDLE file_mapping;
|
|
|
|
#endif
|
2004-07-31 10:09:25 +00:00
|
|
|
|
2014-08-25 18:22:49 +00:00
|
|
|
zend_stat_t sb;
|
2003-02-16 22:19:28 +00:00
|
|
|
} php_stdio_stream_data;
|
2004-07-31 10:09:25 +00:00
|
|
|
#define PHP_STDIOP_GET_FD(anfd, data) anfd = (data)->file ? fileno((data)->file) : (data)->fd
|
|
|
|
|
|
|
|
static int do_fstat(php_stdio_stream_data *d, int force)
|
|
|
|
{
|
|
|
|
if (!d->cached_fstat || force) {
|
|
|
|
int fd;
|
|
|
|
int r;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2004-07-31 10:09:25 +00:00
|
|
|
PHP_STDIOP_GET_FD(fd, d);
|
2014-08-25 18:57:25 +00:00
|
|
|
r = zend_fstat(fd, &d->sb);
|
2004-07-31 10:09:25 +00:00
|
|
|
d->cached_fstat = r == 0;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static php_stream *_php_stream_fopen_from_fd_int(int fd, const char *mode, const char *persistent_id STREAMS_DC)
|
2006-11-10 13:18:35 +00:00
|
|
|
{
|
|
|
|
php_stdio_stream_data *self;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2006-11-10 13:18:35 +00:00
|
|
|
self = pemalloc_rel_orig(sizeof(*self), persistent_id);
|
|
|
|
memset(self, 0, sizeof(*self));
|
|
|
|
self->file = NULL;
|
|
|
|
self->is_pipe = 0;
|
|
|
|
self->lock_flag = LOCK_UN;
|
|
|
|
self->is_process_pipe = 0;
|
2015-03-03 23:05:28 +00:00
|
|
|
self->temp_name = NULL;
|
2006-11-10 13:18:35 +00:00
|
|
|
self->fd = fd;
|
2015-06-30 15:37:38 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
self->is_pipe_blocking = 0;
|
2015-06-30 13:47:25 +00:00
|
|
|
#endif
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2006-11-10 13:18:35 +00:00
|
|
|
return php_stream_alloc_rel(&php_stream_stdio_ops, self, persistent_id, mode);
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static php_stream *_php_stream_fopen_from_file_int(FILE *file, const char *mode STREAMS_DC)
|
2006-11-10 13:18:35 +00:00
|
|
|
{
|
|
|
|
php_stdio_stream_data *self;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2006-11-10 13:18:35 +00:00
|
|
|
self = emalloc_rel_orig(sizeof(*self));
|
|
|
|
memset(self, 0, sizeof(*self));
|
|
|
|
self->file = file;
|
|
|
|
self->is_pipe = 0;
|
|
|
|
self->lock_flag = LOCK_UN;
|
|
|
|
self->is_process_pipe = 0;
|
2015-03-03 23:05:28 +00:00
|
|
|
self->temp_name = NULL;
|
2006-11-10 13:18:35 +00:00
|
|
|
self->fd = fileno(file);
|
2015-06-30 15:37:38 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
self->is_pipe_blocking = 0;
|
2015-06-30 13:47:25 +00:00
|
|
|
#endif
|
2006-11-10 13:18:35 +00:00
|
|
|
|
|
|
|
return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
|
|
|
|
}
|
|
|
|
|
2015-03-03 23:05:28 +00:00
|
|
|
PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_ptr STREAMS_DC)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
2015-03-03 23:05:28 +00:00
|
|
|
zend_string *opened_path = NULL;
|
2014-07-02 10:10:10 +00:00
|
|
|
int fd;
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
fd = php_open_temporary_fd(dir, pfx, &opened_path);
|
2003-03-18 16:40:29 +00:00
|
|
|
if (fd != -1) {
|
2014-07-02 10:10:10 +00:00
|
|
|
php_stream *stream;
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2014-07-02 10:10:10 +00:00
|
|
|
if (opened_path_ptr) {
|
|
|
|
*opened_path_ptr = opened_path;
|
|
|
|
}
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2014-07-02 10:10:10 +00:00
|
|
|
stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL);
|
2003-02-16 22:19:28 +00:00
|
|
|
if (stream) {
|
|
|
|
php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
|
2005-05-24 10:13:52 +00:00
|
|
|
stream->wrapper = &php_plain_files_wrapper;
|
2015-06-30 10:59:27 +00:00
|
|
|
stream->orig_path = estrndup(ZSTR_VAL(opened_path), ZSTR_LEN(opened_path));
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2015-03-03 23:05:28 +00:00
|
|
|
self->temp_name = opened_path;
|
2003-02-25 01:39:06 +00:00
|
|
|
self->lock_flag = LOCK_UN;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
return stream;
|
|
|
|
}
|
2003-03-18 16:40:29 +00:00
|
|
|
close(fd);
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "unable to allocate stream");
|
2003-02-16 22:19:28 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC)
|
2014-07-02 13:41:59 +00:00
|
|
|
{
|
|
|
|
return php_stream_fopen_temporary_file(NULL, "php", NULL);
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const char *persistent_id STREAMS_DC)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
2006-11-10 13:18:35 +00:00
|
|
|
php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);
|
|
|
|
|
|
|
|
if (stream) {
|
|
|
|
php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
|
2003-02-16 22:19:28 +00:00
|
|
|
|
|
|
|
#ifdef S_ISFIFO
|
2006-11-10 13:18:35 +00:00
|
|
|
/* detect if this is a pipe */
|
|
|
|
if (self->fd >= 0) {
|
|
|
|
self->is_pipe = (do_fstat(self, 0) == 0 && S_ISFIFO(self->sb.st_mode)) ? 1 : 0;
|
|
|
|
}
|
2003-02-16 22:19:28 +00:00
|
|
|
#elif defined(PHP_WIN32)
|
2006-11-10 13:18:35 +00:00
|
|
|
{
|
2007-04-16 08:09:56 +00:00
|
|
|
zend_uintptr_t handle = _get_osfhandle(self->fd);
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2007-04-16 08:09:56 +00:00
|
|
|
if (handle != (zend_uintptr_t)INVALID_HANDLE_VALUE) {
|
2006-11-10 13:18:35 +00:00
|
|
|
self->is_pipe = GetFileType((HANDLE)handle) == FILE_TYPE_PIPE;
|
|
|
|
}
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
if (self->is_pipe) {
|
|
|
|
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
|
|
|
|
} else {
|
2014-08-16 09:16:11 +00:00
|
|
|
stream->position = zend_lseek(self->fd, 0, SEEK_CUR);
|
2005-07-18 13:28:24 +00:00
|
|
|
#ifdef ESPIPE
|
2014-08-16 09:16:11 +00:00
|
|
|
if (stream->position == (zend_off_t)-1 && errno == ESPIPE) {
|
2005-07-18 13:28:24 +00:00
|
|
|
stream->position = 0;
|
2008-11-03 16:58:53 +00:00
|
|
|
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
|
2005-07-18 14:12:52 +00:00
|
|
|
self->is_pipe = 1;
|
2005-07-18 13:28:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
2006-11-10 13:18:35 +00:00
|
|
|
php_stream *stream = php_stream_fopen_from_file_int_rel(file, mode);
|
|
|
|
|
|
|
|
if (stream) {
|
|
|
|
php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
|
2003-02-16 22:19:28 +00:00
|
|
|
|
|
|
|
#ifdef S_ISFIFO
|
2006-11-10 13:18:35 +00:00
|
|
|
/* detect if this is a pipe */
|
|
|
|
if (self->fd >= 0) {
|
|
|
|
self->is_pipe = (do_fstat(self, 0) == 0 && S_ISFIFO(self->sb.st_mode)) ? 1 : 0;
|
|
|
|
}
|
2003-02-16 22:19:28 +00:00
|
|
|
#elif defined(PHP_WIN32)
|
2006-11-10 13:18:35 +00:00
|
|
|
{
|
2007-04-16 08:09:56 +00:00
|
|
|
zend_uintptr_t handle = _get_osfhandle(self->fd);
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2007-04-16 08:09:56 +00:00
|
|
|
if (handle != (zend_uintptr_t)INVALID_HANDLE_VALUE) {
|
2006-11-10 13:18:35 +00:00
|
|
|
self->is_pipe = GetFileType((HANDLE)handle) == FILE_TYPE_PIPE;
|
|
|
|
}
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
if (self->is_pipe) {
|
|
|
|
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
|
|
|
|
} else {
|
2014-08-16 09:16:11 +00:00
|
|
|
stream->position = zend_ftell(file);
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STREAMS_DC)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
php_stdio_stream_data *self;
|
|
|
|
php_stream *stream;
|
|
|
|
|
|
|
|
self = emalloc_rel_orig(sizeof(*self));
|
2003-03-01 17:27:23 +00:00
|
|
|
memset(self, 0, sizeof(*self));
|
2003-02-16 22:19:28 +00:00
|
|
|
self->file = file;
|
|
|
|
self->is_pipe = 1;
|
2003-02-25 01:39:06 +00:00
|
|
|
self->lock_flag = LOCK_UN;
|
2003-02-16 22:19:28 +00:00
|
|
|
self->is_process_pipe = 1;
|
|
|
|
self->fd = fileno(file);
|
2015-03-03 23:05:28 +00:00
|
|
|
self->temp_name = NULL;
|
2015-06-30 15:37:38 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
self->is_pipe_blocking = 0;
|
2015-06-30 13:47:25 +00:00
|
|
|
#endif
|
2003-02-16 22:19:28 +00:00
|
|
|
|
|
|
|
stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
|
|
|
|
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static size_t php_stdiop_write(php_stream *stream, const char *buf, size_t count)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
|
|
|
|
|
|
|
|
assert(data != NULL);
|
|
|
|
|
|
|
|
if (data->fd >= 0) {
|
2014-10-27 16:15:26 +00:00
|
|
|
#ifdef PHP_WIN32
|
2015-08-31 19:48:14 +00:00
|
|
|
int bytes_written;
|
|
|
|
if (ZEND_SIZE_T_UINT_OVFL(count)) {
|
|
|
|
count = UINT_MAX;
|
|
|
|
}
|
|
|
|
bytes_written = _write(data->fd, buf, (unsigned int)count);
|
2014-10-27 16:15:26 +00:00
|
|
|
#else
|
2003-02-16 22:19:28 +00:00
|
|
|
int bytes_written = write(data->fd, buf, count);
|
2014-10-27 16:15:26 +00:00
|
|
|
#endif
|
2003-02-16 22:19:28 +00:00
|
|
|
if (bytes_written < 0) return 0;
|
|
|
|
return (size_t) bytes_written;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
#if HAVE_FLUSHIO
|
|
|
|
if (!data->is_pipe && data->last_op == 'r') {
|
2015-03-11 07:55:16 +00:00
|
|
|
zend_fseek(data->file, 0, SEEK_CUR);
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
data->last_op = 'w';
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return fwrite(buf, 1, count, data->file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static size_t php_stdiop_read(php_stream *stream, char *buf, size_t count)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
assert(data != NULL);
|
|
|
|
|
|
|
|
if (data->fd >= 0) {
|
Fixed bug #51800 proc_open on Windows hangs forever
This loop can block for some minutes, theoretically. Practially
however, this is a 99% non issue for a normal use case. This is
required because read() is synchronous. The PHP streams API wants
to fill its internal buffers, therefore it might try to read some
more data than user has demanded. Also, for a case where we want
to read X bytes, but neither enough data nor EOF arrives, read()
will block until it could fill the buffer. If a counterpart station
runs slowly or delivers not all the data at once, read() would
still be waiting. If we quit too early, we possibly could loose
some data from the pipe. Thus it has to emulate the read()
behaviour, but obviously not completely, just to some grade.
Reading big data amount is for sure an issue on any platforms, it
depends on the pipe buffer size, which is controlled by the system.
On Windows, the buffer size seems to be way too small, which causes
buffer congestion and a dead lock. It is essential to read the pipe
descriptors simultaneously and possibly in the same order as the
opposite writes them.
Thus, this will work with smaller buffer data sizes passed through
pipes. As MSDN states, anonymous pipes don't support asynchronous
operations. Neither anonymous pipes do support select() as they are
not SOCKETs but file descriptors. Consequently - bigger data sizes
will need a better solution based on threads. However it is much
more expencive. Maybe a better solution could be exporting a part
of the internal doing as a userspace function which could perform
some kind of lookahead operation on the pipe descriptor.
This is just the first stone, depending on the user feedback we
might go for further improvements in this area.
2014-09-29 14:24:34 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
|
|
|
|
|
2015-06-30 13:47:25 +00:00
|
|
|
if ((self->is_pipe || self->is_process_pipe) && !self->is_pipe_blocking) {
|
Fixed bug #51800 proc_open on Windows hangs forever
This loop can block for some minutes, theoretically. Practially
however, this is a 99% non issue for a normal use case. This is
required because read() is synchronous. The PHP streams API wants
to fill its internal buffers, therefore it might try to read some
more data than user has demanded. Also, for a case where we want
to read X bytes, but neither enough data nor EOF arrives, read()
will block until it could fill the buffer. If a counterpart station
runs slowly or delivers not all the data at once, read() would
still be waiting. If we quit too early, we possibly could loose
some data from the pipe. Thus it has to emulate the read()
behaviour, but obviously not completely, just to some grade.
Reading big data amount is for sure an issue on any platforms, it
depends on the pipe buffer size, which is controlled by the system.
On Windows, the buffer size seems to be way too small, which causes
buffer congestion and a dead lock. It is essential to read the pipe
descriptors simultaneously and possibly in the same order as the
opposite writes them.
Thus, this will work with smaller buffer data sizes passed through
pipes. As MSDN states, anonymous pipes don't support asynchronous
operations. Neither anonymous pipes do support select() as they are
not SOCKETs but file descriptors. Consequently - bigger data sizes
will need a better solution based on threads. However it is much
more expencive. Maybe a better solution could be exporting a part
of the internal doing as a userspace function which could perform
some kind of lookahead operation on the pipe descriptor.
This is just the first stone, depending on the user feedback we
might go for further improvements in this area.
2014-09-29 14:24:34 +00:00
|
|
|
HANDLE ph = (HANDLE)_get_osfhandle(data->fd);
|
|
|
|
int retry = 0;
|
|
|
|
DWORD avail_read = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* Look ahead to get the available data amount to read. Do the same
|
|
|
|
as read() does, however not blocking forever. In case it failed,
|
|
|
|
no data will be read (better than block). */
|
|
|
|
if (!PeekNamedPipe(ph, NULL, 0, NULL, &avail_read, NULL)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-07-02 13:19:38 +00:00
|
|
|
/* If there's nothing to read, wait in 10ms periods. */
|
Fixed bug #51800 proc_open on Windows hangs forever
This loop can block for some minutes, theoretically. Practially
however, this is a 99% non issue for a normal use case. This is
required because read() is synchronous. The PHP streams API wants
to fill its internal buffers, therefore it might try to read some
more data than user has demanded. Also, for a case where we want
to read X bytes, but neither enough data nor EOF arrives, read()
will block until it could fill the buffer. If a counterpart station
runs slowly or delivers not all the data at once, read() would
still be waiting. If we quit too early, we possibly could loose
some data from the pipe. Thus it has to emulate the read()
behaviour, but obviously not completely, just to some grade.
Reading big data amount is for sure an issue on any platforms, it
depends on the pipe buffer size, which is controlled by the system.
On Windows, the buffer size seems to be way too small, which causes
buffer congestion and a dead lock. It is essential to read the pipe
descriptors simultaneously and possibly in the same order as the
opposite writes them.
Thus, this will work with smaller buffer data sizes passed through
pipes. As MSDN states, anonymous pipes don't support asynchronous
operations. Neither anonymous pipes do support select() as they are
not SOCKETs but file descriptors. Consequently - bigger data sizes
will need a better solution based on threads. However it is much
more expencive. Maybe a better solution could be exporting a part
of the internal doing as a userspace function which could perform
some kind of lookahead operation on the pipe descriptor.
This is just the first stone, depending on the user feedback we
might go for further improvements in this area.
2014-09-29 14:24:34 +00:00
|
|
|
if (0 == avail_read) {
|
2015-07-02 13:19:38 +00:00
|
|
|
usleep(10);
|
Fixed bug #51800 proc_open on Windows hangs forever
This loop can block for some minutes, theoretically. Practially
however, this is a 99% non issue for a normal use case. This is
required because read() is synchronous. The PHP streams API wants
to fill its internal buffers, therefore it might try to read some
more data than user has demanded. Also, for a case where we want
to read X bytes, but neither enough data nor EOF arrives, read()
will block until it could fill the buffer. If a counterpart station
runs slowly or delivers not all the data at once, read() would
still be waiting. If we quit too early, we possibly could loose
some data from the pipe. Thus it has to emulate the read()
behaviour, but obviously not completely, just to some grade.
Reading big data amount is for sure an issue on any platforms, it
depends on the pipe buffer size, which is controlled by the system.
On Windows, the buffer size seems to be way too small, which causes
buffer congestion and a dead lock. It is essential to read the pipe
descriptors simultaneously and possibly in the same order as the
opposite writes them.
Thus, this will work with smaller buffer data sizes passed through
pipes. As MSDN states, anonymous pipes don't support asynchronous
operations. Neither anonymous pipes do support select() as they are
not SOCKETs but file descriptors. Consequently - bigger data sizes
will need a better solution based on threads. However it is much
more expencive. Maybe a better solution could be exporting a part
of the internal doing as a userspace function which could perform
some kind of lookahead operation on the pipe descriptor.
This is just the first stone, depending on the user feedback we
might go for further improvements in this area.
2014-09-29 14:24:34 +00:00
|
|
|
}
|
2015-07-02 13:19:38 +00:00
|
|
|
} while (0 == avail_read && retry++ < 3200000);
|
Fixed bug #51800 proc_open on Windows hangs forever
This loop can block for some minutes, theoretically. Practially
however, this is a 99% non issue for a normal use case. This is
required because read() is synchronous. The PHP streams API wants
to fill its internal buffers, therefore it might try to read some
more data than user has demanded. Also, for a case where we want
to read X bytes, but neither enough data nor EOF arrives, read()
will block until it could fill the buffer. If a counterpart station
runs slowly or delivers not all the data at once, read() would
still be waiting. If we quit too early, we possibly could loose
some data from the pipe. Thus it has to emulate the read()
behaviour, but obviously not completely, just to some grade.
Reading big data amount is for sure an issue on any platforms, it
depends on the pipe buffer size, which is controlled by the system.
On Windows, the buffer size seems to be way too small, which causes
buffer congestion and a dead lock. It is essential to read the pipe
descriptors simultaneously and possibly in the same order as the
opposite writes them.
Thus, this will work with smaller buffer data sizes passed through
pipes. As MSDN states, anonymous pipes don't support asynchronous
operations. Neither anonymous pipes do support select() as they are
not SOCKETs but file descriptors. Consequently - bigger data sizes
will need a better solution based on threads. However it is much
more expencive. Maybe a better solution could be exporting a part
of the internal doing as a userspace function which could perform
some kind of lookahead operation on the pipe descriptor.
This is just the first stone, depending on the user feedback we
might go for further improvements in this area.
2014-09-29 14:24:34 +00:00
|
|
|
|
|
|
|
/* Reduce the required data amount to what is available, otherwise read()
|
|
|
|
will block.*/
|
|
|
|
if (avail_read < count) {
|
|
|
|
count = avail_read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-08-16 09:16:11 +00:00
|
|
|
ret = read(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
|
2007-02-23 23:09:14 +00:00
|
|
|
|
|
|
|
if (ret == (size_t)-1 && errno == EINTR) {
|
|
|
|
/* Read was interrupted, retry once,
|
|
|
|
If read still fails, giveup with feof==0
|
|
|
|
so script can retry if desired */
|
2014-08-16 09:16:11 +00:00
|
|
|
ret = read(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
|
2007-02-23 23:09:14 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2007-07-25 16:34:06 +00:00
|
|
|
stream->eof = (ret == 0 || (ret == (size_t)-1 && errno != EWOULDBLOCK && errno != EINTR && errno != EBADF));
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
} else {
|
|
|
|
#if HAVE_FLUSHIO
|
|
|
|
if (!data->is_pipe && data->last_op == 'w')
|
2014-08-16 09:16:11 +00:00
|
|
|
zend_fseek(data->file, 0, SEEK_CUR);
|
2003-02-16 22:19:28 +00:00
|
|
|
data->last_op = 'r';
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = fread(buf, 1, count, data->file);
|
|
|
|
|
2003-10-08 10:55:51 +00:00
|
|
|
stream->eof = feof(data->file);
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_stdiop_close(php_stream *stream, int close_handle)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
|
|
|
|
|
|
|
|
assert(data != NULL);
|
2003-02-27 17:43:38 +00:00
|
|
|
|
|
|
|
#if HAVE_MMAP
|
|
|
|
if (data->last_mapped_addr) {
|
|
|
|
munmap(data->last_mapped_addr, data->last_mapped_len);
|
|
|
|
data->last_mapped_addr = NULL;
|
|
|
|
}
|
2003-02-28 01:47:28 +00:00
|
|
|
#elif defined(PHP_WIN32)
|
|
|
|
if (data->last_mapped_addr) {
|
|
|
|
UnmapViewOfFile(data->last_mapped_addr);
|
|
|
|
data->last_mapped_addr = NULL;
|
|
|
|
}
|
|
|
|
if (data->file_mapping) {
|
|
|
|
CloseHandle(data->file_mapping);
|
|
|
|
data->file_mapping = NULL;
|
|
|
|
}
|
2003-02-27 17:43:38 +00:00
|
|
|
#endif
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
if (close_handle) {
|
|
|
|
if (data->file) {
|
|
|
|
if (data->is_process_pipe) {
|
|
|
|
errno = 0;
|
|
|
|
ret = pclose(data->file);
|
|
|
|
|
|
|
|
#if HAVE_SYS_WAIT_H
|
|
|
|
if (WIFEXITED(ret)) {
|
|
|
|
ret = WEXITSTATUS(ret);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
ret = fclose(data->file);
|
|
|
|
data->file = NULL;
|
|
|
|
}
|
|
|
|
} else if (data->fd != -1) {
|
|
|
|
ret = close(data->fd);
|
|
|
|
data->fd = -1;
|
|
|
|
} else {
|
|
|
|
return 0; /* everything should be closed already -> success */
|
|
|
|
}
|
2015-03-03 23:05:28 +00:00
|
|
|
if (data->temp_name) {
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
php_win32_ioutil_unlink(ZSTR_VAL(data->temp_name));
|
|
|
|
#else
|
2015-06-30 10:59:27 +00:00
|
|
|
unlink(ZSTR_VAL(data->temp_name));
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
#endif
|
2003-03-18 23:37:54 +00:00
|
|
|
/* temporary streams are never persistent */
|
2015-03-03 23:05:28 +00:00
|
|
|
zend_string_release(data->temp_name);
|
|
|
|
data->temp_name = NULL;
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
data->file = NULL;
|
|
|
|
data->fd = -1;
|
|
|
|
}
|
|
|
|
|
2003-03-18 23:37:54 +00:00
|
|
|
pefree(data, stream->is_persistent);
|
2003-02-16 22:19:28 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_stdiop_flush(php_stream *stream)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
|
|
|
|
|
|
|
|
assert(data != NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* stdio buffers data in user land. By calling fflush(3), this
|
|
|
|
* data is send to the kernel using write(2). fsync'ing is
|
|
|
|
* something completely different.
|
|
|
|
*/
|
|
|
|
if (data->file) {
|
|
|
|
return fflush(data->file);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_stdiop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
assert(data != NULL);
|
|
|
|
|
|
|
|
if (data->is_pipe) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "cannot seek on a pipe");
|
2003-02-16 22:19:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->fd >= 0) {
|
2014-08-16 09:16:11 +00:00
|
|
|
zend_off_t result;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-08-16 09:16:11 +00:00
|
|
|
result = zend_lseek(data->fd, offset, whence);
|
|
|
|
if (result == (zend_off_t)-1)
|
2003-02-16 22:19:28 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
*newoffset = result;
|
|
|
|
return 0;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
} else {
|
2014-08-16 09:16:11 +00:00
|
|
|
ret = zend_fseek(data->file, offset, whence);
|
|
|
|
*newoffset = zend_ftell(data->file);
|
2003-02-16 22:19:28 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_stdiop_cast(php_stream *stream, int castas, void **ret)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
2014-03-19 05:26:54 +00:00
|
|
|
php_socket_t fd;
|
2003-02-16 22:19:28 +00:00
|
|
|
php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
|
|
|
|
|
|
|
|
assert(data != NULL);
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
/* as soon as someone touches the stdio layer, buffering may ensue,
|
|
|
|
* so we need to stop using the fd directly in that case */
|
|
|
|
|
|
|
|
switch (castas) {
|
|
|
|
case PHP_STREAM_AS_STDIO:
|
|
|
|
if (ret) {
|
|
|
|
|
|
|
|
if (data->file == NULL) {
|
|
|
|
/* we were opened as a plain file descriptor, so we
|
|
|
|
* need fdopen now */
|
2010-11-05 01:29:08 +00:00
|
|
|
char fixed_mode[5];
|
2010-11-05 18:53:48 +00:00
|
|
|
php_stream_mode_sanitize_fdopen_fopencookie(stream, fixed_mode);
|
2010-11-05 01:29:08 +00:00
|
|
|
data->file = fdopen(data->fd, fixed_mode);
|
2005-04-07 07:29:14 +00:00
|
|
|
if (data->file == NULL) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-03-15 13:29:56 +00:00
|
|
|
*(FILE**)ret = data->file;
|
2014-03-19 05:26:54 +00:00
|
|
|
data->fd = SOCK_ERR;
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
|
2003-06-28 11:24:47 +00:00
|
|
|
case PHP_STREAM_AS_FD_FOR_SELECT:
|
|
|
|
PHP_STDIOP_GET_FD(fd, data);
|
2014-03-19 05:26:54 +00:00
|
|
|
if (SOCK_ERR == fd) {
|
2003-06-28 11:24:47 +00:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
if (ret) {
|
2014-03-19 05:26:54 +00:00
|
|
|
*(php_socket_t *)ret = fd;
|
2003-06-28 11:24:47 +00:00
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
case PHP_STREAM_AS_FD:
|
|
|
|
PHP_STDIOP_GET_FD(fd, data);
|
|
|
|
|
2014-03-19 05:26:54 +00:00
|
|
|
if (SOCK_ERR == fd) {
|
2003-02-16 22:19:28 +00:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
if (data->file) {
|
|
|
|
fflush(data->file);
|
|
|
|
}
|
|
|
|
if (ret) {
|
2014-03-19 05:26:54 +00:00
|
|
|
*(php_socket_t *)ret = fd;
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
default:
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_stdiop_stat(php_stream *stream, php_stream_statbuf *ssb)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
2004-07-31 10:09:25 +00:00
|
|
|
int ret;
|
2003-02-16 22:19:28 +00:00
|
|
|
php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
|
|
|
|
|
|
|
|
assert(data != NULL);
|
2015-01-27 13:18:17 +00:00
|
|
|
if((ret = do_fstat(data, 1)) == 0) {
|
|
|
|
memcpy(&ssb->sb, &data->sb, sizeof(ssb->sb));
|
|
|
|
}
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2004-07-31 10:09:25 +00:00
|
|
|
return ret;
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_stdiop_set_option(php_stream *stream, int option, int value, void *ptrparam)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
|
|
|
|
size_t size;
|
|
|
|
int fd;
|
|
|
|
#ifdef O_NONBLOCK
|
|
|
|
/* FIXME: make this work for win32 */
|
|
|
|
int flags;
|
|
|
|
int oldval;
|
|
|
|
#endif
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
PHP_STDIOP_GET_FD(fd, data);
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
switch(option) {
|
|
|
|
case PHP_STREAM_OPTION_BLOCKING:
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
#ifdef O_NONBLOCK
|
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
oldval = (flags & O_NONBLOCK) ? 0 : 1;
|
|
|
|
if (value)
|
2005-11-17 14:19:40 +00:00
|
|
|
flags &= ~O_NONBLOCK;
|
2003-02-16 22:19:28 +00:00
|
|
|
else
|
|
|
|
flags |= O_NONBLOCK;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
if (-1 == fcntl(fd, F_SETFL, flags))
|
|
|
|
return -1;
|
|
|
|
return oldval;
|
|
|
|
#else
|
|
|
|
return -1; /* not yet implemented */
|
|
|
|
#endif
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
case PHP_STREAM_OPTION_WRITE_BUFFER:
|
|
|
|
|
|
|
|
if (data->file == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
if (ptrparam)
|
|
|
|
size = *(size_t *)ptrparam;
|
|
|
|
else
|
|
|
|
size = BUFSIZ;
|
|
|
|
|
|
|
|
switch(value) {
|
|
|
|
case PHP_STREAM_BUFFER_NONE:
|
|
|
|
return setvbuf(data->file, NULL, _IONBF, 0);
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
case PHP_STREAM_BUFFER_LINE:
|
|
|
|
return setvbuf(data->file, NULL, _IOLBF, size);
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
case PHP_STREAM_BUFFER_FULL:
|
|
|
|
return setvbuf(data->file, NULL, _IOFBF, size);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-25 01:39:06 +00:00
|
|
|
case PHP_STREAM_OPTION_LOCKING:
|
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-04-16 08:09:56 +00:00
|
|
|
if ((zend_uintptr_t) ptrparam == PHP_STREAM_LOCK_SUPPORTED) {
|
2003-02-25 01:39:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-06 13:57:30 +00:00
|
|
|
if (!flock(fd, value)) {
|
2003-02-25 01:39:06 +00:00
|
|
|
data->lock_flag = value;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-02-27 17:43:38 +00:00
|
|
|
case PHP_STREAM_OPTION_MMAP_API:
|
|
|
|
#if HAVE_MMAP
|
|
|
|
{
|
|
|
|
php_stream_mmap_range *range = (php_stream_mmap_range*)ptrparam;
|
|
|
|
int prot, flags;
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-27 17:43:38 +00:00
|
|
|
switch (value) {
|
|
|
|
case PHP_STREAM_MMAP_SUPPORTED:
|
|
|
|
return fd == -1 ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
|
|
|
|
|
|
|
|
case PHP_STREAM_MMAP_MAP_RANGE:
|
2015-01-27 13:18:17 +00:00
|
|
|
if(do_fstat(data, 1) != 0) {
|
|
|
|
return PHP_STREAM_OPTION_RETURN_ERR;
|
|
|
|
}
|
2006-11-29 11:12:59 +00:00
|
|
|
if (range->length == 0 && range->offset > 0 && range->offset < data->sb.st_size) {
|
|
|
|
range->length = data->sb.st_size - range->offset;
|
|
|
|
}
|
2004-07-31 10:09:25 +00:00
|
|
|
if (range->length == 0 || range->length > data->sb.st_size) {
|
|
|
|
range->length = data->sb.st_size;
|
2003-02-27 17:43:38 +00:00
|
|
|
}
|
2006-11-29 11:12:59 +00:00
|
|
|
if (range->offset >= data->sb.st_size) {
|
|
|
|
range->offset = data->sb.st_size;
|
|
|
|
range->length = 0;
|
|
|
|
}
|
2003-02-27 17:43:38 +00:00
|
|
|
switch (range->mode) {
|
|
|
|
case PHP_STREAM_MAP_MODE_READONLY:
|
|
|
|
prot = PROT_READ;
|
|
|
|
flags = MAP_PRIVATE;
|
|
|
|
break;
|
|
|
|
case PHP_STREAM_MAP_MODE_READWRITE:
|
|
|
|
prot = PROT_READ | PROT_WRITE;
|
|
|
|
flags = MAP_PRIVATE;
|
|
|
|
break;
|
|
|
|
case PHP_STREAM_MAP_MODE_SHARED_READONLY:
|
|
|
|
prot = PROT_READ;
|
|
|
|
flags = MAP_SHARED;
|
|
|
|
break;
|
|
|
|
case PHP_STREAM_MAP_MODE_SHARED_READWRITE:
|
|
|
|
prot = PROT_READ | PROT_WRITE;
|
|
|
|
flags = MAP_SHARED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return PHP_STREAM_OPTION_RETURN_ERR;
|
|
|
|
}
|
|
|
|
range->mapped = (char*)mmap(NULL, range->length, prot, flags, fd, range->offset);
|
|
|
|
if (range->mapped == (char*)MAP_FAILED) {
|
|
|
|
range->mapped = NULL;
|
|
|
|
return PHP_STREAM_OPTION_RETURN_ERR;
|
|
|
|
}
|
|
|
|
/* remember the mapping */
|
|
|
|
data->last_mapped_addr = range->mapped;
|
|
|
|
data->last_mapped_len = range->length;
|
|
|
|
return PHP_STREAM_OPTION_RETURN_OK;
|
|
|
|
|
|
|
|
case PHP_STREAM_MMAP_UNMAP:
|
|
|
|
if (data->last_mapped_addr) {
|
|
|
|
munmap(data->last_mapped_addr, data->last_mapped_len);
|
|
|
|
data->last_mapped_addr = NULL;
|
|
|
|
|
|
|
|
return PHP_STREAM_OPTION_RETURN_OK;
|
|
|
|
}
|
|
|
|
return PHP_STREAM_OPTION_RETURN_ERR;
|
|
|
|
}
|
|
|
|
}
|
2003-02-28 01:47:28 +00:00
|
|
|
#elif defined(PHP_WIN32)
|
|
|
|
{
|
|
|
|
php_stream_mmap_range *range = (php_stream_mmap_range*)ptrparam;
|
|
|
|
HANDLE hfile = (HANDLE)_get_osfhandle(fd);
|
|
|
|
DWORD prot, acc, loffs = 0, delta = 0;
|
|
|
|
|
|
|
|
switch (value) {
|
|
|
|
case PHP_STREAM_MMAP_SUPPORTED:
|
|
|
|
return hfile == INVALID_HANDLE_VALUE ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
|
|
|
|
|
|
|
|
case PHP_STREAM_MMAP_MAP_RANGE:
|
|
|
|
switch (range->mode) {
|
|
|
|
case PHP_STREAM_MAP_MODE_READONLY:
|
|
|
|
prot = PAGE_READONLY;
|
|
|
|
acc = FILE_MAP_READ;
|
|
|
|
break;
|
|
|
|
case PHP_STREAM_MAP_MODE_READWRITE:
|
|
|
|
prot = PAGE_READWRITE;
|
|
|
|
acc = FILE_MAP_READ | FILE_MAP_WRITE;
|
|
|
|
break;
|
|
|
|
case PHP_STREAM_MAP_MODE_SHARED_READONLY:
|
|
|
|
prot = PAGE_READONLY;
|
|
|
|
acc = FILE_MAP_READ;
|
|
|
|
/* TODO: we should assign a name for the mapping */
|
|
|
|
break;
|
|
|
|
case PHP_STREAM_MAP_MODE_SHARED_READWRITE:
|
|
|
|
prot = PAGE_READWRITE;
|
|
|
|
acc = FILE_MAP_READ | FILE_MAP_WRITE;
|
|
|
|
/* TODO: we should assign a name for the mapping */
|
|
|
|
break;
|
2011-01-20 07:38:00 +00:00
|
|
|
default:
|
|
|
|
return PHP_STREAM_OPTION_RETURN_ERR;
|
2003-02-28 01:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create a mapping capable of viewing the whole file (this costs no real resources) */
|
|
|
|
data->file_mapping = CreateFileMapping(hfile, NULL, prot, 0, 0, NULL);
|
|
|
|
|
|
|
|
if (data->file_mapping == NULL) {
|
|
|
|
return PHP_STREAM_OPTION_RETURN_ERR;
|
|
|
|
}
|
|
|
|
|
2007-04-18 13:53:19 +00:00
|
|
|
size = GetFileSize(hfile, NULL);
|
|
|
|
if (range->length == 0 && range->offset > 0 && range->offset < size) {
|
|
|
|
range->length = size - range->offset;
|
|
|
|
}
|
|
|
|
if (range->length == 0 || range->length > size) {
|
|
|
|
range->length = size;
|
|
|
|
}
|
|
|
|
if (range->offset >= size) {
|
|
|
|
range->offset = size;
|
|
|
|
range->length = 0;
|
2003-02-28 01:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* figure out how big a chunk to map to be able to view the part that we need */
|
|
|
|
if (range->offset != 0) {
|
|
|
|
SYSTEM_INFO info;
|
|
|
|
DWORD gran;
|
|
|
|
|
|
|
|
GetSystemInfo(&info);
|
|
|
|
gran = info.dwAllocationGranularity;
|
2014-10-27 16:15:26 +00:00
|
|
|
loffs = ((DWORD)range->offset / gran) * gran;
|
|
|
|
delta = (DWORD)range->offset - loffs;
|
2003-02-28 01:47:28 +00:00
|
|
|
}
|
|
|
|
|
2006-06-05 22:19:36 +00:00
|
|
|
data->last_mapped_addr = MapViewOfFile(data->file_mapping, acc, 0, loffs, range->length + delta);
|
2003-02-28 01:47:28 +00:00
|
|
|
|
|
|
|
if (data->last_mapped_addr) {
|
|
|
|
/* give them back the address of the start offset they requested */
|
|
|
|
range->mapped = data->last_mapped_addr + delta;
|
|
|
|
return PHP_STREAM_OPTION_RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(data->file_mapping);
|
|
|
|
data->file_mapping = NULL;
|
|
|
|
|
|
|
|
return PHP_STREAM_OPTION_RETURN_ERR;
|
|
|
|
|
|
|
|
case PHP_STREAM_MMAP_UNMAP:
|
|
|
|
if (data->last_mapped_addr) {
|
|
|
|
UnmapViewOfFile(data->last_mapped_addr);
|
|
|
|
data->last_mapped_addr = NULL;
|
|
|
|
CloseHandle(data->file_mapping);
|
|
|
|
data->file_mapping = NULL;
|
|
|
|
return PHP_STREAM_OPTION_RETURN_OK;
|
|
|
|
}
|
|
|
|
return PHP_STREAM_OPTION_RETURN_ERR;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return PHP_STREAM_OPTION_RETURN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-27 17:43:38 +00:00
|
|
|
#endif
|
|
|
|
return PHP_STREAM_OPTION_RETURN_NOTIMPL;
|
|
|
|
|
|
|
|
case PHP_STREAM_OPTION_TRUNCATE_API:
|
|
|
|
switch (value) {
|
|
|
|
case PHP_STREAM_TRUNCATE_SUPPORTED:
|
|
|
|
return fd == -1 ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
|
|
|
|
|
2007-04-18 14:23:06 +00:00
|
|
|
case PHP_STREAM_TRUNCATE_SET_SIZE: {
|
|
|
|
ptrdiff_t new_size = *(ptrdiff_t*)ptrparam;
|
|
|
|
if (new_size < 0) {
|
|
|
|
return PHP_STREAM_OPTION_RETURN_ERR;
|
|
|
|
}
|
|
|
|
return ftruncate(fd, new_size) == 0 ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
|
|
|
|
}
|
2003-02-27 17:43:38 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2015-06-30 13:47:25 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
case PHP_STREAM_OPTION_PIPE_BLOCKING:
|
|
|
|
data->is_pipe_blocking = value;
|
|
|
|
return PHP_STREAM_OPTION_RETURN_OK;
|
|
|
|
#endif
|
2016-08-17 08:54:21 +00:00
|
|
|
case PHP_STREAM_OPTION_META_DATA_API:
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
#ifdef O_NONBLOCK
|
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
2015-06-30 13:47:25 +00:00
|
|
|
|
2016-08-17 08:54:21 +00:00
|
|
|
add_assoc_bool((zval*)ptrparam, "timed_out", 0);
|
|
|
|
add_assoc_bool((zval*)ptrparam, "blocked", (flags & O_NONBLOCK)? 0 : 1);
|
|
|
|
add_assoc_bool((zval*)ptrparam, "eof", stream->eof);
|
|
|
|
|
|
|
|
return PHP_STREAM_OPTION_RETURN_OK;
|
|
|
|
#endif
|
|
|
|
return -1;
|
2003-02-16 22:19:28 +00:00
|
|
|
default:
|
2003-02-27 17:43:38 +00:00
|
|
|
return PHP_STREAM_OPTION_RETURN_NOTIMPL;
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PHPAPI php_stream_ops php_stream_stdio_ops = {
|
|
|
|
php_stdiop_write, php_stdiop_read,
|
|
|
|
php_stdiop_close, php_stdiop_flush,
|
|
|
|
"STDIO",
|
|
|
|
php_stdiop_seek,
|
|
|
|
php_stdiop_cast,
|
|
|
|
php_stdiop_stat,
|
|
|
|
php_stdiop_set_option
|
|
|
|
};
|
|
|
|
/* }}} */
|
|
|
|
|
2003-02-27 17:43:38 +00:00
|
|
|
/* {{{ plain files opendir/readdir implementation */
|
2014-12-13 22:06:14 +00:00
|
|
|
static size_t php_plain_files_dirstream_read(php_stream *stream, char *buf, size_t count)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
DIR *dir = (DIR*)stream->abstract;
|
|
|
|
/* avoid libc5 readdir problems */
|
|
|
|
char entry[sizeof(struct dirent)+MAXPATHLEN];
|
|
|
|
struct dirent *result = (struct dirent *)&entry;
|
|
|
|
php_stream_dirent *ent = (php_stream_dirent*)buf;
|
|
|
|
|
|
|
|
/* avoid problems if someone mis-uses the stream */
|
|
|
|
if (count != sizeof(php_stream_dirent))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (php_readdir_r(dir, (struct dirent *)entry, &result) == 0 && result) {
|
|
|
|
PHP_STRLCPY(ent->d_name, result->d_name, sizeof(ent->d_name), strlen(result->d_name));
|
|
|
|
return sizeof(php_stream_dirent);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_plain_files_dirstream_close(php_stream *stream, int close_handle)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
return closedir((DIR *)stream->abstract);
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_plain_files_dirstream_rewind(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
rewinddir((DIR *)stream->abstract);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static php_stream_ops php_plain_files_dirstream_ops = {
|
|
|
|
NULL, php_plain_files_dirstream_read,
|
|
|
|
php_plain_files_dirstream_close, NULL,
|
|
|
|
"dir",
|
|
|
|
php_plain_files_dirstream_rewind,
|
|
|
|
NULL, /* cast */
|
|
|
|
NULL, /* stat */
|
|
|
|
NULL /* set_option */
|
|
|
|
};
|
|
|
|
|
2013-07-30 10:49:36 +00:00
|
|
|
static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, const char *path, const char *mode,
|
2015-03-03 23:05:28 +00:00
|
|
|
int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
DIR *dir = NULL;
|
|
|
|
php_stream *stream = NULL;
|
|
|
|
|
2011-02-04 10:47:41 +00:00
|
|
|
#ifdef HAVE_GLOB
|
2007-11-06 12:12:58 +00:00
|
|
|
if (options & STREAM_USE_GLOB_DIR_OPEN) {
|
2014-12-13 22:06:14 +00:00
|
|
|
return php_glob_stream_wrapper.wops->dir_opener(&php_glob_stream_wrapper, path, mode, options, opened_path, context STREAMS_REL_CC);
|
2007-11-06 12:12:58 +00:00
|
|
|
}
|
2011-02-04 10:47:41 +00:00
|
|
|
#endif
|
2007-11-06 12:12:58 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path)) {
|
2003-02-16 22:19:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
dir = VCWD_OPENDIR(path);
|
|
|
|
|
|
|
|
#ifdef PHP_WIN32
|
2010-02-21 17:44:25 +00:00
|
|
|
if (!dir) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_win32_docref2_from_error(GetLastError(), path, path);
|
2010-02-21 17:44:25 +00:00
|
|
|
}
|
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
if (dir && dir->finished) {
|
|
|
|
closedir(dir);
|
|
|
|
dir = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (dir) {
|
|
|
|
stream = php_stream_alloc(&php_plain_files_dirstream_ops, dir, 0, mode);
|
|
|
|
if (stream == NULL)
|
|
|
|
closedir(dir);
|
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
return stream;
|
|
|
|
}
|
2003-02-27 17:43:38 +00:00
|
|
|
/* }}} */
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2004-07-31 10:09:25 +00:00
|
|
|
/* {{{ php_stream_fopen */
|
2015-03-03 23:05:28 +00:00
|
|
|
PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, zend_string **opened_path, int options STREAMS_DC)
|
2004-07-31 10:09:25 +00:00
|
|
|
{
|
2015-03-03 23:05:28 +00:00
|
|
|
char realpath[MAXPATHLEN];
|
2004-07-31 10:09:25 +00:00
|
|
|
int open_flags;
|
|
|
|
int fd;
|
|
|
|
php_stream *ret;
|
|
|
|
int persistent = options & STREAM_OPEN_PERSISTENT;
|
|
|
|
char *persistent_id = NULL;
|
|
|
|
|
|
|
|
if (FAILURE == php_stream_parse_fopen_modes(mode, &open_flags)) {
|
|
|
|
if (options & REPORT_ERRORS) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "`%s' is not a valid mode for fopen", mode);
|
2004-07-31 10:09:25 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-27 10:33:40 +00:00
|
|
|
|
|
|
|
if (options & STREAM_ASSUME_REALPATH) {
|
2015-03-03 23:05:28 +00:00
|
|
|
strlcpy(realpath, filename, sizeof(realpath));
|
2008-03-27 10:33:40 +00:00
|
|
|
} else {
|
2015-03-03 23:05:28 +00:00
|
|
|
if (expand_filepath(filename, realpath) == NULL) {
|
2008-03-27 10:33:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-07-31 10:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (persistent) {
|
|
|
|
spprintf(&persistent_id, 0, "streams_stdio_%d_%s", open_flags, realpath);
|
2014-12-13 22:06:14 +00:00
|
|
|
switch (php_stream_from_persistent_id(persistent_id, &ret)) {
|
2004-07-31 10:09:25 +00:00
|
|
|
case PHP_STREAM_PERSISTENT_SUCCESS:
|
|
|
|
if (opened_path) {
|
2015-03-03 23:05:28 +00:00
|
|
|
//TODO: avoid reallocation???
|
|
|
|
*opened_path = zend_string_init(realpath, strlen(realpath), 0);
|
2004-07-31 10:09:25 +00:00
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case PHP_STREAM_PERSISTENT_FAILURE:
|
2017-06-25 21:24:23 +00:00
|
|
|
efree(persistent_id);
|
2004-07-31 10:09:25 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
fd = php_win32_ioutil_open(realpath, open_flags, 0666);
|
|
|
|
#else
|
2004-07-31 10:09:25 +00:00
|
|
|
fd = open(realpath, open_flags, 0666);
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
#endif
|
2004-07-31 10:09:25 +00:00
|
|
|
if (fd != -1) {
|
|
|
|
|
2006-11-10 13:18:35 +00:00
|
|
|
if (options & STREAM_OPEN_FOR_INCLUDE) {
|
|
|
|
ret = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);
|
|
|
|
} else {
|
|
|
|
ret = php_stream_fopen_from_fd_rel(fd, mode, persistent_id);
|
|
|
|
}
|
2004-07-31 10:09:25 +00:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
if (opened_path) {
|
2015-03-03 23:05:28 +00:00
|
|
|
*opened_path = zend_string_init(realpath, strlen(realpath), 0);
|
2004-07-31 10:09:25 +00:00
|
|
|
}
|
|
|
|
if (persistent_id) {
|
|
|
|
efree(persistent_id);
|
|
|
|
}
|
|
|
|
|
2006-11-10 10:55:26 +00:00
|
|
|
/* WIN32 always set ISREG flag */
|
|
|
|
#ifndef PHP_WIN32
|
2004-07-31 10:09:25 +00:00
|
|
|
/* sanity checks for include/require.
|
|
|
|
* We check these after opening the stream, so that we save
|
|
|
|
* on fstat() syscalls */
|
|
|
|
if (options & STREAM_OPEN_FOR_INCLUDE) {
|
|
|
|
php_stdio_stream_data *self = (php_stdio_stream_data*)ret->abstract;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = do_fstat(self, 0);
|
2006-11-10 10:55:26 +00:00
|
|
|
if ((r == 0 && !S_ISREG(self->sb.st_mode))) {
|
2007-02-13 23:39:14 +00:00
|
|
|
if (opened_path) {
|
2015-03-03 23:05:28 +00:00
|
|
|
zend_string_release(*opened_path);
|
2007-02-13 23:39:14 +00:00
|
|
|
*opened_path = NULL;
|
|
|
|
}
|
2004-07-31 10:09:25 +00:00
|
|
|
php_stream_close(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-06-30 13:47:25 +00:00
|
|
|
|
|
|
|
if (options & STREAM_USE_BLOCKING_PIPE) {
|
|
|
|
php_stdio_stream_data *self = (php_stdio_stream_data*)ret->abstract;
|
|
|
|
self->is_pipe_blocking = 1;
|
|
|
|
}
|
2006-11-10 10:55:26 +00:00
|
|
|
#endif
|
2004-07-31 10:09:25 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
if (persistent_id) {
|
|
|
|
efree(persistent_id);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2013-07-30 10:49:36 +00:00
|
|
|
static php_stream *php_plain_files_stream_opener(php_stream_wrapper *wrapper, const char *path, const char *mode,
|
2015-03-03 23:05:28 +00:00
|
|
|
int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
2014-12-13 22:06:14 +00:00
|
|
|
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path)) {
|
2003-02-16 22:19:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return php_stream_fopen_rel(path, mode, opened_path, options);
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_plain_files_url_stater(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
2014-11-01 18:49:21 +00:00
|
|
|
if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
|
|
|
|
url += sizeof("file://") - 1;
|
2003-12-01 19:47:05 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (php_check_open_basedir_ex(url, (flags & PHP_STREAM_URL_STAT_QUIET) ? 0 : 1)) {
|
2003-12-01 19:47:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-01 09:49:53 +00:00
|
|
|
#ifdef PHP_WIN32
|
2015-04-04 18:39:04 +00:00
|
|
|
if (flags & PHP_STREAM_URL_STAT_LINK) {
|
|
|
|
return VCWD_LSTAT(url, &ssb->sb);
|
2010-09-01 09:49:53 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_SYMLINK
|
2003-11-28 23:25:28 +00:00
|
|
|
if (flags & PHP_STREAM_URL_STAT_LINK) {
|
|
|
|
return VCWD_LSTAT(url, &ssb->sb);
|
|
|
|
} else
|
2010-09-01 09:49:53 +00:00
|
|
|
# endif
|
2003-11-28 23:25:28 +00:00
|
|
|
#endif
|
|
|
|
return VCWD_STAT(url, &ssb->sb);
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_plain_files_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
|
2003-05-14 06:10:04 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2014-11-01 18:49:21 +00:00
|
|
|
if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
|
|
|
|
url += sizeof("file://") - 1;
|
2003-05-14 06:10:04 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (php_check_open_basedir(url)) {
|
2011-07-31 22:43:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-05-14 06:10:04 +00:00
|
|
|
ret = VCWD_UNLINK(url);
|
|
|
|
if (ret == -1) {
|
2003-05-14 15:12:07 +00:00
|
|
|
if (options & REPORT_ERRORS) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
|
2003-05-14 15:12:07 +00:00
|
|
|
}
|
2003-05-14 06:10:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-10-31 13:22:45 +00:00
|
|
|
|
2008-08-07 09:25:33 +00:00
|
|
|
/* Clear stat cache (and realpath cache) */
|
2014-12-13 22:06:14 +00:00
|
|
|
php_clear_stat_cache(1, NULL, 0);
|
2007-10-31 13:22:45 +00:00
|
|
|
|
2003-05-14 06:10:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_from, const char *url_to, int options, php_stream_context *context)
|
2003-12-12 23:06:42 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!url_from || !url_to) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-27 01:18:43 +00:00
|
|
|
#ifdef PHP_WIN32
|
2017-07-09 13:31:05 +00:00
|
|
|
if (!php_win32_check_trailing_space(url_from, strlen(url_from))) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to);
|
2010-02-07 20:15:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-07-09 13:31:05 +00:00
|
|
|
if (!php_win32_check_trailing_space(url_to, strlen(url_to))) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to);
|
2010-02-07 20:15:58 +00:00
|
|
|
return 0;
|
2010-01-27 01:18:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-01 18:49:21 +00:00
|
|
|
if (strncasecmp(url_from, "file://", sizeof("file://") - 1) == 0) {
|
|
|
|
url_from += sizeof("file://") - 1;
|
2003-12-12 23:06:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-01 18:49:21 +00:00
|
|
|
if (strncasecmp(url_to, "file://", sizeof("file://") - 1) == 0) {
|
|
|
|
url_to += sizeof("file://") - 1;
|
2003-12-12 23:06:42 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (php_check_open_basedir(url_from) || php_check_open_basedir(url_to)) {
|
2003-12-12 23:06:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-01-27 02:03:41 +00:00
|
|
|
|
2003-12-12 23:06:42 +00:00
|
|
|
ret = VCWD_RENAME(url_from, url_to);
|
|
|
|
|
|
|
|
if (ret == -1) {
|
2010-01-26 20:04:03 +00:00
|
|
|
#ifndef PHP_WIN32
|
|
|
|
# ifdef EXDEV
|
2003-12-12 23:06:42 +00:00
|
|
|
if (errno == EXDEV) {
|
2014-08-25 18:22:49 +00:00
|
|
|
zend_stat_t sb;
|
2014-12-13 22:06:14 +00:00
|
|
|
if (php_copy_file(url_from, url_to) == SUCCESS) {
|
2004-10-27 11:58:49 +00:00
|
|
|
if (VCWD_STAT(url_from, &sb) == 0) {
|
2016-11-12 10:20:01 +00:00
|
|
|
# ifndef TSRM_WIN32
|
2004-10-27 11:58:49 +00:00
|
|
|
if (VCWD_CHMOD(url_to, sb.st_mode)) {
|
|
|
|
if (errno == EPERM) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
|
2004-10-27 11:58:49 +00:00
|
|
|
VCWD_UNLINK(url_from);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
|
2004-10-27 11:58:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (VCWD_CHOWN(url_to, sb.st_uid, sb.st_gid)) {
|
|
|
|
if (errno == EPERM) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
|
2004-10-27 11:58:49 +00:00
|
|
|
VCWD_UNLINK(url_from);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
|
2004-10-27 11:58:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-01-26 20:04:03 +00:00
|
|
|
# endif
|
2004-10-27 11:58:49 +00:00
|
|
|
VCWD_UNLINK(url_from);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-12-12 23:06:42 +00:00
|
|
|
}
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
|
2004-10-27 11:58:49 +00:00
|
|
|
return 0;
|
2003-12-12 23:06:42 +00:00
|
|
|
}
|
2010-01-26 20:04:03 +00:00
|
|
|
# endif
|
2003-12-12 23:06:42 +00:00
|
|
|
#endif
|
2010-01-26 20:04:03 +00:00
|
|
|
|
|
|
|
#ifdef PHP_WIN32
|
2014-12-13 22:06:14 +00:00
|
|
|
php_win32_docref2_from_error(GetLastError(), url_from, url_to);
|
2010-01-26 20:04:03 +00:00
|
|
|
#else
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
|
2010-01-26 20:04:03 +00:00
|
|
|
#endif
|
2010-09-01 10:16:48 +00:00
|
|
|
return 0;
|
2003-12-12 23:06:42 +00:00
|
|
|
}
|
|
|
|
|
2008-08-07 09:25:33 +00:00
|
|
|
/* Clear stat cache (and realpath cache) */
|
2014-12-13 22:06:14 +00:00
|
|
|
php_clear_stat_cache(1, NULL, 0);
|
2007-10-31 13:22:45 +00:00
|
|
|
|
2003-12-12 23:06:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_plain_files_mkdir(php_stream_wrapper *wrapper, const char *dir, int mode, int options, php_stream_context *context)
|
2003-12-13 04:07:18 +00:00
|
|
|
{
|
|
|
|
int ret, recursive = options & PHP_STREAM_MKDIR_RECURSIVE;
|
|
|
|
char *p;
|
|
|
|
|
2014-11-01 18:49:21 +00:00
|
|
|
if (strncasecmp(dir, "file://", sizeof("file://") - 1) == 0) {
|
|
|
|
dir += sizeof("file://") - 1;
|
2003-12-13 04:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!recursive) {
|
2014-12-13 22:06:14 +00:00
|
|
|
ret = php_mkdir(dir, mode);
|
2003-12-13 04:07:18 +00:00
|
|
|
} else {
|
|
|
|
/* we look for directory separator from the end of string, thus hopefuly reducing our work load */
|
2011-07-26 17:44:20 +00:00
|
|
|
char *e;
|
2014-08-25 18:22:49 +00:00
|
|
|
zend_stat_t sb;
|
2017-07-10 13:48:54 +00:00
|
|
|
size_t dir_len = strlen(dir), offset = 0;
|
2011-07-26 17:44:20 +00:00
|
|
|
char buf[MAXPATHLEN];
|
2003-12-13 04:07:18 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (!expand_filepath_with_mode(dir, buf, NULL, 0, CWD_EXPAND )) {
|
|
|
|
php_error_docref(NULL, E_WARNING, "Invalid path");
|
2011-07-26 17:44:20 +00:00
|
|
|
return 0;
|
2006-10-19 09:49:44 +00:00
|
|
|
}
|
2011-07-26 17:44:20 +00:00
|
|
|
|
|
|
|
e = buf + strlen(buf);
|
2003-12-13 04:07:18 +00:00
|
|
|
|
2005-06-24 02:04:19 +00:00
|
|
|
if ((p = memchr(buf, DEFAULT_SLASH, dir_len))) {
|
|
|
|
offset = p - buf + 1;
|
|
|
|
}
|
|
|
|
|
2006-01-16 19:48:23 +00:00
|
|
|
if (p && dir_len == 1) {
|
2015-01-03 09:22:58 +00:00
|
|
|
/* buf == "DEFAULT_SLASH" */
|
2006-01-16 19:48:23 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* find a top level directory we need to create */
|
2007-10-31 13:22:45 +00:00
|
|
|
while ( (p = strrchr(buf + offset, DEFAULT_SLASH)) || (offset != 1 && (p = strrchr(buf, DEFAULT_SLASH))) ) {
|
2006-10-19 09:49:44 +00:00
|
|
|
int n = 0;
|
|
|
|
|
2006-01-16 19:48:23 +00:00
|
|
|
*p = '\0';
|
2006-10-19 09:49:44 +00:00
|
|
|
while (p > buf && *(p-1) == DEFAULT_SLASH) {
|
|
|
|
++n;
|
|
|
|
--p;
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2006-01-16 19:48:23 +00:00
|
|
|
if (VCWD_STAT(buf, &sb) == 0) {
|
2006-10-19 09:49:44 +00:00
|
|
|
while (1) {
|
|
|
|
*p = DEFAULT_SLASH;
|
|
|
|
if (!n) break;
|
|
|
|
--n;
|
|
|
|
++p;
|
|
|
|
}
|
2006-01-16 19:48:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-12-13 04:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-24 02:04:19 +00:00
|
|
|
|
2003-12-13 04:07:18 +00:00
|
|
|
if (p == buf) {
|
2014-12-13 22:06:14 +00:00
|
|
|
ret = php_mkdir(dir, mode);
|
|
|
|
} else if (!(ret = php_mkdir(buf, mode))) {
|
2003-12-13 04:07:18 +00:00
|
|
|
if (!p) {
|
|
|
|
p = buf;
|
|
|
|
}
|
|
|
|
/* create any needed directories if the creation of the 1st directory worked */
|
|
|
|
while (++p != e) {
|
2006-10-19 09:49:44 +00:00
|
|
|
if (*p == '\0') {
|
2003-12-13 04:07:18 +00:00
|
|
|
*p = DEFAULT_SLASH;
|
2006-10-19 09:49:44 +00:00
|
|
|
if ((*(p+1) != '\0') &&
|
2010-09-01 10:16:48 +00:00
|
|
|
(ret = VCWD_MKDIR(buf, (mode_t)mode)) < 0) {
|
2006-01-17 02:32:09 +00:00
|
|
|
if (options & REPORT_ERRORS) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
|
2006-01-17 02:32:09 +00:00
|
|
|
}
|
2003-12-13 04:07:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
/* Failure */
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/* Success */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_plain_files_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
|
2003-12-13 04:07:18 +00:00
|
|
|
{
|
2014-11-01 18:49:21 +00:00
|
|
|
if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
|
|
|
|
url += sizeof("file://") - 1;
|
2014-11-01 03:32:18 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (php_check_open_basedir(url)) {
|
2003-12-13 04:07:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-10 06:47:24 +00:00
|
|
|
#ifdef PHP_WIN32
|
2017-07-09 13:31:05 +00:00
|
|
|
if (!php_win32_check_trailing_space(url, strlen(url))) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(ENOENT));
|
2010-02-07 20:15:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-12-13 04:07:18 +00:00
|
|
|
if (VCWD_RMDIR(url) < 0) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
|
2003-12-13 04:07:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-07 09:25:33 +00:00
|
|
|
/* Clear stat cache (and realpath cache) */
|
2014-12-13 22:06:14 +00:00
|
|
|
php_clear_stat_cache(1, NULL, 0);
|
2007-10-31 13:22:45 +00:00
|
|
|
|
2003-12-13 04:07:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
static int php_plain_files_metadata(php_stream_wrapper *wrapper, const char *url, int option, void *value, php_stream_context *context)
|
2011-05-25 21:03:55 +00:00
|
|
|
{
|
|
|
|
struct utimbuf *newtime;
|
2016-11-12 10:20:01 +00:00
|
|
|
#ifndef PHP_WIN32
|
2011-05-25 21:03:55 +00:00
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
#endif
|
|
|
|
mode_t mode;
|
|
|
|
int ret = 0;
|
|
|
|
|
2017-04-10 06:47:24 +00:00
|
|
|
#ifdef PHP_WIN32
|
2017-07-09 13:31:05 +00:00
|
|
|
if (!php_win32_check_trailing_space(url, strlen(url))) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(ENOENT));
|
2011-05-25 21:03:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-01 18:49:21 +00:00
|
|
|
if (strncasecmp(url, "file://", sizeof("file://") - 1) == 0) {
|
|
|
|
url += sizeof("file://") - 1;
|
2011-05-25 21:03:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (php_check_open_basedir(url)) {
|
2011-05-25 21:03:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(option) {
|
|
|
|
case PHP_STREAM_META_TOUCH:
|
|
|
|
newtime = (struct utimbuf *)value;
|
|
|
|
if (VCWD_ACCESS(url, F_OK) != 0) {
|
|
|
|
FILE *file = VCWD_FOPEN(url, "w");
|
|
|
|
if (file == NULL) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref1(NULL, url, E_WARNING, "Unable to create file %s because %s", url, strerror(errno));
|
2011-05-25 21:03:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = VCWD_UTIME(url, newtime);
|
|
|
|
break;
|
2016-11-12 10:20:01 +00:00
|
|
|
#ifndef PHP_WIN32
|
2011-05-25 21:03:55 +00:00
|
|
|
case PHP_STREAM_META_OWNER_NAME:
|
|
|
|
case PHP_STREAM_META_OWNER:
|
|
|
|
if(option == PHP_STREAM_META_OWNER_NAME) {
|
2014-12-13 22:06:14 +00:00
|
|
|
if(php_get_uid_by_name((char *)value, &uid) != SUCCESS) {
|
|
|
|
php_error_docref1(NULL, url, E_WARNING, "Unable to find uid for %s", (char *)value);
|
2011-05-25 21:03:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uid = (uid_t)*(long *)value;
|
|
|
|
}
|
|
|
|
ret = VCWD_CHOWN(url, uid, -1);
|
|
|
|
break;
|
|
|
|
case PHP_STREAM_META_GROUP:
|
|
|
|
case PHP_STREAM_META_GROUP_NAME:
|
2015-02-01 07:47:03 +00:00
|
|
|
if(option == PHP_STREAM_META_GROUP_NAME) {
|
2014-12-13 22:06:14 +00:00
|
|
|
if(php_get_gid_by_name((char *)value, &gid) != SUCCESS) {
|
|
|
|
php_error_docref1(NULL, url, E_WARNING, "Unable to find gid for %s", (char *)value);
|
2011-05-25 21:03:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gid = (gid_t)*(long *)value;
|
|
|
|
}
|
|
|
|
ret = VCWD_CHOWN(url, -1, gid);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case PHP_STREAM_META_ACCESS:
|
2014-09-14 12:43:39 +00:00
|
|
|
mode = (mode_t)*(zend_long *)value;
|
2011-05-25 21:03:55 +00:00
|
|
|
ret = VCWD_CHMOD(url, mode);
|
|
|
|
break;
|
|
|
|
default:
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref1(NULL, url, E_WARNING, "Unknown option %d for stream_metadata", option);
|
2011-05-25 21:03:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ret == -1) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref1(NULL, url, E_WARNING, "Operation failed: %s", strerror(errno));
|
2011-05-25 21:03:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-13 22:06:14 +00:00
|
|
|
php_clear_stat_cache(0, NULL, 0);
|
2011-05-25 21:03:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
static php_stream_wrapper_ops php_plain_files_wrapper_ops = {
|
|
|
|
php_plain_files_stream_opener,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
php_plain_files_url_stater,
|
|
|
|
php_plain_files_dir_opener,
|
2003-05-14 06:10:04 +00:00
|
|
|
"plainfile",
|
2003-12-12 23:06:42 +00:00
|
|
|
php_plain_files_unlink,
|
2003-12-13 04:07:18 +00:00
|
|
|
php_plain_files_rename,
|
|
|
|
php_plain_files_mkdir,
|
2011-05-25 21:03:55 +00:00
|
|
|
php_plain_files_rmdir,
|
|
|
|
php_plain_files_metadata
|
2003-02-16 22:19:28 +00:00
|
|
|
};
|
|
|
|
|
2014-11-18 14:38:35 +00:00
|
|
|
PHPAPI php_stream_wrapper php_plain_files_wrapper = {
|
2003-02-16 22:19:28 +00:00
|
|
|
&php_plain_files_wrapper_ops,
|
|
|
|
NULL,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
/* {{{ php_stream_fopen_with_path */
|
2015-03-03 23:05:28 +00:00
|
|
|
PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char *mode, const char *path, zend_string **opened_path, int options STREAMS_DC)
|
2003-02-16 22:19:28 +00:00
|
|
|
{
|
|
|
|
/* code ripped off from fopen_wrappers.c */
|
2013-07-30 10:49:36 +00:00
|
|
|
char *pathbuf, *end;
|
|
|
|
const char *ptr;
|
2003-02-16 22:19:28 +00:00
|
|
|
char trypath[MAXPATHLEN];
|
|
|
|
php_stream *stream;
|
2017-07-10 13:48:54 +00:00
|
|
|
size_t filename_length;
|
2015-03-05 15:18:39 +00:00
|
|
|
zend_string *exec_filename;
|
2003-02-16 22:19:28 +00:00
|
|
|
|
|
|
|
if (opened_path) {
|
|
|
|
*opened_path = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!filename) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-07-10 13:48:54 +00:00
|
|
|
filename_length = strlen(filename);
|
2015-01-30 21:32:00 +00:00
|
|
|
#ifndef PHP_WIN32
|
|
|
|
(void) filename_length;
|
|
|
|
#endif
|
2003-02-16 22:19:28 +00:00
|
|
|
|
|
|
|
/* Relative path open */
|
|
|
|
if (*filename == '.' && (IS_SLASH(filename[1]) || filename[1] == '.')) {
|
|
|
|
/* further checks, we could have ....... filenames */
|
|
|
|
ptr = filename + 1;
|
|
|
|
if (*ptr == '.') {
|
|
|
|
while (*(++ptr) == '.');
|
|
|
|
if (!IS_SLASH(*ptr)) { /* not a relative path after all */
|
|
|
|
goto not_relative_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename)) {
|
2003-10-19 20:57:20 +00:00
|
|
|
return NULL;
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
|
2003-10-19 20:57:20 +00:00
|
|
|
return php_stream_fopen_rel(filename, mode, opened_path, options);
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
not_relative_path:
|
|
|
|
|
|
|
|
/* Absolute path open */
|
|
|
|
if (IS_ABSOLUTE_PATH(filename, filename_length)) {
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename)) {
|
2003-10-19 20:57:20 +00:00
|
|
|
return NULL;
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
|
2003-10-19 20:57:20 +00:00
|
|
|
return php_stream_fopen_rel(filename, mode, opened_path, options);
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
if (IS_SLASH(filename[0])) {
|
2007-04-09 15:38:58 +00:00
|
|
|
size_t cwd_len;
|
2003-02-16 22:19:28 +00:00
|
|
|
char *cwd;
|
2014-12-13 22:06:14 +00:00
|
|
|
cwd = virtual_getcwd_ex(&cwd_len);
|
2003-02-16 22:19:28 +00:00
|
|
|
/* getcwd() will return always return [DRIVE_LETTER]:/) on windows. */
|
|
|
|
*(cwd+3) = '\0';
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2009-02-10 16:14:18 +00:00
|
|
|
if (snprintf(trypath, MAXPATHLEN, "%s%s", cwd, filename) >= MAXPATHLEN) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_NOTICE, "%s/%s path was truncated to %d", cwd, filename, MAXPATHLEN);
|
2009-02-10 14:18:46 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2013-10-17 08:40:43 +00:00
|
|
|
efree(cwd);
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(trypath)) {
|
2003-10-19 20:57:20 +00:00
|
|
|
return NULL;
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-10-19 20:57:20 +00:00
|
|
|
return php_stream_fopen_rel(trypath, mode, opened_path, options);
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-04-28 14:50:27 +00:00
|
|
|
if (!path || !*path) {
|
2003-10-19 20:57:20 +00:00
|
|
|
return php_stream_fopen_rel(filename, mode, opened_path, options);
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check in provided path */
|
|
|
|
/* append the calling scripts' current working directory
|
|
|
|
* as a fall back case
|
|
|
|
*/
|
2015-03-05 15:18:39 +00:00
|
|
|
if (zend_is_executing() &&
|
|
|
|
(exec_filename = zend_get_executed_filename_ex()) != NULL) {
|
2015-06-30 10:59:27 +00:00
|
|
|
const char *exec_fname = ZSTR_VAL(exec_filename);
|
|
|
|
size_t exec_fname_length = ZSTR_LEN(exec_filename);
|
2003-02-16 22:19:28 +00:00
|
|
|
|
2015-03-19 12:43:24 +00:00
|
|
|
while ((--exec_fname_length < SIZE_MAX) && !IS_SLASH(exec_fname[exec_fname_length]));
|
2015-03-05 15:18:39 +00:00
|
|
|
if (exec_fname_length<=0) {
|
|
|
|
/* no path */
|
2003-02-16 22:19:28 +00:00
|
|
|
pathbuf = estrdup(path);
|
|
|
|
} else {
|
2015-03-05 15:18:39 +00:00
|
|
|
size_t path_length = strlen(path);
|
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
pathbuf = (char *) emalloc(exec_fname_length + path_length +1 +1);
|
|
|
|
memcpy(pathbuf, path, path_length);
|
|
|
|
pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;
|
|
|
|
memcpy(pathbuf+path_length+1, exec_fname, exec_fname_length);
|
|
|
|
pathbuf[path_length + exec_fname_length +1] = '\0';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pathbuf = estrdup(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = pathbuf;
|
|
|
|
|
|
|
|
while (ptr && *ptr) {
|
|
|
|
end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
|
|
|
|
if (end != NULL) {
|
|
|
|
*end = '\0';
|
|
|
|
end++;
|
|
|
|
}
|
2006-09-12 15:47:25 +00:00
|
|
|
if (*ptr == '\0') {
|
|
|
|
goto stream_skip;
|
|
|
|
}
|
2009-02-10 16:14:18 +00:00
|
|
|
if (snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename) >= MAXPATHLEN) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_NOTICE, "%s/%s path was truncated to %d", ptr, filename, MAXPATHLEN);
|
2009-02-10 14:18:46 +00:00
|
|
|
}
|
2005-06-20 15:59:13 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir_ex(trypath, 0)) {
|
2006-09-12 15:47:25 +00:00
|
|
|
goto stream_skip;
|
2003-02-16 22:19:28 +00:00
|
|
|
}
|
2015-01-03 09:22:58 +00:00
|
|
|
|
2003-02-16 22:19:28 +00:00
|
|
|
stream = php_stream_fopen_rel(trypath, mode, opened_path, options);
|
|
|
|
if (stream) {
|
|
|
|
efree(pathbuf);
|
|
|
|
return stream;
|
|
|
|
}
|
2006-09-12 15:47:25 +00:00
|
|
|
stream_skip:
|
2003-02-16 22:19:28 +00:00
|
|
|
ptr = end;
|
|
|
|
} /* end provided path */
|
|
|
|
|
|
|
|
efree(pathbuf);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
*/
|