1
1
#include < stdlib.h>
2
+ #include < string.h>
2
3
#include < sys/param.h>
3
4
#include < sysexits.h>
4
- #include < string.h>
5
5
6
6
#include < chrono>
7
7
#include < unordered_map>
8
8
9
9
#include " main.h"
10
+
10
11
#include " utils.h"
11
12
12
13
#include " watch.h"
13
14
#include " watch_fsevents.h"
14
- #include " watch_null.h"
15
15
#include " watch_inotify.h"
16
+ #include " watch_null.h"
16
17
17
18
#include " notify.h"
18
19
#include " notify_udp.h"
@@ -24,9 +25,10 @@ typedef std::chrono::time_point<Clock> TimePoint;
24
25
typedef std::unordered_map<std::string, TimePoint> FeedbackMap;
25
26
typedef std::vector<std::pair<std::string, std::string>> PrefixMap;
26
27
27
- static IWatchPlugin * gCurrentWatchInstance = nullptr ;
28
+ static IWatchPlugin* gCurrentWatchInstance = nullptr ;
28
29
29
- static void handle_sigint (__unused int signal) {
30
+ static void handle_sigint (__unused int signal)
31
+ {
30
32
if (gCurrentWatchInstance ) {
31
33
gCurrentWatchInstance ->stop ();
32
34
}
@@ -35,7 +37,8 @@ static void handle_sigint(__unused int signal) {
35
37
}
36
38
}
37
39
38
- static void print_usage (char *argv0) {
40
+ static void print_usage (char * argv0)
41
+ {
39
42
fprintf (stderr, " Usage: %s watch [-c host[:port]] (<from> <to>)...\n " , argv0);
40
43
fprintf (stderr, " \n " );
41
44
fprintf (stderr, " (<from> <to>) pairs of paths, the first being the local path to watch\n " );
@@ -44,7 +47,8 @@ static void print_usage(char *argv0) {
44
47
fprintf (stderr, " \n " );
45
48
}
46
49
47
- static std::string replace_prefix (const PrefixMap &prefix_map, std::string src) {
50
+ static std::string replace_prefix (const PrefixMap& prefix_map, std::string src)
51
+ {
48
52
for (const auto & pair : prefix_map) {
49
53
50
54
if (src.substr (0 , pair.first .size ()) == pair.first ) {
@@ -55,7 +59,8 @@ static std::string replace_prefix(const PrefixMap &prefix_map, std::string src)
55
59
return src;
56
60
}
57
61
58
- static void clear_expired (FeedbackMap &feedback_map) {
62
+ static void clear_expired (FeedbackMap& feedback_map)
63
+ {
59
64
TimePoint now = Clock::now ();
60
65
61
66
for (auto it = feedback_map.begin (); it != feedback_map.end ();) {
@@ -68,8 +73,9 @@ static void clear_expired(FeedbackMap &feedback_map) {
68
73
}
69
74
}
70
75
71
- static std::vector<std::string> filter_feedback (FeedbackMap &feedback_map,
72
- std::vector<std::string> paths) {
76
+ static std::vector<std::string> filter_feedback (
77
+ FeedbackMap& feedback_map, std::vector<std::string> paths)
78
+ {
73
79
clear_expired (feedback_map);
74
80
75
81
std::vector<std::string> out_paths;
@@ -85,7 +91,8 @@ static std::vector<std::string> filter_feedback(FeedbackMap &feedback_map,
85
91
return out_paths;
86
92
}
87
93
88
- int main_watch (char *argv0, int argc, char ** argv) {
94
+ int main_watch (char * argv0, int argc, char ** argv)
95
+ {
89
96
std::vector<std::string> watch_paths;
90
97
std::vector<std::pair<std::string, std::string>> prefix_map;
91
98
FeedbackMap feedback_map;
@@ -128,20 +135,21 @@ int main_watch(char *argv0, int argc, char** argv) {
128
135
}
129
136
130
137
watch_paths.emplace_back (out_path);
131
- prefix_map.emplace_back (out_path, argv[i+ 1 ]);
138
+ prefix_map.emplace_back (out_path, argv[i + 1 ]);
132
139
}
133
140
134
141
UDPNotifyPlugin notify_plugin (dest_addr, port);
135
142
136
- WatchCallback callback = [¬ify_plugin, &prefix_map, &feedback_map](std::vector<std::string> paths) {
137
- std::vector<std::string> out_paths;
143
+ WatchCallback callback
144
+ = [¬ify_plugin, &prefix_map, &feedback_map](std::vector<std::string> paths) {
145
+ std::vector<std::string> out_paths;
138
146
139
- for (auto & path : paths) {
140
- out_paths.emplace_back (replace_prefix (prefix_map, path));
141
- }
147
+ for (auto & path : paths) {
148
+ out_paths.emplace_back (replace_prefix (prefix_map, path));
149
+ }
142
150
143
- notify_plugin.notify (filter_feedback (feedback_map, out_paths));
144
- };
151
+ notify_plugin.notify (filter_feedback (feedback_map, out_paths));
152
+ };
145
153
146
154
WATCH_PLUGIN_TYPE plugin (watch_paths, callback);
147
155
SignalOverride (SIGINT, handle_sigint);
0 commit comments