refactor(cpp/core): file_watcher usa file_poll_diff

This commit is contained in:
2026-04-28 23:55:11 +02:00
parent 02c9dd93e3
commit f2521bd7d2
3 changed files with 51 additions and 1 deletions
+27
View File
@@ -1,5 +1,7 @@
#include "file_watcher.h"
#include "core/file_poll_diff.h"
#include <cstring>
#include <unordered_map>
#include <vector>
@@ -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<FileEvent> file_watcher_events_from_diff(
const std::vector<FileSnapshotEntry>& before,
const std::vector<FileSnapshotEntry>& after) {
// Convertir a fn_ui::FileEntry para llamar a la funcion pura.
std::vector<fn_ui::FileEntry> 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<FileEvent> 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
+23
View File
@@ -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 <cstdint>
#include <string>
#include <vector>
@@ -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<FileEvent> 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<FileEvent> file_watcher_events_from_diff(
const std::vector<FileSnapshotEntry>& before,
const std::vector<FileSnapshotEntry>& after);
} // namespace fn
+1 -1
View File
@@ -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::FileEvent> 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