Skip to content

Commit f7337da

Browse files
committed
[CMakeLists.txt 和其他文件更新]: 引入新依赖并优化目录监控功能
- 添加了新的子目录`MonitorDir_EFSW`,用于实现基于`efsw`的目录监控功能。 - 在`MonitorDir_EFSW`目录下创建了`CMakeLists.txt`和`main.cc`文件,实现了一个简单的目录监控示例。 - 更新了`README.md`文件,添加了对新目录监控示例的介绍,并调整了其他部分的编号和内容。 - 在`vcpkg.json`中添加了`efsw`作为依赖项,以确保可以通过vcpkg管理这些依赖。
1 parent bebdce9 commit f7337da

File tree

5 files changed

+138
-19
lines changed

5 files changed

+138
-19
lines changed

CMakeLists.txt

+17-12
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,26 @@ find_package(unofficial-breakpad CONFIG REQUIRED)
1616
if(unofficial-breakpad_FOUND)
1717
message(STATUS "found unofficial-breakpad")
1818
endif()
19-
find_package(glog CONFIG REQUIRED)
20-
if(glog_FOUND)
21-
message(STATUS "found glog")
22-
endif()
2319
find_package(benchmark CONFIG REQUIRED)
2420
if(benchmark_FOUND)
2521
message(STATUS "found benchmark")
2622
endif()
23+
find_package(crashpad CONFIG REQUIRED)
24+
if(crashpad_FOUND)
25+
message(STATUS "found crashpad")
26+
endif()
27+
find_package(CURL CONFIG REQUIRED)
28+
if(CURL_FOUND)
29+
message(STATUS "found CURL")
30+
endif()
31+
find_package(efsw CONFIG REQUIRED)
32+
if(efsw_FOUND)
33+
message(STATUS "found efsw")
34+
endif()
35+
find_package(glog CONFIG REQUIRED)
36+
if(glog_FOUND)
37+
message(STATUS "found glog")
38+
endif()
2739
find_package(GTest CONFIG REQUIRED)
2840
if(GTest_FOUND)
2941
message(STATUS "found GTest")
@@ -32,14 +44,6 @@ find_package(OpenSSL REQUIRED)
3244
if(OpenSSL_FOUND)
3345
message(STATUS "found OpenSSL")
3446
endif()
35-
find_package(CURL CONFIG REQUIRED)
36-
if(CURL_FOUND)
37-
message(STATUS "found CURL")
38-
endif()
39-
find_package(crashpad CONFIG REQUIRED)
40-
if(crashpad_FOUND)
41-
message(STATUS "found crashpad")
42-
endif()
4347
find_package(Threads REQUIRED)
4448
if(Threads_FOUND)
4549
message(STATUS "found Threads")
@@ -61,6 +65,7 @@ add_subdirectory(Glog)
6165
add_subdirectory(LinkedList)
6266
add_subdirectory(Memcpy)
6367
add_subdirectory(MonitorDir)
68+
add_subdirectory(MonitorDir_EFSW)
6469
add_subdirectory(Mutex)
6570
add_subdirectory(OpenSSL)
6671
add_subdirectory(Thread)

MonitorDir_EFSW/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_executable(monitor_dir_efsw main.cc)
2+
target_link_libraries(monitor_dir_efsw PRIVATE efsw::efsw)

MonitorDir_EFSW/main.cc

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include <efsw/efsw.hpp>
2+
3+
#include <filesystem>
4+
#include <iostream>
5+
#include <signal.h>
6+
#include <thread>
7+
8+
bool STOP = false;
9+
10+
void sigend(int)
11+
{
12+
std::cout << std::endl << "Bye bye" << std::endl;
13+
STOP = true;
14+
}
15+
16+
std::string getActionName(efsw::Action action)
17+
{
18+
switch (action) {
19+
case efsw::Actions::Add: return "Add";
20+
case efsw::Actions::Modified: return "Modified";
21+
case efsw::Actions::Delete: return "Delete";
22+
case efsw::Actions::Moved: return "Moved";
23+
default: break;
24+
}
25+
return "Bad Action";
26+
}
27+
28+
efsw::WatchID handleWatchID(efsw::WatchID watchid)
29+
{
30+
switch (watchid) {
31+
case efsw::Errors::FileNotFound:
32+
case efsw::Errors::FileRepeated:
33+
case efsw::Errors::FileOutOfScope:
34+
case efsw::Errors::FileRemote:
35+
case efsw::Errors::WatcherFailed:
36+
case efsw::Errors::Unspecified: {
37+
std::cout << efsw::Errors::Log::getLastErrorLog().c_str() << std::endl;
38+
break;
39+
}
40+
default: {
41+
std::cout << "Added WatchID: " << watchid << std::endl;
42+
}
43+
}
44+
return watchid;
45+
}
46+
47+
/// Processes a file action
48+
class UpdateListener : public efsw::FileWatchListener
49+
{
50+
public:
51+
UpdateListener() {}
52+
53+
void handleFileAction(efsw::WatchID watchid,
54+
const std::string &dir,
55+
const std::string &filename,
56+
efsw::Action action,
57+
std::string oldFilename = "") override
58+
{
59+
std::cout << "Watch ID " << watchid << " DIR ("
60+
<< dir + ") FILE ("
61+
+ (oldFilename.empty() ? "" : "from file " + oldFilename + " to ")
62+
+ filename + ") has event "
63+
<< getActionName(action) << std::endl;
64+
}
65+
};
66+
67+
int main(int argc, char **argv)
68+
{
69+
signal(SIGABRT, sigend);
70+
signal(SIGINT, sigend);
71+
signal(SIGTERM, sigend);
72+
73+
std::cout << "Press ^C to exit demo" << std::endl;
74+
75+
bool useGeneric = false;
76+
auto filepath = std::filesystem::current_path().parent_path().parent_path();
77+
std::cout << filepath << std::endl;
78+
79+
auto *ul = new UpdateListener;
80+
81+
/// create the file watcher object
82+
efsw::FileWatcher fileWatcher(useGeneric);
83+
84+
fileWatcher.followSymlinks(false);
85+
fileWatcher.allowOutOfScopeLinks(false);
86+
87+
/// starts watching
88+
fileWatcher.watch();
89+
90+
/// add a watch to the system
91+
handleWatchID(fileWatcher.addWatch((filepath / "bin-64").string(), ul, true));
92+
93+
/// adds another watch after started watching...
94+
std::this_thread::sleep_for(std::chrono::seconds(1));
95+
96+
efsw::WatchID watchID = handleWatchID(
97+
fileWatcher.addWatch((filepath / "build").string(), ul, true));
98+
99+
/// delete the watch
100+
if (watchID > 0) {
101+
std::this_thread::sleep_for(std::chrono::seconds(1));
102+
fileWatcher.removeWatch(watchID);
103+
}
104+
105+
while (!STOP) {
106+
std::this_thread::sleep_for(std::chrono::seconds(1));
107+
}
108+
109+
return 0;
110+
}

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
1. `fanotify`中使用`global`模式,在`fanotify_mark`中加入`FAN_MARK_FILESYSTEM`(需要`CAP_SYS_ADMIN`能力,即root权限)这个`flag`,可以所有在指定文件系统上的事件都会被监控,然后可以根据指定的监控的文件夹目录过滤需要的事件,这个功能比`inotify`更强大;
3535
1. 获取事件发生文件所在的上级路径中,使用了`open_by_handle_at`这个方法,普通用户下会出现`Operation not permitted`错误,也需要`CAP_SYS_ADMIN`能力,即root权限;
3636
2. 建议使用root权限运行,如果一定要在普通用户下运行,还是建议使用`inotify`而不是`fanotify`,反正在打开一个监控文件描述符(`fd`)的情况下,都无法实现`subtree`监控;
37-
23. [Mutex](/Mutex/mutex.hpp)——使用std::atomic_flag实现的简单互斥锁和自旋锁;
38-
24. [OpenSSL](/OpenSSL)——openssl的一些例子;
37+
23. [MonitorDir_EFSW](/MonitorDir_EFSW/main.cc)——使用efsw实现的目录监控的简单例子;
38+
24. [Mutex](/Mutex/mutex.hpp)——使用std::atomic_flag实现的简单互斥锁和自旋锁;
39+
25. [OpenSSL](/OpenSSL)——openssl的一些例子;
3940
1. [aes](/OpenSSL/openssl_aes.cc)——aes加解密的例子;
4041
2. [base64](/OpenSSL/openssl_base64.cc)——base64编解码的例子;
4142
3. [hash](/OpenSSL/openssl_hash.cc)——sha256的例子;
@@ -45,10 +46,10 @@
4546
7. [sm4](/OpenSSL/openssl_sm4.cc)——sm4加解密的例子;
4647
8. [x509](/OpenSSL/openssl_x509.cc)——x509证书的例子;
4748
9. [bash](/OpenSSL/openssl_bash.sh)——openssl命令行的例子;
48-
25. [Server](/Server)——linux server的一些例子;
49+
26. [Server](/Server)——linux server的一些例子;
4950
1. [server_epoll](/Server/server_epoll.cc)——epoll的例子;
5051
2. [server_poll](/Server/server_poll.cc)——poll的例子;
5152
3. [server_select](/Server/server_select.cc)——select的例子;
52-
26. [Thread](/Thread/)——基于std::thread实现的线程类,包括线程池;
53+
27. [Thread](/Thread/)——基于std::thread实现的线程类,包括线程池;
5354
1. [Thread](/Thread/thread.hpp)——线程类;
5455
2. [ThreadPool](/Thread/threadpool.hpp)——线程池;

vcpkg.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"version": "0.0.1",
55
"description": "manifest",
66
"dependencies": [
7-
"glog",
87
"breakpad",
98
"benchmark",
10-
"gtest",
119
"crashpad",
10+
"efsw",
11+
"glog",
12+
"gtest",
1213
{
1314
"name": "openssl",
1415
"features": [
@@ -27,4 +28,4 @@
2728
}
2829
],
2930
"builtin-baseline": "b545373a9a536dc559dac8583467a21497a0e897"
30-
}
31+
}

0 commit comments

Comments
 (0)