Avoid unused fstat() call

If we're including a file via PHP streams, we're not going to trust
the reported file size anyway and populate in a loop -- so don't
bother determining the file size in the first place. Only do this
for non-tty HANDLE_FP now, which is the only case where this
information was used.
This commit is contained in:
Nikita Popov 2019-07-17 11:01:57 +02:00
parent 6fbab09ef0
commit 5a90dc77b8
4 changed files with 19 additions and 65 deletions

View file

@ -23,6 +23,10 @@
#include "zend_compile.h"
#include "zend_stream.h"
#ifndef S_ISREG
# define S_ISREG(m) 1
#endif
ZEND_DLIMPORT int isatty(int fd);
static size_t zend_stream_stdio_reader(void *handle, char *buf, size_t len) /* {{{ */
@ -37,39 +41,6 @@ static void zend_stream_stdio_closer(void *handle) /* {{{ */
}
} /* }}} */
static size_t zend_stream_stdio_fsizer(void *handle) /* {{{ */
{
zend_stat_t buf;
if (handle && zend_fstat(fileno((FILE*)handle), &buf) == 0) {
#ifdef S_ISREG
if (!S_ISREG(buf.st_mode)) {
return 0;
}
#endif
return buf.st_size;
}
return 0;
} /* }}} */
static size_t zend_stream_fsize(zend_file_handle *file_handle) /* {{{ */
{
zend_stat_t buf;
if (file_handle->type == ZEND_HANDLE_STREAM) {
return file_handle->handle.stream.fsizer(file_handle->handle.stream.handle);
}
if (file_handle->handle.fp && zend_fstat(fileno(file_handle->handle.fp), &buf) == 0) {
#ifdef S_ISREG
if (!S_ISREG(buf.st_mode)) {
return 0;
}
#endif
return buf.st_size;
}
return -1;
} /* }}} */
ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename) {
memset(handle, 0, sizeof(zend_file_handle));
handle->type = ZEND_HANDLE_FP;
@ -125,8 +96,7 @@ static size_t zend_stream_read(zend_file_handle *file_handle, char *buf, size_t
ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t *len) /* {{{ */
{
size_t size;
zend_bool is_fp = 0;
size_t size = 0;
if (file_handle->buf) {
*buf = file_handle->buf;
@ -141,25 +111,28 @@ ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t
}
if (file_handle->type == ZEND_HANDLE_FP) {
if (!file_handle->handle.fp) {
FILE *fp = file_handle->handle.fp;
int is_tty;
if (!fp) {
return FAILURE;
}
is_fp = 1;
is_tty = isatty(fileno(fp));
if (!is_tty) {
zend_stat_t buf;
if (zend_fstat(fileno(fp), &buf) == 0 && S_ISREG(buf.st_mode)) {
size = buf.st_size;
}
}
file_handle->type = ZEND_HANDLE_STREAM;
file_handle->handle.stream.handle = file_handle->handle.fp;
file_handle->handle.stream.isatty = isatty(fileno((FILE *)file_handle->handle.stream.handle));
file_handle->handle.stream.handle = fp;
file_handle->handle.stream.isatty = is_tty;
file_handle->handle.stream.reader = (zend_stream_reader_t)zend_stream_stdio_reader;
file_handle->handle.stream.closer = (zend_stream_closer_t)zend_stream_stdio_closer;
file_handle->handle.stream.fsizer = (zend_stream_fsizer_t)zend_stream_stdio_fsizer;
}
size = zend_stream_fsize(file_handle);
if (size == (size_t)-1) {
return FAILURE;
}
if (is_fp && !file_handle->handle.stream.isatty && size) {
if (size) {
file_handle->buf = *buf = safe_emalloc(1, size, ZEND_MMAP_AHEAD);
file_handle->len = zend_stream_read(file_handle, *buf, size);
} else {