1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
3
#include <string.h>
4
+ #include <signal.h>
5
+ #include <poll.h>
4
6
#include <errno.h>
7
+ #include <unistd.h>
5
8
#include <sys/inotify.h>
6
9
7
- #define INOTIFY_PATH "/tmp/"
10
+ #define INOTIFY_PATH "/home/linuxias/Desktop/"
11
+ static void __handle_inotify_event (const struct inotify_event * event )
12
+ {
13
+ if (event -> mask & IN_ACCESS )
14
+ printf ("IN_ACCESS " );
15
+ if (event -> mask & IN_ATTRIB )
16
+ printf ("IN_ATTRIB " );
17
+ if (event -> mask & IN_CLOSE_NOWRITE )
18
+ printf ("IN_CLOSE_NOWRITE " );
19
+ if (event -> mask & IN_CLOSE_WRITE )
20
+ printf ("IN_CLOSE_WRITE " );
21
+ if (event -> mask & IN_CREATE )
22
+ printf ("IN_CREATE " );
23
+ if (event -> mask & IN_DELETE )
24
+ printf ("IN_DELETE " );
25
+ if (event -> mask & IN_ISDIR )
26
+ printf ("IN_ISDIR " );
27
+ if (event -> mask & IN_MODIFY )
28
+ printf ("IN_MODIFY " );
29
+ if (event -> mask & IN_MOVE_SELF )
30
+ printf ("IN_MOVE_SELF " );
31
+ if (event -> mask & IN_MOVED_FROM )
32
+ printf ("IN_MOVED_FROM " );
33
+ if (event -> mask & IN_MOVED_TO )
34
+ printf ("IN_MOVED_TO " );
35
+ if (event -> mask & IN_OPEN )
36
+ printf ("IN_OPEN " );
37
+
38
+ if (event -> len > 0 )
39
+ printf (": name = %s\n" , event -> name );
40
+ }
8
41
9
42
int main (int argc , char * argv [])
10
43
{
11
44
int ret ;
12
45
int fd ;
13
46
int wd ;
47
+ char buf [4096 ] __attribute__ ((aligned (__alignof__(struct inotify_event ))));
48
+ char * ptr ;
49
+ ssize_t size ;
50
+ const struct inotify_event * event ;
14
51
15
52
fd = inotify_init ();
16
53
if (fd < 0 ) {
@@ -25,6 +62,23 @@ int main(int argc, char *argv[])
25
62
exit (EXIT_FAILURE );
26
63
}
27
64
65
+ while (1 ) {
66
+ size = read (fd , buf , sizeof (buf ));
67
+ if (size == -1 && errno != EAGAIN ) {
68
+ perror ("read" );
69
+ fprintf (stderr , "read : %s" , strerror (errno ));
70
+ exit (EXIT_FAILURE );
71
+ }
72
+
73
+ if (size <= 0 )
74
+ break ;
75
+
76
+ for (ptr = buf ; ptr < buf + size ; ptr += sizeof (struct inotify_event ) + event -> len ) {
77
+ event = (struct inotify_event * )ptr ;
78
+ __handle_inotify_event (event );
79
+ }
80
+ }
81
+
28
82
ret = inotify_rm_watch (fd , wd );
29
83
if (ret < 0 ) {
30
84
fprintf (stderr , "Failed to rm watch [fd : %d] [wd : %d] [%s]" , fd , wd , strerror (errno ));
0 commit comments