Skip to content

Commit e398656

Browse files
authored
Merge pull request #190 from tboox/fwatcher
Add fs watcher
2 parents fd67274 + 7f67f09 commit e398656

20 files changed

+1760
-4
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### New features
6+
7+
* [#190](https://github.com/tboox/tbox/pull/190): Add fs watcher
8+
59
### Changes
610

711
* Support wasm
@@ -249,6 +253,10 @@
249253

250254
## master (开发中)
251255

256+
### 新特性
257+
258+
* [#190](https://github.com/tboox/tbox/pull/190): 添加文件系统状态监视器
259+
252260
### 改进
253261

254262
* 支持 wasm

src/demo/coroutine/fwatcher.c

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* //////////////////////////////////////////////////////////////////////////////////////
2+
* includes
3+
*/
4+
#include "../demo.h"
5+
6+
/* //////////////////////////////////////////////////////////////////////////////////////
7+
* private implementation
8+
*/
9+
static tb_void_t tb_demo_coroutine_watch(tb_cpointer_t priv)
10+
{
11+
tb_char_t const* path = (tb_char_t const*)priv;
12+
tb_fwatcher_ref_t fwatcher = tb_fwatcher_init();
13+
if (fwatcher)
14+
{
15+
tb_trace_i("watching %s", path);
16+
if (tb_fwatcher_add(fwatcher, path))
17+
{
18+
tb_bool_t eof = tb_false;
19+
tb_long_t count = 0;
20+
tb_fwatcher_event_t events[64];
21+
while (!eof && (count = tb_fwatcher_wait(fwatcher, events, tb_arrayn(events), -1)) >= 0)
22+
{
23+
for (tb_size_t i = 0; i < count && !eof; i++)
24+
{
25+
tb_fwatcher_event_t const* event = &events[i];
26+
tb_char_t const* status = event->event == TB_FWATCHER_EVENT_CREATE? "created" :
27+
(event->event == TB_FWATCHER_EVENT_MODIFY? "modified" : "deleted");
28+
tb_trace_i("watch: %s %s", event->filepath, status);
29+
if (tb_strstr(event->filepath, "eof"))
30+
eof = tb_true;
31+
}
32+
}
33+
}
34+
tb_fwatcher_exit(fwatcher);
35+
}
36+
}
37+
38+
static tb_void_t tb_demo_coroutine_sleep(tb_cpointer_t priv)
39+
{
40+
while (1)
41+
{
42+
tb_trace_i("sleep ..");
43+
tb_sleep(1);
44+
}
45+
}
46+
47+
/* //////////////////////////////////////////////////////////////////////////////////////
48+
* main
49+
*/
50+
tb_int_t tb_demo_coroutine_fwatcher_main(tb_int_t argc, tb_char_t** argv)
51+
{
52+
tb_co_scheduler_ref_t scheduler = tb_co_scheduler_init();
53+
if (scheduler)
54+
{
55+
// start coroutines
56+
tb_coroutine_start(scheduler, tb_demo_coroutine_watch, argv[1], 1024 * 1024);
57+
tb_coroutine_start(scheduler, tb_demo_coroutine_sleep, tb_null, 0);
58+
59+
// do loop
60+
tb_co_scheduler_loop(scheduler, tb_true);
61+
tb_co_scheduler_exit(scheduler);
62+
}
63+
64+
return 0;
65+
}

src/demo/demo.c

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ static tb_demo_t g_demo[] =
175175
, TB_DEMO_MAIN_ITEM(platform_environment)
176176
, TB_DEMO_MAIN_ITEM(platform_pipe_pair)
177177
, TB_DEMO_MAIN_ITEM(platform_named_pipe)
178+
, TB_DEMO_MAIN_ITEM(platform_fwatcher)
178179
, TB_DEMO_MAIN_ITEM(platform_lock)
179180
, TB_DEMO_MAIN_ITEM(platform_timer)
180181
, TB_DEMO_MAIN_ITEM(platform_ltimer)
@@ -227,6 +228,7 @@ static tb_demo_t g_demo[] =
227228
, TB_DEMO_MAIN_ITEM(coroutine_semaphore)
228229
, TB_DEMO_MAIN_ITEM(coroutine_process)
229230
, TB_DEMO_MAIN_ITEM(coroutine_process_pipe)
231+
, TB_DEMO_MAIN_ITEM(coroutine_fwatcher)
230232
, TB_DEMO_MAIN_ITEM(coroutine_echo_server)
231233
, TB_DEMO_MAIN_ITEM(coroutine_echo_client)
232234
, TB_DEMO_MAIN_ITEM(coroutine_unix_echo_server)

src/demo/demo.h

+2
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ TB_DEMO_MAIN_DECL(platform_poller_client);
185185
TB_DEMO_MAIN_DECL(platform_poller_server);
186186
TB_DEMO_MAIN_DECL(platform_poller_process);
187187
TB_DEMO_MAIN_DECL(platform_context);
188+
TB_DEMO_MAIN_DECL(platform_fwatcher);
188189

189190
// container
190191
TB_DEMO_MAIN_DECL(container_heap);
@@ -219,6 +220,7 @@ TB_DEMO_MAIN_DECL(coroutine_thread);
219220
TB_DEMO_MAIN_DECL(coroutine_pipe);
220221
TB_DEMO_MAIN_DECL(coroutine_process);
221222
TB_DEMO_MAIN_DECL(coroutine_process_pipe);
223+
TB_DEMO_MAIN_DECL(coroutine_fwatcher);
222224
TB_DEMO_MAIN_DECL(coroutine_echo_client);
223225
TB_DEMO_MAIN_DECL(coroutine_echo_server);
224226
TB_DEMO_MAIN_DECL(coroutine_unix_echo_client);

src/demo/platform/fwatcher.c

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* //////////////////////////////////////////////////////////////////////////////////////
2+
* includes
3+
*/
4+
#include "../demo.h"
5+
6+
/* //////////////////////////////////////////////////////////////////////////////////////
7+
* implementation
8+
*/
9+
static tb_bool_t g_stop = tb_false;
10+
static tb_fwatcher_ref_t g_fwatcher = tb_null;
11+
static tb_int_t tb_demo_watcher(tb_cpointer_t priv)
12+
{
13+
tb_char_t const* path = (tb_char_t const*)priv;
14+
tb_fwatcher_ref_t fwatcher = tb_fwatcher_init();
15+
if (fwatcher)
16+
{
17+
g_fwatcher = fwatcher;
18+
tb_trace_i("watching %s", path);
19+
if (tb_fwatcher_add(fwatcher, path))
20+
{
21+
tb_bool_t eof = tb_false;
22+
tb_long_t count = 0;
23+
tb_fwatcher_event_t events[64];
24+
while (!eof && !g_stop && (count = tb_fwatcher_wait(fwatcher, events, tb_arrayn(events), -1)) >= 0)
25+
{
26+
for (tb_size_t i = 0; i < count && !eof; i++)
27+
{
28+
tb_fwatcher_event_t const* event = &events[i];
29+
tb_char_t const* status = event->event == TB_FWATCHER_EVENT_CREATE? "created" :
30+
(event->event == TB_FWATCHER_EVENT_MODIFY? "modified" : "deleted");
31+
tb_trace_i("watch: %s %s", event->filepath, status);
32+
if (tb_strstr(event->filepath, "eof"))
33+
eof = tb_true;
34+
}
35+
}
36+
}
37+
tb_fwatcher_exit(fwatcher);
38+
}
39+
g_fwatcher = tb_null;
40+
return 0;
41+
}
42+
43+
/* //////////////////////////////////////////////////////////////////////////////////////
44+
* main
45+
*/
46+
tb_int_t tb_demo_platform_fwatcher_main(tb_int_t argc, tb_char_t** argv)
47+
{
48+
tb_thread_ref_t thread = tb_thread_init(tb_null, tb_demo_watcher, argv[1], 0);
49+
if (thread)
50+
{
51+
tb_getchar();
52+
g_stop = tb_true;
53+
if (g_fwatcher) tb_fwatcher_spak(g_fwatcher);
54+
tb_thread_wait(thread, -1, tb_null);
55+
tb_thread_exit(thread);
56+
}
57+
return 0;
58+
}

src/demo/xmake.lua

+5
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,8 @@ target("demo")
9393
if is_plat("bsd") then
9494
add_syslinks("execinfo")
9595
end
96+
97+
-- add frameworks
98+
if is_plat("macosx") then
99+
add_frameworks("CoreFoundation", "CoreServices")
100+
end

src/tbox/memory/string_pool.c

+13
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ tb_void_t tb_string_pool_remove(tb_string_pool_ref_t self, tb_char_t const* data
178178
}
179179
}
180180
}
181+
tb_bool_t tb_string_pool_has(tb_string_pool_ref_t self, tb_char_t const* data)
182+
{
183+
// check
184+
tb_string_pool_t* pool = (tb_string_pool_t*)self;
185+
tb_assert_and_check_return_val(pool && data, tb_false);
186+
187+
if (pool->cache)
188+
{
189+
tb_size_t itor = tb_hash_map_find(pool->cache, data);
190+
return itor != tb_iterator_tail(pool->cache);
191+
}
192+
return tb_false;
193+
}
181194
#ifdef __tb_debug__
182195
tb_void_t tb_string_pool_dump(tb_string_pool_ref_t self)
183196
{

src/tbox/memory/string_pool.h

+9
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ tb_char_t const* tb_string_pool_insert(tb_string_pool_ref_t pool, tb_
8181
*/
8282
tb_void_t tb_string_pool_remove(tb_string_pool_ref_t pool, tb_char_t const* data);
8383

84+
/*! has this string?
85+
*
86+
* @param pool the string pool
87+
* @param data the string data
88+
*
89+
* @return tb_true or tb_false
90+
*/
91+
tb_bool_t tb_string_pool_has(tb_string_pool_ref_t pool, tb_char_t const* data);
92+
8493
#ifdef __tb_debug__
8594
/*! dump the string pool
8695
*

0 commit comments

Comments
 (0)