Добавлен новый параметр для ограничения файлов по размеру

This commit is contained in:
Alexander Zhirov 2025-11-16 12:12:20 +03:00
parent dedfe11200
commit 4b18136acf
Signed by: alexander
GPG key ID: C8D8BE544A27C511
4 changed files with 28 additions and 4 deletions

View file

@ -3,7 +3,8 @@
"logfile": "/var/log/dwatch.log",
"watch": [
{
"path": "/home/alexander/Programming/work/dwatch"
"path": "/home/alexander/Programming/work/dwatch",
"size": 512
}
]
}

View file

@ -112,6 +112,7 @@ private:
struct Watch {
string path;
string[] rules;
ulong size;
}
bool _global;
@ -184,7 +185,7 @@ public:
}
tempMountPoints.insert(systemMountPoint);
_watch ~= Watch(watch.path, watch.rules.get.dup);
_watch ~= Watch(watch.path, watch.rules.get.dup, watch.size);
}
_mountPoints = tempMountPoints.array;
@ -215,6 +216,9 @@ public:
info("База данных: ", _database);
foreach (watch; _watch) {
info("Отслеживание директории: ", watch.path);
if (watch.size > 0) {
infof(" Отслеживаемые файлы имеют установленное ограничение в %dK", watch.size);
}
if (watch.rules.length >0) {
info(" Правила отслеживания файлов в директории (нотация правил Git):");
}
@ -294,11 +298,13 @@ public:
} else {
string childPath;
string[] rules;
ulong size;
foreach (Watch watch; _watch) {
if (path.startsWith(watch.path)) {
childPath = path[watch.path.length .. $];
rules = watch.rules;
size = watch.size;
trace("Кандидат для отслеживания: ", childPath);
break;
}
@ -310,7 +316,9 @@ public:
// Получение информации о процессе (паспорт процесса)
// key как out объект, получение данных из функции
if (getDevIno(fd, key, pid)) {
if (key !in _gMap) {
if (size > 0 && key.getSize() > size) {
warningf("Файл пропущен, так как размер превышает допустимое ограничение: %s - %.3fK", path, key.getSize());
} else if (key !in _gMap) {
// Получение имени процесса
auto procName = readProcComm(pid);
// Получение идентификаторов пользователя

View file

@ -78,11 +78,13 @@ final class DWWatch
private:
string _path;
DWRules _rules;
ulong _size;
public:
this(const JSONValue watchJson)
{
_path = watchJson.jget!string("path", JSONType.STRING);
_size = watchJson.jgetOptional!long("size", JSONType.INTEGER);
auto rj = watchJson.jgetOptional!JSONValue("rules", JSONType.OBJECT);
_rules = new DWRules(rj);
}
@ -92,6 +94,11 @@ public:
return _path;
}
@property ulong size() const
{
return _size;
}
@property const(DWRules) rules() const
{
return _rules;

View file

@ -16,6 +16,9 @@ import std.container : RedBlackTree;
import wildmatch;
// 1.0 / 1024.0
private enum double BYTES_TO_KIB = 0.0009765625;
// Получить точки монтирования файловых систем
const(string)[] mountPointsFilesystem(const(string)[] skipMounts = ["/dev", "/sys", "/proc", "/run", "/tmp"]) {
RedBlackTree!string mountPoints = new RedBlackTree!string();
@ -102,6 +105,7 @@ struct DevIno {
ulong dev;
ulong ino;
int pid;
double size;
bool opEquals(const DevIno rhs) const @safe nothrow {
return dev == rhs.dev && ino == rhs.ino && pid == rhs.pid;
@ -110,12 +114,16 @@ struct DevIno {
size_t toHash() const @safe nothrow {
return (dev * 1_315_423_911UL) ^ (ino * 2_654_435_761UL) ^ (cast(size_t) pid);
}
double getSize() const @safe nothrow {
return size;
}
}
bool getDevIno(int fd, out DevIno di, int pid) {
stat_t st;
if (fstat(fd, &st) != 0) return false;
di = DevIno(cast(ulong) st.st_dev, cast(ulong) st.st_ino, pid);
di = DevIno(cast(ulong) st.st_dev, cast(ulong) st.st_ino, pid, cast(long) st.st_size * BYTES_TO_KIB);
return true;
}