1 /*******************************************************************************
2 Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
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:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
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
21 *******************************************************************************/
24 #include <sys/types.h>
25 #include <sys/event.h>
27 #include <sys/socket.h>
29 #include <gio/glocalfile.h>
30 #include <gio/glocalfilemonitor.h>
31 #include <gio/gfile.h>
37 #include "kqueue-helper.h"
41 GFileMonitorSource
*source
;
42 gboolean handle_deleted
;
47 * @udata: a pointer to user data (#handle_context).
48 * @path: file name of a new file.
49 * @inode: inode number of a new file.
51 * A callback function for the directory diff calculation routine,
52 * produces G_FILE_MONITOR_EVENT_CREATED event for a created file.
55 handle_created (void *udata
, const char *path
, ino_t inode
)
57 handle_ctx
*ctx
= NULL
;
63 ctx
= (handle_ctx
*) udata
;
64 g_assert (udata
!= NULL
);
65 g_assert (ctx
->sub
!= NULL
);
66 g_assert (ctx
->source
!= NULL
);
68 now
= g_get_monotonic_time ();
69 g_file_monitor_source_handle_event (ctx
->source
, G_FILE_MONITOR_EVENT_CREATED
, path
,
72 /* Copied from ih_event_callback to report 'CHANGES_DONE_HINT' earlier. */
73 fullname
= g_build_filename (ctx
->sub
->filename
, path
, NULL
);
74 if (stat (fullname
, &st
) != 0 || !S_ISREG (st
.st_mode
) || st
.st_nlink
!= 1)
75 g_file_monitor_source_handle_event (ctx
->source
, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT
, path
,
82 * @udata: a pointer to user data (#handle_context).
83 * @path: file name of the removed file.
84 * @inode: inode number of the removed file.
86 * A callback function for the directory diff calculation routine,
87 * produces G_FILE_MONITOR_EVENT_DELETED event for a deleted file.
90 handle_deleted (void *udata
, const char *path
, ino_t inode
)
92 handle_ctx
*ctx
= NULL
;
95 ctx
= (handle_ctx
*) udata
;
96 g_assert (udata
!= NULL
);
97 g_assert (ctx
->sub
!= NULL
);
98 g_assert (ctx
->source
!= NULL
);
100 if (!ctx
->handle_deleted
)
103 g_file_monitor_source_handle_event (ctx
->source
, G_FILE_MONITOR_EVENT_DELETED
, path
,
104 NULL
, NULL
, g_get_monotonic_time ());
109 * @udata: a pointer to user data (#handle_context).
110 * @from_path: file name of the source file.
111 * @from_inode: inode number of the source file.
112 * @to_path: file name of the replaced file.
113 * @to_inode: inode number of the replaced file.
115 * A callback function for the directory diff calculation routine,
116 * produces G_FILE_MONITOR_EVENT_RENAMED event on a move.
119 handle_moved (void *udata
,
120 const char *from_path
,
125 handle_ctx
*ctx
= NULL
;
130 ctx
= (handle_ctx
*) udata
;
131 g_assert (udata
!= NULL
);
132 g_assert (ctx
->sub
!= NULL
);
133 g_assert (ctx
->source
!= NULL
);
135 g_file_monitor_source_handle_event (ctx
->source
, G_FILE_MONITOR_EVENT_RENAMED
,
136 from_path
, to_path
, NULL
, g_get_monotonic_time ());
140 * handle_overwritten:
141 * @data: a pointer to user data (#handle_context).
142 * @path: file name of the overwritten file.
143 * @node: inode number of the overwritten file.
145 * A callback function for the directory diff calculation routine,
146 * produces G_FILE_MONITOR_EVENT_DELETED/CREATED event pair when
147 * an overwrite occurs in the directory (see dep-list for details).
150 handle_overwritten (void *udata
, const char *path
, ino_t inode
)
152 handle_ctx
*ctx
= NULL
;
155 ctx
= (handle_ctx
*) udata
;
156 g_assert (udata
!= NULL
);
157 g_assert (ctx
->sub
!= NULL
);
158 g_assert (ctx
->source
!= NULL
);
160 g_file_monitor_source_handle_event (ctx
->source
, G_FILE_MONITOR_EVENT_DELETED
,
161 path
, NULL
, NULL
, g_get_monotonic_time ());
163 g_file_monitor_source_handle_event (ctx
->source
, G_FILE_MONITOR_EVENT_CREATED
,
164 path
, NULL
, NULL
, g_get_monotonic_time ());
167 static const traverse_cbs cbs
= {
173 NULL
, /* many added */
174 NULL
, /* many removed */
175 NULL
, /* names updated */
180 _kh_dir_diff (kqueue_sub
*sub
, gboolean handle_deleted
)
185 memset (&ctx
, 0, sizeof (handle_ctx
));
187 ctx
.source
= sub
->source
;
188 ctx
.handle_deleted
= handle_deleted
;
191 sub
->deps
= dl_listing (sub
->filename
);
193 dl_calculate (was
, sub
->deps
, &cbs
, &ctx
);