nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /******************************************************************************* |
2 | Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk> |
||
3 | |||
4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
||
5 | of this software and associated documentation files (the "Software"), to deal |
||
6 | in the Software without restriction, including without limitation the rights |
||
7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
8 | copies of the Software, and to permit persons to whom the Software is |
||
9 | furnished to do so, subject to the following conditions: |
||
10 | |||
11 | The above copyright notice and this permission notice shall be included in |
||
12 | all copies or substantial portions of the Software. |
||
13 | |||
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||
20 | THE SOFTWARE. |
||
21 | *******************************************************************************/ |
||
22 | |||
23 | #include "config.h" |
||
24 | |||
25 | #include "gkqueuefilemonitor.h" |
||
26 | #include "kqueue-helper.h" |
||
27 | #include "kqueue-exclusions.h" |
||
28 | #include <gio/gpollfilemonitor.h> |
||
29 | #include <gio/gfile.h> |
||
30 | #include <gio/giomodule.h> |
||
31 | |||
32 | |||
33 | struct _GKqueueFileMonitor |
||
34 | { |
||
35 | GLocalFileMonitor parent_instance; |
||
36 | |||
37 | kqueue_sub *sub; |
||
38 | |||
39 | GFileMonitor *fallback; |
||
40 | GFile *fbfile; |
||
41 | }; |
||
42 | |||
43 | static gboolean g_kqueue_file_monitor_cancel (GFileMonitor* monitor); |
||
44 | |||
45 | G_DEFINE_TYPE_WITH_CODE (GKqueueFileMonitor, g_kqueue_file_monitor, G_TYPE_LOCAL_FILE_MONITOR, |
||
46 | g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME, |
||
47 | g_define_type_id, |
||
48 | "kqueue", |
||
49 | 20)) |
||
50 | |||
51 | |||
52 | static void |
||
53 | _fallback_callback (GFileMonitor *unused, |
||
54 | GFile *first, |
||
55 | GFile *second, |
||
56 | GFileMonitorEvent event, |
||
57 | gpointer udata) |
||
58 | { |
||
59 | GKqueueFileMonitor *kq_mon = G_KQUEUE_FILE_MONITOR (udata); |
||
60 | GFileMonitor *mon = G_FILE_MONITOR (kq_mon); |
||
61 | g_assert (kq_mon != NULL); |
||
62 | g_assert (mon != NULL); |
||
63 | (void) unused; |
||
64 | |||
65 | if (event == G_FILE_MONITOR_EVENT_CHANGED) |
||
66 | { |
||
67 | GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (kq_mon); |
||
68 | |||
69 | _kh_dir_diff (kq_mon->sub, local_monitor->source); |
||
70 | } |
||
71 | else |
||
72 | g_file_monitor_emit_event (mon, first, second, event); |
||
73 | } |
||
74 | |||
75 | |||
76 | static void |
||
77 | g_kqueue_file_monitor_finalize (GObject *object) |
||
78 | { |
||
79 | GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (object); |
||
80 | |||
81 | if (kqueue_monitor->sub) |
||
82 | { |
||
83 | _kh_cancel_sub (kqueue_monitor->sub); |
||
84 | _kh_sub_free (kqueue_monitor->sub); |
||
85 | kqueue_monitor->sub = NULL; |
||
86 | } |
||
87 | |||
88 | if (kqueue_monitor->fallback) |
||
89 | g_object_unref (kqueue_monitor->fallback); |
||
90 | |||
91 | if (kqueue_monitor->fbfile) |
||
92 | g_object_unref (kqueue_monitor->fbfile); |
||
93 | |||
94 | if (G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize) |
||
95 | (*G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize) (object); |
||
96 | } |
||
97 | |||
98 | static void |
||
99 | g_kqueue_file_monitor_start (GLocalFileMonitor *local_monitor, |
||
100 | const gchar *dirname, |
||
101 | const gchar *basename, |
||
102 | const gchar *filename, |
||
103 | GFileMonitorSource *source) |
||
104 | { |
||
105 | GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (local_monitor); |
||
106 | GObject *obj; |
||
107 | GKqueueFileMonitorClass *klass; |
||
108 | GObjectClass *parent_class; |
||
109 | kqueue_sub *sub = NULL; |
||
110 | gboolean ret_kh_startup = FALSE; |
||
111 | const gchar *path = NULL; |
||
112 | |||
113 | |||
114 | ret_kh_startup = _kh_startup (); |
||
115 | g_assert (ret_kh_startup); |
||
116 | |||
117 | path = filename; |
||
118 | if (!path) |
||
119 | path = dirname; |
||
120 | |||
121 | /* For a directory monitor, create a subscription object anyway. |
||
122 | * It will be used for directory diff calculation routines. |
||
123 | * Wait, directory diff in a GKqueueFileMonitor? |
||
124 | * Yes, it is. When a file monitor is started on an non-existent |
||
125 | * file, GIO uses a GKqueueFileMonitor object for that. If a directory |
||
126 | * will be created under that path, GKqueueFileMonitor will have to |
||
127 | * handle the directory notifications. */ |
||
128 | |||
129 | sub = _kh_sub_new (path, TRUE, source); |
||
130 | |||
131 | /* FIXME: what to do about errors here? we can't return NULL or another |
||
132 | * kind of error and an assertion is probably too hard (same issue as in |
||
133 | * the inotify backend) */ |
||
134 | g_assert (sub != NULL); |
||
135 | kqueue_monitor->sub = sub; |
||
136 | |||
137 | if (!_ke_is_excluded (path)) |
||
138 | _kh_add_sub (sub); |
||
139 | else |
||
140 | { |
||
141 | GFile *file = g_file_new_for_path (path); |
||
142 | kqueue_monitor->fbfile = file; |
||
143 | kqueue_monitor->fallback = _g_poll_file_monitor_new (file); |
||
144 | g_signal_connect (kqueue_monitor->fallback, |
||
145 | "changed", |
||
146 | G_CALLBACK (_fallback_callback), |
||
147 | kqueue_monitor); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | static gboolean |
||
152 | g_kqueue_file_monitor_is_supported (void) |
||
153 | { |
||
154 | return _kh_startup (); |
||
155 | } |
||
156 | |||
157 | static void |
||
158 | g_kqueue_file_monitor_class_init (GKqueueFileMonitorClass *klass) |
||
159 | { |
||
160 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
||
161 | GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass); |
||
162 | GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass); |
||
163 | |||
164 | gobject_class->finalize = g_kqueue_file_monitor_finalize; |
||
165 | file_monitor_class->cancel = g_kqueue_file_monitor_cancel; |
||
166 | |||
167 | local_file_monitor_class->is_supported = g_kqueue_file_monitor_is_supported; |
||
168 | local_file_monitor_class->start = g_kqueue_file_monitor_start; |
||
169 | local_file_monitor_class->mount_notify = TRUE; /* TODO: ??? */ |
||
170 | } |
||
171 | |||
172 | static void |
||
173 | g_kqueue_file_monitor_init (GKqueueFileMonitor *monitor) |
||
174 | { |
||
175 | } |
||
176 | |||
177 | static gboolean |
||
178 | g_kqueue_file_monitor_cancel (GFileMonitor *monitor) |
||
179 | { |
||
180 | GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (monitor); |
||
181 | |||
182 | if (kqueue_monitor->sub) |
||
183 | { |
||
184 | _kh_cancel_sub (kqueue_monitor->sub); |
||
185 | _kh_sub_free (kqueue_monitor->sub); |
||
186 | kqueue_monitor->sub = NULL; |
||
187 | } |
||
188 | else if (kqueue_monitor->fallback) |
||
189 | { |
||
190 | g_signal_handlers_disconnect_by_func (kqueue_monitor->fallback, _fallback_callback, kqueue_monitor); |
||
191 | g_file_monitor_cancel (kqueue_monitor->fallback); |
||
192 | } |
||
193 | |||
194 | if (G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel) |
||
195 | (*G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel) (monitor); |
||
196 | |||
197 | return TRUE; |
||
198 | } |