Skip to content

Commit 5a90dc7

Browse files
committed
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.
1 parent 6fbab09 commit 5a90dc7

File tree

4 files changed

+19
-65
lines changed

4 files changed

+19
-65
lines changed

Zend/zend_stream.c

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include "zend_compile.h"
2424
#include "zend_stream.h"
2525

26+
#ifndef S_ISREG
27+
# define S_ISREG(m) 1
28+
#endif
29+
2630
ZEND_DLIMPORT int isatty(int fd);
2731

2832
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) /* {{{ */
3741
}
3842
} /* }}} */
3943

40-
static size_t zend_stream_stdio_fsizer(void *handle) /* {{{ */
41-
{
42-
zend_stat_t buf;
43-
if (handle && zend_fstat(fileno((FILE*)handle), &buf) == 0) {
44-
#ifdef S_ISREG
45-
if (!S_ISREG(buf.st_mode)) {
46-
return 0;
47-
}
48-
#endif
49-
return buf.st_size;
50-
}
51-
return 0;
52-
} /* }}} */
53-
54-
static size_t zend_stream_fsize(zend_file_handle *file_handle) /* {{{ */
55-
{
56-
zend_stat_t buf;
57-
58-
if (file_handle->type == ZEND_HANDLE_STREAM) {
59-
return file_handle->handle.stream.fsizer(file_handle->handle.stream.handle);
60-
}
61-
if (file_handle->handle.fp && zend_fstat(fileno(file_handle->handle.fp), &buf) == 0) {
62-
#ifdef S_ISREG
63-
if (!S_ISREG(buf.st_mode)) {
64-
return 0;
65-
}
66-
#endif
67-
return buf.st_size;
68-
}
69-
70-
return -1;
71-
} /* }}} */
72-
7344
ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename) {
7445
memset(handle, 0, sizeof(zend_file_handle));
7546
handle->type = ZEND_HANDLE_FP;
@@ -125,8 +96,7 @@ static size_t zend_stream_read(zend_file_handle *file_handle, char *buf, size_t
12596

12697
ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t *len) /* {{{ */
12798
{
128-
size_t size;
129-
zend_bool is_fp = 0;
99+
size_t size = 0;
130100

131101
if (file_handle->buf) {
132102
*buf = file_handle->buf;
@@ -141,25 +111,28 @@ ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t
141111
}
142112

143113
if (file_handle->type == ZEND_HANDLE_FP) {
144-
if (!file_handle->handle.fp) {
114+
FILE *fp = file_handle->handle.fp;
115+
int is_tty;
116+
if (!fp) {
145117
return FAILURE;
146118
}
147119

148-
is_fp = 1;
120+
is_tty = isatty(fileno(fp));
121+
if (!is_tty) {
122+
zend_stat_t buf;
123+
if (zend_fstat(fileno(fp), &buf) == 0 && S_ISREG(buf.st_mode)) {
124+
size = buf.st_size;
125+
}
126+
}
127+
149128
file_handle->type = ZEND_HANDLE_STREAM;
150-
file_handle->handle.stream.handle = file_handle->handle.fp;
151-
file_handle->handle.stream.isatty = isatty(fileno((FILE *)file_handle->handle.stream.handle));
129+
file_handle->handle.stream.handle = fp;
130+
file_handle->handle.stream.isatty = is_tty;
152131
file_handle->handle.stream.reader = (zend_stream_reader_t)zend_stream_stdio_reader;
153132
file_handle->handle.stream.closer = (zend_stream_closer_t)zend_stream_stdio_closer;
154-
file_handle->handle.stream.fsizer = (zend_stream_fsizer_t)zend_stream_stdio_fsizer;
155-
}
156-
157-
size = zend_stream_fsize(file_handle);
158-
if (size == (size_t)-1) {
159-
return FAILURE;
160133
}
161134

162-
if (is_fp && !file_handle->handle.stream.isatty && size) {
135+
if (size) {
163136
file_handle->buf = *buf = safe_emalloc(1, size, ZEND_MMAP_AHEAD);
164137
file_handle->len = zend_stream_read(file_handle, *buf, size);
165138
} else {

Zend/zend_stream.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
/* Lightweight stream implementation for the ZE scanners.
2929
* These functions are private to the engine.
3030
* */
31-
typedef size_t (*zend_stream_fsizer_t)(void* handle);
3231
typedef size_t (*zend_stream_reader_t)(void* handle, char *buf, size_t len);
3332
typedef void (*zend_stream_closer_t)(void* handle);
3433

@@ -44,7 +43,6 @@ typedef struct _zend_stream {
4443
void *handle;
4544
int isatty;
4645
zend_stream_reader_t reader;
47-
zend_stream_fsizer_t fsizer;
4846
zend_stream_closer_t closer;
4947
} zend_stream;
5048

ext/phar/phar.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,11 +3221,6 @@ static size_t phar_zend_stream_reader(void *handle, char *buf, size_t len) /* {{
32213221
}
32223222
/* }}} */
32233223

3224-
static size_t phar_zend_stream_fsizer(void *handle) /* {{{ */
3225-
{
3226-
return ((phar_archive_data*)handle)->halt_offset + 32;
3227-
} /* }}} */
3228-
32293224
zend_op_array *(*phar_orig_compile_file)(zend_file_handle *file_handle, int type);
32303225
#define phar_orig_zend_open zend_stream_open_function
32313226

@@ -3282,7 +3277,6 @@ static zend_op_array *phar_compile_file(zend_file_handle *file_handle, int type)
32823277
file_handle->handle.stream.handle = phar;
32833278
file_handle->handle.stream.reader = phar_zend_stream_reader;
32843279
file_handle->handle.stream.closer = NULL;
3285-
file_handle->handle.stream.fsizer = phar_zend_stream_fsizer;
32863280
file_handle->handle.stream.isatty = 0;
32873281
phar->is_persistent ?
32883282
php_stream_rewind(PHAR_G(cached_fp)[phar->phar_pos].fp) :

main/main.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,16 +1562,6 @@ static void php_zend_stream_closer(void *handle) /* {{{ */
15621562
}
15631563
/* }}} */
15641564

1565-
static size_t php_zend_stream_fsizer(void *handle) /* {{{ */
1566-
{
1567-
php_stream_statbuf ssb;
1568-
if (php_stream_stat((php_stream*)handle, &ssb) == 0) {
1569-
return ssb.sb.st_size;
1570-
}
1571-
return 0;
1572-
}
1573-
/* }}} */
1574-
15751565
static int php_stream_open_for_zend(const char *filename, zend_file_handle *handle) /* {{{ */
15761566
{
15771567
return php_stream_open_for_zend_ex(filename, handle, USE_PATH|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE);
@@ -1589,7 +1579,6 @@ PHPAPI int php_stream_open_for_zend_ex(const char *filename, zend_file_handle *h
15891579
handle->opened_path = opened_path;
15901580
handle->handle.stream.handle = stream;
15911581
handle->handle.stream.reader = (zend_stream_reader_t)_php_stream_read;
1592-
handle->handle.stream.fsizer = php_zend_stream_fsizer;
15931582
handle->handle.stream.isatty = 0;
15941583
handle->handle.stream.closer = php_zend_stream_closer;
15951584
/* suppress warning if this stream is not explicitly closed */

0 commit comments

Comments
 (0)