Skip to content

Add fsync() function #6650

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 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 0 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2560,6 +2560,7 @@ ZEND_FUNCTION(fstat);
ZEND_FUNCTION(fseek);
ZEND_FUNCTION(ftell);
ZEND_FUNCTION(fflush);
ZEND_FUNCTION(fsync);
ZEND_FUNCTION(fwrite);
ZEND_FUNCTION(mkdir);
ZEND_FUNCTION(rename);
Expand Down Expand Up @@ -3197,6 +3198,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(fseek, arginfo_fseek)
ZEND_FE(ftell, arginfo_ftell)
ZEND_FE(fflush, arginfo_fflush)
ZEND_FE(fsync, arginfo_fflush)
ZEND_FE(fwrite, arginfo_fwrite)
ZEND_FALIAS(fputs, fwrite, arginfo_fputs)
ZEND_FE(mkdir, arginfo_mkdir)
Expand Down
20 changes: 20 additions & 0 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,26 @@ PHPAPI PHP_FUNCTION(fwrite)
}
/* }}} */

PHPAPI PHP_FUNCTION(fsync)
{
zval *res;
int ret;
php_stream *stream;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_RESOURCE(res)
ZEND_PARSE_PARAMETERS_END();

PHP_STREAM_TO_ZVAL(stream, res);

ret = php_stream_sync(stream);
if (ret) {
RETURN_FALSE;
}
RETURN_TRUE;

}

/* {{{ Flushes output */
PHPAPI PHP_FUNCTION(fflush)
{
Expand Down
1 change: 1 addition & 0 deletions ext/standard/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ PHPAPI PHP_FUNCTION(fgetc);
PHPAPI PHP_FUNCTION(fgets);
PHPAPI PHP_FUNCTION(fwrite);
PHPAPI PHP_FUNCTION(fflush);
PHPAPI PHP_FUNCTION(fsync);
PHPAPI PHP_FUNCTION(rewind);
PHPAPI PHP_FUNCTION(ftell);
PHPAPI PHP_FUNCTION(fseek);
Expand Down
4 changes: 4 additions & 0 deletions main/php_streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ typedef struct _php_stream_ops {
int (*cast)(php_stream *stream, int castas, void **ret);
int (*stat)(php_stream *stream, php_stream_statbuf *ssb);
int (*set_option)(php_stream *stream, int option, int value, void *ptrparam);
int (*sync)(php_stream *stream);
} php_stream_ops;

typedef struct _php_stream_wrapper_ops {
Expand Down Expand Up @@ -336,6 +337,9 @@ PHPAPI int _php_stream_putc(php_stream *stream, int c);
PHPAPI int _php_stream_flush(php_stream *stream, int closing);
#define php_stream_flush(stream) _php_stream_flush((stream), 0)

PHPAPI int _php_stream_sync(php_stream *stream);
#define php_stream_sync(stream) _php_stream_sync((stream))

PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen, size_t *returned_len);
#define php_stream_gets(stream, buf, maxlen) _php_stream_get_line((stream), (buf), (maxlen), NULL)

Expand Down
3 changes: 2 additions & 1 deletion main/streams/glob_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ const php_stream_ops php_glob_stream_ops = {
php_glob_stream_rewind,
NULL, /* cast */
NULL, /* stat */
NULL /* set_option */
NULL, /* set_option */
NULL
};

/* {{{ php_glob_stream_opener */
Expand Down
20 changes: 19 additions & 1 deletion main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,23 @@ static int php_stdiop_close(php_stream *stream, int close_handle)
return ret;
}

//dwg
static int php_stdiop_sync(php_stream *stream)
{
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
int ret = 0;

assert(data != NULL);

if (data->file) {
ret = fflush(data->file);
if (ret != 0) {
return fsync(fileno(data->file));
}
}
return ret;
}

static int php_stdiop_flush(php_stream *stream)
{
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
Expand Down Expand Up @@ -962,7 +979,8 @@ PHPAPI php_stream_ops php_stream_stdio_ops = {
php_stdiop_seek,
php_stdiop_cast,
php_stdiop_stat,
php_stdiop_set_option
php_stdiop_set_option,
php_stdiop_sync,
};
/* }}} */

Expand Down
9 changes: 9 additions & 0 deletions main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,15 @@ static ssize_t _php_stream_write_filtered(php_stream *stream, const char *buf, s
return consumed;
}

PHPAPI int _php_stream_sync(php_stream *stream)
{
int ret = 0;
if (stream->ops->sync) {
ret = stream->ops->sync(stream);
}
return ret;
}

PHPAPI int _php_stream_flush(php_stream *stream, int closing)
{
int ret = 0;
Expand Down