root/src/filemanager/treestore.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. tree_store_dirty
  2. str_common
  3. pathcmp
  4. decode
  5. tree_store_load_from
  6. encode
  7. tree_store_save_to
  8. tree_store_add_entry
  9. tree_store_notify_remove
  10. remove_entry
  11. process_special_dirs
  12. should_skip_directory
  13. tree_store_whereis
  14. tree_store_get
  15. tree_store_load
  16. tree_store_save
  17. tree_store_add_entry_remove_hook
  18. tree_store_remove_entry_remove_hook
  19. tree_store_remove_entry
  20. tree_store_mark_checked
  21. tree_store_start_check
  22. tree_store_end_check
  23. tree_store_rescan

   1 /*
   2    Tree Store
   3    Contains a storage of the file system tree representation
   4 
   5    This module has been converted to be a widget.
   6 
   7    The program load and saves the tree each time the tree widget is
   8    created and destroyed.  This is required for the future vfs layer,
   9    it will be possible to have tree views over virtual file systems.
  10 
  11    Copyright (C) 1999-2019
  12    Free Software Foundation, Inc.
  13 
  14    Written by:
  15    Janne Kukonlehto, 1994, 1996
  16    Norbert Warmuth, 1997
  17    Miguel de Icaza, 1996, 1999
  18    Slava Zanko <slavazanko@gmail.com>, 2013
  19    Andrew Borodin <aborodin@vmail.ru>, 2013
  20 
  21    This file is part of the Midnight Commander.
  22 
  23    The Midnight Commander is free software: you can redistribute it
  24    and/or modify it under the terms of the GNU General Public License as
  25    published by the Free Software Foundation, either version 3 of the License,
  26    or (at your option) any later version.
  27 
  28    The Midnight Commander is distributed in the hope that it will be useful,
  29    but WITHOUT ANY WARRANTY; without even the implied warranty of
  30    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  31    GNU General Public License for more details.
  32 
  33    You should have received a copy of the GNU General Public License
  34    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  35  */
  36 
  37 /** \file treestore.c
  38  *  \brief Source: tree store
  39  *
  40  *  Contains a storage of the file system tree representation.
  41  */
  42 
  43 #include <config.h>
  44 
  45 #include <errno.h>
  46 #include <stdio.h>
  47 #include <stdlib.h>
  48 #include <string.h>
  49 #include <sys/types.h>
  50 #include <sys/stat.h>
  51 #include <unistd.h>
  52 
  53 #include "lib/global.h"
  54 #include "lib/mcconfig.h"
  55 #include "lib/vfs/vfs.h"
  56 #include "lib/fileloc.h"
  57 #include "lib/strescape.h"
  58 #include "lib/hook.h"
  59 #include "lib/util.h"
  60 
  61 #include "src/setup.h"          /* setup_init() */
  62 
  63 #include "treestore.h"
  64 
  65 /*** global variables ****************************************************************************/
  66 
  67 /*** file scope macro definitions ****************************************************************/
  68 
  69 #define TREE_SIGNATURE "Midnight Commander TreeStore v 2.0"
  70 
  71 /*** file scope type declarations ****************************************************************/
  72 
  73 /*** file scope variables ************************************************************************/
  74 
  75 static struct TreeStore ts;
  76 
  77 static hook_t *remove_entry_hooks;
  78 
  79 /* --------------------------------------------------------------------------------------------- */
  80 /*** file scope functions ************************************************************************/
  81 /* --------------------------------------------------------------------------------------------- */
  82 
  83 static tree_entry *tree_store_add_entry (const vfs_path_t * name);
  84 
  85 /* --------------------------------------------------------------------------------------------- */
  86 
  87 static inline void
  88 tree_store_dirty (gboolean dirty)
     /* [previous][next][first][last][top][bottom][index][help]  */
  89 {
  90     ts.dirty = dirty;
  91 }
  92 
  93 /* --------------------------------------------------------------------------------------------- */
  94 /**
  95   *
  96   * @return the number of common bytes in the strings.
  97   */
  98 
  99 static size_t
 100 str_common (const vfs_path_t * s1_vpath, const vfs_path_t * s2_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 101 {
 102     size_t result = 0;
 103     const char *s1, *s2;
 104 
 105     s1 = vfs_path_as_str (s1_vpath);
 106     s2 = vfs_path_as_str (s2_vpath);
 107 
 108     while (*s1 != '\0' && *s2 != '\0' && *s1++ == *s2++)
 109         result++;
 110 
 111     return result;
 112 }
 113 
 114 /* --------------------------------------------------------------------------------------------- */
 115 /** The directory names are arranged in a single linked list in the same
 116   * order as they are displayed. When the tree is displayed the expected
 117   * order is like this:
 118   * /
 119   * /bin
 120   * /etc
 121   * /etc/X11
 122   * /etc/rc.d
 123   * /etc.old/X11
 124   * /etc.old/rc.d
 125   * /usr
 126   *
 127   * i.e. the required collating sequence when comparing two directory names is 
 128   * '\0' < PATH_SEP < all-other-characters-in-encoding-order
 129   *
 130   * Since strcmp doesn't fulfil this requirement we use pathcmp when
 131   * inserting directory names into the list. The meaning of the return value 
 132   * of pathcmp and strcmp are the same (an integer less than, equal to, or 
 133   * greater than zero if p1 is found to be less than, to match, or be greater 
 134   * than p2.
 135  */
 136 
 137 static int
 138 pathcmp (const vfs_path_t * p1_vpath, const vfs_path_t * p2_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 139 {
 140     int ret_val;
 141     const char *p1, *p2;
 142 
 143     p1 = vfs_path_as_str (p1_vpath);
 144     p2 = vfs_path_as_str (p2_vpath);
 145 
 146     for (; *p1 == *p2; p1++, p2++)
 147         if (*p1 == '\0')
 148             return 0;
 149 
 150     if (*p1 == '\0')
 151         ret_val = -1;
 152     else if (*p2 == '\0')
 153         ret_val = 1;
 154     else if (IS_PATH_SEP (*p1))
 155         ret_val = -1;
 156     else if (IS_PATH_SEP (*p2))
 157         ret_val = 1;
 158     else
 159         ret_val = (*p1 - *p2);
 160 
 161     return ret_val;
 162 }
 163 
 164 /* --------------------------------------------------------------------------------------------- */
 165 
 166 static char *
 167 decode (char *buffer)
     /* [previous][next][first][last][top][bottom][index][help]  */
 168 {
 169     char *res, *p, *q;
 170 
 171     res = g_strdup (buffer);
 172 
 173     for (p = q = res; *p != '\0'; p++, q++)
 174     {
 175         if (*p == '\n')
 176         {
 177             *q = '\0';
 178             return res;
 179         }
 180 
 181         if (*p != '\\')
 182         {
 183             *q = *p;
 184             continue;
 185         }
 186 
 187         p++;
 188 
 189         switch (*p)
 190         {
 191         case 'n':
 192             *q = '\n';
 193             break;
 194         case '\\':
 195             *q = '\\';
 196             break;
 197         default:
 198             break;
 199         }
 200     }
 201 
 202     *q = *p;
 203 
 204     return res;
 205 }
 206 
 207 /* --------------------------------------------------------------------------------------------- */
 208 /** Loads the tree store from the specified filename */
 209 
 210 static int
 211 tree_store_load_from (const char *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 212 {
 213     FILE *file;
 214     char buffer[MC_MAXPATHLEN + 20];
 215 
 216     g_return_val_if_fail (name != NULL, 0);
 217 
 218     if (ts.loaded)
 219         return 1;
 220 
 221     file = fopen (name, "r");
 222 
 223     if (file != NULL
 224         && (fgets (buffer, sizeof (buffer), file) == NULL
 225             || strncmp (buffer, TREE_SIGNATURE, strlen (TREE_SIGNATURE)) != 0))
 226     {
 227         fclose (file);
 228         file = NULL;
 229     }
 230 
 231     if (file != NULL)
 232     {
 233         char oldname[MC_MAXPATHLEN] = "\0";
 234 
 235         ts.loaded = TRUE;
 236 
 237         /* File open -> read contents */
 238         while (fgets (buffer, MC_MAXPATHLEN, file))
 239         {
 240             tree_entry *e;
 241             gboolean scanned;
 242             char *lc_name;
 243 
 244             /* Skip invalid records */
 245             if ((buffer[0] != '0' && buffer[0] != '1'))
 246                 continue;
 247 
 248             if (buffer[1] != ':')
 249                 continue;
 250 
 251             scanned = buffer[0] == '1';
 252 
 253             lc_name = decode (buffer + 2);
 254             if (!IS_PATH_SEP (lc_name[0]))
 255             {
 256                 /* Clear-text decompression */
 257                 char *s;
 258 
 259                 s = strtok (lc_name, " ");
 260                 if (s != NULL)
 261                 {
 262                     char *different;
 263                     int common;
 264 
 265                     common = atoi (s);
 266                     different = strtok (NULL, "");
 267                     if (different != NULL)
 268                     {
 269                         vfs_path_t *vpath;
 270 
 271                         vpath = vfs_path_from_str (oldname);
 272                         strcpy (oldname + common, different);
 273                         if (vfs_file_is_local (vpath))
 274                         {
 275                             vfs_path_t *tmp_vpath;
 276 
 277                             tmp_vpath = vfs_path_from_str (oldname);
 278                             e = tree_store_add_entry (tmp_vpath);
 279                             vfs_path_free (tmp_vpath);
 280                             e->scanned = scanned;
 281                         }
 282                         vfs_path_free (vpath);
 283                     }
 284                 }
 285             }
 286             else
 287             {
 288                 vfs_path_t *vpath;
 289 
 290                 vpath = vfs_path_from_str (lc_name);
 291                 if (vfs_file_is_local (vpath))
 292                 {
 293                     e = tree_store_add_entry (vpath);
 294                     e->scanned = scanned;
 295                 }
 296                 vfs_path_free (vpath);
 297                 strcpy (oldname, lc_name);
 298             }
 299             g_free (lc_name);
 300         }
 301 
 302         fclose (file);
 303     }
 304 
 305     /* Nothing loaded, we add some standard directories */
 306     if (!ts.tree_first)
 307     {
 308         vfs_path_t *tmp_vpath;
 309 
 310         tmp_vpath = vfs_path_from_str (PATH_SEP_STR);
 311         tree_store_add_entry (tmp_vpath);
 312         tree_store_rescan (tmp_vpath);
 313         vfs_path_free (tmp_vpath);
 314         ts.loaded = TRUE;
 315     }
 316 
 317     return 1;
 318 }
 319 
 320 /* --------------------------------------------------------------------------------------------- */
 321 
 322 static char *
 323 encode (const vfs_path_t * vpath, size_t offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 324 {
 325     return strutils_escape (vfs_path_as_str (vpath) + offset, -1, "\n\\", FALSE);
 326 }
 327 
 328 /* --------------------------------------------------------------------------------------------- */
 329 /** Saves the tree to the specified filename */
 330 
 331 static int
 332 tree_store_save_to (char *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 333 {
 334     tree_entry *current;
 335     FILE *file;
 336 
 337     file = fopen (name, "w");
 338     if (file == NULL)
 339         return errno;
 340 
 341     fprintf (file, "%s\n", TREE_SIGNATURE);
 342 
 343     for (current = ts.tree_first; current != NULL; current = current->next)
 344         if (vfs_file_is_local (current->name))
 345         {
 346             int i, common;
 347 
 348             /* Clear-text compression */
 349             if (current->prev != NULL
 350                 && (common = str_common (current->prev->name, current->name)) > 2)
 351             {
 352                 char *encoded;
 353 
 354                 encoded = encode (current->name, common);
 355                 i = fprintf (file, "%d:%d %s\n", current->scanned ? 1 : 0, common, encoded);
 356                 g_free (encoded);
 357             }
 358             else
 359             {
 360                 char *encoded;
 361 
 362                 encoded = encode (current->name, 0);
 363                 i = fprintf (file, "%d:%s\n", current->scanned ? 1 : 0, encoded);
 364                 g_free (encoded);
 365             }
 366 
 367             if (i == EOF)
 368             {
 369                 fprintf (stderr, _("Cannot write to the %s file:\n%s\n"),
 370                          name, unix_error_string (errno));
 371                 break;
 372             }
 373         }
 374 
 375     tree_store_dirty (FALSE);
 376     fclose (file);
 377 
 378     return 0;
 379 }
 380 
 381 /* --------------------------------------------------------------------------------------------- */
 382 
 383 static tree_entry *
 384 tree_store_add_entry (const vfs_path_t * name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 385 {
 386     int flag = -1;
 387     tree_entry *current;
 388     tree_entry *old = NULL;
 389     tree_entry *new;
 390     int submask = 0;
 391 
 392     if (ts.tree_last != NULL && ts.tree_last->next != NULL)
 393         abort ();
 394 
 395     /* Search for the correct place */
 396     for (current = ts.tree_first;
 397          current != NULL && (flag = pathcmp (current->name, name)) < 0; current = current->next)
 398         old = current;
 399 
 400     if (flag == 0)
 401         return current;         /* Already in the list */
 402 
 403     /* Not in the list -> add it */
 404     new = g_new0 (tree_entry, 1);
 405     if (current == NULL)
 406     {
 407         /* Append to the end of the list */
 408         if (ts.tree_first == NULL)
 409         {
 410             /* Empty list */
 411             ts.tree_first = new;
 412             new->prev = NULL;
 413         }
 414         else
 415         {
 416             if (old != NULL)
 417                 old->next = new;
 418             new->prev = old;
 419         }
 420         new->next = NULL;
 421         ts.tree_last = new;
 422     }
 423     else
 424     {
 425         /* Insert in to the middle of the list */
 426         new->prev = old;
 427         if (old != NULL)
 428         {
 429             /* Yes, in the middle */
 430             new->next = old->next;
 431             old->next = new;
 432         }
 433         else
 434         {
 435             /* Nope, in the beginning of the list */
 436             new->next = ts.tree_first;
 437             ts.tree_first = new;
 438         }
 439         new->next->prev = new;
 440     }
 441 
 442     /* Calculate attributes */
 443     new->name = vfs_path_clone (name);
 444     new->sublevel = vfs_path_tokens_count (new->name);
 445 
 446     {
 447         const char *new_name;
 448 
 449         new_name = vfs_path_get_last_path_str (new->name);
 450         new->subname = strrchr (new_name, PATH_SEP);
 451         if (new->subname == NULL)
 452             new->subname = new_name;
 453         else
 454             new->subname++;
 455     }
 456 
 457     if (new->next != NULL)
 458         submask = new->next->submask;
 459 
 460     submask |= 1 << new->sublevel;
 461     submask &= (2 << new->sublevel) - 1;
 462     new->submask = submask;
 463     new->mark = FALSE;
 464 
 465     /* Correct the submasks of the previous entries */
 466     for (current = new->prev;
 467          current != NULL && current->sublevel > new->sublevel; current = current->prev)
 468         current->submask |= 1 << new->sublevel;
 469 
 470     tree_store_dirty (TRUE);
 471     return new;
 472 }
 473 
 474 /* --------------------------------------------------------------------------------------------- */
 475 
 476 static void
 477 tree_store_notify_remove (tree_entry * entry)
     /* [previous][next][first][last][top][bottom][index][help]  */
 478 {
 479     hook_t *p;
 480 
 481     for (p = remove_entry_hooks; p != NULL; p = p->next)
 482     {
 483         tree_store_remove_fn r = (tree_store_remove_fn) p->hook_fn;
 484 
 485         r (entry, p->hook_data);
 486     }
 487 }
 488 
 489 /* --------------------------------------------------------------------------------------------- */
 490 
 491 static tree_entry *
 492 remove_entry (tree_entry * entry)
     /* [previous][next][first][last][top][bottom][index][help]  */
 493 {
 494     tree_entry *current = entry->prev;
 495     long submask = 0;
 496     tree_entry *ret = NULL;
 497 
 498     tree_store_notify_remove (entry);
 499 
 500     /* Correct the submasks of the previous entries */
 501     if (entry->next != NULL)
 502         submask = entry->next->submask;
 503 
 504     for (; current != NULL && current->sublevel > entry->sublevel; current = current->prev)
 505     {
 506         submask |= 1 << current->sublevel;
 507         submask &= (2 << current->sublevel) - 1;
 508         current->submask = submask;
 509     }
 510 
 511     /* Unlink the entry from the list */
 512     if (entry->prev != NULL)
 513         entry->prev->next = entry->next;
 514     else
 515         ts.tree_first = entry->next;
 516 
 517     if (entry->next != NULL)
 518         entry->next->prev = entry->prev;
 519     else
 520         ts.tree_last = entry->prev;
 521 
 522     /* Free the memory used by the entry */
 523     vfs_path_free (entry->name);
 524     g_free (entry);
 525 
 526     return ret;
 527 }
 528 
 529 /* --------------------------------------------------------------------------------------------- */
 530 
 531 static void
 532 process_special_dirs (GList ** special_dirs, const char *file)
     /* [previous][next][first][last][top][bottom][index][help]  */
 533 {
 534     gchar **start_buff;
 535     mc_config_t *cfg;
 536 
 537     cfg = mc_config_init (file, TRUE);
 538     if (cfg == NULL)
 539         return;
 540 
 541     start_buff = mc_config_get_string_list (cfg, "Special dirs", "list", NULL);
 542     if (start_buff != NULL)
 543     {
 544         gchar **buffers;
 545 
 546         for (buffers = start_buff; *buffers != NULL; buffers++)
 547         {
 548             *special_dirs = g_list_prepend (*special_dirs, *buffers);
 549             *buffers = NULL;
 550         }
 551 
 552         g_strfreev (start_buff);
 553     }
 554     mc_config_deinit (cfg);
 555 }
 556 
 557 /* --------------------------------------------------------------------------------------------- */
 558 
 559 static gboolean
 560 should_skip_directory (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 561 {
 562     static GList *special_dirs = NULL;
 563     GList *l;
 564     static gboolean loaded = FALSE;
 565     gboolean ret = FALSE;
 566 
 567     if (!loaded)
 568     {
 569         const char *profile_name;
 570 
 571         profile_name = setup_init ();
 572         process_special_dirs (&special_dirs, profile_name);
 573         process_special_dirs (&special_dirs, global_profile_name);
 574 
 575         loaded = TRUE;
 576     }
 577 
 578     for (l = special_dirs; l != NULL; l = g_list_next (l))
 579         if (strncmp (vfs_path_as_str (vpath), l->data, strlen (l->data)) == 0)
 580         {
 581             ret = TRUE;
 582             break;
 583         }
 584 
 585     return ret;
 586 }
 587 
 588 /* --------------------------------------------------------------------------------------------- */
 589 /*** public functions ****************************************************************************/
 590 /* --------------------------------------------------------------------------------------------- */
 591 
 592 /* Searches for specified directory */
 593 tree_entry *
 594 tree_store_whereis (const vfs_path_t * name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 595 {
 596     tree_entry *current;
 597     int flag = -1;
 598 
 599     for (current = ts.tree_first;
 600          current != NULL && (flag = pathcmp (current->name, name)) < 0; current = current->next)
 601         ;
 602 
 603     return flag == 0 ? current : NULL;
 604 }
 605 
 606 /* --------------------------------------------------------------------------------------------- */
 607 
 608 struct TreeStore *
 609 tree_store_get (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 610 {
 611     return &ts;
 612 }
 613 
 614 /* --------------------------------------------------------------------------------------------- */
 615 /**
 616  * \fn int tree_store_load(void)
 617  * \brief Loads the tree from the default location
 618  * \return 1 if success (true), 0 otherwise (false)
 619  */
 620 
 621 int
 622 tree_store_load (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 623 {
 624     char *name;
 625     int retval;
 626 
 627     name = mc_config_get_full_path (MC_TREESTORE_FILE);
 628     retval = tree_store_load_from (name);
 629     g_free (name);
 630 
 631     return retval;
 632 }
 633 
 634 /* --------------------------------------------------------------------------------------------- */
 635 /**
 636  * \fn int tree_store_save(void)
 637  * \brief Saves the tree to the default file in an atomic fashion
 638  * \return 0 if success, errno on error
 639  */
 640 
 641 int
 642 tree_store_save (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 643 {
 644     char *name;
 645     int retval;
 646 
 647     name = mc_config_get_full_path (MC_TREESTORE_FILE);
 648     mc_util_make_backup_if_possible (name, ".tmp");
 649 
 650     retval = tree_store_save_to (name);
 651     if (retval != 0)
 652         mc_util_restore_from_backup_if_possible (name, ".tmp");
 653     else
 654         mc_util_unlink_backup_if_possible (name, ".tmp");
 655 
 656     g_free (name);
 657     return retval;
 658 }
 659 
 660 /* --------------------------------------------------------------------------------------------- */
 661 
 662 void
 663 tree_store_add_entry_remove_hook (tree_store_remove_fn callback, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 664 {
 665     add_hook (&remove_entry_hooks, (void (*)(void *)) callback, data);
 666 }
 667 
 668 /* --------------------------------------------------------------------------------------------- */
 669 
 670 void
 671 tree_store_remove_entry_remove_hook (tree_store_remove_fn callback)
     /* [previous][next][first][last][top][bottom][index][help]  */
 672 {
 673     delete_hook (&remove_entry_hooks, (void (*)(void *)) callback);
 674 }
 675 
 676 /* --------------------------------------------------------------------------------------------- */
 677 
 678 void
 679 tree_store_remove_entry (const vfs_path_t * name_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 680 {
 681     tree_entry *current, *base;
 682     size_t len;
 683 
 684     g_return_if_fail (name_vpath != NULL);
 685 
 686     /* Miguel Ugly hack */
 687     {
 688         gboolean is_root;
 689         const char *name_vpath_str;
 690 
 691         name_vpath_str = vfs_path_as_str (name_vpath);
 692         is_root = (IS_PATH_SEP (name_vpath_str[0]) && name_vpath_str[1] == '\0');
 693         if (is_root)
 694             return;
 695     }
 696     /* Miguel Ugly hack end */
 697 
 698     base = tree_store_whereis (name_vpath);
 699     if (base == NULL)
 700         return;                 /* Doesn't exist */
 701 
 702     len = vfs_path_len (base->name);
 703     current = base->next;
 704     while (current != NULL && vfs_path_equal_len (current->name, base->name, len))
 705     {
 706         gboolean ok;
 707         tree_entry *old;
 708         const char *cname;
 709 
 710         cname = vfs_path_as_str (current->name);
 711         ok = (cname[len] == '\0' || IS_PATH_SEP (cname[len]));
 712         if (!ok)
 713             break;
 714 
 715         old = current;
 716         current = current->next;
 717         remove_entry (old);
 718     }
 719     remove_entry (base);
 720     tree_store_dirty (TRUE);
 721 }
 722 
 723 /* --------------------------------------------------------------------------------------------- */
 724 /** This subdirectory exists -> clear deletion mark */
 725 
 726 void
 727 tree_store_mark_checked (const char *subname)
     /* [previous][next][first][last][top][bottom][index][help]  */
 728 {
 729     vfs_path_t *name;
 730     tree_entry *current, *base;
 731     int flag = 1;
 732     const char *cname;
 733 
 734     if (!ts.loaded)
 735         return;
 736 
 737     if (ts.check_name == NULL)
 738         return;
 739 
 740     /* Calculate the full name of the subdirectory */
 741     if (DIR_IS_DOT (subname) || DIR_IS_DOTDOT (subname))
 742         return;
 743 
 744     cname = vfs_path_as_str (ts.check_name);
 745     if (IS_PATH_SEP (cname[0]) && cname[1] == '\0')
 746         name = vfs_path_build_filename (PATH_SEP_STR, subname, (char *) NULL);
 747     else
 748         name = vfs_path_append_new (ts.check_name, subname, (char *) NULL);
 749 
 750     /* Search for the subdirectory */
 751     for (current = ts.check_start;
 752          current != NULL && (flag = pathcmp (current->name, name)) < 0; current = current->next)
 753         ;
 754 
 755     if (flag != 0)
 756     {
 757         /* Doesn't exist -> add it */
 758         current = tree_store_add_entry (name);
 759         ts.add_queue_vpath = g_list_prepend (ts.add_queue_vpath, name);
 760     }
 761     else
 762         vfs_path_free (name);
 763 
 764     /* Clear the deletion mark from the subdirectory and its children */
 765     base = current;
 766     if (base != NULL)
 767     {
 768         size_t len;
 769 
 770         len = vfs_path_len (base->name);
 771         base->mark = FALSE;
 772         for (current = base->next;
 773              current != NULL && vfs_path_equal_len (current->name, base->name, len);
 774              current = current->next)
 775         {
 776             gboolean ok;
 777 
 778             cname = vfs_path_as_str (current->name);
 779             ok = (cname[len] == '\0' || IS_PATH_SEP (cname[len]) || len == 1);
 780             if (!ok)
 781                 break;
 782 
 783             current->mark = FALSE;
 784         }
 785     }
 786 }
 787 
 788 /* --------------------------------------------------------------------------------------------- */
 789 /** Mark the subdirectories of the current directory for delete */
 790 
 791 tree_entry *
 792 tree_store_start_check (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 793 {
 794     tree_entry *current, *retval;
 795     size_t len;
 796 
 797     if (!ts.loaded)
 798         return NULL;
 799 
 800     g_return_val_if_fail (ts.check_name == NULL, NULL);
 801     ts.check_start = NULL;
 802 
 803     /* Search for the start of subdirectories */
 804     current = tree_store_whereis (vpath);
 805     if (current == NULL)
 806     {
 807         struct stat s;
 808 
 809         if (mc_stat (vpath, &s) == -1 || !S_ISDIR (s.st_mode))
 810             return NULL;
 811 
 812         current = tree_store_add_entry (vpath);
 813         ts.check_name = vfs_path_clone (vpath);
 814 
 815         return current;
 816     }
 817 
 818     ts.check_name = vfs_path_clone (vpath);
 819 
 820     retval = current;
 821 
 822     /* Mark old subdirectories for delete */
 823     ts.check_start = current->next;
 824     len = vfs_path_len (ts.check_name);
 825 
 826     for (current = ts.check_start;
 827          current != NULL && vfs_path_equal_len (current->name, ts.check_name, len);
 828          current = current->next)
 829     {
 830         gboolean ok;
 831         const char *cname;
 832 
 833         cname = vfs_path_as_str (current->name);
 834         ok = (cname[len] == '\0' || IS_PATH_SEP (cname[len]) || len == 1);
 835         if (!ok)
 836             break;
 837 
 838         current->mark = TRUE;
 839     }
 840 
 841     return retval;
 842 }
 843 
 844 /* --------------------------------------------------------------------------------------------- */
 845 /** Delete subdirectories which still have the deletion mark */
 846 
 847 void
 848 tree_store_end_check (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 849 {
 850     tree_entry *current;
 851     size_t len;
 852     GList *the_queue;
 853 
 854     if (!ts.loaded)
 855         return;
 856 
 857     g_return_if_fail (ts.check_name != NULL);
 858 
 859     /* Check delete marks and delete if found */
 860     len = vfs_path_len (ts.check_name);
 861 
 862     current = ts.check_start;
 863     while (current != NULL && vfs_path_equal_len (current->name, ts.check_name, len))
 864     {
 865         gboolean ok;
 866         tree_entry *old;
 867         const char *cname;
 868 
 869         cname = vfs_path_as_str (current->name);
 870         ok = (cname[len] == '\0' || IS_PATH_SEP (cname[len]) || len == 1);
 871         if (!ok)
 872             break;
 873 
 874         old = current;
 875         current = current->next;
 876         if (old->mark)
 877             remove_entry (old);
 878     }
 879 
 880     /* get the stuff in the scan order */
 881     the_queue = g_list_reverse (ts.add_queue_vpath);
 882     ts.add_queue_vpath = NULL;
 883     vfs_path_free (ts.check_name);
 884     ts.check_name = NULL;
 885 
 886     g_list_free_full (the_queue, (GDestroyNotify) vfs_path_free);
 887 }
 888 
 889 /* --------------------------------------------------------------------------------------------- */
 890 
 891 tree_entry *
 892 tree_store_rescan (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 893 {
 894     DIR *dirp;
 895     struct stat buf;
 896     tree_entry *entry;
 897 
 898     if (should_skip_directory (vpath))
 899     {
 900         entry = tree_store_add_entry (vpath);
 901         entry->scanned = TRUE;
 902         return entry;
 903     }
 904 
 905     entry = tree_store_start_check (vpath);
 906     if (entry == NULL)
 907         return NULL;
 908 
 909     dirp = mc_opendir (vpath);
 910     if (dirp != NULL)
 911     {
 912         struct dirent *dp;
 913 
 914         for (dp = mc_readdir (dirp); dp != NULL; dp = mc_readdir (dirp))
 915             if (!DIR_IS_DOT (dp->d_name) && !DIR_IS_DOTDOT (dp->d_name))
 916             {
 917                 vfs_path_t *tmp_vpath;
 918 
 919                 tmp_vpath = vfs_path_append_new (vpath, dp->d_name, (char *) NULL);
 920                 if (mc_lstat (tmp_vpath, &buf) != -1 && S_ISDIR (buf.st_mode))
 921                     tree_store_mark_checked (dp->d_name);
 922                 vfs_path_free (tmp_vpath);
 923             }
 924 
 925         mc_closedir (dirp);
 926     }
 927     tree_store_end_check ();
 928     entry->scanned = TRUE;
 929 
 930     return entry;
 931 }
 932 
 933 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */