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 /* --------------------------------------------------------------------------------------------- */
176 * Build URL parameters (such as user:pass@host:port) from one path element object
178 * @param element path element
180 * @return newly allocated string
184 vfs_path_build_url_params_str (vfs_path_element_t
* element
)
191 buffer
= g_string_new ("");
193 if (element
->user
!= NULL
)
194 g_string_append (buffer
, element
->user
);
196 if (element
->password
!= NULL
)
198 g_string_append_c (buffer
, ':');
199 g_string_append (buffer
, element
->password
);
202 if (element
->host
!= NULL
)
204 if ((element
->user
!= NULL
) || (element
->password
!= NULL
))
205 g_string_append_c (buffer
, '@');
207 g_string_append_c (buffer
, '[');
208 g_string_append (buffer
, element
->host
);
210 g_string_append_c (buffer
, ']');
213 if ((element
->port
) != 0 && (element
->host
!= NULL
))
215 g_string_append_c (buffer
, ':');
216 g_string_append_printf (buffer
, "%d", element
->port
);
219 return g_string_free (buffer
, FALSE
);
222 /* --------------------------------------------------------------------------------------------- */
223 /** get encoding after last #enc: or NULL, if part does not contain #enc:
227 * @return newly allocated string.
231 vfs_get_encoding (const char *path
)
237 work
= g_strdup (path
);
239 /* try found #enc: */
240 semi
= g_strrstr (work
, VFS_ENCODING_PREFIX
);
242 if (semi
!= NULL
&& (semi
== work
|| *(semi
- 1) == PATH_SEP
))
244 semi
+= strlen (VFS_ENCODING_PREFIX
); /* skip "#enc:" */
245 slash
= strchr (semi
, PATH_SEP
);
249 g_strlcpy (result
, semi
, sizeof (result
));
251 return g_strdup (result
);
260 /* --------------------------------------------------------------------------------------------- */
261 /** Extract the hostname and username from the path
263 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
265 * ftp://sunsite.unc.edu/pub/linux
266 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
267 * ftp://tsx-11.mit.edu:8192/
268 * ftp://joe@foo.edu:11321/private
269 * ftp://joe:password@foo.se
271 * @param path_element is an input string to be parsed
272 * @param path is an input string to be parsed
274 * @return g_malloc()ed url info.
275 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
276 * is not set, then the current login name is supplied.
277 * Return value is a g_malloc()ed structure with the pathname relative to the
282 vfs_path_url_split (vfs_path_element_t
* path_element
, const char *path
)
286 char *dir
, *colon
, *inner_colon
, *at
, *rest
;
288 path_element
->port
= 0;
290 pcopy
= g_strdup (path
);
291 pend
= pcopy
+ strlen (pcopy
);
294 /* search for any possible user */
295 at
= strrchr (pcopy
, '@');
297 /* We have a username */
303 inner_colon
= strchr (pcopy
, ':');
304 if (inner_colon
!= NULL
)
308 path_element
->password
= g_strdup (inner_colon
);
312 path_element
->user
= g_strdup (pcopy
);
320 /* Check if the host comes with a port spec, if so, chop it */
322 colon
= strchr (rest
, ':');
325 colon
= strchr (++rest
, ']');
331 path_element
->ipv6
= TRUE
;
338 if (sscanf (colon
+ 1, "%d", &path_element
->port
) == 1)
340 if (path_element
->port
<= 0 || path_element
->port
>= 65536)
341 path_element
->port
= 0;
344 while (*(++colon
) != '\0')
349 path_element
->port
= 1;
352 path_element
->port
= 2;
357 path_element
->host
= g_strdup (rest
);
361 /* --------------------------------------------------------------------------------------------- */
363 * get VFS class for the given name
365 * @param class_name name of class
367 * @return pointer to class structure or NULL if class not found
370 static struct vfs_class
*
371 vfs_get_class_by_name (const char *class_name
)
375 if (class_name
== NULL
)
378 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
380 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
381 if ((vfs
->name
!= NULL
) && (strcmp (vfs
->name
, class_name
) == 0))
388 /* --------------------------------------------------------------------------------------------- */
390 * Check if path string contain URL-like elements
392 * @param path_str path
394 * @return TRUE if path is deprecated or FALSE otherwise
398 vfs_path_is_str_path_deprecated (const char *path_str
)
400 return strstr (path_str
, VFS_PATH_URL_DELIMITER
) == NULL
;
403 /* --------------------------------------------------------------------------------------------- */
404 /** Split path string to path elements by deprecated algorithm.
406 * @param path_str VFS-path
408 * @return pointer to newly created vfs_path_t object with filled path elements array.
412 vfs_path_from_str_deprecated_parser (char *path
)
415 vfs_path_element_t
*element
;
416 struct vfs_class
*class;
417 const char *local
, *op
;
419 vpath
= vfs_path_new ();
421 while ((class = _vfs_split_with_semi_skip_count (path
, &local
, &op
, 0)) != NULL
)
424 element
= g_new0 (vfs_path_element_t
, 1);
425 element
->class = class;
428 element
->path
= vfs_translate_path_n (local
);
430 element
->encoding
= vfs_get_encoding (local
);
431 element
->dir
.converter
=
432 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
434 url_params
= strchr (op
, ':'); /* skip VFS prefix */
435 if (url_params
!= NULL
)
439 vfs_path_url_split (element
, url_params
);
443 element
->vfs_prefix
= g_strdup (op
);
445 vpath
->path
= g_list_prepend (vpath
->path
, element
);
449 element
= g_new0 (vfs_path_element_t
, 1);
450 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
451 element
->path
= vfs_translate_path_n (path
);
453 element
->encoding
= vfs_get_encoding (path
);
454 element
->dir
.converter
=
455 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
456 vpath
->path
= g_list_prepend (vpath
->path
, element
);
462 /* --------------------------------------------------------------------------------------------- */
463 /** Split path string to path elements by URL algorithm.
465 * @param path_str VFS-path
467 * @return pointer to newly created vfs_path_t object with filled path elements array.
471 vfs_path_from_str_uri_parser (char *path
)
474 vfs_path_element_t
*element
;
478 vpath
= vfs_path_new ();
480 while ((url_delimiter
= g_strrstr (path
, VFS_PATH_URL_DELIMITER
)) != NULL
)
482 char *vfs_prefix_start
;
483 char *real_vfs_prefix_start
= url_delimiter
;
485 struct vfs_s_subclass
*sub
= NULL
;
487 while (real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) != PATH_SEP
)
488 real_vfs_prefix_start
--;
489 vfs_prefix_start
= real_vfs_prefix_start
;
491 if (*(vfs_prefix_start
) == PATH_SEP
)
492 vfs_prefix_start
+= 1;
494 *url_delimiter
= '\0';
496 element
= g_new0 (vfs_path_element_t
, 1);
497 element
->class = vfs_prefix_to_class (vfs_prefix_start
);
498 element
->vfs_prefix
= g_strdup (vfs_prefix_start
);
500 url_delimiter
+= strlen (VFS_PATH_URL_DELIMITER
);
501 sub
= VFSDATA (element
);
502 if (sub
!= NULL
&& sub
->flags
& VFS_S_REMOTE
)
504 slash_pointer
= strchr (url_delimiter
, PATH_SEP
);
505 if (slash_pointer
== NULL
)
507 element
->path
= g_strdup ("");
511 element
->path
= vfs_translate_path_n (slash_pointer
+ 1);
512 element
->encoding
= vfs_get_encoding (slash_pointer
);
514 *slash_pointer
= '\0';
516 vfs_path_url_split (element
, url_delimiter
);
520 element
->path
= vfs_translate_path_n (url_delimiter
);
521 element
->encoding
= vfs_get_encoding (url_delimiter
);
523 element
->dir
.converter
=
524 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
525 vpath
->path
= g_list_prepend (vpath
->path
, element
);
527 if ((real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) == PATH_SEP
) ||
528 (real_vfs_prefix_start
== path
&& *(real_vfs_prefix_start
) != PATH_SEP
))
529 *real_vfs_prefix_start
= '\0';
531 *(real_vfs_prefix_start
+ 1) = '\0';
536 element
= g_new0 (vfs_path_element_t
, 1);
537 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
538 element
->path
= vfs_translate_path_n (path
);
539 element
->encoding
= vfs_get_encoding (path
);
540 element
->dir
.converter
=
541 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
542 vpath
->path
= g_list_prepend (vpath
->path
, element
);
548 /* --------------------------------------------------------------------------------------------- */
550 * Add element's class info to result string (such as VFS name, host, encoding etc)
551 * This function used as helper only in vfs_path_tokens_get() function
553 * @param element current path element
554 * @param ret_tokens total tikens for return
555 * @param element_tokens accumulated element-only tokens
559 vfs_path_tokens_add_class_info (vfs_path_element_t
* element
, GString
* ret_tokens
,
560 GString
* element_tokens
)
562 if (((element
->class->flags
& VFSF_LOCAL
) == 0 || ret_tokens
->len
> 0)
563 && element_tokens
->len
> 0)
567 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
568 g_string_append_c (ret_tokens
, PATH_SEP
);
570 g_string_append (ret_tokens
, element
->vfs_prefix
);
571 g_string_append (ret_tokens
, VFS_PATH_URL_DELIMITER
);
573 url_str
= vfs_path_build_url_params_str (element
);
574 if (*url_str
!= '\0')
576 g_string_append (ret_tokens
, url_str
);
577 g_string_append_c (ret_tokens
, PATH_SEP
);
582 if (element
->encoding
!= NULL
)
584 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
585 g_string_append (ret_tokens
, PATH_SEP_STR
);
586 g_string_append (ret_tokens
, VFS_ENCODING_PREFIX
);
587 g_string_append (ret_tokens
, element
->encoding
);
588 g_string_append (ret_tokens
, PATH_SEP_STR
);
591 g_string_append (ret_tokens
, element_tokens
->str
);
594 /* --------------------------------------------------------------------------------------------- */
595 /*** public functions ****************************************************************************/
596 /* --------------------------------------------------------------------------------------------- */
598 * Convert first elements_count elements from vfs_path_t to string representation.
600 * @param vpath pointer to vfs_path_t object
601 * @param elements_count count of first elements for convert
602 * @param flags flags for parser
604 * @return pointer to newly created string.
607 #define vfs_append_from_path(appendfrom) \
609 if ((*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
610 && (buffer->str[buffer->len - 1] != PATH_SEP)) \
611 g_string_append_c (buffer, PATH_SEP); \
612 g_string_append (buffer, appendfrom); \
616 vfs_path_to_str_elements_count (const vfs_path_t
* vpath
, int elements_count
)
620 GString
*recode_buffer
;
625 if (elements_count
> vfs_path_elements_count (vpath
))
626 elements_count
= vfs_path_elements_count (vpath
);
628 if (elements_count
< 0)
629 elements_count
= vfs_path_elements_count (vpath
) + elements_count
;
631 buffer
= g_string_new ("");
632 recode_buffer
= g_string_new ("");
634 for (element_index
= 0; element_index
< elements_count
; element_index
++)
636 vfs_path_element_t
*element
= vfs_path_get_by_index (vpath
, element_index
);
638 if (element
->vfs_prefix
!= NULL
)
642 if (buffer
->len
== 0 || buffer
->str
[buffer
->len
- 1] != PATH_SEP
)
643 g_string_append_c (buffer
, PATH_SEP
);
645 g_string_append (buffer
, element
->vfs_prefix
);
646 g_string_append (buffer
, VFS_PATH_URL_DELIMITER
);
648 url_str
= vfs_path_build_url_params_str (element
);
649 if (*url_str
!= '\0')
650 g_string_append (buffer
, url_str
);
655 if (vfs_path_element_need_cleanup_converter (element
))
657 if (buffer
->str
[buffer
->len
- 1] != PATH_SEP
)
658 g_string_append (buffer
, PATH_SEP_STR
);
659 g_string_append (buffer
, VFS_ENCODING_PREFIX
);
660 g_string_append (buffer
, element
->encoding
);
661 str_vfs_convert_from (element
->dir
.converter
, element
->path
, recode_buffer
);
662 vfs_append_from_path (recode_buffer
->str
);
663 g_string_set_size (recode_buffer
, 0);
667 vfs_append_from_path (element
->path
);
670 g_string_free (recode_buffer
, TRUE
);
671 return g_string_free (buffer
, FALSE
);
674 #undef vfs_append_from_path
676 /* --------------------------------------------------------------------------------------------- */
678 * Convert vfs_path_t to string representation.
680 * @param vpath pointer to vfs_path_t object
682 * @return pointer to newly created string.
686 vfs_path_to_str (const vfs_path_t
* vpath
)
688 return vfs_path_to_str_elements_count (vpath
, vfs_path_elements_count (vpath
));
691 /* --------------------------------------------------------------------------------------------- */
693 * Split path string to path elements with flags for change parce process.
695 * @param path_str VFS-path
696 * @param flags flags for parser
698 * @return pointer to newly created vfs_path_t object with filled path elements array.
702 vfs_path_from_str_flags (const char *path_str
, vfs_path_flag_t flags
)
707 if (path_str
== NULL
)
710 if ((flags
& VPF_NO_CANON
) == 0)
711 path
= vfs_canon (path_str
);
713 path
= g_strdup (path_str
);
718 if ((flags
& VPF_USE_DEPRECATED_PARSER
) != 0 && vfs_path_is_str_path_deprecated (path
))
719 vpath
= vfs_path_from_str_deprecated_parser (path
);
721 vpath
= vfs_path_from_str_uri_parser (path
);
728 /* --------------------------------------------------------------------------------------------- */
730 * Split path string to path elements.
732 * @param path_str VFS-path
734 * @return pointer to newly created vfs_path_t object with filled path elements array.
738 vfs_path_from_str (const char *path_str
)
740 return vfs_path_from_str_flags (path_str
, VPF_NONE
);
743 /* --------------------------------------------------------------------------------------------- */
745 * Create new vfs_path_t object.
747 * @return pointer to newly created vfs_path_t object.
754 vpath
= g_new0 (vfs_path_t
, 1);
758 /* --------------------------------------------------------------------------------------------- */
760 * Get count of path elements.
762 * @param vpath pointer to vfs_path_t object
764 * @return count of path elements.
768 vfs_path_elements_count (const vfs_path_t
* vpath
)
770 return (vpath
!= NULL
&& vpath
->path
!= NULL
) ? g_list_length (vpath
->path
) : 0;
773 /* --------------------------------------------------------------------------------------------- */
775 * Get one path element by index.
777 * @param vpath pointer to vfs_path_t object
778 * @param element_index element index. May have negative value (in this case count was started at the end of list).
780 * @return path element.
784 vfs_path_get_by_index (const vfs_path_t
* vpath
, int element_index
)
786 if (element_index
< 0)
787 element_index
+= vfs_path_elements_count (vpath
);
789 if (element_index
< 0)
790 vfs_die ("vfs_path_get_by_index: incorrect index!");
792 return g_list_nth_data (vpath
->path
, element_index
);
795 /* --------------------------------------------------------------------------------------------- */
797 * Clone one path element
799 * @param element pointer to vfs_path_element_t object
801 * @return Newly allocated path element
805 vfs_path_element_clone (const vfs_path_element_t
* element
)
807 vfs_path_element_t
*new_element
= g_new0 (vfs_path_element_t
, 1);
808 memcpy (new_element
, element
, sizeof (vfs_path_element_t
));
810 new_element
->user
= g_strdup (element
->user
);
811 new_element
->password
= g_strdup (element
->password
);
812 new_element
->host
= g_strdup (element
->host
);
813 new_element
->path
= g_strdup (element
->path
);
814 new_element
->encoding
= g_strdup (element
->encoding
);
815 if (vfs_path_element_need_cleanup_converter (element
) && new_element
->encoding
!= NULL
)
816 new_element
->dir
.converter
= str_crt_conv_from (new_element
->encoding
);
817 new_element
->vfs_prefix
= g_strdup (element
->vfs_prefix
);
822 /* --------------------------------------------------------------------------------------------- */
824 * Free one path element.
826 * @param element pointer to vfs_path_element_t object
831 vfs_path_element_free (vfs_path_element_t
* element
)
836 g_free (element
->user
);
837 g_free (element
->password
);
838 g_free (element
->host
);
839 g_free (element
->path
);
840 g_free (element
->encoding
);
841 g_free (element
->vfs_prefix
);
843 if (vfs_path_element_need_cleanup_converter (element
))
845 str_close_conv (element
->dir
.converter
);
851 /* --------------------------------------------------------------------------------------------- */
855 * @param vpath pointer to vfs_path_t object
857 * @return Newly allocated path object
861 vfs_path_clone (const vfs_path_t
* vpath
)
863 vfs_path_t
*new_vpath
;
864 int vpath_element_index
;
868 new_vpath
= vfs_path_new ();
869 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
870 vpath_element_index
++)
873 g_list_append (new_vpath
->path
,
874 vfs_path_element_clone (vfs_path_get_by_index
875 (vpath
, vpath_element_index
)));
881 /* --------------------------------------------------------------------------------------------- */
883 * Free vfs_path_t object.
885 * @param vpath pointer to vfs_path_t object
890 vfs_path_free (vfs_path_t
* path
)
894 g_list_foreach (path
->path
, (GFunc
) vfs_path_element_free
, NULL
);
895 g_list_free (path
->path
);
899 /* --------------------------------------------------------------------------------------------- */
901 * Remove one path element by index
903 * @param vpath pointer to vfs_path_t object
904 * @param element_index element index. May have negative value (in this case count was started at the end of list).
909 vfs_path_remove_element_by_index (vfs_path_t
* vpath
, int element_index
)
911 vfs_path_element_t
*element
;
913 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 1))
916 if (element_index
< 0)
917 element_index
= vfs_path_elements_count (vpath
) + element_index
;
919 element
= g_list_nth_data (vpath
->path
, element_index
);
920 vpath
->path
= g_list_remove (vpath
->path
, element
);
921 vfs_path_element_free (element
);
924 /* --------------------------------------------------------------------------------------------- */
925 /** Return VFS class for the given prefix */
928 vfs_prefix_to_class (const char *prefix
)
932 /* Avoid first class (localfs) that would accept any prefix */
933 for (i
= 1; i
< vfs__classes_list
->len
; i
++)
935 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
936 if (vfs
->which
!= NULL
)
938 if (vfs
->which (vfs
, prefix
) == -1)
943 if (vfs
->prefix
!= NULL
&& strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)) == 0)
950 /* --------------------------------------------------------------------------------------------- */
952 * Check if need cleanup charset converter for vfs_path_element_t
954 * @param element part of path
956 * @return TRUE if need cleanup converter or FALSE otherwise
960 vfs_path_element_need_cleanup_converter (const vfs_path_element_t
* element
)
962 return (element
->dir
.converter
!= str_cnv_from_term
&& element
->dir
.converter
!= INVALID_CONV
);
965 /* --------------------------------------------------------------------------------------------- */
967 * Serialize vfs_path_t object to string
969 * @param vpath data for serialization
970 * @param error contain pointer to object for handle error code and message
972 * @return serialized vpath as newly allocated string
976 vfs_path_serialize (const vfs_path_t
* vpath
, GError
** error
)
978 mc_config_t
*cpath
= mc_config_init (NULL
);
979 ssize_t element_index
;
982 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 0))
984 g_set_error (error
, MC_ERROR
, -1, "vpath object is empty");
988 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
990 char *groupname
= g_strdup_printf ("path-element-%zd", element_index
);
991 vfs_path_element_t
*element
= vfs_path_get_by_index (vpath
, element_index
);
993 /* convert one element to config group */
995 mc_config_set_string_raw (cpath
, groupname
, "path", element
->path
);
996 mc_config_set_string_raw (cpath
, groupname
, "class-name", element
->class->name
);
997 mc_config_set_string_raw (cpath
, groupname
, "encoding", element
->encoding
);
999 mc_config_set_string_raw (cpath
, groupname
, "vfs_prefix", element
->vfs_prefix
);
1001 mc_config_set_string_raw (cpath
, groupname
, "user", element
->user
);
1002 mc_config_set_string_raw (cpath
, groupname
, "password", element
->password
);
1003 mc_config_set_string_raw (cpath
, groupname
, "host", element
->host
);
1004 if (element
->port
!= 0)
1005 mc_config_set_int (cpath
, groupname
, "port", element
->port
);
1010 ret_value
= mc_serialize_config (cpath
, error
);
1011 mc_config_deinit (cpath
);
1015 /* --------------------------------------------------------------------------------------------- */
1017 * Deserialize string to vfs_path_t object
1019 * @param data data for serialization
1020 * @param error contain pointer to object for handle error code and message
1022 * @return newly allocated vfs_path_t object
1026 vfs_path_deserialize (const char *data
, GError
** error
)
1028 mc_config_t
*cpath
= mc_deserialize_config (data
, error
);
1029 size_t element_index
= 0;
1035 vpath
= vfs_path_new ();
1039 vfs_path_element_t
*element
;
1043 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1044 if (!mc_config_has_group (cpath
, groupname
))
1050 element
= g_new0 (vfs_path_element_t
, 1);
1052 cfg_value
= mc_config_get_string_raw (cpath
, groupname
, "class-name", NULL
);
1053 element
->class = vfs_get_class_by_name (cfg_value
);
1054 if (element
->class == NULL
)
1057 vfs_path_free (vpath
);
1058 g_set_error (error
, MC_ERROR
, -1, "Unable to find VFS class by name '%s'", cfg_value
);
1060 mc_config_deinit (cpath
);
1065 element
->path
= mc_config_get_string_raw (cpath
, groupname
, "path", NULL
);
1066 element
->encoding
= mc_config_get_string_raw (cpath
, groupname
, "encoding", NULL
);
1067 element
->dir
.converter
=
1068 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
1070 element
->vfs_prefix
= mc_config_get_string_raw (cpath
, groupname
, "vfs_prefix", NULL
);
1072 element
->user
= mc_config_get_string_raw (cpath
, groupname
, "user", NULL
);
1073 element
->password
= mc_config_get_string_raw (cpath
, groupname
, "password", NULL
);
1074 element
->host
= mc_config_get_string_raw (cpath
, groupname
, "host", NULL
);
1075 element
->port
= mc_config_get_int (cpath
, groupname
, "port", 0);
1077 vpath
->path
= g_list_append (vpath
->path
, element
);
1083 mc_config_deinit (cpath
);
1084 if (vfs_path_elements_count (vpath
) == 0)
1086 vfs_path_free (vpath
);
1087 g_set_error (error
, MC_ERROR
, -1, "No any path elements found");
1094 /* --------------------------------------------------------------------------------------------- */
1096 * Build vfs_path_t object from arguments.
1098 * @param ... path tokens, terminated by NULL
1100 * @return newly allocated vfs_path_t object
1104 vfs_path_build_filename (const char *first_element
, ...)
1110 if (first_element
== NULL
)
1113 va_start (args
, first_element
);
1114 str_path
= mc_build_filenamev (first_element
, args
);
1116 vpath
= vfs_path_from_str (str_path
);
1121 /* --------------------------------------------------------------------------------------------- */
1123 * Append tokens to path object
1125 * @param vpath path object
1126 * @param ... NULL-terminated strings
1128 * @return newly allocated path object
1132 vfs_path_append_new (const vfs_path_t
* vpath
, const char *first_element
, ...)
1135 char *str_path
, *result_str
;
1136 vfs_path_t
*ret_vpath
;
1138 if (vpath
== NULL
|| first_element
== NULL
)
1141 va_start (args
, first_element
);
1142 str_path
= mc_build_filenamev (first_element
, args
);
1145 result_str
= vfs_path_to_str (vpath
);
1146 ret_vpath
= vfs_path_build_filename (result_str
, str_path
, NULL
);
1147 g_free (result_str
);
1154 /* --------------------------------------------------------------------------------------------- */
1156 * get tockens count in path.
1158 * @param vpath path object
1160 * @return count of tokens
1164 vfs_path_tokens_count (const vfs_path_t
* vpath
)
1166 size_t count_tokens
= 0;
1172 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1174 vfs_path_element_t
*element
;
1175 char **path_tokens
, **iterator
;
1177 element
= vfs_path_get_by_index (vpath
, element_index
);
1178 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1180 while (*iterator
!= NULL
)
1182 if (**iterator
!= '\0')
1186 g_strfreev (path_tokens
);
1188 return count_tokens
;
1191 /* --------------------------------------------------------------------------------------------- */
1193 * Get subpath by tokens
1195 * @param vpath path object
1196 * @param start_position first token for got/ Started from 0.
1197 * If negative, then position will be relative to end of path
1198 * @param length count of tokens
1200 * @return newly allocated string with path tokens separated by slash
1204 vfs_path_tokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, size_t length
)
1206 GString
*ret_tokens
, *element_tokens
;
1208 size_t tokens_count
= vfs_path_tokens_count (vpath
);
1211 length
= tokens_count
;
1213 if (start_position
< 0)
1214 start_position
= (ssize_t
) tokens_count
+ start_position
;
1216 if (start_position
< 0)
1219 if (start_position
>= (ssize_t
) tokens_count
)
1222 if (start_position
+ (ssize_t
) length
> (ssize_t
) tokens_count
)
1223 length
= tokens_count
- start_position
;
1225 ret_tokens
= g_string_sized_new (32);
1226 element_tokens
= g_string_sized_new (32);
1228 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1230 vfs_path_element_t
*element
;
1231 char **path_tokens
, **iterator
;
1233 g_string_assign (element_tokens
, "");
1234 element
= vfs_path_get_by_index (vpath
, element_index
);
1235 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1237 while (*iterator
!= NULL
)
1239 if (**iterator
!= '\0')
1241 if (start_position
== 0)
1245 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1246 g_string_free (element_tokens
, TRUE
);
1247 g_strfreev (path_tokens
);
1248 return g_string_free (ret_tokens
, FALSE
);
1251 if (element_tokens
->len
!= 0)
1252 g_string_append_c (element_tokens
, PATH_SEP
);
1253 g_string_append (element_tokens
, *iterator
);
1260 g_strfreev (path_tokens
);
1261 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1264 g_string_free (element_tokens
, TRUE
);
1265 return g_string_free (ret_tokens
, !(start_position
== 0 && length
== 0));
1268 /* --------------------------------------------------------------------------------------------- */