Add fsync support for PHP8.1
@@ -123,6 +123,8 @@ extern int php_get_gid_by_name(const char *name, gid_t *gid);
#if defined(PHP_WIN32)
# define PLAIN_WRAP_BUF_SIZE(st) (((st) > UINT_MAX) ? UINT_MAX : (unsigned int)(st))
+#define fsync _commit
+#define fdatasync fsync
#else
# define PLAIN_WRAP_BUF_SIZE(st) (st)
#endif
@@ -1091,6 +1093,29 @@ static int swow_stdiop_fs_flush(php_stream *stream)
return 0;
}
+#ifdef PHP_STREAM_OPTION_SYNC_API
+static int swow_stdiop_fs_sync(php_stream *stream, bool dataonly)
+{
+ swow_stdio_stream_data *data = (swow_stdio_stream_data*)stream->abstract;
+ FILE *fp;
+ int fd;
+
+ if (php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS) == FAILURE) {
+ return -1;
+ }
+
+ if (swow_stdiop_fs_flush(stream) == 0) {
+ int fd = data->file ? fileno(data->file) : data->fd;
+ if (dataonly) {
+ return cat_fs_fdatasync(fd);
+ } else {
+ return cat_fs_fsync(fd);
+ }
+ }
+ return -1;
+}
+#endif
+
static int swow_stdiop_fs_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset)
{
//printf("on calling swow_stdiop_fs_seek(%p, %zu, %d, %p)\n", stream,offset,whence,newoffset);
@@ -1445,6 +1470,18 @@ static int swow_stdiop_fs_set_option(php_stream *stream, int option, int value,
#endif
return PHP_STREAM_OPTION_RETURN_NOTIMPL;
+#ifdef PHP_STREAM_OPTION_SYNC_API
+ 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 swow_stdiop_fs_sync(stream, 0) == 0 ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
+ case PHP_STREAM_SYNC_FDSYNC:
+ return swow_stdiop_fs_sync(stream, 1) == 0 ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
+ }
+#endif
+
case PHP_STREAM_OPTION_TRUNCATE_API:
switch (value) {
case PHP_STREAM_TRUNCATE_SUPPORTED:
