fs_events.c
2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <scot/socket.h>
#include <scot/socket_in.h>
#include <scot/fs_watcher.h>
#include <scot/event.h>
#include <scot/event_listener.h>
#include <scot/exception.h>
#include <scot/memory.h>
#include <scot_common.h>
struct scot_fs_watcher * fsw;
void
on_exit_func (void)
{
scot_event_listener_stop ((struct scot_event_listener *) fsw);
scot_fs_watcher_free (fsw);
exc_end ();
}
void
exit_func (int inum)
{
exit (0);
}
unsigned short
fs_cb (struct scot_event * _e)
{
struct scot_fs_watcher_event * fswe;
struct scot_event * e;
char * e_s;
printf ("serialized %d bytes\n", scot_event_serialize (_e, &e_s));
e = scot_event_deserialize (e, e_s);
printf ("deserialized event\n");
fswe = (struct scot_fs_watcher_event *) e;
SCOT_MEM_FREE (e_s);
switch (e->event)
{
case (SCOT_EVENT_FS_WATCHER_CREATE):
printf ("%s/%s created\n", fswe->path, fswe->name);
break;
case (SCOT_EVENT_FS_WATCHER_DELETE):
printf ("%s/%s deleted\n", fswe->path, fswe->name);
break;
case (SCOT_EVENT_FS_WATCHER_RENAME):
printf ("%s/%s renamed to %s/%s\n",
fswe->path, fswe->oldname,
fswe->path, fswe->name);
}
scot_event_free (e);
return SCOT_EVENT_END;
}
int
main (int argc, char * argv [])
{
excenv_t * ee;
signal (SIGINT, exit_func);
atexit (on_exit_func);
TRY
{
fsw = scot_fs_watcher_new ();
scot_event_listener_register_cb (
(struct scot_event_listener *) fsw,
SCOT_EVENT_FS_WATCHER_CREATE,
fs_cb, NULL);
scot_event_listener_start ((struct scot_event_listener *) fsw);
}
CATCH (ee)
{
print_all_exceptions (ee);
exit (EXIT_FAILURE);
}
TRY
{
char buffer [1024];
fgets (buffer, 1024, stdin);
*strchr (buffer, '\n') = '\0';
while (strcmp ("quit", buffer) != 0)
{
scot_fs_watcher_add (
fsw, buffer, 0 | SCOT_EVENT_FS_WATCHER_CREATE |
SCOT_EVENT_FS_WATCHER_DELETE |
SCOT_EVENT_FS_WATCHER_RENAME);
fgets (buffer, 1024, stdin);
*strchr (buffer, '\n') = '\0';
}
}
CATCH (ee)
{
exception_t *e;
while (e = retrive_exception (ee))
{
print_exception (e);
free_exception (e);
}
free_catched (ee);
}
return 0;
}