root/lib/vfs/path.c

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

DEFINITIONS

This source file includes following definitions.
  1. path_magic
  2. _vfs_split_with_semi_skip_count
  3. vfs_canon
  4. vfs_get_encoding
  5. vfs_path_url_split
  6. vfs_get_class_by_name
  7. vfs_path_is_str_path_deprecated
  8. vfs_path_from_str_deprecated_parser
  9. vfs_path_from_str_uri_parser
  10. vfs_path_tokens_add_class_info
  11. vfs_path_strip_home
  12. vfs_path_to_str_flags
  13. vfs_path_to_str_elements_count
  14. vfs_path_from_str_flags
  15. vfs_path_from_str
  16. vfs_path_new
  17. vfs_path_elements_count
  18. vfs_path_add_element
  19. vfs_path_get_by_index
  20. vfs_path_element_clone
  21. vfs_path_element_free
  22. vfs_path_clone
  23. vfs_path_free
  24. vfs_path_remove_element_by_index
  25. vfs_prefix_to_class
  26. vfs_path_element_need_cleanup_converter
  27. vfs_path_change_encoding
  28. vfs_path_serialize
  29. vfs_path_deserialize
  30. vfs_path_build_filename
  31. vfs_path_append_new
  32. vfs_path_append_vpath_new
  33. vfs_path_tokens_count
  34. vfs_path_tokens_get
  35. vfs_path_vtokens_get
  36. vfs_path_build_url_params_str
  37. vfs_path_element_build_pretty_path_str
  38. vfs_path_equal
  39. vfs_path_equal_len
  40. vfs_path_len
  41. vfs_path_to_absolute

   1 /*
   2    Virtual File System path handlers
   3 
   4    Copyright (C) 2011-2022
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
   9    Andrew Borodin <aborodin@vmail.ru>, 2013-2022
  10 
  11    This file is part of the Midnight Commander.
  12 
  13    The Midnight Commander is free software: you can redistribute it
  14    and/or modify it under the terms of the GNU General Public License as
  15    published by the Free Software Foundation, either version 3 of the License,
  16    or (at your option) any later version.
  17 
  18    The Midnight Commander is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21    GNU General Public License for more details.
  22 
  23    You should have received a copy of the GNU General Public License
  24    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25  */
  26 
  27 /**
  28  * \file
  29  * \brief Source: Virtual File System: path handlers
  30  * \author Slava Zanko
  31  * \date 2011
  32  */
  33 
  34 
  35 #include <config.h>
  36 
  37 #include <errno.h>
  38 
  39 #include "lib/global.h"
  40 #include "lib/strutil.h"
  41 #include "lib/util.h"           /* mc_build_filename() */
  42 #include "lib/serialize.h"
  43 
  44 #include "vfs.h"
  45 #include "utilvfs.h"
  46 #include "xdirentry.h"
  47 #include "path.h"
  48 
  49 extern GPtrArray *vfs__classes_list;
  50 
  51 /*** global variables ****************************************************************************/
  52 
  53 /*** file scope macro definitions ****************************************************************/
  54 
  55 /*** file scope type declarations ****************************************************************/
  56 
  57 /*** file scope variables ************************************************************************/
  58 
  59 /*** file scope functions ************************************************************************/
  60 /* --------------------------------------------------------------------------------------------- */
  61 
  62 static gboolean
  63 path_magic (const char *path)
     /* [previous][next][first][last][top][bottom][index][help]  */
  64 {
  65     struct stat buf;
  66 
  67     return (stat (path, &buf) != 0);
  68 }
  69 
  70 /* --------------------------------------------------------------------------------------------- */
  71 
  72 /**
  73  * Splits path extracting vfs part.
  74  *
  75  * Splits path
  76  * \verbatim /p1#op/inpath \endverbatim
  77  * into
  78  * \verbatim inpath,op; \endverbatim
  79  * returns which vfs it is.
  80  * What is left in path is p1. You still want to g_free(path), you DON'T
  81  * want to free neither *inpath nor *op
  82  */
  83 
  84 static struct vfs_class *
  85 _vfs_split_with_semi_skip_count (char *path, const char **inpath, const char **op,
     /* [previous][next][first][last][top][bottom][index][help]  */
  86                                  size_t skip_count)
  87 {
  88     char *semi;
  89     char *slash;
  90     struct vfs_class *ret;
  91 
  92     if (path == NULL)
  93         vfs_die ("Cannot split NULL");
  94 
  95     semi = strrstr_skip_count (path, "#", skip_count);
  96 
  97     if ((semi == NULL) || (!path_magic (path)))
  98         return NULL;
  99 
 100     slash = strchr (semi, PATH_SEP);
 101     *semi = '\0';
 102 
 103     if (op != NULL)
 104         *op = NULL;
 105 
 106     if (inpath != NULL)
 107         *inpath = NULL;
 108 
 109     if (slash != NULL)
 110         *slash = '\0';
 111 
 112     ret = vfs_prefix_to_class (semi + 1);
 113     if (ret != NULL)
 114     {
 115         if (op != NULL)
 116             *op = semi + 1;
 117         if (inpath != NULL)
 118             *inpath = slash != NULL ? slash + 1 : NULL;
 119         return ret;
 120     }
 121 
 122     if (slash != NULL)
 123         *slash = PATH_SEP;
 124 
 125     *semi = '#';
 126     ret = _vfs_split_with_semi_skip_count (path, inpath, op, skip_count + 1);
 127     return ret;
 128 }
 129 
 130 /* --------------------------------------------------------------------------------------------- */
 131 /**
 132  * remove //, /./ and /../
 133  *
 134  * @return newly allocated string
 135  */
 136 
 137 static char *
 138 vfs_canon (const char *path)
     /* [previous][next][first][last][top][bottom][index][help]  */
 139 {
 140     char *result;
 141 
 142     if (path == NULL)
 143         vfs_die ("Cannot canonicalize NULL");
 144 
 145     if (!IS_PATH_SEP (*path))
 146     {
 147         /* Relative to current directory */
 148 
 149         char *local;
 150 
 151         if (g_str_has_prefix (path, VFS_ENCODING_PREFIX))
 152         {
 153             /*
 154                encoding prefix placed at start of string without the leading slash
 155                should be autofixed by adding the leading slash
 156              */
 157             local = mc_build_filename (PATH_SEP_STR, path, (char *) NULL);
 158         }
 159         else
 160         {
 161             const char *curr_dir;
 162 
 163             curr_dir = vfs_get_current_dir ();
 164             local = mc_build_filename (curr_dir, path, (char *) NULL);
 165         }
 166         result = vfs_canon (local);
 167         g_free (local);
 168     }
 169     else
 170     {
 171         /* Absolute path */
 172 
 173         result = g_strdup (path);
 174         canonicalize_pathname (result);
 175     }
 176 
 177     return result;
 178 }
 179 
 180 /* --------------------------------------------------------------------------------------------- */
 181 
 182 #ifdef HAVE_CHARSET
 183 /** get encoding after last #enc: or NULL, if part does not contain #enc:
 184  *
 185  * @param path null-terminated string
 186  * @param len the maximum length of path, where #enc: should be searched
 187  *
 188  * @return newly allocated string.
 189  */
 190 
 191 static char *
 192 vfs_get_encoding (const char *path, ssize_t len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 193 {
 194     char *semi;
 195 
 196     /* try found #enc: */
 197     semi = g_strrstr_len (path, len, VFS_ENCODING_PREFIX);
 198     if (semi == NULL)
 199         return NULL;
 200 
 201     if (semi == path || IS_PATH_SEP (semi[-1]))
 202     {
 203         char *slash;
 204 
 205         semi += strlen (VFS_ENCODING_PREFIX);   /* skip "#enc:" */
 206         slash = strchr (semi, PATH_SEP);
 207         if (slash != NULL)
 208             return g_strndup (semi, slash - semi);
 209         return g_strdup (semi);
 210     }
 211 
 212     return vfs_get_encoding (path, semi - path);
 213 }
 214 #endif
 215 
 216 /* --------------------------------------------------------------------------------------------- */
 217 /**  Extract the hostname and username from the path
 218  *
 219  * Format of the path is [user@]hostname:port/remote-dir, e.g.:
 220  *
 221  * ftp://sunsite.unc.edu/pub/linux
 222  * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
 223  * ftp://tsx-11.mit.edu:8192/
 224  * ftp://joe@foo.edu:11321/private
 225  * ftp://joe:password@foo.se
 226  *
 227  * @param path_element is an input string to be parsed
 228  * @param path is an input string to be parsed
 229  *
 230  * @return g_malloc()ed url info.
 231  *         If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
 232  *         is not set, then the current login name is supplied.
 233  *         Return value is a g_malloc()ed structure with the pathname relative to the
 234  *         host.
 235  */
 236 
 237 static void
 238 vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
     /* [previous][next][first][last][top][bottom][index][help]  */
 239 {
 240     char *pcopy;
 241     char *colon, *at, *rest;
 242 
 243     path_element->port = 0;
 244 
 245     pcopy = g_strdup (path);
 246 
 247     /* search for any possible user */
 248     at = strrchr (pcopy, '@');
 249 
 250     /* We have a username */
 251     if (at == NULL)
 252         rest = pcopy;
 253     else
 254     {
 255         const char *pend;
 256         char *inner_colon;
 257 
 258         pend = strchr (at, '\0');
 259         *at = '\0';
 260 
 261         inner_colon = strchr (pcopy, ':');
 262         if (inner_colon != NULL)
 263         {
 264             *inner_colon = '\0';
 265             inner_colon++;
 266             path_element->password = g_strdup (inner_colon);
 267         }
 268 
 269         if (*pcopy != '\0')
 270             path_element->user = g_strdup (pcopy);
 271 
 272         if (pend == at + 1)
 273             rest = at;
 274         else
 275             rest = at + 1;
 276     }
 277 
 278     /* Check if the host comes with a port spec, if so, chop it */
 279     if (*rest != '[')
 280         colon = strchr (rest, ':');
 281     else
 282     {
 283         colon = strchr (++rest, ']');
 284         if (colon != NULL)
 285         {
 286             *colon = '\0';
 287             colon++;
 288             *colon = '\0';
 289             path_element->ipv6 = TRUE;
 290         }
 291     }
 292 
 293     if (colon != NULL)
 294     {
 295         *colon = '\0';
 296         /* cppcheck-suppress invalidscanf */
 297         if (sscanf (colon + 1, "%d", &path_element->port) == 1)
 298         {
 299             if (path_element->port <= 0 || path_element->port >= 65536)
 300                 path_element->port = 0;
 301         }
 302         else
 303             while (*(++colon) != '\0')
 304             {
 305                 switch (*colon)
 306                 {
 307                 case 'C':
 308                     path_element->port = 1;
 309                     break;
 310                 case 'r':
 311                     path_element->port = 2;
 312                     break;
 313                 default:
 314                     break;
 315                 }
 316             }
 317     }
 318     path_element->host = g_strdup (rest);
 319     g_free (pcopy);
 320 }
 321 
 322 /* --------------------------------------------------------------------------------------------- */
 323 /**
 324  * get VFS class for the given name
 325  *
 326  * @param class_name name of class
 327  *
 328  * @return pointer to class structure or NULL if class not found
 329  */
 330 
 331 static struct vfs_class *
 332 vfs_get_class_by_name (const char *class_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 333 {
 334     guint i;
 335 
 336     if (class_name == NULL)
 337         return NULL;
 338 
 339     for (i = 0; i < vfs__classes_list->len; i++)
 340     {
 341         struct vfs_class *vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
 342         if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
 343             return vfs;
 344     }
 345 
 346     return NULL;
 347 }
 348 
 349 /* --------------------------------------------------------------------------------------------- */
 350 /**
 351  * Check if path string contain URL-like elements
 352  *
 353  * @param path_str path
 354  *
 355  * @return TRUE if path is deprecated or FALSE otherwise
 356  */
 357 
 358 static gboolean
 359 vfs_path_is_str_path_deprecated (const char *path_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 360 {
 361     return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
 362 }
 363 
 364 /* --------------------------------------------------------------------------------------------- */
 365 /** Split path string to path elements by deprecated algorithm.
 366  *
 367  * @param path_str VFS-path
 368  *
 369  * @return pointer to newly created vfs_path_t object with filled path elements array.
 370 */
 371 
 372 static vfs_path_t *
 373 vfs_path_from_str_deprecated_parser (char *path)
     /* [previous][next][first][last][top][bottom][index][help]  */
 374 {
 375     vfs_path_t *vpath;
 376     vfs_path_element_t *element;
 377     struct vfs_class *class;
 378     const char *local, *op;
 379 
 380     vpath = vfs_path_new ();
 381 
 382     while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
 383     {
 384         char *url_params;
 385         element = g_new0 (vfs_path_element_t, 1);
 386         element->class = class;
 387         if (local == NULL)
 388             local = "";
 389         element->path = vfs_translate_path_n (local);
 390 
 391 #ifdef HAVE_CHARSET
 392         element->encoding = vfs_get_encoding (local, -1);
 393         element->dir.converter =
 394             (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
 395 #endif
 396 
 397         url_params = strchr (op, ':');  /* skip VFS prefix */
 398         if (url_params != NULL)
 399         {
 400             *url_params = '\0';
 401             url_params++;
 402             vfs_path_url_split (element, url_params);
 403         }
 404 
 405         if (*op != '\0')
 406             element->vfs_prefix = g_strdup (op);
 407 
 408         g_array_prepend_val (vpath->path, element);
 409     }
 410     if (path[0] != '\0')
 411     {
 412         element = g_new0 (vfs_path_element_t, 1);
 413         element->class = g_ptr_array_index (vfs__classes_list, 0);
 414         element->path = vfs_translate_path_n (path);
 415 
 416 #ifdef HAVE_CHARSET
 417         element->encoding = vfs_get_encoding (path, -1);
 418         element->dir.converter =
 419             (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
 420 #endif
 421         g_array_prepend_val (vpath->path, element);
 422     }
 423 
 424     return vpath;
 425 }
 426 
 427 /* --------------------------------------------------------------------------------------------- */
 428 /** Split path string to path elements by URL algorithm.
 429  *
 430  * @param path_str VFS-path
 431  * @param flags    flags for converter
 432  *
 433  * @return pointer to newly created vfs_path_t object with filled path elements array.
 434 */
 435 
 436 static vfs_path_t *
 437 vfs_path_from_str_uri_parser (char *path)
     /* [previous][next][first][last][top][bottom][index][help]  */
 438 {
 439     vfs_path_t *vpath;
 440     vfs_path_element_t *element;
 441     char *url_delimiter;
 442 
 443     vpath = vfs_path_new ();
 444     vpath->relative = path != NULL && !IS_PATH_SEP (*path);
 445 
 446     while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
 447     {
 448         char *vfs_prefix_start;
 449         char *real_vfs_prefix_start = url_delimiter;
 450 
 451         while (real_vfs_prefix_start > path && !IS_PATH_SEP (*real_vfs_prefix_start))
 452             real_vfs_prefix_start--;
 453         vfs_prefix_start = real_vfs_prefix_start;
 454 
 455         if (IS_PATH_SEP (*vfs_prefix_start))
 456             vfs_prefix_start += 1;
 457 
 458         *url_delimiter = '\0';
 459 
 460         element = g_new0 (vfs_path_element_t, 1);
 461         element->class = vfs_prefix_to_class (vfs_prefix_start);
 462         element->vfs_prefix = g_strdup (vfs_prefix_start);
 463 
 464         url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
 465 
 466         if (element->class != NULL && (element->class->flags & VFSF_REMOTE) != 0)
 467         {
 468             char *slash_pointer;
 469 
 470             slash_pointer = strchr (url_delimiter, PATH_SEP);
 471             if (slash_pointer == NULL)
 472             {
 473                 element->path = g_strdup ("");
 474             }
 475             else
 476             {
 477                 element->path = vfs_translate_path_n (slash_pointer + 1);
 478 #ifdef HAVE_CHARSET
 479                 element->encoding = vfs_get_encoding (slash_pointer, -1);
 480 #endif
 481                 *slash_pointer = '\0';
 482             }
 483             vfs_path_url_split (element, url_delimiter);
 484         }
 485         else
 486         {
 487             element->path = vfs_translate_path_n (url_delimiter);
 488 #ifdef HAVE_CHARSET
 489             element->encoding = vfs_get_encoding (url_delimiter, -1);
 490 #endif
 491         }
 492 #ifdef HAVE_CHARSET
 493         element->dir.converter =
 494             (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
 495 #endif
 496         g_array_prepend_val (vpath->path, element);
 497 
 498         if ((real_vfs_prefix_start > path && IS_PATH_SEP (*real_vfs_prefix_start)) ||
 499             (real_vfs_prefix_start == path && !IS_PATH_SEP (*real_vfs_prefix_start)))
 500             *real_vfs_prefix_start = '\0';
 501         else
 502             *(real_vfs_prefix_start + 1) = '\0';
 503     }
 504 
 505     if (path[0] != '\0')
 506     {
 507         element = g_new0 (vfs_path_element_t, 1);
 508         element->class = g_ptr_array_index (vfs__classes_list, 0);
 509         element->path = vfs_translate_path_n (path);
 510 #ifdef HAVE_CHARSET
 511         element->encoding = vfs_get_encoding (path, -1);
 512         element->dir.converter =
 513             (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
 514 #endif
 515         g_array_prepend_val (vpath->path, element);
 516     }
 517 
 518     return vpath;
 519 }
 520 
 521 /* --------------------------------------------------------------------------------------------- */
 522 /**
 523  * Add element's class info to result string (such as VFS name, host, encoding etc)
 524  * This function used as helper only in vfs_path_tokens_get() function
 525  *
 526  * @param element current path element
 527  * @param ret_tokens total tikens for return
 528  * @param element_tokens accumulated element-only tokens
 529  */
 530 
 531 static void
 532 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
     /* [previous][next][first][last][top][bottom][index][help]  */
 533                                 GString * element_tokens)
 534 {
 535     if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
 536         && element_tokens->len > 0)
 537     {
 538         char *url_str;
 539 
 540         if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
 541             g_string_append_c (ret_tokens, PATH_SEP);
 542 
 543         g_string_append (ret_tokens, element->vfs_prefix);
 544         g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
 545 
 546         url_str = vfs_path_build_url_params_str (element, TRUE);
 547         if (*url_str != '\0')
 548         {
 549             g_string_append (ret_tokens, url_str);
 550             g_string_append_c (ret_tokens, PATH_SEP);
 551         }
 552 
 553         g_free (url_str);
 554     }
 555 
 556 #ifdef HAVE_CHARSET
 557     if (element->encoding != NULL)
 558     {
 559         if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
 560             g_string_append (ret_tokens, PATH_SEP_STR);
 561         g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
 562         g_string_append (ret_tokens, element->encoding);
 563         g_string_append (ret_tokens, PATH_SEP_STR);
 564     }
 565 #endif
 566 
 567     g_string_append (ret_tokens, element_tokens->str);
 568 }
 569 
 570 /* --------------------------------------------------------------------------------------------- */
 571 /**
 572  * Strip path to home dir.
 573  * @param dir pointer to string contains full path
 574  */
 575 
 576 static char *
 577 vfs_path_strip_home (const char *dir)
     /* [previous][next][first][last][top][bottom][index][help]  */
 578 {
 579     const char *home_dir = mc_config_get_home_dir ();
 580 
 581     if (home_dir != NULL)
 582     {
 583         size_t len;
 584 
 585         len = strlen (home_dir);
 586 
 587         if (strncmp (dir, home_dir, len) == 0 && (IS_PATH_SEP (dir[len]) || dir[len] == '\0'))
 588             return g_strdup_printf ("~%s", dir + len);
 589     }
 590 
 591     return g_strdup (dir);
 592 }
 593 
 594 /* --------------------------------------------------------------------------------------------- */
 595 /*** public functions ****************************************************************************/
 596 /* --------------------------------------------------------------------------------------------- */
 597 
 598 #define vfs_append_from_path(appendfrom, is_relative) \
 599 { \
 600     if ((flags & VPF_STRIP_HOME) && element_index == 0 && \
 601         (element->class->flags & VFSF_LOCAL) != 0) \
 602     { \
 603         char *stripped_home_str; \
 604         stripped_home_str = vfs_path_strip_home (appendfrom); \
 605         g_string_append (buffer, stripped_home_str); \
 606         g_free (stripped_home_str); \
 607     } \
 608     else \
 609     { \
 610         if (!is_relative && !IS_PATH_SEP (*appendfrom) && *appendfrom != '\0' \
 611             && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1]))) \
 612             g_string_append_c (buffer, PATH_SEP); \
 613         g_string_append (buffer, appendfrom); \
 614     } \
 615 }
 616 
 617 /**
 618  * Convert first elements_count elements from vfs_path_t to string representation with flags.
 619  *
 620  * @param vpath pointer to vfs_path_t object
 621  * @param elements_count count of first elements for convert
 622  * @param flags for converter
 623  *
 624  * @return pointer to newly created string.
 625  */
 626 
 627 char *
 628 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 629 {
 630     int element_index;
 631     GString *buffer;
 632     GString *recode_buffer;
 633 
 634     if (vpath == NULL)
 635         return NULL;
 636 
 637     if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
 638         elements_count = vfs_path_elements_count (vpath);
 639 
 640     if (elements_count < 0)
 641         elements_count = vfs_path_elements_count (vpath) + elements_count;
 642 
 643     buffer = g_string_new ("");
 644     recode_buffer = g_string_new ("");
 645 
 646     for (element_index = 0; element_index < elements_count; element_index++)
 647     {
 648         const vfs_path_element_t *element;
 649         gboolean is_relative = vpath->relative && (element_index == 0);
 650 
 651         element = vfs_path_get_by_index (vpath, element_index);
 652         if (element->vfs_prefix != NULL)
 653         {
 654             char *url_str;
 655             if (!is_relative && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
 656                 g_string_append_c (buffer, PATH_SEP);
 657 
 658             g_string_append (buffer, element->vfs_prefix);
 659             g_string_append (buffer, VFS_PATH_URL_DELIMITER);
 660 
 661             url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
 662 
 663             if (*url_str != '\0')
 664             {
 665                 g_string_append (buffer, url_str);
 666                 g_string_append_c (buffer, PATH_SEP);
 667             }
 668 
 669             g_free (url_str);
 670         }
 671 
 672 #ifdef HAVE_CHARSET
 673         if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
 674         {
 675             if ((flags & VPF_HIDE_CHARSET) == 0)
 676             {
 677                 if ((!is_relative)
 678                     && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
 679                     g_string_append (buffer, PATH_SEP_STR);
 680                 g_string_append (buffer, VFS_ENCODING_PREFIX);
 681                 g_string_append (buffer, element->encoding);
 682             }
 683             str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
 684             vfs_append_from_path (recode_buffer->str, is_relative);
 685             g_string_set_size (recode_buffer, 0);
 686         }
 687         else
 688 #endif
 689         {
 690             vfs_append_from_path (element->path, is_relative);
 691         }
 692     }
 693     g_string_free (recode_buffer, TRUE);
 694     return g_string_free (buffer, FALSE);
 695 }
 696 
 697 #undef vfs_append_from_path
 698 
 699 /* --------------------------------------------------------------------------------------------- */
 700 /**
 701  * Convert first elements_count elements from vfs_path_t to string representation.
 702  *
 703  * @param vpath pointer to vfs_path_t object
 704  * @param elements_count count of first elements for convert
 705  *
 706  * @return pointer to newly created string.
 707  */
 708 
 709 char *
 710 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
     /* [previous][next][first][last][top][bottom][index][help]  */
 711 {
 712     return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
 713 }
 714 
 715 /* --------------------------------------------------------------------------------------------- */
 716 /**
 717  * Split path string to path elements with flags for change parce process.
 718  *
 719  * @param path_str VFS-path
 720  * @param flags flags for parser
 721  *
 722  * @return pointer to newly created vfs_path_t object with filled path elements array.
 723  */
 724 
 725 vfs_path_t *
 726 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 727 {
 728     vfs_path_t *vpath;
 729     char *path;
 730 
 731     if (path_str == NULL)
 732         return NULL;
 733 
 734     if ((flags & VPF_NO_CANON) == 0)
 735         path = vfs_canon (path_str);
 736     else
 737         path = g_strdup (path_str);
 738 
 739     if (path == NULL)
 740         return NULL;
 741 
 742     if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
 743         vpath = vfs_path_from_str_deprecated_parser (path);
 744     else
 745         vpath = vfs_path_from_str_uri_parser (path);
 746 
 747     vpath->str = vfs_path_to_str_flags (vpath, 0, flags);
 748     g_free (path);
 749 
 750     return vpath;
 751 }
 752 
 753 /* --------------------------------------------------------------------------------------------- */
 754 /**
 755  * Split path string to path elements.
 756  *
 757  * @param path_str VFS-path
 758  *
 759  * @return pointer to newly created vfs_path_t object with filled path elements array.
 760  */
 761 
 762 vfs_path_t *
 763 vfs_path_from_str (const char *path_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 764 {
 765     return vfs_path_from_str_flags (path_str, VPF_NONE);
 766 }
 767 
 768 /* --------------------------------------------------------------------------------------------- */
 769 /*
 770  * Create new vfs_path_t object.
 771  *
 772  * @return pointer to newly created vfs_path_t object.
 773  */
 774 
 775 vfs_path_t *
 776 vfs_path_new (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 777 {
 778     vfs_path_t *vpath;
 779 
 780     vpath = g_new0 (vfs_path_t, 1);
 781     vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
 782 
 783     return vpath;
 784 }
 785 
 786 /* --------------------------------------------------------------------------------------------- */
 787 /*
 788  * Get count of path elements.
 789  *
 790  * @param vpath pointer to vfs_path_t object
 791  *
 792  * @return count of path elements.
 793  */
 794 
 795 int
 796 vfs_path_elements_count (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 797 {
 798     return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
 799 }
 800 
 801 /* --------------------------------------------------------------------------------------------- */
 802 /**
 803  * Add vfs_path_element_t object to end of list in vfs_path_t object
 804  * @param vpath pointer to vfs_path_t object
 805  * @param path_element pointer to vfs_path_element_t object
 806  */
 807 
 808 void
 809 vfs_path_add_element (vfs_path_t * vpath, const vfs_path_element_t * path_element)
     /* [previous][next][first][last][top][bottom][index][help]  */
 810 {
 811     g_array_append_val (vpath->path, path_element);
 812     g_free (vpath->str);
 813     vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
 814 }
 815 
 816 /* --------------------------------------------------------------------------------------------- */
 817 /*
 818  * Get one path element by index.
 819  *
 820  * @param vpath pointer to vfs_path_t object.
 821  *              May be NULL. In this case NULL is returned and errno set to 0.
 822  * @param element_index element index. May have negative value (in this case count was started at
 823  *                      the end of list). If @element_index is out of range, NULL is returned and
 824  *                      errno set to EINVAL.
 825  *
 826  * @return path element
 827  */
 828 
 829 const vfs_path_element_t *
 830 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
 831 {
 832     int n;
 833 
 834     if (vpath == NULL)
 835     {
 836         errno = 0;
 837         return NULL;
 838     }
 839 
 840     n = vfs_path_elements_count (vpath);
 841 
 842     if (element_index < 0)
 843         element_index += n;
 844 
 845     if (element_index < 0 || element_index > n)
 846     {
 847         errno = EINVAL;
 848         return NULL;
 849     }
 850 
 851     return g_array_index (vpath->path, vfs_path_element_t *, element_index);
 852 }
 853 
 854 /* --------------------------------------------------------------------------------------------- */
 855 /*
 856  * Clone one path element
 857  *
 858  * @param element pointer to vfs_path_element_t object
 859  *
 860  * @return Newly allocated path element
 861  */
 862 
 863 vfs_path_element_t *
 864 vfs_path_element_clone (const vfs_path_element_t * element)
     /* [previous][next][first][last][top][bottom][index][help]  */
 865 {
 866     vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
 867 
 868     new_element->user = g_strdup (element->user);
 869     new_element->password = g_strdup (element->password);
 870     new_element->host = g_strdup (element->host);
 871     new_element->ipv6 = element->ipv6;
 872     new_element->port = element->port;
 873     new_element->path = g_strdup (element->path);
 874     new_element->class = element->class;
 875     new_element->vfs_prefix = g_strdup (element->vfs_prefix);
 876 #ifdef HAVE_CHARSET
 877     new_element->encoding = g_strdup (element->encoding);
 878     if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
 879         new_element->dir.converter = str_crt_conv_from (new_element->encoding);
 880     else
 881         new_element->dir.converter = element->dir.converter;
 882 #endif
 883     new_element->dir.info = element->dir.info;
 884 
 885     return new_element;
 886 }
 887 
 888 /* --------------------------------------------------------------------------------------------- */
 889 /*
 890  * Free one path element.
 891  *
 892  * @param element pointer to vfs_path_element_t object
 893  *
 894  */
 895 
 896 void
 897 vfs_path_element_free (vfs_path_element_t * element)
     /* [previous][next][first][last][top][bottom][index][help]  */
 898 {
 899     if (element == NULL)
 900         return;
 901 
 902     g_free (element->user);
 903     g_free (element->password);
 904     g_free (element->host);
 905     g_free (element->path);
 906     g_free (element->vfs_prefix);
 907 
 908 #ifdef HAVE_CHARSET
 909     g_free (element->encoding);
 910 
 911     if (vfs_path_element_need_cleanup_converter (element))
 912         str_close_conv (element->dir.converter);
 913 #endif
 914 
 915     g_free (element);
 916 }
 917 
 918 /* --------------------------------------------------------------------------------------------- */
 919 /*
 920  * Clone path
 921  *
 922  * @param vpath pointer to vfs_path_t object
 923  *
 924  * @return Newly allocated path object
 925  */
 926 
 927 vfs_path_t *
 928 vfs_path_clone (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 929 {
 930     vfs_path_t *new_vpath;
 931     int vpath_element_index;
 932 
 933     if (vpath == NULL)
 934         return NULL;
 935 
 936     new_vpath = vfs_path_new ();
 937     new_vpath->relative = vpath->relative;
 938 
 939     for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
 940          vpath_element_index++)
 941     {
 942         vfs_path_element_t *path_element;
 943 
 944         path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
 945         g_array_append_val (new_vpath->path, path_element);
 946     }
 947     new_vpath->str = g_strdup (vpath->str);
 948 
 949     return new_vpath;
 950 }
 951 
 952 /* --------------------------------------------------------------------------------------------- */
 953 /*
 954  * Free vfs_path_t object.
 955  *
 956  * @param vpath pointer to vfs_path_t object
 957  * @param free_str if TRUE the string representation of vpath is freed as well
 958  *
 959  * @return the string representation of vpath (i.e. NULL if free_str is TRUE)
 960  */
 961 
 962 char *
 963 vfs_path_free (vfs_path_t * vpath, gboolean free_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 964 {
 965     int vpath_element_index;
 966     char *ret;
 967 
 968     if (vpath == NULL)
 969         return NULL;
 970 
 971     for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
 972          vpath_element_index++)
 973     {
 974         vfs_path_element_t *path_element;
 975 
 976         path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
 977         vfs_path_element_free (path_element);
 978     }
 979 
 980     g_array_free (vpath->path, TRUE);
 981 
 982     if (!free_str)
 983         ret = vpath->str;
 984     else
 985     {
 986         g_free (vpath->str);
 987         ret = NULL;
 988     }
 989 
 990     g_free (vpath);
 991 
 992     return ret;
 993 }
 994 
 995 /* --------------------------------------------------------------------------------------------- */
 996 /*
 997  * Remove one path element by index
 998  *
 999  * @param vpath pointer to vfs_path_t object
1000  * @param element_index element index. May have negative value (in this case count was started at the end of list).
1001  *
1002  */
1003 
1004 void
1005 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
1006 {
1007     vfs_path_element_t *element;
1008 
1009     if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
1010         return;
1011 
1012     if (element_index < 0)
1013         element_index = vfs_path_elements_count (vpath) + element_index;
1014 
1015     element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
1016     vpath->path = g_array_remove_index (vpath->path, element_index);
1017     vfs_path_element_free (element);
1018     g_free (vpath->str);
1019     vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1020 }
1021 
1022 /* --------------------------------------------------------------------------------------------- */
1023 /** Return VFS class for the given prefix */
1024 
1025 struct vfs_class *
1026 vfs_prefix_to_class (const char *prefix)
     /* [previous][next][first][last][top][bottom][index][help]  */
1027 {
1028     guint i;
1029 
1030     /* Avoid first class (localfs) that would accept any prefix */
1031     for (i = 1; i < vfs__classes_list->len; i++)
1032     {
1033         struct vfs_class *vfs;
1034 
1035         vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
1036         if (vfs->which != NULL)
1037         {
1038             if (vfs->which (vfs, prefix) == -1)
1039                 continue;
1040             return vfs;
1041         }
1042 
1043         if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1044             return vfs;
1045     }
1046 
1047     return NULL;
1048 }
1049 
1050 /* --------------------------------------------------------------------------------------------- */
1051 
1052 #ifdef HAVE_CHARSET
1053 
1054 /**
1055  * Check if need cleanup charset converter for vfs_path_element_t
1056  *
1057  * @param element part of path
1058  *
1059  * @return TRUE if need cleanup converter or FALSE otherwise
1060  */
1061 
1062 gboolean
1063 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
     /* [previous][next][first][last][top][bottom][index][help]  */
1064 {
1065     return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1066 }
1067 
1068 /* --------------------------------------------------------------------------------------------- */
1069 /**
1070  * Change encoding for last part (vfs_path_element_t) of vpath
1071  *
1072  * @param vpath pointer to path structure
1073  * encoding name of charset
1074  *
1075  * @return pointer to path structure (for use function in anoter functions)
1076  */
1077 vfs_path_t *
1078 vfs_path_change_encoding (vfs_path_t * vpath, const char *encoding)
     /* [previous][next][first][last][top][bottom][index][help]  */
1079 {
1080     vfs_path_element_t *path_element;
1081 
1082     path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
1083     /* don't add current encoding */
1084     if ((path_element->encoding != NULL) && (strcmp (encoding, path_element->encoding) == 0))
1085         return vpath;
1086 
1087     g_free (path_element->encoding);
1088     path_element->encoding = g_strdup (encoding);
1089 
1090     if (vfs_path_element_need_cleanup_converter (path_element))
1091         str_close_conv (path_element->dir.converter);
1092 
1093     path_element->dir.converter = str_crt_conv_from (path_element->encoding);
1094 
1095     g_free (vpath->str);
1096     vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1097     return vpath;
1098 }
1099 
1100 #endif
1101 
1102 /* --------------------------------------------------------------------------------------------- */
1103 
1104 /**
1105  * Serialize vfs_path_t object to string
1106  *
1107  * @param vpath data for serialization
1108  * @param error contain pointer to object for handle error code and message
1109  *
1110  * @return serialized vpath as newly allocated string
1111  */
1112 
1113 char *
1114 vfs_path_serialize (const vfs_path_t * vpath, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
1115 {
1116     mc_config_t *cpath;
1117     ssize_t element_index;
1118     char *ret_value;
1119 
1120     mc_return_val_if_error (mcerror, FALSE);
1121 
1122     if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1123     {
1124         mc_propagate_error (mcerror, 0, "%s", "vpath object is empty");
1125         return NULL;
1126     }
1127 
1128     cpath = mc_config_init (NULL, FALSE);
1129 
1130     for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1131     {
1132         char groupname[BUF_TINY];
1133         const vfs_path_element_t *element;
1134 
1135         g_snprintf (groupname, sizeof (groupname), "path-element-%zd", element_index);
1136         element = vfs_path_get_by_index (vpath, element_index);
1137         /* convert one element to config group */
1138 
1139         mc_config_set_string_raw (cpath, groupname, "path", element->path);
1140         mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1141 #ifdef HAVE_CHARSET
1142         mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1143 #endif
1144         mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1145 
1146         mc_config_set_string_raw (cpath, groupname, "user", element->user);
1147         mc_config_set_string_raw (cpath, groupname, "password", element->password);
1148         mc_config_set_string_raw (cpath, groupname, "host", element->host);
1149         if (element->port != 0)
1150             mc_config_set_int (cpath, groupname, "port", element->port);
1151     }
1152 
1153     ret_value = mc_serialize_config (cpath, mcerror);
1154     mc_config_deinit (cpath);
1155     return ret_value;
1156 }
1157 
1158 /* --------------------------------------------------------------------------------------------- */
1159 /**
1160  * Deserialize string to vfs_path_t object
1161  *
1162  * @param data data for serialization
1163  * @param error contain pointer to object for handle error code and message
1164  *
1165  * @return newly allocated vfs_path_t object
1166  */
1167 
1168 vfs_path_t *
1169 vfs_path_deserialize (const char *data, GError ** mcerror)
     /* [previous][next][first][last][top][bottom][index][help]  */
1170 {
1171     mc_config_t *cpath;
1172     size_t element_index;
1173     vfs_path_t *vpath;
1174 
1175     mc_return_val_if_error (mcerror, FALSE);
1176 
1177     cpath = mc_deserialize_config (data, mcerror);
1178     if (cpath == NULL)
1179         return NULL;
1180 
1181     vpath = vfs_path_new ();
1182 
1183     for (element_index = 0;; element_index++)
1184     {
1185         struct vfs_class *eclass;
1186         vfs_path_element_t *element;
1187         char *cfg_value;
1188         char groupname[BUF_TINY];
1189 
1190         g_snprintf (groupname, sizeof (groupname), "path-element-%zu", element_index);
1191         if (!mc_config_has_group (cpath, groupname))
1192             break;
1193 
1194         cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1195         eclass = vfs_get_class_by_name (cfg_value);
1196         if (eclass == NULL)
1197         {
1198             vfs_path_free (vpath, TRUE);
1199             g_set_error (mcerror, MC_ERROR, 0, "Unable to find VFS class by name '%s'", cfg_value);
1200             g_free (cfg_value);
1201             mc_config_deinit (cpath);
1202             return NULL;
1203         }
1204         g_free (cfg_value);
1205 
1206         element = g_new0 (vfs_path_element_t, 1);
1207         element->class = eclass;
1208         element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1209 
1210 #ifdef HAVE_CHARSET
1211         element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1212         element->dir.converter =
1213             (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1214 #endif
1215 
1216         element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1217 
1218         element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1219         element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1220         element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1221         element->port = mc_config_get_int (cpath, groupname, "port", 0);
1222 
1223         vpath->path = g_array_append_val (vpath->path, element);
1224     }
1225 
1226     mc_config_deinit (cpath);
1227     if (vfs_path_elements_count (vpath) == 0)
1228     {
1229         vfs_path_free (vpath, TRUE);
1230         g_set_error (mcerror, MC_ERROR, 0, "No any path elements found");
1231         return NULL;
1232     }
1233     vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1234 
1235     return vpath;
1236 }
1237 
1238 /* --------------------------------------------------------------------------------------------- */
1239 /**
1240  * Build vfs_path_t object from arguments.
1241  *
1242  * @param first_element of path
1243  * @param ... path tokens, terminated by NULL
1244  *
1245  * @return newly allocated vfs_path_t object
1246  */
1247 
1248 vfs_path_t *
1249 vfs_path_build_filename (const char *first_element, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
1250 {
1251     va_list args;
1252     char *str_path;
1253     vfs_path_t *vpath;
1254 
1255     if (first_element == NULL)
1256         return NULL;
1257 
1258     va_start (args, first_element);
1259     str_path = mc_build_filenamev (first_element, args);
1260     va_end (args);
1261     vpath = vfs_path_from_str (str_path);
1262     g_free (str_path);
1263     return vpath;
1264 }
1265 
1266 /* --------------------------------------------------------------------------------------------- */
1267 /**
1268  * Append tokens to path object
1269  *
1270  * @param vpath path object
1271  * @param first_element of path
1272  * @param ... NULL-terminated strings
1273  *
1274  * @return newly allocated path object
1275  */
1276 
1277 vfs_path_t *
1278 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
1279 {
1280     va_list args;
1281     char *str_path;
1282     const char *result_str;
1283     vfs_path_t *ret_vpath;
1284 
1285     if (vpath == NULL || first_element == NULL)
1286         return NULL;
1287 
1288     va_start (args, first_element);
1289     str_path = mc_build_filenamev (first_element, args);
1290     va_end (args);
1291 
1292     result_str = vfs_path_as_str (vpath);
1293     ret_vpath = vfs_path_build_filename (result_str, str_path, (char *) NULL);
1294     g_free (str_path);
1295 
1296     return ret_vpath;
1297 
1298 }
1299 
1300 /* --------------------------------------------------------------------------------------------- */
1301 
1302 /**
1303  * Append vpath_t tokens to path object
1304  *
1305  * @param first_vpath vpath objects
1306  * @param ... NULL-terminated vpath objects
1307  *
1308  * @return newly allocated path object
1309  */
1310 
1311 vfs_path_t *
1312 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
1313 {
1314     va_list args;
1315     vfs_path_t *ret_vpath;
1316     const vfs_path_t *current_vpath = first_vpath;
1317 
1318     if (first_vpath == NULL)
1319         return NULL;
1320 
1321     ret_vpath = vfs_path_new ();
1322 
1323     va_start (args, first_vpath);
1324     do
1325     {
1326         int vindex;
1327 
1328         for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1329         {
1330             vfs_path_element_t *path_element;
1331 
1332             path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1333             g_array_append_val (ret_vpath->path, path_element);
1334         }
1335         current_vpath = va_arg (args, const vfs_path_t *);
1336     }
1337     while (current_vpath != NULL);
1338     va_end (args);
1339 
1340     ret_vpath->str = vfs_path_to_str_flags (ret_vpath, 0, VPF_NONE);
1341 
1342     return ret_vpath;
1343 }
1344 
1345 /* --------------------------------------------------------------------------------------------- */
1346 
1347 /**
1348  * get tockens count in path.
1349  *
1350  * @param vpath path object
1351  *
1352  * @return count of tokens
1353  */
1354 
1355 size_t
1356 vfs_path_tokens_count (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1357 {
1358     size_t count_tokens = 0;
1359     int element_index;
1360 
1361     if (vpath == NULL)
1362         return 0;
1363 
1364     for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1365     {
1366         const vfs_path_element_t *element;
1367         const char *token, *prev_token;
1368 
1369         element = vfs_path_get_by_index (vpath, element_index);
1370 
1371         for (prev_token = element->path; (token = strchr (prev_token, PATH_SEP)) != NULL;
1372              prev_token = token + 1)
1373         {
1374             /* skip empty substring */
1375             if (token != prev_token)
1376                 count_tokens++;
1377         }
1378 
1379         if (*prev_token != '\0')
1380             count_tokens++;
1381     }
1382 
1383     return count_tokens;
1384 }
1385 
1386 /* --------------------------------------------------------------------------------------------- */
1387 
1388 /**
1389  * Get subpath by tokens
1390  *
1391  * @param vpath path object
1392  * @param start_position first token for got/ Started from 0.
1393  *        If negative, then position will be relative to end of path
1394  * @param length count of tokens
1395  *
1396  * @return newly allocated string with path tokens separated by slash
1397  */
1398 
1399 char *
1400 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
     /* [previous][next][first][last][top][bottom][index][help]  */
1401 {
1402     GString *ret_tokens, *element_tokens;
1403     int element_index;
1404     size_t tokens_count = vfs_path_tokens_count (vpath);
1405 
1406     if (vpath == NULL)
1407         return NULL;
1408 
1409     if (length == 0)
1410         length = tokens_count;
1411 
1412     if (length < 0)
1413         length = tokens_count + length;
1414 
1415     if (start_position < 0)
1416         start_position = (ssize_t) tokens_count + start_position;
1417 
1418     if (start_position < 0)
1419         return NULL;
1420 
1421     if (start_position >= (ssize_t) tokens_count)
1422         return NULL;
1423 
1424     if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1425         length = tokens_count - start_position;
1426 
1427     ret_tokens = g_string_sized_new (32);
1428     element_tokens = g_string_sized_new (32);
1429 
1430     for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1431     {
1432         const vfs_path_element_t *element;
1433         char **path_tokens, **iterator;
1434 
1435         g_string_assign (element_tokens, "");
1436         element = vfs_path_get_by_index (vpath, element_index);
1437         path_tokens = g_strsplit (element->path, PATH_SEP_STR, -1);
1438 
1439         for (iterator = path_tokens; *iterator != NULL; iterator++)
1440         {
1441             if (**iterator != '\0')
1442             {
1443                 if (start_position == 0)
1444                 {
1445                     if (length == 0)
1446                     {
1447                         vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1448                         g_string_free (element_tokens, TRUE);
1449                         g_strfreev (path_tokens);
1450                         return g_string_free (ret_tokens, FALSE);
1451                     }
1452                     length--;
1453                     if (element_tokens->len != 0)
1454                         g_string_append_c (element_tokens, PATH_SEP);
1455                     g_string_append (element_tokens, *iterator);
1456                 }
1457                 else
1458                     start_position--;
1459             }
1460         }
1461         g_strfreev (path_tokens);
1462         vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1463     }
1464 
1465     g_string_free (element_tokens, TRUE);
1466     return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1467 }
1468 
1469 /* --------------------------------------------------------------------------------------------- */
1470 /**
1471  * Get subpath by tokens
1472  *
1473  * @param vpath path object
1474  * @param start_position first token for got/ Started from 0.
1475  *        If negative, then position will be relative to end of path
1476  * @param length count of tokens
1477  *
1478  * @return newly allocated path object with path tokens separated by slash
1479  */
1480 
1481 vfs_path_t *
1482 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
     /* [previous][next][first][last][top][bottom][index][help]  */
1483 {
1484     char *str_tokens;
1485     vfs_path_t *ret_vpath = NULL;
1486 
1487     str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1488     if (str_tokens != NULL)
1489     {
1490         ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1491         g_free (str_tokens);
1492     }
1493     return ret_vpath;
1494 }
1495 
1496 /* --------------------------------------------------------------------------------------------- */
1497 
1498 /**
1499  * Build URL parameters (such as user:pass @ host:port) from one path element object
1500  *
1501  * @param element path element
1502  * @param keep_password TRUE or FALSE
1503  *
1504  * @return newly allocated string
1505  */
1506 
1507 char *
1508 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
     /* [previous][next][first][last][top][bottom][index][help]  */
1509 {
1510     GString *buffer;
1511 
1512     if (element == NULL)
1513         return NULL;
1514 
1515     buffer = g_string_new ("");
1516 
1517     if (element->user != NULL)
1518         g_string_append (buffer, element->user);
1519 
1520     if (element->password != NULL && keep_password)
1521     {
1522         g_string_append_c (buffer, ':');
1523         g_string_append (buffer, element->password);
1524     }
1525 
1526     if (element->host != NULL)
1527     {
1528         if ((element->user != NULL) || (element->password != NULL))
1529             g_string_append_c (buffer, '@');
1530         if (element->ipv6)
1531             g_string_append_c (buffer, '[');
1532         g_string_append (buffer, element->host);
1533         if (element->ipv6)
1534             g_string_append_c (buffer, ']');
1535     }
1536 
1537     if ((element->port) != 0 && (element->host != NULL))
1538     {
1539         g_string_append_c (buffer, ':');
1540         g_string_append_printf (buffer, "%d", element->port);
1541     }
1542 
1543     return g_string_free (buffer, FALSE);
1544 }
1545 
1546 /* --------------------------------------------------------------------------------------------- */
1547 /**
1548  * Build pretty string representation of one path_element_t object
1549  *
1550  * @param element path element
1551  *
1552  * @return newly allocated string
1553  */
1554 
1555 char *
1556 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
     /* [previous][next][first][last][top][bottom][index][help]  */
1557 {
1558     char *url_params;
1559     GString *pretty_path;
1560 
1561     pretty_path = g_string_new (element->class->prefix);
1562     g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1563 
1564     url_params = vfs_path_build_url_params_str (element, FALSE);
1565     g_string_append (pretty_path, url_params);
1566     g_free (url_params);
1567 
1568     if (!IS_PATH_SEP (*element->path))
1569         g_string_append_c (pretty_path, PATH_SEP);
1570 
1571     g_string_append (pretty_path, element->path);
1572     return g_string_free (pretty_path, FALSE);
1573 }
1574 
1575 /* --------------------------------------------------------------------------------------------- */
1576 /**
1577  * Compare two path objects as strings
1578  *
1579  * @param vpath1 first path object
1580  * @param vpath2 second vpath object
1581  *
1582  * @return integer value like to strcmp.
1583  */
1584 
1585 gboolean
1586 vfs_path_equal (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1587 {
1588     const char *path1, *path2;
1589     gboolean ret_val;
1590 
1591     if (vpath1 == NULL || vpath2 == NULL)
1592         return FALSE;
1593 
1594     path1 = vfs_path_as_str (vpath1);
1595     path2 = vfs_path_as_str (vpath2);
1596 
1597     ret_val = strcmp (path1, path2) == 0;
1598 
1599     return ret_val;
1600 }
1601 
1602 /* --------------------------------------------------------------------------------------------- */
1603 /**
1604  * Compare two path objects as strings
1605  *
1606  * @param vpath1 first path object
1607  * @param vpath2 second vpath object
1608  * @param len number of first 'len' characters
1609  *
1610  * @return integer value like to strcmp.
1611  */
1612 
1613 gboolean
1614 vfs_path_equal_len (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
     /* [previous][next][first][last][top][bottom][index][help]  */
1615 {
1616     const char *path1, *path2;
1617     gboolean ret_val;
1618 
1619     if (vpath1 == NULL || vpath2 == NULL)
1620         return FALSE;
1621 
1622     path1 = vfs_path_as_str (vpath1);
1623     path2 = vfs_path_as_str (vpath2);
1624 
1625     ret_val = strncmp (path1, path2, len) == 0;
1626 
1627     return ret_val;
1628 }
1629 
1630 /* --------------------------------------------------------------------------------------------- */
1631 /**
1632  * Calculate path length in string representation
1633  *
1634  * @param vpath path object
1635  *
1636  * @return length of path
1637  */
1638 
1639 size_t
1640 vfs_path_len (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1641 {
1642     if (vpath == NULL)
1643         return 0;
1644 
1645     return strlen (vpath->str);
1646 }
1647 
1648 /* --------------------------------------------------------------------------------------------- */
1649 /**
1650  * Convert relative vpath object to absolute
1651  *
1652  * @param vpath path object
1653  *
1654  * @return absolute path object
1655  */
1656 
1657 vfs_path_t *
1658 vfs_path_to_absolute (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1659 {
1660     vfs_path_t *absolute_vpath;
1661     const char *path_str;
1662 
1663     if (!vpath->relative)
1664         return vfs_path_clone (vpath);
1665 
1666     path_str = vfs_path_as_str (vpath);
1667     absolute_vpath = vfs_path_from_str (path_str);
1668     return absolute_vpath;
1669 }
1670 
1671 /* --------------------------------------------------------------------------------------------- */

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