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" /* concat_dir_and_file */
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 local
= tilde_expand (path
);
146 if (*local
!= PATH_SEP
)
151 curr_dir
= vfs_get_current_dir ();
152 local
= concat_dir_and_file (curr_dir
, path
);
156 result
= vfs_canon (local
);
162 * So we have path of following form:
163 * /p1/p2#op/.././././p3#op/p4. Good luck.
168 result
= g_strdup (path
);
169 canonicalize_pathname (result
);
174 /* --------------------------------------------------------------------------------------------- */
175 /** get encoding after last #enc: or NULL, if part does not contain #enc:
179 * @return newly allocated string.
183 vfs_get_encoding (const char *path
)
189 work
= g_strdup (path
);
191 /* try found #enc: */
192 semi
= g_strrstr (work
, VFS_ENCODING_PREFIX
);
194 if (semi
!= NULL
&& (semi
== work
|| *(semi
- 1) == PATH_SEP
))
196 semi
+= strlen (VFS_ENCODING_PREFIX
); /* skip "#enc:" */
197 slash
= strchr (semi
, PATH_SEP
);
201 g_strlcpy (result
, semi
, sizeof (result
));
203 return g_strdup (result
);
212 /* --------------------------------------------------------------------------------------------- */
213 /** Extract the hostname and username from the path
215 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
217 * ftp://sunsite.unc.edu/pub/linux
218 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
219 * ftp://tsx-11.mit.edu:8192/
220 * ftp://joe@foo.edu:11321/private
221 * ftp://joe:password@foo.se
223 * @param path_element is an input string to be parsed
224 * @param path is an input string to be parsed
226 * @return g_malloc()ed url info.
227 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
228 * is not set, then the current login name is supplied.
229 * Return value is a g_malloc()ed structure with the pathname relative to the
234 vfs_path_url_split (vfs_path_element_t
* path_element
, const char *path
)
238 char *dir
, *colon
, *inner_colon
, *at
, *rest
;
240 path_element
->port
= 0;
242 pcopy
= g_strdup (path
);
243 pend
= pcopy
+ strlen (pcopy
);
246 /* search for any possible user */
247 at
= strrchr (pcopy
, '@');
249 /* We have a username */
255 inner_colon
= strchr (pcopy
, ':');
256 if (inner_colon
!= NULL
)
260 path_element
->password
= g_strdup (inner_colon
);
264 path_element
->user
= g_strdup (pcopy
);
272 /* Check if the host comes with a port spec, if so, chop it */
274 colon
= strchr (rest
, ':');
277 colon
= strchr (++rest
, ']');
283 path_element
->ipv6
= TRUE
;
290 if (sscanf (colon
+ 1, "%d", &path_element
->port
) == 1)
292 if (path_element
->port
<= 0 || path_element
->port
>= 65536)
293 path_element
->port
= 0;
296 while (*(++colon
) != '\0')
301 path_element
->port
= 1;
304 path_element
->port
= 2;
309 path_element
->host
= g_strdup (rest
);
313 /* --------------------------------------------------------------------------------------------- */
315 * get VFS class for the given name
317 * @param class_name name of class
319 * @return pointer to class structure or NULL if class not found
322 static struct vfs_class
*
323 vfs_get_class_by_name (const char *class_name
)
327 if (class_name
== NULL
)
330 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
332 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
333 if ((vfs
->name
!= NULL
) && (strcmp (vfs
->name
, class_name
) == 0))
340 /* --------------------------------------------------------------------------------------------- */
342 * Check if path string contain URL-like elements
344 * @param path_str path
346 * @return TRUE if path is deprecated or FALSE otherwise
350 vfs_path_is_str_path_deprecated (const char *path_str
)
352 return strstr (path_str
, VFS_PATH_URL_DELIMITER
) == NULL
;
355 /* --------------------------------------------------------------------------------------------- */
356 /** Split path string to path elements by deprecated algorithm.
358 * @param path_str VFS-path
360 * @return pointer to newly created vfs_path_t object with filled path elements array.
364 vfs_path_from_str_deprecated_parser (char *path
)
367 vfs_path_element_t
*element
;
368 struct vfs_class
*class;
369 const char *local
, *op
;
371 vpath
= vfs_path_new ();
373 while ((class = _vfs_split_with_semi_skip_count (path
, &local
, &op
, 0)) != NULL
)
376 element
= g_new0 (vfs_path_element_t
, 1);
377 element
->class = class;
380 element
->path
= vfs_translate_path_n (local
);
382 element
->encoding
= vfs_get_encoding (local
);
383 element
->dir
.converter
=
384 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
386 url_params
= strchr (op
, ':'); /* skip VFS prefix */
387 if (url_params
!= NULL
)
391 vfs_path_url_split (element
, url_params
);
395 element
->vfs_prefix
= g_strdup (op
);
397 g_array_prepend_val (vpath
->path
, element
);
401 element
= g_new0 (vfs_path_element_t
, 1);
402 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
403 element
->path
= vfs_translate_path_n (path
);
405 element
->encoding
= vfs_get_encoding (path
);
406 element
->dir
.converter
=
407 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
408 g_array_prepend_val (vpath
->path
, element
);
414 /* --------------------------------------------------------------------------------------------- */
415 /** Split path string to path elements by URL algorithm.
417 * @param path_str VFS-path
419 * @return pointer to newly created vfs_path_t object with filled path elements array.
423 vfs_path_from_str_uri_parser (char *path
)
426 vfs_path_element_t
*element
;
430 vpath
= vfs_path_new ();
432 while ((url_delimiter
= g_strrstr (path
, VFS_PATH_URL_DELIMITER
)) != NULL
)
434 char *vfs_prefix_start
;
435 char *real_vfs_prefix_start
= url_delimiter
;
437 struct vfs_s_subclass
*sub
= NULL
;
439 while (real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) != PATH_SEP
)
440 real_vfs_prefix_start
--;
441 vfs_prefix_start
= real_vfs_prefix_start
;
443 if (*(vfs_prefix_start
) == PATH_SEP
)
444 vfs_prefix_start
+= 1;
446 *url_delimiter
= '\0';
448 element
= g_new0 (vfs_path_element_t
, 1);
449 element
->class = vfs_prefix_to_class (vfs_prefix_start
);
450 element
->vfs_prefix
= g_strdup (vfs_prefix_start
);
452 url_delimiter
+= strlen (VFS_PATH_URL_DELIMITER
);
453 sub
= VFSDATA (element
);
454 if (sub
!= NULL
&& sub
->flags
& VFS_S_REMOTE
)
456 slash_pointer
= strchr (url_delimiter
, PATH_SEP
);
457 if (slash_pointer
== NULL
)
459 element
->path
= g_strdup ("");
463 element
->path
= vfs_translate_path_n (slash_pointer
+ 1);
464 element
->encoding
= vfs_get_encoding (slash_pointer
);
466 *slash_pointer
= '\0';
468 vfs_path_url_split (element
, url_delimiter
);
472 element
->path
= vfs_translate_path_n (url_delimiter
);
473 element
->encoding
= vfs_get_encoding (url_delimiter
);
475 element
->dir
.converter
=
476 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
477 g_array_prepend_val (vpath
->path
, element
);
479 if ((real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) == PATH_SEP
) ||
480 (real_vfs_prefix_start
== path
&& *(real_vfs_prefix_start
) != PATH_SEP
))
481 *real_vfs_prefix_start
= '\0';
483 *(real_vfs_prefix_start
+ 1) = '\0';
488 element
= g_new0 (vfs_path_element_t
, 1);
489 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
490 element
->path
= vfs_translate_path_n (path
);
491 element
->encoding
= vfs_get_encoding (path
);
492 element
->dir
.converter
=
493 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
494 g_array_prepend_val (vpath
->path
, element
);
500 /* --------------------------------------------------------------------------------------------- */
502 * Add element's class info to result string (such as VFS name, host, encoding etc)
503 * This function used as helper only in vfs_path_tokens_get() function
505 * @param element current path element
506 * @param ret_tokens total tikens for return
507 * @param element_tokens accumulated element-only tokens
511 vfs_path_tokens_add_class_info (vfs_path_element_t
* element
, GString
* ret_tokens
,
512 GString
* element_tokens
)
514 if (((element
->class->flags
& VFSF_LOCAL
) == 0 || ret_tokens
->len
> 0)
515 && element_tokens
->len
> 0)
519 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
520 g_string_append_c (ret_tokens
, PATH_SEP
);
522 g_string_append (ret_tokens
, element
->vfs_prefix
);
523 g_string_append (ret_tokens
, VFS_PATH_URL_DELIMITER
);
525 url_str
= vfs_path_build_url_params_str (element
, TRUE
);
526 if (*url_str
!= '\0')
528 g_string_append (ret_tokens
, url_str
);
529 g_string_append_c (ret_tokens
, PATH_SEP
);
534 if (element
->encoding
!= NULL
)
536 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
537 g_string_append (ret_tokens
, PATH_SEP_STR
);
538 g_string_append (ret_tokens
, VFS_ENCODING_PREFIX
);
539 g_string_append (ret_tokens
, element
->encoding
);
540 g_string_append (ret_tokens
, PATH_SEP_STR
);
543 g_string_append (ret_tokens
, element_tokens
->str
);
546 /* --------------------------------------------------------------------------------------------- */
547 /*** public functions ****************************************************************************/
548 /* --------------------------------------------------------------------------------------------- */
550 * Convert first elements_count elements from vfs_path_t to string representation.
552 * @param vpath pointer to vfs_path_t object
553 * @param elements_count count of first elements for convert
554 * @param flags flags for parser
556 * @return pointer to newly created string.
559 #define vfs_append_from_path(appendfrom) \
561 if ((*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
562 && (buffer->str[buffer->len - 1] != PATH_SEP)) \
563 g_string_append_c (buffer, PATH_SEP); \
564 g_string_append (buffer, appendfrom); \
568 vfs_path_to_str_elements_count (const vfs_path_t
* vpath
, int elements_count
)
572 GString
*recode_buffer
;
577 if (elements_count
> vfs_path_elements_count (vpath
))
578 elements_count
= vfs_path_elements_count (vpath
);
580 if (elements_count
< 0)
581 elements_count
= vfs_path_elements_count (vpath
) + elements_count
;
583 buffer
= g_string_new ("");
584 recode_buffer
= g_string_new ("");
586 for (element_index
= 0; element_index
< elements_count
; element_index
++)
588 vfs_path_element_t
*element
= vfs_path_get_by_index (vpath
, element_index
);
590 if (element
->vfs_prefix
!= NULL
)
594 if (buffer
->len
== 0 || buffer
->str
[buffer
->len
- 1] != PATH_SEP
)
595 g_string_append_c (buffer
, PATH_SEP
);
597 g_string_append (buffer
, element
->vfs_prefix
);
598 g_string_append (buffer
, VFS_PATH_URL_DELIMITER
);
600 url_str
= vfs_path_build_url_params_str (element
, TRUE
);
601 if (*url_str
!= '\0')
602 g_string_append (buffer
, url_str
);
607 if (vfs_path_element_need_cleanup_converter (element
))
609 if (buffer
->str
[buffer
->len
- 1] != PATH_SEP
)
610 g_string_append (buffer
, PATH_SEP_STR
);
611 g_string_append (buffer
, VFS_ENCODING_PREFIX
);
612 g_string_append (buffer
, element
->encoding
);
613 str_vfs_convert_from (element
->dir
.converter
, element
->path
, recode_buffer
);
614 vfs_append_from_path (recode_buffer
->str
);
615 g_string_set_size (recode_buffer
, 0);
619 vfs_append_from_path (element
->path
);
622 g_string_free (recode_buffer
, TRUE
);
623 return g_string_free (buffer
, FALSE
);
626 #undef vfs_append_from_path
628 /* --------------------------------------------------------------------------------------------- */
630 * Convert vfs_path_t to string representation.
632 * @param vpath pointer to vfs_path_t object
634 * @return pointer to newly created string.
638 vfs_path_to_str (const vfs_path_t
* vpath
)
640 return vfs_path_to_str_elements_count (vpath
, vfs_path_elements_count (vpath
));
643 /* --------------------------------------------------------------------------------------------- */
645 * Split path string to path elements with flags for change parce process.
647 * @param path_str VFS-path
648 * @param flags flags for parser
650 * @return pointer to newly created vfs_path_t object with filled path elements array.
654 vfs_path_from_str_flags (const char *path_str
, vfs_path_flag_t flags
)
659 if (path_str
== NULL
)
662 if ((flags
& VPF_NO_CANON
) == 0)
663 path
= vfs_canon (path_str
);
665 path
= g_strdup (path_str
);
670 if ((flags
& VPF_USE_DEPRECATED_PARSER
) != 0 && vfs_path_is_str_path_deprecated (path
))
671 vpath
= vfs_path_from_str_deprecated_parser (path
);
673 vpath
= vfs_path_from_str_uri_parser (path
);
680 /* --------------------------------------------------------------------------------------------- */
682 * Split path string to path elements.
684 * @param path_str VFS-path
686 * @return pointer to newly created vfs_path_t object with filled path elements array.
690 vfs_path_from_str (const char *path_str
)
692 return vfs_path_from_str_flags (path_str
, VPF_NONE
);
695 /* --------------------------------------------------------------------------------------------- */
697 * Create new vfs_path_t object.
699 * @return pointer to newly created vfs_path_t object.
707 vpath
= g_new0 (vfs_path_t
, 1);
708 vpath
->path
= g_array_new (FALSE
, TRUE
, sizeof (vfs_path_element_t
*));
713 /* --------------------------------------------------------------------------------------------- */
715 * Get count of path elements.
717 * @param vpath pointer to vfs_path_t object
719 * @return count of path elements.
723 vfs_path_elements_count (const vfs_path_t
* vpath
)
725 return (vpath
!= NULL
&& vpath
->path
!= NULL
) ? vpath
->path
->len
: 0;
728 /* --------------------------------------------------------------------------------------------- */
730 * Get one path element by index.
732 * @param vpath pointer to vfs_path_t object
733 * @param element_index element index. May have negative value (in this case count was started at the end of list).
735 * @return path element.
739 vfs_path_get_by_index (const vfs_path_t
* vpath
, int element_index
)
741 if (element_index
< 0)
742 element_index
+= vfs_path_elements_count (vpath
);
744 if (element_index
< 0)
745 vfs_die ("vfs_path_get_by_index: incorrect index!");
747 return g_array_index (vpath
->path
, vfs_path_element_t
*, element_index
);
750 /* --------------------------------------------------------------------------------------------- */
752 * Clone one path element
754 * @param element pointer to vfs_path_element_t object
756 * @return Newly allocated path element
760 vfs_path_element_clone (const vfs_path_element_t
* element
)
762 vfs_path_element_t
*new_element
= g_new0 (vfs_path_element_t
, 1);
763 memcpy (new_element
, element
, sizeof (vfs_path_element_t
));
765 new_element
->user
= g_strdup (element
->user
);
766 new_element
->password
= g_strdup (element
->password
);
767 new_element
->host
= g_strdup (element
->host
);
768 new_element
->path
= g_strdup (element
->path
);
769 new_element
->encoding
= g_strdup (element
->encoding
);
770 if (vfs_path_element_need_cleanup_converter (element
) && new_element
->encoding
!= NULL
)
771 new_element
->dir
.converter
= str_crt_conv_from (new_element
->encoding
);
772 new_element
->vfs_prefix
= g_strdup (element
->vfs_prefix
);
777 /* --------------------------------------------------------------------------------------------- */
779 * Free one path element.
781 * @param element pointer to vfs_path_element_t object
786 vfs_path_element_free (vfs_path_element_t
* element
)
791 g_free (element
->user
);
792 g_free (element
->password
);
793 g_free (element
->host
);
794 g_free (element
->path
);
795 g_free (element
->encoding
);
796 g_free (element
->vfs_prefix
);
798 if (vfs_path_element_need_cleanup_converter (element
))
800 str_close_conv (element
->dir
.converter
);
806 /* --------------------------------------------------------------------------------------------- */
810 * @param vpath pointer to vfs_path_t object
812 * @return Newly allocated path object
816 vfs_path_clone (const vfs_path_t
* vpath
)
818 vfs_path_t
*new_vpath
;
819 int vpath_element_index
;
824 new_vpath
= vfs_path_new ();
826 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
827 vpath_element_index
++)
829 vfs_path_element_t
*path_element
;
832 vfs_path_element_clone (vfs_path_get_by_index (vpath
, vpath_element_index
));
833 g_array_append_val (new_vpath
->path
, path_element
);
839 /* --------------------------------------------------------------------------------------------- */
841 * Free vfs_path_t object.
843 * @param vpath pointer to vfs_path_t object
848 vfs_path_free (vfs_path_t
* vpath
)
850 int vpath_element_index
;
855 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
856 vpath_element_index
++)
857 vfs_path_element_free (vfs_path_get_by_index (vpath
, vpath_element_index
));
859 g_array_free (vpath
->path
, TRUE
);
863 /* --------------------------------------------------------------------------------------------- */
865 * Remove one path element by index
867 * @param vpath pointer to vfs_path_t object
868 * @param element_index element index. May have negative value (in this case count was started at the end of list).
873 vfs_path_remove_element_by_index (vfs_path_t
* vpath
, int element_index
)
875 vfs_path_element_t
*element
;
877 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 1))
880 if (element_index
< 0)
881 element_index
= vfs_path_elements_count (vpath
) + element_index
;
883 element
= vfs_path_get_by_index (vpath
, element_index
);
884 vpath
->path
= g_array_remove_index (vpath
->path
, element_index
);
885 vfs_path_element_free (element
);
888 /* --------------------------------------------------------------------------------------------- */
889 /** Return VFS class for the given prefix */
892 vfs_prefix_to_class (const char *prefix
)
896 /* Avoid first class (localfs) that would accept any prefix */
897 for (i
= 1; i
< vfs__classes_list
->len
; i
++)
899 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
900 if (vfs
->which
!= NULL
)
902 if (vfs
->which (vfs
, prefix
) == -1)
907 if (vfs
->prefix
!= NULL
&& strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)) == 0)
914 /* --------------------------------------------------------------------------------------------- */
916 * Check if need cleanup charset converter for vfs_path_element_t
918 * @param element part of path
920 * @return TRUE if need cleanup converter or FALSE otherwise
924 vfs_path_element_need_cleanup_converter (const vfs_path_element_t
* element
)
926 return (element
->dir
.converter
!= str_cnv_from_term
&& element
->dir
.converter
!= INVALID_CONV
);
929 /* --------------------------------------------------------------------------------------------- */
931 * Serialize vfs_path_t object to string
933 * @param vpath data for serialization
934 * @param error contain pointer to object for handle error code and message
936 * @return serialized vpath as newly allocated string
940 vfs_path_serialize (const vfs_path_t
* vpath
, GError
** error
)
942 mc_config_t
*cpath
= mc_config_init (NULL
);
943 ssize_t element_index
;
946 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 0))
948 g_set_error (error
, MC_ERROR
, -1, "vpath object is empty");
952 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
954 char *groupname
= g_strdup_printf ("path-element-%zd", element_index
);
955 vfs_path_element_t
*element
= vfs_path_get_by_index (vpath
, element_index
);
957 /* convert one element to config group */
959 mc_config_set_string_raw (cpath
, groupname
, "path", element
->path
);
960 mc_config_set_string_raw (cpath
, groupname
, "class-name", element
->class->name
);
961 mc_config_set_string_raw (cpath
, groupname
, "encoding", element
->encoding
);
963 mc_config_set_string_raw (cpath
, groupname
, "vfs_prefix", element
->vfs_prefix
);
965 mc_config_set_string_raw (cpath
, groupname
, "user", element
->user
);
966 mc_config_set_string_raw (cpath
, groupname
, "password", element
->password
);
967 mc_config_set_string_raw (cpath
, groupname
, "host", element
->host
);
968 if (element
->port
!= 0)
969 mc_config_set_int (cpath
, groupname
, "port", element
->port
);
974 ret_value
= mc_serialize_config (cpath
, error
);
975 mc_config_deinit (cpath
);
979 /* --------------------------------------------------------------------------------------------- */
981 * Deserialize string to vfs_path_t object
983 * @param data data for serialization
984 * @param error contain pointer to object for handle error code and message
986 * @return newly allocated vfs_path_t object
990 vfs_path_deserialize (const char *data
, GError
** error
)
992 mc_config_t
*cpath
= mc_deserialize_config (data
, error
);
993 size_t element_index
= 0;
999 vpath
= vfs_path_new ();
1003 vfs_path_element_t
*element
;
1007 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1008 if (!mc_config_has_group (cpath
, groupname
))
1014 element
= g_new0 (vfs_path_element_t
, 1);
1016 cfg_value
= mc_config_get_string_raw (cpath
, groupname
, "class-name", NULL
);
1017 element
->class = vfs_get_class_by_name (cfg_value
);
1018 if (element
->class == NULL
)
1021 vfs_path_free (vpath
);
1022 g_set_error (error
, MC_ERROR
, -1, "Unable to find VFS class by name '%s'", cfg_value
);
1024 mc_config_deinit (cpath
);
1029 element
->path
= mc_config_get_string_raw (cpath
, groupname
, "path", NULL
);
1030 element
->encoding
= mc_config_get_string_raw (cpath
, groupname
, "encoding", NULL
);
1031 element
->dir
.converter
=
1032 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
1034 element
->vfs_prefix
= mc_config_get_string_raw (cpath
, groupname
, "vfs_prefix", NULL
);
1036 element
->user
= mc_config_get_string_raw (cpath
, groupname
, "user", NULL
);
1037 element
->password
= mc_config_get_string_raw (cpath
, groupname
, "password", NULL
);
1038 element
->host
= mc_config_get_string_raw (cpath
, groupname
, "host", NULL
);
1039 element
->port
= mc_config_get_int (cpath
, groupname
, "port", 0);
1041 vpath
->path
= g_array_append_val (vpath
->path
, element
);
1047 mc_config_deinit (cpath
);
1048 if (vfs_path_elements_count (vpath
) == 0)
1050 vfs_path_free (vpath
);
1051 g_set_error (error
, MC_ERROR
, -1, "No any path elements found");
1058 /* --------------------------------------------------------------------------------------------- */
1060 * Build vfs_path_t object from arguments.
1062 * @param ... path tokens, terminated by NULL
1064 * @return newly allocated vfs_path_t object
1068 vfs_path_build_filename (const char *first_element
, ...)
1074 if (first_element
== NULL
)
1077 va_start (args
, first_element
);
1078 str_path
= mc_build_filenamev (first_element
, args
);
1080 vpath
= vfs_path_from_str (str_path
);
1085 /* --------------------------------------------------------------------------------------------- */
1087 * Append tokens to path object
1089 * @param vpath path object
1090 * @param ... NULL-terminated strings
1092 * @return newly allocated path object
1096 vfs_path_append_new (const vfs_path_t
* vpath
, const char *first_element
, ...)
1099 char *str_path
, *result_str
;
1100 vfs_path_t
*ret_vpath
;
1102 if (vpath
== NULL
|| first_element
== NULL
)
1105 va_start (args
, first_element
);
1106 str_path
= mc_build_filenamev (first_element
, args
);
1109 result_str
= vfs_path_to_str (vpath
);
1110 ret_vpath
= vfs_path_build_filename (result_str
, str_path
, NULL
);
1111 g_free (result_str
);
1118 /* --------------------------------------------------------------------------------------------- */
1121 * Append vpath_t tokens to path object
1123 * @param ... NULL-terminated vpath objects
1125 * @return newly allocated path object
1129 vfs_path_append_vpath_new (const vfs_path_t
* first_vpath
, ...)
1132 vfs_path_t
*ret_vpath
;
1133 const vfs_path_t
*current_vpath
= first_vpath
;
1135 if (first_vpath
== NULL
)
1138 ret_vpath
= vfs_path_new ();
1140 va_start (args
, first_vpath
);
1145 for (vindex
= 0; vindex
< vfs_path_elements_count (current_vpath
); vindex
++)
1147 vfs_path_element_t
*path_element
;
1150 vfs_path_element_clone (vfs_path_get_by_index (current_vpath
, vindex
));
1151 g_array_append_val (ret_vpath
->path
, path_element
);
1153 current_vpath
= va_arg (args
, const vfs_path_t
*);
1155 while (current_vpath
!= NULL
);
1161 /* --------------------------------------------------------------------------------------------- */
1163 * get tockens count in path.
1165 * @param vpath path object
1167 * @return count of tokens
1171 vfs_path_tokens_count (const vfs_path_t
* vpath
)
1173 size_t count_tokens
= 0;
1179 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1181 vfs_path_element_t
*element
;
1182 char **path_tokens
, **iterator
;
1184 element
= vfs_path_get_by_index (vpath
, element_index
);
1185 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1187 while (*iterator
!= NULL
)
1189 if (**iterator
!= '\0')
1193 g_strfreev (path_tokens
);
1195 return count_tokens
;
1198 /* --------------------------------------------------------------------------------------------- */
1200 * Get subpath by tokens
1202 * @param vpath path object
1203 * @param start_position first token for got/ Started from 0.
1204 * If negative, then position will be relative to end of path
1205 * @param length count of tokens
1207 * @return newly allocated string with path tokens separated by slash
1211 vfs_path_tokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, size_t length
)
1213 GString
*ret_tokens
, *element_tokens
;
1215 size_t tokens_count
= vfs_path_tokens_count (vpath
);
1221 length
= tokens_count
;
1223 if (start_position
< 0)
1224 start_position
= (ssize_t
) tokens_count
+ start_position
;
1226 if (start_position
< 0)
1229 if (start_position
>= (ssize_t
) tokens_count
)
1232 if (start_position
+ (ssize_t
) length
> (ssize_t
) tokens_count
)
1233 length
= tokens_count
- start_position
;
1235 ret_tokens
= g_string_sized_new (32);
1236 element_tokens
= g_string_sized_new (32);
1238 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1240 vfs_path_element_t
*element
;
1241 char **path_tokens
, **iterator
;
1243 g_string_assign (element_tokens
, "");
1244 element
= vfs_path_get_by_index (vpath
, element_index
);
1245 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1247 while (*iterator
!= NULL
)
1249 if (**iterator
!= '\0')
1251 if (start_position
== 0)
1255 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1256 g_string_free (element_tokens
, TRUE
);
1257 g_strfreev (path_tokens
);
1258 return g_string_free (ret_tokens
, FALSE
);
1261 if (element_tokens
->len
!= 0)
1262 g_string_append_c (element_tokens
, PATH_SEP
);
1263 g_string_append (element_tokens
, *iterator
);
1270 g_strfreev (path_tokens
);
1271 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1274 g_string_free (element_tokens
, TRUE
);
1275 return g_string_free (ret_tokens
, !(start_position
== 0 && length
== 0));
1278 /* --------------------------------------------------------------------------------------------- */
1280 * Get subpath by tokens
1282 * @param vpath path object
1283 * @param start_position first token for got/ Started from 0.
1284 * If negative, then position will be relative to end of path
1285 * @param length count of tokens
1287 * @return newly allocated path object with path tokens separated by slash
1291 vfs_path_vtokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, size_t length
)
1294 vfs_path_t
*ret_vpath
= NULL
;
1296 str_tokens
= vfs_path_tokens_get (vpath
, start_position
, length
);
1297 if (str_tokens
!= NULL
)
1299 ret_vpath
= vfs_path_from_str_flags (str_tokens
, VPF_NO_CANON
);
1300 g_free (str_tokens
);
1305 /* --------------------------------------------------------------------------------------------- */
1307 * Build URL parameters (such as user:pass@host:port) from one path element object
1309 * @param element path element
1311 * @return newly allocated string
1315 vfs_path_build_url_params_str (const vfs_path_element_t
* element
, gboolean keep_password
)
1319 if (element
== NULL
)
1322 buffer
= g_string_new ("");
1324 if (element
->user
!= NULL
)
1325 g_string_append (buffer
, element
->user
);
1327 if (element
->password
!= NULL
&& keep_password
)
1329 g_string_append_c (buffer
, ':');
1330 g_string_append (buffer
, element
->password
);
1333 if (element
->host
!= NULL
)
1335 if ((element
->user
!= NULL
) || (element
->password
!= NULL
))
1336 g_string_append_c (buffer
, '@');
1338 g_string_append_c (buffer
, '[');
1339 g_string_append (buffer
, element
->host
);
1341 g_string_append_c (buffer
, ']');
1344 if ((element
->port
) != 0 && (element
->host
!= NULL
))
1346 g_string_append_c (buffer
, ':');
1347 g_string_append_printf (buffer
, "%d", element
->port
);
1350 return g_string_free (buffer
, FALSE
);
1353 /* --------------------------------------------------------------------------------------------- */