Merge branch 'issue-699' into 'master'
[glib.git] / gio / kqueue / dep-list.c
blobd8c3d8a01bf91ce53f1b8a4200c20d790791c361
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
20 THE SOFTWARE.
21 *******************************************************************************/
23 #include <glib.h>
25 #include <stdlib.h> /* calloc */
26 #include <stdio.h> /* printf */
27 #include <dirent.h> /* opendir, readdir, closedir */
28 #include <string.h> /* strcmp */
29 #include <assert.h>
31 #include "dep-list.h"
33 static gboolean kdl_debug_enabled = FALSE;
34 #define perror_msg if (kdl_debug_enabled) g_warning
37 /**
38 * Print a list to stdout.
40 * @param[in] dl A pointer to a list.
41 **/
42 void
43 dl_print (const dep_list *dl)
45 while (dl != NULL) {
46 printf ("%lld:%s ", (long long int) dl->inode, dl->path);
47 dl = dl->next;
49 printf ("\n");
52 /**
53 * Create a new list item.
55 * Create a new list item and initialize its fields.
57 * @param[in] path A name of a file (the string is not copied!).
58 * @param[in] inode A file's inode number.
59 * @return A pointer to a new item or NULL in the case of error.
60 **/
61 dep_list* dl_create (char *path, ino_t inode)
63 dep_list *dl = calloc (1, sizeof (dep_list));
64 if (dl == NULL) {
65 perror_msg ("Failed to create a new dep-list item");
66 return NULL;
69 dl->path = path;
70 dl->inode = inode;
71 return dl;
74 /**
75 * Create a shallow copy of a list.
77 * A shallow copy is a copy of a structure, but not the copy of the
78 * contents. All data pointers ('path' in our case) of a list and its
79 * shallow copy will point to the same memory.
81 * @param[in] dl A pointer to list to make a copy. May be NULL.
82 * @return A shallow copy of the list.
83 **/
84 dep_list*
85 dl_shallow_copy (const dep_list *dl)
87 dep_list *head;
88 dep_list *cp;
89 const dep_list *it;
91 if (dl == NULL) {
92 return NULL;
95 head = calloc (1, sizeof (dep_list));
96 if (head == NULL) {
97 perror_msg ("Failed to allocate head during shallow copy");
98 return NULL;
101 cp = head;
102 it = dl;
104 while (it != NULL) {
105 cp->path = it->path;
106 cp->inode = it->inode;
107 if (it->next) {
108 cp->next = calloc (1, sizeof (dep_list));
109 if (cp->next == NULL) {
110 perror_msg ("Failed to allocate a new element during shallow copy");
111 dl_shallow_free (head);
112 return NULL;
114 cp = cp->next;
116 it = it->next;
119 return head;
123 * Free the memory allocated for shallow copy.
125 * This function will free the memory used by a list structure, but
126 * the list data will remain in the heap.
128 * @param[in] dl A pointer to a list. May be NULL.
130 void
131 dl_shallow_free (dep_list *dl)
133 while (dl != NULL) {
134 dep_list *ptr = dl;
135 dl = dl->next;
136 free (ptr);
141 * Free the memory allocated for a list.
143 * This function will free all the memory used by a list: both
144 * list structure and the list data.
146 * @param[in] dl A pointer to a list. May be NULL.
148 void
149 dl_free (dep_list *dl)
151 while (dl != NULL) {
152 dep_list *ptr = dl;
153 dl = dl->next;
155 free (ptr->path);
156 free (ptr);
161 * Create a directory listing and return it as a list.
163 * @param[in] path A path to a directory.
164 * @return A pointer to a list. May return NULL, check errno in this case.
166 dep_list*
167 dl_listing (const char *path)
169 dep_list *head = NULL;
170 dep_list *prev = NULL;
171 DIR *dir;
173 assert (path != NULL);
175 dir = opendir (path);
176 if (dir != NULL) {
177 struct dirent *ent;
179 while ((ent = readdir (dir)) != NULL) {
180 dep_list *iter;
182 if (!strcmp (ent->d_name, ".") || !strcmp (ent->d_name, "..")) {
183 continue;
186 if (head == NULL) {
187 head = calloc (1, sizeof (dep_list));
188 if (head == NULL) {
189 perror_msg ("Failed to allocate head during listing");
190 goto error;
194 iter = (prev == NULL) ? head : calloc (1, sizeof (dep_list));
195 if (iter == NULL) {
196 perror_msg ("Failed to allocate a new element during listing");
197 goto error;
200 iter->path = strdup (ent->d_name);
201 if (iter->path == NULL) {
202 perror_msg ("Failed to copy a string during listing");
203 goto error;
206 iter->inode = ent->d_ino;
207 iter->next = NULL;
208 if (prev) {
209 prev->next = iter;
211 prev = iter;
214 closedir (dir);
216 return head;
218 error:
219 if (dir != NULL) {
220 closedir (dir);
222 dl_free (head);
223 return NULL;
227 * Perform a diff on lists.
229 * This function performs something like a set intersection. The same items
230 * will be removed from the both lists. Items are comapred by a filename.
232 * @param[in,out] before A pointer to a pointer to a list. Will contain items
233 * which were not found in the 'after' list.
234 * @param[in,out] after A pointer to a pointer to a list. Will containt items
235 * which were not found in the 'before' list.
237 void
238 dl_diff (dep_list **before, dep_list **after)
240 dep_list *before_iter;
241 dep_list *before_prev;
243 assert (before != NULL);
244 assert (after != NULL);
246 if (*before == NULL || *after == NULL) {
247 return;
250 before_iter = *before;
251 before_prev = NULL;
253 while (before_iter != NULL) {
254 dep_list *after_iter = *after;
255 dep_list *after_prev = NULL;
256 dep_list *oldptr;
258 int matched = 0;
259 while (after_iter != NULL) {
260 if (strcmp (before_iter->path, after_iter->path) == 0) {
261 matched = 1;
262 /* removing the entry from the both lists */
263 if (before_prev) {
264 before_prev->next = before_iter->next;
265 } else {
266 *before = before_iter->next;
269 if (after_prev) {
270 after_prev->next = after_iter->next;
271 } else {
272 *after = after_iter->next;
274 free (after_iter);
275 break;
277 after_prev = after_iter;
278 after_iter = after_iter->next;
281 oldptr = before_iter;
282 before_iter = before_iter->next;
283 if (matched == 0) {
284 before_prev = oldptr;
285 } else {
286 free (oldptr);
293 * Traverses two lists. Compares items with a supplied expression
294 * and performs the passed code on a match. Removes the matched entries
295 * from the both lists.
297 #define EXCLUDE_SIMILAR(removed_list, added_list, match_expr, matched_code) \
298 G_STMT_START { \
299 dep_list *removed_list##_iter; \
300 dep_list *removed_list##_prev; \
301 int productive = 0; \
303 assert (removed_list != NULL); \
304 assert (added_list != NULL); \
306 removed_list##_iter = *removed_list; \
307 removed_list##_prev = NULL; \
309 while (removed_list##_iter != NULL) { \
310 dep_list *added_list##_iter = *added_list; \
311 dep_list *added_list##_prev = NULL; \
312 dep_list *oldptr; \
314 int matched = 0; \
315 while (added_list##_iter != NULL) { \
316 if (match_expr) { \
317 matched = 1; \
318 ++productive; \
319 matched_code; \
321 if (removed_list##_prev) { \
322 removed_list##_prev->next = removed_list##_iter->next; \
323 } else { \
324 *removed_list = removed_list##_iter->next; \
326 if (added_list##_prev) { \
327 added_list##_prev->next = added_list##_iter->next; \
328 } else { \
329 *added_list = added_list##_iter->next; \
331 free (added_list##_iter); \
332 break; \
334 added_list##_iter = added_list##_iter->next; \
336 oldptr = removed_list##_iter; \
337 removed_list##_iter = removed_list##_iter->next; \
338 if (matched == 0) { \
339 removed_list##_prev = oldptr; \
340 } else { \
341 free (oldptr); \
344 return (productive > 0); \
345 } G_STMT_END
348 #define cb_invoke(cbs, name, udata, ...) \
349 do { \
350 if (cbs->name) { \
351 (cbs->name) (udata, ## __VA_ARGS__); \
353 } while (0)
356 * Detect and notify about moves in the watched directory.
358 * A move is what happens when you rename a file in a directory, and
359 * a new name is unique, i.e. you didnt overwrite any existing files
360 * with this one.
362 * @param[in,out] removed A list of the removed files in the directory.
363 * @param[in,out] added A list of the added files of the directory.
364 * @param[in] cbs A pointer to #traverse_cbs, an user-defined set of
365 * traverse callbacks.
366 * @param[in] udata A pointer to the user-defined data.
367 * @return 0 if no files were renamed, >0 otherwise.
369 static int
370 dl_detect_moves (dep_list **removed,
371 dep_list **added,
372 const traverse_cbs *cbs,
373 void *udata)
375 assert (cbs != NULL);
377 EXCLUDE_SIMILAR
378 (removed, added,
379 (removed_iter->inode == added_iter->inode),
381 cb_invoke (cbs, moved, udata,
382 removed_iter->path, removed_iter->inode,
383 added_iter->path, added_iter->inode);
388 * Detect and notify about replacements in the watched directory.
390 * Consider you are watching a directory foo with the folloing files
391 * insinde:
393 * foo/bar
394 * foo/baz
396 * A replacement in a watched directory is what happens when you invoke
398 * mv /foo/bar /foo/bar
400 * i.e. when you replace a file in a watched directory with another file
401 * from the same directory.
403 * @param[in,out] removed A list of the removed files in the directory.
404 * @param[in,out] current A list with the current contents of the directory.
405 * @param[in] cbs A pointer to #traverse_cbs, an user-defined set of
406 * traverse callbacks.
407 * @param[in] udata A pointer to the user-defined data.
408 * @return 0 if no files were renamed, >0 otherwise.
410 static int
411 dl_detect_replacements (dep_list **removed,
412 dep_list **current,
413 const traverse_cbs *cbs,
414 void *udata)
416 assert (cbs != NULL);
418 EXCLUDE_SIMILAR
419 (removed, current,
420 (removed_iter->inode == current_iter->inode),
422 cb_invoke (cbs, replaced, udata,
423 removed_iter->path, removed_iter->inode,
424 current_iter->path, current_iter->inode);
429 * Detect and notify about overwrites in the watched directory.
431 * Consider you are watching a directory foo with a file inside:
433 * foo/bar
435 * And you also have a directory tmp with a file 1:
437 * tmp/1
439 * You do not watching directory tmp.
441 * An overwrite in a watched directory is what happens when you invoke
443 * mv /tmp/1 /foo/bar
445 * i.e. when you overwrite a file in a watched directory with another file
446 * from the another directory.
448 * @param[in,out] previous A list with the previous contents of the directory.
449 * @param[in,out] current A list with the current contents of the directory.
450 * @param[in] cbs A pointer to #traverse_cbs, an user-defined set of
451 * traverse callbacks.
452 * @param[in] udata A pointer to the user-defined data.
453 * @return 0 if no files were renamed, >0 otherwise.
455 static int
456 dl_detect_overwrites (dep_list **previous,
457 dep_list **current,
458 const traverse_cbs *cbs,
459 void *udata)
461 assert (cbs != NULL);
463 EXCLUDE_SIMILAR
464 (previous, current,
465 (strcmp (previous_iter->path, current_iter->path) == 0
466 && previous_iter->inode != current_iter->inode),
468 cb_invoke (cbs, overwritten, udata, current_iter->path, current_iter->inode);
474 * Traverse a list and invoke a callback for each item.
476 * @param[in] list A #dep_list.
477 * @param[in] cb A #single_entry_cb callback function.
478 * @param[in] udata A pointer to the user-defined data.
480 static void
481 dl_emit_single_cb_on (dep_list *list,
482 single_entry_cb cb,
483 void *udata)
485 while (cb && list != NULL) {
486 (cb) (udata, list->path, list->inode);
487 list = list->next;
493 * Recognize all the changes in the directory, invoke the appropriate callbacks.
495 * This is the core function of directory diffing submodule.
497 * @param[in] before The previous contents of the directory.
498 * @param[in] after The current contents of the directory.
499 * @param[in] cbs A pointer to user callbacks (#traverse_callbacks).
500 * @param[in] udata A pointer to user data.
502 void
503 dl_calculate (dep_list *before,
504 dep_list *after,
505 const traverse_cbs *cbs,
506 void *udata)
508 int need_update = 0;
509 dep_list *was = dl_shallow_copy (before);
510 dep_list *pre = dl_shallow_copy (before);
511 dep_list *now = dl_shallow_copy (after);
512 dep_list *lst = dl_shallow_copy (after);
514 assert (cbs != NULL);
516 dl_diff (&was, &now);
518 need_update += dl_detect_moves (&was, &now, cbs, udata);
519 need_update += dl_detect_replacements (&was, &lst, cbs, udata);
520 dl_detect_overwrites (&pre, &lst, cbs, udata);
522 if (need_update) {
523 cb_invoke (cbs, names_updated, udata);
526 dl_emit_single_cb_on (was, cbs->removed, udata);
527 dl_emit_single_cb_on (now, cbs->added, udata);
529 cb_invoke (cbs, many_added, udata, now);
530 cb_invoke (cbs, many_removed, udata, was);
532 dl_shallow_free (lst);
533 dl_shallow_free (now);
534 dl_shallow_free (pre);
535 dl_shallow_free (was);