1 /*
2  * Copyright (c) 2017 Balabit
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation, or (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  *
17  * As an additional exemption you are allowed to compile & link against the
18  * OpenSSL libraries as published by the OpenSSL project. See the file
19  * COPYING for details.
20  *
21  */
22 #ifndef MODULES_AFFILE_DIRECTORY_MONITOR_H_
23 #define MODULES_AFFILE_DIRECTORY_MONITOR_H_
24 
25 #include <syslog-ng.h>
26 #include <iv.h>
27 
28 typedef enum
29 {
30   FILE_CREATED,
31   DIRECTORY_CREATED,
32   FILE_DELETED,
33   DIRECTORY_DELETED,
34   UNKNOWN
35 } DirectoryMonitorEventType;
36 
37 typedef struct _DirectoryMonitorEvent
38 {
39   const gchar *name;
40   gchar *full_path;
41   DirectoryMonitorEventType event_type;
42 } DirectoryMonitorEvent;
43 
44 typedef  void (*DirectoryMonitorEventCallback)(const DirectoryMonitorEvent *event,
45                                                gpointer user_data);
46 
47 typedef struct _DirectoryMonitor DirectoryMonitor;
48 
49 struct _DirectoryMonitor
50 {
51   gchar *dir;
52   gchar *real_path;
53   DirectoryMonitorEventCallback callback;
54   gpointer callback_data;
55 
56   guint recheck_time;
57   struct iv_timer check_timer;
58   struct iv_task scheduled_destructor;
59 
60   gboolean watches_running;
61   void (*start_watches)(DirectoryMonitor *self);
62   void (*stop_watches)(DirectoryMonitor *self);
63   void (*free_fn)(DirectoryMonitor *self);
64 };
65 
66 DirectoryMonitor *directory_monitor_new(const gchar *dir, guint recheck_time);
67 void directory_monitor_init_instance(DirectoryMonitor *self, const gchar *dir, guint recheck_time);
68 void directory_monitor_free(DirectoryMonitor *self);
69 void directory_monitor_set_callback(DirectoryMonitor *self, DirectoryMonitorEventCallback callback, gpointer user_data);
70 
71 void directory_monitor_start(DirectoryMonitor *self);
72 void directory_monitor_stop(DirectoryMonitor *self);
73 
74 void directory_monitor_stop_and_destroy(DirectoryMonitor *self);
75 void directory_monitor_schedule_destroy(DirectoryMonitor *self);
76 
77 gchar *build_filename(const gchar *basedir, const gchar *path);
78 
79 #endif /* MODULES_AFFILE_DIRECTORY_MONITOR_H_ */
80