Skip to content

Commit 856c5d2

Browse files
committed
Revert "Avoid unused fstat() call"
This reverts commit 5a90dc7. Let's try to go with the reverse direction here and actually trust the reported size...
1 parent 5a90dc7 commit 856c5d2

File tree

4 files changed

+65
-19
lines changed

4 files changed

+65
-19
lines changed

Zend/zend_stream.c

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

26-
#ifndef S_ISREG
27-
# define S_ISREG(m) 1
28-
#endif
29-
3026
ZEND_DLIMPORT int isatty(int fd);
3127

3228
static size_t zend_stream_stdio_reader(void *handle, char *buf, size_t len) /* {{{ */
@@ -41,6 +37,39 @@ static void zend_stream_stdio_closer(void *handle) /* {{{ */
4137
}
4238
} /* }}} */
4339

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+
4473
ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename) {
4574
memset(handle, 0, sizeof(zend_file_handle));
4675
handle->type = ZEND_HANDLE_FP;
@@ -96,7 +125,8 @@ static size_t zend_stream_read(zend_file_handle *file_handle, char *buf, size_t
96125

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

101131
if (file_handle->buf) {
102132
*buf = file_handle->buf;
@@ -111,28 +141,25 @@ ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t
111141
}
112142

113143
if (file_handle->type == ZEND_HANDLE_FP) {
114-
FILE *fp = file_handle->handle.fp;
115-
int is_tty;
116-
if (!fp) {
144+
if (!file_handle->handle.fp) {
117145
return FAILURE;
118146
}
119147

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-
148+
is_fp = 1;
128149
file_handle->type = ZEND_HANDLE_STREAM;
129-
file_handle->handle.stream.handle = fp;
130-
file_handle->handle.stream.isatty = is_tty;
150+
file_handle->handle.stream.handle = file_handle->handle.fp;
151+
file_handle->handle.stream.isatty = isatty(fileno((FILE *)file_handle->handle.stream.handle));
131152
file_handle->handle.stream.reader = (zend_stream_reader_t)zend_stream_stdio_reader;
132153
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;
133160
}
134161

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

Zend/zend_stream.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
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);
3132
typedef size_t (*zend_stream_reader_t)(void* handle, char *buf, size_t len);
3233
typedef void (*zend_stream_closer_t)(void* handle);
3334

@@ -43,6 +44,7 @@ typedef struct _zend_stream {
4344
void *handle;
4445
int isatty;
4546
zend_stream_reader_t reader;
47+
zend_stream_fsizer_t fsizer;
4648
zend_stream_closer_t closer;
4749
} zend_stream;
4850

ext/phar/phar.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,6 +3221,11 @@ 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+
32243229
zend_op_array *(*phar_orig_compile_file)(zend_file_handle *file_handle, int type);
32253230
#define phar_orig_zend_open zend_stream_open_function
32263231

@@ -3277,6 +3282,7 @@ static zend_op_array *phar_compile_file(zend_file_handle *file_handle, int type)
32773282
file_handle->handle.stream.handle = phar;
32783283
file_handle->handle.stream.reader = phar_zend_stream_reader;
32793284
file_handle->handle.stream.closer = NULL;
3285+
file_handle->handle.stream.fsizer = phar_zend_stream_fsizer;
32803286
file_handle->handle.stream.isatty = 0;
32813287
phar->is_persistent ?
32823288
php_stream_rewind(PHAR_G(cached_fp)[phar->phar_pos].fp) :

main/main.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,16 @@ 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+
15651575
static int php_stream_open_for_zend(const char *filename, zend_file_handle *handle) /* {{{ */
15661576
{
15671577
return php_stream_open_for_zend_ex(filename, handle, USE_PATH|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE);
@@ -1579,6 +1589,7 @@ PHPAPI int php_stream_open_for_zend_ex(const char *filename, zend_file_handle *h
15791589
handle->opened_path = opened_path;
15801590
handle->handle.stream.handle = stream;
15811591
handle->handle.stream.reader = (zend_stream_reader_t)_php_stream_read;
1592+
handle->handle.stream.fsizer = php_zend_stream_fsizer;
15821593
handle->handle.stream.isatty = 0;
15831594
handle->handle.stream.closer = php_zend_stream_closer;
15841595
/* suppress warning if this stream is not explicitly closed */

0 commit comments

Comments
 (0)