Skip to content

Commit f42d8bc

Browse files
committed
Implement fsync via set_option
1 parent 89e0c2f commit f42d8bc

File tree

6 files changed

+56
-37
lines changed

6 files changed

+56
-37
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,9 @@ function ftell($stream): int|false {}
823823
/** @param resource $stream */
824824
function fflush($stream): bool {}
825825

826+
/** @param resource $stream */
827+
function fsync($stream): bool {}
828+
826829
/** @param resource $stream */
827830
function fwrite($stream, string $data, ?int $length = null): int|false {}
828831

ext/standard/file.c

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,26 +1170,6 @@ PHPAPI PHP_FUNCTION(fwrite)
11701170
}
11711171
/* }}} */
11721172

1173-
PHPAPI PHP_FUNCTION(fsync)
1174-
{
1175-
zval *res;
1176-
int ret;
1177-
php_stream *stream;
1178-
1179-
ZEND_PARSE_PARAMETERS_START(1, 1)
1180-
Z_PARAM_RESOURCE(res)
1181-
ZEND_PARSE_PARAMETERS_END();
1182-
1183-
PHP_STREAM_TO_ZVAL(stream, res);
1184-
1185-
ret = php_stream_sync(stream);
1186-
if (ret) {
1187-
RETURN_FALSE;
1188-
}
1189-
RETURN_TRUE;
1190-
1191-
}
1192-
11931173
/* {{{ Flushes output */
11941174
PHPAPI PHP_FUNCTION(fflush)
11951175
{
@@ -1487,6 +1467,34 @@ PHP_FUNCTION(unlink)
14871467
}
14881468
/* }}} */
14891469

1470+
/* {{{ Sync file to storage. Similar to fflush() but blocks until OS buffers have flushed. */
1471+
PHP_FUNCTION(fsync)
1472+
{
1473+
zval *res;
1474+
int ret;
1475+
php_stream *stream;
1476+
1477+
ZEND_PARSE_PARAMETERS_START(1, 1)
1478+
Z_PARAM_RESOURCE(res)
1479+
ZEND_PARSE_PARAMETERS_END();
1480+
1481+
PHP_STREAM_TO_ZVAL(stream, res);
1482+
1483+
if (!php_stream_sync_supported(stream)) {
1484+
php_error_docref(NULL, E_WARNING, "Can't fsync this stream!");
1485+
RETURN_FALSE;
1486+
}
1487+
1488+
ret = php_stream_sync(stream);
1489+
if (ret) {
1490+
RETURN_FALSE;
1491+
}
1492+
RETURN_TRUE;
1493+
1494+
}
1495+
/* }}} */
1496+
1497+
14901498
/* {{{ Truncate file to 'size' length */
14911499
PHP_FUNCTION(ftruncate)
14921500
{

main/php_streams.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ typedef struct _php_stream_ops {
125125
int (*cast)(php_stream *stream, int castas, void **ret);
126126
int (*stat)(php_stream *stream, php_stream_statbuf *ssb);
127127
int (*set_option)(php_stream *stream, int option, int value, void *ptrparam);
128-
int (*sync)(php_stream *stream);
129128
} php_stream_ops;
130129

131130
typedef struct _php_stream_wrapper_ops {
@@ -338,7 +337,7 @@ PHPAPI int _php_stream_flush(php_stream *stream, int closing);
338337
#define php_stream_flush(stream) _php_stream_flush((stream), 0)
339338

340339
PHPAPI int _php_stream_sync(php_stream *stream);
341-
#define php_stream_sync(stream) _php_stream_sync((stream))
340+
#define php_stream_sync(stream) _php_stream_sync((stream))
342341

343342
PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen, size_t *returned_len);
344343
#define php_stream_gets(stream, buf, maxlen) _php_stream_get_line((stream), (buf), (maxlen), NULL)
@@ -448,6 +447,14 @@ END_EXTERN_C()
448447
/* Enable/disable blocking reads on anonymous pipes on Windows. */
449448
#define PHP_STREAM_OPTION_PIPE_BLOCKING 13
450449

450+
/* Stream can support fsync operation */
451+
#define PHP_STREAM_OPTION_SYNC_API 14
452+
#define PHP_STREAM_SYNC_SUPPORTED 0
453+
#define PHP_STREAM_SYNC_FSYNC 1
454+
455+
#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)
456+
457+
451458
#define PHP_STREAM_OPTION_RETURN_OK 0 /* option set OK */
452459
#define PHP_STREAM_OPTION_RETURN_ERR -1 /* problem setting option */
453460
#define PHP_STREAM_OPTION_RETURN_NOTIMPL -2 /* underlying stream does not implement; streams can handle it instead */

main/streams/glob_wrapper.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ const php_stream_ops php_glob_stream_ops = {
190190
php_glob_stream_rewind,
191191
NULL, /* cast */
192192
NULL, /* stat */
193-
NULL, /* set_option */
194-
NULL
193+
NULL /* set_option */
195194
};
196195

197196
/* {{{ php_glob_stream_opener */

main/streams/plain_wrapper.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,6 @@ static int php_stdiop_close(php_stream *stream, int close_handle)
520520
return ret;
521521
}
522522

523-
//dwg
524523
static int php_stdiop_sync(php_stream *stream)
525524
{
526525
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
@@ -534,7 +533,7 @@ static int php_stdiop_sync(php_stream *stream)
534533
return fsync(fileno(data->file));
535534
}
536535
}
537-
return ret;
536+
return ret;
538537
}
539538

540539
static int php_stdiop_flush(php_stream *stream)
@@ -902,6 +901,14 @@ static int php_stdiop_set_option(php_stream *stream, int option, int value, void
902901
#endif
903902
return PHP_STREAM_OPTION_RETURN_NOTIMPL;
904903

904+
case PHP_STREAM_OPTION_SYNC_API:
905+
switch (value) {
906+
case PHP_STREAM_SYNC_SUPPORTED:
907+
return fd == -1 ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
908+
case PHP_STREAM_SYNC_FSYNC:
909+
return php_stdiop_sync(stream) == 0 ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
910+
}
911+
905912
case PHP_STREAM_OPTION_TRUNCATE_API:
906913
switch (value) {
907914
case PHP_STREAM_TRUNCATE_SUPPORTED:
@@ -979,8 +986,7 @@ PHPAPI php_stream_ops php_stream_stdio_ops = {
979986
php_stdiop_seek,
980987
php_stdiop_cast,
981988
php_stdiop_stat,
982-
php_stdiop_set_option,
983-
php_stdiop_sync,
989+
php_stdiop_set_option
984990
};
985991
/* }}} */
986992

main/streams/streams.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,15 +1224,6 @@ static ssize_t _php_stream_write_filtered(php_stream *stream, const char *buf, s
12241224
return consumed;
12251225
}
12261226

1227-
PHPAPI int _php_stream_sync(php_stream *stream)
1228-
{
1229-
int ret = 0;
1230-
if (stream->ops->sync) {
1231-
ret = stream->ops->sync(stream);
1232-
}
1233-
return ret;
1234-
}
1235-
12361227
PHPAPI int _php_stream_flush(php_stream *stream, int closing)
12371228
{
12381229
int ret = 0;
@@ -1415,6 +1406,11 @@ PHPAPI int _php_stream_set_option(php_stream *stream, int option, int value, voi
14151406
return ret;
14161407
}
14171408

1409+
PHPAPI int _php_stream_sync(php_stream *stream)
1410+
{
1411+
return php_stream_set_option(stream, PHP_STREAM_OPTION_SYNC_API, PHP_STREAM_SYNC_FSYNC, NULL);
1412+
}
1413+
14181414
PHPAPI int _php_stream_truncate_set_size(php_stream *stream, size_t newsize)
14191415
{
14201416
return php_stream_set_option(stream, PHP_STREAM_OPTION_TRUNCATE_API, PHP_STREAM_TRUNCATE_SET_SIZE, &newsize);

0 commit comments

Comments
 (0)