From 5e9317b2fdb5c4515fa6950e55787c98fd785416 Mon Sep 17 00:00:00 2001 From: liaozhaoyan Date: Mon, 17 Apr 2023 15:15:13 +0800 Subject: [PATCH] add example for multiInfotifies. --- .../monitor/unity/test/lab/mulInotifies.lua | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 source/tools/monitor/unity/test/lab/mulInotifies.lua diff --git a/source/tools/monitor/unity/test/lab/mulInotifies.lua b/source/tools/monitor/unity/test/lab/mulInotifies.lua new file mode 100644 index 00000000..13a771d7 --- /dev/null +++ b/source/tools/monitor/unity/test/lab/mulInotifies.lua @@ -0,0 +1,115 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by liaozhaoyan. +--- DateTime: 2023/4/17 14:12 +--- + +package.path = package.path .. ";../../?.lua;" + +require("common.class") +local Cinotifies = require("common.inotifies") +local system = require("common.system") +local dirent = require("posix.dirent") +local pstat = require("posix.sys.stat") + +local Cevent = class("event") + +function Cevent:_init_(path) + self._path = path + print("hello " .. path) +end + +function Cevent:_del_() + print("del ".. self._path) +end + +local function addEvents(tbl, path) + tbl[path] = Cevent.new(path) +end + +local function delEvents(tbl, path) + tbl[path] = nil +end + +local CMulIno = class("CmulIno") +function CMulIno:_init_(path) + self._path = path + self._events = {} + self._ino, self._dirs = self:build(path) + + for _, dir in ipairs(self._dirs) do + addEvents(self._events, dir) + end +end + +local function walk_dirs(path, dirs, ino) + local files = dirent.files(path) + for f in files do + if string.sub(f, 1, 1) ~= '.' then + local full = table.concat({path, f}, "/") + local lStat = pstat.lstat(full) + if pstat.S_ISDIR(lStat.st_mode) > 0 then + table.insert(dirs, full) + ino:add(full) + walk_dirs(full, dirs, ino) + end + end + end +end + +function CMulIno:build(path) + local ino = Cinotifies.new() + ino:add(path) + + local dirs = {path} + walk_dirs(path, dirs, ino) + return ino, dirs +end + +local function calcBoolean(newDir, oldDir) + local toAdd, toDel = {}, {} + + for _, k in ipairs(newDir) do + if not system:valueIsIn(oldDir, k) then + table.insert(toAdd, k) + end + end + + for _, k in ipairs(oldDir) do + if not system:valueIsIn(oldDir, k) then + table.insert(toDel, k) + end + end + return toAdd, toDel +end + + +function CMulIno:change(toAdd, toDel) + for _, k in ipairs(toAdd) do + addEvents(self._events, k) + end + + for _, k in ipairs(toDel) do + delEvents(self._events, k) + end + +end + +function CMulIno:work() + while true do + if self._ino:isChange() then + print("change.") + local dirs + + self._ino, dirs = self:build(self._path) + local toAdd, toDel = calcBoolean(dirs, self._dirs) + self:change(toAdd, toDel) + self._dirs = dirs + collectgarbage("collect") + end + system:sleep(1) + end +end + +local mulIno = CMulIno.new("/tmp") +mulIno:work() -- Gitee