diff --git a/cpp/functions/core/file_watcher.cpp b/cpp/functions/core/file_watcher.cpp index a115f707..8de9c4e2 100644 --- a/cpp/functions/core/file_watcher.cpp +++ b/cpp/functions/core/file_watcher.cpp @@ -1,5 +1,7 @@ #include "file_watcher.h" +#include "core/file_poll_diff.h" + #include #include #include @@ -280,4 +282,29 @@ const char* file_watcher_last_error(const FileWatcher* w) { return w ? w->last_ #endif +// ───────────────────────────────────────────────────────────────────────────── +// Helper portable basado en file_poll_diff (puro). Comun a todas las plataformas. +// ───────────────────────────────────────────────────────────────────────────── + +std::vector file_watcher_events_from_diff( + const std::vector& before, + const std::vector& after) { + + // Convertir a fn_ui::FileEntry para llamar a la funcion pura. + std::vector a, b; + a.reserve(before.size()); + b.reserve(after.size()); + for (const auto& e : before) a.push_back({e.path, e.size, e.mtime}); + for (const auto& e : after) b.push_back({e.path, e.size, e.mtime}); + + fn_ui::FileDiff diff = fn_ui::file_poll_diff(a, b); + + std::vector out; + out.reserve(diff.added.size() + diff.modified.size() + diff.removed.size()); + for (const auto& p : diff.added) out.push_back({p, FileEvent::Created}); + for (const auto& p : diff.modified) out.push_back({p, FileEvent::Modified}); + for (const auto& p : diff.removed) out.push_back({p, FileEvent::Deleted}); + return out; +} + } // namespace fn diff --git a/cpp/functions/core/file_watcher.h b/cpp/functions/core/file_watcher.h index fcfa817f..0c847d70 100644 --- a/cpp/functions/core/file_watcher.h +++ b/cpp/functions/core/file_watcher.h @@ -7,7 +7,12 @@ // Otros: stub (poll() devuelve siempre vacio) // // API no bloqueante: poll() drena los eventos disponibles desde la ultima llamada. +// +// Para plataformas sin backend nativo, el caller puede usar la helper +// file_watcher_events_from_diff que delega en `file_poll_diff` (puro) para +// derivar FileEvents a partir de dos snapshots tomados con stat()/opendir(). +#include #include #include @@ -21,6 +26,14 @@ struct FileEvent { Kind kind; }; +// Snapshot de filesystem (forma simple, identica al fn_ui::FileEntry de +// file_poll_diff_cpp_core). Se replica aqui para no acoplar headers en apps. +struct FileSnapshotEntry { + std::string path; + uint64_t size = 0; + int64_t mtime = 0; +}; + // Crea un watcher vacio. El caller llama destroy. FileWatcher* file_watcher_create(); @@ -38,4 +51,14 @@ std::vector file_watcher_poll(FileWatcher* w); // Devuelve el mensaje del ultimo error (vacio si no hay). const char* file_watcher_last_error(const FileWatcher* w); +// Helper portable: convierte dos snapshots de FS en FileEvents usando la +// logica pura `file_poll_diff_cpp_core`. Util en plataformas sin inotify/ +// RDCW o como fallback de polling. El caller construye los snapshots con +// stat()/opendir(). +// +// Mapeo: added -> Created, modified -> Modified, removed -> Deleted. +std::vector file_watcher_events_from_diff( + const std::vector& before, + const std::vector& after); + } // namespace fn diff --git a/cpp/functions/core/file_watcher.md b/cpp/functions/core/file_watcher.md index 517b38e4..d91eef4c 100644 --- a/cpp/functions/core/file_watcher.md +++ b/cpp/functions/core/file_watcher.md @@ -8,7 +8,7 @@ purity: impure signature: "fn::FileWatcher* fn::file_watcher_create(); void fn::file_watcher_destroy(fn::FileWatcher*); bool fn::file_watcher_add(fn::FileWatcher*, const char* path); std::vector fn::file_watcher_poll(fn::FileWatcher*); const char* fn::file_watcher_last_error(const fn::FileWatcher*)" description: "Watcher de archivos/directorios cross-platform (Linux inotify, Windows ReadDirectoryChangesW). API no bloqueante: registra paths con add() y consulta eventos con poll(). Cada poll() drena todos los eventos pendientes desde la llamada anterior." tags: [filesystem, watcher, inotify, file_events, io] -uses_functions: [] +uses_functions: ["file_poll_diff_cpp_core"] uses_types: [] returns: [] returns_optional: false