From a7da360587b8f0aeddd94ef9c6bfc86eb2368ec1 Mon Sep 17 00:00:00 2001 From: Benjamin Lamowski Date: Fri, 19 Jul 2024 00:09:59 +0200 Subject: [PATCH] libc: make Vfs_plugin::Sync accessible Make the Sync object accessible outside of Vfs_plugin implementation, e.g., for use in AIO. Issue #5302 --- .../src/lib/libc/internal/vfs_plugin.h | 25 +++++- repos/libports/src/lib/libc/vfs_plugin.cc | 85 +++++++++++-------- 2 files changed, 72 insertions(+), 38 deletions(-) diff --git a/repos/libports/src/lib/libc/internal/vfs_plugin.h b/repos/libports/src/lib/libc/internal/vfs_plugin.h index 690d815223a..01a7118701d 100644 --- a/repos/libports/src/lib/libc/internal/vfs_plugin.h +++ b/repos/libports/src/lib/libc/internal/vfs_plugin.h @@ -26,6 +26,7 @@ #include /* libc-internal includes */ +#include #include #include #include @@ -36,9 +37,31 @@ namespace Libc { class Vfs_plugin; } class Libc::Vfs_plugin final : public Plugin { public: - enum class Update_mtime { NO, YES }; + /* + * Needed to construct a sync object from a Vfs_plugin / Plugin reference. + */ + class Sync + { + private: + + enum { INITIAL, TIMESTAMP_UPDATED, QUEUED, COMPLETE } _state { INITIAL }; + + Vfs::Vfs_handle &_vfs_handle; + Vfs::Timestamp _mtime { Vfs::Timestamp::INVALID }; + + public: + + Sync(Vfs::Vfs_handle &vfs_handle, Libc::Vfs_plugin::Update_mtime update_mtime, + Libc::Current_real_time ¤t_real_time); + + Sync(Vfs::Vfs_handle &vfs_handle, Plugin & plugin); + + bool complete(); + }; + + /** * Return path to pseudo files used for ioctl operations of a given FD * diff --git a/repos/libports/src/lib/libc/vfs_plugin.cc b/repos/libports/src/lib/libc/vfs_plugin.cc index 3f137d46f90..66370be2156 100644 --- a/repos/libports/src/lib/libc/vfs_plugin.cc +++ b/repos/libports/src/lib/libc/vfs_plugin.cc @@ -44,7 +44,6 @@ #include #include #include -#include static Libc::Monitor *_monitor_ptr; @@ -543,51 +542,63 @@ Libc::File_descriptor *Libc::Vfs_plugin::open(char const *path, int flags) } -struct Sync +Libc::Vfs_plugin::Sync::Sync(Vfs::Vfs_handle &vfs_handle, Libc::Vfs_plugin::Update_mtime update_mtime, + Libc::Current_real_time ¤t_real_time) +: + _vfs_handle(vfs_handle) { - enum { INITIAL, TIMESTAMP_UPDATED, QUEUED, COMPLETE } state { INITIAL }; + if (update_mtime == Libc::Vfs_plugin::Update_mtime::NO + || !current_real_time.has_real_time()) { - Vfs::Vfs_handle &vfs_handle; - Vfs::Timestamp mtime { Vfs::Timestamp::INVALID }; + _state = TIMESTAMP_UPDATED; - Sync(Vfs::Vfs_handle &vfs_handle, Libc::Vfs_plugin::Update_mtime update_mtime, - Libc::Current_real_time ¤t_real_time) - : - vfs_handle(vfs_handle) - { - if (update_mtime == Libc::Vfs_plugin::Update_mtime::NO - || !current_real_time.has_real_time()) { + } else { + timespec const ts = current_real_time.current_real_time(); - state = TIMESTAMP_UPDATED; + _mtime = { .value = (long long)ts.tv_sec }; + } +} - } else { - timespec const ts = current_real_time.current_real_time(); - mtime = { .value = (long long)ts.tv_sec }; - } + +Libc::Vfs_plugin::Sync::Sync(Vfs::Vfs_handle &vfs_handle, Plugin & plugin) +: + _vfs_handle(vfs_handle) +{ + Vfs_plugin & vfs_plugin = reinterpret_cast(plugin); + if (vfs_plugin._update_mtime == Libc::Vfs_plugin::Update_mtime::NO + || !vfs_plugin._current_real_time.has_real_time()) { + + _state = TIMESTAMP_UPDATED; + + } else { + timespec const ts = vfs_plugin._current_real_time.current_real_time(); + + _mtime = { .value = (long long)ts.tv_sec }; } +} - bool complete() - { - switch (state) { - case Sync::INITIAL: - if (!vfs_handle.fs().update_modification_timestamp(&vfs_handle, mtime)) - return false; - state = Sync::TIMESTAMP_UPDATED; [[ fallthrough ]]; - case Sync::TIMESTAMP_UPDATED: - if (!vfs_handle.fs().queue_sync(&vfs_handle)) - return false; - state = Sync::QUEUED; [[ fallthrough ]]; - case Sync::QUEUED: - if (vfs_handle.fs().complete_sync(&vfs_handle) == Vfs::File_io_service::SYNC_QUEUED) - return false; - state = Sync::COMPLETE; [[ fallthrough ]]; - case Sync::COMPLETE: - break; - } - return true; + +bool Libc::Vfs_plugin::Sync::complete() +{ + switch (_state) { + case Sync::INITIAL: + if (!_vfs_handle.fs().update_modification_timestamp(&_vfs_handle, _mtime)) + return false; + _state = Sync::TIMESTAMP_UPDATED; [[ fallthrough ]]; + case Sync::TIMESTAMP_UPDATED: + if (!_vfs_handle.fs().queue_sync(&_vfs_handle)) + return false; + _state = Sync::QUEUED; [[ fallthrough ]]; + case Sync::QUEUED: + if (_vfs_handle.fs().complete_sync(&_vfs_handle) == Vfs::File_io_service::SYNC_QUEUED) + return false; + _state = Sync::COMPLETE; [[ fallthrough ]]; + case Sync::COMPLETE: + break; } -}; + return true; +} int Libc::Vfs_plugin::close_from_kernel(File_descriptor *fd)