dwatch/source/app.d

54 lines
1.7 KiB
D
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fanotify_wrapper;
import std.stdio;
import std.file : readLink;
import std.format : format;
import core.sys.posix.fcntl : AT_FDCWD;
import core.sys.posix.unistd : close;
void main()
{
// Инициализация fanotify в режиме уведомлений (без разрешений), с CLOEXEC
auto fan = new Fanotify(FAN_CLASS_NOTIF | FAN_CLOEXEC);
// Маска событий: открытие, модификация, закрытие (с флагом на события в подфайлах)
auto eventMask = FAN_OPEN | FAN_MODIFY | FAN_CLOSE | FAN_EVENT_ON_CHILD;
// Маркировка директории /tmp/scripts
fan.mark(FAN_MARK_ADD, eventMask, AT_FDCWD, "/tmp/scripts");
writeln("Мониторинг запущен для /tmp/scripts...");
// Бесконечный цикл чтения событий
while (true)
{
auto events = fan.readEvents();
foreach (ref e; events)
{
string path = "unknown";
if (e.eventFd != FAN_NOFD)
{
try
{
path = readLink(format("/proc/self/fd/%d", e.eventFd));
}
catch (Exception ex)
{
// Игнорируем ошибки (например, если файл удалён)
}
close(e.eventFd); // Обязательно закрываем fd после обработки
}
// Вывод информации о событии
writefln("Событие: mask=0x%x, pid=%d, path=%s", e.mask, e.pid, path);
if (e.isOpen)
writeln(" - Открытие файла");
if (e.isModify)
writeln(" - Модификация файла");
if (e.isCloseWrite)
writeln(" - Закрытие после записи");
if (e.isCloseNoWrite)
writeln(" - Закрытие без записи");
}
}
}