2 Virtual File System path handlers
4 Copyright (C) 2011-2017
5 Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2011, 2013
9 Andrew Borodin <aborodin@vmail.ru>, 2013
11 This file is part of the Midnight Commander.
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.
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.
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/>.
29 * \brief Source: Virtual File System: path handlers
37 #include "lib/global.h"
38 #include "lib/strutil.h"
39 #include "lib/util.h" /* mc_build_filename() */
40 #include "lib/serialize.h"
44 #include "xdirentry.h"
47 extern GPtrArray
*vfs__classes_list
;
49 /*** global variables ****************************************************************************/
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
61 path_magic (const char *path
)
65 return (stat (path
, &buf
) != 0);
68 /* --------------------------------------------------------------------------------------------- */
71 * Splits path extracting vfs part.
74 * \verbatim /p1#op/inpath \endverbatim
76 * \verbatim inpath,op; \endverbatim
77 * returns which vfs it is.
78 * What is left in path is p1. You still want to g_free(path), you DON'T
79 * want to free neither *inpath nor *op
82 static struct vfs_class
*
83 _vfs_split_with_semi_skip_count (char *path
, const char **inpath
, const char **op
,
88 struct vfs_class
*ret
;
91 vfs_die ("Cannot split NULL");
93 semi
= strrstr_skip_count (path
, "#", skip_count
);
95 if ((semi
== NULL
) || (!path_magic (path
)))
98 slash
= strchr (semi
, PATH_SEP
);
110 ret
= vfs_prefix_to_class (semi
+ 1);
116 *inpath
= slash
!= NULL
? slash
+ 1 : NULL
;
124 ret
= _vfs_split_with_semi_skip_count (path
, inpath
, op
, skip_count
+ 1);
128 /* --------------------------------------------------------------------------------------------- */
130 * remove //, /./ and /../
132 * @return newly allocated string
136 vfs_canon (const char *path
)
141 vfs_die ("Cannot canonicalize NULL");
143 if (!IS_PATH_SEP (*path
))
145 /* Relative to current directory */
149 if (g_str_has_prefix (path
, VFS_ENCODING_PREFIX
))
152 encoding prefix placed at start of string without the leading slash
153 should be autofixed by adding the leading slash
155 local
= mc_build_filename (PATH_SEP_STR
, path
, (char *) NULL
);
159 const char *curr_dir
;
161 curr_dir
= vfs_get_current_dir ();
162 local
= mc_build_filename (curr_dir
, path
, (char *) NULL
);
164 result
= vfs_canon (local
);
171 result
= g_strdup (path
);
172 canonicalize_pathname (result
);
178 /* --------------------------------------------------------------------------------------------- */
181 /** get encoding after last #enc: or NULL, if part does not contain #enc:
183 * @param path null-terminated string
184 * @param len the maximum length of path, where #enc: should be searched
186 * @return newly allocated string.
190 vfs_get_encoding (const char *path
, ssize_t len
)
194 /* try found #enc: */
195 semi
= g_strrstr_len (path
, len
, VFS_ENCODING_PREFIX
);
199 if (semi
== path
|| IS_PATH_SEP (semi
[-1]))
203 semi
+= strlen (VFS_ENCODING_PREFIX
); /* skip "#enc:" */
204 slash
= strchr (semi
, PATH_SEP
);
206 return g_strndup (semi
, slash
- semi
);
207 return g_strdup (semi
);
210 return vfs_get_encoding (path
, semi
- path
);
214 /* --------------------------------------------------------------------------------------------- */
215 /** Extract the hostname and username from the path
217 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
219 * ftp://sunsite.unc.edu/pub/linux
220 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
221 * ftp://tsx-11.mit.edu:8192/
222 * ftp://joe@foo.edu:11321/private
223 * ftp://joe:password@foo.se
225 * @param path_element is an input string to be parsed
226 * @param path is an input string to be parsed
228 * @return g_malloc()ed url info.
229 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
230 * is not set, then the current login name is supplied.
231 * Return value is a g_malloc()ed structure with the pathname relative to the
236 vfs_path_url_split (vfs_path_element_t
* path_element
, const char *path
)
240 char *colon
, *at
, *rest
;
242 path_element
->port
= 0;
244 pcopy
= g_strdup (path
);
245 pend
= pcopy
+ strlen (pcopy
);
247 /* search for any possible user */
248 at
= strrchr (pcopy
, '@');
250 /* We have a username */
258 inner_colon
= strchr (pcopy
, ':');
259 if (inner_colon
!= NULL
)
263 path_element
->password
= g_strdup (inner_colon
);
267 path_element
->user
= g_strdup (pcopy
);
275 /* Check if the host comes with a port spec, if so, chop it */
277 colon
= strchr (rest
, ':');
280 colon
= strchr (++rest
, ']');
286 path_element
->ipv6
= TRUE
;
293 /* cppcheck-suppress invalidscanf */
294 if (sscanf (colon
+ 1, "%d", &path_element
->port
) == 1)
296 if (path_element
->port
<= 0 || path_element
->port
>= 65536)
297 path_element
->port
= 0;
300 while (*(++colon
) != '\0')
305 path_element
->port
= 1;
308 path_element
->port
= 2;
315 path_element
->host
= g_strdup (rest
);
319 /* --------------------------------------------------------------------------------------------- */
321 * get VFS class for the given name
323 * @param class_name name of class
325 * @return pointer to class structure or NULL if class not found
328 static struct vfs_class
*
329 vfs_get_class_by_name (const char *class_name
)
333 if (class_name
== NULL
)
336 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
338 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
339 if ((vfs
->name
!= NULL
) && (strcmp (vfs
->name
, class_name
) == 0))
346 /* --------------------------------------------------------------------------------------------- */
348 * Check if path string contain URL-like elements
350 * @param path_str path
352 * @return TRUE if path is deprecated or FALSE otherwise
356 vfs_path_is_str_path_deprecated (const char *path_str
)
358 return strstr (path_str
, VFS_PATH_URL_DELIMITER
) == NULL
;
361 /* --------------------------------------------------------------------------------------------- */
362 /** Split path string to path elements by deprecated algorithm.
364 * @param path_str VFS-path
366 * @return pointer to newly created vfs_path_t object with filled path elements array.
370 vfs_path_from_str_deprecated_parser (char *path
, vfs_path_flag_t flags
)
373 vfs_path_element_t
*element
;
374 struct vfs_class
*class;
375 const char *local
, *op
;
378 vpath
= vfs_path_new ();
380 while ((class = _vfs_split_with_semi_skip_count (path
, &local
, &op
, 0)) != NULL
)
383 element
= g_new0 (vfs_path_element_t
, 1);
384 element
->class = class;
387 element
->path
= vfs_translate_path_n (local
);
390 element
->encoding
= vfs_get_encoding (local
, -1);
391 element
->dir
.converter
=
392 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
395 url_params
= strchr (op
, ':'); /* skip VFS prefix */
396 if (url_params
!= NULL
)
400 vfs_path_url_split (element
, url_params
);
404 element
->vfs_prefix
= g_strdup (op
);
406 g_array_prepend_val (vpath
->path
, element
);
410 element
= g_new0 (vfs_path_element_t
, 1);
411 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
412 element
->path
= vfs_translate_path_n (path
);
415 element
->encoding
= vfs_get_encoding (path
, -1);
416 element
->dir
.converter
=
417 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
419 g_array_prepend_val (vpath
->path
, element
);
425 /* --------------------------------------------------------------------------------------------- */
426 /** Split path string to path elements by URL algorithm.
428 * @param path_str VFS-path
429 * @param flags flags for converter
431 * @return pointer to newly created vfs_path_t object with filled path elements array.
435 vfs_path_from_str_uri_parser (char *path
, vfs_path_flag_t flags
)
438 vfs_path_element_t
*element
;
443 vpath
= vfs_path_new ();
444 vpath
->relative
= path
!= NULL
&& !IS_PATH_SEP (*path
);
446 while ((url_delimiter
= g_strrstr (path
, VFS_PATH_URL_DELIMITER
)) != NULL
)
448 char *vfs_prefix_start
;
449 char *real_vfs_prefix_start
= url_delimiter
;
450 struct vfs_s_subclass
*sub
= NULL
;
452 while (real_vfs_prefix_start
> path
&& !IS_PATH_SEP (*real_vfs_prefix_start
))
453 real_vfs_prefix_start
--;
454 vfs_prefix_start
= real_vfs_prefix_start
;
456 if (IS_PATH_SEP (*vfs_prefix_start
))
457 vfs_prefix_start
+= 1;
459 *url_delimiter
= '\0';
461 element
= g_new0 (vfs_path_element_t
, 1);
462 element
->class = vfs_prefix_to_class (vfs_prefix_start
);
463 element
->vfs_prefix
= g_strdup (vfs_prefix_start
);
465 url_delimiter
+= strlen (VFS_PATH_URL_DELIMITER
);
466 sub
= VFSDATA (element
);
467 if (sub
!= NULL
&& (sub
->flags
& VFS_S_REMOTE
) != 0)
471 slash_pointer
= strchr (url_delimiter
, PATH_SEP
);
472 if (slash_pointer
== NULL
)
474 element
->path
= g_strdup ("");
478 element
->path
= vfs_translate_path_n (slash_pointer
+ 1);
480 element
->encoding
= vfs_get_encoding (slash_pointer
, -1);
482 *slash_pointer
= '\0';
484 vfs_path_url_split (element
, url_delimiter
);
488 element
->path
= vfs_translate_path_n (url_delimiter
);
490 element
->encoding
= vfs_get_encoding (url_delimiter
, -1);
494 element
->dir
.converter
=
495 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
497 g_array_prepend_val (vpath
->path
, element
);
499 if ((real_vfs_prefix_start
> path
&& IS_PATH_SEP (*real_vfs_prefix_start
)) ||
500 (real_vfs_prefix_start
== path
&& !IS_PATH_SEP (*real_vfs_prefix_start
)))
501 *real_vfs_prefix_start
= '\0';
503 *(real_vfs_prefix_start
+ 1) = '\0';
508 element
= g_new0 (vfs_path_element_t
, 1);
509 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
510 element
->path
= vfs_translate_path_n (path
);
512 element
->encoding
= vfs_get_encoding (path
, -1);
513 element
->dir
.converter
=
514 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
516 g_array_prepend_val (vpath
->path
, element
);
522 /* --------------------------------------------------------------------------------------------- */
524 * Add element's class info to result string (such as VFS name, host, encoding etc)
525 * This function used as helper only in vfs_path_tokens_get() function
527 * @param element current path element
528 * @param ret_tokens total tikens for return
529 * @param element_tokens accumulated element-only tokens
533 vfs_path_tokens_add_class_info (const vfs_path_element_t
* element
, GString
* ret_tokens
,
534 GString
* element_tokens
)
536 if (((element
->class->flags
& VFSF_LOCAL
) == 0 || ret_tokens
->len
> 0)
537 && element_tokens
->len
> 0)
541 if (ret_tokens
->len
> 0 && !IS_PATH_SEP (ret_tokens
->str
[ret_tokens
->len
- 1]))
542 g_string_append_c (ret_tokens
, PATH_SEP
);
544 g_string_append (ret_tokens
, element
->vfs_prefix
);
545 g_string_append (ret_tokens
, VFS_PATH_URL_DELIMITER
);
547 url_str
= vfs_path_build_url_params_str (element
, TRUE
);
548 if (*url_str
!= '\0')
550 g_string_append (ret_tokens
, url_str
);
551 g_string_append_c (ret_tokens
, PATH_SEP
);
558 if (element
->encoding
!= NULL
)
560 if (ret_tokens
->len
> 0 && !IS_PATH_SEP (ret_tokens
->str
[ret_tokens
->len
- 1]))
561 g_string_append (ret_tokens
, PATH_SEP_STR
);
562 g_string_append (ret_tokens
, VFS_ENCODING_PREFIX
);
563 g_string_append (ret_tokens
, element
->encoding
);
564 g_string_append (ret_tokens
, PATH_SEP_STR
);
568 g_string_append (ret_tokens
, element_tokens
->str
);
571 /* --------------------------------------------------------------------------------------------- */
573 * Strip path to home dir.
574 * @param dir pointer to string contains full path
578 vfs_path_strip_home (const char *dir
)
580 const char *home_dir
= mc_config_get_home_dir ();
582 if (home_dir
!= NULL
)
586 len
= strlen (home_dir
);
588 if (strncmp (dir
, home_dir
, len
) == 0 && (IS_PATH_SEP (dir
[len
]) || dir
[len
] == '\0'))
589 return g_strdup_printf ("~%s", dir
+ len
);
592 return g_strdup (dir
);
595 /* --------------------------------------------------------------------------------------------- */
596 /*** public functions ****************************************************************************/
597 /* --------------------------------------------------------------------------------------------- */
599 #define vfs_append_from_path(appendfrom, is_relative) \
601 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
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); \
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); \
618 * Convert first elements_count elements from vfs_path_t to string representation with flags.
620 * @param vpath pointer to vfs_path_t object
621 * @param elements_count count of first elements for convert
622 * @param flags for converter
624 * @return pointer to newly created string.
628 vfs_path_to_str_flags (const vfs_path_t
* vpath
, int elements_count
, vfs_path_flag_t flags
)
632 GString
*recode_buffer
;
637 if (elements_count
== 0 || elements_count
> vfs_path_elements_count (vpath
))
638 elements_count
= vfs_path_elements_count (vpath
);
640 if (elements_count
< 0)
641 elements_count
= vfs_path_elements_count (vpath
) + elements_count
;
643 buffer
= g_string_new ("");
644 recode_buffer
= g_string_new ("");
646 for (element_index
= 0; element_index
< elements_count
; element_index
++)
648 const vfs_path_element_t
*element
;
649 gboolean is_relative
= vpath
->relative
&& (element_index
== 0);
651 element
= vfs_path_get_by_index (vpath
, element_index
);
652 if (element
->vfs_prefix
!= NULL
)
655 if (!is_relative
&& (buffer
->len
== 0 || !IS_PATH_SEP (buffer
->str
[buffer
->len
- 1])))
656 g_string_append_c (buffer
, PATH_SEP
);
658 g_string_append (buffer
, element
->vfs_prefix
);
659 g_string_append (buffer
, VFS_PATH_URL_DELIMITER
);
661 url_str
= vfs_path_build_url_params_str (element
, !(flags
& VPF_STRIP_PASSWORD
));
663 if (*url_str
!= '\0')
665 g_string_append (buffer
, url_str
);
666 g_string_append_c (buffer
, PATH_SEP
);
673 if ((flags
& VPF_RECODE
) == 0 && vfs_path_element_need_cleanup_converter (element
))
675 if ((flags
& VPF_HIDE_CHARSET
) == 0)
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
);
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);
690 vfs_append_from_path (element
->path
, is_relative
);
693 g_string_free (recode_buffer
, TRUE
);
694 return g_string_free (buffer
, FALSE
);
697 #undef vfs_append_from_path
699 /* --------------------------------------------------------------------------------------------- */
701 * Convert first elements_count elements from vfs_path_t to string representation.
703 * @param vpath pointer to vfs_path_t object
704 * @param elements_count count of first elements for convert
706 * @return pointer to newly created string.
710 vfs_path_to_str_elements_count (const vfs_path_t
* vpath
, int elements_count
)
712 return vfs_path_to_str_flags (vpath
, elements_count
, VPF_NONE
);
715 /* --------------------------------------------------------------------------------------------- */
717 * Split path string to path elements with flags for change parce process.
719 * @param path_str VFS-path
720 * @param flags flags for parser
722 * @return pointer to newly created vfs_path_t object with filled path elements array.
726 vfs_path_from_str_flags (const char *path_str
, vfs_path_flag_t flags
)
731 if (path_str
== NULL
)
734 if ((flags
& VPF_NO_CANON
) == 0)
735 path
= vfs_canon (path_str
);
737 path
= g_strdup (path_str
);
742 if ((flags
& VPF_USE_DEPRECATED_PARSER
) != 0 && vfs_path_is_str_path_deprecated (path
))
743 vpath
= vfs_path_from_str_deprecated_parser (path
, flags
);
745 vpath
= vfs_path_from_str_uri_parser (path
, flags
);
747 vpath
->str
= vfs_path_to_str_flags (vpath
, 0, flags
);
753 /* --------------------------------------------------------------------------------------------- */
755 * Split path string to path elements.
757 * @param path_str VFS-path
759 * @return pointer to newly created vfs_path_t object with filled path elements array.
763 vfs_path_from_str (const char *path_str
)
765 return vfs_path_from_str_flags (path_str
, VPF_NONE
);
768 /* --------------------------------------------------------------------------------------------- */
770 * Create new vfs_path_t object.
772 * @return pointer to newly created vfs_path_t object.
780 vpath
= g_new0 (vfs_path_t
, 1);
781 vpath
->path
= g_array_new (FALSE
, TRUE
, sizeof (vfs_path_element_t
*));
786 /* --------------------------------------------------------------------------------------------- */
788 * Get count of path elements.
790 * @param vpath pointer to vfs_path_t object
792 * @return count of path elements.
796 vfs_path_elements_count (const vfs_path_t
* vpath
)
798 return (vpath
!= NULL
&& vpath
->path
!= NULL
) ? vpath
->path
->len
: 0;
801 /* --------------------------------------------------------------------------------------------- */
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
809 vfs_path_add_element (vfs_path_t
* vpath
, const vfs_path_element_t
* path_element
)
811 g_array_append_val (vpath
->path
, path_element
);
813 vpath
->str
= vfs_path_to_str_flags (vpath
, 0, VPF_NONE
);
816 /* --------------------------------------------------------------------------------------------- */
818 * Get one path element by index.
820 * @param vpath pointer to vfs_path_t object
821 * @param element_index element index. May have negative value (in this case count was started at the end of list).
823 * @return path element.
826 const vfs_path_element_t
*
827 vfs_path_get_by_index (const vfs_path_t
* vpath
, int element_index
)
832 if (element_index
< 0)
833 element_index
+= vfs_path_elements_count (vpath
);
835 if (element_index
< 0)
836 vfs_die ("vfs_path_get_by_index: incorrect index!");
838 return g_array_index (vpath
->path
, vfs_path_element_t
*, element_index
);
841 /* --------------------------------------------------------------------------------------------- */
843 * Clone one path element
845 * @param element pointer to vfs_path_element_t object
847 * @return Newly allocated path element
851 vfs_path_element_clone (const vfs_path_element_t
* element
)
853 vfs_path_element_t
*new_element
= g_new (vfs_path_element_t
, 1);
855 new_element
->user
= g_strdup (element
->user
);
856 new_element
->password
= g_strdup (element
->password
);
857 new_element
->host
= g_strdup (element
->host
);
858 new_element
->ipv6
= element
->ipv6
;
859 new_element
->port
= element
->port
;
860 new_element
->path
= g_strdup (element
->path
);
861 new_element
->class = element
->class;
862 new_element
->vfs_prefix
= g_strdup (element
->vfs_prefix
);
864 new_element
->encoding
= g_strdup (element
->encoding
);
865 if (vfs_path_element_need_cleanup_converter (element
) && new_element
->encoding
!= NULL
)
866 new_element
->dir
.converter
= str_crt_conv_from (new_element
->encoding
);
868 new_element
->dir
.converter
= element
->dir
.converter
;
870 new_element
->dir
.info
= element
->dir
.info
;
875 /* --------------------------------------------------------------------------------------------- */
877 * Free one path element.
879 * @param element pointer to vfs_path_element_t object
884 vfs_path_element_free (vfs_path_element_t
* element
)
889 g_free (element
->user
);
890 g_free (element
->password
);
891 g_free (element
->host
);
892 g_free (element
->path
);
893 g_free (element
->vfs_prefix
);
896 g_free (element
->encoding
);
898 if (vfs_path_element_need_cleanup_converter (element
))
899 str_close_conv (element
->dir
.converter
);
905 /* --------------------------------------------------------------------------------------------- */
909 * @param vpath pointer to vfs_path_t object
911 * @return Newly allocated path object
915 vfs_path_clone (const vfs_path_t
* vpath
)
917 vfs_path_t
*new_vpath
;
918 int vpath_element_index
;
923 new_vpath
= vfs_path_new ();
924 new_vpath
->relative
= vpath
->relative
;
926 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
927 vpath_element_index
++)
929 vfs_path_element_t
*path_element
;
931 path_element
= vfs_path_element_clone (vfs_path_get_by_index (vpath
, vpath_element_index
));
932 g_array_append_val (new_vpath
->path
, path_element
);
934 new_vpath
->str
= g_strdup (vpath
->str
);
939 /* --------------------------------------------------------------------------------------------- */
941 * Free vfs_path_t object.
943 * @param vpath pointer to vfs_path_t object
948 vfs_path_free (vfs_path_t
* vpath
)
950 int vpath_element_index
;
955 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
956 vpath_element_index
++)
958 vfs_path_element_t
*path_element
;
960 path_element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, vpath_element_index
);
961 vfs_path_element_free (path_element
);
964 g_array_free (vpath
->path
, TRUE
);
969 /* --------------------------------------------------------------------------------------------- */
971 * Remove one path element by index
973 * @param vpath pointer to vfs_path_t object
974 * @param element_index element index. May have negative value (in this case count was started at the end of list).
979 vfs_path_remove_element_by_index (vfs_path_t
* vpath
, int element_index
)
981 vfs_path_element_t
*element
;
983 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 1))
986 if (element_index
< 0)
987 element_index
= vfs_path_elements_count (vpath
) + element_index
;
989 element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, element_index
);
990 vpath
->path
= g_array_remove_index (vpath
->path
, element_index
);
991 vfs_path_element_free (element
);
993 vpath
->str
= vfs_path_to_str_flags (vpath
, 0, VPF_NONE
);
996 /* --------------------------------------------------------------------------------------------- */
997 /** Return VFS class for the given prefix */
1000 vfs_prefix_to_class (const char *prefix
)
1004 /* Avoid first class (localfs) that would accept any prefix */
1005 for (i
= 1; i
< vfs__classes_list
->len
; i
++)
1007 struct vfs_class
*vfs
;
1009 vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
1010 if (vfs
->which
!= NULL
)
1012 if (vfs
->which (vfs
, prefix
) == -1)
1017 if (vfs
->prefix
!= NULL
&& strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)) == 0)
1024 /* --------------------------------------------------------------------------------------------- */
1029 * Check if need cleanup charset converter for vfs_path_element_t
1031 * @param element part of path
1033 * @return TRUE if need cleanup converter or FALSE otherwise
1037 vfs_path_element_need_cleanup_converter (const vfs_path_element_t
* element
)
1039 return (element
->dir
.converter
!= str_cnv_from_term
&& element
->dir
.converter
!= INVALID_CONV
);
1042 /* --------------------------------------------------------------------------------------------- */
1044 * Change encoding for last part (vfs_path_element_t) of vpath
1046 * @param vpath pointer to path structure
1047 * encoding name of charset
1049 * @return pointer to path structure (for use function in anoter functions)
1052 vfs_path_change_encoding (vfs_path_t
* vpath
, const char *encoding
)
1054 vfs_path_element_t
*path_element
;
1056 path_element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, -1);
1057 /* don't add current encoding */
1058 if ((path_element
->encoding
!= NULL
) && (strcmp (encoding
, path_element
->encoding
) == 0))
1061 g_free (path_element
->encoding
);
1062 path_element
->encoding
= g_strdup (encoding
);
1064 if (vfs_path_element_need_cleanup_converter (path_element
))
1065 str_close_conv (path_element
->dir
.converter
);
1067 path_element
->dir
.converter
= str_crt_conv_from (path_element
->encoding
);
1069 g_free (vpath
->str
);
1070 vpath
->str
= vfs_path_to_str_flags (vpath
, 0, VPF_NONE
);
1076 /* --------------------------------------------------------------------------------------------- */
1079 * Serialize vfs_path_t object to string
1081 * @param vpath data for serialization
1082 * @param error contain pointer to object for handle error code and message
1084 * @return serialized vpath as newly allocated string
1088 vfs_path_serialize (const vfs_path_t
* vpath
, GError
** mcerror
)
1091 ssize_t element_index
;
1094 mc_return_val_if_error (mcerror
, FALSE
);
1096 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 0))
1098 mc_propagate_error (mcerror
, 0, "%s", "vpath object is empty");
1102 cpath
= mc_config_init (NULL
, FALSE
);
1104 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1106 char groupname
[BUF_TINY
];
1107 const vfs_path_element_t
*element
;
1109 g_snprintf (groupname
, sizeof (groupname
), "path-element-%zd", element_index
);
1110 element
= vfs_path_get_by_index (vpath
, element_index
);
1111 /* convert one element to config group */
1113 mc_config_set_string_raw (cpath
, groupname
, "path", element
->path
);
1114 mc_config_set_string_raw (cpath
, groupname
, "class-name", element
->class->name
);
1116 mc_config_set_string_raw (cpath
, groupname
, "encoding", element
->encoding
);
1118 mc_config_set_string_raw (cpath
, groupname
, "vfs_prefix", element
->vfs_prefix
);
1120 mc_config_set_string_raw (cpath
, groupname
, "user", element
->user
);
1121 mc_config_set_string_raw (cpath
, groupname
, "password", element
->password
);
1122 mc_config_set_string_raw (cpath
, groupname
, "host", element
->host
);
1123 if (element
->port
!= 0)
1124 mc_config_set_int (cpath
, groupname
, "port", element
->port
);
1127 ret_value
= mc_serialize_config (cpath
, mcerror
);
1128 mc_config_deinit (cpath
);
1132 /* --------------------------------------------------------------------------------------------- */
1134 * Deserialize string to vfs_path_t object
1136 * @param data data for serialization
1137 * @param error contain pointer to object for handle error code and message
1139 * @return newly allocated vfs_path_t object
1143 vfs_path_deserialize (const char *data
, GError
** mcerror
)
1146 size_t element_index
;
1149 mc_return_val_if_error (mcerror
, FALSE
);
1151 cpath
= mc_deserialize_config (data
, mcerror
);
1155 vpath
= vfs_path_new ();
1157 for (element_index
= 0;; element_index
++)
1159 struct vfs_class
*eclass
;
1160 vfs_path_element_t
*element
;
1162 char groupname
[BUF_TINY
];
1164 g_snprintf (groupname
, sizeof (groupname
), "path-element-%zu", element_index
);
1165 if (!mc_config_has_group (cpath
, groupname
))
1168 cfg_value
= mc_config_get_string_raw (cpath
, groupname
, "class-name", NULL
);
1169 eclass
= vfs_get_class_by_name (cfg_value
);
1172 vfs_path_free (vpath
);
1173 g_set_error (mcerror
, MC_ERROR
, 0, "Unable to find VFS class by name '%s'", cfg_value
);
1175 mc_config_deinit (cpath
);
1180 element
= g_new0 (vfs_path_element_t
, 1);
1181 element
->class = eclass
;
1182 element
->path
= mc_config_get_string_raw (cpath
, groupname
, "path", NULL
);
1185 element
->encoding
= mc_config_get_string_raw (cpath
, groupname
, "encoding", NULL
);
1186 element
->dir
.converter
=
1187 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
1190 element
->vfs_prefix
= mc_config_get_string_raw (cpath
, groupname
, "vfs_prefix", NULL
);
1192 element
->user
= mc_config_get_string_raw (cpath
, groupname
, "user", NULL
);
1193 element
->password
= mc_config_get_string_raw (cpath
, groupname
, "password", NULL
);
1194 element
->host
= mc_config_get_string_raw (cpath
, groupname
, "host", NULL
);
1195 element
->port
= mc_config_get_int (cpath
, groupname
, "port", 0);
1197 vpath
->path
= g_array_append_val (vpath
->path
, element
);
1200 mc_config_deinit (cpath
);
1201 if (vfs_path_elements_count (vpath
) == 0)
1203 vfs_path_free (vpath
);
1204 g_set_error (mcerror
, MC_ERROR
, 0, "No any path elements found");
1207 vpath
->str
= vfs_path_to_str_flags (vpath
, 0, VPF_NONE
);
1212 /* --------------------------------------------------------------------------------------------- */
1214 * Build vfs_path_t object from arguments.
1216 * @param first_element of path
1217 * @param ... path tokens, terminated by NULL
1219 * @return newly allocated vfs_path_t object
1223 vfs_path_build_filename (const char *first_element
, ...)
1229 if (first_element
== NULL
)
1232 va_start (args
, first_element
);
1233 str_path
= mc_build_filenamev (first_element
, args
);
1235 vpath
= vfs_path_from_str (str_path
);
1240 /* --------------------------------------------------------------------------------------------- */
1242 * Append tokens to path object
1244 * @param vpath path object
1245 * @param first_element of path
1246 * @param ... NULL-terminated strings
1248 * @return newly allocated path object
1252 vfs_path_append_new (const vfs_path_t
* vpath
, const char *first_element
, ...)
1256 const char *result_str
;
1257 vfs_path_t
*ret_vpath
;
1259 if (vpath
== NULL
|| first_element
== NULL
)
1262 va_start (args
, first_element
);
1263 str_path
= mc_build_filenamev (first_element
, args
);
1266 result_str
= vfs_path_as_str (vpath
);
1267 ret_vpath
= vfs_path_build_filename (result_str
, str_path
, (char *) NULL
);
1274 /* --------------------------------------------------------------------------------------------- */
1277 * Append vpath_t tokens to path object
1279 * @param first_vpath vpath objects
1280 * @param ... NULL-terminated vpath objects
1282 * @return newly allocated path object
1286 vfs_path_append_vpath_new (const vfs_path_t
* first_vpath
, ...)
1289 vfs_path_t
*ret_vpath
;
1290 const vfs_path_t
*current_vpath
= first_vpath
;
1292 if (first_vpath
== NULL
)
1295 ret_vpath
= vfs_path_new ();
1297 va_start (args
, first_vpath
);
1302 for (vindex
= 0; vindex
< vfs_path_elements_count (current_vpath
); vindex
++)
1304 vfs_path_element_t
*path_element
;
1306 path_element
= vfs_path_element_clone (vfs_path_get_by_index (current_vpath
, vindex
));
1307 g_array_append_val (ret_vpath
->path
, path_element
);
1309 current_vpath
= va_arg (args
, const vfs_path_t
*);
1311 while (current_vpath
!= NULL
);
1314 ret_vpath
->str
= vfs_path_to_str_flags (ret_vpath
, 0, VPF_NONE
);
1319 /* --------------------------------------------------------------------------------------------- */
1322 * get tockens count in path.
1324 * @param vpath path object
1326 * @return count of tokens
1330 vfs_path_tokens_count (const vfs_path_t
* vpath
)
1332 size_t count_tokens
= 0;
1338 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1340 const vfs_path_element_t
*element
;
1341 const char *token
, *prev_token
;
1343 element
= vfs_path_get_by_index (vpath
, element_index
);
1345 for (prev_token
= element
->path
; (token
= strchr (prev_token
, PATH_SEP
)) != NULL
;
1346 prev_token
= token
+ 1)
1348 /* skip empty substring */
1349 if (token
!= prev_token
)
1353 if (*prev_token
!= '\0')
1357 return count_tokens
;
1360 /* --------------------------------------------------------------------------------------------- */
1363 * Get subpath by tokens
1365 * @param vpath path object
1366 * @param start_position first token for got/ Started from 0.
1367 * If negative, then position will be relative to end of path
1368 * @param length count of tokens
1370 * @return newly allocated string with path tokens separated by slash
1374 vfs_path_tokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1376 GString
*ret_tokens
, *element_tokens
;
1378 size_t tokens_count
= vfs_path_tokens_count (vpath
);
1384 length
= tokens_count
;
1387 length
= tokens_count
+ length
;
1389 if (start_position
< 0)
1390 start_position
= (ssize_t
) tokens_count
+ start_position
;
1392 if (start_position
< 0)
1395 if (start_position
>= (ssize_t
) tokens_count
)
1398 if (start_position
+ (ssize_t
) length
> (ssize_t
) tokens_count
)
1399 length
= tokens_count
- start_position
;
1401 ret_tokens
= g_string_sized_new (32);
1402 element_tokens
= g_string_sized_new (32);
1404 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1406 const vfs_path_element_t
*element
;
1407 char **path_tokens
, **iterator
;
1409 g_string_assign (element_tokens
, "");
1410 element
= vfs_path_get_by_index (vpath
, element_index
);
1411 path_tokens
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1413 for (iterator
= path_tokens
; *iterator
!= NULL
; iterator
++)
1415 if (**iterator
!= '\0')
1417 if (start_position
== 0)
1421 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1422 g_string_free (element_tokens
, TRUE
);
1423 g_strfreev (path_tokens
);
1424 return g_string_free (ret_tokens
, FALSE
);
1427 if (element_tokens
->len
!= 0)
1428 g_string_append_c (element_tokens
, PATH_SEP
);
1429 g_string_append (element_tokens
, *iterator
);
1435 g_strfreev (path_tokens
);
1436 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1439 g_string_free (element_tokens
, TRUE
);
1440 return g_string_free (ret_tokens
, !(start_position
== 0 && length
== 0));
1443 /* --------------------------------------------------------------------------------------------- */
1445 * Get subpath by tokens
1447 * @param vpath path object
1448 * @param start_position first token for got/ Started from 0.
1449 * If negative, then position will be relative to end of path
1450 * @param length count of tokens
1452 * @return newly allocated path object with path tokens separated by slash
1456 vfs_path_vtokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1459 vfs_path_t
*ret_vpath
= NULL
;
1461 str_tokens
= vfs_path_tokens_get (vpath
, start_position
, length
);
1462 if (str_tokens
!= NULL
)
1464 ret_vpath
= vfs_path_from_str_flags (str_tokens
, VPF_NO_CANON
);
1465 g_free (str_tokens
);
1470 /* --------------------------------------------------------------------------------------------- */
1473 * Build URL parameters (such as user:pass @ host:port) from one path element object
1475 * @param element path element
1476 * @param keep_password TRUE or FALSE
1478 * @return newly allocated string
1482 vfs_path_build_url_params_str (const vfs_path_element_t
* element
, gboolean keep_password
)
1486 if (element
== NULL
)
1489 buffer
= g_string_new ("");
1491 if (element
->user
!= NULL
)
1492 g_string_append (buffer
, element
->user
);
1494 if (element
->password
!= NULL
&& keep_password
)
1496 g_string_append_c (buffer
, ':');
1497 g_string_append (buffer
, element
->password
);
1500 if (element
->host
!= NULL
)
1502 if ((element
->user
!= NULL
) || (element
->password
!= NULL
))
1503 g_string_append_c (buffer
, '@');
1505 g_string_append_c (buffer
, '[');
1506 g_string_append (buffer
, element
->host
);
1508 g_string_append_c (buffer
, ']');
1511 if ((element
->port
) != 0 && (element
->host
!= NULL
))
1513 g_string_append_c (buffer
, ':');
1514 g_string_append_printf (buffer
, "%d", element
->port
);
1517 return g_string_free (buffer
, FALSE
);
1520 /* --------------------------------------------------------------------------------------------- */
1522 * Build pretty string representation of one path_element_t object
1524 * @param element path element
1526 * @return newly allocated string
1530 vfs_path_element_build_pretty_path_str (const vfs_path_element_t
* element
)
1533 GString
*pretty_path
;
1535 pretty_path
= g_string_new (element
->class->prefix
);
1536 g_string_append (pretty_path
, VFS_PATH_URL_DELIMITER
);
1538 url_params
= vfs_path_build_url_params_str (element
, FALSE
);
1539 g_string_append (pretty_path
, url_params
);
1540 g_free (url_params
);
1542 if (!IS_PATH_SEP (*element
->path
))
1543 g_string_append_c (pretty_path
, PATH_SEP
);
1545 g_string_append (pretty_path
, element
->path
);
1546 return g_string_free (pretty_path
, FALSE
);
1549 /* --------------------------------------------------------------------------------------------- */
1551 * Compare two path objects as strings
1553 * @param vpath1 first path object
1554 * @param vpath2 second vpath object
1556 * @return integer value like to strcmp.
1560 vfs_path_equal (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
1562 const char *path1
, *path2
;
1565 if (vpath1
== NULL
|| vpath2
== NULL
)
1568 path1
= vfs_path_as_str (vpath1
);
1569 path2
= vfs_path_as_str (vpath2
);
1571 ret_val
= strcmp (path1
, path2
) == 0;
1576 /* --------------------------------------------------------------------------------------------- */
1578 * Compare two path objects as strings
1580 * @param vpath1 first path object
1581 * @param vpath2 second vpath object
1582 * @param len number of first 'len' characters
1584 * @return integer value like to strcmp.
1588 vfs_path_equal_len (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
, size_t len
)
1590 const char *path1
, *path2
;
1593 if (vpath1
== NULL
|| vpath2
== NULL
)
1596 path1
= vfs_path_as_str (vpath1
);
1597 path2
= vfs_path_as_str (vpath2
);
1599 ret_val
= strncmp (path1
, path2
, len
) == 0;
1604 /* --------------------------------------------------------------------------------------------- */
1606 * Calculate path length in string representation
1608 * @param vpath path object
1610 * @return length of path
1614 vfs_path_len (const vfs_path_t
* vpath
)
1619 return strlen (vpath
->str
);
1622 /* --------------------------------------------------------------------------------------------- */
1624 * Convert relative vpath object to absolute
1626 * @param vpath path object
1628 * @return absolute path object
1632 vfs_path_to_absolute (const vfs_path_t
* vpath
)
1634 vfs_path_t
*absolute_vpath
;
1635 const char *path_str
;
1637 if (!vpath
->relative
)
1638 return vfs_path_clone (vpath
);
1640 path_str
= vfs_path_as_str (vpath
);
1641 absolute_vpath
= vfs_path_from_str (path_str
);
1642 return absolute_vpath
;
1645 /* --------------------------------------------------------------------------------------------- */