Patch add-win-fs-unicode-support for *Directory/Filesystem functions Bug #63401
Patch version 2012-10-30 22:06 UTC
Return to Bug #63401
| Download this patch
Patch Revisions:
2012-10-30 22:09 UTC | 2012-10-30 22:07 UTC | 2012-10-30 22:06 UTC | 2012-10-30 22:05 UTCDeveloper: j4f@bk.ru
| 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. |
+----------------------------------------------------------------------+
| Authors: Wez Furlong <wez@thebrainroom.com> |
| Authors: Andi Gutmans <andi@zend.com> |
| Sascha Schumann <sascha@schumann.cx> |
| Pierre Joye <pierre@php.net> |
+----------------------------------------------------------------------+
*/
*/
/* $Id$ */
#include "php.h"
#include "php_globals.h"
#include "php_network.h"
#include "php_open_temporary_file.h"
#include "ext/standard/file.h"
#include "ext/standard/flock_compat.h"
#include "ext/standard/php_filestat.h"
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#if HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include "SAPI.h"
#include "php_streams_int.h"
#ifdef PHP_WIN32
# include "win32/winutil.h"
#include <time.h>
#include "tsrm_virtual_cwd.h"
#include "tsrm_strtok_r.h"
#ifdef TSRM_WIN32
#include <io.h>
#include "tsrm_win32.h"
# ifndef IO_REPARSE_TAG_SYMLINK
# define IO_REPARSE_TAG_SYMLINK 0xA000000C
# endif
# ifndef VOLUME_NAME_NT
# define VOLUME_NAME_NT 0x2
# endif
# ifndef VOLUME_NAME_DOS
# define VOLUME_NAME_DOS 0x0
# endif
#endif
#ifndef S_IFLNK
# define S_IFLNK 0120000
#endif
#ifdef NETWARE
#include <fsio.h>
#endif
#ifndef HAVE_REALPATH
#define realpath(x,y) strcpy(y,x)
#endif
#define VIRTUAL_CWD_DEBUG 0
#include "TSRM.h"
/* Only need mutex for popen() in Windows and NetWare because it doesn't chdir() on UNIX */
#if (defined(TSRM_WIN32) || defined(NETWARE)) && defined(ZTS)
MUTEX_T cwd_mutex;
#endif
#ifdef ZTS
ts_rsrc_id cwd_globals_id;
#else
virtual_cwd_globals cwd_globals;
#endif
cwd_state main_cwd_state; /* True global */
#ifndef TSRM_WIN32
#include <unistd.h>
#else
#include <direct.h>
#endif
#ifndef S_ISDIR
#define S_ISDIR(mode) ((mode) & _S_IFDIR)
#endif
#ifndef S_ISREG
#define S_ISREG(mode) ((mode) & _S_IFREG)
#endif
#ifdef TSRM_WIN32
#include <tchar.h>
#define tsrm_strtok_r(a,b,c) _tcstok((a),(b))
#define TOKENIZER_STRING "/\\"
static int php_check_dots(const char *element, int n)
{
while (n-- > 0) if (element[n] != '.') break;
return (n != -1);
}
#define IS_DIRECTORY_UP(element, len) \
(len >= 2 && !php_check_dots(element, len))
#define IS_DIRECTORY_CURRENT(element, len) \
(len == 1 && element[0] == '.')
#elif defined(NETWARE)
/* NetWare has strtok() (in LibC) and allows both slashes in paths, like Windows --
but rest of the stuff is like Unix */
/* strtok() call in LibC is abending when used in a different address space -- hence using
PHP's version itself for now */
/*#define tsrm_strtok_r(a,b,c) strtok((a),(b))*/
#define TOKENIZER_STRING "/\\"
#else
#define TOKENIZER_STRING "/"
#endif
/* default macros */
#ifndef IS_DIRECTORY_UP
#define IS_DIRECTORY_UP(element, len) \
(len == 2 && element[0] == '.' && element[1] == '.')
#endif
#ifndef IS_DIRECTORY_CURRENT
#define IS_DIRECTORY_CURRENT(element, len) \
(len == 1 && element[0] == '.')
#endif
/* define this to check semantics */
#define IS_DIR_OK(s) (1)
#ifndef IS_DIR_OK
#define IS_DIR_OK(state) (php_is_dir_ok(state) == 0)
#endif
#define CWD_STATE_COPY(d, s) \
(d)->cwd_length = (s)->cwd_length; \
(d)->cwd = (char *) malloc((s)->cwd_length+1); \
memcpy((d)->cwd, (s)->cwd, (s)->cwd_length+1);
#define CWD_STATE_FREE(s) \
free((s)->cwd);
#ifdef TSRM_WIN32
#ifdef CTL_CODE
#undef CTL_CODE
#endif
#define CTL_CODE(DeviceType,Function,Method,Access) (((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
#define FILE_DEVICE_FILE_SYSTEM 0x00000009
#define METHOD_BUFFERED 0
#define FILE_ANY_ACCESS 0
#define FSCTL_GET_REPARSE_POINT CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 42, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
typedef struct {
unsigned long ReparseTag;
unsigned short ReparseDataLength;
unsigned short Reserved;
union {
struct {
unsigned short SubstituteNameOffset;
unsigned short SubstituteNameLength;
unsigned short PrintNameOffset;
unsigned short PrintNameLength;
unsigned long Flags;
wchar_t ReparseTarget[1];
} SymbolicLinkReparseBuffer;
struct {
unsigned short SubstituteNameOffset;
unsigned short SubstituteNameLength;
unsigned short PrintNameOffset;
unsigned short PrintNameLength;
wchar_t ReparseTarget[1];
} MountPointReparseBuffer;
struct {
unsigned char ReparseTarget[1];
} GenericReparseBuffer;
};
} REPARSE_DATA_BUFFER;
#define SECS_BETWEEN_EPOCHS (__int64)11644473600
#define SECS_TO_100NS (__int64)10000000
static inline time_t FileTimeToUnixTime(const FILETIME FileTime)
{
__int64 UnixTime;
long *nsec = NULL;
SYSTEMTIME SystemTime;
FileTimeToSystemTime(&FileTime, &SystemTime);
UnixTime = ((__int64)FileTime.dwHighDateTime << 32) +
FileTime.dwLowDateTime;
UnixTime -= (SECS_BETWEEN_EPOCHS * SECS_TO_100NS);
if (nsec) {
*nsec = (UnixTime % SECS_TO_100NS) * (__int64)100;
}
UnixTime /= SECS_TO_100NS; /* now convert to seconds */
if ((time_t)UnixTime != UnixTime) {
UnixTime = 0;
}
return (time_t)UnixTime;
}
CWD_API int php_sys_readlink(const char *link, char *target, size_t target_len){ /* {{{ */
HINSTANCE kernel32;
HANDLE hFile;
DWORD dwRet;
typedef BOOL (WINAPI *gfpnh_func)(HANDLE, LPTSTR, DWORD, DWORD);
gfpnh_func pGetFinalPathNameByHandle;
kernel32 = LoadLibrary("kernel32.dll");
if (kernel32) {
pGetFinalPathNameByHandle = (gfpnh_func)GetProcAddress(kernel32, "GetFinalPathNameByHandleA");
if (pGetFinalPathNameByHandle == NULL) {
return -1;
}
} else {
return -1;
}
hFile = CreateFile(link, // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_FLAG_BACKUP_SEMANTICS, // normal file
NULL); // no attr. template
if( hFile == INVALID_HANDLE_VALUE) {
return -1;
}
dwRet = pGetFinalPathNameByHandle(hFile, target, MAXPATHLEN, VOLUME_NAME_DOS);
if(dwRet >= MAXPATHLEN) {
return -1;
}
CloseHandle(hFile);
if(dwRet > 4) {
/* Skip first 4 characters if they are "\??\" */
if(target[0] == '\\' && target[1] == '\\' && target[2] == '?' && target[3] == '\\') {
char tmp[MAXPATHLEN];
unsigned int offset = 4;
dwRet -= 4;
/* \??\UNC\ */
if (dwRet > 7 && target[4] == 'U' && target[5] == 'N' && target[6] == 'C') {
offset += 2;
dwRet -= 2;
target[offset] = '\\';
}
memcpy(tmp, target + offset, dwRet);
memcpy(target, tmp, dwRet);
}
}
target[dwRet] = '\0';
return dwRet;
}
/* }}} */
HANDLE FindFirstFileWW( LPCSTR lpFileName, WIN32_FIND_DATA *lpFindFileData) {
wchar_t *lpFileNameW;
char cFileNameR[260]; // return value
char cAlternateFileNameR[14]; // return value
HANDLE hFindW;
WIN32_FIND_DATAW lpFindFileDataW;
int lpFileNameW_len;
lpFileNameW_len = MultiByteToWideChar( CP_UTF8, 0, lpFileName, -1, NULL, 0 );
lpFileNameW = (wchar_t *)malloc(lpFileNameW_len *2);
//lpFileNameW = lpFileName.GetBuffer(lpFileNameW_len - 1);
//lpFileNameW = lpFileNameW.GetBuffer(lpFileNameW_len);
MultiByteToWideChar( CP_UTF8, 0, lpFileName, -1, lpFileNameW, lpFileNameW_len *2 );
hFindW = FindFirstFileW(lpFileNameW, &lpFindFileDataW);
free(lpFileNameW);
WideCharToMultiByte( CP_UTF8, 0, lpFindFileDataW.cFileName, -1, cFileNameR, 260, NULL, NULL);
WideCharToMultiByte( CP_UTF8, 0, lpFindFileDataW.cAlternateFileName, -1, cAlternateFileNameR, 14, NULL, NULL);
lpFindFileData->dwFileAttributes = lpFindFileDataW.dwFileAttributes;
lpFindFileData->ftCreationTime = lpFindFileDataW.ftCreationTime;
lpFindFileData->ftLastAccessTime = lpFindFileDataW.ftLastAccessTime;
lpFindFileData->ftLastWriteTime = lpFindFileDataW.ftLastWriteTime;
lpFindFileData->nFileSizeHigh = lpFindFileDataW.nFileSizeHigh;
lpFindFileData->nFileSizeLow = lpFindFileDataW.nFileSizeLow;
lpFindFileData->dwReserved0 = lpFindFileDataW.dwReserved0;
lpFindFileData->dwReserved1 = lpFindFileDataW.dwReserved1;
strcpy(lpFindFileData->cFileName , cFileNameR);
strcpy(lpFindFileData->cAlternateFileName , cAlternateFileNameR);
return hFindW;
}
#define php_stream_fopen_from_fd_int(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_CC TSRMLS_CC)
#define php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_REL_CC TSRMLS_CC)
#define php_stream_fopen_from_file_int(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_CC TSRMLS_CC)
#define php_stream_fopen_from_file_int_rel(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_REL_CC TSRMLS_CC)
#if !defined(WINDOWS) && !defined(NETWARE)
extern int php_get_uid_by_name(const char *name, uid_t *uid TSRMLS_DC);
extern int php_get_gid_by_name(const char *name, gid_t *gid TSRMLS_DC);
#endif
LPCWSTR UTF8toUTF163( LPCSTR UTF8 ) {
LPCWSTR UTF8toUTF16( LPCSTR UTF8 ) {
LPCWSTR UTF16;
int UTF16_len;
MultiByteToWideChar( CP_UTF8, 0, UTF8, -1, UTF16, UTF16_len *2 );
return UTF16;
}
/* parse standard "fopen" modes into open() flags */
PHPAPI int php_stream_parse_fopen_modes(const char *mode, int *open_flags)
CWD_API int php_sys_stat_ex(const char *path, struct stat *buf, int lstat) /* {{{ */
{
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;
case 'c':
flags = O_CREAT;
break;
default:
/* unknown mode */
return FAILURE;
}
#if defined(O_NONBLOCK)
if (strchr(mode, 'n')) {
flags |= O_NONBLOCK;
WIN32_FILE_ATTRIBUTE_DATA data;
__int64 t;
const size_t path_len = strlen(path);
//if (!GetFileAttributesEx(path, GetFileExInfoStandard, &data)) { // mod@yur
if (!GetFileAttributesExW(UTF8toUTF16(path), GetFileExInfoStandard, &data)) { // add@yur
return stat(path, buf);
}
#endif
if (strchr(mode, '+')) {
flags |= O_RDWR;
} else if (flags) {
flags |= O_WRONLY;
if (path_len >= 1 && path[1] == ':') {
if (path[0] >= 'A' && path[0] <= 'Z') {
buf->st_dev = buf->st_rdev = path[0] - 'A';
} else {
buf->st_dev = buf->st_rdev = path[0] - 'a';
}
} else if (IS_UNC_PATH(path, path_len)) {
buf->st_dev = buf->st_rdev = 0;
} else {
flags |= O_RDONLY;
char cur_path[MAXPATHLEN+1];
DWORD len = sizeof(cur_path);
char *tmp = cur_path;
while(1) {
DWORD r = GetCurrentDirectory(len, tmp);
if (r < len) {
if (tmp[1] == ':') {
if (path[0] >= 'A' && path[0] <= 'Z') {
buf->st_dev = buf->st_rdev = path[0] - 'A';
} else {
buf->st_dev = buf->st_rdev = path[0] - 'a';
}
} else {
buf->st_dev = buf->st_rdev = -1;
}
break;
} else if (!r) {
buf->st_dev = buf->st_rdev = -1;
break;
} else {
len = r+1;
tmp = (char*)malloc(len);
}
}
if (tmp != cur_path) {
free(tmp);
}
}
#if defined(_O_TEXT) && defined(O_BINARY)
if (strchr(mode, 't')) {
flags |= _O_TEXT;
buf->st_uid = buf->st_gid = buf->st_ino = 0;
if (lstat && data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
/* File is a reparse point. Get the target */
HANDLE hLink = NULL;
REPARSE_DATA_BUFFER * pbuffer;
unsigned int retlength = 0;
TSRM_ALLOCA_FLAG(use_heap_large);
hLink = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL);
if(hLink == INVALID_HANDLE_VALUE) {
return -1;
}
pbuffer = (REPARSE_DATA_BUFFER *)tsrm_do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
tsrm_free_alloca(pbuffer, use_heap_large);
CloseHandle(hLink);
return -1;
}
CloseHandle(hLink);
if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
buf->st_mode = S_IFLNK;
buf->st_mode |= (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)) : (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)|S_IWRITE|(S_IWRITE>>3)|(S_IWRITE>>6));
}
#if 0 /* Not used yet */
else if(pbuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
buf->st_mode |=;
}
#endif
tsrm_free_alloca(pbuffer, use_heap_large);
} else {
flags |= O_BINARY;
}
#endif
*open_flags = flags;
return SUCCESS;
}
/* {{{ ------- STDIO stream implementation -------*/
typedef struct {
FILE *file;
int fd; /* underlying file descriptor */
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 */
unsigned _reserved:29;
int lock_flag; /* stores the lock state */
char *temp_file_name; /* if non-null, this is the path to a temporary file that
* is to be deleted when the stream is closed */
#if HAVE_FLUSHIO
char last_op;
#endif
#if HAVE_MMAP
char *last_mapped_addr;
size_t last_mapped_len;
#endif
#ifdef PHP_WIN32
char *last_mapped_addr;
HANDLE file_mapping;
#endif
struct stat sb;
} php_stdio_stream_data;
#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;
buf->st_mode = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? (S_IFDIR|S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6)) : S_IFREG;
buf->st_mode |= (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)) : (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)|S_IWRITE|(S_IWRITE>>3)|(S_IWRITE>>6));
}
PHP_STDIOP_GET_FD(fd, d);
r = fstat(fd, &d->sb);
d->cached_fstat = r == 0;
return r;
}
return 0;
}
static php_stream *_php_stream_fopen_from_fd_int(int fd, const char *mode, const char *persistent_id STREAMS_DC TSRMLS_DC)
{
php_stdio_stream_data *self;
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;
self->temp_file_name = NULL;
self->fd = fd;
return php_stream_alloc_rel(&php_stream_stdio_ops, self, persistent_id, mode);
}
if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
int len = strlen(path);
static php_stream *_php_stream_fopen_from_file_int(FILE *file, const char *mode STREAMS_DC TSRMLS_DC)
{
php_stdio_stream_data *self;
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;
self->temp_file_name = NULL;
self->fd = fileno(file);
return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
}
PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path STREAMS_DC TSRMLS_DC)
{
int fd = php_open_temporary_fd(dir, pfx, opened_path TSRMLS_CC);
if (path[len-4] == '.') {
if (_memicmp(path+len-3, "exe", 3) == 0 ||
_memicmp(path+len-3, "com", 3) == 0 ||
_memicmp(path+len-3, "bat", 3) == 0 ||
_memicmp(path+len-3, "cmd", 3) == 0) {
buf->st_mode |= (S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6));
}
}
}
if (fd != -1) {
php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL);
if (stream) {
return stream;
}
close(fd);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to allocate stream");
return NULL;
}
return NULL;
}
PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC TSRMLS_DC)
{
char *opened_path = NULL;
int fd = php_open_temporary_fd(NULL, "php", &opened_path TSRMLS_CC);
if (fd != -1) {
php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL);
if (stream) {
php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
stream->wrapper = &php_plain_files_wrapper;
stream->orig_path = estrdup(opened_path);
self->temp_file_name = opened_path;
self->lock_flag = LOCK_UN;
return stream;
}
close(fd);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to allocate stream");
return NULL;
}
return NULL;
}
PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const char *persistent_id STREAMS_DC TSRMLS_DC)
{
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;
#ifdef S_ISFIFO
/* 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;
}
#elif defined(PHP_WIN32)
{
zend_uintptr_t handle = _get_osfhandle(self->fd);
if (handle != (zend_uintptr_t)INVALID_HANDLE_VALUE) {
self->is_pipe = GetFileType((HANDLE)handle) == FILE_TYPE_PIPE;
}
}
#endif
if (self->is_pipe) {
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
} else {
stream->position = lseek(self->fd, 0, SEEK_CUR);
#ifdef ESPIPE
if (stream->position == (off_t)-1 && errno == ESPIPE) {
stream->position = 0;
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
self->is_pipe = 1;
}
#endif
}
}
return stream;
}
PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC TSRMLS_DC)
{
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;
#ifdef S_ISFIFO
/* 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;
}
#elif defined(PHP_WIN32)
{
zend_uintptr_t handle = _get_osfhandle(self->fd);
if (handle != (zend_uintptr_t)INVALID_HANDLE_VALUE) {
self->is_pipe = GetFileType((HANDLE)handle) == FILE_TYPE_PIPE;
}
}
#endif
if (self->is_pipe) {
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
} else {
stream->position = ftell(file);
}
}
return stream;
}
PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STREAMS_DC TSRMLS_DC)
{
php_stdio_stream_data *self;
php_stream *stream;
self = emalloc_rel_orig(sizeof(*self));
memset(self, 0, sizeof(*self));
self->file = file;
self->is_pipe = 1;
self->lock_flag = LOCK_UN;
self->is_process_pipe = 1;
self->fd = fileno(file);
self->temp_file_name = NULL;
stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
return stream;
}
static size_t php_stdiop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
{
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
assert(data != NULL);
if (data->fd >= 0) {
int bytes_written = write(data->fd, buf, count);
if (bytes_written < 0) return 0;
return (size_t) bytes_written;
} else {
#if HAVE_FLUSHIO
if (!data->is_pipe && data->last_op == 'r') {
fseek(data->file, 0, SEEK_CUR);
}
data->last_op = 'w';
#endif
return fwrite(buf, 1, count, data->file);
}
}
static size_t php_stdiop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
{
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
size_t ret;
assert(data != NULL);
if (data->fd >= 0) {
ret = read(data->fd, buf, count);
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 */
ret = read(data->fd, buf, count);
}
stream->eof = (ret == 0 || (ret == (size_t)-1 && errno != EWOULDBLOCK && errno != EINTR && errno != EBADF));
} else {
#if HAVE_FLUSHIO
if (!data->is_pipe && data->last_op == 'w')
fseek(data->file, 0, SEEK_CUR);
data->last_op = 'r';
#endif
ret = fread(buf, 1, count, data->file);
stream->eof = feof(data->file);
}
return ret;
}
static int php_stdiop_close(php_stream *stream, int close_handle TSRMLS_DC)
{
int ret;
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
assert(data != NULL);
#if HAVE_MMAP
if (data->last_mapped_addr) {
munmap(data->last_mapped_addr, data->last_mapped_len);
data->last_mapped_addr = NULL;
}
#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;
}
#endif
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 */
}
if (data->temp_file_name) {
unlink(data->temp_file_name);
/* temporary streams are never persistent */
efree(data->temp_file_name);
data->temp_file_name = NULL;
}
} else {
ret = 0;
data->file = NULL;
data->fd = -1;
}
pefree(data, stream->is_persistent);
return ret;
}
static int php_stdiop_flush(php_stream *stream TSRMLS_DC)
{
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;
}
static int php_stdiop_seek(php_stream *stream, off_t offset, int whence, off_t *newoffset TSRMLS_DC)
{
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
int ret;
assert(data != NULL);
if (data->is_pipe) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot seek on a pipe");
return -1;
}
if (data->fd >= 0) {
off_t result;
result = lseek(data->fd, offset, whence);
if (result == (off_t)-1)
return -1;
*newoffset = result;
return 0;
} else {
ret = fseek(data->file, offset, whence);
*newoffset = ftell(data->file);
return ret;
}
}
static int php_stdiop_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
{
int fd;
php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
assert(data != NULL);
/* 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 */
char fixed_mode[5];
php_stream_mode_sanitize_fdopen_fopencookie(stream, fixed_mode);
data->file = fdopen(data->fd, fixed_mode);
if (data->file == NULL) {
return FAILURE;
}
}
*(FILE**)ret = data->file;
data->fd = -1;
}
return SUCCESS;
case PHP_STREAM_AS_FD_FOR_SELECT:
PHP_STDIOP_GET_FD(fd, data);
if (fd < 0) {
return FAILURE;
}
if (ret) {
*(int*)ret = fd;
}
return SUCCESS;
case PHP_STREAM_AS_FD:
PHP_STDIOP_GET_FD(fd, data);
if (fd < 0) {
return FAILURE;
}
if (data->file) {
fflush(data->file);
}
if (ret) {
*(int*)ret = fd;
}
return SUCCESS;
default:
return FAILURE;
}
}
static int php_stdiop_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
{
int ret;
php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
assert(data != NULL);
ret = do_fstat(data, 1);
memcpy(&ssb->sb, &data->sb, sizeof(ssb->sb));
return ret;
buf->st_nlink = 1;
t = data.nFileSizeHigh;
t = t << 32;
t |= data.nFileSizeLow;
buf->st_size = t;
buf->st_atime = FileTimeToUnixTime(data.ftLastAccessTime);
buf->st_ctime = FileTimeToUnixTime(data.ftCreationTime);
buf->st_mtime = FileTimeToUnixTime(data.ftLastWriteTime);
return 0;
}
/* }}} */
#endif
static int php_is_dir_ok(const cwd_state *state) /* {{{ */
{
struct stat buf;
if (php_sys_stat(state->cwd, &buf) == 0 && S_ISDIR(buf.st_mode))
return (0);
return (1);
}
/* }}} */
static int php_is_file_ok(const cwd_state *state) /* {{{ */
{
struct stat buf;
if (php_sys_stat(state->cwd, &buf) == 0 && S_ISREG(buf.st_mode))
return (0);
return (1);
}
/* }}} */
static void cwd_globals_ctor(virtual_cwd_globals *cwd_g TSRMLS_DC) /* {{{ */
{
CWD_STATE_COPY(&cwd_g->cwd, &main_cwd_state);
cwd_g->realpath_cache_size = 0;
cwd_g->realpath_cache_size_limit = REALPATH_CACHE_SIZE;
cwd_g->realpath_cache_ttl = REALPATH_CACHE_TTL;
memset(cwd_g->realpath_cache, 0, sizeof(cwd_g->realpath_cache));
}
/* }}} */
static void cwd_globals_dtor(virtual_cwd_globals *cwd_g TSRMLS_DC) /* {{{ */
{
CWD_STATE_FREE(&cwd_g->cwd);
realpath_cache_clean(TSRMLS_C);
}
/* }}} */
CWD_API void virtual_cwd_startup(void) /* {{{ */
{
char cwd[MAXPATHLEN];
char *result;
#ifdef NETWARE
result = getcwdpath(cwd, NULL, 1);
if(result)
{
char *c=cwd;
while(c = strchr(c, '\\'))
{
*c='/';
++c;
}
}
#else
result = getcwd(cwd, sizeof(cwd));
#endif
if (!result) {
cwd[0] = '\0';
}
main_cwd_state.cwd_length = strlen(cwd);
#ifdef TSRM_WIN32
if (main_cwd_state.cwd_length >= 2 && cwd[1] == ':') {
cwd[0] = toupper(cwd[0]);
}
#endif
main_cwd_state.cwd = strdup(cwd);
#ifdef ZTS
ts_allocate_id(&cwd_globals_id, sizeof(virtual_cwd_globals), (ts_allocate_ctor) cwd_globals_ctor, (ts_allocate_dtor) cwd_globals_dtor);
#else
cwd_globals_ctor(&cwd_globals TSRMLS_CC);
#endif
#if (defined(TSRM_WIN32) || defined(NETWARE)) && defined(ZTS)
cwd_mutex = tsrm_mutex_alloc();
#endif
}
/* }}} */
CWD_API void virtual_cwd_shutdown(void) /* {{{ */
{
#ifndef ZTS
cwd_globals_dtor(&cwd_globals TSRMLS_CC);
#endif
#if (defined(TSRM_WIN32) || defined(NETWARE)) && defined(ZTS)
tsrm_mutex_free(cwd_mutex);
#endif
free(main_cwd_state.cwd); /* Don't use CWD_STATE_FREE because the non global states will probably use emalloc()/efree() */
}
/* }}} */
CWD_API char *virtual_getcwd_ex(size_t *length TSRMLS_DC) /* {{{ */
{
cwd_state *state;
state = &CWDG(cwd);
if (state->cwd_length == 0) {
char *retval;
*length = 1;
retval = (char *) malloc(2);
if (retval == NULL) {
return NULL;
}
retval[0] = DEFAULT_SLASH;
retval[1] = '\0';
return retval;
}
#ifdef TSRM_WIN32
/* If we have something like C: */
if (state->cwd_length == 2 && state->cwd[state->cwd_length-1] == ':') {
char *retval;
*length = state->cwd_length+1;
retval = (char *) malloc(*length+1);
if (retval == NULL) {
return NULL;
}
memcpy(retval, state->cwd, *length);
retval[0] = toupper(retval[0]);
retval[*length-1] = DEFAULT_SLASH;
retval[*length] = '\0';
return retval;
}
#endif
*length = state->cwd_length;
return strdup(state->cwd);
}
/* }}} */
/* Same semantics as UNIX getcwd() */
CWD_API char *virtual_getcwd(char *buf, size_t size TSRMLS_DC) /* {{{ */
{
size_t length;
char *cwd;
cwd = virtual_getcwd_ex(&length TSRMLS_CC);
if (buf == NULL) {
return cwd;
}
if (length > size-1) {
free(cwd);
errno = ERANGE; /* Is this OK? */
return NULL;
}
memcpy(buf, cwd, length+1);
free(cwd);
return buf;
}
/* }}} */
#ifdef PHP_WIN32
static inline unsigned long realpath_cache_key(const char *path, int path_len TSRMLS_DC) /* {{{ */
{
register unsigned long h;
char *bucket_key_start = tsrm_win32_get_path_sid_key(path TSRMLS_CC);
char *bucket_key = (char *)bucket_key_start;
const char *e = bucket_key + strlen(bucket_key);
if (!bucket_key) {
return 0;
}
for (h = 2166136261U; bucket_key < e;) {
h *= 16777619;
h ^= *bucket_key++;
}
HeapFree(GetProcessHeap(), 0, (LPVOID)bucket_key_start);
return h;
}
/* }}} */
#else
static inline unsigned long realpath_cache_key(const char *path, int path_len) /* {{{ */
{
register unsigned long h;
const char *e = path + path_len;
for (h = 2166136261U; path < e;) {
h *= 16777619;
h ^= *path++;
}
return h;
}
/* }}} */
#endif /* defined(PHP_WIN32) */
CWD_API void realpath_cache_clean(TSRMLS_D) /* {{{ */
{
int i;
for (i = 0; i < sizeof(CWDG(realpath_cache))/sizeof(CWDG(realpath_cache)[0]); i++) {
realpath_cache_bucket *p = CWDG(realpath_cache)[i];
while (p != NULL) {
realpath_cache_bucket *r = p;
p = p->next;
free(r);
}
CWDG(realpath_cache)[i] = NULL;
}
CWDG(realpath_cache_size) = 0;
}
/* }}} */
CWD_API void realpath_cache_del(const char *path, int path_len TSRMLS_DC) /* {{{ */
{
#ifdef PHP_WIN32
unsigned long key = realpath_cache_key(path, path_len TSRMLS_CC);
#else
unsigned long key = realpath_cache_key(path, path_len);
#endif
unsigned long n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
while (*bucket != NULL) {
if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
memcmp(path, (*bucket)->path, path_len) == 0) {
realpath_cache_bucket *r = *bucket;
*bucket = (*bucket)->next;
/* if the pointers match then only subtract the length of the path */
if(r->path == r->realpath) {
CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1;
} else {
CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
}
free(r);
return;
} else {
bucket = &(*bucket)->next;
}
}
}
/* }}} */
static inline void realpath_cache_add(const char *path, int path_len, const char *realpath, int realpath_len, int is_dir, time_t t TSRMLS_DC) /* {{{ */
{
long size = sizeof(realpath_cache_bucket) + path_len + 1;
int same = 1;
if (realpath_len != path_len ||
memcmp(path, realpath, path_len) != 0) {
size += realpath_len + 1;
same = 0;
}
if (CWDG(realpath_cache_size) + size <= CWDG(realpath_cache_size_limit)) {
realpath_cache_bucket *bucket = malloc(size);
unsigned long n;
if (bucket == NULL) {
return;
}
#ifdef PHP_WIN32
bucket->key = realpath_cache_key(path, path_len TSRMLS_CC);
#else
bucket->key = realpath_cache_key(path, path_len);
#endif
bucket->path = (char*)bucket + sizeof(realpath_cache_bucket);
memcpy(bucket->path, path, path_len+1);
bucket->path_len = path_len;
if (same) {
bucket->realpath = bucket->path;
} else {
bucket->realpath = bucket->path + (path_len + 1);
memcpy(bucket->realpath, realpath, realpath_len+1);
}
bucket->realpath_len = realpath_len;
bucket->is_dir = is_dir;
#ifdef PHP_WIN32
bucket->is_rvalid = 0;
bucket->is_readable = 0;
bucket->is_wvalid = 0;
bucket->is_writable = 0;
#endif
bucket->expires = t + CWDG(realpath_cache_ttl);
n = bucket->key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
bucket->next = CWDG(realpath_cache)[n];
CWDG(realpath_cache)[n] = bucket;
CWDG(realpath_cache_size) += size;
}
}
/* }}} */
static inline realpath_cache_bucket* realpath_cache_find(const char *path, int path_len, time_t t TSRMLS_DC) /* {{{ */
{
#ifdef PHP_WIN32
unsigned long key = realpath_cache_key(path, path_len TSRMLS_CC);
#else
unsigned long key = realpath_cache_key(path, path_len);
#endif
unsigned long n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
while (*bucket != NULL) {
if (CWDG(realpath_cache_ttl) && (*bucket)->expires < t) {
realpath_cache_bucket *r = *bucket;
*bucket = (*bucket)->next;
/* if the pointers match then only subtract the length of the path */
if(r->path == r->realpath) {
CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1;
} else {
CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
}
free(r);
} else if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
memcmp(path, (*bucket)->path, path_len) == 0) {
return *bucket;
} else {
bucket = &(*bucket)->next;
}
}
return NULL;
}
/* }}} */
CWD_API realpath_cache_bucket* realpath_cache_lookup(const char *path, int path_len, time_t t TSRMLS_DC) /* {{{ */
{
return realpath_cache_find(path, path_len, t TSRMLS_CC);
}
/* }}} */
CWD_API int realpath_cache_size(TSRMLS_D)
{
return CWDG(realpath_cache_size);
}
CWD_API int realpath_cache_max_buckets(TSRMLS_D)
{
return (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
}
CWD_API realpath_cache_bucket** realpath_cache_get_buckets(TSRMLS_D)
{
return CWDG(realpath_cache);
}
static int php_stdiop_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC)
#undef LINK_MAX
#define LINK_MAX 32
static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, int use_realpath, int is_dir, int *link_is_dir TSRMLS_DC) /* {{{ */
{
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
PHP_STDIOP_GET_FD(fd, data);
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)
flags &= ~O_NONBLOCK;
else
flags |= O_NONBLOCK;
if (-1 == fcntl(fd, F_SETFL, flags))
return -1;
return oldval;
#else
return -1; /* not yet implemented */
#endif
case PHP_STREAM_OPTION_WRITE_BUFFER:
if (data->file == NULL) {
return -1;
}
if (ptrparam)
size = *(size_t *)ptrparam;
else
size = BUFSIZ;
switch(value) {
case PHP_STREAM_BUFFER_NONE:
return setvbuf(data->file, NULL, _IONBF, 0);
case PHP_STREAM_BUFFER_LINE:
return setvbuf(data->file, NULL, _IOLBF, size);
case PHP_STREAM_BUFFER_FULL:
return setvbuf(data->file, NULL, _IOFBF, size);
default:
return -1;
}
break;
case PHP_STREAM_OPTION_LOCKING:
if (fd == -1) {
return -1;
}
if ((zend_uintptr_t) ptrparam == PHP_STREAM_LOCK_SUPPORTED) {
return 0;
}
int i, j, save;
int directory = 0;
#ifdef TSRM_WIN32
WIN32_FIND_DATA data;
HANDLE hFind;
TSRM_ALLOCA_FLAG(use_heap_large)
#else
struct stat st;
#endif
realpath_cache_bucket *bucket;
char *tmp;
TSRM_ALLOCA_FLAG(use_heap)
while (1) {
if (len <= start) {
if (link_is_dir) {
*link_is_dir = 1;
}
return start;
}
i = len;
while (i > start && !IS_SLASH(path[i-1])) {
i--;
}
if (i == len ||
(i == len - 1 && path[i] == '.')) {
/* remove double slashes and '.' */
len = i - 1;
is_dir = 1;
continue;
} else if (i == len - 2 && path[i] == '.' && path[i+1] == '.') {
/* remove '..' and previous directory */
is_dir = 1;
if (link_is_dir) {
*link_is_dir = 1;
}
if (i - 1 <= start) {
return start ? start : len;
}
j = tsrm_realpath_r(path, start, i-1, ll, t, use_realpath, 1, NULL TSRMLS_CC);
if (j > start) {
j--;
while (j > start && !IS_SLASH(path[j])) {
j--;
}
if (!start) {
/* leading '..' must not be removed in case of relative path */
if (j == 0 && path[0] == '.' && path[1] == '.' &&
IS_SLASH(path[2])) {
path[3] = '.';
path[4] = '.';
path[5] = DEFAULT_SLASH;
j = 5;
} else if (j > 0 &&
path[j+1] == '.' && path[j+2] == '.' &&
IS_SLASH(path[j+3])) {
j += 4;
path[j++] = '.';
path[j++] = '.';
path[j] = DEFAULT_SLASH;
}
}
} else if (!start && !j) {
/* leading '..' must not be removed in case of relative path */
path[0] = '.';
path[1] = '.';
path[2] = DEFAULT_SLASH;
j = 2;
}
return j;
}
path[len] = 0;
save = (use_realpath != CWD_EXPAND);
if (start && save && CWDG(realpath_cache_size_limit)) {
/* cache lookup for absolute path */
if (!*t) {
*t = time(0);
}
if ((bucket = realpath_cache_find(path, len, *t TSRMLS_CC)) != NULL) {
if (is_dir && !bucket->is_dir) {
/* not a directory */
return -1;
} else {
if (link_is_dir) {
*link_is_dir = bucket->is_dir;
}
memcpy(path, bucket->realpath, bucket->realpath_len + 1);
return bucket->realpath_len;
}
}
}
#ifdef TSRM_WIN32
//if (save && (hFind = FindFirstFile(path, &data)) == INVALID_HANDLE_VALUE) { // mod@yur
if (save && (hFind = FindFirstFileWW(path, &data)) == INVALID_HANDLE_VALUE) { // add@yur
if (use_realpath == CWD_REALPATH) {
/* file not found */
return -1;
}
/* continue resolution anyway but don't save result in the cache */
save = 0;
}
if (save) {
FindClose(hFind);
}
tmp = tsrm_do_alloca(len+1, use_heap);
memcpy(tmp, path, len+1);
if(save &&
!(IS_UNC_PATH(path, len) && len >= 3 && path[2] != '?') &&
(data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
/* File is a reparse point. Get the target */
HANDLE hLink = NULL;
REPARSE_DATA_BUFFER * pbuffer;
unsigned int retlength = 0;
int bufindex = 0, isabsolute = 0;
wchar_t * reparsetarget;
BOOL isVolume = FALSE;
char printname[MAX_PATH];
char substitutename[MAX_PATH];
int printname_len, substitutename_len;
int substitutename_off = 0;
if(++(*ll) > LINK_MAX) {
return -1;
}
hLink = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL);
if(hLink == INVALID_HANDLE_VALUE) {
return -1;
}
pbuffer = (REPARSE_DATA_BUFFER *)tsrm_do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
if (pbuffer == NULL) {
return -1;
}
if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
tsrm_free_alloca(pbuffer, use_heap_large);
CloseHandle(hLink);
return -1;
}
CloseHandle(hLink);
if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
reparsetarget = pbuffer->SymbolicLinkReparseBuffer.ReparseTarget;
printname_len = pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR);
isabsolute = (pbuffer->SymbolicLinkReparseBuffer.Flags == 0) ? 1 : 0;
if (!WideCharToMultiByte(CP_THREAD_ACP, 0,
reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR),
printname_len + 1,
printname, MAX_PATH, NULL, NULL
)) {
tsrm_free_alloca(pbuffer, use_heap_large);
return -1;
};
printname_len = pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR);
printname[printname_len] = 0;
substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
if (!WideCharToMultiByte(CP_THREAD_ACP, 0,
reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR),
substitutename_len + 1,
substitutename, MAX_PATH, NULL, NULL
)) {
tsrm_free_alloca(pbuffer, use_heap_large);
return -1;
};
substitutename[substitutename_len] = 0;
}
else if(pbuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
isabsolute = 1;
reparsetarget = pbuffer->MountPointReparseBuffer.ReparseTarget;
printname_len = pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR);
if (!WideCharToMultiByte(CP_THREAD_ACP, 0,
reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR),
printname_len + 1,
printname, MAX_PATH, NULL, NULL
)) {
tsrm_free_alloca(pbuffer, use_heap_large);
return -1;
};
printname[pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR)] = 0;
substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
if (!WideCharToMultiByte(CP_THREAD_ACP, 0,
reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR),
substitutename_len + 1,
substitutename, MAX_PATH, NULL, NULL
)) {
tsrm_free_alloca(pbuffer, use_heap_large);
return -1;
};
substitutename[substitutename_len] = 0;
} else {
tsrm_free_alloca(pbuffer, use_heap_large);
return -1;
}
if(isabsolute && substitutename_len > 4) {
/* Do not resolve volumes (for now). A mounted point can
target a volume without a drive, it is not certain that
all IO functions we use in php and its deps support
path with volume GUID instead of the DOS way, like:
d:\test\mnt\foo
\\?\Volume{62d1c3f8-83b9-11de-b108-806e6f6e6963}\foo
*/
if (strncmp(substitutename, "\\??\\Volume{",11) == 0
|| strncmp(substitutename, "\\\\?\\Volume{",11) == 0
|| strncmp(substitutename, "\\??\\UNC\\", 8) == 0
) {
isVolume = TRUE;
substitutename_off = 0;
} else
/* do not use the \??\ and \\?\ prefix*/
if (strncmp(substitutename, "\\??\\", 4) == 0
|| strncmp(substitutename, "\\\\?\\", 4) == 0) {
substitutename_off = 4;
}
}
if (!isVolume) {
char * tmp2 = substitutename + substitutename_off;
for(bufindex = 0; bufindex < (substitutename_len - substitutename_off); bufindex++) {
*(path + bufindex) = *(tmp2 + bufindex);
}
*(path + bufindex) = 0;
j = bufindex;
} else {
j = len;
}
#if VIRTUAL_CWD_DEBUG
fprintf(stderr, "reparse: print: %s ", printname);
fprintf(stderr, "sub: %s ", substitutename);
fprintf(stderr, "resolved: %s ", path);
#endif
tsrm_free_alloca(pbuffer, use_heap_large);
if(isabsolute == 1) {
if (!((j == 3) && (path[1] == ':') && (path[2] == '\\'))) {
/* use_realpath is 0 in the call below coz path is absolute*/
j = tsrm_realpath_r(path, 0, j, ll, t, 0, is_dir, &directory TSRMLS_CC);
if(j < 0) {
tsrm_free_alloca(tmp, use_heap);
return -1;
}
}
}
else {
if(i + j >= MAXPATHLEN - 1) {
tsrm_free_alloca(tmp, use_heap);
return -1;
}
memmove(path+i, path, j+1);
memcpy(path, tmp, i-1);
path[i-1] = DEFAULT_SLASH;
j = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory TSRMLS_CC);
if(j < 0) {
tsrm_free_alloca(tmp, use_heap);
return -1;
}
}
directory = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
if(link_is_dir) {
*link_is_dir = directory;
}
}
else {
if (save) {
directory = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (is_dir && !directory) {
/* not a directory */
return -1;
}
}
#elif defined(NETWARE)
save = 0;
tmp = tsrm_do_alloca(len+1, use_heap);
memcpy(tmp, path, len+1);
#else
if (save && php_sys_lstat(path, &st) < 0) {
if (use_realpath == CWD_REALPATH) {
/* file not found */
return -1;
}
/* continue resolution anyway but don't save result in the cache */
save = 0;
}
if (!flock(fd, value)) {
data->lock_flag = value;
return 0;
tmp = tsrm_do_alloca(len+1, use_heap);
memcpy(tmp, path, len+1);
if (save && S_ISLNK(st.st_mode)) {
if (++(*ll) > LINK_MAX || (j = php_sys_readlink(tmp, path, MAXPATHLEN)) < 0) {
/* too many links or broken symlinks */
tsrm_free_alloca(tmp, use_heap);
return -1;
}
path[j] = 0;
if (IS_ABSOLUTE_PATH(path, j)) {
j = tsrm_realpath_r(path, 1, j, ll, t, use_realpath, is_dir, &directory TSRMLS_CC);
if (j < 0) {
tsrm_free_alloca(tmp, use_heap);
return -1;
}
} else {
if (i + j >= MAXPATHLEN-1) {
tsrm_free_alloca(tmp, use_heap);
return -1; /* buffer overflow */
}
memmove(path+i, path, j+1);
memcpy(path, tmp, i-1);
path[i-1] = DEFAULT_SLASH;
j = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory TSRMLS_CC);
if (j < 0) {
tsrm_free_alloca(tmp, use_heap);
return -1;
}
}
if (link_is_dir) {
*link_is_dir = directory;
}
} else {
if (save) {
directory = S_ISDIR(st.st_mode);
if (link_is_dir) {
*link_is_dir = directory;
}
if (is_dir && !directory) {
/* not a directory */
tsrm_free_alloca(tmp, use_heap);
return -1;
}
}
#endif
if (i - 1 <= start) {
j = start;
} else {
/* some leading directories may be unaccessable */
j = tsrm_realpath_r(path, start, i-1, ll, t, save ? CWD_FILEPATH : use_realpath, 1, NULL TSRMLS_CC);
if (j > start) {
path[j++] = DEFAULT_SLASH;
}
}
#ifdef TSRM_WIN32
if (j < 0 || j + len - i >= MAXPATHLEN-1) {
tsrm_free_alloca(tmp, use_heap);
return -1;
}
if (save) {
i = strlen(data.cFileName);
memcpy(path+j, data.cFileName, i+1);
j += i;
} else {
/* use the original file or directory name as it wasn't found */
memcpy(path+j, tmp+i, len-i+1);
j += (len-i);
}
}
#else
if (j < 0 || j + len - i >= MAXPATHLEN-1) {
tsrm_free_alloca(tmp, use_heap);
return -1;
}
break;
case PHP_STREAM_OPTION_MMAP_API:
#if HAVE_MMAP
{
php_stream_mmap_range *range = (php_stream_mmap_range*)ptrparam;
int prot, flags;
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:
do_fstat(data, 1);
if (range->length == 0 && range->offset > 0 && range->offset < data->sb.st_size) {
range->length = data->sb.st_size - range->offset;
}
if (range->length == 0 || range->length > data->sb.st_size) {
range->length = data->sb.st_size;
}
if (range->offset >= data->sb.st_size) {
range->offset = data->sb.st_size;
range->length = 0;
}
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;
memcpy(path+j, tmp+i, len-i+1);
j += (len-i);
}
#endif
return PHP_STREAM_OPTION_RETURN_OK;
}
return PHP_STREAM_OPTION_RETURN_ERR;
}
}
#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;
if (save && start && CWDG(realpath_cache_size_limit)) {
/* save absolute path in the cache */
realpath_cache_add(tmp, len, path, j, directory, *t TSRMLS_CC);
}
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;
default:
return PHP_STREAM_OPTION_RETURN_ERR;
}
/* 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;
}
tsrm_free_alloca(tmp, use_heap);
return j;
}
}
/* }}} */
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;
}
/* 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;
/* Resolve path relatively to state and put the real path into state */
/* returns 0 for ok, 1 for error */
CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath TSRMLS_DC) /* {{{ */
{
int path_length = strlen(path);
char resolved_path[MAXPATHLEN];
int start = 1;
int ll = 0;
time_t t;
int ret;
int add_slash;
void *tmp;
GetSystemInfo(&info);
gran = info.dwAllocationGranularity;
loffs = (range->offset / gran) * gran;
delta = range->offset - loffs;
}
data->last_mapped_addr = MapViewOfFile(data->file_mapping, acc, 0, loffs, range->length + delta);
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;
}
}
if (path_length == 0 || path_length >= MAXPATHLEN-1) {
#ifdef TSRM_WIN32
# if _MSC_VER < 1300
errno = EINVAL;
# else
_set_errno(EINVAL);
# endif
#else
errno = EINVAL;
#endif
return 1;
}
#if VIRTUAL_CWD_DEBUG
fprintf(stderr,"cwd = %s path = %s\n", state->cwd, path);
#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;
case PHP_STREAM_TRUNCATE_SET_SIZE: {
ptrdiff_t new_size = *(ptrdiff_t*)ptrparam;
if (new_size < 0) {
return PHP_STREAM_OPTION_RETURN_ERR;
/* cwd_length can be 0 when getcwd() fails.
* This can happen under solaris when a dir does not have read permissions
* but *does* have execute permissions */
if (!IS_ABSOLUTE_PATH(path, path_length)) {
if (state->cwd_length == 0) {
/* resolve relative path */
start = 0;
memcpy(resolved_path , path, path_length + 1);
} else {
int state_cwd_length = state->cwd_length;
#ifdef TSRM_WIN32
if (IS_SLASH(path[0])) {
if (state->cwd[1] == ':') {
/* Copy only the drive name */
state_cwd_length = 2;
} else if (IS_UNC_PATH(state->cwd, state->cwd_length)) {
/* Copy only the share name */
state_cwd_length = 2;
while (IS_SLASH(state->cwd[state_cwd_length])) {
state_cwd_length++;
}
return ftruncate(fd, new_size) == 0 ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
}
}
default:
return PHP_STREAM_OPTION_RETURN_NOTIMPL;
}
}
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
};
/* }}} */
/* {{{ plain files opendir/readdir implementation */
static size_t php_plain_files_dirstream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
{
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;
}
static int php_plain_files_dirstream_close(php_stream *stream, int close_handle TSRMLS_DC)
{
return closedir((DIR *)stream->abstract);
}
static int php_plain_files_dirstream_rewind(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
{
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 */
};
static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, char *path, char *mode,
int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
{
DIR *dir = NULL;
php_stream *stream = NULL;
#ifdef HAVE_GLOB
if (options & STREAM_USE_GLOB_DIR_OPEN) {
return php_glob_stream_wrapper.wops->dir_opener(&php_glob_stream_wrapper, path, mode, options, opened_path, context STREAMS_REL_CC TSRMLS_CC);
}
#endif
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path TSRMLS_CC)) {
return NULL;
}
dir = VCWD_OPENDIR(path);
#ifdef PHP_WIN32
if (!dir) {
php_win32_docref2_from_error(GetLastError(), path, path TSRMLS_CC);
}
if (dir && dir->finished) {
closedir(dir);
dir = NULL;
}
while (state->cwd[state_cwd_length] &&
!IS_SLASH(state->cwd[state_cwd_length])) {
state_cwd_length++;
}
while (IS_SLASH(state->cwd[state_cwd_length])) {
state_cwd_length++;
}
while (state->cwd[state_cwd_length] &&
!IS_SLASH(state->cwd[state_cwd_length])) {
state_cwd_length++;
}
}
}
#endif
if (path_length + state_cwd_length + 1 >= MAXPATHLEN-1) {
return 1;
}
memcpy(resolved_path, state->cwd, state_cwd_length);
if (resolved_path[state_cwd_length-1] == DEFAULT_SLASH) {
memcpy(resolved_path + state_cwd_length, path, path_length + 1);
path_length += state_cwd_length;
} else {
resolved_path[state_cwd_length] = DEFAULT_SLASH;
memcpy(resolved_path + state_cwd_length + 1, path, path_length + 1);
path_length += state_cwd_length + 1;
}
}
} else {
#ifdef TSRM_WIN32
if (path_length > 2 && path[1] == ':' && !IS_SLASH(path[2])) {
resolved_path[0] = path[0];
resolved_path[1] = ':';
resolved_path[2] = DEFAULT_SLASH;
memcpy(resolved_path + 3, path + 2, path_length - 1);
path_length++;
} else
#endif
if (dir) {
stream = php_stream_alloc(&php_plain_files_dirstream_ops, dir, 0, mode);
if (stream == NULL)
closedir(dir);
}
return stream;
}
/* }}} */
/* {{{ php_stream_fopen */
PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, char **opened_path, int options STREAMS_DC TSRMLS_DC)
{
char *realpath = NULL;
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) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "`%s' is not a valid mode for fopen", mode);
}
return NULL;
}
if (options & STREAM_ASSUME_REALPATH) {
realpath = estrdup(filename);
} else {
if ((realpath = expand_filepath(filename, NULL TSRMLS_CC)) == NULL) {
return NULL;
}
memcpy(resolved_path, path, path_length + 1);
}
#ifdef TSRM_WIN32
if (memchr(resolved_path, '*', path_length) ||
memchr(resolved_path, '?', path_length)) {
return 1;
}
#endif
if (persistent) {
spprintf(&persistent_id, 0, "streams_stdio_%d_%s", open_flags, realpath);
switch (php_stream_from_persistent_id(persistent_id, &ret TSRMLS_CC)) {
case PHP_STREAM_PERSISTENT_SUCCESS:
if (opened_path) {
*opened_path = realpath;
realpath = NULL;
}
/* fall through */
case PHP_STREAM_PERSISTENT_FAILURE:
if (realpath) {
efree(realpath);
}
efree(persistent_id);;
return ret;
}
}
//fd = open(realpath, open_flags, 0666); // mod@yur
fd = _wopen(UTF8toUTF163(realpath), open_flags, 0666); // add@yur
if (fd != -1) {
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);
}
if (ret) {
if (opened_path) {
*opened_path = realpath;
realpath = NULL;
}
if (realpath) {
efree(realpath);
}
if (persistent_id) {
efree(persistent_id);
}
/* WIN32 always set ISREG flag */
#ifndef PHP_WIN32
/* 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);
if ((r == 0 && !S_ISREG(self->sb.st_mode))) {
if (opened_path) {
efree(*opened_path);
*opened_path = NULL;
}
php_stream_close(ret);
return NULL;
}
#ifdef TSRM_WIN32
if (IS_UNC_PATH(resolved_path, path_length)) {
/* skip UNC name */
resolved_path[0] = DEFAULT_SLASH;
resolved_path[1] = DEFAULT_SLASH;
start = 2;
while (!IS_SLASH(resolved_path[start])) {
if (resolved_path[start] == 0) {
goto verify;
}
#endif
return ret;
resolved_path[start] = toupper(resolved_path[start]);
start++;
}
close(fd);
}
efree(realpath);
if (persistent_id) {
efree(persistent_id);
}
return NULL;
}
/* }}} */
static php_stream *php_plain_files_stream_opener(php_stream_wrapper *wrapper, char *path, char *mode,
int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
{
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path TSRMLS_CC)) {
return NULL;
}
return php_stream_fopen_rel(path, mode, opened_path, options);
}
static int php_plain_files_url_stater(php_stream_wrapper *wrapper, char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context TSRMLS_DC)
{
if (strncmp(url, "file://", sizeof("file://") - 1) == 0) {
url += sizeof("file://") - 1;
}
if (php_check_open_basedir_ex(url, (flags & PHP_STREAM_URL_STAT_QUIET) ? 0 : 1 TSRMLS_CC)) {
return -1;
}
#ifdef PHP_WIN32
if (EG(windows_version_info).dwMajorVersion >= 5) {
if (flags & PHP_STREAM_URL_STAT_LINK) {
return VCWD_LSTAT(url, &ssb->sb);
resolved_path[start++] = DEFAULT_SLASH;
while (!IS_SLASH(resolved_path[start])) {
if (resolved_path[start] == 0) {
goto verify;
}
resolved_path[start] = toupper(resolved_path[start]);
start++;
}
resolved_path[start++] = DEFAULT_SLASH;
} else if (IS_ABSOLUTE_PATH(resolved_path, path_length)) {
/* skip DRIVE name */
resolved_path[0] = toupper(resolved_path[0]);
resolved_path[2] = DEFAULT_SLASH;
start = 3;
}
#elif defined(NETWARE)
if (IS_ABSOLUTE_PATH(resolved_path, path_length)) {
/* skip VOLUME name */
start = 0;
while (start != ':') {
if (resolved_path[start] == 0) return -1;
start++;
}
start++;
if (!IS_SLASH(resolved_path[start])) return -1;
resolved_path[start++] = DEFAULT_SLASH;
}
#endif
add_slash = (use_realpath != CWD_REALPATH) && path_length > 0 && IS_SLASH(resolved_path[path_length-1]);
t = CWDG(realpath_cache_ttl) ? 0 : -1;
path_length = tsrm_realpath_r(resolved_path, start, path_length, &ll, &t, use_realpath, 0, NULL TSRMLS_CC);
if (path_length < 0) {
errno = ENOENT;
return 1;
}
if (!start && !path_length) {
resolved_path[path_length++] = '.';
}
if (add_slash && path_length && !IS_SLASH(resolved_path[path_length-1])) {
if (path_length >= MAXPATHLEN-1) {
return -1;
}
resolved_path[path_length++] = DEFAULT_SLASH;
}
resolved_path[path_length] = 0;
#ifdef TSRM_WIN32
verify:
#endif
if (verify_path) {
cwd_state old_state;
CWD_STATE_COPY(&old_state, state);
state->cwd_length = path_length;
tmp = realloc(state->cwd, state->cwd_length+1);
if (tmp == NULL) {
#if VIRTUAL_CWD_DEBUG
fprintf (stderr, "Out of memory\n");
#endif
return 1;
}
state->cwd = (char *) tmp;
memcpy(state->cwd, resolved_path, state->cwd_length+1);
if (verify_path(state)) {
CWD_STATE_FREE(state);
*state = old_state;
ret = 1;
} else {
CWD_STATE_FREE(&old_state);
ret = 0;
}
} else {
state->cwd_length = path_length;
tmp = realloc(state->cwd, state->cwd_length+1);
if (tmp == NULL) {
#if VIRTUAL_CWD_DEBUG
fprintf (stderr, "Out of memory\n");
#endif
return 1;
}
state->cwd = (char *) tmp;
memcpy(state->cwd, resolved_path, state->cwd_length+1);
ret = 0;
}
#if VIRTUAL_CWD_DEBUG
fprintf (stderr, "virtual_file_ex() = %s\n",state->cwd);
#endif
return (ret);
}
/* }}} */
CWD_API int virtual_chdir(const char *path TSRMLS_DC) /* {{{ */
{
return virtual_file_ex(&CWDG(cwd), path, php_is_dir_ok, CWD_REALPATH TSRMLS_CC)?-1:0;
}
/* }}} */
CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path TSRMLS_DC) TSRMLS_DC) /* {{{ */
{
int length = strlen(path);
char *temp;
int retval;
TSRM_ALLOCA_FLAG(use_heap)
if (length == 0) {
return 1; /* Can't cd to empty string */
}
while(--length >= 0 && !IS_SLASH(path[length])) {
}
if (length == -1) {
/* No directory only file name */
errno = ENOENT;
return -1;
}
if (length == COPY_WHEN_ABSOLUTE(path) && IS_ABSOLUTE_PATH(path, length+1)) { /* Also use trailing slash if this is absolute */
length++;
}
temp = (char *) tsrm_do_alloca(length+1, use_heap);
memcpy(temp, path, length);
temp[length] = 0;
#if VIRTUAL_CWD_DEBUG
fprintf (stderr, "Changing directory to %s\n", temp);
#endif
retval = p_chdir(temp TSRMLS_CC);
tsrm_free_alloca(temp, use_heap);
return retval;
}
/* }}} */
CWD_API char *virtual_realpath(const char *path, char *real_path TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
char *retval;
char cwd[MAXPATHLEN];
/* realpath("") returns CWD */
if (!*path) {
new_state.cwd = (char*)malloc(1);
if (new_state.cwd == NULL) {
retval = NULL;
goto end;
}
new_state.cwd[0] = '\0';
new_state.cwd_length = 0;
if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
path = cwd;
}
} else if (!IS_ABSOLUTE_PATH(path, strlen(path))) {
CWD_STATE_COPY(&new_state, &CWDG(cwd));
} else {
new_state.cwd = (char*)malloc(1);
if (new_state.cwd == NULL) {
retval = NULL;
goto end;
}
new_state.cwd[0] = '\0';
new_state.cwd_length = 0;
}
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH TSRMLS_CC)==0) {
int len = new_state.cwd_length>MAXPATHLEN-1?MAXPATHLEN-1:new_state.cwd_length;
memcpy(real_path, new_state.cwd, len);
real_path[len] = '\0';
retval = real_path;
} else {
retval = NULL;
}
CWD_STATE_FREE(&new_state);
end:
return retval;
}
/* }}} */
CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
int retval;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
retval = virtual_file_ex(&new_state, path, verify_path, CWD_FILEPATH TSRMLS_CC);
*filepath = new_state.cwd;
return retval;
}
/* }}} */
CWD_API int virtual_filepath(const char *path, char **filepath TSRMLS_DC) /* {{{ */
{
return virtual_filepath_ex(path, filepath, php_is_file_ok TSRMLS_CC);
}
/* }}} */
CWD_API FILE *virtual_fopen(const char *path, const char *mode TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
FILE *f;
if (path[0] == '\0') { /* Fail to open empty path */
return NULL;
}
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return NULL;
}
//f = fopen(new_state.cwd, mode); // mod@yur
f = _wfopen(UTF8toUTF16(new_state.cwd), UTF8toUTF16(mode)); // add@yur
CWD_STATE_FREE(&new_state);
return f;
}
/* }}} */
CWD_API int virtual_access(const char *pathname, int mode TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
int ret;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return -1;
}
#if defined(TSRM_WIN32)
ret = tsrm_win32_access(new_state.cwd, mode TSRMLS_CC);
#else
# ifdef HAVE_SYMLINK
if (flags & PHP_STREAM_URL_STAT_LINK) {
return VCWD_LSTAT(url, &ssb->sb);
} else
# endif
ret = access(new_state.cwd, mode);
#endif
return VCWD_STAT(url, &ssb->sb);
}
static int php_plain_files_unlink(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC)
{
char *p;
int ret;
if ((p = strstr(url, "://")) != NULL) {
url = p + 3;
}
if (php_check_open_basedir(url TSRMLS_CC)) {
return 0;
}
CWD_STATE_FREE(&new_state);
return ret;
}
/* }}} */
#if HAVE_UTIME
#ifdef TSRM_WIN32
static void UnixTimeToFileTime(time_t t, LPFILETIME pft) /* {{{ */
{
// Note that LONGLONG is a 64-bit value
LONGLONG ll;
ll = Int32x32To64(t, 10000000) + 116444736000000000;
pft->dwLowDateTime = (DWORD)ll;
pft->dwHighDateTime = ll >> 32;
}
/* }}} */
ret = VCWD_UNLINK(url);
if (ret == -1) {
if (options & REPORT_ERRORS) {
php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "%s", strerror(errno));
}
return 0;
TSRM_API int win32_utime(const char *filename, struct utimbuf *buf) /* {{{ */
{
FILETIME mtime, atime;
HANDLE hFile;
hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, NULL);
/* OPEN_ALWAYS mode sets the last error to ERROR_ALREADY_EXISTS but
the CreateFile operation succeeds */
if (GetLastError() == ERROR_ALREADY_EXISTS) {
SetLastError(0);
}
/* Clear stat cache (and realpath cache) */
php_clear_stat_cache(1, NULL, 0 TSRMLS_CC);
return 1;
}
static int php_plain_files_rename(php_stream_wrapper *wrapper, char *url_from, char *url_to, int options, php_stream_context *context TSRMLS_DC)
{
char *p;
int ret;
if (!url_from || !url_to) {
return 0;
}
#ifdef PHP_WIN32
if (!php_win32_check_trailing_space(url_from, strlen(url_from))) {
php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to TSRMLS_CC);
return 0;
}
if (!php_win32_check_trailing_space(url_to, strlen(url_to))) {
php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to TSRMLS_CC);
return 0;
}
#endif
if ((p = strstr(url_from, "://")) != NULL) {
url_from = p + 3;
}
if ((p = strstr(url_to, "://")) != NULL) {
url_to = p + 3;
if ( hFile == INVALID_HANDLE_VALUE ) {
return -1;
}
if (!buf) {
SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &mtime);
atime = mtime;
} else {
UnixTimeToFileTime(buf->modtime, &mtime);
UnixTimeToFileTime(buf->actime, &atime);
}
if (php_check_open_basedir(url_from TSRMLS_CC) || php_check_open_basedir(url_to TSRMLS_CC)) {
return 0;
if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
CloseHandle(hFile);
return -1;
}
ret = VCWD_RENAME(url_from, url_to);
if (ret == -1) {
#ifndef PHP_WIN32
# ifdef EXDEV
if (errno == EXDEV) {
struct stat sb;
if (php_copy_file(url_from, url_to TSRMLS_CC) == SUCCESS) {
if (VCWD_STAT(url_from, &sb) == 0) {
# if !defined(TSRM_WIN32) && !defined(NETWARE)
if (VCWD_CHMOD(url_to, sb.st_mode)) {
if (errno == EPERM) {
php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
VCWD_UNLINK(url_from);
return 1;
}
php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
return 0;
}
if (VCWD_CHOWN(url_to, sb.st_uid, sb.st_gid)) {
if (errno == EPERM) {
php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
VCWD_UNLINK(url_from);
return 1;
}
php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
return 0;
}
# endif
VCWD_UNLINK(url_from);
return 1;
}
}
php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
return 0;
}
# endif
#endif
#ifdef PHP_WIN32
php_win32_docref2_from_error(GetLastError(), url_from, url_to TSRMLS_CC);
#else
php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
CloseHandle(hFile);
return 1;
}
/* }}} */
#endif
CWD_API int virtual_utime(const char *filename, struct utimbuf *buf TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
int ret;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return -1;
}
#ifdef TSRM_WIN32
ret = win32_utime(new_state.cwd, buf);
#else
ret = utime(new_state.cwd, buf);
#endif
CWD_STATE_FREE(&new_state);
return ret;
}
/* }}} */
#endif
CWD_API int virtual_chmod(const char *filename, mode_t mode TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
int ret;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return -1;
}
ret = chmod(new_state.cwd, mode);
CWD_STATE_FREE(&new_state);
return ret;
}
/* }}} */
#if !defined(TSRM_WIN32) && !defined(NETWARE)
CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
int ret;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return -1;
}
if (link) {
#if HAVE_LCHOWN
ret = lchown(new_state.cwd, owner, group);
#else
ret = -1;
#endif
} else {
ret = chown(new_state.cwd, owner, group);
}
CWD_STATE_FREE(&new_state);
return ret;
}
/* }}} */
#endif
CWD_API int virtual_open(const char *path TSRMLS_DC, int flags, ...) /* {{{ */
{
cwd_state new_state;
int f;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return -1;
}
if (flags & O_CREAT) {
mode_t mode;
va_list arg;
va_start(arg, flags);
mode = (mode_t) va_arg(arg, int);
va_end(arg);
f = open(new_state.cwd, flags, mode); // warn@yur
} else {
f = open(new_state.cwd, flags); // warn@yur
}
CWD_STATE_FREE(&new_state);
return f;
}
/* }}} */
CWD_API int virtual_creat(const char *path, mode_t mode TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
int f;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return -1;
}
f = creat(new_state.cwd, mode);
CWD_STATE_FREE(&new_state);
return f;
}
/* }}} */
CWD_API int virtual_rename(char *oldname, char *newname TSRMLS_DC) /* {{{ */
{
cwd_state old_state;
cwd_state new_state;
int retval;
CWD_STATE_COPY(&old_state, &CWDG(cwd));
if (virtual_file_ex(&old_state, oldname, NULL, CWD_EXPAND TSRMLS_CC)) {
CWD_STATE_FREE(&old_state);
return -1;
}
oldname = old_state.cwd;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, newname, NULL, CWD_EXPAND TSRMLS_CC)) {
CWD_STATE_FREE(&old_state);
CWD_STATE_FREE(&new_state);
return -1;
}
newname = new_state.cwd;
/* rename on windows will fail if newname already exists.
MoveFileEx has to be used */
#ifdef TSRM_WIN32
/* MoveFileEx returns 0 on failure, other way 'round for this function */
retval = (MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED) == 0) ? -1 : 0;
#else
retval = rename(oldname, newname);
#endif
return 0;
CWD_STATE_FREE(&old_state);
CWD_STATE_FREE(&new_state);
return retval;
}
/* }}} */
CWD_API int virtual_stat(const char *path, struct stat *buf TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
int retval;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return -1;
}
retval = php_sys_stat(new_state.cwd, buf);
CWD_STATE_FREE(&new_state);
return retval;
}
/* }}} */
CWD_API int virtual_lstat(const char *path, struct stat *buf TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
int retval;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return -1;
}
retval = php_sys_lstat(new_state.cwd, buf);
CWD_STATE_FREE(&new_state);
return retval;
}
/* }}} */
CWD_API int virtual_unlink(const char *path TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
int retval;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return -1;
}
retval = unlink(new_state.cwd);
CWD_STATE_FREE(&new_state);
return retval;
}
/* }}} */
CWD_API int virtual_mkdir(const char *pathname, mode_t mode TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
int retval;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, pathname, NULL, CWD_FILEPATH TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return -1;
}
/* Clear stat cache (and realpath cache) */
php_clear_stat_cache(1, NULL, 0 TSRMLS_CC);
return 1;
}
static int php_plain_files_mkdir(php_stream_wrapper *wrapper, char *dir, int mode, int options, php_stream_context *context TSRMLS_DC)
{
int ret, recursive = options & PHP_STREAM_MKDIR_RECURSIVE;
char *p;
#ifdef TSRM_WIN32
retval = mkdir(new_state.cwd);
#else
retval = mkdir(new_state.cwd, mode);
#endif
CWD_STATE_FREE(&new_state);
return retval;
}
/* }}} */
CWD_API int virtual_rmdir(const char *pathname TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
int retval;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, pathname, NULL, CWD_EXPAND TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return -1;
}
retval = rmdir(new_state.cwd);
CWD_STATE_FREE(&new_state);
return retval;
}
/* }}} */
#ifdef TSRM_WIN32
DIR *opendir(const char *name);
#endif
CWD_API DIR *virtual_opendir(const char *pathname TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
DIR *retval;
CWD_STATE_COPY(&new_state, &CWDG(cwd));
if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH TSRMLS_CC)) {
CWD_STATE_FREE(&new_state);
return NULL;
}
retval = opendir(new_state.cwd);
CWD_STATE_FREE(&new_state);
return retval;
}
/* }}} */
#ifdef TSRM_WIN32
CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC) /* {{{ */
{
return popen_ex(command, type, CWDG(cwd).cwd, NULL TSRMLS_CC);
}
/* }}} */
#elif defined(NETWARE)
/* On NetWare, the trick of prepending "cd cwd; " doesn't work so we need to perform
a VCWD_CHDIR() and mutex it
*/
CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC) /* {{{ */
{
char prev_cwd[MAXPATHLEN];
char *getcwd_result;
FILE *retval;
getcwd_result = VCWD_GETCWD(prev_cwd, MAXPATHLEN);
if (!getcwd_result) {
return NULL;
}
#ifdef ZTS
tsrm_mutex_lock(cwd_mutex);
#endif
VCWD_CHDIR(CWDG(cwd).cwd);
retval = popen(command, type);
VCWD_CHDIR(prev_cwd);
#ifdef ZTS
tsrm_mutex_unlock(cwd_mutex);
#endif
if ((p = strstr(dir, "://")) != NULL) {
dir = p + 3;
return retval;
}
/* }}} */
#else /* Unix */
CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC) /* {{{ */
{
int command_length;
int dir_length, extra = 0;
char *command_line;
char *ptr, *dir;
FILE *retval;
command_length = strlen(command);
dir_length = CWDG(cwd).cwd_length;
dir = CWDG(cwd).cwd;
while (dir_length > 0) {
if (*dir == '\'') extra+=3;
dir++;
dir_length--;
}
dir_length = CWDG(cwd).cwd_length;
dir = CWDG(cwd).cwd;
ptr = command_line = (char *) malloc(command_length + sizeof("cd '' ; ") + dir_length + extra+1+1);
if (!command_line) {
return NULL;
}
memcpy(ptr, "cd ", sizeof("cd ")-1);
ptr += sizeof("cd ")-1;
if (!recursive) {
ret = php_mkdir(dir, mode TSRMLS_CC);
if (CWDG(cwd).cwd_length == 0) {
*ptr++ = DEFAULT_SLASH;
} else {
/* we look for directory separator from the end of string, thus hopefuly reducing our work load */
char *e;
struct stat sb;
int dir_len = strlen(dir);
int offset = 0;
char buf[MAXPATHLEN];
if (!expand_filepath_with_mode(dir, buf, NULL, 0, CWD_EXPAND TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid path");
return 0;
}
e = buf + strlen(buf);
if ((p = memchr(buf, DEFAULT_SLASH, dir_len))) {
offset = p - buf + 1;
}
if (p && dir_len == 1) {
/* buf == "DEFAULT_SLASH" */
}
else {
/* find a top level directory we need to create */
while ( (p = strrchr(buf + offset, DEFAULT_SLASH)) || (offset != 1 && (p = strrchr(buf, DEFAULT_SLASH))) ) {
int n = 0;
*p = '\0';
while (p > buf && *(p-1) == DEFAULT_SLASH) {
++n;
--p;
*p = '\0';
}
if (VCWD_STAT(buf, &sb) == 0) {
while (1) {
*p = DEFAULT_SLASH;
if (!n) break;
--n;
++p;
}
break;
}
*ptr++ = '\'';
while (dir_length > 0) {
switch (*dir) {
case '\'':
*ptr++ = '\'';
*ptr++ = '\\';
*ptr++ = '\'';
/* fall-through */
default:
*ptr++ = *dir;
}
dir++;
dir_length--;
}
if (p == buf) {
ret = php_mkdir(dir, mode TSRMLS_CC);
} else if (!(ret = php_mkdir(buf, mode TSRMLS_CC))) {
if (!p) {
p = buf;
}
/* create any needed directories if the creation of the 1st directory worked */
while (++p != e) {
if (*p == '\0') {
*p = DEFAULT_SLASH;
if ((*(p+1) != '\0') &&
(ret = VCWD_MKDIR(buf, (mode_t)mode)) < 0) {
if (options & REPORT_ERRORS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
}
break;
}
}
}
}
}
if (ret < 0) {
/* Failure */
return 0;
} else {
/* Success */
return 1;
}
}
static int php_plain_files_rmdir(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC)
{
#if PHP_WIN32
int url_len = strlen(url);
#endif
if (php_check_open_basedir(url TSRMLS_CC)) {
return 0;
*ptr++ = '\'';
}
#if PHP_WIN32
if (!php_win32_check_trailing_space(url, url_len)) {
php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "%s", strerror(ENOENT));
return 0;
}
#endif
if (VCWD_RMDIR(url) < 0) {
php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "%s", strerror(errno));
return 0;
}
/* Clear stat cache (and realpath cache) */
php_clear_stat_cache(1, NULL, 0 TSRMLS_CC);
*ptr++ = ' ';
*ptr++ = ';';
*ptr++ = ' ';
return 1;
memcpy(ptr, command, command_length+1);
retval = popen(command_line, type);
free(command_line);
return retval;
}
static int php_plain_files_metadata(php_stream_wrapper *wrapper, char *url, int option, void *value, php_stream_context *context TSRMLS_DC)
{
struct utimbuf *newtime;
char *p;
#if !defined(WINDOWS) && !defined(NETWARE)
uid_t uid;
gid_t gid;
#endif
mode_t mode;
int ret = 0;
#if PHP_WIN32
int url_len = strlen(url);
/* }}} */
#endif
#if PHP_WIN32
if (!php_win32_check_trailing_space(url, url_len)) {
php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "%s", strerror(ENOENT));
return 0;
}
#endif
if ((p = strstr(url, "://")) != NULL) {
url = p + 3;
}
if (php_check_open_basedir(url TSRMLS_CC)) {
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) {
php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "Unable to create file %s because %s", url, strerror(errno));
return 0;
}
fclose(file);
}
CWD_API char *tsrm_realpath(const char *path, char *real_path TSRMLS_DC) /* {{{ */
{
cwd_state new_state;
char cwd[MAXPATHLEN];
ret = VCWD_UTIME(url, newtime);
break;
#if !defined(WINDOWS) && !defined(NETWARE)
case PHP_STREAM_META_OWNER_NAME:
case PHP_STREAM_META_OWNER:
if(option == PHP_STREAM_META_OWNER_NAME) {
if(php_get_uid_by_name((char *)value, &uid TSRMLS_CC) != SUCCESS) {
php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "Unable to find uid for %s", (char *)value);
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:
if(option == PHP_STREAM_META_OWNER_NAME) {
if(php_get_gid_by_name((char *)value, &gid TSRMLS_CC) != SUCCESS) {
php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "Unable to find gid for %s", (char *)value);
return 0;
}
} else {
gid = (gid_t)*(long *)value;
}
ret = VCWD_CHOWN(url, -1, gid);
break;
#endif
case PHP_STREAM_META_ACCESS:
mode = (mode_t)*(long *)value;
ret = VCWD_CHMOD(url, mode);
break;
default:
php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "Unknown option %d for stream_metadata", option);
return 0;
}
if (ret == -1) {
php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "Operation failed: %s", strerror(errno));
return 0;
}
php_clear_stat_cache(0, NULL, 0 TSRMLS_CC);
return 1;
}
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,
"plainfile",
php_plain_files_unlink,
php_plain_files_rename,
php_plain_files_mkdir,
php_plain_files_rmdir,
php_plain_files_metadata
};
php_stream_wrapper php_plain_files_wrapper = {
&php_plain_files_wrapper_ops,
NULL,
0
};
/* {{{ php_stream_fopen_with_path */
PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char *path, char **opened_path, int options STREAMS_DC TSRMLS_DC)
{
/* code ripped off from fopen_wrappers.c */
char *pathbuf, *ptr, *end;
const char *exec_fname;
char trypath[MAXPATHLEN];
php_stream *stream;
int path_length;
int filename_length;
int exec_fname_length;
if (opened_path) {
*opened_path = NULL;
}
if(!filename) {
return NULL;
}
filename_length = strlen(filename);
/* 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;
}
}
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename TSRMLS_CC)) {
return NULL;
}
return php_stream_fopen_rel(filename, mode, opened_path, options);
}
not_relative_path:
/* Absolute path open */
if (IS_ABSOLUTE_PATH(filename, filename_length)) {
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename TSRMLS_CC)) {
return NULL;
}
return php_stream_fopen_rel(filename, mode, opened_path, options);
}
#ifdef PHP_WIN32
if (IS_SLASH(filename[0])) {
size_t cwd_len;
char *cwd;
cwd = virtual_getcwd_ex(&cwd_len TSRMLS_CC);
/* getcwd() will return always return [DRIVE_LETTER]:/) on windows. */
*(cwd+3) = '\0';
if (snprintf(trypath, MAXPATHLEN, "%s%s", cwd, filename) >= MAXPATHLEN) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s/%s path was truncated to %d", cwd, filename, MAXPATHLEN);
/* realpath("") returns CWD */
if (!*path) {
new_state.cwd = (char*)malloc(1);
if (new_state.cwd == NULL) {
return NULL;
}
free(cwd);
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(trypath TSRMLS_CC)) {
return NULL;
}
return php_stream_fopen_rel(trypath, mode, opened_path, options);
}
#endif
if (!path || (path && !*path)) {
return php_stream_fopen_rel(filename, mode, opened_path, options);
}
/* check in provided path */
/* append the calling scripts' current working directory
* as a fall back case
*/
if (zend_is_executing(TSRMLS_C)) {
exec_fname = zend_get_executed_filename(TSRMLS_C);
exec_fname_length = strlen(exec_fname);
path_length = strlen(path);
while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
if ((exec_fname && exec_fname[0] == '[')
|| exec_fname_length<=0) {
/* [no active file] or no path */
pathbuf = estrdup(path);
} else {
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';
new_state.cwd[0] = '\0';
new_state.cwd_length = 0;
if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
path = cwd;
}
} else if (!IS_ABSOLUTE_PATH(path, strlen(path)) &&
VCWD_GETCWD(cwd, MAXPATHLEN)) {
new_state.cwd = strdup(cwd);
new_state.cwd_length = strlen(cwd);
} else {
pathbuf = estrdup(path);
new_state.cwd = (char*)malloc(1);
if (new_state.cwd == NULL) {
return NULL;
}
new_state.cwd[0] = '\0';
new_state.cwd_length = 0;
}
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH TSRMLS_CC)) {
free(new_state.cwd);
return NULL;
}
ptr = pathbuf;
while (ptr && *ptr) {
end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
if (end != NULL) {
*end = '\0';
end++;
}
if (*ptr == '\0') {
goto stream_skip;
}
if (snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename) >= MAXPATHLEN) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s/%s path was truncated to %d", ptr, filename, MAXPATHLEN);
}
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir_ex(trypath, 0 TSRMLS_CC)) {
goto stream_skip;
}
stream = php_stream_fopen_rel(trypath, mode, opened_path, options);
if (stream) {
efree(pathbuf);
return stream;
}
stream_skip:
ptr = end;
} /* end provided path */
efree(pathbuf);
return NULL;
if (real_path) {
int copy_len = new_state.cwd_length>MAXPATHLEN-1 ? MAXPATHLEN-1 : new_state.cwd_length;
memcpy(real_path, new_state.cwd, copy_len);
real_path[copy_len] = '\0';
free(new_state.cwd);
return real_path;
} else {
return new_state.cwd;
}
}
/* }}} */
/*
* 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
*/
|