Skip to content

Commit 684a979

Browse files
committed
fsync initial
1 parent 042e0d8 commit 684a979

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

ext/standard/basic_functions_arginfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,6 +2560,7 @@ ZEND_FUNCTION(fstat);
25602560
ZEND_FUNCTION(fseek);
25612561
ZEND_FUNCTION(ftell);
25622562
ZEND_FUNCTION(fflush);
2563+
ZEND_FUNCTION(fsync);
25632564
ZEND_FUNCTION(fwrite);
25642565
ZEND_FUNCTION(mkdir);
25652566
ZEND_FUNCTION(rename);
@@ -3197,6 +3198,7 @@ static const zend_function_entry ext_functions[] = {
31973198
ZEND_FE(fseek, arginfo_fseek)
31983199
ZEND_FE(ftell, arginfo_ftell)
31993200
ZEND_FE(fflush, arginfo_fflush)
3201+
ZEND_FE(fsync, arginfo_fflush)
32003202
ZEND_FE(fwrite, arginfo_fwrite)
32013203
ZEND_FALIAS(fputs, fwrite, arginfo_fputs)
32023204
ZEND_FE(mkdir, arginfo_mkdir)

ext/standard/file.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,26 @@ 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+
11731193
/* {{{ Flushes output */
11741194
PHPAPI PHP_FUNCTION(fflush)
11751195
{

ext/standard/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ PHPAPI PHP_FUNCTION(fgetc);
2929
PHPAPI PHP_FUNCTION(fgets);
3030
PHPAPI PHP_FUNCTION(fwrite);
3131
PHPAPI PHP_FUNCTION(fflush);
32+
PHPAPI PHP_FUNCTION(fsync);
3233
PHPAPI PHP_FUNCTION(rewind);
3334
PHPAPI PHP_FUNCTION(ftell);
3435
PHPAPI PHP_FUNCTION(fseek);

main/php_streams.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ typedef struct _php_stream_ops {
117117
ssize_t (*read)(php_stream *stream, char *buf, size_t count);
118118
int (*close)(php_stream *stream, int close_handle);
119119
int (*flush)(php_stream *stream);
120+
int (*sync)(php_stream *stream);
120121

121122
const char *label; /* label for this ops structure */
122123

@@ -336,6 +337,9 @@ PHPAPI int _php_stream_putc(php_stream *stream, int c);
336337
PHPAPI int _php_stream_flush(php_stream *stream, int closing);
337338
#define php_stream_flush(stream) _php_stream_flush((stream), 0)
338339

340+
PHPAPI int _php_stream_sync(php_stream *stream);
341+
#define php_stream_sync(stream) _php_stream_sync((stream))
342+
339343
PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen, size_t *returned_len);
340344
#define php_stream_gets(stream, buf, maxlen) _php_stream_get_line((stream), (buf), (maxlen), NULL)
341345

main/streams/plain_wrapper.c

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

523+
//dwg
524+
static int php_stdiop_sync(php_stream *stream)
525+
{
526+
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
527+
528+
assert(data != NULL);
529+
530+
if (data->file) {
531+
fflush(data->file);
532+
return fsync(fileno(data->file));
533+
}
534+
return 0;
535+
536+
}
537+
523538
static int php_stdiop_flush(php_stream *stream)
524539
{
525540
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
@@ -958,6 +973,7 @@ static int php_stdiop_set_option(php_stream *stream, int option, int value, void
958973
PHPAPI php_stream_ops php_stream_stdio_ops = {
959974
php_stdiop_write, php_stdiop_read,
960975
php_stdiop_close, php_stdiop_flush,
976+
php_stdiop_sync,
961977
"STDIO",
962978
php_stdiop_seek,
963979
php_stdiop_cast,

main/streams/streams.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,15 @@ 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+
12271236
PHPAPI int _php_stream_flush(php_stream *stream, int closing)
12281237
{
12291238
int ret = 0;

0 commit comments

Comments
 (0)