2 Virtual File System path handlers
5 The Free Software Foundation, Inc.
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/>.
28 * \brief Source: Virtual File System: path handlers
36 #include "lib/global.h"
37 #include "lib/strutil.h"
38 #include "lib/util.h" /* mc_build_filename() */
39 #include "lib/serialize.h"
43 #include "xdirentry.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 /* --------------------------------------------------------------------------------------------- */
60 path_magic (const char *path
)
64 return (stat (path
, &buf
) != 0);
67 /* --------------------------------------------------------------------------------------------- */
70 * Splits path extracting vfs part.
73 * \verbatim /p1#op/inpath \endverbatim
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
,
87 struct vfs_class
*ret
;
90 vfs_die ("Cannot split NULL");
92 semi
= strrstr_skip_count (path
, "#", skip_count
);
94 if ((semi
== NULL
) || (!path_magic (path
)))
97 slash
= strchr (semi
, PATH_SEP
);
109 ret
= vfs_prefix_to_class (semi
+ 1);
115 *inpath
= slash
!= NULL
? slash
+ 1 : NULL
;
123 ret
= _vfs_split_with_semi_skip_count (path
, inpath
, op
, skip_count
+ 1);
127 /* --------------------------------------------------------------------------------------------- */
129 * remove //, /./ and /../
131 * @return newly allocated string
135 vfs_canon (const char *path
)
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
);
157 curr_dir
= vfs_get_current_dir ();
158 local
= mc_build_filename (curr_dir
, path
, NULL
);
161 result
= vfs_canon (local
);
167 * So we have path of following form:
168 * /p1/p2#op/.././././p3#op/p4. Good luck.
173 result
= g_strdup (path
);
174 canonicalize_pathname (result
);
179 /* --------------------------------------------------------------------------------------------- */
182 /** get encoding after last #enc: or NULL, if part does not contain #enc:
186 * @return newly allocated string.
190 vfs_get_encoding (const char *path
)
196 work
= g_strdup (path
);
198 /* try found #enc: */
199 semi
= g_strrstr (work
, VFS_ENCODING_PREFIX
);
201 if (semi
!= NULL
&& (semi
== work
|| *(semi
- 1) == PATH_SEP
))
203 semi
+= strlen (VFS_ENCODING_PREFIX
); /* skip "#enc:" */
204 slash
= strchr (semi
, PATH_SEP
);
208 g_strlcpy (result
, semi
, sizeof (result
));
210 return g_strdup (result
);
220 /* --------------------------------------------------------------------------------------------- */
221 /** Extract the hostname and username from the path
223 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
225 * ftp://sunsite.unc.edu/pub/linux
226 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
227 * ftp://tsx-11.mit.edu:8192/
228 * ftp://joe@foo.edu:11321/private
229 * ftp://joe:password@foo.se
231 * @param path_element is an input string to be parsed
232 * @param path is an input string to be parsed
234 * @return g_malloc()ed url info.
235 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
236 * is not set, then the current login name is supplied.
237 * Return value is a g_malloc()ed structure with the pathname relative to the
242 vfs_path_url_split (vfs_path_element_t
* path_element
, const char *path
)
246 char *colon
, *inner_colon
, *at
, *rest
;
248 path_element
->port
= 0;
250 pcopy
= g_strdup (path
);
251 pend
= pcopy
+ strlen (pcopy
);
253 /* search for any possible user */
254 at
= strrchr (pcopy
, '@');
256 /* We have a username */
262 inner_colon
= strchr (pcopy
, ':');
263 if (inner_colon
!= NULL
)
267 path_element
->password
= g_strdup (inner_colon
);
271 path_element
->user
= g_strdup (pcopy
);
279 /* Check if the host comes with a port spec, if so, chop it */
281 colon
= strchr (rest
, ':');
284 colon
= strchr (++rest
, ']');
290 path_element
->ipv6
= TRUE
;
297 if (sscanf (colon
+ 1, "%d", &path_element
->port
) == 1)
299 if (path_element
->port
<= 0 || path_element
->port
>= 65536)
300 path_element
->port
= 0;
303 while (*(++colon
) != '\0')
308 path_element
->port
= 1;
311 path_element
->port
= 2;
316 path_element
->host
= g_strdup (rest
);
320 /* --------------------------------------------------------------------------------------------- */
322 * get VFS class for the given name
324 * @param class_name name of class
326 * @return pointer to class structure or NULL if class not found
329 static struct vfs_class
*
330 vfs_get_class_by_name (const char *class_name
)
334 if (class_name
== NULL
)
337 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
339 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
340 if ((vfs
->name
!= NULL
) && (strcmp (vfs
->name
, class_name
) == 0))
347 /* --------------------------------------------------------------------------------------------- */
349 * Check if path string contain URL-like elements
351 * @param path_str path
353 * @return TRUE if path is deprecated or FALSE otherwise
357 vfs_path_is_str_path_deprecated (const char *path_str
)
359 return strstr (path_str
, VFS_PATH_URL_DELIMITER
) == NULL
;
362 /* --------------------------------------------------------------------------------------------- */
363 /** Split path string to path elements by deprecated algorithm.
365 * @param path_str VFS-path
367 * @return pointer to newly created vfs_path_t object with filled path elements array.
371 vfs_path_from_str_deprecated_parser (char *path
, vfs_path_flag_t flags
)
374 vfs_path_element_t
*element
;
375 struct vfs_class
*class;
376 const char *local
, *op
;
379 vpath
= vfs_path_new ();
381 while ((class = _vfs_split_with_semi_skip_count (path
, &local
, &op
, 0)) != NULL
)
384 element
= g_new0 (vfs_path_element_t
, 1);
385 element
->class = class;
388 element
->path
= vfs_translate_path_n (local
);
391 element
->encoding
= vfs_get_encoding (local
);
392 element
->dir
.converter
=
393 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
396 url_params
= strchr (op
, ':'); /* skip VFS prefix */
397 if (url_params
!= NULL
)
401 vfs_path_url_split (element
, url_params
);
405 element
->vfs_prefix
= g_strdup (op
);
407 g_array_prepend_val (vpath
->path
, element
);
411 element
= g_new0 (vfs_path_element_t
, 1);
412 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
413 element
->path
= vfs_translate_path_n (path
);
416 element
->encoding
= vfs_get_encoding (path
);
417 element
->dir
.converter
=
418 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
420 g_array_prepend_val (vpath
->path
, element
);
426 /* --------------------------------------------------------------------------------------------- */
427 /** Split path string to path elements by URL algorithm.
429 * @param path_str VFS-path
430 * @param flags flags for converter
432 * @return pointer to newly created vfs_path_t object with filled path elements array.
436 vfs_path_from_str_uri_parser (char *path
, vfs_path_flag_t flags
)
439 vfs_path_element_t
*element
;
443 vpath
= vfs_path_new ();
444 vpath
->relative
= (flags
& VPF_NO_CANON
) != 0;
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
;
451 struct vfs_s_subclass
*sub
= NULL
;
453 while (real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) != PATH_SEP
)
454 real_vfs_prefix_start
--;
455 vfs_prefix_start
= real_vfs_prefix_start
;
457 if (*(vfs_prefix_start
) == PATH_SEP
)
458 vfs_prefix_start
+= 1;
460 *url_delimiter
= '\0';
462 element
= g_new0 (vfs_path_element_t
, 1);
463 element
->class = vfs_prefix_to_class (vfs_prefix_start
);
464 element
->vfs_prefix
= g_strdup (vfs_prefix_start
);
466 url_delimiter
+= strlen (VFS_PATH_URL_DELIMITER
);
467 sub
= VFSDATA (element
);
468 if (sub
!= NULL
&& (sub
->flags
& VFS_S_REMOTE
) != 0)
470 slash_pointer
= strchr (url_delimiter
, PATH_SEP
);
471 if (slash_pointer
== NULL
)
473 element
->path
= g_strdup ("");
477 element
->path
= vfs_translate_path_n (slash_pointer
+ 1);
479 element
->encoding
= vfs_get_encoding (slash_pointer
);
481 *slash_pointer
= '\0';
483 vfs_path_url_split (element
, url_delimiter
);
487 element
->path
= vfs_translate_path_n (url_delimiter
);
489 element
->encoding
= vfs_get_encoding (url_delimiter
);
493 element
->dir
.converter
=
494 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
496 g_array_prepend_val (vpath
->path
, element
);
498 if ((real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) == PATH_SEP
) ||
499 (real_vfs_prefix_start
== path
&& *(real_vfs_prefix_start
) != PATH_SEP
))
500 *real_vfs_prefix_start
= '\0';
502 *(real_vfs_prefix_start
+ 1) = '\0';
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
);
511 element
->encoding
= vfs_get_encoding (path
);
512 element
->dir
.converter
=
513 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
515 g_array_prepend_val (vpath
->path
, element
);
521 /* --------------------------------------------------------------------------------------------- */
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
526 * @param element current path element
527 * @param ret_tokens total tikens for return
528 * @param element_tokens accumulated element-only tokens
532 vfs_path_tokens_add_class_info (const vfs_path_element_t
* element
, GString
* ret_tokens
,
533 GString
* element_tokens
)
535 if (((element
->class->flags
& VFSF_LOCAL
) == 0 || ret_tokens
->len
> 0)
536 && element_tokens
->len
> 0)
540 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
541 g_string_append_c (ret_tokens
, PATH_SEP
);
543 g_string_append (ret_tokens
, element
->vfs_prefix
);
544 g_string_append (ret_tokens
, VFS_PATH_URL_DELIMITER
);
546 url_str
= vfs_path_build_url_params_str (element
, TRUE
);
547 if (*url_str
!= '\0')
549 g_string_append (ret_tokens
, url_str
);
550 g_string_append_c (ret_tokens
, PATH_SEP
);
557 if (element
->encoding
!= NULL
)
559 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
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
);
567 g_string_append (ret_tokens
, element_tokens
->str
);
570 /* --------------------------------------------------------------------------------------------- */
572 * Strip path to home dir.
573 * @param dir pointer to string contains full path
577 vfs_path_strip_home (const char *dir
)
579 const char *home_dir
= mc_config_get_home_dir ();
581 if (home_dir
!= NULL
)
585 len
= strlen (home_dir
);
587 if (strncmp (dir
, home_dir
, len
) == 0 && (dir
[len
] == PATH_SEP
|| dir
[len
] == '\0'))
588 return g_strdup_printf ("~%s", dir
+ len
);
591 return g_strdup (dir
);
594 /* --------------------------------------------------------------------------------------------- */
596 /* --------------------------------------------------------------------------------------------- */
597 /*** public functions ****************************************************************************/
598 /* --------------------------------------------------------------------------------------------- */
600 #define vfs_append_from_path(appendfrom, is_relative) \
602 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
604 char *stripped_home_str; \
605 stripped_home_str = vfs_path_strip_home (appendfrom); \
606 g_string_append (buffer, stripped_home_str); \
607 g_free (stripped_home_str); \
611 if ((!is_relative) && (*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
612 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)) \
613 g_string_append_c (buffer, PATH_SEP); \
614 g_string_append (buffer, appendfrom); \
619 * Convert first elements_count elements from vfs_path_t to string representation with flags.
621 * @param vpath pointer to vfs_path_t object
622 * @param elements_count count of first elements for convert
623 * @param flags for converter
625 * @return pointer to newly created string.
629 vfs_path_to_str_flags (const vfs_path_t
* vpath
, int elements_count
, vfs_path_flag_t flags
)
633 GString
*recode_buffer
;
638 if (elements_count
== 0 || elements_count
> vfs_path_elements_count (vpath
))
639 elements_count
= vfs_path_elements_count (vpath
);
641 if (elements_count
< 0)
642 elements_count
= vfs_path_elements_count (vpath
) + elements_count
;
644 buffer
= g_string_new ("");
645 recode_buffer
= g_string_new ("");
647 for (element_index
= 0; element_index
< elements_count
; element_index
++)
649 const vfs_path_element_t
*element
;
650 gboolean is_relative
= vpath
->relative
&& (element_index
== 0);
652 element
= vfs_path_get_by_index (vpath
, element_index
);
653 if (element
->vfs_prefix
!= NULL
)
656 if ((!is_relative
) && (buffer
->len
== 0 || buffer
->str
[buffer
->len
- 1] != PATH_SEP
))
657 g_string_append_c (buffer
, PATH_SEP
);
659 g_string_append (buffer
, element
->vfs_prefix
);
660 g_string_append (buffer
, VFS_PATH_URL_DELIMITER
);
662 url_str
= vfs_path_build_url_params_str (element
, !(flags
& VPF_STRIP_PASSWORD
));
664 if (*url_str
!= '\0')
666 g_string_append (buffer
, url_str
);
667 g_string_append_c (buffer
, PATH_SEP
);
674 if ((flags
& VPF_RECODE
) == 0 && vfs_path_element_need_cleanup_converter (element
))
676 if ((flags
& VPF_HIDE_CHARSET
) == 0)
679 && (buffer
->len
== 0 || buffer
->str
[buffer
->len
- 1] != PATH_SEP
))
680 g_string_append (buffer
, PATH_SEP_STR
);
681 g_string_append (buffer
, VFS_ENCODING_PREFIX
);
682 g_string_append (buffer
, element
->encoding
);
684 str_vfs_convert_from (element
->dir
.converter
, element
->path
, recode_buffer
);
685 vfs_append_from_path (recode_buffer
->str
, is_relative
);
686 g_string_set_size (recode_buffer
, 0);
691 vfs_append_from_path (element
->path
, is_relative
);
694 g_string_free (recode_buffer
, TRUE
);
695 return g_string_free (buffer
, FALSE
);
698 #undef vfs_append_from_path
700 /* --------------------------------------------------------------------------------------------- */
702 * Convert first elements_count elements from vfs_path_t to string representation.
704 * @param vpath pointer to vfs_path_t object
705 * @param elements_count count of first elements for convert
707 * @return pointer to newly created string.
711 vfs_path_to_str_elements_count (const vfs_path_t
* vpath
, int elements_count
)
713 return vfs_path_to_str_flags (vpath
, elements_count
, VPF_NONE
);
716 /* --------------------------------------------------------------------------------------------- */
718 * Convert vfs_path_t to string representation.
720 * @param vpath pointer to vfs_path_t object
722 * @return pointer to newly created string.
726 vfs_path_to_str (const vfs_path_t
* vpath
)
728 return vfs_path_to_str_elements_count (vpath
, vfs_path_elements_count (vpath
));
731 /* --------------------------------------------------------------------------------------------- */
733 * Split path string to path elements with flags for change parce process.
735 * @param path_str VFS-path
736 * @param flags flags for parser
738 * @return pointer to newly created vfs_path_t object with filled path elements array.
742 vfs_path_from_str_flags (const char *path_str
, vfs_path_flag_t flags
)
747 if (path_str
== NULL
)
750 if ((flags
& VPF_NO_CANON
) == 0)
751 path
= vfs_canon (path_str
);
753 path
= g_strdup (path_str
);
758 if ((flags
& VPF_USE_DEPRECATED_PARSER
) != 0 && vfs_path_is_str_path_deprecated (path
))
759 vpath
= vfs_path_from_str_deprecated_parser (path
, flags
);
761 vpath
= vfs_path_from_str_uri_parser (path
, flags
);
768 /* --------------------------------------------------------------------------------------------- */
770 * Split path string to path elements.
772 * @param path_str VFS-path
774 * @return pointer to newly created vfs_path_t object with filled path elements array.
778 vfs_path_from_str (const char *path_str
)
780 return vfs_path_from_str_flags (path_str
, VPF_NONE
);
783 /* --------------------------------------------------------------------------------------------- */
785 * Create new vfs_path_t object.
787 * @return pointer to newly created vfs_path_t object.
795 vpath
= g_new0 (vfs_path_t
, 1);
796 vpath
->path
= g_array_new (FALSE
, TRUE
, sizeof (vfs_path_element_t
*));
801 /* --------------------------------------------------------------------------------------------- */
803 * Get count of path elements.
805 * @param vpath pointer to vfs_path_t object
807 * @return count of path elements.
811 vfs_path_elements_count (const vfs_path_t
* vpath
)
813 return (vpath
!= NULL
&& vpath
->path
!= NULL
) ? vpath
->path
->len
: 0;
816 /* --------------------------------------------------------------------------------------------- */
818 * Add vfs_path_element_t object to end of list in vfs_path_t object
819 * @param vpath pointer to vfs_path_t object
820 * @param path_element pointer to vfs_path_element_t object
824 vfs_path_add_element (const vfs_path_t
* vpath
, const vfs_path_element_t
* path_element
)
826 g_array_append_val (vpath
->path
, path_element
);
829 /* --------------------------------------------------------------------------------------------- */
831 * Get one path element by index.
833 * @param vpath pointer to vfs_path_t object
834 * @param element_index element index. May have negative value (in this case count was started at the end of list).
836 * @return path element.
839 const vfs_path_element_t
*
840 vfs_path_get_by_index (const vfs_path_t
* vpath
, int element_index
)
845 if (element_index
< 0)
846 element_index
+= vfs_path_elements_count (vpath
);
848 if (element_index
< 0)
849 vfs_die ("vfs_path_get_by_index: incorrect index!");
851 return g_array_index (vpath
->path
, vfs_path_element_t
*, element_index
);
854 /* --------------------------------------------------------------------------------------------- */
856 * Clone one path element
858 * @param element pointer to vfs_path_element_t object
860 * @return Newly allocated path element
864 vfs_path_element_clone (const vfs_path_element_t
* element
)
866 vfs_path_element_t
*new_element
= g_new (vfs_path_element_t
, 1);
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
);
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
);
881 new_element
->dir
.converter
= element
->dir
.converter
;
883 new_element
->dir
.info
= element
->dir
.info
;
888 /* --------------------------------------------------------------------------------------------- */
890 * Free one path element.
892 * @param element pointer to vfs_path_element_t object
897 vfs_path_element_free (vfs_path_element_t
* element
)
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
);
909 g_free (element
->encoding
);
911 if (vfs_path_element_need_cleanup_converter (element
))
912 str_close_conv (element
->dir
.converter
);
918 /* --------------------------------------------------------------------------------------------- */
922 * @param vpath pointer to vfs_path_t object
924 * @return Newly allocated path object
928 vfs_path_clone (const vfs_path_t
* vpath
)
930 vfs_path_t
*new_vpath
;
931 int vpath_element_index
;
936 new_vpath
= vfs_path_new ();
937 new_vpath
->relative
= vpath
->relative
;
939 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
940 vpath_element_index
++)
942 vfs_path_element_t
*path_element
;
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
);
951 /* --------------------------------------------------------------------------------------------- */
953 * Free vfs_path_t object.
955 * @param vpath pointer to vfs_path_t object
960 vfs_path_free (vfs_path_t
* vpath
)
962 int vpath_element_index
;
967 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
968 vpath_element_index
++)
970 vfs_path_element_t
*path_element
;
972 path_element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, vpath_element_index
);
973 vfs_path_element_free (path_element
);
976 g_array_free (vpath
->path
, TRUE
);
980 /* --------------------------------------------------------------------------------------------- */
982 * Remove one path element by index
984 * @param vpath pointer to vfs_path_t object
985 * @param element_index element index. May have negative value (in this case count was started at the end of list).
990 vfs_path_remove_element_by_index (vfs_path_t
* vpath
, int element_index
)
992 vfs_path_element_t
*element
;
994 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 1))
997 if (element_index
< 0)
998 element_index
= vfs_path_elements_count (vpath
) + element_index
;
1000 element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, element_index
);
1001 vpath
->path
= g_array_remove_index (vpath
->path
, element_index
);
1002 vfs_path_element_free (element
);
1005 /* --------------------------------------------------------------------------------------------- */
1006 /** Return VFS class for the given prefix */
1009 vfs_prefix_to_class (const char *prefix
)
1013 /* Avoid first class (localfs) that would accept any prefix */
1014 for (i
= 1; i
< vfs__classes_list
->len
; i
++)
1016 struct vfs_class
*vfs
;
1018 vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
1019 if (vfs
->which
!= NULL
)
1021 if (vfs
->which (vfs
, prefix
) == -1)
1026 if (vfs
->prefix
!= NULL
&& strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)) == 0)
1033 /* --------------------------------------------------------------------------------------------- */
1038 * Check if need cleanup charset converter for vfs_path_element_t
1040 * @param element part of path
1042 * @return TRUE if need cleanup converter or FALSE otherwise
1046 vfs_path_element_need_cleanup_converter (const vfs_path_element_t
* element
)
1048 return (element
->dir
.converter
!= str_cnv_from_term
&& element
->dir
.converter
!= INVALID_CONV
);
1052 /* --------------------------------------------------------------------------------------------- */
1055 * Serialize vfs_path_t object to string
1057 * @param vpath data for serialization
1058 * @param error contain pointer to object for handle error code and message
1060 * @return serialized vpath as newly allocated string
1064 vfs_path_serialize (const vfs_path_t
* vpath
, GError
** error
)
1067 ssize_t element_index
;
1070 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 0))
1072 g_set_error (error
, MC_ERROR
, -1, "vpath object is empty");
1077 cpath
= mc_config_init (NULL
, FALSE
);
1079 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1082 const vfs_path_element_t
*element
;
1084 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1085 element
= vfs_path_get_by_index (vpath
, element_index
);
1086 /* convert one element to config group */
1088 mc_config_set_string_raw (cpath
, groupname
, "path", element
->path
);
1089 mc_config_set_string_raw (cpath
, groupname
, "class-name", element
->class->name
);
1091 mc_config_set_string_raw (cpath
, groupname
, "encoding", element
->encoding
);
1093 mc_config_set_string_raw (cpath
, groupname
, "vfs_prefix", element
->vfs_prefix
);
1095 mc_config_set_string_raw (cpath
, groupname
, "user", element
->user
);
1096 mc_config_set_string_raw (cpath
, groupname
, "password", element
->password
);
1097 mc_config_set_string_raw (cpath
, groupname
, "host", element
->host
);
1098 if (element
->port
!= 0)
1099 mc_config_set_int (cpath
, groupname
, "port", element
->port
);
1104 ret_value
= mc_serialize_config (cpath
, error
);
1105 mc_config_deinit (cpath
);
1109 /* --------------------------------------------------------------------------------------------- */
1111 * Deserialize string to vfs_path_t object
1113 * @param data data for serialization
1114 * @param error contain pointer to object for handle error code and message
1116 * @return newly allocated vfs_path_t object
1120 vfs_path_deserialize (const char *data
, GError
** error
)
1123 size_t element_index
= 0;
1126 cpath
= mc_deserialize_config (data
, error
);
1130 vpath
= vfs_path_new ();
1134 vfs_path_element_t
*element
;
1138 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1139 if (!mc_config_has_group (cpath
, groupname
))
1145 element
= g_new0 (vfs_path_element_t
, 1);
1147 cfg_value
= mc_config_get_string_raw (cpath
, groupname
, "class-name", NULL
);
1148 element
->class = vfs_get_class_by_name (cfg_value
);
1149 if (element
->class == NULL
)
1152 vfs_path_free (vpath
);
1153 g_set_error (error
, MC_ERROR
, -1, "Unable to find VFS class by name '%s'", cfg_value
);
1155 mc_config_deinit (cpath
);
1160 element
->path
= mc_config_get_string_raw (cpath
, groupname
, "path", NULL
);
1163 element
->encoding
= mc_config_get_string_raw (cpath
, groupname
, "encoding", NULL
);
1164 element
->dir
.converter
=
1165 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
1168 element
->vfs_prefix
= mc_config_get_string_raw (cpath
, groupname
, "vfs_prefix", NULL
);
1170 element
->user
= mc_config_get_string_raw (cpath
, groupname
, "user", NULL
);
1171 element
->password
= mc_config_get_string_raw (cpath
, groupname
, "password", NULL
);
1172 element
->host
= mc_config_get_string_raw (cpath
, groupname
, "host", NULL
);
1173 element
->port
= mc_config_get_int (cpath
, groupname
, "port", 0);
1175 vpath
->path
= g_array_append_val (vpath
->path
, element
);
1181 mc_config_deinit (cpath
);
1182 if (vfs_path_elements_count (vpath
) == 0)
1184 vfs_path_free (vpath
);
1185 g_set_error (error
, MC_ERROR
, -1, "No any path elements found");
1192 /* --------------------------------------------------------------------------------------------- */
1194 * Build vfs_path_t object from arguments.
1196 * @param first_element of path
1197 * @param ... path tokens, terminated by NULL
1199 * @return newly allocated vfs_path_t object
1203 vfs_path_build_filename (const char *first_element
, ...)
1209 if (first_element
== NULL
)
1212 va_start (args
, first_element
);
1213 str_path
= mc_build_filenamev (first_element
, args
);
1215 vpath
= vfs_path_from_str (str_path
);
1220 /* --------------------------------------------------------------------------------------------- */
1222 * Append tokens to path object
1224 * @param vpath path object
1225 * @param first_element of path
1226 * @param ... NULL-terminated strings
1228 * @return newly allocated path object
1232 vfs_path_append_new (const vfs_path_t
* vpath
, const char *first_element
, ...)
1235 char *str_path
, *result_str
;
1236 vfs_path_t
*ret_vpath
;
1238 if (vpath
== NULL
|| first_element
== NULL
)
1241 va_start (args
, first_element
);
1242 str_path
= mc_build_filenamev (first_element
, args
);
1245 result_str
= vfs_path_to_str (vpath
);
1246 ret_vpath
= vfs_path_build_filename (result_str
, str_path
, NULL
);
1247 g_free (result_str
);
1254 /* --------------------------------------------------------------------------------------------- */
1257 * Append vpath_t tokens to path object
1259 * @param first_vpath vpath objects
1260 * @param ... NULL-terminated vpath objects
1262 * @return newly allocated path object
1266 vfs_path_append_vpath_new (const vfs_path_t
* first_vpath
, ...)
1269 vfs_path_t
*ret_vpath
;
1270 const vfs_path_t
*current_vpath
= first_vpath
;
1272 if (first_vpath
== NULL
)
1275 ret_vpath
= vfs_path_new ();
1277 va_start (args
, first_vpath
);
1282 for (vindex
= 0; vindex
< vfs_path_elements_count (current_vpath
); vindex
++)
1284 vfs_path_element_t
*path_element
;
1286 path_element
= vfs_path_element_clone (vfs_path_get_by_index (current_vpath
, vindex
));
1287 g_array_append_val (ret_vpath
->path
, path_element
);
1289 current_vpath
= va_arg (args
, const vfs_path_t
*);
1291 while (current_vpath
!= NULL
);
1297 /* --------------------------------------------------------------------------------------------- */
1300 * get tockens count in path.
1302 * @param vpath path object
1304 * @return count of tokens
1308 vfs_path_tokens_count (const vfs_path_t
* vpath
)
1310 size_t count_tokens
= 0;
1316 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1318 const vfs_path_element_t
*element
;
1319 char **path_tokens
, **iterator
;
1321 element
= vfs_path_get_by_index (vpath
, element_index
);
1322 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1324 while (*iterator
!= NULL
)
1326 if (**iterator
!= '\0')
1330 g_strfreev (path_tokens
);
1332 return count_tokens
;
1335 /* --------------------------------------------------------------------------------------------- */
1338 * Get subpath by tokens
1340 * @param vpath path object
1341 * @param start_position first token for got/ Started from 0.
1342 * If negative, then position will be relative to end of path
1343 * @param length count of tokens
1345 * @return newly allocated string with path tokens separated by slash
1349 vfs_path_tokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1351 GString
*ret_tokens
, *element_tokens
;
1353 size_t tokens_count
= vfs_path_tokens_count (vpath
);
1359 length
= tokens_count
;
1362 length
= tokens_count
+ length
;
1364 if (start_position
< 0)
1365 start_position
= (ssize_t
) tokens_count
+ start_position
;
1367 if (start_position
< 0)
1370 if (start_position
>= (ssize_t
) tokens_count
)
1373 if (start_position
+ (ssize_t
) length
> (ssize_t
) tokens_count
)
1374 length
= tokens_count
- start_position
;
1376 ret_tokens
= g_string_sized_new (32);
1377 element_tokens
= g_string_sized_new (32);
1379 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1381 const vfs_path_element_t
*element
;
1382 char **path_tokens
, **iterator
;
1384 g_string_assign (element_tokens
, "");
1385 element
= vfs_path_get_by_index (vpath
, element_index
);
1386 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1388 while (*iterator
!= NULL
)
1390 if (**iterator
!= '\0')
1392 if (start_position
== 0)
1396 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1397 g_string_free (element_tokens
, TRUE
);
1398 g_strfreev (path_tokens
);
1399 return g_string_free (ret_tokens
, FALSE
);
1402 if (element_tokens
->len
!= 0)
1403 g_string_append_c (element_tokens
, PATH_SEP
);
1404 g_string_append (element_tokens
, *iterator
);
1411 g_strfreev (path_tokens
);
1412 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1415 g_string_free (element_tokens
, TRUE
);
1416 return g_string_free (ret_tokens
, !(start_position
== 0 && length
== 0));
1419 /* --------------------------------------------------------------------------------------------- */
1421 * Get subpath by tokens
1423 * @param vpath path object
1424 * @param start_position first token for got/ Started from 0.
1425 * If negative, then position will be relative to end of path
1426 * @param length count of tokens
1428 * @return newly allocated path object with path tokens separated by slash
1432 vfs_path_vtokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1435 vfs_path_t
*ret_vpath
= NULL
;
1437 str_tokens
= vfs_path_tokens_get (vpath
, start_position
, length
);
1438 if (str_tokens
!= NULL
)
1440 ret_vpath
= vfs_path_from_str_flags (str_tokens
, VPF_NO_CANON
);
1441 g_free (str_tokens
);
1446 /* --------------------------------------------------------------------------------------------- */
1449 * Build URL parameters (such as user:pass @ host:port) from one path element object
1451 * @param element path element
1452 * @param keep_password TRUE or FALSE
1454 * @return newly allocated string
1458 vfs_path_build_url_params_str (const vfs_path_element_t
* element
, gboolean keep_password
)
1462 if (element
== NULL
)
1465 buffer
= g_string_new ("");
1467 if (element
->user
!= NULL
)
1468 g_string_append (buffer
, element
->user
);
1470 if (element
->password
!= NULL
&& keep_password
)
1472 g_string_append_c (buffer
, ':');
1473 g_string_append (buffer
, element
->password
);
1476 if (element
->host
!= NULL
)
1478 if ((element
->user
!= NULL
) || (element
->password
!= NULL
))
1479 g_string_append_c (buffer
, '@');
1481 g_string_append_c (buffer
, '[');
1482 g_string_append (buffer
, element
->host
);
1484 g_string_append_c (buffer
, ']');
1487 if ((element
->port
) != 0 && (element
->host
!= NULL
))
1489 g_string_append_c (buffer
, ':');
1490 g_string_append_printf (buffer
, "%d", element
->port
);
1493 return g_string_free (buffer
, FALSE
);
1496 /* --------------------------------------------------------------------------------------------- */
1498 * Build pretty string representation of one path_element_t object
1500 * @param element path element
1502 * @return newly allocated string
1506 vfs_path_element_build_pretty_path_str (const vfs_path_element_t
* element
)
1509 GString
*pretty_path
;
1511 pretty_path
= g_string_new (element
->class->prefix
);
1512 g_string_append (pretty_path
, VFS_PATH_URL_DELIMITER
);
1514 url_params
= vfs_path_build_url_params_str (element
, FALSE
);
1515 g_string_append (pretty_path
, url_params
);
1516 g_free (url_params
);
1518 if (*element
->path
!= PATH_SEP
)
1519 g_string_append_c (pretty_path
, PATH_SEP
);
1521 g_string_append (pretty_path
, element
->path
);
1522 return g_string_free (pretty_path
, FALSE
);
1525 /* --------------------------------------------------------------------------------------------- */
1527 * Compare two path objects as strings
1529 * @param vpath1 first path object
1530 * @param vpath2 second vpath object
1532 * @return integer value like to strcmp.
1536 vfs_path_equal (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
1542 if (vpath1
== NULL
|| vpath2
== NULL
)
1545 path1
= vfs_path_to_str (vpath1
);
1546 path2
= vfs_path_to_str (vpath2
);
1548 ret_val
= strcmp (path1
, path2
) == 0;
1556 /* --------------------------------------------------------------------------------------------- */
1558 * Compare two path objects as strings
1560 * @param vpath1 first path object
1561 * @param vpath2 second vpath object
1562 * @param len number of first 'len' characters
1564 * @return integer value like to strcmp.
1568 vfs_path_equal_len (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
, size_t len
)
1574 if (vpath1
== NULL
|| vpath2
== NULL
)
1577 path1
= vfs_path_to_str (vpath1
);
1578 path2
= vfs_path_to_str (vpath2
);
1580 ret_val
= strncmp (path1
, path2
, len
) == 0;
1588 /* --------------------------------------------------------------------------------------------- */
1590 * Calculate path length in string representation
1592 * @param vpath path object
1594 * @return length of path
1598 vfs_path_len (const vfs_path_t
* vpath
)
1606 path
= vfs_path_to_str (vpath
);
1607 ret_val
= strlen (path
);
1612 /* --------------------------------------------------------------------------------------------- */
1614 * Convert relative vpath object to absolute
1616 * @param vpath path object
1618 * @return absolute path object
1622 vfs_path_to_absolute (const vfs_path_t
* vpath
)
1624 vfs_path_t
*absolute_vpath
;
1627 if (!vpath
->relative
)
1628 return vfs_path_clone (vpath
);
1630 path_str
= vfs_path_to_str (vpath
);
1631 absolute_vpath
= vfs_path_from_str (path_str
);
1633 return absolute_vpath
;
1636 /* --------------------------------------------------------------------------------------------- */