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 *******************************************************************************/
25 #include <stdlib.h> /* calloc */
26 #include <stdio.h> /* printf */
27 #include <dirent.h> /* opendir, readdir, closedir */
28 #include <string.h> /* strcmp */
33 static gboolean kdl_debug_enabled
= FALSE
;
34 #define perror_msg if (kdl_debug_enabled) g_warning
38 * Print a list to stdout.
40 * @param[in] dl A pointer to a list.
43 dl_print (const dep_list
*dl
)
46 printf ("%lld:%s ", (long long int) dl
->inode
, dl
->path
);
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.
61 dep_list
* dl_create (char *path
, ino_t inode
)
63 dep_list
*dl
= calloc (1, sizeof (dep_list
));
65 perror_msg ("Failed to create a new dep-list item");
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.
85 dl_shallow_copy (const dep_list
*dl
)
95 head
= calloc (1, sizeof (dep_list
));
97 perror_msg ("Failed to allocate head during shallow copy");
106 cp
->inode
= it
->inode
;
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
);
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.
131 dl_shallow_free (dep_list
*dl
)
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.
149 dl_free (dep_list
*dl
)
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.
167 dl_listing (const char *path
)
169 dep_list
*head
= NULL
;
170 dep_list
*prev
= NULL
;
173 assert (path
!= NULL
);
175 dir
= opendir (path
);
179 while ((ent
= readdir (dir
)) != NULL
) {
182 if (!strcmp (ent
->d_name
, ".") || !strcmp (ent
->d_name
, "..")) {
187 head
= calloc (1, sizeof (dep_list
));
189 perror_msg ("Failed to allocate head during listing");
194 iter
= (prev
== NULL
) ? head
: calloc (1, sizeof (dep_list
));
196 perror_msg ("Failed to allocate a new element during listing");
200 iter
->path
= strdup (ent
->d_name
);
201 if (iter
->path
== NULL
) {
202 perror_msg ("Failed to copy a string during listing");
206 iter
->inode
= ent
->d_ino
;
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.
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
) {
250 before_iter
= *before
;
253 while (before_iter
!= NULL
) {
254 dep_list
*after_iter
= *after
;
255 dep_list
*after_prev
= NULL
;
259 while (after_iter
!= NULL
) {
260 if (strcmp (before_iter
->path
, after_iter
->path
) == 0) {
262 /* removing the entry from the both lists */
264 before_prev
->next
= before_iter
->next
;
266 *before
= before_iter
->next
;
270 after_prev
->next
= after_iter
->next
;
272 *after
= after_iter
->next
;
277 after_prev
= after_iter
;
278 after_iter
= after_iter
->next
;
281 oldptr
= before_iter
;
282 before_iter
= before_iter
->next
;
284 before_prev
= 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) \
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; \
315 while (added_list##_iter != NULL) { \
321 if (removed_list##_prev) { \
322 removed_list##_prev->next = removed_list##_iter->next; \
324 *removed_list = removed_list##_iter->next; \
326 if (added_list##_prev) { \
327 added_list##_prev->next = added_list##_iter->next; \
329 *added_list = added_list##_iter->next; \
331 free (added_list##_iter); \
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; \
344 return (productive > 0); \
348 #define cb_invoke(cbs, name, udata, ...) \
351 (cbs->name) (udata, ## __VA_ARGS__); \
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
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.
370 dl_detect_moves (dep_list
**removed
,
372 const traverse_cbs
*cbs
,
375 assert (cbs
!= NULL
);
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
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.
411 dl_detect_replacements (dep_list
**removed
,
413 const traverse_cbs
*cbs
,
416 assert (cbs
!= NULL
);
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:
435 * And you also have a directory tmp with a file 1:
439 * You do not watching directory tmp.
441 * An overwrite in a watched directory is what happens when you invoke
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.
456 dl_detect_overwrites (dep_list
**previous
,
458 const traverse_cbs
*cbs
,
461 assert (cbs
!= NULL
);
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.
481 dl_emit_single_cb_on (dep_list
*list
,
485 while (cb
&& list
!= NULL
) {
486 (cb
) (udata
, list
->path
, list
->inode
);
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.
503 dl_calculate (dep_list
*before
,
505 const traverse_cbs
*cbs
,
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
);
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
);