Skip to content

Commit 03e77c0

Browse files
committed
Optimize code && fix tests
1 parent 889fa0e commit 03e77c0

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

thirdparty/php/streams/plain_wrapper.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ typedef struct {
182182
unsigned no_forced_fstat:1; /* Use fstat cache even if forced */
183183
unsigned is_seekable:1; /* don't try and seek, if not set */
184184
unsigned can_poll:1;
185-
unsigned _reserved:25;
185+
unsigned seekable_detected:1;
186+
unsigned _reserved:24;
186187

187188
int lock_flag; /* stores the lock state */
188189
zend_string *temp_name; /* if non-null, this is the path to a temporary file that
@@ -252,6 +253,7 @@ static void _sw_detect_is_seekable(php_stdio_stream_data *self) {
252253
self->is_seekable = !(S_ISFIFO(self->sb.st_mode) || S_ISCHR(self->sb.st_mode));
253254
self->is_pipe = S_ISFIFO(self->sb.st_mode);
254255
self->can_poll = S_ISFIFO(self->sb.st_mode) || S_ISSOCK(self->sb.st_mode) || S_ISCHR(self->sb.st_mode);
256+
self->seekable_detected = 1;
255257
if (self->can_poll) {
256258
swoole_coroutine_socket_create(self->fd);
257259
}
@@ -287,7 +289,7 @@ static php_stream *_sw_php_stream_fopen_from_fd(int fd, const char *mode, const
287289
php_stream *stream = sw_php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);
288290

289291
if (stream) {
290-
php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
292+
php_stdio_stream_data *self = (php_stdio_stream_data *) stream->abstract;
291293

292294
_sw_detect_is_seekable(self);
293295
if (!self->is_seekable) {
@@ -328,10 +330,11 @@ static php_stream_size_t sw_php_stdiop_write(php_stream *stream, const char *buf
328330
}
329331
bytes_written = _write(data->fd, buf, (unsigned int)count);
330332
#else
331-
php_stdio_stream_data *self = (php_stdio_stream_data*) stream->abstract;
332-
ssize_t bytes_written = self->can_poll ?
333-
swoole_coroutine_write(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count)) :
334-
write(data->fd, buf, count);
333+
php_stdio_stream_data *self = (php_stdio_stream_data *) stream->abstract;
334+
if (!self->seekable_detected) {
335+
_sw_detect_is_seekable(self);
336+
}
337+
ssize_t bytes_written = write(data->fd, buf, count);
335338
#endif
336339
if (bytes_written < 0) {
337340
if (PHP_IS_TRANSIENT_ERROR(errno)) {
@@ -393,18 +396,17 @@ static php_stream_size_t sw_php_stdiop_read(php_stream *stream, char *buf, size_
393396
}
394397
}
395398
#endif
396-
php_stdio_stream_data *self = (php_stdio_stream_data*) stream->abstract;
397-
ret = self->can_poll ?
398-
swoole_coroutine_read(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count)) :
399-
read(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
399+
php_stdio_stream_data *self = (php_stdio_stream_data *) stream->abstract;
400+
if (!self->seekable_detected) {
401+
_sw_detect_is_seekable(self);
402+
}
403+
ret = read(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
400404

401405
if (ret == (ssize_t) -1 && errno == EINTR) {
402406
/* Read was interrupted, retry once,
403407
If read still fails, giveup with feof==0
404408
so script can retry if desired */
405-
ret = self->can_poll ?
406-
swoole_coroutine_read(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count)) :
407-
read(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
409+
ret = read(data->fd, buf, PLAIN_WRAP_BUF_SIZE(count));
408410
}
409411

410412
if (ret < 0) {

0 commit comments

Comments
 (0)