Optimization of ini files load.
[midnight-commander.git] / lib / vfs / path.c
blob3a6eb8b88629a97e5c65df8dab6b78122783d8b8
1 /*
2 Virtual File System path handlers
4 Copyright (C) 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /**
27 * \file
28 * \brief Source: Virtual File System: path handlers
29 * \author Slava Zanko
30 * \date 2011
34 #include <config.h>
36 #include "lib/global.h"
37 #include "lib/strutil.h"
38 #include "lib/util.h" /* mc_build_filename() */
39 #include "lib/serialize.h"
41 #include "vfs.h"
42 #include "utilvfs.h"
43 #include "xdirentry.h"
44 #include "path.h"
46 extern GPtrArray *vfs__classes_list;
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
52 /*** file scope type declarations ****************************************************************/
54 /*** file scope variables ************************************************************************/
56 /*** file scope functions ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
59 static gboolean
60 path_magic (const char *path)
62 struct stat buf;
64 return (stat (path, &buf) != 0);
67 /* --------------------------------------------------------------------------------------------- */
69 /**
70 * Splits path extracting vfs part.
72 * Splits path
73 * \verbatim /p1#op/inpath \endverbatim
74 * into
75 * \verbatim inpath,op; \endverbatim
76 * returns which vfs it is.
77 * What is left in path is p1. You still want to g_free(path), you DON'T
78 * want to free neither *inpath nor *op
81 static struct vfs_class *
82 _vfs_split_with_semi_skip_count (char *path, const char **inpath, const char **op,
83 size_t skip_count)
85 char *semi;
86 char *slash;
87 struct vfs_class *ret;
89 if (path == NULL)
90 vfs_die ("Cannot split NULL");
92 semi = strrstr_skip_count (path, "#", skip_count);
94 if ((semi == NULL) || (!path_magic (path)))
95 return NULL;
97 slash = strchr (semi, PATH_SEP);
98 *semi = '\0';
100 if (op != NULL)
101 *op = NULL;
103 if (inpath != NULL)
104 *inpath = NULL;
106 if (slash != NULL)
107 *slash = '\0';
109 ret = vfs_prefix_to_class (semi + 1);
110 if (ret != NULL)
112 if (op != NULL)
113 *op = semi + 1;
114 if (inpath != NULL)
115 *inpath = slash != NULL ? slash + 1 : NULL;
116 return ret;
119 if (slash != NULL)
120 *slash = PATH_SEP;
122 *semi = '#';
123 ret = _vfs_split_with_semi_skip_count (path, inpath, op, skip_count + 1);
124 return ret;
127 /* --------------------------------------------------------------------------------------------- */
129 * remove //, /./ and /../
131 * @return newly allocated string
134 static char *
135 vfs_canon (const char *path)
137 if (path == NULL)
138 vfs_die ("Cannot canonicalize NULL");
140 /* Relative to current directory */
141 if (*path != PATH_SEP)
143 char *result, *local;
145 if (g_str_has_prefix (path, VFS_ENCODING_PREFIX))
148 encoding prefix placed at start of string without the leading slash
149 should be autofixed by adding the leading slash
151 local = mc_build_filename (PATH_SEP_STR, path, NULL);
153 else
155 local = tilde_expand (path);
156 if (*local != PATH_SEP)
158 char *curr_dir;
160 g_free (local);
161 curr_dir = vfs_get_current_dir ();
162 local = mc_build_filename (curr_dir, path, NULL);
163 g_free (curr_dir);
166 result = vfs_canon (local);
167 g_free (local);
168 return result;
172 * So we have path of following form:
173 * /p1/p2#op/.././././p3#op/p4. Good luck.
176 char *result;
178 result = g_strdup (path);
179 canonicalize_pathname (result);
180 return result;
184 /* --------------------------------------------------------------------------------------------- */
186 #ifdef HAVE_CHARSET
187 /** get encoding after last #enc: or NULL, if part does not contain #enc:
189 * @param path string
191 * @return newly allocated string.
194 static char *
195 vfs_get_encoding (const char *path)
197 char result[16];
198 char *work;
199 char *semi;
200 char *slash;
201 work = g_strdup (path);
203 /* try found #enc: */
204 semi = g_strrstr (work, VFS_ENCODING_PREFIX);
206 if (semi != NULL && (semi == work || *(semi - 1) == PATH_SEP))
208 semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
209 slash = strchr (semi, PATH_SEP);
210 if (slash != NULL)
211 slash[0] = '\0';
213 g_strlcpy (result, semi, sizeof (result));
214 g_free (work);
215 return g_strdup (result);
217 else
219 g_free (work);
220 return NULL;
223 #endif
225 /* --------------------------------------------------------------------------------------------- */
226 /** Extract the hostname and username from the path
228 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
230 * ftp://sunsite.unc.edu/pub/linux
231 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
232 * ftp://tsx-11.mit.edu:8192/
233 * ftp://joe@foo.edu:11321/private
234 * ftp://joe:password@foo.se
236 * @param path_element is an input string to be parsed
237 * @param path is an input string to be parsed
239 * @return g_malloc()ed url info.
240 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
241 * is not set, then the current login name is supplied.
242 * Return value is a g_malloc()ed structure with the pathname relative to the
243 * host.
246 static void
247 vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
249 char *pcopy;
250 const char *pend;
251 char *dir, *colon, *inner_colon, *at, *rest;
253 path_element->port = 0;
255 pcopy = g_strdup (path);
256 pend = pcopy + strlen (pcopy);
257 dir = pcopy;
259 /* search for any possible user */
260 at = strrchr (pcopy, '@');
262 /* We have a username */
263 if (at == NULL)
264 rest = pcopy;
265 else
267 *at = '\0';
268 inner_colon = strchr (pcopy, ':');
269 if (inner_colon != NULL)
271 *inner_colon = '\0';
272 inner_colon++;
273 path_element->password = g_strdup (inner_colon);
276 if (*pcopy != '\0')
277 path_element->user = g_strdup (pcopy);
279 if (pend == at + 1)
280 rest = at;
281 else
282 rest = at + 1;
285 /* Check if the host comes with a port spec, if so, chop it */
286 if (*rest != '[')
287 colon = strchr (rest, ':');
288 else
290 colon = strchr (++rest, ']');
291 if (colon != NULL)
293 colon[0] = '\0';
294 colon[1] = '\0';
295 colon++;
296 path_element->ipv6 = TRUE;
300 if (colon != NULL)
302 *colon = '\0';
303 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
305 if (path_element->port <= 0 || path_element->port >= 65536)
306 path_element->port = 0;
308 else
309 while (*(++colon) != '\0')
311 switch (*colon)
313 case 'C':
314 path_element->port = 1;
315 break;
316 case 'r':
317 path_element->port = 2;
318 break;
322 path_element->host = g_strdup (rest);
323 g_free (pcopy);
326 /* --------------------------------------------------------------------------------------------- */
328 * get VFS class for the given name
330 * @param class_name name of class
332 * @return pointer to class structure or NULL if class not found
335 static struct vfs_class *
336 vfs_get_class_by_name (const char *class_name)
338 guint i;
340 if (class_name == NULL)
341 return NULL;
343 for (i = 0; i < vfs__classes_list->len; i++)
345 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
346 if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
347 return vfs;
350 return NULL;
353 /* --------------------------------------------------------------------------------------------- */
355 * Check if path string contain URL-like elements
357 * @param path_str path
359 * @return TRUE if path is deprecated or FALSE otherwise
362 static gboolean
363 vfs_path_is_str_path_deprecated (const char *path_str)
365 return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
368 /* --------------------------------------------------------------------------------------------- */
369 /** Split path string to path elements by deprecated algorithm.
371 * @param path_str VFS-path
373 * @return pointer to newly created vfs_path_t object with filled path elements array.
376 static vfs_path_t *
377 vfs_path_from_str_deprecated_parser (char *path, vfs_path_flag_t flags)
379 vfs_path_t *vpath;
380 vfs_path_element_t *element;
381 struct vfs_class *class;
382 const char *local, *op;
384 (void) flags;
385 vpath = vfs_path_new ();
387 while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
389 char *url_params;
390 element = g_new0 (vfs_path_element_t, 1);
391 element->class = class;
392 if (local == NULL)
393 local = "";
394 element->path = vfs_translate_path_n (local);
396 #ifdef HAVE_CHARSET
397 element->encoding = vfs_get_encoding (local);
398 element->dir.converter =
399 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
400 #endif
402 url_params = strchr (op, ':'); /* skip VFS prefix */
403 if (url_params != NULL)
405 *url_params = '\0';
406 url_params++;
407 vfs_path_url_split (element, url_params);
410 if (*op != '\0')
411 element->vfs_prefix = g_strdup (op);
413 g_array_prepend_val (vpath->path, element);
415 if (path[0] != '\0')
417 element = g_new0 (vfs_path_element_t, 1);
418 element->class = g_ptr_array_index (vfs__classes_list, 0);
419 element->path = vfs_translate_path_n (path);
421 #ifdef HAVE_CHARSET
422 element->encoding = vfs_get_encoding (path);
423 element->dir.converter =
424 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
425 #endif
426 g_array_prepend_val (vpath->path, element);
429 return vpath;
432 /* --------------------------------------------------------------------------------------------- */
433 /** Split path string to path elements by URL algorithm.
435 * @param path_str VFS-path
436 * @param flags flags for converter
438 * @return pointer to newly created vfs_path_t object with filled path elements array.
441 static vfs_path_t *
442 vfs_path_from_str_uri_parser (char *path, vfs_path_flag_t flags)
444 vfs_path_t *vpath;
445 vfs_path_element_t *element;
447 char *url_delimiter;
449 vpath = vfs_path_new ();
450 vpath->relative = (flags & VPF_NO_CANON) != 0;
452 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
454 char *vfs_prefix_start;
455 char *real_vfs_prefix_start = url_delimiter;
456 char *slash_pointer;
457 struct vfs_s_subclass *sub = NULL;
459 while (real_vfs_prefix_start > path && *(real_vfs_prefix_start) != PATH_SEP)
460 real_vfs_prefix_start--;
461 vfs_prefix_start = real_vfs_prefix_start;
463 if (*(vfs_prefix_start) == PATH_SEP)
464 vfs_prefix_start += 1;
466 *url_delimiter = '\0';
468 element = g_new0 (vfs_path_element_t, 1);
469 element->class = vfs_prefix_to_class (vfs_prefix_start);
470 element->vfs_prefix = g_strdup (vfs_prefix_start);
472 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
473 sub = VFSDATA (element);
474 if (sub != NULL && (sub->flags & VFS_S_REMOTE) != 0)
476 slash_pointer = strchr (url_delimiter, PATH_SEP);
477 if (slash_pointer == NULL)
479 element->path = g_strdup ("");
481 else
483 element->path = vfs_translate_path_n (slash_pointer + 1);
484 #ifdef HAVE_CHARSET
485 element->encoding = vfs_get_encoding (slash_pointer);
486 #endif
487 *slash_pointer = '\0';
489 vfs_path_url_split (element, url_delimiter);
491 else
493 element->path = vfs_translate_path_n (url_delimiter);
494 #ifdef HAVE_CHARSET
495 element->encoding = vfs_get_encoding (url_delimiter);
496 #endif
498 #ifdef HAVE_CHARSET
499 element->dir.converter =
500 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
501 #endif
502 g_array_prepend_val (vpath->path, element);
504 if ((real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP) ||
505 (real_vfs_prefix_start == path && *(real_vfs_prefix_start) != PATH_SEP))
506 *real_vfs_prefix_start = '\0';
507 else
508 *(real_vfs_prefix_start + 1) = '\0';
511 if (path[0] != '\0')
513 element = g_new0 (vfs_path_element_t, 1);
514 element->class = g_ptr_array_index (vfs__classes_list, 0);
515 element->path = vfs_translate_path_n (path);
516 #ifdef HAVE_CHARSET
517 element->encoding = vfs_get_encoding (path);
518 element->dir.converter =
519 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
520 #endif
521 g_array_prepend_val (vpath->path, element);
524 return vpath;
527 /* --------------------------------------------------------------------------------------------- */
529 * Add element's class info to result string (such as VFS name, host, encoding etc)
530 * This function used as helper only in vfs_path_tokens_get() function
532 * @param element current path element
533 * @param ret_tokens total tikens for return
534 * @param element_tokens accumulated element-only tokens
537 static void
538 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
539 GString * element_tokens)
541 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
542 && element_tokens->len > 0)
544 char *url_str;
546 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
547 g_string_append_c (ret_tokens, PATH_SEP);
549 g_string_append (ret_tokens, element->vfs_prefix);
550 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
552 url_str = vfs_path_build_url_params_str (element, TRUE);
553 if (*url_str != '\0')
555 g_string_append (ret_tokens, url_str);
556 g_string_append_c (ret_tokens, PATH_SEP);
559 g_free (url_str);
562 #ifdef HAVE_CHARSET
563 if (element->encoding != NULL)
565 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
566 g_string_append (ret_tokens, PATH_SEP_STR);
567 g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
568 g_string_append (ret_tokens, element->encoding);
569 g_string_append (ret_tokens, PATH_SEP_STR);
571 #endif
573 g_string_append (ret_tokens, element_tokens->str);
576 /* --------------------------------------------------------------------------------------------- */
578 * Strip path to home dir.
579 * @param dir pointer to string contains full path
582 static char *
583 vfs_path_strip_home (const char *dir)
585 const char *home_dir = mc_config_get_home_dir ();
587 if (home_dir != NULL)
589 size_t len;
591 len = strlen (home_dir);
593 if (strncmp (dir, home_dir, len) == 0 && (dir[len] == PATH_SEP || dir[len] == '\0'))
594 return g_strdup_printf ("~%s", dir + len);
597 return g_strdup (dir);
600 /* --------------------------------------------------------------------------------------------- */
602 /* --------------------------------------------------------------------------------------------- */
603 /*** public functions ****************************************************************************/
604 /* --------------------------------------------------------------------------------------------- */
606 * Convert first elements_count elements from vfs_path_t to string representation with flags.
608 * @param vpath pointer to vfs_path_t object
609 * @param elements_count count of first elements for convert
610 * @param flags flags for converter
612 * @return pointer to newly created string.
615 #define vfs_append_from_path(appendfrom, is_relative) \
617 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
619 char *stripped_home_str; \
620 stripped_home_str = vfs_path_strip_home (appendfrom); \
621 g_string_append (buffer, stripped_home_str); \
622 g_free (stripped_home_str); \
624 else \
626 if ((!is_relative) && (*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
627 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)) \
628 g_string_append_c (buffer, PATH_SEP); \
629 g_string_append (buffer, appendfrom); \
633 char *
634 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
636 int element_index;
637 GString *buffer;
638 GString *recode_buffer;
640 if (vpath == NULL)
641 return NULL;
643 if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
644 elements_count = vfs_path_elements_count (vpath);
646 if (elements_count < 0)
647 elements_count = vfs_path_elements_count (vpath) + elements_count;
649 buffer = g_string_new ("");
650 recode_buffer = g_string_new ("");
652 for (element_index = 0; element_index < elements_count; element_index++)
654 const vfs_path_element_t *element;
655 gboolean is_relative = vpath->relative && (element_index == 0);
657 element = vfs_path_get_by_index (vpath, element_index);
658 if (element->vfs_prefix != NULL)
660 char *url_str;
661 if ((!is_relative) && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
662 g_string_append_c (buffer, PATH_SEP);
664 g_string_append (buffer, element->vfs_prefix);
665 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
667 url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
669 if (*url_str != '\0')
671 g_string_append (buffer, url_str);
672 g_string_append_c (buffer, PATH_SEP);
675 g_free (url_str);
678 #ifdef HAVE_CHARSET
679 if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
681 if ((flags & VPF_HIDE_CHARSET) == 0)
683 if ((!is_relative)
684 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
685 g_string_append (buffer, PATH_SEP_STR);
686 g_string_append (buffer, VFS_ENCODING_PREFIX);
687 g_string_append (buffer, element->encoding);
689 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
690 vfs_append_from_path (recode_buffer->str, is_relative);
691 g_string_set_size (recode_buffer, 0);
693 else
694 #endif
696 vfs_append_from_path (element->path, is_relative);
699 g_string_free (recode_buffer, TRUE);
700 return g_string_free (buffer, FALSE);
703 #undef vfs_append_from_path
705 /* --------------------------------------------------------------------------------------------- */
707 * Convert first elements_count elements from vfs_path_t to string representation.
709 * @param vpath pointer to vfs_path_t object
710 * @param elements_count count of first elements for convert
712 * @return pointer to newly created string.
715 char *
716 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
718 return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
721 /* --------------------------------------------------------------------------------------------- */
723 * Convert vfs_path_t to string representation.
725 * @param vpath pointer to vfs_path_t object
727 * @return pointer to newly created string.
730 char *
731 vfs_path_to_str (const vfs_path_t * vpath)
733 return vfs_path_to_str_elements_count (vpath, vfs_path_elements_count (vpath));
736 /* --------------------------------------------------------------------------------------------- */
738 * Split path string to path elements with flags for change parce process.
740 * @param path_str VFS-path
741 * @param flags flags for parser
743 * @return pointer to newly created vfs_path_t object with filled path elements array.
746 vfs_path_t *
747 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
749 vfs_path_t *vpath;
750 char *path;
752 if (path_str == NULL)
753 return NULL;
755 if ((flags & VPF_NO_CANON) == 0)
756 path = vfs_canon (path_str);
757 else
758 path = g_strdup (path_str);
760 if (path == NULL)
761 return NULL;
763 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
764 vpath = vfs_path_from_str_deprecated_parser (path, flags);
765 else
766 vpath = vfs_path_from_str_uri_parser (path, flags);
768 g_free (path);
770 return vpath;
773 /* --------------------------------------------------------------------------------------------- */
775 * Split path string to path elements.
777 * @param path_str VFS-path
779 * @return pointer to newly created vfs_path_t object with filled path elements array.
782 vfs_path_t *
783 vfs_path_from_str (const char *path_str)
785 return vfs_path_from_str_flags (path_str, VPF_NONE);
788 /* --------------------------------------------------------------------------------------------- */
790 * Create new vfs_path_t object.
792 * @return pointer to newly created vfs_path_t object.
795 vfs_path_t *
796 vfs_path_new (void)
798 vfs_path_t *vpath;
800 vpath = g_new0 (vfs_path_t, 1);
801 vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
803 return vpath;
806 /* --------------------------------------------------------------------------------------------- */
808 * Get count of path elements.
810 * @param vpath pointer to vfs_path_t object
812 * @return count of path elements.
816 vfs_path_elements_count (const vfs_path_t * vpath)
818 return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
821 /* --------------------------------------------------------------------------------------------- */
823 * Add vfs_path_element_t object to end of list in vfs_path_t object
824 * @param vpath pointer to vfs_path_t object
825 * @param path_element pointer to vfs_path_element_t object
828 void
829 vfs_path_add_element (const vfs_path_t * vpath, const vfs_path_element_t * path_element)
831 g_array_append_val (vpath->path, path_element);
834 /* --------------------------------------------------------------------------------------------- */
836 * Get one path element by index.
838 * @param vpath pointer to vfs_path_t object
839 * @param element_index element index. May have negative value (in this case count was started at the end of list).
841 * @return path element.
844 const vfs_path_element_t *
845 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
847 if (vpath == NULL)
848 return NULL;
850 if (element_index < 0)
851 element_index += vfs_path_elements_count (vpath);
853 if (element_index < 0)
854 vfs_die ("vfs_path_get_by_index: incorrect index!");
856 return g_array_index (vpath->path, vfs_path_element_t *, element_index);
859 /* --------------------------------------------------------------------------------------------- */
861 * Clone one path element
863 * @param element pointer to vfs_path_element_t object
865 * @return Newly allocated path element
868 vfs_path_element_t *
869 vfs_path_element_clone (const vfs_path_element_t * element)
871 vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
873 new_element->user = g_strdup (element->user);
874 new_element->password = g_strdup (element->password);
875 new_element->host = g_strdup (element->host);
876 new_element->ipv6 = element->ipv6;
877 new_element->port = element->port;
878 new_element->path = g_strdup (element->path);
879 new_element->class = element->class;
880 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
881 #ifdef HAVE_CHARSET
882 new_element->encoding = g_strdup (element->encoding);
883 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
884 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
885 else
886 new_element->dir.converter = element->dir.converter;
887 #endif
888 new_element->dir.info = element->dir.info;
890 return new_element;
893 /* --------------------------------------------------------------------------------------------- */
895 * Free one path element.
897 * @param element pointer to vfs_path_element_t object
901 void
902 vfs_path_element_free (vfs_path_element_t * element)
904 if (element == NULL)
905 return;
907 g_free (element->user);
908 g_free (element->password);
909 g_free (element->host);
910 g_free (element->path);
911 g_free (element->vfs_prefix);
913 #ifdef HAVE_CHARSET
914 g_free (element->encoding);
916 if (vfs_path_element_need_cleanup_converter (element))
917 str_close_conv (element->dir.converter);
918 #endif
920 g_free (element);
923 /* --------------------------------------------------------------------------------------------- */
925 * Clone path
927 * @param vpath pointer to vfs_path_t object
929 * @return Newly allocated path object
932 vfs_path_t *
933 vfs_path_clone (const vfs_path_t * vpath)
935 vfs_path_t *new_vpath;
936 int vpath_element_index;
938 if (vpath == NULL)
939 return NULL;
941 new_vpath = vfs_path_new ();
942 new_vpath->relative = vpath->relative;
944 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
945 vpath_element_index++)
947 vfs_path_element_t *path_element;
949 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
950 g_array_append_val (new_vpath->path, path_element);
953 return new_vpath;
956 /* --------------------------------------------------------------------------------------------- */
958 * Free vfs_path_t object.
960 * @param vpath pointer to vfs_path_t object
964 void
965 vfs_path_free (vfs_path_t * vpath)
967 int vpath_element_index;
969 if (vpath == NULL)
970 return;
972 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
973 vpath_element_index++)
975 vfs_path_element_t *path_element;
977 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
978 vfs_path_element_free (path_element);
981 g_array_free (vpath->path, TRUE);
982 g_free (vpath);
985 /* --------------------------------------------------------------------------------------------- */
987 * Remove one path element by index
989 * @param vpath pointer to vfs_path_t object
990 * @param element_index element index. May have negative value (in this case count was started at the end of list).
994 void
995 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
997 vfs_path_element_t *element;
999 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
1000 return;
1002 if (element_index < 0)
1003 element_index = vfs_path_elements_count (vpath) + element_index;
1005 element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
1006 vpath->path = g_array_remove_index (vpath->path, element_index);
1007 vfs_path_element_free (element);
1010 /* --------------------------------------------------------------------------------------------- */
1011 /** Return VFS class for the given prefix */
1013 struct vfs_class *
1014 vfs_prefix_to_class (const char *prefix)
1016 guint i;
1018 /* Avoid first class (localfs) that would accept any prefix */
1019 for (i = 1; i < vfs__classes_list->len; i++)
1021 struct vfs_class *vfs;
1023 vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
1024 if (vfs->which != NULL)
1026 if (vfs->which (vfs, prefix) == -1)
1027 continue;
1028 return vfs;
1031 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1032 return vfs;
1035 return NULL;
1038 /* --------------------------------------------------------------------------------------------- */
1040 * Check if need cleanup charset converter for vfs_path_element_t
1042 * @param element part of path
1044 * @return TRUE if need cleanup converter or FALSE otherwise
1046 #ifdef HAVE_CHARSET
1047 gboolean
1048 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
1050 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1052 #endif
1054 /* --------------------------------------------------------------------------------------------- */
1056 * Serialize vfs_path_t object to string
1058 * @param vpath data for serialization
1059 * @param error contain pointer to object for handle error code and message
1061 * @return serialized vpath as newly allocated string
1064 char *
1065 vfs_path_serialize (const vfs_path_t * vpath, GError ** error)
1067 mc_config_t *cpath;
1068 ssize_t element_index;
1069 char *ret_value;
1071 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1073 g_set_error (error, MC_ERROR, -1, "vpath object is empty");
1074 return NULL;
1078 cpath = mc_config_init (NULL, FALSE);
1080 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1082 char *groupname;
1083 const vfs_path_element_t *element;
1085 groupname = g_strdup_printf ("path-element-%zd", element_index);
1086 element = vfs_path_get_by_index (vpath, element_index);
1087 /* convert one element to config group */
1089 mc_config_set_string_raw (cpath, groupname, "path", element->path);
1090 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1091 #ifdef HAVE_CHARSET
1092 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1093 #endif
1094 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1096 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1097 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1098 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1099 if (element->port != 0)
1100 mc_config_set_int (cpath, groupname, "port", element->port);
1102 g_free (groupname);
1105 ret_value = mc_serialize_config (cpath, error);
1106 mc_config_deinit (cpath);
1107 return ret_value;
1110 /* --------------------------------------------------------------------------------------------- */
1112 * Deserialize string to vfs_path_t object
1114 * @param data data for serialization
1115 * @param error contain pointer to object for handle error code and message
1117 * @return newly allocated vfs_path_t object
1120 vfs_path_t *
1121 vfs_path_deserialize (const char *data, GError ** error)
1123 mc_config_t *cpath;
1124 size_t element_index = 0;
1125 vfs_path_t *vpath;
1127 cpath = mc_deserialize_config (data, error);
1128 if (cpath == NULL)
1129 return NULL;
1131 vpath = vfs_path_new ();
1133 while (TRUE)
1135 vfs_path_element_t *element;
1136 char *cfg_value;
1137 char *groupname;
1139 groupname = g_strdup_printf ("path-element-%zd", element_index);
1140 if (!mc_config_has_group (cpath, groupname))
1142 g_free (groupname);
1143 break;
1146 element = g_new0 (vfs_path_element_t, 1);
1148 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1149 element->class = vfs_get_class_by_name (cfg_value);
1150 if (element->class == NULL)
1152 g_free (element);
1153 vfs_path_free (vpath);
1154 g_set_error (error, MC_ERROR, -1, "Unable to find VFS class by name '%s'", cfg_value);
1155 g_free (cfg_value);
1156 mc_config_deinit (cpath);
1157 return NULL;
1159 g_free (cfg_value);
1161 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1163 #ifdef HAVE_CHARSET
1164 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1165 element->dir.converter =
1166 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1167 #endif
1169 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1171 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1172 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1173 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1174 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1176 vpath->path = g_array_append_val (vpath->path, element);
1178 g_free (groupname);
1179 element_index++;
1182 mc_config_deinit (cpath);
1183 if (vfs_path_elements_count (vpath) == 0)
1185 vfs_path_free (vpath);
1186 g_set_error (error, MC_ERROR, -1, "No any path elements found");
1187 return NULL;
1190 return vpath;
1193 /* --------------------------------------------------------------------------------------------- */
1195 * Build vfs_path_t object from arguments.
1197 * @param ... path tokens, terminated by NULL
1199 * @return newly allocated vfs_path_t object
1202 vfs_path_t *
1203 vfs_path_build_filename (const char *first_element, ...)
1205 va_list args;
1206 char *str_path;
1207 vfs_path_t *vpath;
1209 if (first_element == NULL)
1210 return NULL;
1212 va_start (args, first_element);
1213 str_path = mc_build_filenamev (first_element, args);
1214 va_end (args);
1215 vpath = vfs_path_from_str (str_path);
1216 g_free (str_path);
1217 return vpath;
1220 /* --------------------------------------------------------------------------------------------- */
1222 * Append tokens to path object
1224 * @param vpath path object
1225 * @param ... NULL-terminated strings
1227 * @return newly allocated path object
1230 vfs_path_t *
1231 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1233 va_list args;
1234 char *str_path, *result_str;
1235 vfs_path_t *ret_vpath;
1237 if (vpath == NULL || first_element == NULL)
1238 return NULL;
1240 va_start (args, first_element);
1241 str_path = mc_build_filenamev (first_element, args);
1242 va_end (args);
1244 result_str = vfs_path_to_str (vpath);
1245 ret_vpath = vfs_path_build_filename (result_str, str_path, NULL);
1246 g_free (result_str);
1247 g_free (str_path);
1249 return ret_vpath;
1253 /* --------------------------------------------------------------------------------------------- */
1256 * Append vpath_t tokens to path object
1258 * @param ... NULL-terminated vpath objects
1260 * @return newly allocated path object
1263 vfs_path_t *
1264 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1266 va_list args;
1267 vfs_path_t *ret_vpath;
1268 const vfs_path_t *current_vpath = first_vpath;
1270 if (first_vpath == NULL)
1271 return NULL;
1273 ret_vpath = vfs_path_new ();
1275 va_start (args, first_vpath);
1278 int vindex;
1280 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1282 vfs_path_element_t *path_element;
1284 path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1285 g_array_append_val (ret_vpath->path, path_element);
1287 current_vpath = va_arg (args, const vfs_path_t *);
1289 while (current_vpath != NULL);
1290 va_end (args);
1292 return ret_vpath;
1295 /* --------------------------------------------------------------------------------------------- */
1297 * get tockens count in path.
1299 * @param vpath path object
1301 * @return count of tokens
1304 size_t
1305 vfs_path_tokens_count (const vfs_path_t * vpath)
1307 size_t count_tokens = 0;
1308 int element_index;
1310 if (vpath == NULL)
1311 return 0;
1313 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1315 const vfs_path_element_t *element;
1316 char **path_tokens, **iterator;
1318 element = vfs_path_get_by_index (vpath, element_index);
1319 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1321 while (*iterator != NULL)
1323 if (**iterator != '\0')
1324 count_tokens++;
1325 iterator++;
1327 g_strfreev (path_tokens);
1329 return count_tokens;
1332 /* --------------------------------------------------------------------------------------------- */
1334 * Get subpath by tokens
1336 * @param vpath path object
1337 * @param start_position first token for got/ Started from 0.
1338 * If negative, then position will be relative to end of path
1339 * @param length count of tokens
1341 * @return newly allocated string with path tokens separated by slash
1344 char *
1345 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1347 GString *ret_tokens, *element_tokens;
1348 int element_index;
1349 size_t tokens_count = vfs_path_tokens_count (vpath);
1351 if (vpath == NULL)
1352 return NULL;
1354 if (length == 0)
1355 length = tokens_count;
1357 if (length < 0)
1358 length = tokens_count + length;
1360 if (start_position < 0)
1361 start_position = (ssize_t) tokens_count + start_position;
1363 if (start_position < 0)
1364 return NULL;
1366 if (start_position >= (ssize_t) tokens_count)
1367 return NULL;
1369 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1370 length = tokens_count - start_position;
1372 ret_tokens = g_string_sized_new (32);
1373 element_tokens = g_string_sized_new (32);
1375 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1377 const vfs_path_element_t *element;
1378 char **path_tokens, **iterator;
1380 g_string_assign (element_tokens, "");
1381 element = vfs_path_get_by_index (vpath, element_index);
1382 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1384 while (*iterator != NULL)
1386 if (**iterator != '\0')
1388 if (start_position == 0)
1390 if (length == 0)
1392 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1393 g_string_free (element_tokens, TRUE);
1394 g_strfreev (path_tokens);
1395 return g_string_free (ret_tokens, FALSE);
1397 length--;
1398 if (element_tokens->len != 0)
1399 g_string_append_c (element_tokens, PATH_SEP);
1400 g_string_append (element_tokens, *iterator);
1402 else
1403 start_position--;
1405 iterator++;
1407 g_strfreev (path_tokens);
1408 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1411 g_string_free (element_tokens, TRUE);
1412 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1415 /* --------------------------------------------------------------------------------------------- */
1417 * Get subpath by tokens
1419 * @param vpath path object
1420 * @param start_position first token for got/ Started from 0.
1421 * If negative, then position will be relative to end of path
1422 * @param length count of tokens
1424 * @return newly allocated path object with path tokens separated by slash
1427 vfs_path_t *
1428 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1430 char *str_tokens;
1431 vfs_path_t *ret_vpath = NULL;
1433 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1434 if (str_tokens != NULL)
1436 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1437 g_free (str_tokens);
1439 return ret_vpath;
1442 /* --------------------------------------------------------------------------------------------- */
1444 * Build URL parameters (such as user:pass@host:port) from one path element object
1446 * @param element path element
1448 * @return newly allocated string
1451 char *
1452 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1454 GString *buffer;
1456 if (element == NULL)
1457 return NULL;
1459 buffer = g_string_new ("");
1461 if (element->user != NULL)
1462 g_string_append (buffer, element->user);
1464 if (element->password != NULL && keep_password)
1466 g_string_append_c (buffer, ':');
1467 g_string_append (buffer, element->password);
1470 if (element->host != NULL)
1472 if ((element->user != NULL) || (element->password != NULL))
1473 g_string_append_c (buffer, '@');
1474 if (element->ipv6)
1475 g_string_append_c (buffer, '[');
1476 g_string_append (buffer, element->host);
1477 if (element->ipv6)
1478 g_string_append_c (buffer, ']');
1481 if ((element->port) != 0 && (element->host != NULL))
1483 g_string_append_c (buffer, ':');
1484 g_string_append_printf (buffer, "%d", element->port);
1487 return g_string_free (buffer, FALSE);
1490 /* --------------------------------------------------------------------------------------------- */
1492 * Build pretty string representation of one path_element_t object
1494 * @param element path element
1496 * @return newly allocated string
1499 char *
1500 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
1502 char *url_params;
1503 GString *pretty_path;
1505 pretty_path = g_string_new (element->class->prefix);
1506 g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1508 url_params = vfs_path_build_url_params_str (element, FALSE);
1509 g_string_append (pretty_path, url_params);
1510 g_free (url_params);
1512 if (*element->path != PATH_SEP)
1513 g_string_append_c (pretty_path, PATH_SEP);
1515 g_string_append (pretty_path, element->path);
1516 return g_string_free (pretty_path, FALSE);
1519 /* --------------------------------------------------------------------------------------------- */
1521 * Compare two path objects as strings
1523 * @param vpath1 first path object
1524 * @param vpath2 second vpath object
1526 * @return integer value like to strcmp.
1530 vfs_path_cmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1532 char *path1;
1533 char *path2;
1534 int ret_val;
1536 if (vpath1 == NULL || vpath2 == NULL)
1537 return -1;
1539 path1 = vfs_path_to_str (vpath1);
1540 path2 = vfs_path_to_str (vpath2);
1542 ret_val = strcmp (path1, path2);
1544 g_free (path1);
1545 g_free (path2);
1547 return ret_val;
1550 /* --------------------------------------------------------------------------------------------- */
1552 * Compare two path objects as strings
1554 * @param vpath1 first path object
1555 * @param vpath2 second vpath object
1556 * @param len number of first 'len' characters
1558 * @return integer value like to strcmp.
1562 vfs_path_ncmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1564 char *path1;
1565 char *path2;
1566 int ret_val;
1568 if (vpath1 == NULL || vpath2 == NULL)
1569 return -1;
1571 path1 = vfs_path_to_str (vpath1);
1572 path2 = vfs_path_to_str (vpath2);
1574 ret_val = strncmp (path1, path2, len);
1576 g_free (path1);
1577 g_free (path2);
1579 return ret_val;
1582 /* --------------------------------------------------------------------------------------------- */
1584 * Calculate path length in string representation
1586 * @param vpath path object
1588 * @return length of path
1591 size_t
1592 vfs_path_len (const vfs_path_t * vpath)
1594 char *path;
1595 size_t ret_val;
1597 if (vpath == NULL)
1598 return 0;
1600 path = vfs_path_to_str (vpath);
1601 ret_val = strlen (path);
1602 g_free (path);
1603 return ret_val;
1606 /* --------------------------------------------------------------------------------------------- */
1608 * Convert relative vpath object to absolute
1610 * @param vpath path object
1612 * @return absolute path object
1615 vfs_path_t *
1616 vfs_path_to_absolute (const vfs_path_t * vpath)
1618 vfs_path_t *absolute_vpath;
1619 char *path_str;
1621 if (!vpath->relative)
1622 return vfs_path_clone (vpath);
1624 path_str = vfs_path_to_str (vpath);
1625 absolute_vpath = vfs_path_from_str (path_str);
1626 g_free (path_str);
1627 return absolute_vpath;
1630 /* --------------------------------------------------------------------------------------------- */