46#define php_stream_fopen_from_fd_int(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_CC)
47#define php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_REL_CC)
48#define php_stream_fopen_from_file_int(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_CC)
49#define php_stream_fopen_from_file_int_rel(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_REL_CC)
57# define PLAIN_WRAP_BUF_SIZE(st) ((unsigned int)(st > INT_MAX ? INT_MAX : st))
59#define fdatasync fsync
61# define PLAIN_WRAP_BUF_SIZE(st) (st)
62# if !defined(HAVE_FDATASYNC)
63# define fdatasync fsync
64# elif defined(__APPLE__)
80 flags = O_TRUNC|O_CREAT;
83 flags = O_CREAT|O_APPEND;
86 flags = O_CREAT|O_EXCL;
104#if defined(O_CLOEXEC)
110#if defined(O_NONBLOCK)
116#if defined(_O_TEXT) && defined(O_BINARY)
150 char *last_mapped_addr;
151 size_t last_mapped_len;
154 char *last_mapped_addr;
160#define PHP_STDIOP_GET_FD(anfd, data) anfd = (data)->file ? fileno((data)->file) : (data)->fd
182 memset(self, 0,
sizeof(*self));
202 memset(self, 0,
sizeof(*self));
226 if (opened_path_ptr) {
227 *opened_path_ptr = opened_path;
256#if defined(S_ISFIFO) && defined(S_ISCHR)
257 if (self->
fd >= 0 && do_fstat(self, 0) == 0) {
258 self->
is_seekable = !(S_ISFIFO(self->
sb.st_mode) || S_ISCHR(self->
sb.st_mode));
259 self->
is_pipe = S_ISFIFO(self->
sb.st_mode);
261#elif defined(PHP_WIN32)
262 uintptr_t
handle = _get_osfhandle(self->
fd);
264 if (
handle != (uintptr_t)INVALID_HANDLE_VALUE) {
267 self->
is_seekable = !(file_type == FILE_TYPE_PIPE || file_type == FILE_TYPE_CHAR);
268 self->
is_pipe = file_type == FILE_TYPE_PIPE;
285 detect_is_seekable(self);
289 }
else if (zero_position) {
314 detect_is_seekable(self);
332 memset(self, 0,
sizeof(*self));
352 ssize_t bytes_written;
362 if (bytes_written < 0) {
366 if (
errno == EINTR) {
368 return bytes_written;
377 if (
data->is_seekable &&
data->last_op ==
'r') {
391 return bytes_written;
406 HANDLE ph = (HANDLE)_get_osfhandle(
data->fd);
408 DWORD avail_read = 0;
414 if (!PeekNamedPipe(ph,
NULL, 0,
NULL, &avail_read,
NULL)) {
418 if (0 == avail_read) {
421 }
while (0 == avail_read && retry++ < 3200000);
425 if (avail_read <
count) {
432 if (
ret == (
size_t)-1 &&
errno == EINTR) {
443 }
else if (
errno == EINTR) {
451 if (
errno != EBADF) {
455 }
else if (
ret == 0) {
461 if (
data->is_seekable &&
data->last_op ==
'w')
479static int php_stdiop_close(
php_stream *stream,
int close_handle)
487 if (
data->last_mapped_addr) {
488 munmap(
data->last_mapped_addr,
data->last_mapped_len);
491#elif defined(PHP_WIN32)
492 if (
data->last_mapped_addr) {
493 UnmapViewOfFile(
data->last_mapped_addr);
496 if (
data->file_mapping) {
497 CloseHandle(
data->file_mapping);
504 if (
data->is_process_pipe) {
508#ifdef HAVE_SYS_WAIT_H
509 if (WIFEXITED(
ret)) {
517 }
else if (
data->fd != -1) {
523 if (
data->temp_name) {
544static int php_stdiop_flush(
php_stream *stream)
566static int php_stdiop_sync(
php_stream *stream,
bool dataonly)
576 if (php_stdiop_flush(stream) == 0) {
594 if (!
data->is_seekable) {
616static int php_stdiop_cast(
php_stream *stream,
int castas,
void **
ret)
635 data->file = fdopen(
data->fd, fixed_mode);
680 if((
ret = do_fstat(
data, 1)) == 0) {
687static int php_stdiop_set_option(
php_stream *stream,
int option,
int value,
void *ptrparam)
712 if (-1 == fcntl(
fd, F_SETFL,
flags))
726 size = *(
size_t *)ptrparam;
732 return setvbuf(
data->file,
NULL, _IONBF, 0);
773 if (do_fstat(
data, 1) != 0) {
776 if (range->offset >
data->sb.st_size) {
777 range->offset =
data->sb.st_size;
779 if (range->length == 0 ||
780 range->length >
data->sb.st_size - range->offset) {
781 range->length =
data->sb.st_size - range->offset;
783 switch (range->mode) {
803 range->mapped = (
char*)mmap(
NULL, range->length, prot,
flags,
fd, range->offset);
805 range->mapped =
NULL;
809 data->last_mapped_addr = range->mapped;
810 data->last_mapped_len = range->length;
814 if (
data->last_mapped_addr) {
815 munmap(
data->last_mapped_addr,
data->last_mapped_len);
823#elif defined(PHP_WIN32)
826 HANDLE hfile = (HANDLE)_get_osfhandle(
fd);
827 DWORD prot, acc, loffs = 0, hoffs = 0, delta = 0;
828 LARGE_INTEGER file_size;
835 switch (range->mode) {
837 prot = PAGE_READONLY;
841 prot = PAGE_READWRITE;
842 acc = FILE_MAP_READ | FILE_MAP_WRITE;
845 prot = PAGE_READONLY;
850 prot = PAGE_READWRITE;
851 acc = FILE_MAP_READ | FILE_MAP_WRITE;
859 data->file_mapping = CreateFileMapping(hfile,
NULL, prot, 0, 0,
NULL);
865 if (!GetFileSizeEx(hfile, &file_size)) {
866 CloseHandle(
data->file_mapping);
871 size = file_size.QuadPart;
873 if (file_size.HighPart) {
874 CloseHandle(
data->file_mapping);
878 size = file_size.LowPart;
881 if (range->offset >
size) {
882 range->offset =
size;
884 if (range->length == 0 || range->length >
size - range->offset) {
885 range->length =
size - range->offset;
889 if (range->offset != 0) {
893 GetSystemInfo(&info);
894 gran = info.dwAllocationGranularity;
895 ZEND_ASSERT(gran != 0 && (gran & (gran - 1)) == 0);
896 size_t rounded_offset = (range->offset / gran) * gran;
897 delta = range->offset - rounded_offset;
898 loffs = (
DWORD)rounded_offset;
900 hoffs = (
DWORD)(rounded_offset >> 32);
907 if (range->length + delta == 0) {
911 data->last_mapped_addr = MapViewOfFile(
data->file_mapping, acc, hoffs, loffs, range->length + delta);
913 if (
data->last_mapped_addr) {
915 range->mapped =
data->last_mapped_addr + delta;
919 CloseHandle(
data->file_mapping);
925 if (
data->last_mapped_addr) {
926 UnmapViewOfFile(
data->last_mapped_addr);
928 CloseHandle(
data->file_mapping);
965 HANDLE h = (HANDLE) _get_osfhandle(
fd);
966 if (INVALID_HANDLE_VALUE == h) {
970 LARGE_INTEGER sz, old_sz;
973 if (!SetFilePointerEx(h, sz, &old_sz, FILE_CURRENT)) {
978 sz.QuadPart = new_size;
981 sz.LowPart = new_size;
983 if (!SetFilePointerEx(h, sz,
NULL, FILE_BEGIN)) {
986 if (0 == SetEndOfFile(h)) {
989 if (!SetFilePointerEx(h, old_sz,
NULL, FILE_BEGIN)) {
1009 flags = fcntl(
fd, F_GETFL, 0);
1011 add_assoc_bool((
zval*)ptrparam,
"timed_out", 0);
1013 add_assoc_bool((
zval*)ptrparam,
"eof", stream->eof);
1026 php_stdiop_close, php_stdiop_flush,
1031 php_stdiop_set_option
1036static ssize_t php_plain_files_dirstream_read(
php_stream *stream,
char *
buf,
size_t count)
1049#ifdef _DIRENT_HAVE_D_TYPE
1059static int php_plain_files_dirstream_close(
php_stream *stream,
int close_handle)
1071 NULL, php_plain_files_dirstream_read,
1072 php_plain_files_dirstream_close,
NULL,
1074 php_plain_files_dirstream_rewind,
1100 php_win32_docref1_from_error(GetLastError(), path);
1103 if (
dir &&
dir->finished) {
1126 char *persistent_id =
NULL;
1152 efree(persistent_id);
1157 fd = php_win32_ioutil_open(
realpath, open_flags, 0666);
1184 if (persistent_id) {
1185 efree(persistent_id);
1197 r = do_fstat(self, 0);
1198 if ((r == 0 && !
S_ISREG(self->
sb.st_mode))) {
1201 *opened_path =
NULL;
1222 if (persistent_id) {
1223 efree(persistent_id);
1243 if (
strncasecmp(url,
"file://",
sizeof(
"file://") - 1) == 0) {
1244 url +=
sizeof(
"file://") - 1;
1270 if (
strncasecmp(url,
"file://",
sizeof(
"file://") - 1) == 0) {
1271 url +=
sizeof(
"file://") - 1;
1296 if (!url_from || !url_to) {
1302 php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to);
1306 php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to);
1311 if (
strncasecmp(url_from,
"file://",
sizeof(
"file://") - 1) == 0) {
1312 url_from +=
sizeof(
"file://") - 1;
1315 if (
strncasecmp(url_to,
"file://",
sizeof(
"file://") - 1) == 0) {
1316 url_to +=
sizeof(
"file://") - 1;
1328 if (
errno == EXDEV) {
1330# if !defined(ZTS) && !defined(TSRM_WIN32)
1332 int oldmask =
umask(077);
1346 if (
VCWD_CHOWN(url_to, sb.st_uid, sb.st_gid)) {
1348 if (
errno != EPERM) {
1356 if (
errno != EPERM) {
1371# if !defined(ZTS) && !defined(TSRM_WIN32)
1380 php_win32_docref2_from_error(GetLastError(), url_from, url_to);
1396 dir +=
sizeof(
"file://") - 1;
1433 if (
p && dir_len == 1) {
1471 bool replaced_slash =
false;
1474 replaced_slash =
true;
1476 if (*(
p+1) !=
'\0') {
1481 if (
p == e || !replaced_slash) {
1497 if (
strncasecmp(url,
"file://",
sizeof(
"file://") - 1) == 0) {
1498 url +=
sizeof(
"file://") - 1;
1525 struct utimbuf *newtime;
1540 if (
strncasecmp(url,
"file://",
sizeof(
"file://") - 1) == 0) {
1541 url +=
sizeof(
"file://") - 1;
1550 newtime = (
struct utimbuf *)
value;
1560 ret = VCWD_UTIME(url, newtime);
1571 uid = (uid_t)*(
long *)
value;
1583 gid = (gid_t)*(
long *)
value;
1606 php_plain_files_stream_opener,
1609 php_plain_files_url_stater,
1610 php_plain_files_dir_opener,
1612 php_plain_files_unlink,
1613 php_plain_files_rename,
1614 php_plain_files_mkdir,
1615 php_plain_files_rmdir,
1616 php_plain_files_metadata
1621 &php_plain_files_wrapper_ops,
1630 char *pathbuf, *
end;
1634 size_t filename_length;
1638 *opened_path =
NULL;
1645 filename_length =
strlen(filename);
1647 (
void) filename_length;
1651 if (*filename ==
'.' && (
IS_SLASH(filename[1]) || filename[1] ==
'.')) {
1655 while (*(++
ptr) ==
'.');
1657 goto not_relative_path;
1703 if (!path || !*path) {
1713 const char *exec_fname =
ZSTR_VAL(exec_filename);
1714 size_t exec_fname_length =
ZSTR_LEN(exec_filename);
1716 while ((--exec_fname_length <
SIZE_MAX) && !
IS_SLASH(exec_fname[exec_fname_length]));
1717 if (exec_fname_length<=0) {
1721 size_t path_length =
strlen(path);
1723 pathbuf = (
char *)
emalloc(exec_fname_length + path_length +1 +1);
1724 memcpy(pathbuf, path, path_length);
1726 memcpy(pathbuf+path_length+1, exec_fname, exec_fname_length);
1727 pathbuf[path_length + exec_fname_length +1] =
'\0';
unlink(string $filename, $context=null)
usleep(int $microseconds)
readdir($dir_handle=null)
ftruncate($stream, int $size)
file(string $filename, int $flags=0, $context=null)
closedir($dir_handle=null)
strrchr(string $haystack, string $needle, bool $before_needle=false)
fwrite($stream, string $data, ?int $length=null)
dir(string $directory, $context=null)
count(Countable|array $value, int $mode=COUNT_NORMAL)
fread($stream, int $length)
flock($stream, int $operation, &$would_block=null)
assert(mixed $assertion, Throwable|string|null $description=null)
rewinddir($dir_handle=null)
strchr(string $haystack, string $needle, bool $before_needle=false)
void php_stream_mode_sanitize_fdopen_fopencookie(php_stream *stream, char *result)
memset(ptr, 0, type->size)
zend_ffi_ctype_name_buf buf
PHPAPI zend_result php_copy_file(const char *src, const char *dest)
PHPAPI void php_clear_stat_cache(bool clear_realpath_cache, const char *filename, size_t filename_len)
PHPAPI char * expand_filepath_with_mode(const char *filepath, char *real_path, const char *relative_to, size_t relative_to_len, int realpath_mode)
PHPAPI int php_check_open_basedir(const char *path)
PHPAPI char * expand_filepath(const char *filepath, char *real_path)
PHPAPI int php_check_open_basedir_ex(const char *path, int warn)
PHPAPI ZEND_COLD void php_error_docref2(const char *docref, const char *param1, const char *param2, int type, const char *format,...)
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
PHPAPI ZEND_COLD void php_error_docref1(const char *docref, const char *param1, int type, const char *format,...)
#define PHP_STRLCPY(dst, src, size, src_size)
unsigned const char * end
PHP_JSON_API size_t int options
#define PHP_IS_TRANSIENT_ERROR(err)
PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, zend_string **opened_path_p)
php_output_handler * active
PHPAPI const php_stream_wrapper php_glob_stream_wrapper
@ PHP_STREAM_MAP_MODE_READWRITE
@ PHP_STREAM_MAP_MODE_READONLY
@ PHP_STREAM_MAP_MODE_SHARED_READONLY
@ PHP_STREAM_MAP_MODE_SHARED_READWRITE
#define php_stream_mmap_range(stream, offset, length, mode, mapped_len)
@ PHP_STREAM_MMAP_SUPPORTED
@ PHP_STREAM_MMAP_MAP_RANGE
PHPAPI php_stream_wrapper php_plain_files_wrapper
#define php_stream_fopen_temporary_file(dir, pfx, opened_path)
PHPAPI php_stream_ops php_stream_stdio_ops
#define php_stream_cast(stream, as, ret, show_err)
struct _php_stream_wrapper_ops php_stream_wrapper_ops
struct _php_stream php_stream
struct _php_stream_context php_stream_context
#define php_stream_fopen_from_fd_rel(fd, mode, persistent_id, zero_position)
#define PHP_STREAM_META_GROUP
#define PHP_STREAM_OPTION_BLOCKING
struct _php_stream_dirent php_stream_dirent
#define PHP_STREAM_AS_FD_FOR_SELECT
#define STREAM_USE_GLOB_DIR_OPEN
#define PHP_STREAM_TRUNCATE_SET_SIZE
#define PHP_STREAM_URL_STAT_IGNORE_OPEN_BASEDIR
PHPAPI int php_stream_from_persistent_id(const char *persistent_id, php_stream **stream)
#define PHP_STREAM_META_OWNER_NAME
#define php_stream_alloc_rel(ops, thisptr, persistent, mode)
#define PHP_STREAM_FLAG_NO_SEEK
#define PHP_STREAM_LOCK_SUPPORTED
#define STREAM_OPEN_FOR_INCLUDE
#define PHP_STREAM_BUFFER_NONE
#define PHP_STREAM_MKDIR_RECURSIVE
#define PHP_STREAM_OPTION_WRITE_BUFFER
#define PHP_STREAM_META_GROUP_NAME
#define STREAM_ASSUME_REALPATH
#define php_stream_close(stream)
#define PHP_STREAM_OPTION_RETURN_NOTIMPL
#define PHP_STREAM_FLAG_SUPPRESS_ERRORS
#define PHP_STREAM_META_OWNER
#define PHP_STREAM_PERSISTENT_FAILURE
#define STREAM_DISABLE_OPEN_BASEDIR
#define STREAM_OPEN_PERSISTENT
struct _php_stream_ops php_stream_ops
#define PHP_STREAM_URL_STAT_LINK
#define PHP_STREAM_OPTION_LOCKING
#define PHP_STREAM_META_TOUCH
#define PHP_STREAM_BUFFER_FULL
#define PHP_STREAM_PERSISTENT_SUCCESS
#define PHP_STREAM_OPTION_META_DATA_API
#define PHP_STREAM_OPTION_SYNC_API
#define PHP_STREAM_OPTION_RETURN_ERR
#define PHP_STREAM_SYNC_FSYNC
#define PHP_STREAM_OPTION_TRUNCATE_API
#define PHP_STREAM_BUFFER_LINE
struct _php_stream_wrapper php_stream_wrapper
#define PHP_STREAM_SYNC_SUPPORTED
#define PHP_STREAM_SYNC_FDSYNC
#define PHP_STREAM_TRUNCATE_SUPPORTED
#define php_stream_fopen_rel(filename, mode, opened, options)
#define php_stream_alloc(ops, thisptr, persistent_id, mode)
#define PHP_STREAM_OPTION_RETURN_OK
#define PHP_STREAM_URL_STAT_QUIET
#define PHP_STREAM_OPTION_PIPE_BLOCKING
#define STREAM_USE_BLOCKING_PIPE
#define PHP_STREAM_AS_STDIO
struct _php_stream_statbuf php_stream_statbuf
PHPAPI void php_stream_wrapper_log_error(const php_stream_wrapper *wrapper, int options, const char *fmt,...) PHP_ATTRIBUTE_FORMAT(printf
#define PHP_STREAM_OPTION_MMAP_API
#define PHP_STREAM_META_ACCESS
#define pemalloc_rel_orig(size, persistent)
#define emalloc_rel_orig(size)
ssize_t(* php_stdiop_write)(php_stream *, const char *, size_t)
#define php_stream_fopen_from_file_int_rel(file, mode)
PHPAPI php_stream * _php_stream_fopen(const char *filename, const char *mode, zend_string **opened_path, int options STREAMS_DC)
PHPAPI php_stream * _php_stream_fopen_from_pipe(FILE *file, const char *mode STREAMS_DC)
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)
PHPAPI php_stream * _php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC)
#define php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id)
PHPAPI php_stream * _php_stream_fopen_tmpfile(int dummy STREAMS_DC)
int php_get_uid_by_name(const char *name, uid_t *uid)
PHPAPI php_stream * _php_stream_fopen_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_ptr STREAMS_DC)
int php_get_gid_by_name(const char *name, gid_t *gid)
PHPAPI int php_stream_parse_fopen_modes(const char *mode, int *open_flags)
PHPAPI php_stream * _php_stream_fopen_from_fd(int fd, const char *mode, const char *persistent_id, bool zero_position STREAMS_DC)
#define PLAIN_WRAP_BUF_SIZE(st)
#define PHP_STDIOP_GET_FD(anfd, data)
php_stream_wrapper * wrapper
unsigned is_pipe_blocking
int php_win32_check_trailing_space(const char *path, const size_t path_len)
ZEND_API ZEND_COLD void zend_value_error(const char *format,...)
#define estrndup(s, length)
#define pefree(ptr, persistent)
zend_string_release_ex(func->internal_function.function_name, 0)
#define strncasecmp(s1, s2, n)
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
ZEND_API zend_string * zend_get_executed_filename_ex(void)
ZEND_API bool zend_is_executing(void)
struct _zend_string zend_string
CWD_API char * virtual_getcwd_ex(size_t *length)
#define VCWD_MKDIR(pathname, mode)
#define VCWD_RENAME(oldname, newname)
#define VCWD_FOPEN(path, mode)
#define VCWD_CHOWN(path, owner, group)
#define IS_ABSOLUTE_PATH(path, len)
#define VCWD_LSTAT(path, buff)
#define VCWD_OPENDIR(pathname)
#define VCWD_UNLINK(path)
#define DEFAULT_DIR_SEPARATOR
#define VCWD_RMDIR(pathname)
#define VCWD_CHMOD(path, mode)
#define VCWD_ACCESS(pathname, mode)
#define VCWD_STAT(path, buff)