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