Skip to content

main/streams/plain_wrapper: skip lseek(SEEK_CUR) for newly opened files #8540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main/php_streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ END_EXTERN_C()

#define php_stream_fopen_with_path_rel(filename, mode, path, opened, options) _php_stream_fopen_with_path((filename), (mode), (path), (opened), (options) STREAMS_REL_CC)

#define php_stream_fopen_from_fd_rel(fd, mode, persistent_id) _php_stream_fopen_from_fd((fd), (mode), (persistent_id) STREAMS_REL_CC)
#define php_stream_fopen_from_fd_rel(fd, mode, persistent_id, zero_position) _php_stream_fopen_from_fd((fd), (mode), (persistent_id), (zero_position) STREAMS_REL_CC)
#define php_stream_fopen_from_file_rel(file, mode) _php_stream_fopen_from_file((file), (mode) STREAMS_REL_CC)

#define php_stream_fopen_from_pipe_rel(file, mode) _php_stream_fopen_from_pipe((file), (mode) STREAMS_REL_CC)
Expand Down
4 changes: 2 additions & 2 deletions main/streams/php_stream_plain_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ PHPAPI php_stream *_php_stream_fopen_with_path(const char *filename, const char
PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC);
#define php_stream_fopen_from_file(file, mode) _php_stream_fopen_from_file((file), (mode) STREAMS_CC)

PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const char *persistent_id STREAMS_DC);
#define php_stream_fopen_from_fd(fd, mode, persistent_id) _php_stream_fopen_from_fd((fd), (mode), (persistent_id) STREAMS_CC)
PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const char *persistent_id, bool zero_position STREAMS_DC);
#define php_stream_fopen_from_fd(fd, mode, persistent_id) _php_stream_fopen_from_fd((fd), (mode), (persistent_id), false STREAMS_CC)

PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STREAMS_DC);
#define php_stream_fopen_from_pipe(file, mode) _php_stream_fopen_from_pipe((file), (mode) STREAMS_CC)
Expand Down
12 changes: 10 additions & 2 deletions main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ static void detect_is_seekable(php_stdio_stream_data *self) {
#endif
}

PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const char *persistent_id STREAMS_DC)
PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const char *persistent_id, bool zero_position STREAMS_DC)
{
php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);

Expand All @@ -285,6 +285,9 @@ PHPAPI php_stream *_php_stream_fopen_from_fd(int fd, const char *mode, const cha
if (!self->is_seekable) {
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
stream->position = -1;
} else if (zero_position) {
ZEND_ASSERT(zend_lseek(self->fd, 0, SEEK_CUR) == 0);
stream->position = 0;
} else {
stream->position = zend_lseek(self->fd, 0, SEEK_CUR);
#ifdef ESPIPE
Expand Down Expand Up @@ -1141,7 +1144,12 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, zen
if (options & STREAM_OPEN_FOR_INCLUDE) {
ret = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);
} else {
ret = php_stream_fopen_from_fd_rel(fd, mode, persistent_id);
/* skip the lseek(SEEK_CUR) system call to
* determine the current offset because we
* know newly opened files are at offset zero
* (unless the file has been opened in
* O_APPEND mode) */
ret = php_stream_fopen_from_fd_rel(fd, mode, persistent_id, (open_flags & O_APPEND) == 0);
}

if (ret) {
Expand Down