1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
/*******************************************************************************
Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/
#include <glib.h>
#include <stdlib.h> /* calloc */
#include <stdio.h> /* printf */
#include <dirent.h> /* opendir, readdir, closedir */
#include <string.h> /* strcmp */
#include <assert.h>
#include "dep-list.h"
static gboolean kdl_debug_enabled = FALSE;
#define perror_msg if (kdl_debug_enabled) g_warning
/**
* Print a list to stdout.
*
* @param[in] dl A pointer to a list.
**/
void
dl_print (const dep_list *dl)
{
while (dl != NULL) {
printf ("%lld:%s ", (long long int) dl->inode, dl->path);
dl = dl->next;
}
printf ("\n");
}
/**
* Create a new list item.
*
* Create a new list item and initialize its fields.
*
* @param[in] path A name of a file (the string is not copied!).
* @param[in] inode A file's inode number.
* @return A pointer to a new item or NULL in the case of error.
**/
dep_list* dl_create (char *path, ino_t inode)
{
dep_list *dl = calloc (1, sizeof (dep_list));
if (dl == NULL) {
perror_msg ("Failed to create a new dep-list item");
return NULL;
}
dl->path = path;
dl->inode = inode;
return dl;
}
/**
* Create a shallow copy of a list.
*
* A shallow copy is a copy of a structure, but not the copy of the
* contents. All data pointers ('path' in our case) of a list and its
* shallow copy will point to the same memory.
*
* @param[in] dl A pointer to list to make a copy. May be NULL.
* @return A shallow copy of the list.
**/
dep_list*
dl_shallow_copy (const dep_list *dl)
{
dep_list *head;
dep_list *cp;
const dep_list *it;
if (dl == NULL) {
return NULL;
}
head = calloc (1, sizeof (dep_list));
if (head == NULL) {
perror_msg ("Failed to allocate head during shallow copy");
return NULL;
}
cp = head;
it = dl;
while (it != NULL) {
cp->path = it->path;
cp->inode = it->inode;
if (it->next) {
cp->next = calloc (1, sizeof (dep_list));
if (cp->next == NULL) {
perror_msg ("Failed to allocate a new element during shallow copy");
dl_shallow_free (head);
return NULL;
}
cp = cp->next;
}
it = it->next;
}
return head;
}
/**
* Free the memory allocated for shallow copy.
*
* This function will free the memory used by a list structure, but
* the list data will remain in the heap.
*
* @param[in] dl A pointer to a list. May be NULL.
**/
void
dl_shallow_free (dep_list *dl)
{
while (dl != NULL) {
dep_list *ptr = dl;
dl = dl->next;
free (ptr);
}
}
/**
* Free the memory allocated for a list.
*
* This function will free all the memory used by a list: both
* list structure and the list data.
*
* @param[in] dl A pointer to a list. May be NULL.
**/
void
dl_free (dep_list *dl)
{
while (dl != NULL) {
dep_list *ptr = dl;
dl = dl->next;
free (ptr->path);
free (ptr);
}
}
/**
* Create a directory listing and return it as a list.
*
* @param[in] path A path to a directory.
* @return A pointer to a list. May return NULL, check errno in this case.
**/
dep_list*
dl_listing (const char *path)
{
dep_list *head = NULL;
dep_list *prev = NULL;
DIR *dir;
assert (path != NULL);
dir = opendir (path);
if (dir != NULL) {
struct dirent *ent;
while ((ent = readdir (dir)) != NULL) {
dep_list *iter;
if (!strcmp (ent->d_name, ".") || !strcmp (ent->d_name, "..")) {
continue;
}
if (head == NULL) {
head = calloc (1, sizeof (dep_list));
if (head == NULL) {
perror_msg ("Failed to allocate head during listing");
goto error;
}
}
iter = (prev == NULL) ? head : calloc (1, sizeof (dep_list));
if (iter == NULL) {
perror_msg ("Failed to allocate a new element during listing");
goto error;
}
iter->path = strdup (ent->d_name);
if (iter->path == NULL) {
perror_msg ("Failed to copy a string during listing");
goto error;
}
iter->inode = ent->d_ino;
iter->next = NULL;
if (prev) {
prev->next = iter;
}
prev = iter;
}
closedir (dir);
}
return head;
error:
if (dir != NULL) {
closedir (dir);
}
dl_free (head);
return NULL;
}
/**
* Perform a diff on lists.
*
* This function performs something like a set intersection. The same items
* will be removed from the both lists. Items are comapred by a filename.
*
* @param[in,out] before A pointer to a pointer to a list. Will contain items
* which were not found in the 'after' list.
* @param[in,out] after A pointer to a pointer to a list. Will contain items
* which were not found in the 'before' list.
**/
void
dl_diff (dep_list **before, dep_list **after)
{
dep_list *before_iter;
dep_list *before_prev;
assert (before != NULL);
assert (after != NULL);
if (*before == NULL || *after == NULL) {
return;
}
before_iter = *before;
before_prev = NULL;
while (before_iter != NULL) {
dep_list *after_iter = *after;
dep_list *after_prev = NULL;
dep_list *oldptr;
int matched = 0;
while (after_iter != NULL) {
if (strcmp (before_iter->path, after_iter->path) == 0) {
matched = 1;
/* removing the entry from the both lists */
if (before_prev) {
before_prev->next = before_iter->next;
} else {
*before = before_iter->next;
}
if (after_prev) {
after_prev->next = after_iter->next;
} else {
*after = after_iter->next;
}
free (after_iter);
break;
}
after_prev = after_iter;
after_iter = after_iter->next;
}
oldptr = before_iter;
before_iter = before_iter->next;
if (matched == 0) {
before_prev = oldptr;
} else {
free (oldptr);
}
}
}
/**
* Traverses two lists. Compares items with a supplied expression
* and performs the passed code on a match. Removes the matched entries
* from the both lists.
**/
#define EXCLUDE_SIMILAR(removed_list, added_list, match_expr, matched_code) \
G_STMT_START { \
dep_list *removed_list##_iter; \
dep_list *removed_list##_prev; \
int productive = 0; \
\
assert (removed_list != NULL); \
assert (added_list != NULL); \
\
removed_list##_iter = *removed_list; \
removed_list##_prev = NULL; \
\
while (removed_list##_iter != NULL) { \
dep_list *added_list##_iter = *added_list; \
dep_list *added_list##_prev = NULL; \
dep_list *oldptr; \
\
int matched = 0; \
while (added_list##_iter != NULL) { \
if (match_expr) { \
matched = 1; \
++productive; \
matched_code; \
\
if (removed_list##_prev) { \
removed_list##_prev->next = removed_list##_iter->next; \
} else { \
*removed_list = removed_list##_iter->next; \
} \
if (added_list##_prev) { \
added_list##_prev->next = added_list##_iter->next; \
} else { \
*added_list = added_list##_iter->next; \
} \
free (added_list##_iter); \
break; \
} \
added_list##_iter = added_list##_iter->next; \
} \
oldptr = removed_list##_iter; \
removed_list##_iter = removed_list##_iter->next; \
if (matched == 0) { \
removed_list##_prev = oldptr; \
} else { \
free (oldptr); \
} \
} \
return (productive > 0); \
} G_STMT_END
#define cb_invoke(cbs, name, udata, ...) \
do { \
if (cbs->name) { \
(cbs->name) (udata, ## __VA_ARGS__); \
} \
} while (0)
/**
* Detect and notify about moves in the watched directory.
*
* A move is what happens when you rename a file in a directory, and
* a new name is unique, i.e. you didn't overwrite any existing files
* with this one.
*
* @param[in,out] removed A list of the removed files in the directory.
* @param[in,out] added A list of the added files of the directory.
* @param[in] cbs A pointer to #traverse_cbs, a user-defined set of
* traverse callbacks.
* @param[in] udata A pointer to the user-defined data.
* @return 0 if no files were renamed, >0 otherwise.
**/
static int
dl_detect_moves (dep_list **removed,
dep_list **added,
const traverse_cbs *cbs,
void *udata)
{
assert (cbs != NULL);
EXCLUDE_SIMILAR
(removed, added,
(removed_iter->inode == added_iter->inode),
{
cb_invoke (cbs, moved, udata,
removed_iter->path, removed_iter->inode,
added_iter->path, added_iter->inode);
});
}
/**
* Detect and notify about replacements in the watched directory.
*
* Consider you are watching a directory foo with the following files
* insinde:
*
* foo/bar
* foo/baz
*
* A replacement in a watched directory is what happens when you invoke
*
* mv /foo/bar /foo/bar
*
* i.e. when you replace a file in a watched directory with another file
* from the same directory.
*
* @param[in,out] removed A list of the removed files in the directory.
* @param[in,out] current A list with the current contents of the directory.
* @param[in] cbs A pointer to #traverse_cbs, a user-defined set of
* traverse callbacks.
* @param[in] udata A pointer to the user-defined data.
* @return 0 if no files were renamed, >0 otherwise.
**/
static int
dl_detect_replacements (dep_list **removed,
dep_list **current,
const traverse_cbs *cbs,
void *udata)
{
assert (cbs != NULL);
EXCLUDE_SIMILAR
(removed, current,
(removed_iter->inode == current_iter->inode),
{
cb_invoke (cbs, replaced, udata,
removed_iter->path, removed_iter->inode,
current_iter->path, current_iter->inode);
});
}
/**
* Detect and notify about overwrites in the watched directory.
*
* Consider you are watching a directory foo with a file inside:
*
* foo/bar
*
* And you also have a directory tmp with a file 1:
*
* tmp/1
*
* You do not watching directory tmp.
*
* An overwrite in a watched directory is what happens when you invoke
*
* mv /tmp/1 /foo/bar
*
* i.e. when you overwrite a file in a watched directory with another file
* from the another directory.
*
* @param[in,out] previous A list with the previous contents of the directory.
* @param[in,out] current A list with the current contents of the directory.
* @param[in] cbs A pointer to #traverse_cbs, a user-defined set of
* traverse callbacks.
* @param[in] udata A pointer to the user-defined data.
* @return 0 if no files were renamed, >0 otherwise.
**/
static int
dl_detect_overwrites (dep_list **previous,
dep_list **current,
const traverse_cbs *cbs,
void *udata)
{
assert (cbs != NULL);
EXCLUDE_SIMILAR
(previous, current,
(strcmp (previous_iter->path, current_iter->path) == 0
&& previous_iter->inode != current_iter->inode),
{
cb_invoke (cbs, overwritten, udata, current_iter->path, current_iter->inode);
});
}
/**
* Traverse a list and invoke a callback for each item.
*
* @param[in] list A #dep_list.
* @param[in] cb A #single_entry_cb callback function.
* @param[in] udata A pointer to the user-defined data.
**/
static void
dl_emit_single_cb_on (dep_list *list,
single_entry_cb cb,
void *udata)
{
while (cb && list != NULL) {
(cb) (udata, list->path, list->inode);
list = list->next;
}
}
/**
* Recognize all the changes in the directory, invoke the appropriate callbacks.
*
* This is the core function of directory diffing submodule.
*
* @param[in] before The previous contents of the directory.
* @param[in] after The current contents of the directory.
* @param[in] cbs A pointer to user callbacks (#traverse_callbacks).
* @param[in] udata A pointer to user data.
**/
void
dl_calculate (dep_list *before,
dep_list *after,
const traverse_cbs *cbs,
void *udata)
{
int need_update = 0;
dep_list *was = dl_shallow_copy (before);
dep_list *pre = dl_shallow_copy (before);
dep_list *now = dl_shallow_copy (after);
dep_list *lst = dl_shallow_copy (after);
assert (cbs != NULL);
dl_diff (&was, &now);
need_update += dl_detect_moves (&was, &now, cbs, udata);
need_update += dl_detect_replacements (&was, &lst, cbs, udata);
dl_detect_overwrites (&pre, &lst, cbs, udata);
if (need_update) {
cb_invoke (cbs, names_updated, udata);
}
dl_emit_single_cb_on (was, cbs->removed, udata);
dl_emit_single_cb_on (now, cbs->added, udata);
cb_invoke (cbs, many_added, udata, now);
cb_invoke (cbs, many_removed, udata, was);
dl_shallow_free (lst);
dl_shallow_free (now);
dl_shallow_free (pre);
dl_shallow_free (was);
}
|