dwatch/source/app.d

77 lines
1.9 KiB
D
Raw 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 dfanotify;
import std.stdio;
import std.format : format;
import core.sys.posix.fcntl : AT_FDCWD;
import core.stdc.stdlib : exit;
void main()
{
Fanotify fan;
try
{
fan = new Fanotify(
FAN_CLASS_NOTIF | FAN_CLOEXEC | FAN_REPORT_FID | FAN_REPORT_DIR_FID | FAN_REPORT_NAME
);
}
catch (Exception e)
{
writeln(e.msg);
exit(1);
}
auto eventMask = FAN_OPEN | FAN_MODIFY | FAN_CLOSE | FAN_CREATE | FAN_DELETE | FAN_EVENT_ON_CHILD | FAN_ACCESS;
try
{
fan.mark(FAN_MARK_ADD | FAN_MARK_ONLYDIR, eventMask, AT_FDCWD, "/tmp/scripts");
}
catch (Exception e)
{
writeln(e.msg);
exit(1);
}
writeln(
"Мониторинг с разрешениями запущен для /tmp/scripts...");
while (true)
{
auto events = fan.readEvents();
foreach (e; events)
{
string path = e.name.length ? e.name : "unknown";
writefln("Событие: mask=0x%x, pid=%d, name/path=%s", e.mask, e.pid, path);
// Обработка permission-события
if (e.isOpenPerm)
{
writeln(" - Запрос на открытие файла. Отклоняем (предполагаем попытку записи).");
e.respond(FAN_DENY); // Отклонить (для FAN_ALLOW используйте FAN_ALLOW)
}
else
{
// Обычные уведомления (не permission)
if (e.isOpen)
writeln(" - Открытие файла");
if (e.isAccess)
writeln(" - Доступ к файлу");
if (e.isModify)
writeln(" - Модификация файла");
if (e.isCloseWrite)
writeln(" - Закрытие после записи");
if (e.isCloseNoWrite)
writeln(" - Закрытие без записи");
if (e.isCreate)
writeln(" - Создание файла/директории");
if (e.isDelete)
writeln(" - Удаление файла/директории");
}
// Опционально: вызов постобработки
e.postProcess();
}
}
}