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 4 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
3 changes: 3 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,9 @@ function ftell($stream): int|false {}
/** @param resource $stream */
function fflush($stream): bool {}

/** @param resource $stream */
function fsync($stream): bool {}

/** @param resource $stream */
function fwrite($stream, string $data, ?int $length = null): int|false {}

Expand Down
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
28 changes: 28 additions & 0 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,34 @@ PHP_FUNCTION(unlink)
}
/* }}} */

/* {{{ Sync file to storage. Similar to fflush() but blocks until OS buffers have flushed. */
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);

if (!php_stream_sync_supported(stream)) {
php_error_docref(NULL, E_WARNING, "Can't fsync this stream!");
RETURN_FALSE;
}

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

}
/* }}} */


/* {{{ Truncate file to 'size' length */
PHP_FUNCTION(ftruncate)
{
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
11 changes: 11 additions & 0 deletions main/php_streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,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 Expand Up @@ -444,6 +447,14 @@ END_EXTERN_C()
/* Enable/disable blocking reads on anonymous pipes on Windows. */
#define PHP_STREAM_OPTION_PIPE_BLOCKING 13

/* Stream can support fsync operation */
#define PHP_STREAM_OPTION_SYNC_API 14
#define PHP_STREAM_SYNC_SUPPORTED 0
#define PHP_STREAM_SYNC_FSYNC 1

#define php_stream_sync_supported(stream) (_php_stream_set_option((stream), PHP_STREAM_OPTION_SYNC_API, PHP_STREAM_SYNC_SUPPORTED, NULL) == PHP_STREAM_OPTION_RETURN_OK ? 1 : 0)


#define PHP_STREAM_OPTION_RETURN_OK 0 /* option set OK */
#define PHP_STREAM_OPTION_RETURN_ERR -1 /* problem setting option */
#define PHP_STREAM_OPTION_RETURN_NOTIMPL -2 /* underlying stream does not implement; streams can handle it instead */
Expand Down
24 changes: 24 additions & 0 deletions main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,22 @@ static int php_stdiop_close(php_stream *stream, int close_handle)
return ret;
}

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 @@ -885,6 +901,14 @@ static int php_stdiop_set_option(php_stream *stream, int option, int value, void
#endif
return PHP_STREAM_OPTION_RETURN_NOTIMPL;

case PHP_STREAM_OPTION_SYNC_API:
switch (value) {
case PHP_STREAM_SYNC_SUPPORTED:
return fd == -1 ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
case PHP_STREAM_SYNC_FSYNC:
return php_stdiop_sync(stream) == 0 ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
}

case PHP_STREAM_OPTION_TRUNCATE_API:
switch (value) {
case PHP_STREAM_TRUNCATE_SUPPORTED:
Expand Down
5 changes: 5 additions & 0 deletions main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,11 @@ PHPAPI int _php_stream_set_option(php_stream *stream, int option, int value, voi
return ret;
}

PHPAPI int _php_stream_sync(php_stream *stream)
{
return php_stream_set_option(stream, PHP_STREAM_OPTION_SYNC_API, PHP_STREAM_SYNC_FSYNC, NULL);
}

PHPAPI int _php_stream_truncate_set_size(php_stream *stream, size_t newsize)
{
return php_stream_set_option(stream, PHP_STREAM_OPTION_TRUNCATE_API, PHP_STREAM_TRUNCATE_SET_SIZE, &newsize);
Expand Down